/**
Redirects the page to the appropriate country, based on the selected country and language.
Also responsible for setting the cookies.
*/
function GoToCountry(countrySelect,languageSelect,checkbox)
{
//this._forwardUrl=(typeof(forwardUrl)=="string") ? document.getElementById(forwardUrl):forwardUrl;
this._countrySelector=(typeof(countrySelect)=="string") ? document.getElementById(countrySelect) : countrySelect;
this._languageSelector = (typeof(languageSelect) == "string") ? document.getElementById(languageSelect) : languageSelect;
this._checkbox=(typeof(checkbox) == "string") ? document.getElementById(checkbox) : checkbox;

		var myindex = this._languageSelector.selectedIndex;
		var myLanguageValue = this._languageSelector.options[myindex].value;
		var myLanguageArray = myLanguageValue.split("|");
		var myLanguage = myLanguageArray[0];
		var myURL = myLanguageArray[1];

		if (myLanguageValue == "-1"){
			alert("please select a country/region");
		}
		else
		{
			if(this._checkbox.checked)
			{
				//myURL = this._forwardUrl.value;
				//myURL = myURL.toLowerCase();
				//if (myURL.indexOf("direct.motorola.com") != -1){
					CallSetCookie();
				//}
			}
			document.location.href = myURL;
		}
}

/* CallSetCookie, set the cookie */
function CallSetCookie()
{
	var myDate = new Date();
	var myindex = this._languageSelector.selectedIndex;
	
	var myLanguageValue = this._languageSelector.options[myindex].value;
	var myindexCountry = this._countrySelector.selectedIndex;
	if (myLanguageValue == "-1")
	return;
	
	 var mycountry = this._countrySelector.options[myindexCountry].value;
    
    if (mycountry == "-1")
	return;
	
	var namesArray = [ 'BusinessLocale', 'BusinessLocaleURL'];
	var valuesArray = myLanguageValue.split( '|');
	var days = 365;
	createCookie( 'BusinessLocale', valuesArray[0], days);
	createCookie( 'BusinessLocaleURL', valuesArray[1], days);
	createCookie( 'countryselect', mycountry, days);
}

function createCookie( name, value, days) {
	
	var expires = "";
	 if ( days) {
	     var date = new Date();
	     date.setTime( date.getTime() + ( days*24*60*60*1000));
	     var expires = "; expires=" + date.toGMTString();
	 }
	 
	 var cookieValue = name + "=" + value + expires + "; path=/";
     document.cookie = cookieValue;	 
    
}

/* getCookieVal return the cookie value */

function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) {
endstr = document.cookie.length;
}
return unescape(document.cookie.substring(offset, endstr));
}

/* Set the cookie */
function SetCookie (name,value, expires)
{
	var myString = ""
	myString = name+"="+escape (value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + "; path = /" ;
	//alert (myString);

	document.cookie =  myString;
}


/* GetCookie return the cookie name */

function GetCookie (name)
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen)
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			{
				return getCookieVal (j);
			}
			i = document.cookie.indexOf(" ", i) + 1;
			if (i == 0)
			{
				break;
			}
	}
	return null;
}

/* Redirects the page */
function DoRedirect()
{
	var myLanguageValue = (GetCookie("BusinessLocale") == null) ? "" : GetCookie("BusinessLocale");
	if (myLanguageValue.length == 0)
		{//alert ("In Valid Redirect");

		}
		else
		{//alert ("Valid Redirect");
			var myLanguageArray = myLanguageValue.split("|");
			var myLanguage = myLanguageArray[0];
			var myUrl = myLanguageArray[1];
			location.href =  myUrl; //Countryobj[myCountry];
		}
}