/*Depending on the region, passed as a parameter in the URL or 
by cookie it will redirect the member to the correct eLerts app*/
function loadPage () { 
	
	switch (readCookie('elertRegionv1')){
		case "LA":
			location.href="./es/";
			break;
		case "BR":
			location.href="./pt/";
			break;
		case "US":
			location.href="http://app.rci.com/elert/login.cfm";
			break;
		case "UK":
			location.href="http://f.chtah.com/s/2/696983339/Loginpage.html";
			break;
		case "FR":
			location.href="http://f.chtah.com/s/2/696983339/LoginpageFR.html";
			break;
		case "GR":
			location.href="http://f.chtah.com/s/2/696983339/LoginpageGR.html";
			break;
		case "IT":
			location.href="http://f.chtah.com/s/2/696983339/LoginpageIT.html";
			break;
		case "SP":
			location.href="http://f.chtah.com/s/2/696983339/LoginpageSP.html";
			break;
	}	
} 	

/* A locale parameter can be passed through the URL as
www.rci.com/elerts/index.html?locale=US and the page will
stablish that cookie and redirect the member to the selected
locale.*/
function readParam(){
	var qsParm = new Array();
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	qsParm['locale'] = null;
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			qsParm[key] = val;
		}
	}
	return qsParm['locale'];
}

/*Establishes the cookie passed by URL or clicking on the map*/
function selRegion(idRegion){
		SetCookie('elertRegionv1', idRegion);
		loadPage();
}



/************* COOKIES FUNCTIONS ****************/
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i<ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function SetCookie(cookieName,cookieValue) {
	deleteCookie(cookieName);
	var today = new Date();
	var expire = new Date();
	var nDays = 365;
	if (nDays==null || nDays==0) nDays=1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}

function deleteCookie (cookie_name) {
  var cookie_date = new Date();  // current date & time
  cookie_date.setTime( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();	
}

