
//--------------------------
function SecureLogin(f, login, licagree, signup){ //f=form object,login=login failed page[defaults to calling page],licagree=licagree page[defaults to LicagreeDefault]

  var LoginDefault    = location.href;//default login failure (handles expired pw,too many, on hold, etc.)
  var LicagreeDefault = "https://ecom.loislaw.com/snp/licagree.htp"; //for licagree login failure
  var SignupDefault   = "/estore/expired.htm"; //for expired DP login failure

  WaitStart(f,document.body); 
  window.setTimeout("window.AjaxLogin()",10);//to allow showing hourglass

  window.AjaxLogin = function(){
    var url = "https://ecom.loislaw.com/security.htp?remember="
      +((f.remember&&f.remember.type&&f.remember.type.match(/checkbox/i))?((f.remember.checked)?"1":"0"):(f.remember&&f.remember.value)?f.remember.value:"0")
      +((typeof f.username != 'undefined')?"&username="+escape(f.username.value):"")
      +((typeof f.password != 'undefined')?"&password="+escape(f.password.value):"")
      +((typeof f.passwordnew != 'undefined')?"&passwordnew="+escape(f.passwordnew.value):"")
      +((typeof f.secretquestion != 'undefined')?"&secretquestion="+escape(f.secretquestion.value):"")
      +((typeof f.secretanswer != 'undefined')?"&secretanswer="+escape(f.secretanswer.value):"")
      +((typeof f.custaction != 'undefined')?"&custaction="+escape(f.custaction.value):"")
      +"&targeturl="+Url2Path(f.action) //need relative path only
    if(typeof WSRet != 'undefined') WSRet = -1; //for safari callback, see _ImportScript()
    _ImportScript(url, false, window.AjaxLoginCallBack, "typeof WSRet!= 'undefined' && WSRet != -1");
    window.setTimeout("window.AjaxLoginCallBack()",15000); //timeout in case of failed ajax callback
  }
  
  window.AjaxLoginCallBack = function(){
    WaitClear(f,document.body);
    if(typeof WSRet == 'undefined' || WSRet == -1 ){ SubmitCleanForm(); }//call to login server never returned
    if(WSRet == 0){
      //reassign cookie domains (for non www.loislaw.com)
      if(typeof WSIDC != 'undefined' && WSIDC && WSIDC.length){
        document.cookie = WSIDC.replace(/domain=[^;]+/,"");
        var SessId = WSIDC.replace(/^.*LOISLAWID=([^;]+);.*/i,"$1");
        ReplaceInput(f,'SessId',SessId);        
      }
      if(typeof WSRem != 'undefined' && WSRem && WSRem.length)
        document.cookie = WSRem.replace(/domain=[^;]+/,"");      
      SubmitCleanForm();
    }else{
      SubmitFailedForm();
    }
  }  
  
  function SubmitCleanForm(){
    try{
      f.style.visibility = "hidden";
      RemoveInput(f,'password');
      RemoveInput(f,'passwordnew');
      RemoveInput(f,'passwordcon');
      RemoveInput(f,'pu');
      RemoveInput(f,"secretquestion");
      RemoveInput(f,"secretanswer");
      RemoveInput(f,"custaction");
      CleanEcomForm(f);
      CSubmit(f);
    }catch(e){}
  }
  function SubmitFailedForm(){
    try{
      f.style.visibility = "hidden";
      ReplaceInput(f,"WSRet",(WSRet||1));
      ReplaceInput(f,"OLDURL", Path2Url(f.action) );
      
      if(WSRet && (WSRet%10 == 7) ){ //save un/pw for licagree
         ReplaceInput(f,"loginurl", Path2Url(login||location.href) );//incase login after licagree fails (eg too many users)
         RemoveInput(f,"custaction");      
        f.action = Path2Url(licagree||LicagreeDefault);
        if( !f.action.match(/^https/i) ){ RemoveInput(f,'password'); }//for safety
        CSubmit(f);
      }else if(WSRet && (WSRet == 13) ){ //save un/pw for expired PRP
        /**if(!FindInput(f,"promo")){
          ReplaceInput(f,"promo", "EXPPRPLOGIN" );
        }
        f.action = Path2Url(signup||SignupDefault);
        if( !f.action.match(/^https/i) ){ RemoveInput(f,'password'); }//for safety
        CSubmit(f);*/
        document.location.href=SignupDefault; //redirect all previous ECOM users to generic expired page.
      }else{ //all other WSRet failures
        if(WSRet && WSRet < 15 ){RemoveInput(f,'username');}//save un for expired pw
        f.action = Path2Url(login||LoginDefault);
        SubmitCleanForm();
      }
    }catch(e){}
  }
  
  function CSubmit(f){//Submit via GET or POST: determined by Form.method
    if(f && f.method && f.method.match(/GET/i)){ //do a real GET (elimiates https->http warnings)
      var url = Path2Url(f.action);
      url += (url.match(/\?/))?'&':'?';
      for(var i=0; i<f.elements.length; i++){
        url += f.elements[i].name +"="+ escape(f.elements[i].value).replace(/%20/g,"+") +"&";
      }
      url = url.replace(/&$/,"");
      if(f.target && f.target.length){
        window.open(url,f.target);
      }else{
        location.href = url;
      }
    }else{//POST
      f.submit();
    }
  }
  
  function CleanEcomForm(f){//strip Trans-Ecom data
    var c, i=f.elements.length-1;
    for(; i>=0; i--){ 
      c = f.elements[i];
      if(c.name.match(/^Tran|Ecom|Market|Prior/i) ){
        try{f.removeChild(c);}catch(e){try{c.parentNode.removeChild(c);}catch(e){}}
      }
    }
  }
  
  function Path2Url(url){//Relative path to Full URL
    if(!url.match(/^http/i))
      url = location.protocol+"//"+location.host+url;  
    return url;
  }
  function Url2Path(url){//Full URL to Relative path
    return url.replace(/^https?:\/\/[^\/]+/,"")
  }
  
  function ReplaceInput(f,name,value){
    RemoveInput(f,name);
    var i = document.createElement("input");
    i.name = String(name);
    i.type = "hidden";
    i.value = value;
    f.appendChild(i);
  }
  function RemoveInput(f,name){
    var i = FindInput(f,name);
    if(i){try{f.removeChild(i);}catch(e){try{i.parentNode.removeChild(i);}catch(e){}}}
  }
  function FindInput(f,name){
    name = new RegExp("^"+name+"$","i");
    for(var i=0; i<f.elements.length; i++){
      if( String(f.elements[i].name).match(name) ){ return f.elements[i]; }
    }
    return null;
  }
  
  function WaitStart(){
    for(var e,i=0; e=arguments[i]; i++){
      e.style.cursor = "wait";
      if( !e.nodeName.match(/body/i) ){
        e.disabled = true;
        e.savecolor = String(e.style.color);
        e.style.color = "#ccc";
        for(var c = e.childNodes.length-1; c>=0; c--)
          if(e.childNodes[c].style) WaitStart(e.childNodes[c]);
      }
    }
  }
  function WaitClear(){
    for(var e,i=0; e=arguments[i]; i++){
      e.style.cursor = "";
      if( !e.nodeName.match(/body/i) ){
        e.disabled = false;
        e.style.color = e.savecolor;
        for(var c = e.childNodes.length-1; c>=0; c--)
          if(e.childNodes[c].style) WaitClear(e.childNodes[c]);        
      }
    }
  }
  return false; 
}
//--------------------------
function SecureSecretQA(username, password, targeturl, CallBack ){

  var url = "https://ecom.loislaw.com/secuser.htp?targeturl="+escape(targeturl.replace(/^https?:\/\/[^\/]+/,""))+"&username="+escape(username)+"&password="+escape(password);
  //alert(url);
  _ImportScript(url, false, CallBack, "typeof UserInfo != 'undefined'");
  window.SecureSecretQACallback = function(){CallBack();}
}