﻿function read_querystring()	{
	var a_out = new Object();
	var s_loc = String(location.href);

	if(s_loc.indexOf('?')>0)
		{
		var p;
		var s_query = s_loc.substr(s_loc.indexOf('?')+1);
		var a_query = s_query ? s_query.split('&') : new Array();

		for(var i=0; i<a_query.length; i++)
		{
		p = a_query[i].split('=');
		a_out[p[0]] = p[1].replace(/\+/g, ' ');
		}
	}
	return a_out;
}

var query_vars = read_querystring();
		
function URLDecode(aText)
{
    // Replace + with ' '
    // Replace %xx with equivalent character
    // Put [ERROR] in output if %xx is invalid.
    var HEXCHARS = "0123456789ABCDEFabcdef"; 
    var encoded = aText;
    var plaintext = "";
    var i = 0;
    while (i < encoded.length) {
	    var ch = encoded.charAt(i);
	    if (ch == "+") {
		    plaintext += " ";
		    i++;
	    } else if (ch == "%") {
			    if (i < (encoded.length-2) 
					    && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					    && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				    plaintext += unescape( encoded.substr(i,3) );
				    i += 3;
			    } else {
				    alert( 'Bad escape combination near ...' + encoded.substr(i) );
				    plaintext += "%[ERROR]";
				    i++;
			    }
		    } else {
		    plaintext += ch;
		    i++;
		    }
	    } // while
    return plaintext;
}
function ShowIMError(){
    if (query_vars['err']!=undefined){
	    ShowErrorMessage(URLDecode(query_vars["err"]));
    }
}

function ShowErrorMessage(aError) {
    alert(aError);
}

function LoginRegisteredClick() {
    document.getElementById("LoginType").value="LoginRegistered";
    var username = document.getElementById("txtUsernameRegistered").value;
    var password = document.getElementById("txtPasswordRegistered").value;
    
    if ((username!='')&&(password!='')) {
        if(checkValidChars(username)&&(checkValidCharsPassword(password))){
            document.getElementById("LoginForm").submit();
        }else{
            ShowErrorMessage('Caracteres inválidos en el nombre de usuario o contraseña');
        }
    }
    else {
        ShowErrorMessage('El usuario y la contraseña no pueden estar en blanco')
    }
}
/*function ClickEnter(e){
	e = e || window.event;
	if(e.keyCode == 13){
		document.getElementById("LoginRegistered").click();
	}
}
if(document.all){
	document.attachEvent("onkeydown",ClickEnter);
}else{
	document.addEventListener("keyup",ClickEnter,false)
}*/

function checkValidChars(aText){
	var plaintext = aText;
	var isValid = true;
	for (var i = 0; i < plaintext.length; i++ ) {
		var code = plaintext.charCodeAt(i);
		if ((isNaN(plaintext.charAt(i))&&!(code >= 65 && code <=90)&&!(code >= 97 && code <= 122))||(code == 32)) {
			isValid = false;
			return isValid;
		}
	}
	return isValid;
};

function checkValidCharsPassword(aText){
	var plaintext = aText;
	var isValid = true;
	for (var i = 0; i < plaintext.length; i++ ) {
		var code = plaintext.charCodeAt(i);
		//if (!(code > 32 && code <=126)) {
		if (code == 32){
			isValid = false;
			return isValid;
		}
	}
	return isValid;
}