var whitespace = " \t\n\r";
var browser = new BrowserDetectLite();

function BrowserDetectLite() {
    var ua = navigator.userAgent.toLowerCase();

    this.ua = ua;

    this.isGecko = (ua.indexOf('gecko') != -1);
    this.isMozilla = (this.isGecko && ua.indexOf("gecko/") + 14 == ua.length);
    this.isNS = ((this.isGecko) ? (ua.indexOf('netscape') != -1) : ((ua.indexOf('mozilla') != -1) && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('opera') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1)));
    this.isIE = ((ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1));
    this.isOpera = (ua.indexOf("opera") != -1);
    this.isKonqueror = (ua.indexOf("konqueror") != -1);
    this.isAol = (ua.indexOf("aol") != -1);
    this.isOmniweb = (ua.indexOf("omniweb") != -1);
    this.isDreamcast = (ua.indexOf("dreamcast") != -1);
    this.isSafari = (ua.indexOf('safari') != -1);

    this.isIECompatible = ((ua.indexOf("msie") != -1) && !this.isIE);
    this.isNSCompatible = ((ua.indexOf("mozilla") != -1) && !this.isIE && !this.isNS && !this.isMozilla);

    this.versionMinor = parseFloat(navigator.appVersion);

    if (this.isNS && this.isGecko) {
        this.versionMinor = parseFloat(ua.substring(ua.lastIndexOf('/') + 1));
    }

    else if (this.isIE && this.versionMinor >= 4) {
        this.versionMinor = parseFloat(ua.substring(ua.indexOf('msie ') + 5));
    }

    else if (this.isOpera) {
        if (ua.indexOf('opera/') != -1) {
            this.versionMinor = parseFloat(ua.substring(ua.indexOf('opera/') + 6));
        }
        else {
            this.versionMinor = parseFloat(ua.substring(ua.indexOf('opera ') + 6));
        }
    }
    else if (this.isKonqueror) {
        this.versionMinor = parseFloat(ua.substring(ua.indexOf('konqueror/') + 10));
    }

    this.versionMajor = parseInt(this.versionMinor);
    this.geckoVersion = ((this.isGecko) ? ua.substring((ua.lastIndexOf('gecko/') + 6), (ua.lastIndexOf('gecko/') + 14)) : -1);

    this.isWin = (ua.indexOf('win') != -1);
    this.isWin32 = (this.isWin && (ua.indexOf('95') != -1 || ua.indexOf('98') != -1 || ua.indexOf('nt') != -1 || ua.indexOf('win32') != -1 || ua.indexOf('32bit') != -1));
    this.isMac = (ua.indexOf('mac') != -1);
    this.isUnix = (ua.indexOf('unix') != -1 || ua.indexOf('linux') != -1 || ua.indexOf('sunos') != -1 || ua.indexOf('bsd') != -1 || ua.indexOf('x11') != -1)

    this.isNS4x = (this.isNS && this.versionMajor == 4);
    this.isNS40x = (this.isNS4x && this.versionMinor < 4.5);
    this.isNS47x = (this.isNS4x && this.versionMinor >= 4.7);
    this.isNS4up = (this.isNS && this.versionMinor >= 4);
    this.isNS6x = (this.isNS && this.versionMajor == 6);
    this.isNS6up = (this.isNS && this.versionMajor >= 6);

    this.isIE4x = (this.isIE && this.versionMajor == 4);
    this.isIE4up = (this.isIE && this.versionMajor >= 4);
    this.isIE5x = (this.isIE && this.versionMajor == 5);
    this.isIE55 = (this.isIE && this.versionMinor == 5.5);
    this.isIE5up = (this.isIE && this.versionMajor >= 5);
    this.isIE6x = (this.isIE && this.versionMajor == 6);
    this.isIE6up = (this.isIE && this.versionMajor >= 6);

    this.isIE4xMac = (this.isIE4x && this.isMac);
}

function setEnableDisable(control, state) {
    var c = findObject(control);

    if (c != null) {
        c.disabled = !state;

        if (c.tagName == 'INPUT' && c.type == 'text') {
            c.readOnly = !state;
        }

        if (c.tagName == 'A') {
            c.style.color = state ? '' : '#ACA899';
        }

        if (c.tagName != 'SELECT' && c.childNodes.length > 0) {
            disableEnableChildren(c, state, setEnableDisable.arguments.length < 3);
        }
    }

    c = null;
}

