var IE;
if (jQuery.browser.msie) { 
 	IE=true;
}else{
	IE=false
}
var uss;
/*
window.onload = function(){
	uss = nuevoUss;
	
}*/

var tablaLogin;



function estaVacio(texto){
	if (texto == null || texto == ""){ 
		return true;
	}
	return false;
}

function validarDatos(theForm){
	if(estaVacio(theForm.parNick.value)){
		alert(ingUss);
		theForm.parNick.focus();
		return false;
	}else if(estaVacio(theForm.parPass.value)){
		alert(ingPass);
		theForm.parPass.focus();
		return false;		
	}
	
	return true;
	
}

function login(theForm){
    if(validarDatos(theForm)){
    	usuario = theForm.parNick.value;
		
		for (i=0;i<usuario.length;i++ ){
			
			if (usuario.charAt(i) == "@"){
				theForm.esMail.value = "S";
				break;
			} else {
				theForm.esMail.value = "N";
			}
		}
		
        theForm.submit();
    }
}



function logoutHome(){
	
	tablaLogin = '<TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="115" ALIGN="center" id="tablaLogin">'+
				 '<TR><TD CLASS="titulo_tabla_login" ALIGN="center">'+lLogin+'</TD></TR></TABLE><TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="115" ALIGN="center" id="tablaUssPass">'+
				 '<TR><TD CLASS="TRDetallesCen" ALIGN="center" colspan="2" NOWRAP><INPUT TYPE="TEXT" NAME="parNick" CLASS="input_text_login" VALUE="'+uss+'" SIZE="11" MAXLENGTH="60" id="textUsuario" onKeyDown="if (event.keyCode==13) this.form.parPass.focus();" OnClick="controlInput();" style="borderwidth:2px;border-color:#98b0d2;border-style:solid;padding-left:2px;"></TD>'+
				 '</TR><TR><td align="center" nowrap="" colspan="2" class="TRDetallesCen"><input type="PASSWORD" style="border-color: rgb(152, 176, 210); border-style: solid; padding-left: 2px;" onfocus="controlPass();" onkeydown="if (event.keyCode==13) loginHome();" id="textPass" maxlength="20" size="11" class="input_text_login" name="parPass"></td>'+
				 '</TR><TR><td align="left" width="65px" nowrap="" style="vertical-align: text-top;" onclick="location.href=\'/BQBWeb/RegistraUsuario\'" class="input_text_login_datos">'+nuevoUss+'</td><td align="right" style="padding-right: 5px;"><img width="18" height="17" onclick="loginHome()" style="cursor: pointer; padding-right: 5px;" title="Entrar" alt="Entrar" src="/BQBWeb/img/botones/Aceptar-Login.gif"></td>'+
				 '</tr><tr><td align="left" width="92px" nowrap="" style="vertical-align: text-top;" colspan="2" onclick="location.href=\'/BQBWeb/RecuperarPassword\'" class="input_text_login_datos">'+olvPass+'</td>';
	
	$.ajax({
		type: "POST",
		url: "com.buquebus.web.login.LogoutUsuario",
		beforeSend:function(req){$("#tablaUssPass").replaceWith("<img src='/BQBWeb/img/cargando_ch.gif' id='cargando' style='vertical-align:middle; margin:19px 10px; padding-right:35px;'>");},
		success: function(data){ $("#contieneLogin").html(tablaLogin);},
		timeout:10000,
		error:function(data){alert(errconex);}
	});

	
}

