// DomValidation.js.
//<script>
var Page_DomValidationVer = "2";
var Page_IsValid = true;  
var Page_BlockSubmit = false;

function ValidatorUpdateDisplay(val) {
    if (typeof(val.display) == "string" && val.isvalid == false) {
        if (val.display == "None") {
            return;
        }
        if (val.display == "Dynamic") {
			if(val.alert_enabled == "True"){
				alert(val.innerText);
				if(typeof(val.controltovalidate) == "string"){
					document.getElementById(val.controltovalidate).focus();
				}
			}else{
				val.style.display = val.isvalid ? "none" : "inline";
            }
            return;
        }
    }
    val.style.visibility = val.isvalid ? "hidden" : "visible";
    
}

function ValidatorUpdateIsValid() {
    var i;
    for (i = 0; i < Page_Validators.length; i++) {
        if (!Page_Validators[i].isvalid) {
            Page_IsValid = false;
            return;
        }
   }
   Page_IsValid = true;
}

function ValidatorHookupControl(control, val) {
    if (typeof(control.Validators) == "undefined") {
        control.Validators = new Array;
        var ev = control.onchange;
        var func = new Function("ValidatorOnChange('" + control.id + "');");
        control.onchange = func;
    }
    control.Validators[control.Validators.length] = val;
}

function ValidatorGetValue(id) {
    var control;
    control = document.getElementById(id);
		if (typeof(control.value) == "string") {
			return control.value;
		}
		//
		if (typeof(control.tagName) == "undefined" && typeof(control.length) == "number") {
			var j;
			for (j=0; j < control.length; j++) {
				var inner = control[j];
				if (typeof(inner.value) == "string" && (inner.type != "radio" || inner.status == true)) {
					return inner.value;
				}
			}
		}
    return "";
}

function Page_ClientValidate() {
    var i;
    for (i = 0; i < Page_Validators.length; i++) {
        ValidatorValidate(Page_Validators[i]);
        if(typeof(Page_Validators[i].alert_enabled) == "string"){
			if(Page_Validators[i].alert_enabled = "True"){
				if(!Page_Validators[i].isvalid){
					break;
				}
			}
        }
    }
    ValidatorUpdateIsValid();   
    Page_BlockSubmit = !Page_IsValid;
    return Page_IsValid;
}
function canValidate(val)
{
	var cont = document.getElementById(val.getAttribute("controltovalidate"));
	if(typeof(cont) == "object")
	{
		if(cont.style.visibility != "hidden" || cont.style.display != "none")
		{
			return true;
		}else{
			return false;
		}
	}
}
function ValidatorCommonOnSubmit() {
    var returnValue =  !Page_BlockSubmit;
    Page_BlockSubmit = false;    
    return returnValue;
}

function ValidatorOnChange(controlID) {
var cont
	if(typeof(controlID) == "object"){
		cont = controlID
	}else{
		cont = document.getElementById(controlID);
    }
    var vals = cont.Validators;
    var i;
    for (i = 0; i < vals.length; i++) {
        ValidatorValidate(vals[i]);
    }
    ValidatorUpdateIsValid();
    Page_BlockSubmit = !Page_IsValid;
    return Page_IsValid;   
}

function ValidatorValidate(val) {    
    val.isvalid = true;
    if (typeof(val.evalfunc) == "function") {
        val.isvalid = val.evalfunc(val);
    }
    ValidatorUpdateDisplay(val);
}

function ValidatorOnLoad() {
    if (typeof(Page_Validators) == "undefined")
        return;

    var i, val;
    for (i = 0; i < Page_Validators.length; i++) {
        val = Page_Validators[i];
        var evalFunction = val.getAttribute("evaluationfunction");
        if (typeof(evalFunction) == "string") {
            eval("val.evalfunc = " + evalFunction + ";");
        }
        var isValidAttribute = val.getAttribute("isvalid");
        if (typeof(isValidAttribute) == "string") {
            if (isValidAttribute == "False") {
                val.isvalid = false;                                
                Page_IsValid = false;
            } 
            else {
                val.isvalid = true;
            }
        } else {
            val.isvalid = true;
        }
        var controlToValidate = val.getAttribute("controltovalidate");
        if (typeof(controlToValidate) == "string") {
            ValidatorHookupControl(document.getElementById(controlToValidate), val);
        }        
    }
    Page_ValidationActive = true;
}

function RegularExpressionValidatorEvaluateIsValid(val) {
    var value = ValidatorGetValue(val.getAttribute("controltovalidate"));
    if (value == "")
        return true;        
    var rx = new RegExp(val.getAttribute("validationexpression"));
    var matches = rx.exec(value);
    return (matches != null && value == matches[0]);
}

