/*
Desc.: Func to support account creation.
*/

var sAutoAddFriendUrl_g = ""; //The url of the auto add friend elem link, if any.  If none, must be "".

/*
   biPropOnly: Flag to indicate whether we are editing only the prop section
*/
function accntSignUpSupport(siValStr){            
   this.sValStr = siValStr;
   this.validateInput = accntSignUpValidateInput;
   this.submitSelected = accntSignUpSubmitPressed;   
   this.submitComplete = accntSignUpSubmitComplete;
}

/*Superficial validation*/
function accntSignUpValidateInput(){
   iErrCnt = 0;
   
   if ("" == $(csIdFirstNameInput).value){
      $(csIdFirstNameInputErrMsg).value = "<-- Please enter your first name.";     
      iErrCnt++;
   }
   else{
      $(csIdFirstNameInputErrMsg).value = "";
   }
   
   if ("" == $(csIdLastNameInput).value){
      $(csIdLastNameInputErrMsg).value = "<-- Please enter your last name.";      
      iErrCnt++;
   }
   else{
      $(csIdLastNameInputErrMsg).value = "";
   }

   if ("" == $(csIdEmailInput).value){
      $(csIdEmailInputErrMsg).value = "<-- Please enter your email address.";      
      iErrCnt++;
   }
   else{
      $(csIdEmailInputErrMsg).value = "";
   }

   if (0 == iErrCnt){
      if (!isValidEmail(trim($(csIdEmailInput).value))){
         $(csIdEmailInputErrMsg).value = "<-- Email address does not appear valid.";         
         iErrCnt++;
      }
      else{
         $(csIdEmailInputErrMsg).value = "";
      }
   }
   
   
   var sPassword = $(csIdPasswordInput).value;
   
   if (sPassword.length < csMinPwLen){
      $(csIdPasswordInputErrMsg).value = "<-- Please enter a password of at least 6 char.";      
      iErrCnt++;
   }
   else{
      $(csIdPasswordInputErrMsg).value = "";
   }
    
   if (0 == iErrCnt){
      if (!isValidName($(csIdFirstNameInput).value)){
         $(csIdFirstNameInputErrMsg).value = "<-- Name must be letters only.";         
         iErrCnt++;
      }
      else{
         $(csIdFirstNameInputErrMsg).value = "";
      }
   
      if (!isValidName($(csIdLastNameInput).value)){
         $(csIdLastNameInputErrMsg).value = "<-- Name must be letters only.";         
         iErrCnt++;
      }   
      else{
         $(csIdLastNameInputErrMsg).value = "";
      }
   
   }
   
   if (0 == iErrCnt){            
      if ($(csIdRetypePasswordInput).value != $(csIdPasswordInput).value){
         $(csIdRetypePasswordInputErrMsg).value = "<-- Retyped password does not match.";        
         iErrCnt++;
      }
      else{
         $(csIdRetypePasswordInputErrMsg).value = "";
      }                  
      
      if ($(csIdImgValidationInput).value != this.sValStr){
         $(csIdImgValidationInputErrMsg).value = "<-- Please type the value seen in image.";         
         iErrCnt++;         
      }
      else{
         $(csIdImgValidationInputErrMsg).value = "";
      }      
   }
   
   if (!$(csIdTouAgreeInput).checked){
      $(csIdTouAgreeInputErrMsg).value = "<-- Please check to agree with the terms of use.";      
      iErrCnt++;             
   }
   else{
      $(csIdTouAgreeInputErrMsg).value = "";
   }
   
   return iErrCnt;
}

/*
Desc.: Save draft.
In:
   siAutoAddFriendUrl: The url of the auto add friend elem link, if any.  
      If none, must be "".
*/
function accntSignUpSubmitPressed(siAutoAddFriendUrl){
   var url = csServerUrlRoot + '/register_update';   
   sAutoAddFriendUrl_g = siAutoAddFriendUrl; //auto add link url 
   
   if (0 == this.validateInput()){           
      //Note: Names in "parameters" must match def in sysDefs.php section "Defs for params of account signup (register)"
      var myAjax = new Ajax.Request(
         url,
         {
            method: 'post',
            parameters: {                              
               first_name: $(csIdFirstNameInput).value,
               last_name: $(csIdLastNameInput).value,
               email: $(csIdEmailInput).value,
               password: $(csIdPasswordInput).value,               
            },
               
            onComplete: this.submitComplete
         }); //new Ajax.Request
   }
}

/*

Note: All ajax on complete function are callback where only the func is passed and not the obj.  Inside callbacks, you cannot access 
   object data members.
*/
function accntSignUpSubmitComplete(iRequest){
   if (csAjaxRespPass == iRequest.responseText){       

      if ("" == sAutoAddFriendUrl_g){         
         notifBoxJsObj.show("Account signup successful.");
         setTimeout("",csNotifTimeout);
         window.location = csServerUrlRoot + '/';
      }
      else{
         notifBoxJsObj.show("Account signup successful, auto adding friend.");
         setTimeout("",csNotifTimeout);
         window.location = sAutoAddFriendUrl_g;
      }
   }
   //Check if failure string found.
   else if (iRequest.responseText.search(csAjaxRespFail) >= 0){ 
      iAjaxRespDelimOffset = iRequest.responseText.indexOf(csAjaxRespDelim);
      
      //If error message specification found.
      if (iAjaxRespDelimOffset >= 0){            
         sErrorMsg = iRequest.responseText.substring(iAjaxRespDelimOffset+1);  
         notifBoxJsObj2.show(sErrorMsg);
      }
      else{
         notifBoxJsObj2.show("An error occured while trying to create account.");
      }
   }
   //Unknown error.
   else{
      notifBoxJsObj2.show("An error occured while trying to create account.");
   }
}