function disableEnableChildren(control, enable, disableTextTag) {
    if (control.tagName == 'INPUT') {
        if (control.type != 'text') {
            control.disabled = !enable;
        }
        else {
            if (disableTextTag) {
                control.disabled = !enable;
            }

            control.readOnly = !enable;
        }
    }

    if (control.tagName == 'SELECT' || control.tagName == 'A' || control.tagName == 'TD' || control.tagName == 'SPAN') {
        control.disabled = !enable;
        control.style.disabled = !enable;
    }

    if (control.tagName == 'TD' || control.tagName == 'A' || control.tagName == 'SPAN') {
        if (!control.disabled) {
            control.removeAttribute('disabled', false);
        }
        else {
            control.setAttribute('disabled', 'disabled');
        }
    }

    if (control.tagName == 'A') {
        control.style.color = enable ? '' : '#ACA899';
    }

    if (control.tagName != 'SELECT') {
        for (var i = 0; i < control.childNodes.length; i++) {
            disableEnableChildren(control.childNodes.item(i), enable, disableTextTag);
        }
    }
}

function findObject(n, d) {
    var p, i, x;

    if (!d) {
        d = document;

        if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
            d = parent.frames[n.substring(p + 1)].document;
            n = n.substring(0, p);
        }

        if (!(x = d[n]) && d.all) x = d.all[n];

        for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];

        for (i = 0; !x && d.layers && i < d.layers.length; i++) x = findObject(n, d.layers[i].document);

        if (!x && d.getElementById) x = d.getElementById(n);

        return x;
    }
    else {
        return findObject_recursive(n, d);
    }
}

function findAttribute(control, attribute) {
    var __val_IE = (document.all);
    var __val_DOM = (document.getElementById);

    var attrib;

    if (__val_DOM) {
        attrib = control.getAttribute(attribute, false);
    }
    else {
        attrib = eval("document." + (__val_IE) ? "all." : (__val_DOM) ? "getElementById(\"" : ""
			+ control.id + "." + attribute + (_val_DOM && !__val_IE) ? "\")" : "");
    }

    return attrib;
}

function findObject_recursive(n, d) {
    for (var i = 0; i < d.childNodes.length; i++) {
        if (d.childNodes.item(i).id == n) {
            return d.childNodes.item(i);
        }
        var fObj = findObject_recursive(n, d.childNodes.item(i));

        if (fObj != null) return fObj;
    }

    return null;
}

function changeImage() {
    var i, j = 0, x, a = changeImage.arguments;

    for (i = 0; i < (a.length - 1); i += 2) {
        if ((x = findObject(a[i])) != null) {
            x.src = a[i + 1];
        }
    }
}

function preloadImages() {
    var d = document;

    if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();

        var i, j = d.MM_p.length, a = preloadImages.arguments;

        for (i = 0; i < a.length; i++) {
            if (a[i].indexOf("#") != 0) {
                d.MM_p[j] = new Image;
                d.MM_p[j++].src = a[i];
            }
        }
    }
}

function setFocus(control) {
    var c = findObject(control);

    if (c != null && c.focus) {
        c.focus();
    }
}

function textCounter(itemName, maxLimit) {
    var field = findObject(itemName);

    if ((field != null) && (field.value.length > maxLimit)) {
        field.value = field.value.substring(0, maxLimit);
    }
}

function replaceAll(s, fromStr, toStr) {
    var new_s = s;

    for (var i = 0; i < 100 && new_s.indexOf(fromStr) != -1; i++) {
        new_s = new_s.replace(fromStr, toStr);
    }

    return new_s;
}

function isEmpty(s) {
    return ((s == null) || (s.length == 0))
}

function RTrim(strTrim) {
    var str = new String(strTrim);
    var i = 0;
    var c = "";
    var endpos = 0

    for (i = str.length; i >= 0 && endpos == 0; i = i - 1) {
        c = str.charAt(i);

        if (whitespace.indexOf(c) == -1) {
            endpos = i;
        }
    }

    return str.substring(0, endpos + 1);
}

