// customLib.js
//
// Custom JS library
//

var strFunction = '';
var locatorWin;

function doLogin(strFunction){

  var strResponse = '';
  var strTemp = '';
  var strUser = document.getElementById('txtUserName').value;
  var strPassword = document.getElementById('txtPassword').value;
  var argCount = doLogin.arguments.length;

  if ( !IsBlank(strUser) && !IsBlank(strPassword) )
  {
    document.body.style.cursor="wait";
    var showTimer; // = setTimeout("showPopWin('','<DIV id=\"modalContent\" style=\"margin-top:25px;\"><TABLE align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"200px\"><TR><TD id=\"loadingMsg\"align=\"center\"><span style=\"font-size:10pt;font-weight:bold;\">Retrieving Login Information.<br>Please Wait...</span></TD></TR></TABLE></DIV>',325,100,refreshCurrentPage);", 100);
    var hideTimer; // = setTimeout("hidePopWin(true);", 500);

    /* Grey out the form here */
    if (browserVersion == 1) {
      window.onkeypress = stopEntry;
      window.onclick = stopEntry;
    }

    blnDisabled = true;
    var loc = document.URL.toString().toLowerCase();
    loc = loc.substring(0,loc.indexOf('customerscripts'));
    //loc=loc.replace('http:','https:');
    //if (browserVersion==1) {
    //  strResponse = xmlHTTPCall(loc+'Includes/ajaxFunctions.aspx?type=login&user=' + strUser + '&pwd=' + strPassword, false,false,false);
    //}
    //else
    strResponse = xmlHTTPCall('../Includes/ajaxFunctions.aspx?type=login&user=' + strUser + '&pwd=' + strPassword, false,false,false);
    var aryResponse = strResponse.split('|',20);
    try {
      if ( aryResponse[0] == 'true' ) {
        if ( aryResponse.length > 1 && Trim(aryResponse[1].toLowerCase()) == 'admin' ) {
          location.replace('../CustomerScripts/page.aspx?page=admin');
          return false;
        }
        else {
          try {
            if (document.getElementById('loginTable')!=null)
              document.getElementById('loginTable').style.display='none';
          }
          catch (e) { }
          
          try {
            document.getElementById('loginBox').style.display = 'none';
            if (document.getElementById('loginError') != null) {
              document.getElementById('loginError').innerHTML = '';
              document.getElementById('loginError').style.display = 'none';
              if (document.getElementById('loginErrorText') != null) {
                document.getElementById('loginErrorText').style.display = 'none'; 
              }
            }
          }
          catch (e) { }

          if ( strFunction == 'checkout'){
            document.getElementById('billingFirstName').value = aryResponse[2];
            document.getElementById('billingInitial').value = aryResponse[3];
            document.getElementById('billingLastName').value = aryResponse[4];
            document.getElementById('billingAddress1').value = aryResponse[5];
            document.getElementById('billingAddress2').value = aryResponse[6];
            document.getElementById('billingCity').value = aryResponse[7];

            SetSelectValue('cboBillingState',aryResponse[8]);

            document.getElementById('billingZip').value = aryResponse[9];
            document.getElementById('billingPhone').value = aryResponse[10];
            document.getElementById('billingEmail').value = aryResponse[11];
            blnDisabled = false;
            window.location.reload();
            return false;
          }
          else if ( strFunction == 'custom' ) {
            blnDisabled = false;
            if ( argCount > 1 ) 
              window.location.replace(doLogin.arguments[1].toString());
            else
              window.location.reload();
            return false;
          }
          else {

            document.body.style.cursor='default';
            blnDisabled = false;
            //window.location.replace('../CustomerScripts/page.aspx?type=defaultbrowse');
            siteNavigate('../CustomerScripts/page.aspx','',1,0);
            return false;
          }
        }
      }
      else {
        try {
          if (document.getElementById('loginError') != null) {
            if ( aryResponse[1]+'' == 'undefined' || aryResponse[1] == '' || aryResponse[1] == null ) {
              document.getElementById('loginErrorText').innerHTML = 'Unable to Login. Please try again.';
            }
            else {
              document.getElementById('loginErrorText').innerHTML = aryResponse[1];
            }
            clearTimeout(hideTimer);
            document.getElementById('loginErrorText').style.display = '';
            document.getElementById('loginError').style.display = '';
          }
        }
        catch (e) { 
          document.body.style.cursor='default';
          blnDisabled = false;
        }
      }
    }
    catch (e) { 
      //TODO
    }

    if (browserVersion == 1) {
      window.onkeypress = null;
      window.onclick = null;
    }

  }
  
  document.body.style.cursor='default';
  blnDisabled = false;
  //if (gPopupIsShown)
    setTimeout("hidePopWin(false);",150);

  return false;
}


