// JavaScript Document
var id_user = "0";
var str_email = "";
var str_pais = "MEX";

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };

function msg_salida(pais)
{	
	var txt_msg = "Detectamos que tiene productos marcados en su lista de favoritos.\n\n";	
	txt_msg += "Si desea guardar su lista de favoritos seleccione la opci\u00F3n Cancelar.\n\n";
	txt_msg += "El permanecer en la p\u00E1gina actual le permitir\u00E1 realizar su registro y conservar su historial."
	
	if(pais == "USA"){	
		txt_msg = "You have products selected in your favorites list.\n\n";	
		txt_msg += "To save your favorites list select the Cancel option.\n\n";
		txt_msg += "Staying in the current page allows you to preserve your history."
	}	
	
	return txt_msg;
}

function AddFavorito(pais, iduser, idproducto)
{
	id_user = iduser;
	var url  = pais + "/Favoritos.aspx?tipo_mov=add&id_user=" + iduser + "&id_producto=" + idproducto + "&hash=" + Math.random();
	xmlHttp=GetXmlHttpObject(SendPage_handler)	
	xmlHttp.open("GET", url , true)
	xmlHttp.send(null)
}

function DeleteFavorito(pais, iduser, idproducto)
{
	var url  = pais + "/Favoritos.aspx?tipo_mov=delete&id_user=" + iduser + "&id_producto=" + idproducto + "&hash=" + Math.random();
	//alert(url);
	xmlHttp=GetXmlHttpObject(SendPage_handler)	
	xmlHttp.open("GET", url , true)
	xmlHttp.send(null)		
}

function DeleteFavoritoRegresar(pais, iduser, idproducto)
{
	var url  = pais + "/Favoritos.aspx?tipo_mov=delete&id_user=" + iduser + "&id_producto=" + idproducto + "&hash=" + Math.random();
	xmlHttp=GetXmlHttpObject(SendPage_handler)	
	xmlHttp.open("GET", url , true)
	xmlHttp.send(null)	
}

function SendPage_handler() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		if(id_user != "0")
		{						
			if(document.getElementById("txt_id_user") != null)
			{
			   document.getElementById("txt_id_user").value = id_user;			
			}			
		}		
	} 
}

