/*
 * Returns a new XMLHttpRequest object, or false if this browser
 * doesn't support it
 */
function newXMLHttpRequest(){
	var xmlreq = false;
	if (window.XMLHttpRequest){
		xmlreq = new XMLHttpRequest();
	}else if (window.ActiveXObject){
		try{
			xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e1){
			try{
				xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e2){ }
		}
	}
	return xmlreq;
}
/*
 *Obtencion y creacion de cookies para evaluar si es necesario hacer el keepalive y en que tiempo.
 */
function getCookie(name, time) {
	//buscamos cookie de fecha y tiempo
	var cname=name + "=";
	var dc=document.cookie;
	if(dc.length>0){
		begin=dc.indexOf(cname);
		if(begin!=-1){
			begin+=cname.length;
			end=dc.indexOf(";",begin);
			if(end==-1)
				end=dc.length;
			var result = dc.substring(begin,end)
			return(result);
		}else{
			//en caso de no existir la cookie  deseada se guarda
			document.cookie=name+'='+time+";path=/";
			return null;
		}
	}else{
		//en caso de no haber ninguna cookie
		document.cookie=name+'='+time+";path=/";
		return null;
	}
} 
/*
 * KeepAlive
 */
function keepalive(){
	var cookie = getCookie("dateTimeout", Date()); 
	var actualDate = new Date(Date());
	var timems = 540000;
	if(cookie == null){
		var req = newXMLHttpRequest();
		req.open("GET","/myaccount/keepalive.php",true);
		req.send(null);
		document.cookie='dateTimeout='+actualDate+";path=/";
		returntimeout(timems);
	}else{
		var olddate = new Date(cookie);
		var actualMinutes = actualDate.getMinutes();
		var oldMinutes = olddate.getMinutes();
		if(actualMinutes < oldMinutes){
			actualMinutes = actualMinutes + 60;
		}
		timeout = actualMinutes - oldMinutes;
		if(timeout == 9){
			var req = newXMLHttpRequest();
			req.open("GET","/myaccount/keepalive.php",true);
			req.send(null);
			document.cookie='dateTimeout='+actualDate+";path=/";
			returntimeout(timems);
		}else{
			timeout =(9 - timeout ) * 60000;
			if(timeout > 0){
				returntimeout(timeout);
			}else{
				var req = newXMLHttpRequest();
				req.open("GET","/myaccount/keepalive.php",true);
				req.send(null);
				document.cookie='dateTimeout='+actualDate+";path=/";
				returntimeout(timems);				   							
			}
		}
	}	   
}
/*
 *Timeout para keepalive
 */
function returntimeout(miliseconds){
	 setTimeout("keepalive()",miliseconds);
	 return;
}
/*
 *updatedata
 */
function updatedata() {
	var cookie = getCookie("dateUpdate", Date());
	var actualDate = new Date(Date());
	var timems = 3540000;
	if(cookie == null){
		var req = newXMLHttpRequest();
		var handlerFunction = getReadyStateHandlerUpd(req,msgParser);
		req.onreadystatechange = handlerFunction;
		req.open("GET","/myaccount/updatedata.php",true);
		req.send(null);
		document.cookie='dateUpdate='+actualDate+";path=/";
		setTimeout("updatedata()",timems);
	}else{
		var olddate = new Date(cookie);
		var actualMinutes = actualDate.getMinutes();
		var oldMinutes = olddate.getMinutes();
		if(actualMinutes < oldMinutes){
			actualMinutes = actualMinutes + 60;
		}
		timeout = actualMinutes - oldMinutes;
		if(timeout == 59){
			var req = newXMLHttpRequest();
			var handlerFunction = getReadyStateHandlerUpd(req,msgParser);
			req.onreadystatechange = handlerFunction;
			req.open("GET","/myaccount/updatedata.php",true);
			req.send(null);
			document.cookie='dateUpdate='+actualDate+";path=/";
			setTimeout("updatedata()",timems);
		}else{
			timeout =(59 - timeout ) * 60000;
			if(timeout > 0){
				setTimeout("updatedata()",timeout);
			}else{
				var req = newXMLHttpRequest();
				var handlerFunction = getReadyStateHandlerUpd(req,msgParser);
				req.onreadystatechange = handlerFunction;
				req.open("GET","/myaccount/updatedata.php",true);
				req.send(null);
				document.cookie='dateUpdate='+actualDate+";path=/";
				setTimeout("updatedata()",timems);
			}
		}
	}
}
/*
 * Funcion que hace lo mismo que getReadyStateHandler pero 
 * ahora para el update data
 */
function getReadyStateHandlerUpd(req, responseXmlHandler){
	return function (){
		if (req.readyState == 4){
			if (req.status == 200){
				try {
					responseXmlHandler(req.responseXML);
				}catch(e3){}
			}else{
				//alert("HTTP error: " + req.status);
			}
		}
	}
}
/*
 *Funcion que parsea el xml y crea la ventanita
 */