function regularExpressionValidator(value, expression) {
    var rx = new RegExp(expression);
    var matches = rx.exec(value);

    return r = (matches != null && value == matches[0]);
}

function isFileName(value) {
    if (navigator.appVersion.toLowerCase().indexOf("mac") > 0) {
        return true;
    }

    if (window.opera) {
        value = "c:\\" + value;
    }

    return regularExpressionValidator(value, "([a-zA-Z]:\\\\[^/:\\*\\?<>\\|]+\\.\\w{2,6})|(\\\\{2}[^/:\\*\\?<>\\|]+\\.\\w{2,6})");
}

function isEmailList(value) {
    return regularExpressionValidator(value, "^(([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5}){1,25})+)*$");
}

function getCheckBoxEnabled(val) {
    var isEnabled = false;

    if (val != null && val.tagName == 'INPUT') {
        if (val.type == 'checkbox' && !val.disabled) {
            isEnabled = true;
        }
    }

    var childItems = val.childNodes;

    if (childItems != null && isEnabled == false) {
        for (var i = 0; i < childItems.length; i++) {
            isEnabled = getCheckBoxEnabled(childItems.item(i));

            if (isEnabled) {
                break;
            }
        }
    }

    return isEnabled;
}

function getCheckBoxChecked(val) {
    var isChecked = false;

    if (val != null && val.tagName == 'INPUT') {
        if (val.type == 'checkbox' && !val.disabled) {
            if (val.checked) {
                isChecked = true;
            }
        }
    }

    var childItems = val.childNodes;

    if (childItems != null && isChecked == false) {
        for (var i = 0; i < childItems.length; i++) {
            isChecked = getCheckBoxChecked(childItems.item(i));

            if (isChecked) {
                break;
            }
        }
    }

    return isChecked;
}

function CheckBoxListValidation(val) {
    var target = findObject(dom_getAttribute(val, "controltovalidate"));

    if (target != null) {
        var panel = target.parentNode;

        if (panel == null) {
            return false;
        }

        var hasEnabledItems = getCheckBoxEnabled(panel);

        if (hasEnabledItems) {
            return getCheckBoxChecked(panel);
        }
        else {
            return true;
        }
    }

    return false;
}

function EnabledTextBoxValidation(val) {
    var valid = true;
    var textBox = findObject(dom_getAttribute(val, "controltovalidate"));

    if ((textBox.tagName == 'INPUT') || (textBox.tagName == 'TEXTAREA')) {
        if ((textBox.type == 'text') || (textBox.type == 'file') || textBox.type == 'textarea') {
            if ((textBox.readOnly) || (textBox.disabled) || (textBox.value.length > 0)) {
                valid = true;
            }
            else {
                valid = false;
            }
        }
    }

    return valid;
}

function EnabledTextBoxValidationSelectedRadio(val) {
    var radio = findObject(dom_getAttribute(val, "controllingradiobutton"));

    if (radio == null) {
        return false;
    }

    var valid = EnabledTextBoxValidation(val);

    return radio.checked ? valid : true;
}

function GetCountCheckedItem(parentName) {
    var target = findObject(parentName);

    return getCheckedCount(target);
}

function getCheckedCount(target) {
    var count = 0;

    if (target != null) {
        var col = target.childNodes;

        if (col != null) {
            for (var i = 0; i < col.length; i++) {
                if (col.item(i).tagName == 'INPUT') {
                    var id_element = col.item(i).getAttribute('id');

                    if (id_element != null) {
                        var index = id_element.indexOf("AllSelector");

                        if (col.item(i).checked) {
                            count++;

                            if (index != -1) {
                                count--;
                            }
                        }
                    }
                }
                else {
                    count += getCheckedCount(col.item(i));
                }
            }
        }
    }

    return count;
}
function GetCountCheckedItemWithSpecificAttribut(parentName, attribut) {
    var targetObj = findObject(parentName);

    return getCheckedCountWithSpecificAttribut(targetObj, attribut);
}

