﻿$(document).ready(function() {
    var regex = new RegExp(/fillform/);
    var results = regex.exec(window.location.href);
    if (results != null) {
        $('input').each(function() {
            if ($(this).attr('type') == "text")
            $(this).val($(this).attr("name"));
        });
        $('.zip').each(function() {
            $(this).val("77401");
        });
        $('.phone').each(function() {
            $(this).val("7135555555");
        });
        $('.email').each(function() {
            $(this).val($(this).attr("name")+"@domain.com");
        });
    }
});

function validateForm() {
    var isFormValid = true;
    $(".validate").each(
        function(index){
            var isElementValid = true;

            if ($(this).is('.confirm')) {
                if ($(this).val() != $('#'+$(this).attr('confirmControlID')).val()) {
                    isElementValid = false;
                }
            }

            if ($(this).is('.date')) {
                re = /^\s*\d{1,2}(\/|-)\d{1,2}(\/|-)\d{4}\s*$/; 
                if ((!$(this).val().match(re)) && ($(this).val().match(/\S/))) {
                    isElementValid = false;
                }
            }
            if ($(this).is('.email')) {
                re = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/; 
                if ((!$(this).val().match(re)) && ($(this).val().match(/\S/))) {
                    isElementValid = false;
                }
            }
            if ($(this).is('.phone')) {
                re = /^\s*\(?\d{3}\)?(-|\.|\s)*\d{3}(-|\.|\s)*\d{4}\s*$/; 
                if ((!$(this).val().match(re)) && ($(this).val().match(/\S/))) {
                    isElementValid = false;
                }
            }
            if ($(this).is('.number')) {
                re = /^\s*\d+\s*$/; 
                if ((!$(this).val().match(re)) && ($(this).val().match(/\S/))) {
                    isElementValid = false;
                }
            }
            if ($(this).is('.money')) {
                re = /^\s*\d+(.\d\d?)?\s*$/; 
                if ((!$(this).val().match(re)) && ($(this).val().match(/\S/))) {
                    isElementValid = false;
                }
            }
            if ($(this).is('.zip')) {
                re = /^\s*\d{5}(-\d{4})?\s*$/; 
                if ((!$(this).val().match(re)) && ($(this).val().match(/\S/))) {
                    isElementValid = false;
                }
            }
            if ($(this).is('.required.radiobuttons')) {
                if (!$("input[name='"+$(this).attr("id")+"']:checked").val()) {
                        $(this).css("background-color","#ff0000");
                        validForm = false;
                }
                else {
                        $(this).css("background-color","");
                }
            } else if ($(this).is('.required')) {
                if (!$(this).val().match(/\S/)) {
                    isElementValid = false;
                }
            }
            if (isElementValid) {
                $(this).css("border","");
            } else {
                $(this).css("border","#ff0000 solid 3px");
                isFormValid = false;
            }
        }
    )
    if (!isFormValid) 
        scroll(0,0);
    return isFormValid;
}
