var Page_ValidationVer = "125";
var Page_IsValid = true;
var Page_BlockSubmit = false;
var Page_InvalidControlToBeFocused = null;
function ValidatorUpdateDisplay(val) {
    if (typeof(val.getAttribute("display")) == "string") {
        if (val.getAttribute("display") == "None") {
            return;
        }
        if (val.getAttribute("display") == "Dynamic") {
            val.style.display = val.isvalid ? "none" : "";
            return;
        }
    }
    if ((navigator.userAgent.indexOf("Mac") > -1) &&
        (navigator.userAgent.indexOf("MSIE") > -1)) {
        val.style.display = "inline";
    }
    val.style.visibility = val.isvalid ? "hidden" : "visible";
}
function ValidatorUpdateIsValid() {
    Page_IsValid = AllValidatorsValid(Page_Validators);
}
function AllValidatorsValid(validators) {
    if (validators != null) {
        var i;
        for (i = 0; i < validators.length; i++) {
            if (!validators[i].isvalid) {
                return false;
            }
        }
    }
    return true;
}
function ValidatorHookupControlID(controlID, val) {
    if (typeof(controlID) != "string") {
        return;
    }
    var ctrl = document.getElementById(controlID);
    if (ctrl != null) {
        ValidatorHookupControl(ctrl, val);
    }
    else {
        val.isvalid = true;
        val.enabled = false;
    }
}
function ValidatorHookupControl(control, val) {
    if (control.tagName != "INPUT" && control.tagName != "TEXTAREA" && control.tagName != "SELECT") {
        var i;
        var children = (control.children) ? control.children : control.childNodes;
        for (i = 0; i < children.length; i++) {
            ValidatorHookupControl(children[i], val);
        }
        return;
    }
    else {
        if (typeof(control.Validators) == "undefined") {
            control.Validators = new Array;
            var eventType;
            if (control.type == "radio") {
                eventType = "onclick";
            } else {
                eventType = "onchange";
                if (val.getAttribute("focusOnError") == "t") {
                    ValidatorHookupEvent(control, "onblur", "ValidatedControlOnBlur(event); ");
                }
            }
			ValidatorHookupEvent(control, eventType, "ValidatorOnChange(event); ");
			if (control.type == "text" ||
				control.type == "password" ||
				control.type == "file") {
				ValidatorHookupEvent(control, "onkeypress", "if (!ValidatedTextBoxOnKeyPress(event)) return false; ");
			}
        }
        control.Validators[control.Validators.length] = val;
    }
}
function ValidatorHookupEvent(control, eventType, functionPrefix) {
    var ev;
    eval("ev = control." + eventType + ";");
    if (typeof(ev) == "function") {
        ev = ev.toString();
        ev = ev.substring(ev.indexOf("{") + 1, ev.lastIndexOf("}"));
    }
    else {
        ev = "";
    }
    var func;
    if (navigator.appName.toLowerCase().indexOf('explorer') > -1) {
        func = new Function(functionPrefix + " " + ev);
    }
    else {
        func = new Function("event", functionPrefix + " " + ev);
    }
    eval("control." + eventType + " = func;");
}
function ValidatorGetValue(id) {
    var control;
    control = document.getElementById(id);
    if (typeof(control.value) == "string") {
        return control.value;
    }
    return ValidatorGetValueRecursive(control);
}
function ValidatorGetValueRecursive(control)
{
    if (typeof(control.value) == "string" && (control.type != "radio" || control.status == true)) {
        return control.value;
    }
    var i, val;
	var children = (control.children) ? control.children : control.childNodes;	// TZK: DOM compatible
    for (i = 0; i<children.length; i++) {
        val = ValidatorGetValueRecursive(children[i]);
        if (val != "") return val;
    }
    return "";
}
function Page_ClientValidate(validationGroup) {
    Page_InvalidControlToBeFocused = null;
    if (Page_Validators == null) {
        return true;
    }
    // TZK: ensure validationGroup is declared
	if (typeof(validationGroup) == "undefined") {
		validationGroup = null;
	}
    var i;
    for (i = 0; i < Page_Validators.length; i++) {
        ValidatorValidate(Page_Validators[i], validationGroup, null);
    }
    ValidatorUpdateIsValid();
    ValidationSummaryOnSubmit(validationGroup);
    Page_BlockSubmit = !Page_IsValid;
    return Page_IsValid;
}
function ValidatorCommonOnSubmit() {
    Page_InvalidControlToBeFocused = null;
    var result = !Page_BlockSubmit;
    if (window.event != null) {
        window.event.returnValue = result;
    }
    Page_BlockSubmit = false;
    return result;
}
function ValidatorEnable(val, enable, noUpdate) {
    val.enabled = (enable != false);
	// TZK: Check if we need to cause validation.
    if (noUpdate && val.enabled) {
		return;
	}
    ValidatorValidate(val);
    ValidatorUpdateIsValid();
}
function ValidatorOnChange(event) {
    if (!event) {
        event = window.event;
    }
    Page_InvalidControlToBeFocused = null;
    var vals;
    if (event.srcElement != null) {
        vals = event.srcElement.Validators;
    }
    else {
        vals = event.target.Validators;
    }
	// TZK: Added for safety.
	if (vals != null) {
		var i;
		for (i = 0; i < vals.length; i++) {
			ValidatorValidate(vals[i], null, event, vals[i].errorOnChange);
		}
	}
	ValidatorUpdateIsValid();
}
function ValidatedTextBoxOnKeyPress(event) {
    if (event.keyCode == 13) {
        ValidatorOnChange(event);
        var vals;
        if (event.srcElement != null) {
            vals = event.srcElement.Validators;
        }
        else {
            vals = event.target.Validators;
        }
        return AllValidatorsValid(vals);
    }
    return true;
}
function ValidatedControlOnBlur(event) {
    var control;
    if (event.srcElement != null) {
        control = event.srcElement;
    }
    else {
        control = event.target;
    }
    if (control != null && Page_InvalidControlToBeFocused == control) {
        control.focus();
        Page_InvalidControlToBeFocused = null;
    }
}
function ValidatorValidate(val, validationGroup, event, validateOnChange) {
    val.isvalid = true;
    if (val.enabled != false && IsValidationGroupMatch(val, validationGroup)) {
        if (typeof(val.evaluationfunction) == "function") {
            val.isvalid = val.evaluationfunction(val);
            if (!val.isvalid && Page_InvalidControlToBeFocused == null && val.getAttribute("focusOnError") == "t") {
                ValidatorSetFocus(val, event);
            }
        }
    }
    // TZK: Do not show error if validateOnChange is turned off.
    if (typeof(validateOnChange) != "undefined" && !validateOnChange && !val.isvalid) {
		return;
	}
    ValidatorUpdateDisplay(val);
}
function ValidatorSetFocus(val, event) {
    var ctrl;
    if (typeof(val.getAttribute("controlhookup")) == "string") {
        var eventCtrl;
        if (event != null) {
            if (event.srcElement != null) {
                eventCtrl = event.srcElement;
            }
            else {
                eventCtrl = event.target;
            }
        }
        if (eventCtrl != null &&
            typeof(eventCtrl.id) == "string" &&
            eventCtrl.id == val.getAttribute("controlhookup")) {
            ctrl = eventCtrl;
        }
    }
    if (ctrl == null) {
        ctrl = document.getElementById(val.getAttribute("controltovalidate"));
    }
    if ((ctrl != null) &&
        (ctrl.tagName.toLowerCase() != "table") && 
        ((ctrl.tagName.toLowerCase() != "input") || (ctrl.type.toLowerCase() != "hidden")) &&
        (ctrl.disabled == false) &&
        (ctrl.visible != false) &&
        (IsInVisibleContainer(ctrl))) {
        ctrl.focus();
        Page_InvalidControlToBeFocused = ctrl;
    }
}
function IsInVisibleContainer(ctrl) {
    if (typeof(ctrl.style) != "undefined" &&
        ( ( typeof(ctrl.style.display) != "undefined" &&
            ctrl.style.display == "none") ||
          ( typeof(ctrl.style.visibility) != "undefined" &&
            ctrl.style.visibility == "hidden") ) ) {
        return false;
    }
    else if (typeof(ctrl.parentNode) != "undefined" &&
             ctrl.parentNode != null &&
             ctrl.parentNode != ctrl) {
        return IsInVisibleContainer(ctrl.parentNode);
    }
    return true;
}
function IsValidationGroupMatch(control, validationGroup) {
    if (validationGroup == null) {
        return true;
    }
    var controlGroup = "";
    if (typeof(control.getAttribute("validationGroup")) == "string") {
        controlGroup = control.getAttribute("validationGroup");
    }
    // TZK: support one global ValidationSummary control, i.e. ValidationGroup set to empty.
    if (controlGroup.length == 0) {
        return true;
    }
    return (controlGroup == validationGroup);
}
function ValidatorOnLoad() {
    if (typeof(Page_Validators) == "undefined")
        return;
    var i, val;
    for (i = 0; i < Page_Validators.length; i++) {
        val = Page_Validators[i];
        if (typeof(val.getAttribute("evaluationfunction")) == "string") {
            eval("val.evaluationfunction = " + val.getAttribute("evaluationfunction") + ";");
        }
        if (typeof(val.isvalid) == "string") {
            if (val.isvalid == "False") {
                val.isvalid = false;
                Page_IsValid = false;
            }
            else {
                val.isvalid = true;
            }
        } else {
            val.isvalid = true;
        }
        if (typeof(val.enabled) == "string") {
            val.enabled = (val.enabled != "False");
        }
        // HE: Firefox treats enabled like an expando and needs it 
        // to be accessed through getAttribute().
        else if(typeof(val.getAttribute("enabled")) == "string")
        {
            val.enabled = (val.getAttribute("enabled") != "False");
        }
        // TK: Switch to hook up validator to onchange event
        if (typeof(val.errorOnChange) == "string") {
            val.errorOnChange = (val.errorOnChange != "False");
        }
        ValidatorHookupControlID(val.getAttribute("controltovalidate"), val);
        ValidatorHookupControlID(val.getAttribute("controlhookup"), val);
    }
    Page_ValidationActive = true;
}
function ValidatorConvert(op, dataType, val) {
    function GetFullYear(year) {
        return (year + parseInt(val.getAttribute("century"))) - ((year <= parseInt(val.getAttribute("cutoffyear"), 10)) ? 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.getAttribute("decimalchar") + "?(\\d*)\\s*$");
        m = op.match(exp);
        if (m == null)
            return null;
        if (m[2].length == 0 && m[3].length == 0)
            return null;
        cleanInput = (m[1] != null ? m[1] : "") + (m[2].length>0 ? m[2] : "0") + (m[3].length>0 ? "." + m[3] : "");
        num = parseFloat(cleanInput);
        return (isNaN(num) ? null : num);
    }
    else if (dataType == "Currency") {
        var hasDigits = (val.getAttribute("digits") > 0);
        var beginGroupSize, subsequentGroupSize;
        var groupSizeNum = parseInt(val.getAttribute("groupsize"), 10);
        if (!isNaN(groupSizeNum) && groupSizeNum > 0) {
            beginGroupSize = "{1," + groupSizeNum + "}";
            subsequentGroupSize = "{" + groupSizeNum + "}";
        }
        else {
            beginGroupSize = subsequentGroupSize = "+";
        }
        exp = new RegExp("^\\s*([-\\+])?((\\d" + beginGroupSize + "(\\" + val.getAttribute("groupchar") + "\\d" + subsequentGroupSize + ")+)|\\d*)"
                        + (hasDigits ? "\\" + val.getAttribute("decimalchar") + "?(\\d{0," + val.getAttribute("digits") + "})" : "")
                        + "\\s*$");
        m = op.match(exp);
        if (m == null)
            return null;
        if (m[2].length == 0 && hasDigits && m[5].length == 0)
            return null;
        cleanInput = (m[1] != null ? m[1] : "") + m[2].replace(new RegExp("(\\" + val.getAttribute("groupchar") + ")", "g"), "") + ((hasDigits && m[5].length > 0) ? "." + m[5] : "");
        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.getAttribute("dateorder") == "ymd")) {
            day = m[6];
            month = m[5];
            year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10))
        }
        else {
            if (val.getAttribute("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.getAttribute("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);
        if (year < 100) {
            date.setFullYear(year);
        }
        return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;
    }
    else {
        return op.toString();
    }
}
function ValidatorCompare(operand1, operand2, operator, val) {
    var dataType = val.getAttribute("type");
    var op1, op2;
    if (ValidatorValueIsDefault(operand1, dataType)) {
		return true;
    }
    if ((op1 = ValidatorConvert(operand1, dataType, val)) == null)
        return false;
    if (operator == "DataTypeCheck")
        return true;
    if ((op2 = ValidatorConvert(operand2, dataType, val)) == null)
        return true;
    switch (operator) {
        case "NotEqual":
            return (op1 != op2);
        case "GreaterThan":
            return (op1 > op2);
        case "GreaterThanEqual":
            return (op1 >= op2);
        case "LessThan":
            return (op1 < op2);
        case "LessThanEqual":
            return (op1 <= op2);
        default:
            return (op1 == op2);
    }
}
function ValidatorValueIsDefault(op, dataType) {
    if (dataType == "Date") {
		if (op == "dd/mm/yyyy" || op == "dd-mm-yyyy" || op == "dd-mm-jjjj" || op =="tt.mm.jjjj") {
			return true;
		}
    }
    return false;
}
function CompareValidatorEvaluateIsValid(val) {
    var value = ValidatorGetValue(val.getAttribute("controltovalidate"));
    if (ValidatorTrim(value).length == 0)
        return true;
    var compareTo = "";
    if (null == document.getElementById(val.getAttribute("controltocompare"))) {
        if (typeof(val.getAttribute("valuetocompare")) == "string") {
            compareTo = val.getAttribute("valuetocompare");
        }
    }
    else {
        compareTo = ValidatorGetValue(val.getAttribute("controltocompare"));
    }
    return ValidatorCompare(value, compareTo, val.getAttribute("operator"), val);
}
function CustomValidatorEvaluateIsValid(val) {
    var value = "";
    if (typeof(val.getAttribute("controltovalidate")) == "string") {
        value = ValidatorGetValue(val.getAttribute("controltovalidate"));
        if ((ValidatorTrim(value).length == 0) &&
            ((typeof(val.getAttribute("validateemptytext")) != "string") || (val.getAttribute("validateemptytext") != "true"))) {
            return true;
        }
    }
    var args = { Value:value, IsValid:true };
    if (typeof(val.getAttribute("clientvalidationfunction")) == "string") {
        eval(val.getAttribute("clientvalidationfunction") + "(val, args) ;");
    }
    return args.IsValid;
}
// ODB: Used by RegularExpressionValidatorEvaluateIsValid
var testrx;
try  {
    testrx = new RegExp('^(?!test)$');
}
catch (e) {
}
function RegularExpressionValidatorEvaluateIsValid(val) {
    // ODB: Check to see if negative lookahead is supported, if not always
    // evaluate to true, and let server side validation do the work    
    if (typeof(testrx) == 'undefined')
        return true;
    var value = ValidatorGetValue(val.getAttribute("controltovalidate"));
    if (ValidatorTrim(value).length == 0)
        return true;
    // TZK: Optionally trim value before evaluation
    // N.B. The trimText attribute only exists if server-side property set to True
    // JAW: Its not always 'undefined' it can also be a 'null' 
    if ((typeof(val.getAttribute("trimText")) != "undefined") && (val.getAttribute("trimText") != null)){
		value = ValidatorTrim(value);
    }
    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 (ValidatorTrim(ValidatorGetValue(val.getAttribute("controltovalidate"))) != ValidatorTrim(val.getAttribute("initialvalue")))
}
function RangeValidatorEvaluateIsValid(val) {
    var value = ValidatorGetValue(val.getAttribute("controltovalidate"));
    if (ValidatorTrim(value).length == 0)
        return true;
    return (ValidatorCompare(value, val.getAttribute("minimumvalue"), "GreaterThanEqual", val) &&
            ValidatorCompare(value, val.getAttribute("maximumvalue"), "LessThanEqual", val));
}
function ValidationSummaryOnSubmit(validationGroup) {
    if (typeof(Page_ValidationSummaries) == "undefined")
        return;
    var summary, sums, s;
    var scrollToSummary;	// TZK
    for (sums = 0; sums < Page_ValidationSummaries.length; sums++) {
        summary = Page_ValidationSummaries[sums];
        summary.style.display = "none";
        if (!Page_IsValid && IsValidationGroupMatch(summary, validationGroup)) {
            var i;
            if (summary.getAttribute("showsummary") != "False") {
                summary.style.display = "";
                if (typeof(summary.getAttribute("displaymode")) != "string") {
                    summary.displaymode = "BulletList";
                }
                switch (summary.getAttribute("displaymode")) {
                    case "List":
                        headerSep = "<br>";
                        first = "";
                        pre = "";
                        post = "<br>";
                        end = "";
                        break;
                    case "BulletList":
                    default:
                        headerSep = "";
                        first = "<ul>";
                        pre = "<li>";
                        post = "</li>";
                        end = "</ul>";
                        break;
                    case "SingleParagraph":
                        headerSep = " ";
                        first = "";
                        pre = "";
                        post = " ";
                        end = "<br>";
                        break;
                }
                s = "";
                if (typeof(summary.getAttribute("headertext")) == "string") {
                    s += summary.getAttribute("headertext") + headerSep;
                }
                s += first;
                // AL 24/05/2006: Added so error messages in the validation summary control can be suppressed
                if (summary.getAttribute("showerrormessage") == null || summary.getAttribute("showerrormessage") == "True") {
					for (i=0; i<Page_Validators.length; i++) {
						if (!Page_Validators[i].isvalid && typeof(Page_Validators[i].getAttribute("errormessage")) == "string") {
							s += pre + Page_Validators[i].getAttribute("errormessage") + post;
						}
					}
                }
                s += end;
                // TZK: Added to enhance customisation
                if (typeof(summary.getAttribute("footertext")) == "string") {
                    s += summary.getAttribute("footertext");
                }
                summary.innerHTML = s;
                // TZK 17/10/2006: Do NOT scroll to top of page.
//                window.scrollTo(0,0);
				if (!!scrollToSummary == false) {
					scrollToSummary = summary;
				}
            }
            if (summary.getAttribute("showmessagebox") == "True") {
                s = "";
                if (typeof(summary.getAttribute("headertext")) == "string") {
                    s += summary.getAttribute("headertext") + "\r\n";
                }
                var lastValIndex = Page_Validators.length - 1;
                for (i=0; i<=lastValIndex; i++) {
                    if (!Page_Validators[i].isvalid && typeof(Page_Validators[i].getAttribute("errormessage")) == "string") {
                        switch (summary.getAttribute("displaymode")) {
                            case "List":
                                s += Page_Validators[i].getAttribute("errormessage");
                                if (i < lastValIndex) {
                                    s += "\r\n";
                                }
                                break;
                            case "BulletList":
                            default:
                                s += "- " + Page_Validators[i].getAttribute("errormessage");
                                if (i < lastValIndex) {
                                    s += "\r\n";
                                }
                                break;
                            case "SingleParagraph":
                                s += Page_Validators[i].getAttribute("errormessage") + " ";
                                break;
                        }
                    }
                }
                alert(s);
            }
        }
    }
    // TZK: Scroll to top of validation summary control.
    if (!!scrollToSummary) {
		var top = Math.max(ValidationSummaryGetTop(scrollToSummary)-20, 0);
		window.scrollTo(0, top);
    }
}
// TZK: Gets top of validation summary control.
function ValidationSummaryGetTop(element) {
    var offsetY = 0;
    var parent;
    for (parent = element; parent; parent = parent.offsetParent) {
        if (parent.offsetTop) {
            offsetY += parent.offsetTop;
        }
    }
    return offsetY;
}