function getCheckedCountWithSpecificAttribut(target, attribut) {
    var count = 0;

    if (target != null) {
        var col = target.childNodes;

        if (col != null) {
            for (var i = 0; i < col.length; i++) {
                if (col.item(i).tagName == 'INPUT') {
                    var id_element = col.item(i).getAttribute('id');
                    var attr = col.item(i).getAttribute(attribut);

                    if (id_element != null && attr != null) {
                        if (col.item(i).checked) {
                            count++;
                        }
                    }
                }
                else {
                    count += getCheckedCountWithSpecificAttribut(col.item(i), attribut);
                }
            }
        }
    }

    return count;
}

function GetCountCheckedItemEx(parentName) {
    var target = findObject(parentName);

    return getCheckedCountEx(target);
}

function getCheckedCountEx(target) {
    var count = 0;

    if (target != null) {
        var col = target.childNodes;

        if (col != null) {
            for (var i = 0; i < col.length; i++) {
                if (col.item(i).tagName == 'INPUT') {
                    if (col.item(i).checked) {
                        count++;
                    }
                }
                else if (col.item(i).tagName != 'TABLE') {
                    count += getCheckedCountEx(col.item(i));
                }
            }
        }
    }

    return count;
}

function disableFileInputs() {
    var objects = document.all;

    for (var i = 0; i < objects.length; i++) {
        if ((objects.item(i).tagName == 'INPUT') && (objects.item(i).type == 'file')) {
            objects.item(i).disabled = true;
        }
    }
}

function validateUploadFile(control, message) {
    var c = findObject(control);

    var result = true;

    if ((c != null) && !isEmpty(c.value)) {
        result = isFileName(c.value);
    }

    if (!result) {
        alert(message);
    }

    return result;
}
function validateTextFields() {
    var params = validateTextFields.arguments;
    var curElem, curForm, allForms = document.forms;

    for (var i = 0; i < params.length; i += 2) {
        for (var j = 0; j < allForms.length; j++) {
            curForm = allForms.item(j);

            for (var k = 0; k < curForm.elements.length; k++) {
                curElem = curForm.elements.item(k);

                if ((curElem.tagName == "INPUT") && (curElem.type == "text")
												 && (curElem.id.indexOf(params[i]) == 0)) {
                    if (isEmpty(curElem.value)) {
                        if (params[i + 1] != "") {
                            alert(params[i + 1]);
                        }
                        curElem.focus();

                        return false;
                    }
                }
            }
        }
    }

    return true;
}

function validateOneFieldsFill(fieldID, type, message) {
    var curElem, curForm, allForms = document.forms, countFillElem = 0;

    for (var i = 0; i < allForms.length; i++) {
        curForm = allForms.item(i);

        for (var j = 0; j < curForm.elements.length; j++) {
            curElem = curForm.elements.item(j);

            if ((curElem.tagName == "INPUT") && (curElem.type == type)
											 && (curElem.id.indexOf(fieldID) != -1)) {
                if (curElem.disabled == true) return true;

                if (!isEmpty(curElem.value)) countFillElem++;

                curElem.focus();
            }
        }
    }

    if (countFillElem > 0) return true;

    if (message != "") alert(message);

    return false;
}

function validateFileFields(fieldID, message, expression, allowEmpties, allowLike) {
    var curElem, curForm, allForms = document.forms;

    if (allowLike == null) {
        allowLike = true;
    }

    for (var i = 0; i < allForms.length; i++) {
        curForm = allForms.item(i);

        for (var j = 0; j < curForm.elements.length; j++) {
            curElem = curForm.elements.item(j);

            if ((curElem.tagName == "INPUT") && (curElem.type == "file")
											 && ((allowLike && curElem.id.indexOf(fieldID) != -1) || curElem.id == fieldID)) {
                if (curElem.disabled == true) return true;
                if (allowEmpties && isEmpty(curElem.value)) continue;

                var isValidExpression = true;

                if ((expression != null) && (expression.length > 0)) {
                    isValidExpression = regularExpressionValidator(curElem.value, expression);
                }

                if ((isEmpty(curElem.value)) || (!isFileName(curElem.value)) || (!isValidExpression)) {
                    if (message != "") alert(message);

                    curElem.focus();

                    return false;
                }
            }
        }
    }

    return true;
}

