﻿$(document).ready(function() {
    
    picTrue = new Image(18,18); 
    picTrue.src ="i/ValidateTrue.gif";    

    picFalse = new Image(18,18); 
    picFalse.src ="i/ValidateFalse.gif";    

    $(".ValidateTextBox").blur(function() {  
            
        msg = isValidTextBox(this,$(this).attr("class"));
                         
        validationImage = "#" + this.id.replace("TXT_","VI_");
        validationMessage = "#" + this.id.replace("TXT_","VM_");
                                
        if(msg != ""){                     
            $(validationImage).removeAttr("src");
            $(validationImage).attr("src",picFalse.src);                                               
            $(validationMessage).html(msg);
            $(validationImage).show();
        }else{    
        
            if($(this).attr("class").match(" CheckEmailExists")){    
            
                $.get("Handlers/Handler.ashx", { "action": "CustomerExists", "Email": this.value, "Refresh": new Date().getTime() }, function(data){                            
                                    
                    if(data == "True"){  
                        $(validationImage).removeAttr("src");              
                        $(validationImage).attr("src",picFalse.src);                                               
                        $(validationMessage).html("Already exists");   
                        $(validationImage).show();                     
                    }else{
                        $(validationImage).removeAttr("src");
                        $(validationImage).attr("src",picTrue.src);  
                        $(validationMessage).html("");   
                        $(validationImage).show();                 
                    }   
                                                  
                }); 
                                                                                      
            }else{
                if($(this).attr("class").match(" IsCurrentPassword")){                    
                    $.get("Handlers/Handler.ashx", { "action": "CurrentPassword", "password": this.value, "Refresh": new Date().getTime() }, function(data){                            
                                        
                        if(data == "True"){                         
                            $(validationImage).removeAttr("src");
                            $(validationImage).attr("src",picTrue.src);  
                            $(validationMessage).html("");   
                            $(validationImage).show();                                                                    
                        }else{
                            $(validationImage).removeAttr("src");              
                            $(validationImage).attr("src",picFalse.src);                                               
                            $(validationMessage).html("Incorrect");   
                            $(validationImage).show();                  
                        }                                                         
                    });                                                                                           
                }else{                                                                                                    
                    $(validationImage).removeAttr("src");
                    $(validationImage).attr("src",picTrue.src);  
                    $(validationMessage).html("");            
                    $(validationImage).show();
                }
            }               
        }               
        //$("#div_errors").hide();                         
    });     
         
})

function isValidTextBox(myTextBox, myClasses){
         
    if(myClasses.match(" Child-")){     

        childID = myClasses.substring(myClasses.indexOf("Child-"));
        childID = childID.substring(6);    
        if(childID.indexOf(" ") > 0){
            childID = childID.substring(0,childID.indexOf(" "));
        }  
        childID = "#" + childID;          
        
        childVI = childID.replace("TXT_","VI_");
        childVM = childID.replace("TXT_","VM_");
        if($(childVI).css("display") == "inline"){
        
            $(childVI).removeAttr("src");       
            if($(childID).val() != myTextBox.value){
                $(childVI).attr("src","i/ValidateFalse.gif");
                $(childVM).html("Does not match");
            }else{
                $(childVI).attr("src","i/ValidateTrue.gif");
                $(childVM).html("");                    
            }                
                              
        }                
    }          
                                    
    if(myClasses.match(" Required")){    
        if(myTextBox.value == ""){
            return "Required";
        }                
    }
    
    if(myClasses.match(" MinLength-")){
        MinLength = myClasses.substring(myClasses.indexOf("MinLength-"));
        MinLength = MinLength.substring(10);  
        if(myClasses.match(" Required") || myTextBox.value.length > 0){ 
  
            if(MinLength.indexOf(" ") > 0){
                MinLength = MinLength.substring(0,MinLength.indexOf(" "));
            }  

            if(myTextBox.value.length < MinLength){
                return "Invalid Length"
            }   
        }   
    }
    
    if(myClasses.match(" IsNumeric")){    
        if(!IsNumeric(myTextBox.value)){
            return "Not Numeric";
        }                
    }    
    
    if(myClasses.match(" IsValidEmail")){     
        if(!isValidEmail(myTextBox.value)){
            return "Invalid Email";
        }
    }       
             
    if(myClasses.match(" Parent-")){
    
        parentID = myClasses.substring(myClasses.indexOf("Parent-"));
        parentID = parentID.substring(7);            
        if(parentID.indexOf(" ") > 0){
            parentID = parentID.substring(0,parentID.indexOf(" "));   
        }
        parentID = "#" + parentID;
                   
        if(myTextBox.value != $(parentID).val()){
            return "Does not match";            
        }
             
    }
    
    return "";
                               
}

function displayerror(formelement, msg){

    imageContainer = formelement.replace("TXT_","VI_");
    msgContainer = formelement.replace("TXT_","VM_");

    $(imageContainer).removeAttr("src");
    $(imageContainer).attr("src","i/ValidateFalse.gif");                                               
    $(msgContainer).html(msg);
    $(imageContainer).show();

}

function isValidEmail(Email){

    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!(filter.test(Email))){
        return false;
    }else{
        return true;
    }       

}

function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}
