/*MLM*/

var naturalSites=new Array("google.com","yahoo.com","msn.com",
			   "altavista.com","hispavista.com",
			   "aol.com.mx","yahoo.net","dmoz.org", "todito.com", "terra.com.mx");

var naturalPmsIds=new Array(78963944,78964053,78971833,78979790,
			   78971842,78971848,78979805,78964076, 79867016, 79852678);

var naturalPmsSiteIds=new Array(305851,305890,305914,305869,305852,
				305853,305871,305934, 309557, 309559);

function nsGetCookieValue(name) {
	var start=document.cookie.indexOf(name+"=");
	var len=start+name.length+1;
	if (start == -1) 
		return null;
	var end=document.cookie.indexOf(";",len);
	if (end==-1) 
		end=document.cookie.length;
	return unescape(document.cookie.substring(len,end));
}


function performNaturalCheck(referrer, currentUrl){
	//document.write("<BR>Iniciando identificación para:["+referrer+"] ---> ["+currentUrl+"]<BR>");
	ind= getNaturalReferrerInd(referrer, currentUrl);
	if(ind == -1){
		//document.write("<BR><center>NN</center><BR>");
		return ;
	}
	//document.write("<BR><center>N</center><BR>");
	pageId= getPageIdentification(currentUrl);
	//document.write("<BR>Page Id="+pageId+" <BR>");
	
	naturalPmsSiteId= naturalPmsSiteIds[ind];
	naturalPmsId= naturalPmsIds[ind];
	//document.write("<BR>naturalPmsSiteId="+naturalPmsSiteId+"<BR>");
	//document.write("<BR>naturalPmsId="+naturalPmsId+"<BR>");
	
	key= "PMS"+naturalPmsId;
	setCookie("orgpms", naturalPmsSiteId, 30);
	setCookie("pmsword", pageId, 30);
	setCookie(key, key, 30);
	//document.write("<BR>Cookies seteadas<BR>");
}

function getPageIdentification(currentUrl){
	if(currentUrl.indexOf("jm/item") != -1)
		return "ITEM";
	if(currentUrl.indexOf("jm/search") != -1)
		return "SEARCH";
	if(currentUrl.indexOf("/home")!= -1)
		return "HOME";
	if(currentUrl.length == 1 && currentUrl.lastIndexOf('/')==0)
		return "HOME";
	return "OTHER";	
}

function getNaturalReferrerInd(referrer, currentUrl){
	fromPms= nsGetCookieValue("pmsonline");
	//document.write("<BR>pmsonline:"+fromPms+"<BR>");
	if(fromPms!=null){
		if(fromPms=="YES"){
			setCookie("pmsonline", currentUrl, null);
			return -1;
		}
		if(fromPms.toLowerCase()==currentUrl.toLowerCase())
			return -1;
	}
		
	iQueryIndex= referrer.indexOf("?");
	if(iQueryIndex != -1)
		referrer= referrer.substring(0, iQueryIndex);
	referrer= referrer.toLowerCase();
	finded= -1;
	for(var i= naturalSites.length-1; i>-1 && finded==-1; i--){
		if(referrer.indexOf(naturalSites[i])!=-1)
			finded = i;		
	}
	return finded;
}

function setCookie(cookieName,cookieValue,nDays) {
	if(nDays!=null){
		today  = new Date();
		expire = new Date();
		if (nDays==null || nDays==0) 
			nDays=1;
		expire.setTime(today.getTime() + 3600000*24*nDays);
		document.cookie = cookieName+"="+escape(cookieValue)+"; path=/" + ";expires="+expire.toGMTString();
	}
	else
		document.cookie = cookieName+"="+escape(cookieValue)+"; path=/";
	//document.write("<BR>Seteada cookie: name:["+cookieName+"] value:["+cookieValue+"] <BR>");
}