function displayInline(stype) {
   
    var iwidth=535;
    var iheight=310;
    var strOpts = '';
    var currentTime = new Date();

    hs.dimmingOpacity = 0.25;
    hs.onDimmerClick = function() {
      return false;
    };
    
    if (displayInline.arguments.length > 1 )
      strOpts = displayInline.arguments[1];

    strOpts += '&currenttime=' + currentTime.getTime();

    hs.Expander.prototype.onAfterClose = function() {
       this.onDimmerClick = null;
    };

    switch (stype.toLowerCase()) {
      
      case 'storemaintenance':
          iwidth=825;
          iheight=525;
          hs.htmlExpand(document.getElementById('pageLink'), { width: iwidth, height:iheight, objectType: 'iframe', objectWidth: iwidth, objectHeight: iheight, src: '../CustomerScripts/page.aspx?page=storemaintenance' + strOpts, preserveContent: false, headingText: 'Store Maintenance' } );      
          break;
      case 'locatorwindow':
          hs.htmlExpand(document.getElementById('pageLink1'), { contentId: 'locatorWindow' } );
          //iwidth=300;
          //iheight=175;
          //hs.htmlExpand(document.getElementById('pageLink1'), { width: iwidth, height:iheight, objectType: 'iframe', objectWidth: iwidth, objectHeight: iheight, src: '../CustomerScripts/page.aspx?page=locatorwindow' + strOpts, preserveContent: false, headingText: '' } );      
          break;
        break;
      case 'storelocator':
          iwidth=850;
          iheight=625;
          hs.htmlExpand(document.getElementById('pageLink'), { width: iwidth, height:iheight, objectType: 'iframe', objectWidth: iwidth, objectHeight: iheight, src: '../CustomerScripts/page.aspx?page=storelocator' + strOpts, preserveContent: false, headingText: 'Store Locator' } );
        break;
    }

}

