﻿var RequiredField = ""
var RequiredGroupField = ""
var EmailField = ""
var DateField = ""
var strRequiredFieldMessage = ""
var strValidEmailID = ""
var strValidDate = ""

function Validator(divId, lblId){

    var i = 0;
    var bcolor="#FFD200";
    var flag = true;
    var Msg = new Array(3); 
    Msg[0] = "";
    Msg[1] = "";
    Msg[2] = "";
       
    if (RequiredField != "" ){
        var arrRequiredField = RequiredField.split(",")
        for(i = 0;i < arrRequiredField.length;i++){
             if (document.getElementById(arrRequiredField[i]) != undefined){
                var control = document.getElementById(arrRequiredField[i]);
                if (control.selectedIndex != undefined){
                    if (control.selectedIndex == 0){
                        control.style.backgroundColor = bcolor;
                        flag = false;
                        Msg[0] = strRequiredFieldMessage;
                    }
                    else{
                        control.style.backgroundColor= "";
                    }
                }
                else{
                    if (control.value == ""){
                       // alert(control.style.backgroundColor)
                        control.style.backgroundColor = bcolor;
                        flag = false;
                        Msg[0] = strRequiredFieldMessage;
                    }
                    else{
                        control.style.backgroundColor= "";
                    }
                 }
             }
        }
    }
    
    if (EmailField != ""){
        var arrEmailField = EmailField.split(",")
        for(i = 0;i < arrEmailField.length;i++){
            if (document.getElementById(arrEmailField[i]) != undefined){
                var control = document.getElementById(arrEmailField[i]);
                if (control.value != ""){
                    if (EmailValidator(control.value) == false){
                        control.style.backgroundColor = bcolor;
                        flag = false;
                        Msg[1] = strValidEmailID;
                    }
                    else{
                        control.style.backgroundColor= "";
                    }
                    
                }
            }
        }
    }
    
    if (DateField != ""){
    	var arrDateField = DateField.split(",")
    	for(i = 0;i < arrDateField.length;i++){
    	    if (document.getElementById(arrDateField[i]) != undefined){
    	        //alert(arrDateField[i])
                var control = document.getElementById(arrDateField[i]);
                if (control.value != ""){
                    if (DateValidator(control.value) == false){
                        control.style.backgroundColor = bcolor;
                        flag = false;
                        Msg[2] = strValidDate;
                       
                    }
                    else{
                        control.style.backgroundColor= "";
                    }
                    
                }
            }
        }
    }
    if (RequiredGroupField != ""){
        var ArryGroupField = RequiredGroupField.split("|")
        var Group1 = false
        var Group2 = false
        if (ArryGroupField.length == 2){
            var arryGroup1 = ArryGroupField[0].split(",")
            var arryGroup2 = ArryGroupField[1].split(",")
            for (i = 0;i < arryGroup1.length;i++){
                if (document.getElementById(arryGroup1[i]) != undefined){
                    var control = document.getElementById(arryGroup1[i]);
                    if (control.selectedIndex == undefined){
                        if (control.value != ""){                           
                            Group1 = true;
                        }
                    }
                 }
            }
            for (i = 0;i < arryGroup2.length;i++){
                if (document.getElementById(arryGroup2[i]) != undefined){
                    var control = document.getElementById(arryGroup2[i]);
                    if (control.selectedIndex == undefined){
                        if (control.value != ""){                           
                            Group2 = true;
                        }
                    }
                 }
            }
            if (Group1 == false && Group2 == false){
                Group1 = true
                Group2 = true
            }
            if (Group1 == true){
                for(i = 0;i < arryGroup1.length;i++){
                     if (document.getElementById(arryGroup1[i]) != undefined){
                        var control = document.getElementById(arryGroup1[i]);
                        if (control.selectedIndex != undefined){
                            if (control.selectedIndex == 0){
                                control.style.backgroundColor = bcolor;
                                flag = false;
                                Msg[0] = strRequiredFieldMessage;
                            }
                            else{
                                control.style.backgroundColor= "";
                            }
                        }
                        else{
                            if (control.value == ""){
                                control.style.backgroundColor = bcolor;
                                flag = false;
                                Msg[0] = strRequiredFieldMessage;
                            }
                            else{
                                control.style.backgroundColor= "";
                            }
                         }
                     }
                     
                }
            }
            else{
                for(i = 0;i < arryGroup1.length;i++){
                    if (document.getElementById(arryGroup1[i]) != undefined){
                        var control = document.getElementById(arryGroup1[i]);
                        control.style.backgroundColor= "";
                    }
                }
            }
            if (Group2 == true){
                for(i = 0;i < arryGroup2.length;i++){
                     if (document.getElementById(arryGroup2[i]) != undefined){
                        var control = document.getElementById(arryGroup2[i]);
                        if (control.selectedIndex != undefined){
                            if (control.selectedIndex == 0){
                                control.style.backgroundColor = bcolor;
                                flag = false;
                                Msg[0] = strRequiredFieldMessage;
                            }
                            else{
                                control.style.backgroundColor= "";
                            }
                        }
                        else{
                            if (control.value == ""){
                                control.style.backgroundColor = bcolor;
                                flag = false;
                                Msg[0] = strRequiredFieldMessage;
                            }
                            else{
                                control.style.backgroundColor= "";
                            }
                         }
                     }
                     
                }
            }
            else{
                for(i = 0;i < arryGroup2.length;i++){
                    if (document.getElementById(arryGroup2[i]) != undefined){
                        var control = document.getElementById(arryGroup2[i]);
                        control.style.backgroundColor= "";
                    }
                }
            }
        }
        
    }

    if (flag == false){    
        var Meass = ""
        if (document.getElementById(divId) != undefined && document.getElementById(lblId) != undefined){
            var divControl = document.getElementById(divId);
            var labelControl = document.getElementById(lblId);
            divControl.style.visibility="Visible";
            for (i = 0;i < Msg.length; i++){
                if (Msg[i] != ""){
                    if (Meass == ""){
                        Meass = Msg[i]
                    }
                    else{
                        Meass = Meass + ", " + Msg[i]
                    }
                }
                
            }
            labelControl.innerHTML = Meass;
            
        }
    }                          
    return flag;
}
function DateValidator(str)
{
    var arrDate = str.split("/")
    if (isDate(arrDate[1] + '/' + arrDate[0] + '/' + arrDate[2]) == true)
        return true
    else
        return false
}

function EmailValidator(str){
    var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length - 1
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   
	    return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	    return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }
	 if (str.lastIndexOf(dot)==lstr){
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	    return false
	 }

}

function isDate(dateStr) {

    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        //alert("Please enter date in one of the following formats:\nmm/dd/yy, mm/dd/yyyy, mm-dd-yyyy, or mm-dd-yyyy.");
        return false;
    }

    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[4];

    if (month < 1 || month > 12) { // check month range
        //alert("Month must be between 1 and 12.");
        return false;
    }

    if (day < 1 || day > 31) {
        //alert("Day must be between 1 and 31.");
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        //alert("Month "+month+" doesn't have 31 days!")
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            //alert("February " + year + " doesn't have " + day + " days!");
            return false;
        }
    }
    return true; // date is valid
}
