/* 
Función que se encarga de comprobar la obligatoriedad de los campos correspondientes al código de usuario y al
password. En caso que esta validación sea correcta, llamamos en protocolo seguro a una página encargada 
de autentificar al usuario. Como paso previo cambiamos el nombre de los campos correspondientes al código y al
password del usuario, de esta forma siempre que lleguen a la página de autentificación se llamarán de la
misma forma, independientemente de cómo se llame el control que los contiene.
*/	
			
function AutentificarUsuario(_strIdValidadorUsuario,
							_strIdValidadorPassword,
							_strCampoCodigoDeUsuario,
							_strCampoPassword,
							_strUsuarioObligatorio,
							_strPasswordObligatorio,
							_strTarget,
							ClickMapOmniture)
{
	var bolEsInfoOK = false;

	if (document.all)
	{
		document.getElementById(_strIdValidadorUsuario).enabled = true;
		document.getElementById(_strIdValidadorPassword).enabled = true;

		bolEsInfoOK = Page_ClientValidate();

		document.getElementById(_strIdValidadorUsuario).enabled = false;
		document.getElementById(_strIdValidadorPassword).enabled = false;	
	}
	else
	{
		var UserKO = false;
		var PassKO = false;

		//MNG Pet A1137 - Firefox
		//Realizamos la validacion manual para firefox.

		if (document.getElementsByName(_strCampoCodigoDeUsuario)[0].value == "")
			UserKO = true;

		if (document.getElementsByName(_strCampoPassword)[0].value == "")
			PassKO = true;
		
		if (UserKO == true && PassKO == true)
		{
			alert("- " + _strUsuarioObligatorio + "\n" + "- " + _strPasswordObligatorio);
		}
		else
		{
			if (UserKO == true)
			{
				alert("- " + _strUsuarioObligatorio);
			}
			else
			{
				if (PassKO == true)
					alert("- " + _strPasswordObligatorio);
				else
					bolEsInfoOK = true;
			}
		}
	}
	
	if ( bolEsInfoOK == true )
	{
		if (document.all)
		{
		
			document.forms(0).__VIEWSTATE.name = "__ORIGINALVIEWSTATE";
			document.getElementById(_strCampoCodigoDeUsuario).name = "__CodigoDeUsuario";
			document.getElementById(_strCampoPassword).name = "__PasswordUsuario";
			document.forms(0).action = "https://" + window.document.domain +
				"/BarceloHotels/Authentication/Autentificacion?Target=" + _strTarget;
			document.forms(0).submit();		
			
			//Caso IE
			s_objectID = ClickMapOmniture;			
		}
		else
		{
			document.forms[0].__VIEWSTATE.name = "__ORIGINALVIEWSTATE";
			
			document.getElementsByName(_strCampoCodigoDeUsuario)[0].name = "__CodigoDeUsuario";
			document.getElementsByName(_strCampoPassword)[0].name = "__PasswordUsuario";
			document.forms[0].action = "https://" + window.document.domain +
				"/BarceloHotels/Authentication/Autentificacion?Target=" + _strTarget;
			document.forms[0].submit();
			
			//Caso Firefox
			s_objectID = ClickMapOmniture;			
		}
	}
}

//Función que abre en una nueva pantalla el posting de redirección al portal b2b.
function AbrirAlertaB2B(_strHost, _strCultura)
{
	//MNG Pet A1137 Firefox
	/*window.showModalDialog("http://" + _strHost + "/barcelohotels/" + _strCultura + "/UserRegistration/AvisoB2B"
							, "","dialogHeight:200px;dialogWidth:400px;scroll:no;resizable:no;status:no" );*/
	xShowModalDialog("http://" + _strHost + "/barcelohotels/" + _strCultura + "/UserRegistration/AvisoB2B"
							, "","dialogHeight=200px;dialogWidth=400px;scrollbars=no;resizable=no;status=no" );
}

//Función que abre en una nueva pantalla el posting de "Usuario incorrecto".
function AbrirAlertaUsuarioErroneo(_strHost, _strCultura)
{
	//MNG Pet A1137 Firefox
	/*window.showModalDialog("http://" + _strHost + "/barcelohotels/" + _strCultura + "/UserRegistration/UsuarioIncorrecto"
							, "","dialogHeight:200px;dialogWidth:400px;scroll:no;resizable:no;status:no" );*/
	xShowModalDialog("http://" + _strHost + "/barcelohotels/" + _strCultura + "/UserRegistration/UsuarioIncorrecto"
							, "","dialogHeight=240px;dialogWidth=450px;scrollbars=no;resizable=no;status=no" );					
}

