﻿//Validation Types are as follows:

//example use: <input id="myTextBox" type="text" class="[required]" /> 
//[required] = required field, will accept any non empty string
//[date] = not required, will accept empty string or valid DD/MM/YYYY formated date
//[date_req] = required, will only accept DD/MM/YYYY date string, will not accept empty string
//[email] = not required will accept empty string or valid email address
//[email_req] = required, will only accept valid email string, will not accept empty string.
//[number] = not required, valid value is whole number or decimals. Will accept empty string.
//[number_req] = required, valid value is whole number or decimals.

/*
The following CSS class is required. The errorback.png image is only the red background
.validationbox
{
border:solid 1px maroon;
background:url(Images/errorback.png) repeat-x left center;
color:White;
font-family:Verdana;
font-size:8pt;
padding:2px;
display:none; 
position:absolute;
z-index:5000;
}

Apply Example. To use this function you must apply the 
following code to a submit element.
$(document).ready(function() {
$(".thebutton").validate();
});

*/
var validated = false;
jQuery.fn.validate = function () {

    //Set blur functions for the individual fields.
    validated = false;
    setOnBlur();
    return this.click(function () {

        var isValid = 0;
        var checkCount = 0;

        $(".validationbox").remove();

        $("input[type='text']").each(function () {
            checkCount++;
            if (checkField(this) == 1) isValid++;
        });

        $("input[type='password']").each(function () {
            checkCount++;
            if (checkField(this) == 1) isValid++;
        });

        $("select").each(function () {
            checkCount++;
            if (checkField(this) == 1) isValid++;
        });

        $("textarea").each(function () {
            checkCount++;
            if (checkField(this) == 1) isValid++;
        });


        //if all fields return valid then return true
        //else return false and show error messages.
        if (isValid == checkCount) {
            validated = true;
        }
        else {
            $(".validationbox").fadeIn(300);
        }

        return validated;
    });
};

function addMessage(top, left, id, message, width, obj) {
    $(obj).parent().prepend("<div  id='" + id + "validate' class='validationbox' style='position:absolute;top:" + top + "px;left:" + left + "px;width:" + width + "px;text-align:center;'>" + message + "</div>");
}

function setOnBlur() {

    $("input[type='text']").blur(function () {
        var isValid = checkField(this);
        if (isValid == 1) {
            $("#" + $(this).attr("id") + "validate").remove();
        }
        else {
            $("#" + $(this).attr("id") + "validate").fadeIn(300);
        }
    });

    $("input[type='password']").blur(function () {
        var isValid = checkField(this);
        if (isValid == 1) {
            $("#" + $(this).attr("id") + "validate").remove();
        }
        else {
            $("#" + $(this).attr("id") + "validate").fadeIn(300);
        }
    });

    $("textarea").blur(function () {
        var isValid = checkField(this);
        if (isValid == 1) {
            $("#" + $(this).attr("id") + "validate").remove();
        }
        else {
            $("#" + $(this).attr("id") + "validate").fadeIn(300);
        }
    });

    $("select").change(function () {
        var isValid = checkField(this);
        if (isValid == 1) {
            $("#" + $(this).attr("id") + "validate").remove();
        }
        else {
            $("#" + $(this).attr("id") + "validate").fadeIn(300);
            $("#" + $(this).attr("id") + "validate").bgiframe();
        }
    });
}

function checkField(obj) {

    var className = $(obj).attr("class");
    if (className == "radiobutton" || className == "selectedCheckboxes") {
        return 1;
    }
    var pos = $(obj).position();
    var top = pos.top + 1;
    var left = pos.left + $(obj).width() + 45;
    var val = $(obj).val();
    var id = $(obj).attr("id");
    //Regular expression for DD/MM/YYYY date format
    var dateRegEx = "^([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0]?[1-9]|[1][0-2])[./-]([0-9]{4}|[0-9]{2})$";
    var emailRegEx = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
    var numberRegEx = "^[-+]?\\d+(\\.\\d+)?$";

    //isolate the class name between the [] symbols.
    if (className.indexOf("[") >= 0) {
        var start = className.indexOf("[");
        var end = className.indexOf("]") + 1;
        className = $.trim(className.substring(start, end));
    }

    switch (className) {

        case "[required]":
            if (val == null || val == "" || val == "0") {
                addMessage(top, left, id, "Required", 82, obj);
                return 0;
            }
            else return 1;
            break;

        case "[date]":
            if (val != null && val != "") {
                if (val.match(dateRegEx) == null) { addMessage(top, left, id, "Use DD/MM/YYYY", 130, obj); return 0; }
                else return 1;
            }
            else return 1;
            break;

        case "[date_req]":
            if (val.match(dateRegEx) == null) { addMessage(top, left, id, "Use DD/MM/YYYY", 120, obj); return 0; }
            else return 1;
            break;

        case "[email]":
            if (val != null && val != "") {
                if (val.match(emailRegEx) == null) { addMessage(top, left, id, "Invalid Email Format", 123, obj); return 0; }
                else return 1;
            }
            else return 1;
            break;

        case "[email_req]":
            if (val.match(emailRegEx) == null) { addMessage(top, left, id, "Invalid Email Format", 123, obj); return 0; }
            else return 1;
            break;

        case "[number]":
            if (val != null && val != "") {
                if (val.match(numberRegEx) == null) { addMessage(top, left, id, "Numbers Only", 120, obj); return 0; }
                else return 1;
            }
            else return 1;
            break;

        case "[number_req]":
            if (val.match(numberRegEx) == null) { addMessage(top, left, id, "Number is required", 120, obj); return 0; }
            else return 1;
            break;


        default:
            return 1; //No validation rules apply.    
    }

}
