/*** COPYRIGHT 2011  BY FDM4 INTERNATIONAL INC. - ALL RIGHTS RESERVED ********/
/*** b2c-register.js -- B2C Sign In/User Registration JavaScript           ***/
/*****************************************************************************/
/*B010100 11/10/11 JZ - Alo mods.                                            */
/*B010001 10/19/11 MS - Use siteContent.fdmCookie                            */
/*B010000 08/02/11 MS - Created.                                             */
/*****************************************************************************/

var vCookieName="saveLogin" + siteContent.fdmCookie; /* B010001 */
var vCookieValue= getCookie(vCookieName);

/*****************************************************************************/

function updateLogin(vChecked) {
     if(vChecked)
    {
      var expirydate = new Date();
      expirydate.setTime( expirydate.getTime() + (3650 * 24 * 60 * 60 * 1000) );
      if(vCookieValue!="")
     { deleteCookie(vCookieName, "/", document.domain.substring(document.domain.lastIndexOf(".",document.domain.lastIndexOf(".") - 1), document.domain.length), null); }
      setCookie(vCookieName, $("#email-signin").val() + "|" +  $("#password-signin").val(), expirydate, "/", document.domain.substring(document.domain.lastIndexOf(".",document.domain.lastIndexOf(".") - 1), document.domain.length), null);
     }
     else
    {
      deleteCookie(vCookieName, "/", document.domain.substring(document.domain.lastIndexOf(".",document.domain.lastIndexOf(".") - 1), document.domain.length), null);
    }

} // updateLogin

/*****************************************************************************/

$('.formError').live('click', function() {

    // close error message when you click on it
 $(this).validationEngine('hidePrompt');

return false;

});

// Enable using the <ENTER> key to sign in
$("input#strPassword").live("keypress", function(e) {
    code = e.keyCode ? e.keyCode : e.which;
    // keys ref: http://www.quirksmode.org/js/keys.html
    if(code.toString() == 13) {
       loginRequestPopup('login', this);
  }
});


/*****************************************************************************/

function loginRequestLuxx(action,formobj)  {

var user = $("#email-signin").val();
var pass = $("#password-signin").val();
var loc  = $("#location").val();

var error = $("#error");

   // clear previous error messages
   $('#reg-signin').validationEngine('hide');

 if(user == "") {
  $("#email-signin").validationEngine('showPrompt',"Please enter a user name or email address.",'','topLeft',true);
  $("#email-signin").focus();
  return;
 }

 if(pass == "" && action == "login") {
 $("#password-signin").validationEngine('showPrompt',"Please enter a password.",'','topLeft',true);
    $("#password-signin").focus();
    return;
 }

 $.ajax(
 {
  type: "GET", url: appPath +"/xml/login-request.w?action=login&sponsor="+fdmSponsor+"&userid="+user+"&password="+pass+"&location="+escape(loc), dataType : "xml", cache: false, complete:
   function (data)
   {
    var vLoginObj=$.xmlToJSON(data.responseXML);
    var vResponse="";
    var vValid=vLoginObj.loginRequest[0].valid!="no";

     if(!vValid)vResponse=vLoginObj.loginRequest[0].response[0].code;

     if(!vValid&&!(vResponse=="password"&&action=="hint"))
     {
   $("#password-signin").validationEngine('showPrompt',vLoginObj.loginRequest[0].response[0].Text,'','topLeft',true);
      return;
     }
     if(action=="hint") {
      setMessage("Password Hint: "+vLoginObj.loginRequest[0].hint[0].Text);
      return false;
     }
     if(vValid)
     {
     var vDomain=document.domain.substring(document.domain.lastIndexOf(".",document.domain.lastIndexOf(".") - 1), document.domain.length);
     var vIPCheck=vDomain.split(".");
     if(!isNaN(vIPCheck[1])) vDomain=null;

     var vCookieExp=vLoginObj.loginRequest[0].cookiedate[0].Text;
     var vCookieDate=new Date(vCookieExp);
     var vDateToday=new Date();


      if((vCookieDate<=vDateToday)||(navigator.userAgent.indexOf("Mac") != -1))
        vCookieDate=null;

      var vFirstName=vLoginObj.loginRequest[0].firstname[0].Text;
      var vLastName=vLoginObj.loginRequest[0].lastname[0].Text;
      var vCookieValue=vLoginObj.loginRequest[0].cookievalue[0].Text;
      var vRedirect=vLoginObj.loginRequest[0].redirect[0].Text;

      deleteCookie(fdmCookie,"/",vDomain,null);
      deleteCookie(fdmCookie+fdmSponsor,"/",vDomain,null);

      setCookie(fdmCookie+fdmSponsor,vCookieValue,vCookieDate,"/",vDomain,null);
      setCookie("loggedIn"+fdmCookie,vFirstName+" "+vLastName,vCookieDate,"/",vDomain,null);

      //updateLogin(true);
      document.location.href=vRedirect+"&target=main&sponsor="+fdmSponsor;

     }
    }
 });
} //loginRequestLuxx

