﻿var $ControlInValid = new Array();
var $ErrorMsg = new Array();
var MsgReq = "Required field Missing!";
var MsgInvalidEmail = "Invalid Email ID!";
var MsgPasswordLength = "Password must be of 6 or greater than 6 characters!";
var msgPasswordMatch = "Password Not Matching!";
var msgDuplicateEmail = "Duplicate Email!";

function Validate(type, jqSelector, fn) {
    this.type = (typeof type == "string") ? type : null;
    if (typeof jqSelector == "string") {
        this.control = $(jqSelector);
        if (typeof fn == "function") {
            this.Validation = fn;
        }
        else {
            this.Validation = function() {
                return false;
            }
        }
    }
    else {
        this.control = null;
    }
    this.cIndicate = clearIndication;
    this.Data = HasData;
    this.msg = Msgbox;
}
function Email() {
    var flag = true;

    if (typeof this.id == "string") {
        if (emailValidation(this.id) == false) {
            flag = false;
        }
    }
    else if (typeof this.control == "object") {
        for (var i = 0; i < this.control.length; i++) {
            if ($(this.control[i]).is(':visible') && $(this.control[i]).val() != "") {
                if (emailValidation(this.control[i].id) == false) {
                    flag = false;
                }
            }
        }
        if (!$(this.control).findDuplicates()) { flag = false; }
    }
    return flag;
}
function emailValidation(control) {
    var $txtcontrol = $("#" + control);
    var Val = $txtcontrol.val();
    if (isValidEmailAddress(Val) == false) {
        InValidArray("#" + control, MsgInvalidEmail);
        return false;
    }

}
function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}
function Required() {
    var flag = true;

    if (typeof this.id == "string") {
        if (RequiredValidation(this.id) == false) {
            flag = false;
        }
    }
    else if (typeof this.control == "object") {
        for (var i = 0; i < this.control.length; i++) {
            if ($(this.control[i]).is(':visible')) {
                if (RequiredValidation(this.control[i].id) == false) {
                    flag = false;
                }
            }
        }
    }
    return flag;
}
function RequiredValidation(control) {
    var $txtcontrol = $("#" + control);
    var Val = $txtcontrol.val();
    //alert($txtcontrol.tagName() + " " + $txtcontrol.type());
    if ($txtcontrol.tagName() == "SELECT") {
        if (Val == 0) {
            InValidArray("#" + control, MsgReq);
            return false;
        }
    }
    else if (($txtcontrol.tagName() == "INPUT" && ($txtcontrol.type() == "text" || $txtcontrol.type() == "password")) || $txtcontrol.tagName() == "TEXTAREA") {
        if (trim(Val, " ") == "") {
            InValidArray("#" + control, MsgReq);
            return false;
        }
    }

}
$.fn.tagName = function() {
    return this.get(0).tagName;
}
$.fn.type = function() {
    return this.get(0).type;
}

function InValidArray(controlid, Msg) {
    $ControlInValid[$ControlInValid.length] = controlid;
    $ErrorMsg[$ErrorMsg.length] = Msg;
}

function Msgbox() {
    this.cIndicate()
    var per = ""
    var curr = ""
    var Msg = ""

    if ($ControlInValid.length > 0) {
        $ControlInValid.sort();
        per = ""
        curr = ""
        for (var i = 0; i < $ControlInValid.length; i++) {
            curr = $ControlInValid[i];
            if (curr == per)
                continue;
            else {
                $($ControlInValid[i]).css("background-color", "#FAEFA7");
            }
            per = $ControlInValid[i];
        }

        $ControlInValid = new Array();
    }

    if ($ErrorMsg.length > 0) {
        $ErrorMsg.sort();
        per = ""
        curr = ""

        for (var i = 0; i < $ErrorMsg.length; i++) {
            curr = $ErrorMsg[i];
            if (curr == per)
                continue;
            else {
                if (Msg == "")
                    Msg = $ErrorMsg[i]
                else
                    Msg = Msg + "\n" + $ErrorMsg[i]
            }
            per = $ErrorMsg[i];
        }
        $ErrorMsg = new Array();
        if (Msg != "") {
            //alert(Msg)
            //$("DivError1")
            $("div[id*=DivError1]").css("visibility", "visible").find("span[id*=lblDefineError]").text(Msg);
            $("div[id*=DivError]").css("display", "block").find("span[id*=lblDefineError]").text(Msg);

        }

    }
}

function clearIndication() {
    for (var i = 0; i < this.control.length; i++) {
        $("#" + this.control[i].id).css("background-color", "White");
    }
}


function HasData(IntValue) {
    InitialValue = (typeof IntValue == "string") ? IntValue : "";
    var flag = false;
    for (var i = 0; i < this.control.length; i++) {
        if ($("#" + this.control[i].id).val() != InitialValue)
            flag = true;
    }
    return flag;
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


function ValidPassword() {
    var flag = true, flag1 = true;
    var Passwordcont = this.control;
    $(this.control).each(function(i) {
        if ($(this).val().length < 6) {
            flag = false;
            InValidArray("#" + this.id, MsgPasswordLength);
        }
        
        if (i >= 1) {
            if ($(Passwordcont[i - 1]).val() != $(Passwordcont[i]).val()) {
                flag1 = false;
                InValidArray("#" + this.id, msgPasswordMatch);
            }
        }        
    });
    if (flag == false || flag1 == false) {
        return false;  
    }

}


$.fn.findDuplicates = function() {
    var isDuplicate = false;
    EmailControls = this
    $(EmailControls).each(function(i, el1) {
        var current_val = $(el1).val();
        if (current_val != "") {
            $(EmailControls).each(function(j, el2) {
                if ($(el2).val() == current_val && $(el1).attr("name") != $(el2).attr("name")) {
                    isDuplicate = true;
                    InValidArray("#" + el1.id, msgDuplicateEmail);
                    InValidArray("#" + el2.id, msgDuplicateEmail);
                    return;
                }
            });
        }
    });

    if (isDuplicate) {
        return false;
    } else {
        return true;
    }
}