﻿
function pageLoad(sender,args){
    //event handlers
     $addHandler(document, "keyup", OnKeyPressUp);
     $addHandler(document, "keydown", OnKeyPress);
     var ModalPopupBehavior1 = $find("programmaticModalPopupBehavior1");
    if (ModalPopupBehavior1!=null) ModalPopupBehavior1.add_showing(onModalShowing);
   //search filter check        
      CheckSearchX();
    }
    
 
    
    
 function OnKeyPressUp(args)
   {
    var txtLoginEmail = document.getElementById("ctl00_LoginView1_Login1_UserName");
    if (txtLoginEmail!=null) 
        {
             if (args.target.id == txtLoginEmail.id) 
                   if(IsValidMail(txtLoginEmail.value))
                      {
                        var textF = document.getElementById("Textforgot");
                          if (textF!=null) 
                                textF.innerHTML="<span> click </span> <a  style='color:Blue; text-decoration: underline; cursor:pointer;'  onclick='onChangePassClick(); onDoPost();' >here </a> <span>and we will email your new password </span> " ;
                      }
                   else
                      {
                          document.getElementById("Textforgot").innerHTML="<span> Please enter a valid email address.</span>";       
                      }
             if(args.keyCode == Sys.UI.Key.enter)
                {
                   SetBtnLoginFocus(args.target.id);
                   SetBtnChangePswdFocus(args.target.id);
                   SetBtnSearchFocus(args.target.id);
                   SetBtnLoginAspxFocus(args.target.id);
                   SetBtnRegistrationSaveFocus(args.target.id);
                }
          }
      //search filter check        
      CheckSearchX();  
   }
   
function OnKeyPress(args)
   {
    if(args.keyCode == Sys.UI.Key.enter) {
        SetBtnOrderFocus(args.target.id);
          SetBtnLoginFocus(args.target.id);
          SetBtnChangePswdFocus(args.target.id);
          SetBtnSearchFocus(args.target.id);
          SetBtnLoginAspxFocus(args.target.id);
          SetBtnRegistrationSaveFocus(args.target.id);
        }
      if(args.keyCode == Sys.UI.Key.esc)
      {
         onModalHiding(); //default.aspx
      }   
  }
  
    //default.aspx
function onModalHiding() {
    var anim =  $find('ctl00_ContentPlaceHolder1_CloseAnimation');
    if (anim!=null) anim.OnClick();
    
     var Featured =  $find("programmaticModalPopupBehaviorFeatured");
     if (Featured!=null) Featured.hide();
 }

 function SetBtnOrderFocus(sourceID) {

     var txtSalesOrder = document.getElementById('ctl00_ContentPlaceHolder1_txtSalesOrder');
     var btnSearch = document.getElementById('ctl00_ContentPlaceHolder1_btnSearch');
     if (txtSalesOrder != null)
         if (sourceID == txtSalesOrder.id) {
             if (btnSearch != null) btnSearch.focus();
     }
 }
    
 function SetBtnLoginFocus(sourceID)
    {
        var txtLoginEmail = document.getElementById("ctl00_LoginView1_Login1_UserName");
        var txtPswd =  document.getElementById("ctl00_LoginView1_Login1_Password");
        var btnLogin =  document.getElementById("ctl00_LoginView1_Login1_LoginButton1");
         if (txtLoginEmail!=null) 
             if (sourceID == txtLoginEmail.id) 
                {
                   if(btnLogin!=null) btnLogin.focus();
                }
          if (txtPswd!=null)
             if (sourceID == txtPswd.id) 
                {
                   if(btnLogin!=null) btnLogin.focus();
                }
    }
    
function SetBtnLoginAspxFocus(sourceID)
    {
        var txtLoginEmail = document.getElementById("ctl00_ContentPlaceHolder1_ctrlLogin1_txtEmailAdd");
        var txtPswd =  document.getElementById("ctl00_ContentPlaceHolder1_ctrlLogin1_txtPassword");
        var btnLogin =  document.getElementById("ctl00_ContentPlaceHolder1_ctrlLogin1_btnReturnCustomer");
         if (txtLoginEmail!=null) 
             if (sourceID == txtLoginEmail.id) 
                {
                   if(btnLogin!=null) btnLogin.focus();
                }
          if (txtPswd!=null)
             if (sourceID == txtPswd.id) 
                {
                   if(btnLogin!=null) btnLogin.focus();
                }
    }
    
