﻿var EmailFormat = /^([a-zA-Z0-9_-])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+$/;
var AlphaNumeric = /^([a-zA-Z0-9-_,+.\s]+)$/;
var DateFormat = /^(0|1)[0-9]\/[0-3][0-9]\/(19|20)[0-9]{2}$/;
var PhoneFormat = /^([a-zA-Z0-9-_+().\s]+)$/;
var MultiLineAlphaNumeric = /^([A-Za-z0-9-_.,+()\s]){1,500}$/;
function trim(stringToTrim) {
    if (stringToTrim != null) {
        return stringToTrim.replace(/^\s+|\s+$/g, "");
    }
    else {
        return "";
    }
}
function EnableDisableValidation(FormName, IsEnabled) {
    var FormTable = document.getElementById(FormName);
    var i;
    for (i = 0; i < FormTable.getElementsByTagName("*").length; i++) {
        var control = FormTable.getElementsByTagName("*")[i];
        if (control.getAttribute("Required") != null && IsEnabled) {
            control.setAttribute("Required", "true");
        }
        else if (control.getAttribute("Required") != null && !IsEnabled) {
            control.setAttribute("Required", "false");
        }
    }
}

function CompareDates(control1, control2, operator) {
    if (control1.getAttribute("type") == "text" && control2.getAttribute("type") == "text") {
        if (DateFormat.test(control1.value) && DateFormat.test(control2.value)) {
            switch (operator) {
                case "=":
                    if (control1.value == control2.value) {
                        valid = true;
                        return true;
                    }
                    else {
                        valid = false;
                        return false;
                    }
                    break;
                case ">":
                    if (control1.value > control2.value) {
                        valid = true;
                        return true;
                    }
                    else {
                        valid = false;
                        return false;
                    }
                    break;
                case "<":
                    if (control1.value < control2.value) {
                        valid = true;
                        return true;
                    }
                    else {
                        valid = false;
                        return false;
                    }
                    break;
                case ">=":
                    if (control1.value >= control2.value) {
                        valid = true;
                        return true;
                    }
                    else {
                        valid = false;
                        return false;
                    }
                    break;
                case "<=":
                    if (control1.value <= control2.value) {
                        valid = true;
                        return true;
                    }
                    else {
                        valid = false;
                        return false;
                    }
                    break;
                default:
                    alert("UnSupported Operator");
                    valid = false;
                    return false;
                    break;
            }

        }
    }
}

function ValidateControls(control) {
    if (navigator.userAgent.indexOf("MSIE 8.0") > 0) {
        if (control.getAttribute("type") == "text" || control.getAttribute("type") == "textarea" || control.getAttribute("type") == "password") {
            if (control.disabled == false) {
                if (trim(control.value) == "") {
                    if (control.getAttribute("Required") == "true") {
                        control.className = "invalid";
                        valid = false;
                        try {
                            document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "<span style='color:red'>Required</span>";
                        }
                        catch (err) {
                            //Handle errors here
                        }
                    }
                }
                else {
                    control.className = "bodyfont";
                    try {
                        document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "&nbsp;";
                    } catch (err) {
                    }
                    IsValid(control, control.value);
                }
            }
        }
        else if (control.type == "select-one") {
            if (control.disabled == false && trim(control.value) == "-1") {
                if (control.getAttribute("Required") == "true") {
                    control.className = "invalid";
                    valid = false;
                    try {
                        document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "<span style='color:red'>Required</span>";
                    }
                    catch (err) {
                        //Handle errors here
                    }
                }
            }
            else {
                control.className = "bodyfont";
                try {
                    document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "&nbsp;";
                }
                catch (err) {
                    //Handle errors here
                }
            }
        }
    }
    else if (navigator.userAgent.indexOf("MSIE") > 0) {
        if (control.getAttribute("disabled") == false) {
            if (control.getAttribute("type") == "text" || control.getAttribute("type") == "textarea" || control.getAttribute("type") == "password") {
                if (trim(control.getAttribute("value")) == "") {
                    if (control.getAttribute("Required") == "true") {
                        control.className = "invalid";
                        valid = false;
                        try {
                            document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "<span style='color:red'>Required</span>";
                        }
                        catch (err) {
                            //Handle errors here
                        }
                    }
                }
                else {
                    control.className = "bodyfont";
                    try {
                        document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "&nbsp;";
                    } catch (err) {

                    }

                    IsValid(control, control.getAttribute("value"));
                }
            }
            else if (control.type == "select-one") {
                if (control.getAttribute("disabled") == false && trim(control.getAttribute("value")) == "-1") {
                    if (control.getAttribute("Required") == "true") {
                        control.className = "invalid";
                        valid = false;
                        try {
                            document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "<span style='color:red'>Required</span>";
                        }
                        catch (err) {
                            //Handle errors here
                        }
                    }
                }
                else {
                    control.className = "bodyfont";
                    try {
                        document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "&nbsp;";
                    }
                    catch (err) {
                        //Handle errors here
                    }
                }
            }
        }
    }
    else if (navigator.appName == "Netscape") {
        if (control.disabled == false) {
            if (control.getAttribute("type") == "text" || control.getAttribute("type") == "textarea" || control.getAttribute("type") == "password") {
                if (trim(control.value) == "") {
                    if (control.getAttribute("Required") == "true") {
                        control.className = "invalid";
                        valid = false;
                        try {
                            document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "<span style='color:red'>Required</span>";
                        }
                        catch (err) {
                            //Handle errors here
                        }
                    }
                }
                else {
                    control.className = "bodyfont";
                    try {
                        document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "&nbsp;";
                    } catch (err)
                    { }
                    

                    IsValid(control, control.value);
                }
            }
            else if (control.type == "select-one") {
                if (control.disabled == false && trim(control.value) == "-1") {
                    if (control.getAttribute("Required") == "true") {
                        control.className = "invalid";
                        valid = false;
                        try {
                            document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "<span style='color:red'>Required</span>";
                        }
                        catch (err) {
                            //Handle errors here
                        }
                    }
                }
                else {
                    control.className = "bodyfont";
                    try {
                        document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "&nbsp;";
                    }
                    catch (err) {
                        //Handle errors here
                    }
                }
            }
        }
    }
}

