﻿/*
	    Author  :   Anand Kumar Sharma
	    Date    :   26-10-2009
	    Purpose :   Validate form fields.
	Examples:
    <input type='text' validate="group" required="error message" />
    <input type='text' validate="group" regular="error message" validExpress=".{6,}" />
	<input type='text' validate="group" regular="error message" invalidExpress=".{,6}" />
    <input type='text' validate="group" compare="error message" compareTo="button1" />
    <input type='text' validate="group" custom="error message" customFn="return this.value.length < 6" />
    <input type='text' validate="group" invalid="error message" invalidVal="username" />
*/

function validate(group) {
    var marker = true;
    $("*[validate=" + group + "]").each(function(i, elm) {
        if (check(elm)) {
            $(elm).highlight();
            if (marker)
                $(elm).focus();
            marker = false;
        }
        else
            $(elm).unhighlight();
    });
    return marker;
}

function revalidate() {
    if (!check(this))
        $(this).unhighlight();
    else
       $(this).highlight();
}

function check(elm) {
    var jelm = $(elm);


	var listsize = jelm.find("input:radio, input:checkbox").size();
    if (jelm.attr("disabled") || listsize > 0 && listsize == jelm.find("input:radio:disabled, input:checkbox:disabled").size())
        return "";
    
    //if empty value only perform required validation
    if ((jelm.attr("required") && jelm.val() == ""))
        return jelm.attr("required") ? "required" : "";
    
    if (jelm.attr("regular") && jelm.attr("validExpress") && !new RegExp(jelm.attr("validExpress"), "m").test(jelm.val()))
        return "regular";
    
    if (jelm.attr("regular") && jelm.attr("invalidExpress") && new RegExp(jelm.attr("invalidExpress"), "m").test(jelm.val()))
        return "regular";
    
    if (jelm.attr("compare") && $("#" + jelm.attr("compareTo")).val() != jelm.val())
        return "compare";
    
    if (jelm.attr("custom") && !new Function(jelm.attr("customFn")).call(elm))
        return "custom";
    
    if (jelm.attr("invalid") && jelm.val() == jelm.attr("invalidVal"))
        return "invalid";
}

function showAlert() {
    var ctrl = $(this);
    var ctrl1 = document.getElementById(this.id);
    var left = $(this).position().left + 0;
    var top = $(this).position().top + 25;
	$(".errorBoxWrapper").html('');
    $("#errorBoxWrapper").remove();
    var alertTip = $("<div id='errorBoxWrapper' class='errorBoxWrapper' style='position:absolute; top:" + top + "px; left:" + left + "px;' ><div class='errorBox'><p>" + ctrl1.getAttribute(check(this)) + "</p></div></div>");
    //if(left > 15 && top > 15)
    //ctrl.parent().append("<div class='alertboxWrapper' style='top:" + top + "px; left:" + left + "px;'><div class='alertbox' ></div><div class='alertboxRight'><p>" + ctrl.attr(check(this)) + "</p></div></div>");
    $('#' + ctrl.attr('id') + ':visible').parent().append(alertTip);//.insertAfter(ctrl);
    //else
    //ctrl.parent().append("<div class='alertboxWrapper'><div class='alertbox'></div><div class='alertboxRight'><p>" + ctrl.attr(check(this)) + "</p></div></div>");
    //ctrl.parent().append("<div id='errorBoxWrapper'><div class='errorBox'><p>" + ctrl.attr(check(this)) + "</p></div></div>");
    
}

function hideAlert() {
    //$(this).parent().children(".alertboxWrapper").remove();
    $("#errorBoxWrapper").remove();
}



(function($) 
{ 
    $.fn.highlight = function() 
    { 
        this.addClass("highlight").focus(showAlert).blur(hideAlert).keypress(hideAlert).change(revalidate); 
    } 
})(jQuery);

(function($) 
{ 
    $.fn.unhighlight = function() 
    { 
        this.removeClass("highlight").unbind("focus", showAlert).unbind("blur", hideAlert).unbind("keypress", hideAlert).parent().children(".alertboxWrapper").remove(); 
    } 
})(jQuery);