function sendReminder(objField) {

  try {
    if (document.getElementById(objField).value != '') { // || !validateEMail(document.getElementById(objField).value)){
      try {
        var postData;
        postData = '&a='+Url.encode(document.getElementById(objField).value);
        var strResponse = xmlHTTPCall('../Includes/ajaxFunctions.asp?type=passwordreminder'+postData,false,false,false);
        if ( strResponse.toLowerCase().indexOf('true')!=-1 )
          showPopWin('','<DIV id="modalContent" style="margin-top:25px;"><TABLE align="center" border="0" cellpadding="0" cellspacing="0" width="200px"><TR><TD id="loadingMsg"align="center"><span style="font-size:10pt;font-weight:bold;">Reminder Sent Successfully.</span></TD></TR></TABLE></DIV>',250,125,redirectHome);
        else
          showPopWin('','<DIV id="modalContent" style="margin-top:25px;"><TABLE align="center" border="0" cellpadding="0" cellspacing="0" width="200px"><TR><TD id="loadingMsg"align="center"><span style="font-size:10pt;font-weight:bold;">Unable to Locate User. Please try Again</span></TD></TR></TABLE></DIV>',250,125,null);
      }
      catch (e) {
        showPopWin('','<DIV id="modalContent" style="margin-top:25px;"><TABLE align="center" border="0" cellpadding="0" cellspacing="0" width="200px"><TR><TD id="loadingMsg"align="center"><span style="font-size:10pt;font-weight:bold;">Unable to Locate User. Please try Again</span></TD></TR></TABLE></DIV>',250,125,null);
      }
    }
    else {
      showPopWin('','<DIV id="modalContent" style="margin-top:25px;"><TABLE align="center" border="0" cellpadding="0" cellspacing="0" width="200px"><TR><TD id="loadingMsg"align="center"><span style="font-size:10pt;font-weight:bold;">Please Enter Valid Email Address And Try Again</span></TD></TR></TABLE></DIV>',250,125,null);
    }
  }
  catch (e) { 
    showPopWin('','<DIV id="modalContent" style="margin-top:25px;"><TABLE align="center" border="0" cellpadding="0" cellspacing="0" width="200px"><TR><TD id="loadingMsg"align="center"><span style="font-size:10pt;font-weight:bold;">Please Enter Valid Email Address. Please try Again</span></TD></TR></TABLE></DIV>',250,125,null);
  }
}

function findStore() {

  var strResponse;

  try {

      var store = cboStoreNumber.getSelectedValue();      
      if (store == null) store = cboStoreNumber.getComboText();
      if (store == lastStore)
        return false;
      else if (store != '') {
        clearStoreScreen();
        lastStore = store;
        var response = xmlHTTPCall('../Includes/ajaxFunctions.aspx?type=findstore&store='+store,false,true,false);
        findStoreReturn(response, store);
      }
  }
  catch (e) {  }
  return false;

}

function findStoreReturn(strResponse,strStore) {

  var root;
  var orderLevel = 'stores';
  var storeNode;
  var store;

  try {
    if( strResponse != null) {

      if (browserVersion == 1 ){
        var xmlDoc = new Sarissa.getDomDocument();
        xmlDoc.async = false;
        xmlDoc.onreadystatechange=IExmlVerify;
        xmlDoc.loadXML(strResponse);
        var root = xmlDoc.documentElement;
      }
      else{
        var xmlDoc = strResponse;
        root = xmlDoc.getElementsByTagName('stores').item(0);
        if ( root == null) {
          return false;
        }
      }

      storeNode = root.selectSingleNode("//stores/store[@id='" + strStore+ "']");
      if (storeNode == null) {
          return false;
      }

      if (storeNode.getAttribute('active')=='1')
        document.getElementById('chkStoreActive').checked = true;
      else
        document.getElementById('chkStoreActive').checked = false;
        
      lastStore = storeNode.getAttribute('id');
      document.getElementById('txtStoreAddress1').value = storeNode.getAttribute('address1');
      document.getElementById('txtStoreAddress2').value = storeNode.getAttribute('address2');
      document.getElementById('txtStoreCity').value = storeNode.getAttribute('city');
      SetSelectValue('cboStoreState',storeNode.getAttribute('state'));
      document.getElementById('txtStoreZip').value = storeNode.getAttribute('zip');
      document.getElementById('txtStoreEmail').value = storeNode.getAttribute('email');
      document.getElementById('txtStorePhone').value = storeNode.getAttribute('phone1');
      document.getElementById('txtStoreFax').value = storeNode.getAttribute('phone2');
      document.getElementById('txtStoreLatitude').value = storeNode.getAttribute('lat');
      document.getElementById('txtStoreLongitude').value = storeNode.getAttribute('lon');
      document.getElementById('txtStoreInformation').value = storeNode.getAttribute('information');

    }
  }
  catch (e) { }
  
  return true;
  
}