function IsValid(control, controlValue) {
    if (control.getAttribute("Dtype") == "Email") {
        if (!EmailFormat.test(controlValue)) {
            control.className = "invalid";
            valid = false;
            try {
                document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "<span style='color:red'>Invalid Email format</span>";
            }
            catch (err) {
                //Handle errors here
            }
        }
        else {
            control.className = "bodyfont";
            try {
                document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "&nbsp;";
            }
            catch (err) {
                //Handle errors here
            }
        }
    }
    else if (control.getAttribute("Dtype") == "AlphaNumeric") {
        if (!AlphaNumeric.test(controlValue)) {
            control.className = "invalid";
            valid = false;
            try {
                document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "<span style='color:red'>Invalid format</span>";
            }
            catch (err) {
                //Handle errors here
            }
        }
        else {
            control.className = "bodyfont";
            try {
                document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "&nbsp;";
            }
            catch (err) {
                //Handle errors here
            }
        }

    }
    else if (control.getAttribute("Dtype") == "Numeric") {
        if (isNaN(controlValue)) {
            control.className = "invalid";
            valid = false;
            try {
                document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "<span style='color:red'>Invalid format</span>";
            }
            catch (err) {
                //Handle errors here
            }
        }
        else {
            control.className = "bodyfont";
            try {
                document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "&nbsp;";
            }
            catch (err) {
                //Handle errors here
            }
        }
    }
    else if (control.getAttribute("Dtype") == "Date") {
        if (!DateFormat.test(controlValue)) {
            control.className = "invalid";
            valid = false;
            try {
                document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "<span style='color:red'>Invalid Email format</span>";
            }
            catch (err) {
                //Handle errors here
            }
        }
        else {
            control.className = "bodyfont";
            try {
                document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "&nbsp;";
            }
            catch (err) {
                //Handle errors here
            }
        }

    }
    else if (control.getAttribute("Dtype") == "MultiLineAlphaNumeric") {
        if (!MultiLineAlphaNumeric.test(controlValue)) {
            control.className = "invalid";
            valid = false;
            try {
                document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "<span style='color:red'>Invalid format</span>";
            }
            catch (err) {
                //Handle errors here
            }
        }
        else {
            control.className = "bodyfont";
            try {
                document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "&nbsp;";
            }
            catch (err) {
                //Handle errors here
            }
        }

    }
    else if (control.getAttribute("Dtype") == "Phone") {
        if (!PhoneFormat.test(controlValue)) {
            control.className = "invalid";
            valid = false;
        }
        else {
            control.className = "bodyfont";
            try {
                document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "&nbsp;";
            }
            catch (err) {
                //Handle errors here
            }
        }
    }
    else if (control.getAttribute("Dtype") == "Password") {
        if (!AlphaNumeric.test(controlValue)) {
            control.className = "bodyfont";
            //valid = false;
        }
        else {
            control.className = "bodyfont";
            try {
                document.getElementById(control.getAttribute("Id").replace("ctl00_Content_", "") + "_val").innerHTML = "&nbsp;";
            }
            catch (err) {
                //Handle errors here
            }
        }
    }

}

var valid = true;
function ValidatePage(FormName) {
    valid = true;
    var FormTable = document.getElementById(FormName);
    var i;
    for (i = 0; i < FormTable.getElementsByTagName("*").length; i++) {
        var control = FormTable.getElementsByTagName("*")[i];
        if (control.getAttribute("Required") == null) {
            continue;
        }

        ValidateControls(control);
    }
    if (!valid) {

        return false;
    }
    else {
        return true;
    }
}


