﻿// This is a fix for all pages , just every page have login //
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}


// This file defines our own ajax realization for login //
function ShowLogin()
{
   // try to disable modal popup //
   try
   {
   var modalPopupBehavior = $find('ModalPopupBehavior');
   modalPopupBehavior.hide();
   }catch(exxx){};
   // open login //
   try
   {
        openLogin();
   }catch(exx)
   {
        window.location.href = 'register.aspx';
   }
   
}

function openLogin()  
{   
    document.getElementById('login_box_home').style.visibility = 'visible';
    document.getElementById('login_box_home_shim').style.visibility = 'visible';
    $("#login_box_home").load(loginContentUrl,"",function(){document.getElementById('login_login').focus();});
}

function closeLogin()  
{   
    document.getElementById('login_box_home').style.visibility = 'hidden';
    document.getElementById('login_box_home_shim').style.visibility = 'hidden';
    document.getElementById('login_login').blur();
}


function LoginBox()
{
     this.suffix = "";
}

 Function.prototype.bind = function(object) {
 var method = this
 return function() {
       return method.apply(object, arguments) 
 }
 }


LoginBox.xmlHttp = null; 

LoginBox.prototype.createXMLHttpRequest = function()
  { 
      if (window.ActiveXObject)  
       { 
         this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
       } 
      else if (window.XMLHttpRequest)  
       { 
         this.xmlHttp = new XMLHttpRequest(); 
       } 
  } 
 

LoginBox.prototype.constructor = LoginBox;
 
LoginBox.prototype.AddSuffix = function(val)
{
    this.suffix = val;
}
 
LoginBox.prototype.startRequest = function(POST)
 { 
     this.createXMLHttpRequest(); 
     if(this.xmlHttp == null)
     {
        alert("Please use Internet Explorer or Mozilla Firefox. Need AJAX compatible web browser.")
     }
     this.xmlHttp.onreadystatechange = this.handleStateChange.bind(this); 
     
     var url = loginajaxurl;

     var hasSetHeader = this.xmlHttp && (window.ActiveXObject || this.xmlHttp.setRequestHeader); 
     
     var post="";
     if(hasSetHeader)
     {
         for(v in POST) 
         {
              post += v + "=" + encodeURIComponent(POST[v]) + "&";
         }
        this.xmlHttp.open("POST", url , true); 
        this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        this.xmlHttp.setRequestHeader("Content-Length", post.length);
    }else
    {
        url+= "?"+post;
        this.xmlHttp.open("GET", url , true); 
        post = null;
        
    }


     this.xmlHttp.send(post); 
 } 
 

 
 LoginBox.prototype.DoLogin = function()
 {
     this.createXMLHttpRequest();
     var login = document.getElementById('login_login'+this.suffix).value;
     var psw = document.getElementById('login_password'+this.suffix).value;
     /* validation */
     document.getElementById('login_login_v'+this.suffix).style.visibility = (login=="")?'visible':'hidden';
     document.getElementById('login_password_v'+this.suffix).style.visibility = (psw=="")?'visible':'hidden';
     if((login=="")||(psw=="")) return;
     /* processing */
     this.requestType = "login";
     var POST = new Array();
     POST['login'] = login;
     POST['psw'] = psw;
     POST['remember'] = document.getElementById('login_remember'+this.suffix).checked;
     POST['type'] = "login";
     this.startRequest(POST);          
 }
 
 LoginBox.prototype.ShowRecovery = function()
 {
    document.getElementById('login_login_box'+this.suffix).style.display="none";
    document.getElementById('login_forgot_box'+this.suffix).style.display="block";
 
 }
 
 LoginBox.prototype.DoRecovery = function()
 {
    this.createXMLHttpRequest();
    var email = document.getElementById("login_forgot_email"+this.suffix).value;
    this.requestType = "recovery";
    var POST = new Array();
    POST['login'] = email;
    POST['type'] = "recovery";
    this.startRequest(POST);          
 }
 
 
 
 LoginBox.prototype.RecoveryBack = function()
 {
    document.getElementById('login_recovery_ok'+this.suffix).style.display="none";
    document.getElementById('login_login_box'+this.suffix).style.display="block";
 }
 
 LoginBox.prototype.handleStateChange = function()  
 { 
   if(this.xmlHttp.readyState == 4)  
   { 
     if(this.xmlHttp.status == 200)  
     {         
         var response = this.xmlHttp.responseText;
         if(this.requestType == "login")
         {
            eval( response );
            if(res=="OK") 
            {
                window.location.href=loginaspxUrl;
                
                
                return;
            }
            if(res=="FAILED")
            {
                document.getElementById("login_box_failed"+this.suffix).style.display="block";
            }
         }
         
         if(this.requestType == "recovery")
         {
            eval( response );
            if(res)
            {
                document.getElementById('login_forgot_box'+this.suffix).style.display="none";
                document.getElementById('login_recovery_ok'+this.suffix).style.display="block";
                return;
            }else
            {
                document.getElementById('login_forgot_error'+this.suffix).style.display="block";
            }
         }
     }
   } 
 } 
 
var loginbox = new LoginBox();