// Keep user from entering more than maxLength characters
function doKeypress(control) {
    maxLength = control.attributes["maxLength"].value;
    value = control.value;
    if (maxLength && value.length > maxLength - 1) {
        event.returnValue = false;
        maxLength = parseInt(maxLength);
    }
}
// Cancel default behavior
function doBeforePaste(control) {
    maxLength = control.attributes["maxLength"].value;
    if (maxLength) {
        event.returnValue = false;
    }
}
// Cancel default behavior and create a new paste routine
function doPaste(control) {
    maxLength = control.attributes["maxLength"].value;
    value = control.value;
    if (maxLength) {
        event.returnValue = false;
        maxLength = parseInt(maxLength);
        var oTR = control.document.selection.createRange();
        var iInsertLength = maxLength - value.length + oTR.text.length;
        var sData = window.clipboardData.getData("Text").substr(0, iInsertLength);
        oTR.text = sData;
    }
}

function SetInnerText(id, value) {
    var elem = document.getElementById(id);
    if (BrowserSupportsInnerText())
        elem.innerText = value;
    else
        elem.textContent = value;

}

function BrowserSupportsInnerText() {
    return (document.getElementsByTagName("body")[0].innerText != undefined) ? true : false;

}

function HideContent(id) {
    var element = document.getElementById(id);

    if (element != null)
        element.style.display = "hidden";
}

function ShowContent(id) {
    var element = document.getElementById(id);

    if (element != null)
        element.style.display = "";
}

function SetTextBox(ddlYearId, ddlMonthId, ddlDayId, textboxId) {
    ddlyear = document.getElementById(ddlYearId);
    ddlmonth = document.getElementById(ddlMonthId);
    ddlday = document.getElementById(ddlDayId);
    txtdate = document.getElementById(textboxId);

    //Don't set the text box if not all the drop downs have valid values.
    if ((ddlyear.value == 0) || (ddlmonth.value == 0) || (ddlday.value == 0)) {
        txtdate.value = "";
        return;
    }

    //Pad additional "0" if necessary for Month and Day.
    if (ddlmonth.value.length == 1)
        strMonth = "0" + ddlmonth.value;
    else
        strMonth = ddlmonth.value;

    if (ddlday.value.length == 1)
        strDay = "0" + ddlday.value;
    else
        strDay = ddlday.value;

    strYear = ddlyear.value;

    //Set the Text box with the date in the specified format 
    txtdate.value = strMonth + "/" + strDay + "/" + strYear;
}

/*
forces ui to fill screen horizontally, regardless of content
<wraper> : id of element used to wrap your content
<element_to_pad> : id of element to add padding to (usually main content div)
*/
function stretchUI_vertical(wrapper, element_to_pad) {
    if (!YAHOO.util.Dom)
        return;

    var wrapper_bottom = YAHOO.util.Dom.getRegion(wrapper).bottom;
    var wrapper_top = YAHOO.util.Dom.getRegion(wrapper).top;
    var viewport_height = YAHOO.util.Dom.getViewportHeight();

    // if the page is scrolling, we don't need to do anything
    if (viewport_height < wrapper_bottom)
        return;

    var offset = wrapper_top;
    var padding_needed = (viewport_height - wrapper_bottom + 10) - (offset);

    if (padding_needed > 0)
        YAHOO.util.Dom.setStyle(element_to_pad, "paddingBottom", padding_needed + "px");
}

// DHTML Effects
var enablepersist = "on"
var memoryduration = "7"

var contractsymbol = '/eSecuritel/HITS/WebSite/images/Collapse_13x13.jpg'
var expandsymbol = '/eSecuritel/HITS/WebSite/images/Expand_13x13.jpg'


function getElementbyClass(rootobj, classname) {
    var temparray = new Array()
    var inc = 0
    var rootlength = rootobj.length
    for (i = 0; i < rootlength; i++) {
        if (rootobj[i].className == classname)
            temparray[inc++] = rootobj[i]
    }
    return temparray
}

function sweeptoggle(ec) {
    var inc = 0
    while (ccollect[inc]) {
        ccollect[inc].style.display = (ec == "contract") ? "none" : ""
        inc++
    }
    revivestatus()
}

function expandcontent(curobj, cid) {
    if (ccollect.length > 0) {
        document.getElementById(cid).style.display = (document.getElementById(cid).style.display != "none") ? "none" : ""
        curobj.alt = (document.getElementById(cid).style.display == "none") ? "expand" : "contract"
        curobj.src = (document.getElementById(cid).style.display == "none") ? expandsymbol : contractsymbol
    }
}

//function to show and hide info on default claims wizard page
function expandcontent_claim(curobj, cid, Show, Hide) {
    document.getElementById(cid).style.display = (document.getElementById(cid).style.display != "none") ? "none" : ""
    curobj.alt = (document.getElementById(cid).style.display == "none") ? "expand" : "contract"
    curobj.src = (document.getElementById(cid).style.display == "none") ? expandsymbol : contractsymbol

    if (curobj.alt == "contract") {
        document.getElementById(Show).style.display = "none";
        document.getElementById(Hide).style.display = "";
    }
    else {
        document.getElementById(Show).style.display = "";
        document.getElementById(Hide).style.display = "none";
    }
}

function expandMasterSearch(curobj, cid) {
    document.getElementById(cid).style.display = (document.getElementById(cid).style.display != "none") ? "none" : ""
    curobj.alt = (document.getElementById(cid).style.display == "none") ? "expand" : "contract"
    curobj.src = (document.getElementById(cid).style.display == "none") ? expandsymbol : contractsymbol
}


function revivecontent() {
    selectedItem = getselectedItem()
    selectedComponents = selectedItem.split("|")
    for (i = 0; i < selectedComponents.length - 1; i++)
        document.getElementById(selectedComponents[i]).style.display = "none"
}

