var Login = {

    validateFields: function(f){		

		if(Validate.validateNick(f.UN)){
	    f.UN.value=f.UN.value.toLowerCase();
			if(Validate.validatePassword(f.UP)){
			
			   this.checkLogin();

			}
	
		}	

	},
  
	checkLogin: function(){

			if (this.oXmlHttpReq==null) {	

	        this.oXmlHttpReq = CreateXMLHttpRequest();	

	    } else {	

	        this.oXmlHttpReq.abort();	

	    }	


			var oSelf = this;

			this.oXmlHttpReq.onreadystatechange = function()

			{	

			    if (oSelf.oXmlHttpReq.readyState == 4) {	

			        if (oSelf.oXmlHttpReq.status == 200) {	
			        //habra que ver que hace cuando devuelve ok

    					  	try{					  		
    
    					    	oSelf.iIdUser = oSelf.oXmlHttpReq.responseXML.getElementsByTagName("check-login")[0].getAttribute("result");
    
                  
    
    							if(oSelf.iIdUser != "-1"){
    							  	
    							  	Login.doLogin(oSelf.iIdUser,document.getElementById("UN").value,document.getElementById("URL").value);
    
    							} else{	
    
    							  	alert("No has introducido tu usuario o contraseņa correctamente. Vuelve a intentarlo.");
    
    							}
    
    						}catch(exception){
    
    						}
			        }
			     }    	        
			};		

			var sUrl = "/SLogin?UN=" + document.getElementById("UN").value + "&UP=" + document.getElementById("UP").value + "&SID=" + document.getElementById("site-id").value + "&r=" + Math.random();	

			this.oXmlHttpReq.open("GET", sUrl, true);	

			this.oXmlHttpReq.setRequestHeader("Cache-Control", "no-cache");	

			this.oXmlHttpReq.send(null);

		},
    
    doLogin: function (sIdUser, sNick, sUrl){
      var sSiteName = document.getElementById("sitename").value;
      if (isConnected("COMMUNITY_" + sSiteName))
      { 
        CookieRemove("COMMUNITY_" + sSiteName,"/");
      }
      var sContent = sIdUser + "_" + sNick;
      var newCookie = Cookie("COMMUNITY_" + sSiteName,sContent);
      var date = new Date();
      newCookie.expires = new Date(date.setDate(date.getDate() + 30));      
      newCookie.CookieSet();
      
      if (sUrl!=undefined){
        document.location.href = sUrl;
      }
    },
    
    doLogout: function() {    
      CookieRemove("COMMUNITY_" + sSiteName,"/");
      
      document.location.href = document.getElementById("hosts-static").value;
    }  
};