function validateStatusFilter(statusID, stateID, conditionID, daysID, relatedDateID, message) {
    var curElem, curForm, allForms = document.forms;
    var statusControl, stateControl, conditionControl;

    for (var i = 0; i < allForms.length; i++) {
        curForm = allForms.item(i);

        for (var j = 0; j < curForm.elements.length; j++) {
            curElem = curForm.elements.item(j);

            if ((curElem.tagName == "SELECT") && (curElem.value == "-1" || curElem.value == "")
				&& curElem.disabled != true && curElem.id.indexOf(stateID) != -1) {
                alert(message);

                curElem.focus();

                return false;
            }

            if ((curElem.tagName == "INPUT") && (curElem.type == "text") &&
				(curElem.id.indexOf(daysID) != -1) && curElem.disabled != true && (curElem.value == "" || Number(curElem.value).toString() == "NaN")) {
                alert(message);

                curElem.focus();

                return false;
            }
        }
    }

    return true;
}

function validateUploadRegionFields(txtControl, txtMessage, fileControl, fileMessage, fileRegExp) {
    var curElem, curForm, allForms = document.forms;

    for (var j = 0; j < allForms.length; j++) {
        curForm = allForms.item(j);

        for (var k = 0; k < curForm.elements.length; k++) {
            curElem = curForm.elements.item(k);

            if ((curElem.tagName == "INPUT") && (curElem.id.indexOf(txtControl) == 0)) {
                var pairElem = findObject(fileControl + curElem.id.substr(txtControl.length));

                if (pairElem != null) {
                    if (isEmpty(curElem.value) && isEmpty(pairElem.value)) continue;

                    if (isEmpty(curElem.value)) {
                        alert(txtMessage);
                        curElem.focus();

                        return false;
                    }
                    else if (isEmpty(pairElem.value) || !isFileName(pairElem.value) || !regularExpressionValidator(pairElem.value, fileRegExp)) {
                        alert(fileMessage);
                        pairElem.focus();

                        return false;
                    }
                }
            }
        }
    }

    return true;
}

function generateProfileDomain(outputControl, textBoxControl) {
    var c = findObject(outputControl);

    if (c != null) {
        var expr = /[\W\s]/g;

        c.innerHTML = c.innerHTML.substring(0, c.innerHTML.lastIndexOf('/') + 1) + textBoxControl.value.replace(expr, '_');
    }
}

// Session watcher
var clientTimeout = 0;
var expiredLimit = 7200 - 20; // Session Timeout minus some amount of forestalling time
var warningLimit = expiredLimit - 300;
var warnWnd = null;
var warnWndPath = null;
var timerID;

function attachWatch(wndPath) {
    warnWndPath = wndPath;

    if (window.addEventListener) {
        window.addEventListener('load', startWatch, false);
        window.addEventListener('unload', closeWarning, false);
    }
    else {
        window.attachEvent('onload', startWatch);
        window.attachEvent('onunload', closeWarning);
    }
}

function resetWatch() {
    if (warnWnd == null) clientTimeout = 0;
    else if (!warnWnd.closed) warnWnd.focus();
}

function startWatch() {
    if (timerID != null) {
        window.clearInterval(timerID);
    }

    timerID = window.setInterval("watchDog()", 1000);
}

function watchDog() {
    var el;

    clientTimeout++;

    if (clientTimeout > expiredLimit) {
        closeWarning();

        location = "logout.aspx";
    }
    else if (clientTimeout > warningLimit) {
        var positionLeft = Math.round(document.body.offsetWidth / 2) - 150 + document.body.scrollLeft;
        var positionTop = Math.round(document.body.offsetHeight / 2) - 125 + document.body.scrollTop;

        if (warnWnd == null) {
            warnWnd = window.open(warnWndPath, "_blank", "width=300,height=250,menubar=no,toolbar=no,scrollbars=no,resizable=no,statusbar=no,left=" + positionLeft + ",top=" + positionTop);

            warnWnd.focus();
        }

        if (warnWnd != null && !warnWnd.closed && (el = warnWnd.document.getElementById("expire"))) {
            el.value = expiredLimit - clientTimeout;
        }
    }
}

function closeWarning() {
    if (warnWnd != null && !warnWnd.closed) {
        warnWnd.close();
    }

    warnWnd = null;

    resetWatch();
}

/* Multiline script */

var names = new Array;
var count;

