var IHS = IHS || {};

IHS.expressions = 
    {
        name: /^[a-z-.,()'\"\s]+$/i,
        address: /^[a-z0-9-.,()'\"\s]*$/i,
        zip1: /^\d{5}$/,
        zip2: /^\d{4}$/,
        phone: /^\d+$/,
        mail: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
    };
IHS.validator = function() {
    var validators = new Array();   
    
    var lastElement;
    this.addValidator = function(el, rules) {  
    	if (!el.style)
            {
                el = document.getElementById(el);
                
            }
            if (!el)
            	return; 	
        validators.push(function() {
            

            if (el.className.indexOf("noValidate")>-1)
                return true;
                
            var res = false;
            for (var i in rules)
            {
                res = rules[i](el);
                if (res === true)
                {
                    continue;
                }
                else
                {                
                    break;
                }
            }
            if (res!==true)
            {  
                lastElement = el;
                
            }
            return res;
        });
    }
    
    this.addValidator('Shipping_First_Name', 
        [function(el){
                           
          return (el.value.replace(/\s/g,'').length > 0) || "Please enter your first name.";
            
        },function(el) {
            return IHS.expressions.name.test(el.value) || "Please, enter a valid first name.";
        }]);
    
    this.addValidator('Shipping_Last_Name', 
        [function(el){
            return (el.value.replace(/\s/g,'').length > 0) || "Please enter your last name.";
            
        },function(el) {
            return IHS.expressions.name.test(el.value) || "Your last name does not appear to be valid.";
        }]);
        
    this.addValidator('Shipping_Address_1', 
        [function(el){
            if (el.value.replace(/\s/g,'').length > 0)
            {
                return true;
            }
            else
            {               
                return "Please enter shipping address.";
            }
        },function(el) {
            return (IHS.expressions.address.test(el.value) && !/^\d+$/.test(el.value)) || "Please enter a valid address.";
        }]);
    
    this.addValidator('Shipping_Address_2', 
        [function(el) {
            return (IHS.expressions.address.test(el.value) && !/^\d+$/.test(el.value)) || "Your address does not appear to be valid.";
        }]);
    
    this.addValidator('Shipping_City', 
        [function(el){
                           
          return (el.value.replace(/\s/g,'').length > 0) || "Please enter your city.";
            
        },function(el) {
            return IHS.expressions.name.test(el.value) || "Your city does not appear to be valid.";
        }]);
        
    var szips = $('.zip.shipping, .shipping .zip, .postal.shipping');
    if (szips.length>0)
    {
        this.addValidator(szips[0],
        [function(el){
            return (el.value.replace(/\s/g,'').length > 0) || "Please enter your zip code.";
            
        },function(el) {
            return IHS.expressions.zip1.test(el.value) || "Your zip code does not appear to be valid.";
        }]);
       if (szips.length>1)
        {
        	
            this.addValidator(szips[1]
            ,[function(el) {
                return el.style.display == "none" || el.value.replace(/\s/g,'').length == 0 || IHS.expressions.zip2.test(el.value) || "Your zip code does not appear to be valid.";
            }]
            );
        }
    }
    
    var sphones = $('.phone.shipping'); 
    if (sphones.length>0)
    {
        this.addValidator(sphones[0],
        [function(el){
            return (el.value.replace(/\s/g,'').length > 0) || "Please enter your phone number.";
            
        },function(el) {
            return (IHS.expressions.phone.test(el.value) && el.value.replace(/\s/g,'').length == $(el).attr("maxlength")) || "Your phone number does not appear to be valid.";
        }]);
        
        this.addValidator(sphones[1],
        [function(el) {
            return sphones[0].value.replace(/\s/g,'').length == 0 || (IHS.expressions.phone.test(el.value) && el.value.replace(/\s/g,'').length == $(el).attr("maxlength")) || "Your phone number does not appear to be valid.";
        }]);
        
        this.addValidator(sphones[2],
        [function(el) {
            return sphones[1].value.replace(/\s/g,'').length == 0 || (IHS.expressions.phone.test(el.value) && el.value.replace(/\s/g,'').length == $(el).attr("maxlength")) || "Please, enter valid shipping phone.";
        }]);
    }
    
    this.addValidator('Shipping_Email', 
        [function(el){
                           
          return (el.value.replace(/\s/g,'').length > 0) || "Please enter your Email address.";
            
        },function(el) {
            return IHS.expressions.mail.test(el.value) || "Your Email address does not appear to be valid.";
        }]);
        
    this.isValid = function() {
        for (var i in validators)
        {
            var res = validators[i]();
            if (res === true)
                continue;
            else
            {
                alert(res);  
                lastElement.focus();              
                return false;
            }           
        }
         return true;
    };
};










 
