/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var maxReviewLength=2500;
function ValidateTextAreaForm(){
  //setting to false caters for elementsInputs.length returning 0
  var isValid = false;
  var showMsgBox=true;  
  var elementsInputs;
  var formReview = document.getElementById("TextAreaForm"); 
  for (var intCounter = 0; intCounter < formReview.length; intCounter++) { 
     isValid=ValidateTaFormControl(formReview[intCounter].id,showMsgBox);
     if (!isValid) return isValid; 
   }
   return isValid;
}

function ValidateTaFormControl(controlID,showMsgBox) {
    if (controlID==null){
         DisplayNotification(null,showMsgBox,"No control to validate.");
         return false;
    }
    
    var control=document.getElementById(controlID);
    if (control==null){
         DisplayNotification(null,showMsgBox,"No control to validate.");
         return false;
    }
    
    if (control.type==null || control.type=="label" || control.type=="fieldset" ||control.type=="password")
         return true;
     
    if (control.type=="submit" || control.type=="reset"  || control.type=="hidden")
      return true;
    
    //var control=document.getElementById(controlID);
    var isValid=false;
    //all test area controls are mandatory
    if (IsControlEmpty(control,showMsgBox))
        return false;
    else if (control.id=="taCaptchaResponse")
        isValid=ValidateTaFormCaptcha(control,showMsgBox);
    else if (control.id=="taTextArea")
        isValid=ValidateTextAreaField(control,showMsgBox);
    else if (control.id=="taFormName" || control.id=="taFormForename" || control.id=="taFormSurname")
        isValid=ValidateTaFormName(control,showMsgBox);  
    else if (control.id=="taFormEmail") 
        isValid=ValidateTaFormEmail(control,showMsgBox);
     else if (control.id=="taOptIn" ||control.id=="taFormCountDown") 
         isValid=true;
    else{
      DisplayNotification(null,showMsgBox,"No such control to validate: "+ controlID);
      return false;
    }
    
    //clear the status toolbar in case there is anything there
    if (isValid) DisplayNotification(null,false,"");
    return isValid;
}

function ValidateTaFormName(control,showMsgBox){
   var strName=control.value;
   var whitespace=" \t\n\r";//white space characters TODO is there a trim function????
    
   if (strName.length>40)
      DisplayNotification(null,showMsgBox,control.name + " must not be greater than 40 characters long");
   
   for (x=0; x<strName.length; x++){
       var character=strName.charAt(x);
       if (((character<'a' || character>'z') &&  (character<'A' || character>'Z'))
              && (isNaN(character)) && whitespace.indexOf(character)==-1){
                DisplayNotification(control,showMsgBox,control.name + " can only compose of letters, numbers and spaces");
                return false;
              }
   } 
   
  //if down this far then the name is only composed of alplanumeric charcters,numbers & spaces    
   return true;
}

function ValidateTaFormEmail(control,showMsgBox){
    var strEmail=control.value;
    var whitespace=" \t\n\r";//white space characters TODO is there a trim function????
    
    var strEmailMsg="Emails must be valid and conform to myemail@mail.domain format";
    var at="@";
    var dot=".";
    
    var indexAt=strEmail.indexOf(at)
    var emailLength=strEmail.length
    var indexDot=strEmail.indexOf(dot)
    
    if (strEmail.indexOf(at)==-1){
       DisplayNotification(control,showMsgBox,strEmailMsg);
       return false;
    }

    if (strEmail.indexOf(at)==-1 || strEmail.indexOf(at)==0 || strEmail.indexOf(at)==emailLength){
       DisplayNotification(control,showMsgBox,strEmailMsg);
       return false;
    }

    if (strEmail.indexOf(dot)==-1 || strEmail.indexOf(dot)==0 || strEmail.indexOf(dot)==emailLength){
         DisplayNotification(control,showMsgBox,strEmailMsg);
        return false;
    }

     if (strEmail.indexOf(at,(indexAt+1))!=-1){
        DisplayNotification(control,showMsgBox,strEmailMsg);
        return false;
     }

     if (strEmail.substring(indexAt-1,indexAt)==dot || strEmail.substring(indexAt+1,indexAt+2)==dot){
        DisplayNotification(control,showMsgBox,strEmailMsg);
        return false;
     }

     if (strEmail.indexOf(dot,(indexAt+2))==-1){
        DisplayNotification(control,showMsgBox,strEmailMsg);
       return false;
     }

     
     if (strEmail.indexOf(" ")>=0 || strEmail.indexOf("\t")>=0 || strEmail.indexOf("\n")>=0 ||strEmail.indexOf("\r")>=0){
        DisplayNotification(control,showMsgBox,strEmailMsg);
        return false;
     }

     return true;					
}

function ValidateTextAreaField(control,showMsgBox){
    var formReview = document.getElementById("TextAreaForm");
    var errMsg="Review must be only have a maximum length of " + maxReviewLength +" characters";
    if (formReview.name=="ReviewForm")//else it would be a contact us/gym email form. Any length is applicable here
        if (control.value.length>maxReviewLength){
            DisplayNotification(control,showMsgBox,errMsg);
            return false;
        }
    
    return true;
}

function CheckLengthOfReview(reviewControlID,countDownControlID){
    if (reviewControlID==null || reviewControlID.length==0 || countDownControlID==null || countDownControlID.length==0) return;
    
    var reviewTextArea=document.getElementById(reviewControlID);
    if (reviewTextArea==null) return;
    
    var countDownControl=document.getElementById(countDownControlID);
    if (countDownControl==null)return;
    
    var errMsg="The review can not exceed " + maxReviewLength;
    if (reviewTextArea.value.length<=maxReviewLength){
        countDownControl.value= eval(maxReviewLength-reviewTextArea.value.length);
    }else{
        reviewTextArea.value=reviewTextArea.value.substr(0, maxReviewLength);
        countDownControl.value=0
        
        DisplayNotification(reviewTextArea,true,errMsg);
    }
   
}

function ValidateTaFormCaptcha(control,showMsgBValidateTaFormCaptchaox){
    return true;
}