function SetBtnRegistrationSaveFocus(sourceID)
    {
        
        var txtName = document.getElementById("ctl00_ContentPlaceHolder1_ctrlCustomerReg1_txtName");
        var txtEmailAdd =  document.getElementById("ctl00_ContentPlaceHolder1_ctrlCustomerReg1_txtEmailAdd");
        var txtEmailConfirm =  document.getElementById("ctl00_ContentPlaceHolder1_ctrlCustomerReg1_txtEmailConfirm");
        var txtPhone =  document.getElementById("ctl00_ContentPlaceHolder1_ctrlCustomerReg1_txtPhone");
        var txtCompanyName =  document.getElementById("ctl00_ContentPlaceHolder1_ctrlCustomerReg1_txtCompanyName");
        var txtPassword =  document.getElementById("ctl00_ContentPlaceHolder1_ctrlCustomerReg1_txtPassword");
        var txtReEnterPassword =  document.getElementById("ctl00_ContentPlaceHolder1_ctrlCustomerReg1_txtReEnterPassword");
        var btnContinue =  document.getElementById("ctl00_ContentPlaceHolder1_ctrlCustomerReg1_btnContinue");
        
         if (txtName!=null)  
                if (sourceID == txtName.id)   
                        if(btnContinue!=null) btnContinue.focus();
         if (txtEmailAdd!=null)  
                if (sourceID == txtEmailAdd.id)   
                        if(btnContinue!=null) btnContinue.focus();
         if (txtEmailConfirm!=null)  
                if (sourceID == txtEmailConfirm.id)   
                        if(btnContinue!=null) btnContinue.focus();
         if (txtPhone!=null)  
                if (sourceID == txtPhone.id)   
                        if(btnContinue!=null) btnContinue.focus();
         if (txtCompanyName!=null)  
                if (sourceID == txtCompanyName.id)   
                        if(btnContinue!=null) btnContinue.focus();
         if (txtPassword!=null)  
                if (sourceID == txtPassword.id)   
                        if(btnContinue!=null) btnContinue.focus();
         if (txtReEnterPassword!=null)  
                if (sourceID == txtReEnterPassword.id)   
                        if(btnContinue!=null) btnContinue.focus();
                        
                
          
    }
    
    
