
function ProdGetCookie(Name) {   
	var search = Name + "="   
	if (document.cookie.length > 0) { // if there are any cookies 
	     offset = document.cookie.indexOf(search)       
		 if (offset != -1) { // if cookie exists          
			 offset += search.length          
			 // set index of beginning of value 
			 end = document.cookie.indexOf(";", offset)          
			 // set index of end of cookie value         
			 if (end == -1)             
			 	end = document.cookie.length         
			return unescape(document.cookie.substring(offset, end))      
		}    
	}
}


function ProdSetCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=365;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}


function ProdChangeLang(newLangCode,strQueryString,strQueryStringLanguageCode)
{
	var blnStaging = false;
	var strItem = '';
	var strNewQueryString = '';
	var strPath = window.location.pathname;
	var strPageName = strPath.substring(strPath.lastIndexOf('/') + 1);
	
	if (strPageName == ''){strPageName = 'index.htm'};

	if (strQueryString != ''){
		var arrQueryString = strQueryString.split('&');
		for (var i=0; i<arrQueryString.length; i++) {
			strItem = arrQueryString[i];
			if (strItem.substring(0,2) != 'lc'){
				if (strNewQueryString == ''){
					strNewQueryString = '?' + strItem;
				} else {
					strNewQueryString = strNewQueryString + '&' + strItem;
				};
			};
		};
		if (strNewQueryString == ''){
			strNewQueryString = '?lc=' + newLangCode;
		} else {
			strNewQueryString = strNewQueryString + '&lc=' + newLangCode;
		};
	} else {
		strNewQueryString = '?lc=' + newLangCode;
	};
	
	var currLang = '<%= Session("langCode") %>';
	
	if (blnStaging) {
		var enUrl = strPageName;
		var olUrl = strPageName;
	} else {
		var enUrl = 'http://www.fatcatgames.com/' + strPageName;
		var olUrl = 'http://intl.fatcatgames.com/' + strPageName;		
	};

	ProdSetCookie('lc', newLangCode);
	
	if (newLangCode == 'en'){
		top.location.href = enUrl + strNewQueryString;
	} else {
		top.location.href = olUrl + strNewQueryString;
	};
}


//Get the query string parameters as a hashtable
function ProdGetArgs() {
    var args = new Object();
    var query = unescape(location.search.substring(1));  // Get query string.
    var pairs = query.split("&");              // Break at comma.
    for(var i = 0; i < pairs.length; i++) {
	var pos = pairs[i].indexOf('=');       // Look for "name=value".
	if (pos == -1) continue;               // If not found, skip.
	var argname = pairs[i].substring(0,pos);  // Extract the name.
	var value = pairs[i].substring(pos+1); // Extract the value.
	args[argname] = unescape(value);          // Store as a property.
    }
    return args;                               // Return the object.
}



var queryArgs = ProdGetArgs();

//If the query-string doesn't contain the lc parameter, the language may be not as expected
if (queryArgs['lc'] == '' || queryArgs['lc'] == null) {
  var newlc = ProdGetCookie('lc');
  if (newlc == '') {
    newlc = 'en'
  }
  
  //Concatenate the language to the query-string
  if (location.search.substring(1) == null || location.search.substring(1) == '') {
    window.location.href=window.location.href + '?lc=' + newlc;
  }
  else {
    window.location.href=window.location.href + '&lc=' + newlc;
  }  
}