function clearStoreScreen() {

  try {
  
    document.getElementById('chkStoreActive').checked = false;
    document.getElementById('txtStoreAddress1').value = '';
    document.getElementById('txtStoreAddress2').value = '';
    document.getElementById('txtStoreCity').value = '';
    SetSelectValue('cboStoreState','');
    document.getElementById('txtStoreZip').value = '';
    document.getElementById('txtStoreEmail').value = '';
    document.getElementById('txtStorePhone').value = '';
    document.getElementById('txtStoreFax').value = '';
    document.getElementById('txtStoreLatitude').value = '';
    document.getElementById('txtStoreLongitude').value = '';
    document.getElementById('txtStoreInformation').value = '';
  
  }
  catch (e) { }

}

function UpdateStore() {

  try {
      var store = cboStoreNumber.getSelectedValue();      
      var name = cboStoreNumber.getComboText();
      if (store == null) store = cboStoreNumber.getComboText();
      var active = document.getElementById('chkStoreActive').checked;
      var address1 = document.getElementById('txtStoreAddress1').value;
      var address2 = document.getElementById('txtStoreAddress2').value;
      var city = document.getElementById('txtStoreCity').value;
      var state = GetSelectValue('cboStoreState');
      var zip = document.getElementById('txtStoreZip').value;
      var email = document.getElementById('txtStoreEmail').value;
      var phone = document.getElementById('txtStorePhone').value;
      var fax = document.getElementById('txtStoreFax').value;
      var lat = document.getElementById('txtStoreLatitude').value;
      var lon = document.getElementById('txtStoreLongitude').value;
      var information = document.getElementById('txtStoreInformation').value;
      
      
      if (store == 'None' || store == '' || store == null) {
          alert("You must enter a Store Name! ");
          return false;
      }
      if (address1==''|| city==''||state==''||zip=='') {
          alert("You must enter a Complete Address! ");
          return false;
      }
      
      var update = '<store>';
      update += '<id><![CDATA['+store+']]></id>';
      update += '<name><![CDATA['+name+']]></name>';
      update += '<active>'+active+'</active>';
      update += '<address1><![CDATA['+address1+']]></address1>';
      update += '<address2><![CDATA['+address2+']]></address2>';
      update += '<city><![CDATA['+city+']]></city>';
      update += '<state>'+state+'</state>';
      update += '<zip>'+zip+'</zip>';
      update += '<email><![CDATA['+email+']]></email>';
      update += '<phone>'+phone+'</phone>';
      update += '<fax>'+fax+'</fax>';
      update += '<lat>'+lat+'</lat>';
      update += '<lon>'+lon+'</lon>';
      update += '<information><![CDATA['+information+']]></information>';
      update += '</store>';      
      
      var response = xmlHTTPCall('../Includes/ajaxFunctions.aspx?type=updatestore&data='+update,false,true,false);
      updateStoreReturn(response);

      return true;
  }
  catch (e) { return false; }


}

function updateStoreReturn(strResponse) {

  var root;
  var storeNode;
  var store;

  try {
    if( strResponse != null) {

      if (browserVersion == 1 ){
        var xmlDoc = new Sarissa.getDomDocument();
        xmlDoc.async = false;
        xmlDoc.onreadystatechange=IExmlVerify;
        xmlDoc.loadXML(strResponse);
        var root = xmlDoc.documentElement;
      }
      else{
        var xmlDoc = strResponse;
        root = xmlDoc.getElementsByTagName('stores').item(0);
        if ( root == null) {
          return false;
        }
      }

      storeNode = root.selectSingleNode("//stores/store");
      if (storeNode == null) {
          return false;
      }

      lastStore = storeNode.getAttribute('id');

      try {
        document.getElementById('txtStoreLatitude').value = storeNode.getAttribute('lat');
        document.getElementById('txtStoreLongitude').value = storeNode.getAttribute('lon');
      }
      catch (e) { }
    }
  }
  catch (e) { return false; }
  
  return true;
  
}