function addRow(elem, isEnabled) {
    tableObj = findParent(elem, 'TABLE')
    if (!tableObj.processed) {
        recursive(tableObj, 'save')
        tableObj.processed = true
    }

    tableObj.tBodies[0].insertBefore(tableObj.rows[tableObj.rows.length - 2].cloneNode(true), tableObj.rows[tableObj.rows.length - 1])

    if (isEnabled != null) {
        if (isEnabled) {
            recursive(tableObj.rows[tableObj.rows.length - 2], 'clear&enabled')
        }
        else {
            recursive(tableObj.rows[tableObj.rows.length - 2], 'clear&disabled')
        }
    }
    else {
        recursive(tableObj.rows[tableObj.rows.length - 2], 'clear')
    }
    setIndex(tableObj)
}

function remRow(elem) {
    tableObj = findParent(elem, 'TABLE')
    if (tableObj.rows.length > 3) {
        trObj = findParent(elem, 'TR')
        tableObj.deleteRow(trObj.sectionRowIndex)
    }
}

function remRowNotUploaded(elem) {
    tableObj = findParent(elem, 'TABLE')
    if (tableObj.rows.length > 4) {
        trObj = findParent(elem, 'TR')
        tableObj.deleteRow(trObj.sectionRowIndex)
    }
}
function remRowUploaded(elem) {
    tableObj = findParent(elem, 'TABLE')
    trObj = findParent(elem, 'TR')
    tableObj.deleteRow(trObj.sectionRowIndex)
}

function findParent(childObj, parentTag) {
    try { while (childObj.parentNode.tagName != parentTag) childObj = childObj.parentNode } catch (childObj) { return null }
    return childObj.parentNode
}

function setIndex(tableObj) {
    for (i = 1; i < tableObj.rows.length; i++) {
        count = 0
        recursive(tableObj.rows[i], 'set', i)
    }
}

function recursive(parentObj, mode, row) {
    var i, childObj, mode;
    childObj = parentObj.childNodes
    for (i = 0; i < childObj.length; i++) {
        if (childObj[i].tagName == 'INPUT' || childObj[i].tagName == 'SELECT' || childObj[i].tagName == 'TEXTAREA')
            switch (mode) {
            case "save":
                names[tableObj, names.length] = childObj[i].name
                break
            case "set":
                childObj[i].id = childObj[i].name = names[tableObj, count] + row.toString()
                count++
                break
            case "clear":
            case "clear&enabled":
            case "clear&disabled":
                if (childObj[i].tagName == 'INPUT' && childObj[i].type == 'file') {
                    var MyObject = new Object();

                    MyObject = document.createElement('INPUT')
                    MyObject.setAttribute('type', 'file')
                    MyObject.setAttribute('size', childObj[i].getAttribute('size'))
                    if (this.isIE) {
                        MyObject.setAttribute('className', childObj[i].className)
                    }
                    else {
                        MyObject.setAttribute('class', childObj[i].getAttribute('class'))
                    }

                    parentObj.removeChild(childObj[i])
                    parentObj.appendChild(MyObject)
                }
                else childObj[i].value = ''
                if (mode == "clear&enabled") {
                    if (childObj[i].tagName == 'INPUT' && childObj[i].type == 'text') {
                        childObj[i].readOnly = false;
                        childObj[i].disabled = false;
                    }
                    else {
                        childObj[i].disabled = false;
                    }
                }
                if (mode == "clear&disabled") {
                    if (childObj[i].tagName == 'INPUT' && childObj[i].type == 'text') {
                        childObj[i].readOnly = true;
                        childObj[i].disabled = true;
                    }
                    else {
                        childObj[i].disabled = true;
                    }
                }
                break
        }

        if (childObj[i] != null) {
            if (childObj[i].childNodes.length > 0) recursive(childObj[i], mode, row)
        }
    }
}

// Itinerary

var validatorsState;

function initValidatorsState(disabledValidators) {
    if (disabledValidators != null) {
        var controls = disabledValidators.split(",");

        for (j = 0; j < controls.length; j++) {
            var object = findObject(controls[j]);

            if (object != null) {
                object.enabled = false;
            }
        }
    }
}