//Unload Eventos
//Marco A. 07/abr/10

	var browser = navigator.userAgent.toLowerCase();
	var IEVersion;
	var brVerId = browser.indexOf('msie');	
	if (brVerId > 0) {
		IEVersion = browser.substr( brVerId + 5 ,1);
	}
	//var AlertBox = "Detectamos que tiene productos marcados en su lista de favoritos.\n\n";	
	
	window.onbeforeunload = function(){			
		var b = true;
		
		if(tb_detectMSIE()){			
			var n = window.event.screenX - window.screenLeft;   
      		b = n > document.documentElement.scrollWidth-20; 
		}		
		
		if(b == true){		 
			if(document.getElementById("txt_id_user") != null){			
				var user_id = document.getElementById("txt_id_user").value;
				if(user_id != "0"){					
					if(user_id.indexOf("tmp") != -1){
						document.getElementById("txt_id_user").value = "0";						
						if(ReadCookie(user_id).length > 0)
							return ExitPop(isExit, user_id);						
					}					
				}				
			}
		}	
	}
	
	function tb_detectMSIE() { 		 		
  		if (browser.indexOf('msie')!=-1) { return true;	}
		else { return false; }
	}
	
	function tb_detectFF() {  		
  		if (browser.indexOf('firefox')!=-1) { return true; }
		else { return false; }
	}
	
	function ReadCookie(cookiename) {
    	var numOfCookies = document.cookie.length;
    	var nameOfCookie = cookiename + "=";
    	var cookieLen = nameOfCookie.length;
    	var x = 0 ;
    	while (x <= numOfCookies) {
        	var y = (x + cookieLen);
        	if (document.cookie.substring(x, y) == nameOfCookie)
            	return (extractCookieValue(y));
        	x = document.cookie.indexOf(" ", x) + 1;
        	if (x == 0)
            	break;
    	}
    	return "";
	}
	
	function extractCookieValue(val)
	{
    	if ((endOfCookie = document.cookie.indexOf(";", val)) == -1)
        	endOfCookie = document.cookie.length;
    	return unescape(document.cookie.substring(val, endOfCookie));
	}
	
	function AbrirCentrado(Url,NombreVentana,width,height,extras)
	{
		var largo = width;
		var altura = height;
		var adicionales= extras;
		var top = (screen.height-altura)/2;
		var izquierda = (screen.width-largo)/2;
		nuevaVentana=window.open(''+ Url + '',''+ NombreVentana + '','width=' + largo + ',height=' + altura + ',top=' + top + ',left=' + izquierda + ',features=' + adicionales + ''); 
		nuevaVentana.focus();
	}
	
	function cambiar_password(pais, id_user)
	{		
		var NombreVentana = 'user_edit';
		var Url = "0";
		var id_user = id_user;
		var largo = 500;
		var altura = 250;
		var adicionales= '';
		var top = (screen.height-altura)/2;
		var izquierda = (screen.width-largo)/2;
		
		if(pais.toLowerCase() == "mex" || pais.toLowerCase() == "usa")
		{
			var url_origen = document.location.href;
			
			if(pais.toLowerCase() == "mex")
			{				
				Url = '../../Util/user_edit.asp?pais=MEX&id=' + id_user;
				//Url = url_origen;
			}
			else
			{
				//Url = './Util/user_edit_ing.asp?pais=USA&id=' + id_user;
				Url = '../../Util/user_edit_ing.asp?pais=USA&id=' + id_user;
			}			
			nuevaVentana=window.open(''+ Url + '',''+ NombreVentana + '','width=' + largo + ',height=' + altura + ',top=' + top + ',left=' + izquierda + ',features=' + adicionales + ''); 
			nuevaVentana.focus();
		}
		else
		{
			alert('Parametro erroneo, pais = MEX o USA');
		}
			
	}
	
	function reporte_pdf()
	{
		var pais = 'MEX';		
		var url_origen = document.location.href;
		var pruebas = 0;
		url_origen = url_origen.toLowerCase();
		
		if(url_origen.indexOf('/usa/') > 0)
			pais = 'USA';
			
		if(url_origen.indexOf('/exa3/') > 0)
			pruebas = 1;	
			
		var NombreVentana = 'informe';
		var Url = 'http://interceramicgreen.softbox.com.mx/MEX/green/genera_pdf.aspx?opc=usuarios&pais=' + pais;
		
		if(pruebas == 1)
			Url = 'http://exa3/interceramicgreen/MEX/green/genera_pdf.aspx?opc=usuarios&pais=' + pais;	
		
		var largo = 900;
		var altura = 500;
		var adicionales= '';
		var top = (screen.height-altura)/2;
		var izquierda = (screen.width-largo)/2;					
		nuevaVentana=window.open(''+ Url + '',''+ NombreVentana + '','width=' + largo + ',height=' + altura + ',top=' + top + ',left=' + izquierda + ',features=' + adicionales + ''); 
		nuevaVentana.focus();		
		return false;
	}
	
	function reporte_pdf_favoritos(id_user)
	{		
		var pais = 'MEX';		
		var url_origen = document.location.href;
		var pruebas = 0;
		var prods_cookie = "";
		url_origen = url_origen.toLowerCase();
		
		if(id_user.indexOf('tmp') > -1)
		{
			var valores_cookie = ReadCookie(id_user);
			var items_prod = valores_cookie.split('&');
			var items_num = items_prod.length; 
			
			if(items_prod.length > 0)
			{
				for(n=0; n<items_num; n++)
				{
					if(n>0)
						prods_cookie += "_";
						
					prods_cookie += items_prod[n].substring(items_prod[n].indexOf('=')+1);					
				}				
			}			
		}	
		
		if(url_origen.indexOf('/usa/') > 0)
			pais = 'USA';
			
		if(url_origen.indexOf('/exa3/') > 0)
			pruebas = 1;			
			
		//if(url_origen.indexOf('/interceramic2') > 0)
		//	pruebas = 2;
			
		var NombreVentana = 'favoritos';
		var Url = 'http://interceramicgreen.softbox.com.mx/MEX/green/genera_pdf.aspx?opc=favoritos&pais=' + pais + '&id_user=' + id_user + '&readc=' + prods_cookie;
		
		if(pruebas == 1)
			Url = 'http://exa3/interceramicgreen/MEX/green/genera_pdf.aspx?opc=favoritos&pais=' + pais + '&id_user=' + id_user + '&readc=' + prods_cookie ;	
			
		if(pruebas == 2)
			Url = 'http://exa3/interceramicgreen/MEX/green/genera_pdf.aspx?opc=favoritos&pais=' + pais + '&id_user=' + id_user + '&readc=' + prods_cookie ;	
			
		if(url_origen.indexOf('/oficina.exagono.net/') > -1)
			Url = 'http://oficina.exagono.net/interceramicgreen/MEX/green/genera_pdf.aspx?opc=favoritos&pais=' + pais + '&id_user=' + id_user + '&readc=' + prods_cookie ;
		
		var largo = 700;
		var altura = 500;
		var adicionales= '';
		var top = (screen.height-altura)/2;
		var izquierda = (screen.width-largo)/2;					
		nuevaVentana=window.open(''+ Url + '',''+ NombreVentana + '','width=' + largo + ',height=' + altura + ',top=' + top + ',left=' + izquierda + ',features=' + adicionales + ''); 
		nuevaVentana.focus();				
	}
	
	function open_download_image(Url1)
	{			
		var NombreVentana = 'favoritos';
		var Url = 'http://exa3/interceramic/MEX/generaxml.html';
		alert(Url1);
		
		var largo = 700;
		var altura = 500;
		var adicionales= '';
		var top = (screen.height-altura)/2;
		var izquierda = (screen.width-largo)/2;					
		nuevaVentana=window.open(''+ Url1 + '',''+ NombreVentana + '','width=' + largo + ',height=' + altura + ',top=' + top + ',left=' + izquierda + ',features=' + adicionales + ''); 
		nuevaVentana.focus();		
	}
	
	function lanzar_busqueda(url, opc)
	{
		var t='0'
		
		if(opc == "busqueda")
		{
			window.location= url + 'sitio/MEX/' + opc + '.xhtml';
		}
		else
		{		
			if(document.getElementById("txt_buscar_grl")!= null)
			{
				t = document.getElementById("txt_buscar_grl").value;			
			}
				
			if(t.trim().length == 0)
			{
				t='0';
			}
			else
			{
				t=encodeURIComponent(t);
			}
			window.location= url + 'sitio/MEX/' + t + '/' + opc + '.xhtml';
		}				
	}

	function call_qs(event)
	{
		t = document.getElementById("txt_buscar_grl").value;
		var boton = document.getElementById("btn_quick_search1");		
    	boton.click();		
		if (event.cancelable === undefined) {
        	event.returnValue = false;                
        }
        else {
        	// in Firefox, the cancelable property always returns true,
            // so the cancelable state of the event cannot be determined
            if (event.cancelable) {				
            	event.preventDefault();                	    
            }            
        }		
	}

	function lanzar_busquedaUSA(url, opc)
	{		
		var t='0'
		
		if(opc == "busqueda")
		{
			window.location= url + 'site/USA/' + opc + '.xhtml';
		}
		else
		{		
			if(document.getElementById("txt_buscar_grl")!= null)
			{
				t = document.getElementById("txt_buscar_grl").value;				
			}
				
			if(t.trim().length == 0)
			{
				t='0';
			}
			else
			{
				t=encodeURIComponent(t);
			}
			//alert(url + 'sitio/MEX/' + t + '/' + opc + '.xhtml');
			//return false;
			window.location= url + 'site/USA/' + t + '/' + opc + '.xhtml';			
		}				
	}
