// DomValidation.js.
//<script>
var Page_DomValidationVer = "2";
var Page_IsValid = true;  
var Page_BlockSubmit = false;
var Invisible_Text_Value = "______________________";

function ValidatorUpdateDisplay(val) {
    //if (typeof(val.display) == "string") {    
        if (val.display == "None") {
            return;
        }

			
		if (val.display == "Dynamic" || val.display == null) {
            val.style.display = val.isvalid ? "none" : "inline";
			if (typeof(UpdateNewCIValidatorDisplay) == 'function')
			{
				UpdateNewCIValidatorDisplay(val);
			}
            return;
        }
   // }
    val.style.visibility = val.isvalid ? "hidden" : "visible";
    if (typeof(UpdateNewCIValidatorDisplay) == 'function')
    {
		UpdateNewCIValidatorDisplay(val);
    }    
}

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 (control == null)
		return "";
		
    //HACK
    if (control.style.display == 'none')
		return Invisible_Text_Value;
		
    if (typeof(control.value) == "string") {
        return control.value;
    }
    var j;
    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;
            }
        }
    }
    for (j=0; j < 1000; j++) {
        var inner = document.getElementById(id+'_'+j);
        if (inner==null) break;
        if (typeof(inner.value) == "string" && inner.type == "radio" && inner.checked) {
            return inner.value;
        }
    }
    return "";
}

function Page_ClientValidate() {
    var i;
    for (i = 0; i < Page_Validators.length; i++) {
        ValidatorValidate(Page_Validators[i]);
    }
    ValidatorUpdateIsValid();    
    ValidationSummaryOnSubmit();
    Page_BlockSubmit = !Page_IsValid;
    return Page_IsValid;
}

function ValidatorCommonOnSubmit() {
    var returnValue =  !Page_BlockSubmit;
    Page_BlockSubmit = false;    
    return returnValue;
}

function ValidatorEnable(val, enable) {
    val.enabled = (enable != false);
    ValidatorValidate(val);
    ValidatorUpdateIsValid();
}

function ValidatorOnChange(controlID) {
    var cont = document.getElementById(controlID);
    var vals = cont.Validators;
    var i;
    
    if(vals != null)
    {
		for (i = 0; i < vals.length; i++) {
			ValidatorValidate(vals[i]);
		}
    }
    ValidatorUpdateIsValid();    
}

function ValidatorValidate(val) {    
    val.isvalid = true;
    if (val.enabled != false) {
		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);
        }        
        var controlToHookup = val.getAttribute("controlhookup");
        if (typeof(controlToHookup) == "string") {
            ValidatorHookupControl(document.getElementById(controlToHookup), val);
        }
    }
    Page_ValidationActive = true;
}

function RegularExpressionValidatorEvaluateIsValid(val) {
    var value = ValidatorGetValue(val.getAttribute("controltovalidate"));
    if (value == "" || val.getAttribute("validationexpression") == null || val.getAttribute("validationexpression") == "")
        return true;        
    var rx = new RegExp(val.getAttribute("validationexpression"));
    var matches = rx.exec(value);
    return (matches != null && value == matches[0]);
}

function ValidatorTrim(s) {
	s = s.replace(/^\s+/, '');
	
	for (var i = s.length - 1; i >= 0; i--) {
		if (/\S/.test(s.charAt(i))) {
			s = s.substring(0, i + 1);
			break;
		}
	}
	
	return s;

/*
    var m = s.match(/^\s*(.*\S)\s*$/);alert(m);
    return (m == null) ? "" : m[1];
    */
}

function RequiredFieldValidatorEvaluateIsValid(val) {


    return (ValidatorTrim(ValidatorGetValue(val.getAttribute("controltovalidate"))) != ValidatorTrim(val.getAttribute("initialvalue")));
}





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 {
        return op.toString();
    }
}
function ValidatorCompare(operand1, operand2, operator, val) {
    var dataType = val.type;
    var op1, op2;
    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 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.operator, val);
}