//login con ajax
function loginHome(){
	
	usuario = document.getElementById("textUsuario").value
	contrasena = document.getElementById("textPass").value
	
	
	tablaLogin = '<TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="115" ALIGN="center" id="tablaLogin">'+
				 '<TR><TD CLASS="titulo_tabla_login" ALIGN="center">'+lLogin+'</TD></TR></TABLE><TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="115" ALIGN="center" id="tablaUssPass">'+
				 '<TR><TD CLASS="TRDetallesCen" ALIGN="center" colspan="2" NOWRAP><INPUT TYPE="TEXT" NAME="parNick" CLASS="input_text_login" VALUE="'+uss+'" SIZE="11" MAXLENGTH="60" id="textUsuario" onKeyDown="if (event.keyCode==13) this.form.parPass.focus();" OnClick="controlInput();" style="borderwidth:2px;border-color:#98b0d2;border-style:solid;padding-left:2px;"></TD>'+
				 '</TR><TR><td align="center" nowrap="" colspan="2" class="TRDetallesCen"><input type="PASSWORD" style="border-color: rgb(152, 176, 210); border-style: solid; padding-left: 2px;" onfocus="controlPass();" onkeydown="if (event.keyCode==13) loginHome();" id="textPass" maxlength="20" size="11" class="input_text_login" name="parPass"></td>'+
				 '</TR><TR><td align="left" width="65px" nowrap="" style="vertical-align: text-top;" onclick="location.href=\'/BQBWeb/RegistraUsuario\'" class="input_text_login_datos">'+nuevoUss+'</td><td align="right" style="padding-right: 5px;"><img width="18" height="17" onclick="loginHome()" style="cursor: pointer; padding-right: 5px;" title="Entrar" alt="Entrar" src="/BQBWeb/img/botones/Aceptar-Login.gif"></td>'+
				 '</tr><tr><td align="left" width="92px" nowrap="" style="vertical-align: text-top;" colspan="2" onclick="location.href=\'/BQBWeb/RecuperarPassword\'" class="input_text_login_datos">'+olvPass+'</td>';
	
	if (usuario == null || usuario == "" || usuario == "Usuario"){
		alert(ingUss);
		return false;
	} else if (contrasena == null || contrasena == "" || contrasena == "Contrasena"){
		alert(ingPass);
		return false;
	} else {
		
		for (i=0;i<usuario.length;i++ ){
			if (usuario[i] == "@"){
				esMail = "S";
				break;
			} else {
				esMail = "N";
			}
		}
		if (IE){
			var datos = {parNick:encodeURI(usuario), parPass:contrasena, loginAjax:"S", validarUsuario:"true", esMail:esMail};
		}else{//si es firefox
		  var datos = {parNick:usuario, parPass:contrasena, loginAjax:"S", validarUsuario:"true", esMail:esMail};
		}

		$.ajax({
			type: 'POST',
			url: "com.buquebus.web.login.LoginUsuario",
			data: datos,
			beforeSend:function(req){$("#tablaUssPass").replaceWith("<img src='/BQBWeb/img/cargando_ch.gif' id='cargando' style='vertical-align:middle; margin:19px 10px; padding-right:35px;'>");},
			success: function(data){ 
				
				var nombre = data.split("-")[1];
				var msg = data.split("-")[0];
				
				if(msg == "ERR_USER") {
					msg = errUss;
					$("#tablaLogin").replaceWith("<div align='center' id='usuario' style='font-family:Arial; font-size:9pt;color:#696A6C; text-decoration:none;'>"+msg+" </br></div>");  
					$("#cargando").replaceWith("<div align='center' id='loginPropertiesLink' style='margin-top:10px; font-family:Arial; font-size:9pt; color:#696A6C; text-decoration:none; cursor:pointer;' OnClick='logoutHome()'>"+volver+"</div>");
				
				} else if(msg == "ERR_PASS") {
					msg = errPass;
					$("#tablaLogin").replaceWith("<div align='center' id='usuario' style='font-family:Arial; font-size:9pt;color:#696A6C; text-decoration:none;'>"+msg+" </br></div>");  
					$("#cargando").replaceWith("<div align='center' id='loginPropertiesLink' style='margin-top:10px; font-family:Arial; font-size:9pt; color:#696A6C; text-decoration:none; cursor:pointer;' OnClick='logoutHome()'>"+volver+"</div>");
				
				} else if(msg == "ERR_CLASS") {
					msg = errInt;
					$("#tablaLogin").replaceWith("<div align='center' id='usuario' style='font-family:Arial; font-size:9pt;color:#696A6C; text-decoration:none;'>"+msg+" </br></div>");  
					$("#cargando").replaceWith("<div align='center' id='loginPropertiesLink' style='margin-top:10px; font-family:Arial; font-size:9pt; color:#696A6C; text-decoration:none; cursor:pointer;' OnClick='logoutHome()'>"+volver+"</div>");
				
				} else if(msg == "USUARIO VALIDADO") {
					msg = bienvenidos;	
					$("#tablaLogin").replaceWith("<div align='center' id='usuario' style='font-family:Arial; font-size:9pt;color:#696A6C; text-decoration:none;'>"+msg+" </br>"+nombre+"</div>");  
					$("#cargando").replaceWith("<div align='center' id='loginPropertiesLink' style='margin-top:10px; font-family:Arial; font-size:9pt; color:#696A6C; text-decoration:none; cursor:pointer;' OnClick='logoutHome()'>"+logOut+"</div>"+
											   "<div align='left' id='modifDatos' style='margin-top:10px; padding-left:12px; font-family:Arial; font-size:7pt; color:#0000FF; text-decoration:none; cursor:pointer;' OnClick=\"location.href='/BQBWeb/ModifDatosUsuario'\">"+modifDatos+"</div>");
				}
			},
			timeout:10000,
			error:function(data){alert(errconex);$("#contieneLogin").html(tablaLogin);},
			dataType: "text"
		});
	}

}

function encodearCompleto(cadena){
	return escape(encodeURI(cadena));
}


/*window.onload = darFoco;

function darFoco(){
	if(document.frmSend.focoParNick != undefined && document.frmSend.focoParNick.value == "true")
		document.frmSend.parNick.focus();
	else
		document.frmSend.parEmail.focus();
}*/