/*****************************************************************************/

function registerRequestLuxx() {

 var params = $("#regNewuser").serialize();
 var email  = $("#emailNew").val();

    // validate registration inputs
 if(!validate_regform()) return;

 // validate if email already exists in the Users record
  $.ajax(
  {
   type: "GET",
   url: appPath +"/b2c/b2c-register-work.w?mode=validate&sponsor="+fdmSponsor+"&email="+email,
      dataType : "xml",
      cache: false,
   complete:
    function (data)
    {
  var vRegObj=$.xmlToJSON(data.responseXML);
  var vResponse="";
  var vValid= "no";
      vValid = vRegObj.registerRequest[0].valid != "no";

   if(!vValid)
   {
   vResponse=vResponse=vRegObj.registerRequest[0].response[0].errortype;
   $("#emailNew").validationEngine('showPrompt',vRegObj.registerRequest[0].response[0].Text,'','topLeft',true);
   return false;
   }
   if(vValid)
   {

   createUser();

   }

  }
  });


} // registerRequestLuxx


/*****************************************************************************/

function createAccountLuxx() {

var error = $("#error");
var params = $("#regNewuser").serialize();

// validate registration inputs
if(!validate_regform()) return;

  $.ajax(
 {
  type: "POST",
  data : params,
  url: appPath + "/b2c/b2c-register-work.w" + "?mode=newluxx&target=main&frames=no&sponsor=" + fdmSponsor + getNoCacheParam() ,
  dataType : "xml",
  cache: false,
  complete:
  function (data)
  {
   var vRegObj=$.xmlToJSON(data.responseXML);
   var vResponse="";
   var vValid="no";
       vValid=vRegObj.registerRequest[0].valid!="no";

   if(!vValid)vResponse=vRegObj.registerRequest[0].response[0].errortype;

   if(!vValid)
   {
    $("#emailNew").validationEngine('showPrompt',vRegObj.registerRequest[0].response[0].Text,'','topLeft',true);
    return false;
   }

   if(vValid)
   {

             // Create loggedIn cookie for quick signup
    var firstName = vRegObj.registerRequest[0].firstName[0].Text;
    var lastName = vRegObj.registerRequest[0].lastName[0].Text;

    if (firstName!=""&&lastName!="") {
      var cookieExpDate = new Date(vRegObj.registerRequest[0].cookieExpDate[0].Text);
      setCookie("loggedIn"+siteContent.fdmCookie,firstName+' '+lastName,cookieExpDate,"/",document.domain.substring(document.domain.lastIndexOf(".",document.domain.lastIndexOf(".") - 1), document.domain.length),null);


   // hide registration form
   $("#regNewuser").hide();

   // show thank you
   $("#thankyou").show();
   $("#thankyou").html("Your registration was completed successfully! <br /> You will recieve a registration email confirmation shortly.");

   } //valid != "no"

   }
  }
 });

} // createUser()

/*****************************************************************************/

function setRegMessage(pMsg)
{
 if (document.getElementById("registerMessageArea")!=null) {
  document.getElementById("registerMessageArea").innerHTML=pMsg; }
 else alert(pMsg);

}

/*****************************************************************************/

$(document).ready(function() {
  if(document.getElementById('strUserId')!=null)
   document.getElementById('strUserId').focus();
  if(document.getElementById('savelogin')!=null)
    getSavedLogin();
});


/*****************************************************************************/

