//<summary>Get Cookie value by cookie name</summary>
//<paramref name="Name">The name of the cookie</paramref>
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))      
		}    
	}
}

//<summary>Set cookie (override the name if the name already exists</summary>
//<paramref name="cookieName">Name of the cookie</paramref>
//<paramref name="cookieValue"></paramref>
//<paramref name="nDays">if 0 or null, the cookie will be valid for 1 year</paramref>
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();
}

//<summary>When changing language call this function, it change the cookie and the query string</summary>
//<paramref name="newLangCode">new lc (for example: en,he,fr...)</paramref>
function changeLanguageCodeAndRedirect(newLangCode)
{
	strQueryString	= document.location.search;
	if(strQueryString.charAt(0) == '?')
		strQueryString	= strQueryString.substring(1,strQueryString.length);

	var strItem = '';
	var strNewQueryString = '';
	var strPath = window.location.pathname;
	var strPageName = strPath.substring(strPath.lastIndexOf('/') + 1);

	strNewQueryString	= generateQueryStringWithLC(strQueryString,newLangCode);
	
	ProdSetCookie('lc', newLangCode);	
	top.location.href	= getUrlByLc(window.location.protocol + "//" + window.location.hostname + strNewQueryString,strNewQueryString,newLangCode);
}

//<summary>Helper function for changeLanguageCodeAndRedirect, add the lc parameter to the query string</summary>
//<paramref name="strQueryString">Old query string</paramref>
//<paramref name="newLangCode">new Language code</paramref>
function generateQueryStringWithLC(strQueryString,newLangCode)
{
	var strNewQueryString = "";
	if (strQueryString != ''){
		var arrQueryString = strQueryString.split('&');
		for (var i=0; i<arrQueryString.length; i++) 
		{
			strItem = arrQueryString[i];
			if (strItem.substring(0,2) != 'lc')
				strNewQueryString = (strNewQueryString == '')? '?' + strItem : strNewQueryString + '&' + strItem;
		}	
		strNewQueryString = (strNewQueryString == '')? '?lc=' + newLangCode : strNewQueryString + '&lc=' + newLangCode;
	} 
	else 
	{
		strNewQueryString = '?lc=' + newLangCode;
	}
	return strNewQueryString;
}

//Get the query string parameters as a hashtable
//<summary></summary>
//<paramref name=""></paramref>
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.
}

//<summary>Returns the system default language code (executes for new users - detect their default language)<summary>
function getBrowserLanguage()
{
	//The language defined in Control Panel -> Region and Language option -> Advanced -> Language for non-unicode programs.
	if(document.clientInformation && document.clientInformation.systemLanguage)
	{
		return document.clientInformation.systemLanguage.substr(0,2);
	}

	//The language defined in Control Panel -> Region and Language option -> Advanced -> Language for non-unicode programs.
	if(navigator && navigator.systemLanguage)
	{
		return navigator.systemLanguage.substr(0,2);
	}
	if(window.navigator.language) //support firefox
		return window.navigator.language.substr(0,2);

	//If javascript can't get the system language code return default (en).
	return "en";
}

//<summary>Force the query string and the cookie to contain valid language code
//In order to run this function, import this JS to your page:
//<script type="text/javascript" src="/skin/LangCodeFunctions.js"></script>
//</summary>
function forceLanguageCodeExistance()
{
	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 == 'undefined')||(newlc == undefined)||(newlc == null||(newlc == 'null'))) 
		{
			//first time user enter the site, get his default language
			newlc = getBrowserLanguage();
		}
	
		//check if the default systemLanguage is valid
		var siteUrl	= window.location.href;
		if(isValidLC(newlc))
		{
			siteUrl	= getUrlByLc(siteUrl,window.location.search,newlc);
		}
		else
		{
			//LC is not valid, set lc to default (en).
			newlc	= "en";
			siteUrl	= getUrlByLc(siteUrl,window.location.search,newlc);
		}
		ProdSetCookie("lc",newlc,null);
			
		//Concatenate the language to the query-string
		if (location.search.substring(1) == null || location.search.substring(1) == '') 
			window.location.replace(siteUrl + '?lc=' + newlc);
		else 
			window.location.replace(siteUrl + '&lc=' + newlc);
	}
	else
	{
		ProdSetCookie("lc",queryArgs['lc'],null);
	}
}
//<summary>Check if the site supports the selected language code (checks it by lc2baseUrl array)</summary>
//<paramref name="newlc">current lc</paramref>
function isValidLC(newlc)
{
	return (lc2baseUrl && lc2baseUrl[newlc]);
}
//<summary>Return the url according to the language code, selects the domain accoring to the information in the lc2baseUrl</summary>
//<paramref name="siteUrl">Current site url, if lc2baseUrl[newlc] is empty, return this Url</paramref>
//<paramref name="siteUrlSearch">All url information after the ?. if lc2baseUrl[newlc] is not empty, returns the domain of newlc + this parameter</paramref>
//<paramref name="newlc">Selected lc</paramref>
//<Example>getUrlByLc("http://games.friendster.com?lc=en",?lc=en,en)</Example>
function getUrlByLc(siteUrl,siteUrlSearch,newlc)
{
	if(lc2baseUrl && lc2baseUrl[newlc])
	{
		return lc2baseUrl[newlc] + window.location.pathname + siteUrlSearch;
	}
	else
	{
		return siteUrl;
	}
}