function ValidatorTrim(s) {
    var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (m == null) ? "" : m[1];
}

function RequiredFieldValidatorEvaluateIsValid(val) {
    return vs_non_blank(ValidatorGetValue(val.getAttribute("controltovalidate")));//(ValidatorTrim(ValidatorGetValue(val.getAttribute("controltovalidate"))) != ValidatorTrim(val.getAttribute("initialvalue")));
}

function DateFieldValidatorEvaluateIsValid(val) {
	return (ValidatorConvert(ValidatorGetValue(val.getAttribute("controltovalidate")),'Date',val) == null) ? false : true ;
}
function DateTimeFieldValidatorEvaluateIsValid(val) {
	return (ValidatorConvert(ValidatorGetValue(val.getAttribute("controltovalidate")),'DateTime',val) == null) ? false : true ;
}

function ValidatorConvert(op, dataType, val) {
    function GetFullYear(year) {
        return (year + parseInt(val.century)) - ((year < val.cutoffyear) ? 0 : 100);
    }
    var num, cleanInput, m, exp;
    if (dataType == "Integer") {
        exp = /^\s*[-\+]?\d+\s*$/;
        if (op.match(exp) == null) 
            return null;
        num = parseInt(op, 10);
        return (isNaN(num) ? null : num);
    }
    else if(dataType == "Double") {
        exp = new RegExp("^\\s*([-\\+])?(\\d+)?(\\" + val.decimalchar + "(\\d+))?\\s*$");
        m = op.match(exp);
        if (m == null)
            return null;
        cleanInput = m[1] + (m[2].length>0 ? m[2] : "0") + "." + m[4];
        num = parseFloat(cleanInput);
        return (isNaN(num) ? null : num);            
    } 
    else if (dataType == "Currency") {
        exp = new RegExp("^\\s*([-\\+])?(((\\d+)\\" + val.groupchar + ")*)(\\d+)"
                        + ((val.digits > 0) ? "(\\" + val.decimalchar + "(\\d{1," + val.digits + "}))?" : "")
                        + "\\s*$");
        m = op.match(exp);
        if (m == null)
            return null;
        var intermed = m[2] + m[5] ;
        cleanInput = m[1] + intermed.replace(new RegExp("(\\" + val.groupchar + ")", "g"), "") + ((val.digits > 0) ? "." + m[7] : 0);
        num = parseFloat(cleanInput);
        return (isNaN(num) ? null : num);            
    }
    else if (dataType == "Date") {
        var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})\\s*$");
        m = op.match(yearFirstExp);
        var day, month, year;
        if (m != null && (m[2].length == 4 || val.dateorder == "ymd")) {
            day = m[6];
            month = m[5];
            year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10))
        }
        else {
            if (val.dateorder == "ymd"){
                return null;		
            }						
            var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-./])(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$");
            m = op.match(yearLastExp);
            if (m == null) {
                return null;
            }
            if (val.dateorder == "mdy") {
                day = m[3];
                month = m[1];
            }
            else {
                day = m[1];
                month = m[3];
            }
            year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10))
        }
        month -= 1;
        var date = new Date(year, month, day);
        return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;
    }else if (dataType == "DateTime"){
		var reg = "20\\d{2}(-|\\/)((0[1-9])|(1[0-2]))(-|\\/)((0[1-9])|([1-2][0-9])|(3[0-1]))(T|\\s)(([0-1][0-9])|(2[0-3]))h([0-5][0-9])"
		var rx = new RegExp(reg);
		var matches = rx.exec(op);
		if(matches != null){
			return  matches[0];
		}else{
			return null;
		}
    }else {
        return op.toString();
    }
}
function vs_non_blank(s) {
	s=s.Trim();
	if (s.length==0) {
		return false;
	}
	return true;
}
function trim_string() {
	var ichar, icount;
	var strValue = this;
	ichar = strValue.length - 1;
	icount = -1;
	while (strValue.charAt(ichar)==' ' && ichar > icount)
		--ichar;
	if (ichar!=(strValue.length-1))
		strValue = strValue.slice(0,ichar+1);
	ichar = 0;
	icount = strValue.length - 1;
	while (strValue.charAt(ichar)==' ' && ichar < icount)
		++ichar;
	if (ichar!=0)
		strValue = strValue.slice(ichar,strValue.length);
	return strValue;
}

function date_toSimpleForm() {
	var toSimpleForm = new String;
	toSimpleForm = this.toLocaleString();
	toSimpleForm = toSimpleForm.substring(0,toSimpleForm.indexOf(' '));
	return toSimpleForm;
}
// Extend the string object to include a trim function
String.prototype.Trim = trim_string;
// Extend the date object to include a simple form string conversion
Date.prototype.toSimpleForm = date_toSimpleForm;