function revivestatus() {
    var inc = 0
    while (statecollect[inc]) {
        if (ccollect[inc].style.display == "none") {
            statecollect[inc].src = expandsymbol
            statecollect[inc].alt = "expand"
        }
        else {
            statecollect[inc].src = contractsymbol
            statecollect[inc].alt = "contract"
        }
        inc++
    }
}

function get_cookie(Name) {
    var search = Name + "="
    var returnvalue = "";
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search)
        if (offset != -1) {
            offset += search.length
            end = document.cookie.indexOf(";", offset);
            if (end == -1) end = document.cookie.length;
            returnvalue = unescape(document.cookie.substring(offset, end))
        }
    }
    return returnvalue;
}

function getselectedItem() {
    if (get_cookie(window.location.pathname) != "") {
        selectedItem = get_cookie(window.location.pathname)
        return selectedItem
    } else
        return ""
}

function saveswitchstate() {
    var inc = 0, selectedItem = "";
    if (typeof (ccollect) != 'undefined') {
        if (ccollect != undefined) {
            while (ccollect[inc]) {
                if (ccollect[inc].style.display == "none")
                    selectedItem += ccollect[inc].id + "|"
                inc++
            }
        }
    }
    if (get_cookie(window.location.pathname) != selectedItem) { //only update cookie if current states differ from cookie's
        var expireDate = new Date()
        expireDate.setDate(expireDate.getDate() + parseInt(memoryduration))
        document.cookie = window.location.pathname + "=" + selectedItem + ";path=/;expires=" + expireDate.toGMTString()
    }
}

/**
 * HTML-Encode the supplied input
 * 
 * Parameters:
 *
 * (String)  source    The text to be encoded.
 * 
 * (boolean) display   The output is intended for display.
 *
 *                     If true:
 *                     * Tabs will be expanded to the number of spaces 
 *                       indicated by the 'tabs' argument.
 *                     * Line breaks will be converted to <br />.
 *
 *                     If false:
 *                     * Tabs and linebreaks get turned into &#____;
 *                       entities just like all other control characters.
 *
 * (integer) tabs      The number of spaces to expand tabs to.  (Ignored 
 *                     when the 'display' parameter evaluates to false.)
 *
 */
function htmlEncode(source, display, tabs) {
    function special(source) {
        var result = '';
        for (var i = 0; i < source.length; i++) {
            var c = source.charAt(i);
            if (c < ' ' || c > '~') {
                c = '&#' + c.charCodeAt() + ';';
            }
            result += c;
        }
        return result;
    }
    function format(source) {
        // Use only integer part of tabs, and default to 4
        tabs = (tabs >= 0) ? Math.floor(tabs) : 4;
        // split along line breaks
        var lines = source.split(/\r\n|\r|\n/);
        // expand tabs
        for (var i = 0; i < lines.length; i++) {
            var line = lines[i];
            var newLine = '';
            for (var p = 0; p < line.length; p++) {
                var c = line.charAt(p);
                if (c === '\t') {
                    var spaces = tabs - (newLine.length % tabs);
                    for (var s = 0; s < spaces; s++) {
                        newLine += ' ';
                    }
                }
                else {
                    newLine += c;
                }
            }
            // If a line starts or ends with a space, it evaporates in html
            // unless it's an nbsp.
            newLine = newLine.replace(/(^ )|( $)/g, '&nbsp;');
            lines[i] = newLine;
        }
        // re-join lines
        var result = lines.join('<br />');
        // break up contiguous blocks of spaces with non-breaking spaces
        result = result.replace(/  /g, ' &nbsp;');
        // tada!
        return result;
    }
    var result = source;
    // ampersands (&)
    result = result.replace(/\&/g, '&amp;');
    // less-thans (<)
    result = result.replace(/\</g, '&lt;');
    // greater-thans (>)
    result = result.replace(/\>/g, '&gt;');
    if (display) {
        // format for display
        result = format(result);
    }
    else {
        // Replace quotes if it isn't for display,
        // since it's probably going in an html attribute.
        result = result.replace(new RegExp('"', 'g'), '&quot;');
    }
    // special characters
    result = special(result);
    // tada!
    return result;
}

function do_onload() {
    uniqueidn = window.location.pathname + "firsttimeload"
    var alltags = document.all ? document.all : document.getElementsByTagName("*")
    ccollect = getElementbyClass(alltags, "switchcontent")
    statecollect = getElementbyClass(alltags, "showstate")
    if (enablepersist == "on" && get_cookie(window.location.pathname) != "" && ccollect.length > 0)
        revivecontent()
    if (ccollect.length > 0 && statecollect.length > 0)
        revivestatus()
}

if (window.addEventListener)
    window.addEventListener("load", do_onload, false)
else if (window.attachEvent)
    window.attachEvent("onload", do_onload)
else if (document.getElementById)
    window.onload = do_onload

if (enablepersist == "on" && document.getElementById)
    window.onunload = saveswitchstate


function foo() {
    var txtNewWorkflowGate = document.getElementById('{0}');
    var rfvNewWorkflowGate = document.getElementById('{1}');

    if (this.value == '-1') {
        txtNewWorkflowGate.disabled = false;
        ValidatorEnable(rfvNewWorkflowGate, true);
    }
    else {
        txtNewWorkflowGate.disabled = true;
        ValidatorEnable(rfvNewWorkflowGate, false);
    }
}

// Popup function
function Popup(url) {
    var sOption = "toolbar=no,location=no,menubar=yes,resizable=yes,";
    sOption += "scrollbars=yes,width=900,height=600";

    window.open(url, "", sOption);
}