function ValidationSummaryOnSubmit() {
    if (typeof(Page_ValidationSummaries) == "undefined") 
        return;
    var summary, sums, s;
    for (sums = 0; sums < Page_ValidationSummaries.length; sums++) {
        summary = Page_ValidationSummaries[sums];
        summary.style.display = "none";
        if (!Page_IsValid) {
            if (summary.getAttribute("showsummary") != "False") {
                summary.style.display = "";
                if (typeof(summary.getAttribute("displaymode")) != "string") {
                    summary.setAttribute("displaymode", "BulletList");
                }
                switch (summary.getAttribute("displaymode")) {
                    case "List":
                        headerSep = "<br>";
                        first = "";
                        pre = "";
                        post = "<br>";
                        final = "";
                        break;
                    case "BulletList":
                    default: 
                        headerSep = "";
                        first = "<ul>";
                        pre = "<li>";
                        post = "</li>";
                        final = "</ul>";
                        break;
                    case "SingleParagraph":
                        headerSep = " ";
                        first = "";
                        pre = "";
                        post = " ";
                        final = "<br>";
                        break;
                }
                s = "";
                if (typeof(summary.getAttribute("headertext")) == "string") {
                    s += summary.getAttribute("headertext") + headerSep;
                }
                s += first;
                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 += final;
                summary.innerHTML = s; 
                window.scrollTo(0,0);
            }
            if (summary.getAttribute("showmessagebox") == "True") {
                s = "";
                if (typeof(summary.getAttribute("headertext")) == "string") {
                    s += summary.getAttribute("headertext") + "<BR>";
                }
                for (i=0; i<Page_Validators.length; 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") + "<BR>";
                                break;
                            case "BulletList":
                            default: 
                                s += "  - " + Page_Validators[i].getAttribute("errormessage") + "\n";//"<BR>";
                                break;
                            case "SingleParagraph":
                                s += Page_Validators[i].getAttribute("errormessage") + " ";
                                break;
                        }
                    }
                }
                return;
                
                
                span = document.createElement("SPAN");
				alert(span.innerText);return;
                s = span.innerText;
                alert(s);
            }
        }
    }
}

//**********************************
function L(name, otherValue, x) 
{ 
	t = document.getElementById(name+'__ctl1__ctl0'); 
	if (t == null) return; 
	for (i=0;i<10000;i++) 
	{ 
		o = document.getElementById(name+'__ctl0_'+i); 
		if (o == null) break;
		t.style.display = o.checked ? '':'none'; 
	} 
	M(t, x);
}
function M(t, x)
{
	if (t.style.display == 'none' && t.value == '')
		t.value = Invisible_Text_Value;
	else if (t.value == Invisible_Text_Value)
		t.value = '';
		
	if (x) ValidatorOnChange(t.id);
}
function DDL(name, x)
{ 
	t = document.getElementById(name+'__ctl1__ctl0');
	o = document.getElementById(name+'__ctl0'); 
	if (o == null || t == null) return; 
	t.style.display = o.selectedIndex == o.length - 1 ? '':'none'; 
	M(t, x);
}
function TB(name, maxLength, x)
{
	t = document.getElementById(name); 
	if (t == null) return; 

	if (t.value.length > maxLength) 
		t.value = t.value.substring(0, maxLength);		
}

var date_arr = new Array;
date_arr[0]=31;
date_arr[1]=31;
date_arr[2]=28;
date_arr[3]=31;
date_arr[4]=30;
date_arr[5]=31;
date_arr[6]=30;
date_arr[7]=31;
date_arr[8]=30;
date_arr[9]=30;
date_arr[10]=31;
date_arr[11]=31;
date_arr[12]=30;

function UpdateDays(name)
{
	d=document.getElementById(name+'__ctl1__ctl0');
	m=document.getElementById(name+'__ctl3__ctl0');
	y=document.getElementById(name+'__ctl5__ctl0');
	
	temp=d.selectedIndex;

	for(x=d.options.length; x<=date_arr[0];x++)
	{
		d.options[x]=new Option(x,x);
	}
	d.options.length=date_arr[m.selectedIndex]+1;

	cy=parseInt(y[y.selectedIndex].value);
	if(m.selectedIndex==2&&((cy%4==0&&cy%100!= 0)||(y.selectedIndex==0))) d.options[d.options.length]=new Option(29,29);
	if(temp >= d.options.length ) temp=d.options.length-1;
	d.options[temp].selected=true;
}

		function IngoreWhiteSpace(e,el) {
			if(typeof(el) != 'undefined') {
				el.value = (el.value).replace(/^\s*|\s*$/g,'');
			}
			var unicode = e.charCode ? e.charCode : e.keyCode;
				if (unicode == 32) {
                    return false;
                }
                else {
                    return true;
                }
		}