function saveValidatorsState(enabledValidators) {
    if (validatorsState == null) {
        validatorsState = new Array(Page_Validators.length);
    }

    for (i = 0; i < Page_Validators.length; i++) {
        validatorsState[i] = Page_Validators[i].enabled;

        Page_Validators[i].enabled = false;
    }

    if (enabledValidators != null) {
        var controls = enabledValidators.split(",");

        for (j = 0; j < controls.length; j++) {
            var object = findObject(controls[j]);

            if (object != null) {
                object.enabled = true;
            }
        }
    }
}

function restoreValidatorsState() {
    for (i = 0; i < Page_Validators.length; i++) {
        Page_Validators[i].enabled = validatorsState[i];
    }
}


function syncObj() {
    var repeat = false, obj;
    var step = 10;

    if (syncLoadTimer) clearTimeout(syncLoadTimer);

    if (loadObj.style.visibility == "visible") obj = loadObj; else obj = winObj;

    /* Fast position 
    obj.style.top=document.body.scrollTop+document.body.clientHeight/2-obj.offsetHeight/2
    obj.style.left=document.body.clientWidth/2-obj.offsetWidth/2
    */

    topReq = document.body.scrollTop + document.body.clientHeight / 2 - obj.offsetHeight / 2;
    leftReq = document.body.clientWidth / 2 - obj.offsetWidth / 2;

    if (obj.offsetTop < topReq) {
        obj.style.top = obj.offsetTop + step;
        if (obj.offsetTop + step > topReq) obj.style.top = topReq; else repeat = true;
    }

    if (obj.offsetTop > topReq) {
        obj.style.top = obj.offsetTop - step;
        if (obj.offsetTop - step < topReq) obj.style.top = topReq; else repeat = true;
    }

    if (obj.offsetLeft < leftReq) {
        obj.style.left = obj.offsetLeft + step;
        if (obj.offsetLeft + step > leftReq) obj.style.left = leftReq; else repeat = true;
    }
    if (obj.offsetLeft > leftReq) {
        obj.style.left = obj.offsetLeft - step;
        if (obj.offsetLeft - step < leftReq) obj.style.left = leftReq; else repeat = true;
    }

    if (repeat) syncLoadTimer = setTimeout("syncObj()", 0)
}

function PostBackRegisterEditors() {
    if (isOPERA) {
        if (RadEditorGlobalArray != null) {
            for (var i = 0; i < RadEditorGlobalArray.length; i++) {
                if (typeof (RadEditorGlobalArray[i].PostBackRegisterEditor) == "function") {
                    RadEditorGlobalArray[i].PostBackRegisterEditor(RadEditorGlobalArray[i]);
                }
            }
        }
    }
}

function SetStatusName(selectObject, selectedObjectName, stateName, conditionName, daysName, dateName) {
    var prefix = selectObject.id.substring(0, selectObject.id.indexOf(selectedObjectName))
    var suffix = selectObject.id.substring(selectObject.id.indexOf(selectedObjectName) + selectedObjectName.length)

    var stateID = prefix + stateName + suffix;
    var conditionID = prefix + conditionName + suffix;
    var daysID = prefix + daysName + suffix;
    var dateID = prefix + dateName + suffix;

    if (selectObject != null) {
        if (selectObject.value == "") {
            setEnableDisable(stateID, false);
            setEnableDisable(conditionID, false);
            setEnableDisable(daysID, false);
            setEnableDisable(dateID, false);
        }
        else {
            setEnableDisable(stateID, true);

            var object = findObject(stateID);

            SetStatusState(object, stateName, conditionName, daysName, dateName);
        }
    }
}

function SetStatusState(selectObject, selectedObjectName, conditionName, daysName, dateName) {
    var prefix = selectObject.id.substring(0, selectObject.id.indexOf(selectedObjectName))
    var suffix = selectObject.id.substring(selectObject.id.indexOf(selectedObjectName) + selectedObjectName.length)

    var conditionID = prefix + conditionName + suffix;
    var daysID = prefix + daysName + suffix;
    var dateID = prefix + dateName + suffix;

    if (selectObject != null && selectObject.value != "") {
        var selectedID = selectObject.value;

        if (selectedID == -1) {
            setEnableDisable(conditionID, false);
            setEnableDisable(daysID, false);
            setEnableDisable(dateID, false);
        }
        else if (selectedID == 1 || selectedID == 0) {
            setEnableDisable(conditionID, true);

            var object = findObject(daysID);

            SetStatusCondition(object, conditionName, daysName, dateName);
        }
    }
}