function SetBtnChangePswdFocus(sourceID)
    {
        var txtCPassword = document.getElementById('ctl00_ContentPlaceHolder1_txtCPassword');
        var txtNPassword = document.getElementById('ctl00_ContentPlaceHolder1_txtNPassword');
        var txtRePassword = document.getElementById('ctl00_ContentPlaceHolder1_txtRePassword');
                
         if (txtCPassword!=null) 
                if (sourceID == txtCPassword.id) 
                    {
                       var txtRePassword = document.getElementById('ctl00_ContentPlaceHolder1_btnChange');
                       if(txtRePassword!=null) txtRePassword.focus();
                    }
         if (txtNPassword!=null) 
                if (sourceID == txtNPassword.id) 
                    {
                          var txtRePassword = document.getElementById('ctl00_ContentPlaceHolder1_btnChange');
                       if(txtRePassword!=null) txtRePassword.focus();
                     }
         if (txtRePassword!=null) 
                if (sourceID == txtRePassword.id) 
                    {
                       var txtRePassword = document.getElementById('ctl00_ContentPlaceHolder1_btnChange');
                       if(txtRePassword!=null) txtRePassword.focus();
                    }
    }
    
 //Searh on Default
 function SetBtnSearchFocus(sourceID)
 {
        var txtSearch = document.getElementById('ctl00_ContentPlaceHolder1_txtSearch');
        if (txtSearch!=null) 
              if (sourceID == txtSearch.id) 
                {
                   var btnSearch = document.getElementById('ctl00_ContentPlaceHolder1_btnSearch'); 
                   if(btnSearch!=null) { btnSearch.focus();  }
                }
 }
 
 
  function SearchClear(ako){
     if (ako!=null) 
        {
             var txtSearch = document.getElementById('ctl00_ContentPlaceHolder1_txtSearch');
             if (txtSearch!=null) txtSearch.value = '';
            ako.style.display="none";
        }
    }
    
  function CheckSearchX(){
    var txtSearch = document.getElementById('ctl00_ContentPlaceHolder1_txtSearch');
    CheckClearVisible(txtSearch); 
  }
    
  function CheckClearVisible(searchtxt){
        if (searchtxt!=null) {
            var clearX = document.getElementById('clearX'); 
             if (searchtxt.value.length > 0)
                {
                    if (clearX!=null) 
                         clearX.style.display="block";
                }
             else
                {
                    if (clearX!=null) 
                         clearX.style.display="none";
                }
        }
   }

 function IsTextEmpty() {
    var  usr = document.getElementById("ctl00_LoginView1_Login1_UserName");
    var psw =  document.getElementById("ctl00_LoginView1_Login1_Password");
    var btn =  document.getElementById("ctl00_LoginView1_Login1_LoginButton1");
    if (usr != null && psw != null) {
        if (usr.value.replace(" ","").length == 0 || psw.value.replace(" ","").length == 0) {
            return false; 
        }
    }
}

 function onDoPost()
 {
   var  usr = document.getElementById("ctl00_LoginView1_Login1_UserName");
    var usrTex=usr.value;
    if (usrTex==null) usrTex='';
    if (usr!=null) usr.value='';
    __doPostBack('ForgotPassword',usrTex);
    
 }

   function onChangePass() {
    var  usr = document.getElementById("ctl00_LoginView1_Login1_UserName");
    if (usr!=null)
       if (!IsValidMail(usr.value))
        { 
           document.getElementById("Textforgot").innerHTML="<span> Please enter a valid email address.</span>";
           usr.focus(); 
         }
         else
          { 
              document.getElementById("Textforgot").innerHTML="<span> click </span> <a  style='color:Blue; text-decoration: underline; cursor:pointer;'  onclick='onChangePassClick(); onDoPost();' >here </a> <span>and we will email your new password </span> " ;
          }
    }
 
 function onChangePassClick() {
    document.getElementById("Textforgot").innerHTML="Your password have been sent to your email address. \n Please check your email now."
  }
 

function IsValidMail(textMail) 
{
 var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/; 
  return regex.test(textMail)

}

  
function EnterFocus(e,ctrl,btn) {
    if (ctrl.value.length > 1) {
        if (e.keyCode == 13) {
            btn.click();
        }
    }
}
        
function isNumberKey(metxt,evt,range) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
       return false;
    else
        {
            if (metxt.value > range) alert("we");
            return true;
         }
}
      
      
 // find the first node with the given tagName, starting from the
  // node the event was triggered on; traverses the DOM upwards
 function findElementE(event, tagName) {
    var element = event;
    while (element.parentNode && (!element.tagName ||
        (element.tagName.toUpperCase() != tagName.toUpperCase())))
      element = element.parentNode;
    return element;
  } 
       
    // <input type="hidden"
 var lastme     
 function HideMe(me){
    if (me.title != "Hide Details...")
        me.style.display = 'none';
    if (lastme != null) 
         lastme.style.display = '';
    lastme = me;
 }

        
 function SelectAllCheckboxes(chkMe,gvclient){
    xState=chkMe.checked;
    elm=gvclient.getElementsByTagName("input"); 
    for(i=0;i<elm.length;i++)
        if(elm[i].type=="checkbox") {
            if(elm[i].checked!=xState)
                elm[i].checked = xState;
        }
 }

  function ValidaparentCheckboxes(chkMe,gvclient,chkParent){
    xState=chkMe.checked;
    if (xState==false) {
        chkParent.checked = false;
    }
    else {
        elm=gvclient.getElementsByTagName("input"); 
        cnt = 0
        for(i=0;i<elm.length;i++)
            if(elm[i].type=="checkbox") {
                if(elm[i].checked==false)
                    cnt = cnt + 1;
            }
             if (cnt==0) 
                chkParent.checked = true;  
   }
 
    
 }
 
 function checkForm()  {   
    var checked = false;   
    var cont = document .getElementById ('<%= DListShippingMethods.ClientID %>');  
    if (cont!=null) { 
            var elm=cont.getElementsByTagName("input");  

                for(i=0;i<elm.length;i++)
                    if(elm[i].type=="radio") 
                        if(elm[i].checked) {
                            checked = true;       
                            break;     
                         }

              if(!checked)      alert("you have to choose a button");     
              return false ; 
        }
}
