﻿var LogonBtn;
var LoadImg;
var EmailTxt;
var PswdTxt;
var resultElement;
var MenuDiv;
var GreetingDiv;
var slider;
var sliderIntervalId = 0;
var sliderHeight = 0;
var slideMaxHeight = 225;
var sliding = false;
var slideSpeed = 10;

function pageLoad()
{
    MenuDiv = $get("headerMenu");
    GreetingDiv = $get("headerGreeting");
    slider = $get('slider');
}

function Logon(button, img, txtEmail, txtPswd) { 
    resultElement = $get(button.toString().replace('btnLogon', 'ResultId'));
    resultElement.setAttribute("class", "formError");
    resultElement.setAttribute("className", "formError");
    
    var ParentID = button.toString().replace("btnLogon", "");
    if(ValidateEmailEntry(ParentID) == false) { 
        resultElement.setAttribute("class", "formErrorShow");
        resultElement.setAttribute("className", "formErrorShow");
        return; 
    }
    
    LogonBtn = $get(button);            
    LoadImg = $get(img);
    EmailTxt = $get(txtEmail);            
    PswdTxt = $get(txtPswd);
    LogonBtn.style.display = 'none';
    LoadImg.style.display = 'inline';
    
    var wRequest = new Sys.Net.WebRequest();
    if (window.location.host == "dotd.4imprint.com") {
        wRequest.set_url("/sop/logonpost.aspx");
    } else {
        if(window.location.host == "promotions.4imprint.com") {
            wRequest.set_url("http://promotions.4imprint.com/dealoftheday/sop/logonpost.aspx");
        } else {
            wRequest.set_url("https://secure.4imprint.com/dealoftheday/sop/logonpost.aspx");
        }
    }
    wRequest.set_httpVerb("POST");
    wRequest.add_completed(LoginSuccess);
 
    //alert("Email=" + EmailTxt.value + "&Pswd=" + b64_hmac_sha1(PswdTxt.value, slt) + "&slt=" + slt);
    var requestBody = "Email=" + EmailTxt.value + "&Pswd=" + PswdTxt.value;
    wRequest.set_body(requestBody);
    wRequest.get_headers()["Content-Length"] = requestBody.length;
     
    try{
        wRequest.invoke(); 
    } catch (exception) {
        alert('exception occured, error: ' + exception);
    } finally {}
}

function LoginSuccess(executor, eventArgs) {
    if(executor.get_responseAvailable()) {
        // Clear the previous results.  
        var response = Sys.Serialization.JavaScriptSerializer.deserialize(executor.get_responseData());
//        resultElement.innerHTML = response;
        if(response[0] == "ERROR") {
            resultElement.innerHTML = "";
            resultElement.innerHTML = response[1];
            //slt = response[2];
            LogonBtn.style.display = 'inline';
            LoadImg.style.display = 'none';
            resultElement.setAttribute("class", "formErrorShow");
            resultElement.setAttribute("className", "formErrorShow");
            PswdTxt.value = '';
        } else {
            // Login succeded, so update the menu and greeting
            MenuDiv.innerHTML = "";
            MenuDiv.innerHTML = response[1];
            GreetingDiv.innerHTML = "";
            GreetingDiv.innerHTML = response[2];
            Slide();
            EmailTxt.value = '';
            PswdTxt.value = '';
        }       
    } else {
        resultElement.innerHTML = "";
        resultElement.innerHTML = 'Unable to login. Please <a href="https://secure.4imprint.com/dealoftheday/SOP/Logon.aspx">click here</a> to attempt to login using a different method.';
        resultElement.setAttribute("class", "formErrorShow");
        resultElement.setAttribute("className", "formErrorShow");
        LogonBtn.style.display = 'inline';
        LoadImg.style.display = 'none';
        EmailTxt.value = '';
        PswdTxt.value = '';
    }
}

function Slide() {
    if(sliding) { return; }
    sliding = true;
    if(sliderHeight == slideMaxHeight) {
        sliderIntervalId = setInterval('SlideUpRun()', 15);
    } else {
        var popTriggerLocation = Sys.UI.DomElement.getLocation($get('menuLogon'));
        var divPop = $get('divPop');
        divPop.style.visibility = 'visible';
        Sys.UI.DomElement.setLocation(divPop, popTriggerLocation.x, popTriggerLocation.y + 27);
        sliderIntervalId = setInterval('SlideDownRun()', 15);
        //EmailTxt.focus();
    }
}

function SlideUpRun(){
   
   if(sliderHeight <= 0) {
      sliding = false;
      sliderHeight = 0;
      slider.style.height = '0px';
      clearInterval(sliderIntervalId);
      $get('divPop').style.visibility = 'hidden';
   } else {
      sliderHeight -= slideSpeed;
      if(sliderHeight <0)
         sliderHeight = 0;
      slider.style.height = sliderHeight + 'px';
   }
}

function SlideDownRun()
{
   //slider = document.getElementById('exampleSlider');
   if(sliderHeight>= slideMaxHeight) {
      sliding = false;
      sliderHeight = slideMaxHeight;
      slider.style.height = slideMaxHeight + 'px';
      clearInterval(sliderIntervalId);
   } else {
      sliderHeight += slideSpeed;
      if(sliderHeight> slideMaxHeight) { sliderHeight = slideMaxHeight; }
      slider.style.height = sliderHeight + 'px';
   }
}

function ValidateEmailEntry(parentID)
{
    var valid = true;
    
    if($get(parentID + 'txtLogonEmail').value == "")
    {
        $get(parentID + 'lblReqEmail').style.display = 'inline';
        valid = false;
    } else {
        $get(parentID + 'lblReqEmail').style.display = 'none';
    }
    
    if($get(parentID + 'txtPassword').value == "")
    {
        $get(parentID + 'lblReqPswd').style.display = 'inline';
        valid = false;
    } else {
        $get(parentID + 'lblReqPswd').style.display = 'none';
    }
    
    return valid;
}

// Notify ScriptManager that this is the end of the script.
if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

