﻿/*How to use this script*/
/*
    you must have a reference to the Jquery library before this script.
    I prefer to link to the google jquery librarys to reduce downloads and be sure that the link 
    always points to the latest library
    e.g.<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    
    For this to work with asp.net controls and prevent a postback based on validation failing you need to add the 
    onclientclick attribute to load the script and prevent attach code to all effected controls.
    e.g. <asp:ImageButton OnClientClick="return validatecontrols(true)" etc...
    
    Then is you want to have a message showup indication a failure at the location of the button, add a div/span or some other 
    control with an id of "error"
    
    Each input control that needs an entry (read asp:textbox) requires to reference one of the following classes.
    
        required
        needed
        mail
        phone
    
    The required and needed are the same. An error will produce (by default) a message indicating "an entry is expected"
    the mail and phone trigger a test against a regular expression validation function (based on US standard formats)
    Returning an error if the entry fails validation.
    
   (Mail and phone also accept an optional "opt" class. if the control has the "opt" class the code only validates
   non blank entrys)
   
   Required and Needed also work against "radio list box's"

*/




$(
        function() {
            $("input.phone").attr("title", "e.g. (000)000-0000");
            $("input.email").attr("title", "e.g. someone@some-domain.com");
            $("input.needed").attr("title", "none blank entry required");
            $("input.required").attr("title", "none blank entry required");
            $("SELECT.required,SPAN.required,TABLE.required").attr("title", "Select One");
        }
        );
        //Function to Test Email//
        function validateEmail(value) {
            var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
            return emailPattern.test(value);
        }
        //Function to Test Phone Number (U.S. Style)//
        function validatePhone(value) {
            var phone = /^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/;
            return phone.test(value)
        }
        //Looks for all input controls that have the appropriate classes and tests to see if they are completed correctly//
        function validatecontrols(AstrixOnly) {
            $(".val").remove();
            var good = true;
            var required = "* required";
            var email ="* invalid email";
            var phone ="* invalid phone";
            if (AstrixOnly == true) {
                required = "*";
                
                email = "*";
                phone = "*";
            }
            
            $("input.needed").each(
            function(index) {
                if ($(this).attr("value") == "") {
                    $(this).after("<span style='color:red' class='val'>" + required +  "</span>");
                    good = false;
                }
            });
            
            $("input.required").each(
            function(index) {
                if ($(this).attr("value") == "") {
                    $(this).after("<span style='color:red' class='val'>" + required + "</span>");
                    good = false;
                }
            });
            
            $("input.email").each(
                function(index) {
                    if ($(this).attr("value") == "" && $(this).hasClass("opt") == true)
                    { }
                    else {
                        if (validateEmail($(this).attr("value")) == false) {
                            $(this).after("<span style='color:red' class='val'>" + email + "</span>");
                            good = false;
                        } 
                    }
                });

                $("input.phone").each(
                function(index) {
                    if ($(this).attr("value") == "" && $(this).hasClass("opt") == true)
                    { }
                    else {
                        if (validatePhone($(this).attr("value")) == false) {
                            $(this).after("<span style='color:red;' class='val'>" + phone + "</span>");
                            good = false;
                        } 
                    }
                });
                //-------------------------------------------------
                $(".required").each(
                function(index) {
                 var id = $(this).attr("id");
                 if (required != "*") {
                     required = "please make a selection";
                 }
                 var x = document.getElementById(id);
                 var NodeName = x.nodeName;
                 if (NodeName == "SELECT") {
                     //DropDown Listbox//
                     if (x.selectedIndex != 0) {
                         // Good //
                     }
                     else {
                         $("#" + id).after("<span style='color:red' class='val'>" + required + "</span>");
                         good = false;
                     }
                 }
                 else if (NodeName == "TABLE") {
                     //Radio Buttons//
                     var localSuccess = false;
                     if (x.rows.length > 1) {
                         for (var i = 0; i < x.rows.length; i++) {
                             var o = x.rows[i].cells[0]; // Returns the TD
                             var co = o.childNodes[0];
                             if (co.checked == true) {
                                 localSuccess = true;
                             }
                         }
                     }
                     else {
                         for (var i = 0; i < x.rows[0].cells.length; i++) {
                             var o = x.rows[0].cells[i];
                             var co = o.childNodes[0];
                             if (co.checked == true) {
                                 localSuccess = true;
                             }
                         }
                     }
                     if (localSuccess != true) {
                         $("#" + id).after("<span style='color:red' class='val'>" + required + "</span>");
                         good = false;
                     }
                 }
                 else if (NodeName == "SPAN") {
                     var localSuccess2 = false;
                     for (var i = 0; i < x.childNodes.length; i++) {
                         var o = x.childNodes[i];
                         var node_type = o.nodeName
                         if (node_type == "INPUT") {
                             if (o.checked == true) {
                                 localSuccess2 = true;
                             }
                         }
                     }
                     if (localSuccess2 == false) {
                         $("#" + id).after("<span style='color:red' class='val'>" + required + "</span>");
                         good = false;
                     }
                 }
                 }
                )
                
                
                //
                if (good == false) {
                try{
                    $("#error").text("Invalid or Missing Data!")
                                .css("color","red");}
                catch(ex){
                }
            }
            else {
                try{
                $("#error").text("");}
                catch(ex){}
            }
            return good;
        }
   
