


var hsLogins;
function FindHsLoginCallback(name)
{
    if(hsLogins != null)
    {
        var hslObj;
        for(var i=0; i<hsLogins.length; i++)
        {
            hslObj = hsLogins[i];
            if(hslObj.name == name)
                return hslObj;
        }
    }
    return null;
}

function sureLogout(){return confirm("Are you sure you want to logout?");}

HsLoginCallback.prototype.name = null;
HsLoginCallback.prototype.callbackUrl = null;
HsLoginCallback.prototype.userBoxId = null;
HsLoginCallback.prototype.passwordBoxId = null;
HsLoginCallback.prototype.loggedOutMessagesElementId = null;
HsLoginCallback.prototype.loggedInMessagesElementId = null;
HsLoginCallback.prototype.loginDivId = null;
HsLoginCallback.prototype.loginDiv2Id = null;
HsLoginCallback.prototype.loggedinDivId = null;
HsLoginCallback.prototype.loggedinDiv2Id = null;
HsLoginCallback.prototype.controlIdsPrefix = null;
HsLoginCallback.prototype.actionAfterLogin = null;
HsLoginCallback.prototype.actionAfterLogout = null;
HsLoginCallback.prototype.xmlhttp = null;
function HsLoginCallback(parentElementId, callbackUrl, userBoxId, passwordBoxId, loggedOutMessagesElementId, loggedInMessagesElementId, loginDivId, loggedinDivId,
  actionAfterLogin, actionAfterLogout, isCurrentlyLoggedIn, loginDiv2Id, loggedinDiv2Id)
{
    this.name = parentElementId;
    this.callbackUrl = callbackUrl.replace("http:", "https:");
    this.userBoxId = userBoxId;
    this.passwordBoxId = passwordBoxId;
    this.loggedOutMessagesElementId = loggedOutMessagesElementId;
    this.loggedOutMessagesElementId = loggedOutMessagesElementId;
    this.controlIdsPrefix = (parentElementId == "")? "" : parentElementId + "_";
    this.loginDivId = loginDivId;
    if (loginDiv2Id != null) this.loginDiv2Id = loginDiv2Id;
     else this.loginDiv2Id = "#";
    this.loggedinDivId = loggedinDivId;
    if (loggedinDiv2Id != null) this.loggedinDiv2Id = loggedinDiv2Id;
     else this.loggedinDiv2Id = "#";
    this.actionAfterLogin = actionAfterLogin;
    this.actionAfterLogout = actionAfterLogout;
    
    if(hsLogins == null)
        hsLogins = new Array();
    hsLogins.push(this);
    
    if(isCurrentlyLoggedIn != null)
    {
        this.loggedInSwitch(isCurrentlyLoggedIn);
    }
}
HsLoginCallback.prototype.DoLogin = function(stateChangedMethod){
  	this.xmlhttp=null;
	if (window.XMLHttpRequest)
	{//code for all new browsers
		this.xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{// code for IE5 and IE6
		this.xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	  
	if (this.xmlhttp!=null)
	{
	    this.xmlhttp.myHsLoginCallback = this;
	    var d = new Date();
		var url = this.callbackUrl + "?from=client&act=login&rnd=" + d.getMilliseconds() + "&_username=" + $('#'+this.userBoxId).val() + "&_password=" + $('#'+this.passwordBoxId).val();
		url += "&url=" + urlencode(location);
		this.xmlhttp.onreadystatechange=stateChangedMethod;
		//paragraphRequests.push(xmlhttp);
		this.xmlhttp.open("GET",url,true);
		this.xmlhttp.send(null);
	}
	else{/*alert("Your browser does not support XMLHTTP.");*/}
}
HsLoginCallback.prototype.DoLogout = function(stateChangedMethod){
  	this.xmlhttp=null;
	if (window.XMLHttpRequest)
	{//code for all new browsers
		this.xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{// code for IE5 and IE6
		this.xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	  
	if (this.xmlhttp!=null)
	{
	    this.xmlhttp.myHsLoginCallback = this;
	    var d = new Date();
		var url = this.callbackUrl + "?from=client&act=logout&rnd=" + d.getMilliseconds();
		url += "&url=" + urlencode(location);
		this.xmlhttp.onreadystatechange=stateChangedMethod;
		//paragraphRequests.push(xmlhttp);
		this.xmlhttp.open("GET",url,true);
		this.xmlhttp.send(null);
	}
	else{/*alert("Your browser does not support XMLHTTP.");*/}
}

HsLoginCallback.prototype.LoginResponding = function() {
    if (this.xmlhttp.readyState == 4) {// 4 = "loaded"
        if (this.xmlhttp.status == 200) {// 200 = OK
            // ...our code here...

            var response = $(this.xmlhttp.responseXML).find("loginResponse");

            var messages = response.find("message");
            if (messages.length > 0) {
                var answer = messages.text();
                $('#' + this.loggedOutMessagesElementId).html(answer);
                $('#' + this.loggedInMessagesElementId).html(answer);
            }

            var controlIdsPrefix = this.controlIdsPrefix;
            response.find("validator").each(
      function() {
          var validator = $(this);

          var isValid = (validator.attr("isValid") == "true");

          var controlName = validator.attr("controltoValidate");
          var box = $('#' + controlIdsPrefix + controlName);
          if (box != null) {
              box.parent().toggleClass("error", !isValid);
          }
      });

            var success = response.attr("positive") == "true";
            var loggedIn = response.attr("loggedin") == "true";
            var newUrl = response.find("newUrl").text();
            this.loggedInSwitch(loggedIn, success, newUrl);
        }
        else {
            alert("Problem retrieving data");
        }
    }
}

HsLoginCallback.prototype.LogoutResponding = function()
{
  if (this.xmlhttp.readyState==4){// 4 = "loaded"
    if (this.xmlhttp.status==200){// 200 = OK
      // ...our code here...
      
      var response = $(this.xmlhttp.responseXML).find("loginResponse");
      
      var messages = response.find("message");
      if(messages.length > 0)
      {
        var answer = messages.text();
        $('#'+this.loggedOutMessagesElementId).html(answer);
        $('#'+this.loggedInMessagesElementId).html(answer);
      }
      
      var success = response.attr("positive") == "true";
      var loggedIn = response.attr("loggedin") == "true";
      var newUrl = response.find("newUrl").text();
      this.loggedInSwitch(loggedIn, success, newUrl);
      
      var newUrls = response.find("newUrl");
      if(newUrls.length > 0)
      {
        var loc = newUrls.text();
        window.location = loc;
      }
    }
    else{
      alert("Problem retrieving data");
    }
  }
}

HsLoginCallback.prototype.loggedInSwitch = function(loggedIn, hasChanged, newUrl) {
    var hslObj;
    for (var i = 0; i < hsLogins.length; i++) {
        hslObj = hsLogins[i];

        $('#' + hslObj.passwordBoxId).val("");

        var loginDiv = $('#' + hslObj.loginDivId);
        var loginDiv2 = $('#' + hslObj.loginDiv2Id);
        var loggedInDiv = $('#' + hslObj.loggedinDivId);
        var loggedInDiv2 = $('#' + hslObj.loggedinDiv2Id);

        if (loggedIn) {
            if (hasChanged) //(loggedInDiv.length > 0) && (loggedInDiv[0].style.display != "none")) //was logged out
            {
                if (hslObj.actionAfterLogin == "redirect") {
                    if(newUrl != "") window.location = newUrl;
                }
                else {
                    eval(hslObj.actionAfterLogin);
                }
            }
            loginDiv.hide();
            loginDiv2.hide();
            loggedInDiv.show();
            loggedInDiv2.show();
        }
        else {
            if (hasChanged) //(loginDiv.length > 0) && (loginDiv[0].style.display != "none")) //was logged in
            {
                if (hslObj.actionAfterLogout == "redirect") {
                    if (newUrl != "") window.location = newUrl;
                }
                else {
                    eval(hslObj.actionAfterLogout);
                }
            }
            loginDiv.show();
            loginDiv2.show();
            loggedInDiv.hide();
            loggedInDiv2.hide();



        }
    }
}