var index=-1;
var ajaxObjects = new Array();


function loguear(form) {

	//creo nueva instancia de objeto ajax
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	// paso los parametros 
		
	ajaxObjects[ajaxIndex].setVar("username",form.usuario.value);
	ajaxObjects[ajaxIndex].setVar("password",form.pass.value);
	ajaxObjects[ajaxIndex].setVar("action_login","Log In");
	ajaxObjects[ajaxIndex].requestFile = url_login_general;
	ajaxObjects[ajaxIndex].onCompletion = function() { login_passed(ajaxIndex,form); };
	ajaxObjects[ajaxIndex].onError = function() { _error(ajaxIndex);  };
	ajaxObjects[ajaxIndex].runAJAX();


};


function login_passed(ajaxIndex,form){
	/*trae la respuesta del servidor en formato texto*/
	var respuesta_texto = ajaxObjects[ajaxIndex].response;
	ajaxObjects[ajaxIndex]=false;
		
	if (respuesta_texto=='ok') {

	    document.getElementById("loginon").className="on";
	    document.getElementById("loginonerr").className="off";
	    document.getElementById("loginoff").className="off";
	    
	    index=-1;	    	    
	    login_sistemas(form);
	    
	}else{
	
	    document.getElementById("loginoff").className="off";
	    document.getElementById("loginon").className="off";
	    document.getElementById("loginonerr").className="on";
	};
    
};


function validar(form) {


	//creo nueva instancia de objeto ajax
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	// paso los parametros 
	ajaxObjects[ajaxIndex].requestFile = url_validar_general;	                                     
	ajaxObjects[ajaxIndex].onCompletion = function() { validar_passed(ajaxIndex,form); };
	ajaxObjects[ajaxIndex].onError = function() { _error(ajaxIndex);  };
	ajaxObjects[ajaxIndex].runAJAX();


};


function validar_passed(ajaxIndex,form){
	/*trae la respuesta del servidor en formato texto*/
	var respuesta_texto = ajaxObjects[ajaxIndex].response;
	ajaxObjects[ajaxIndex]=false;

	if (respuesta_texto!='') {
	    
	    if(respuesta_texto=='novaliduser') {
		    document.getElementById("loginon").className="off";
		    document.getElementById("loginoff").className="off";
		    document.getElementById("loginonerr").className="on";
		    document.getElementById("user_name").innerHTML=respuesta_texto;		    
	    }else{
		    document.getElementById("loginon").className="on";
		    document.getElementById("loginoff").className="off";
		    document.getElementById("loginonerr").className="off";
		    document.getElementById("user_name").innerHTML=respuesta_texto;
	    };


	    
	}else{
	
	    document.getElementById("loginoff").className="on";
	    document.getElementById("loginon").className="off";
	    document.getElementById("loginonerr").className="off";
	};
    
};


function login_sistemas(form){
	
	index ++;
	
	if (index==url_login.length) location.reload(true);
	
	var a = fields_names[index];
	var b = fields_values[index];
	var field = a.split(",");
	var value = b.split(",");
	var fields_count = field.length;
	var u=0;
	
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	
	while (u<fields_count){
	    switch (value[u]){
		case "{username}":
		    ajaxObjects[ajaxIndex].setVar(field[u],form.usuario.value + name_sufix[index]);
		break;
		
		case "{password}":
		    ajaxObjects[ajaxIndex].setVar(field[u],form.pass.value);
		break;
		
		default:
		    ajaxObjects[ajaxIndex].setVar(field[u],value[u]);
		break;
		
	    }
	    
	    u++;
	}
	
	ajaxObjects[ajaxIndex].requestFile = url_login[index];
	ajaxObjects[ajaxIndex].onCompletion = function() { login_sistemas(form); };
	ajaxObjects[ajaxIndex].onError = function() { _error(ajaxIndex);  };
	ajaxObjects[ajaxIndex].runAJAX();
}


function relogin(){
	    document.getElementById("loginon").className="off";
	    document.getElementById("loginonerr").className="off";
	    document.getElementById("loginoff").className="on";

}

// FUNCION QUE SE EJECUTA CUANDO HAY ALGUN ERROR EN LA SOLICITUD
function _error(ajaxIndex){

    alert("Error llamando al archivo "+ajaxObjects[ajaxIndex].requestFile+"!!!");
};

function log_out(form) {

//	//creo nueva instancia de objeto ajax
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	// paso los parametros 
	ajaxObjects[ajaxIndex].requestFile = url_logout_general;
	ajaxObjects[ajaxIndex].onCompletion = function() { logout_passed(ajaxIndex,form); };
	ajaxObjects[ajaxIndex].onError = function() {_error(ajaxIndex);  };
	ajaxObjects[ajaxIndex].runAJAX();
};


function logout_passed(ajaxIndex,form){
	/*trae la respuesta del servidor en formato texto*/
	var respuesta_texto = ajaxObjects[ajaxIndex].response;
	ajaxObjects[ajaxIndex]=false;

	    document.getElementById("loginon").className="off";
	    document.getElementById("loginoff").className="on";
	    
	    index =-1;
	    logout_sistemas(form);
	
    
};

function logout_sistemas(form){
	
	index ++;
	
	if (index==url_login.length) location.reload(true);
	
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	
	ajaxObjects[ajaxIndex].requestFile = url_logout[index];
	ajaxObjects[ajaxIndex].onCompletion = function() { logout_sistemas(form); };
	ajaxObjects[ajaxIndex].onError = function() { _error(ajaxIndex);  };
	ajaxObjects[ajaxIndex].runAJAX();
}




 $(document).ready(function(){
    // Your code here

    validar();

     });


//-->