function SetStatusCondition(selectObject, selectedObjectName, daysName, dateName) {
    var prefix = selectObject.id.substring(0, selectObject.id.indexOf(selectedObjectName))
    var suffix = selectObject.id.substring(selectObject.id.indexOf(selectedObjectName) + selectedObjectName.length)

    var daysID = prefix + daysName + suffix;
    var dateID = prefix + dateName + suffix;

    if (selectObject != null && selectObject.value != "") {
        var selectedID = selectObject.value;

        if (selectedID == -1) {
            setEnableDisable(daysID, false);
            setEnableDisable(dateID, false);
        }
        else {
            setEnableDisable(daysID, true);
            setEnableDisable(dateID, true);
        }
    }
}

function RefreshNewRow(object, selectedObjectName, stateName, conditionName, daysName, dateName) {
    var tableObj = findParent(object, 'TABLE');

    var selectObject = tableObj.rows[tableObj.rows.length - 2].children[0].children[0];

    SetStatusName(selectObject, selectedObjectName, stateName, conditionName, daysName, dateName);

    var prefix = selectObject.id.substring(0, selectObject.id.indexOf('ddlStatus'))
    var suffix = selectObject.id.substring(selectObject.id.indexOf('ddlStatus') + 'ddlStatus'.length)

    var stateID = prefix + 'ddlState' + suffix;

    findObject(stateID).value = -1;
}


function findAspControlClientId(aspControlId) {
    var count = document.getElementsByTagName('*').length;
    var i = 0;
    var elementName;
    for (i = 0; i < count; i++) {
        elementName = document.getElementsByTagName('*')[i].id;
        pos = elementName.indexOf(aspControlId);
        if (pos >= 0) break;
    }
    return elementName;
}

/*-------------- Message Box ---------*/
function getViewportWidth() {
    var width = 0;
    if (document.documentElement && document.documentElement.clientWidth) {
        width = document.documentElement.clientWidth;

    }
    else if (document.body && document.body.clientWidth) {
        width = document.body.clientWidth;
    }
    else if (window.innerWidth) {
        width = window.innerWidth - 18;
    }
    return width;
}

function getViewportHeight() {
    var height = 0;
    if (document.documentElement && document.documentElement.clientHeight) {
        height = document.documentElement.clientHeight;
    }
    else if (document.body && document.body.clientHeight) {
        height = document.body.clientHeight;
    }
    else if (window.innerHeight) {
        height = window.innerHeight - 18;
    }
    return height;
}
function getBackgroundWidth() {
    var windowWidth = getViewportWidth();
    var htmlWidth = -1;
    if (document.body) {
        for (var i = 0; i < document.body.childNodes.length; i++) {
            var cN = document.body.childNodes[i];
            var cW = cN.offsetWidth ? cN.offsetWidth : (cN.style && cN.style.width) ? parseInt(cN.style.width) : 0;
            htmlWidth = (htmlWidth < cW) ? cW : htmlWidth;
        }
    }
    else {
        htmlWidth = document.body.scrollWidth;
    }

    if (htmlWidth > windowWidth) {
        return htmlWidth;
    }
    else {
        return windowWidth;
    }
}

function getBackgroundHeight() {
    var windowHeight = getViewportHeight();
    var htmlHeight = document.body.scrollHeight;
    if (navigator.userAgent.toLowerCase().indexOf("opera") != -1) {
        htmlHeight = document.body.offsetHeight;
    }
    if (htmlHeight > windowHeight) {
        return htmlHeight;
    }
    else {
        return windowHeight;
    }
}

function onResizeHandler() {
    var divWarringBG = document.getElementById("dvWarringBackgroud");
    var divWarring = document.getElementById("dvMessage");

    divWarringBG.style.width = getBackgroundWidth() + "px";
    divWarringBG.style.height = getBackgroundHeight() + "px";

    divWarring.style.left = ((getViewportWidth() - 300) / 2) + "px";
    divWarring.style.top = ((getViewportHeight() - 160) / 2) + "px";

}