function validate_regform ()
{

  var formObj = document.getElementById("regNewuser");
  var password = new String(formObj.passwordNewuser.value);
  var username = new String(formObj.emailNewuser.value);
  var return_val = true;

  // clear out error messages
   $('#regNewuser').validationEngine('hide');


   if(checkBlank( formObj.fnameNewuser ))
   {
    self.scroll(0,0);
 $("#firstName").validationEngine('showPrompt',"You must enter a first name.",'','topLeft',true);
    $('#firstName').focus();
    return false;
   }

   if(checkBlank( formObj.lnameNewuser ))
   {
    self.scroll(0,0);
 $("#lastName").validationEngine('showPrompt',"You must enter a last name.",'','topLeft',true);
    $('#lastName').focus();
    return false;
   }
   if(checkBlank( formObj.emailNewuser ))
   {
    self.scroll(0,0);
 $("#emailNew").validationEngine('showPrompt',"You must enter an email address.",'','topLeft',true);
    $('#emailNew').focus();
    return false;
   }

   if (checkBlank(formObj.passwordNewuser))
   {
      self.scroll(0,0);
   $("#newPassword").validationEngine('showPrompt',"You must enter a password.",'','topLeft',true);
      $("#newPassword").focus();
      return false;
   }

   if (formObj.passwordNewuser.length < 8)
   {
      self.scroll(0,0);
   $("#newPassword").validationEngine('showPrompt',"Your password must be at least 8 characters in length.",'','topLeft',true);
      $("#newPassword").focus();
      return false;
   }

   if ((password.charAt(0) == password.charAt(1)) && (password.charAt(1) == password.charAt(2)))
   {
      self.scroll(0,0);
   $("#newPassword").validationEngine('showPrompt',"Your password's first 3 characters may not be the same.",'','topLeft',true);
      $("#newPassword").focus();
      return false;
   }
   var subpass;
   var endstr = password.length-2;
   for (i=0;i<endstr;i++)
   {
      subpass = password.substring(i,i+3);
      if (username.indexOf(subpass,0) >= 0)
      {
         self.scroll(0,0);
   $("#newPassword").validationEngine('showPrompt',"Your password may not contain any 3 continuous characters in your email/username.",'','topLeft',true);
         formObj.newpassword.focus();
         return false;
      }
   }

   if ((return_val == true) && (formObj.passwordNewuser.value.indexOf(formObj.emailNewuser.value, 0) > -1))
   {
      self.scroll(0,0);
   $("#newPassword").validationEngine('showPrompt',"Your password can not equal or contain your username.",'','topLeft',true);
      $("#newPassword").focus();
      return false;
   }

   if (password.indexOf("_") >= 0 || password.indexOf(" ") >= 0)
   {
    $("#newPassword").validationEngine('showPrompt',"No underscores or spaces may be used as part of your password.",'','topLeft',true);
       $("#newPassword").focus();
       return false;
   }

   if ((return_val == true) && (formObj.confirmpassNewuser.value != formObj.passwordNewuser.value))
   {
      self.scroll(0,0);
   $("#confirmPassword").validationEngine('showPrompt',"Your password confirmation does not match your password.",'','topLeft',true);
      $("#confirmPassword").focus();
     return false;
   }


   var str  = formObj.emailNewuser.value; // email string
   if ( !echeck(str) ) // if syntax is not valid
   {
    self.scroll(0,0);
 $("#emailNew").validationEngine('showPrompt',"Please enter a valid email address to continue.",'','topLeft',true);
    $("#emailNew").focus();
    return false;
   }

   return true;

}

/*****************************************************************************/

function processReset(responseXML)
{
 var vMode = responseXML.mode[0].Text;

 if (vMode=="hint")
 {
  document.getElementById("hint").innerHTML = responseXML.message[0].Text;
  document.getElementById("MsgArea").style.display = "block";
 }

 if (vMode=="reset")
 {
  var vSuccess = responseXML.success[0].Text;

  if(vSuccess!="default")
   document.location.href = responseXML.success[0].Text;
  else
  {
   document.getElementById("hintMsg").innerHTML = responseXML.message[0].Text;
   document.getElementById("MsgArea").style.display = "block";
   document.getElementById("forgot").style.display = "none";
  }
 }

} // processReset()

/*****************************************************************************/