//Función que abre una nueva pantalla que permite acceder a la pregunta secreta y recordar el password
function AbrirPasswordErroneo(_strHost, _strCultura, _strUsuario)
{
	//MNG Pet A1137 Firefox
	/*window.showModalDialog("https://" + _strHost + "/barcelohotels/" + _strCultura + "/UserRegistration/PasswordIncorrecto?Usuario="+_strUsuario
							, "","dialogHeight:200px;dialogWidth:400px;scroll:no;resizable:no;status:no" );*/
		xShowModalDialog("https://" + _strHost + "/barcelohotels/" + _strCultura + "/UserRegistration/PasswordIncorrecto?Usuario="+_strUsuario
							, "","dialogHeight=240px;dialogWidth=450px;scrollbars=no;resizable=no;status=no" );
}

function AbrirRecordarPassword(_strHost, _strCultura)
{	
	xShowModalDialog("http://" + _strHost + "/barcelohotels/" + _strCultura + "/UserRegistration/RecordarContrasena", 
					"", "dialogHeight=240px;dialogWidth=450px;scrollbars=no;resizable=no;status=no"); 
}

function ClickLogin(event, _strNombreBoton)
{
	if (event.keyCode == 13)
	{
		document.getElementById(_strNombreBoton).click();
		event.returnValue = false;
	}
}

//Funcion que llama a la pagina que contiene el control con el mapa de situación del hotel
//y lo inyecta en un DIV de la pagina principal
function MostrarVentajasBarcelo(e,cultura)
{
	var request = null;

	try
	{
		// Firefox, Opera 8.0+, Safari
		request = new XMLHttpRequest();
	}
	catch(ex)  
	{
		// Internet Explorer
		try
		{
			request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(ex)
		{
			request = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	request.open("GET","http://" + window.document.domain +  "/BarceloHotels/" + cultura + "/UserRegistration/VentajasBarcelo.aspx?" + "?sCultura=" + cultura, false, false);
	request.send(null);

	//Atencion, es necesario eliminar el TAG "form" si no salta error JavaScript
	var response = request.responseText;
	var ini = response.indexOf("<!--#FormEnd##-->");
	var fin = response.indexOf("</form>");
	response = response.substring(ini + 17, fin);
	document.getElementById(divCerrar1.id).innerHTML = response;
	
	var diva = document.getElementById(divCerrar1.id);
	diva.style.display = "inline";

	//Se centra el DIV
	var documentHeight;
	var documentWidth;

	if (document.all)
	{
		documentHeight = document.documentElement.offsetHeight;
		documentWidth = document.documentElement.offsetWidth;
	}
	else
	{
			documentHeight = self.innerHeight;
			documentWidth = self.innerWidth;	
	}

    diva.style.top = (( documentHeight - diva.offsetHeight)/2) +  document.body.scrollTop;
    diva.style.left = (( documentWidth - diva.offsetWidth)/2) + document.body.scrollLeft;
    diva.style.border = "#a2b4ba 5px";
    
/*
    //Se evita el solape con los controles por debajo
	if ( document.getElementById("frameSoporte") )
	{
		document.getElementById("frameSoporte").style.top = diva.style.top;
		document.getElementById("frameSoporte").style.left = diva.style.left;
		document.getElementById("frameSoporte").style.height = diva.offsetHeight;
		document.getElementById("frameSoporte").style.width = diva.offsetWidth;
		document.getElementById("frameSoporte").style.display = "inline";
	}*/
}

function CloseDiv_Ventajas()
{
	document.getElementById(divCerrar1.id).style.display = "none";

	if (document.getElementById("frameSoporte"))
		document.getElementById("frameSoporte").style.display = "none";
}

function RegisterDiv()
{
	document.write('<iframe id="frameSoporte" frameborder="0" style="POSITION:absolute"></iframe>'); 
	document.writeln('<div id="divCerrar" style="DISPLAY: none; Z-INDEX: 101; POSITION: absolute" runat="server"></div></script>');
}

function ClickRegistro(cultura)
{
	var url = "https://" + window.document.domain + "/BarceloHotels/" + cultura + "/UserRegistration/Registro";
	window.location.href = url;
}