function msgParser(XML){
	var root = XML.getElementsByTagName("msg")[0];
	var text = root.getAttribute('text');
	var url = root.getAttribute('url');
	var redirect = root.getAttribute('redirect');
	if(text!="" || url!=""){
		var so = new SWFObject("/ventanamensajeC.swf", "player", "200", "300", "8", "#F3F3F3");
		so.addParam("wmode", "transparent");
		so.addVariable("rootPath", "http://chat.corazones.com/flashcoms/");
		so.addVariable("mensaje", "<a href='"+url+"' target='_blank'>"+text+"</a>");
		so.addVariable("duracion",15000);
		if (parseInt(navigator.appVersion)>3){
			if (navigator.appName=="Netscape"){
				winH = window.innerHeight;
			}
			if (navigator.appName.indexOf("Microsoft")!=-1){
				winH = document.documentElement.clientHeight;
			}
		}
		var posH = winH;
		posH = posH-270;
		posH = posH-52;
		document.getElementById("ventana").style.top=posH+"px";	
		so.write("ventana");	
		setTimeout('document.getElementById("ventana").innerHTML=""',30000);
		if(redirect != '' && redirect > 0){
			redirect = redirect*1000;
			document.cookie = "dateUpdate=";
			setTimeout('window.location="/memberships.php"', redirect);
		}
	}
}
/**
 *	Nos regresa un int  con el id del Sistema Operativo que usa el cliente a segun la bd de Metrics
 */
function getSistemaOperativo() {
	if (navigator.userAgent.indexOf('IRIX') != -1) {return 1;}					//SO = "Irix" }
	else if ((navigator.userAgent.indexOf('Win') != -1) && (navigator.userAgent.indexOf('98') != -1)) { return 2;}		//SO= "Windows 98"}
	else if ((navigator.userAgent.indexOf('Win') != -1) && (navigator.userAgent.indexOf('95') != -1)) { return 3;}		//SO= "Windows 95"}
	else if (navigator.appVersion.indexOf("16") !=-1){ return 4;}				//SO= "Windows 3.1"}
	else if (navigator.appVersion.indexOf("Win") !=-1 && navigator.appVersion.indexOf("3.1") !=-1) { return 4;}			//SO= "Windows 3.1"}
	else if((navigator.userAgent.indexOf('Windows NT 5.1') != -1)){ return 5;} 	//SO= "Windows XP"}
	else if (navigator.appVersion.indexOf("SunOS") !=-1){ return 7;}			//SO= "SunOS"}
	else if (navigator.appVersion.indexOf("Linux") !=-1){ return 8;}			//SO= "Linux"}
	else if (navigator.userAgent.indexOf('Mac') != -1){ return 9;}				//SO= "Macintosh"}
	else if (navigator.appName=="WebTV Internet Terminal"){ return 10;}			//SO="WebTV"}
	else if (navigator.appVersion.indexOf("HP") !=-1) { return 11;}				//SO="HP-UX"}
	else if((navigator.userAgent.indexOf('Windows NT 6.0') != -1)){ return 12;} //SO= "Windows Vista"}
	else if((navigator.userAgent.indexOf('Windows NT 5.2') != -1)){ return 13;}	//SO= "Windows Server 2003, Windows XP x64 Edition"}
	else if (navigator.userAgent.indexOf("NetBSD") !=-1 || navigator.platform.indexOf("NetBSD") !=-1){ return 14;}		//SO="NetBSD"}
	else if((navigator.userAgent.indexOf('Windows NT 5.01') != -1)){ return 15;}//SO= "Windows 2000, (SP1)"}
	else if((navigator.userAgent.indexOf('Windows NT 5.0') != -1)){	return 16;}	//SO= "Windows 2000"}
	else if((navigator.userAgent.indexOf('Windows 2000') != -1)){ return 16;}	//SO= "Windows 2000"}
	else if((navigator.userAgent.indexOf('Windows NT 4.0') != -1)){ return 17;}	//SO= "Windows NT 4.0"}
	else if((navigator.userAgent.indexOf('Win') != -1) && (navigator.userAgent.indexOf('NT') != -1)){ return 6;}		//SO= "Windows NT"}
	else if((navigator.userAgent.indexOf('Win') != -1) && (navigator.userAgent.indexOf('9x 4.90') != -1)){ return 18;}	//SO= "Windows Me"}
	else if((navigator.userAgent.indexOf('Windows CE') != -1)){ return 19;}		//SO= "Windows CE"}
	else if (navigator.userAgent.indexOf("FreeBSD") !=-1 || navigator.platform.indexOf("FreeBSD") !=-1){ return 21;}	//SO="FreeBSD"}
	else if (navigator.userAgent.indexOf("Linux") !=-1){ return 8;}			//SO= "Linux"}
	else {return 20}	//Otro
}
/**
 *	Funcion para Insertar en la tabla de Vistas mediante ajax
 */
function RegisterVisit(iModulo, sReferer){
	ajax=newXMLHttpRequest();
	if (ajax==null){return}
	sReferer = sReferer.replace(/&/g,"*amp*");//Remplazamos todos los amperson por *amp* para poder mandar el parametro como uno solo
	//Mandamos llamar a la página que registrara la visita con el modulo, OS, Resolucion y Query (si viene de busqueda)
	ajax.open("GET", "/include/stats/RegisterVisit.php?iModulo="+iModulo+"&iOS="+getSistemaOperativo()+"&iResH="+screen.height+"&iResW="+screen.width+"&sReferer="+sReferer+"&cookieEnabled="+navigator.cookieEnabled, true);
	ajax.onreadystatechange=function(){
		if (ajax.readyState==4){}
	}
	ajax.send(null);
}