function showPasshint(pHint,pMode)
{
 if (pHint.length < 1) {
  $("#email-signin").validationEngine('showPrompt',"Warning: Email Address cannot be left blank. Please enter a value.",'','topLeft',true);
  $("#email-signin").focus();
  return;
 }

 var vCurrTime = new Date();
 var vCurrSecs  = (((vCurrTime.getHours() * 60) + vCurrTime.getMinutes()) * 60) + vCurrTime.getSeconds();
 var vNoCache = "&nocache=" + vCurrSecs;

 $.ajax({
   type: "GET",
   url: siteContent.appPath + "/b2c/password-reset.w?email=" + pHint + "&mode=" + pMode + "&sponsor=" + siteContent.fdmSponsor + vNoCache, /* B010004 */
   dataType: "xml",
   complete: function(data)
   {
    responseXML = $.xmlToJSON(data.responseXML); // Save XML in JSON

    processReset(responseXML);
   }
 });

} // showPasshint()

/*****************************************************************************/

function getErrorStatus() {
  var topNode = xmlResponse.getElementsByTagName('ajaxml').item(0);
  errorStatus = topNode.getAttribute('status');
  if (errorStatus == "error")
     errorMessage = xmlResponse.getElementsByTagName("error")[0].firstChild.data;
}

/*****************************************************************************/

/********************v B010100 v********************/

function loginRequestAlo(action,formobj)
{
 var user = $("#header-signin").val();
 var pass = $("#header-password").val();
 var loc  = $("#location").val();

 var error = $("#error");

 // clear previous error messages
 $('#header-signin').validationEngine('hide');
 $('#header-password').validationEngine('hide');

 if(user == "")
 {
  $("#header-signin").validationEngine('showPrompt',"Please enter a user name or email address.",'','topRight',true);
  $("#header-signin").focus();
  return;
 }

 if(pass == "" && action == "login")
 {
  $("#header-password").validationEngine('showPrompt',"Please enter a password.",'','topRight',true);
  $("#header-password").focus();
  return;
 }

 $.ajax(
 {
  type: "GET",
  url: appPath +"/xml/login-request.w?action=login&sponsor="+fdmSponsor+"&userid="+user+"&password="+pass+"&location="+escape(loc),
  dataType: "xml",
  cache: false,
  complete:
   function (data)
   {
    var vLoginObj=$.xmlToJSON(data.responseXML);
    var vResponse="";
    var vValid=vLoginObj.loginRequest[0].valid!="no";

     if(!vValid)
      vResponse=vLoginObj.loginRequest[0].response[0].code;

     if(!vValid && vResponse=="userid")
     {
      $("#header-email").validationEngine('showPrompt',vLoginObj.loginRequest[0].response[0].Text,'','topRight',true);
      return;
     }

     if(!vValid&&!(vResponse=="password"&&action=="hint"))
     {
      $("#header-password").validationEngine('showPrompt',vLoginObj.loginRequest[0].response[0].Text,'','topRight',true);
      return;
     }

     if(action=="hint")
     {
      setMessage("Password Hint: "+vLoginObj.loginRequest[0].hint[0].Text);
      return false;
     }

     if(vValid)
     {
      var vDomain=document.domain.substring(document.domain.lastIndexOf(".",document.domain.lastIndexOf(".") - 1), document.domain.length);
      var vIPCheck=vDomain.split(".");

      if(!isNaN(vIPCheck[1]))
       vDomain=null;

      var vCookieExp=vLoginObj.loginRequest[0].cookiedate[0].Text;
      var vCookieDate=new Date(vCookieExp);
      var vDateToday=new Date();

      if((vCookieDate<=vDateToday)||(navigator.userAgent.indexOf("Mac") != -1))
       vCookieDate=null;

      var vFirstName=vLoginObj.loginRequest[0].firstname[0].Text;
      var vLastName=vLoginObj.loginRequest[0].lastname[0].Text;
      var vCookieValue=vLoginObj.loginRequest[0].cookievalue[0].Text;
      var vRedirect=vLoginObj.loginRequest[0].redirect[0].Text;

      deleteCookie(fdmCookie,"/",vDomain,null);
      deleteCookie(fdmCookie+fdmSponsor,"/",vDomain,null);

      setCookie(fdmCookie+fdmSponsor,vCookieValue,vCookieDate,"/",vDomain,null);
      setCookie("loggedIn"+fdmCookie,vFirstName+" "+vLastName,vCookieDate,"/",vDomain,null);

      updateLogin(true);
      document.location.href=vRedirect+"&target=main&sponsor="+fdmSponsor;

     }
    }
 });
} //loginRequestAlo

/********************^ B010100 ^********************/


