﻿var eControlType = { TextBox:0, RadioButtonList:1, CheckBox:2, DropDownList:3 };
function hideCalendar(oCalendar) {
    oCalendar.hide();
    oCalendar.get_element().blur();
}

function getWindowHeight() {
    var myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myHeight = document.body.clientHeight;
    }
    return myHeight;
}

function getWindowWidth() {
    var myWidth = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
    }
    return myWidth;
}

function SetDetailRec(Fld, HndFldName, DtlId, FieldNum,
    varControlType, ConvertToNumber) {

    var FldValue;
    switch (varControlType) {
        case eControlType.TextBox:
        case eControlType.DropDownList:
            FldValue = Fld.value;
            break;
        case eControlType.RadioButtonList:
            for (var i = 0; i < Fld.cells.length; i++) {
                if (Fld.cells[i].children[0].checked) {
                    FldValue = Fld.cells[i].children[0].value;
                }
            }
            break;
        case eControlType.CheckBox:
            FldValue = Fld.checked;
            break;
        default:
            alert("Invalid control type when storing field value");
            return;
    }

    if (ConvertToNumber)
        FldValue = convertNumber(FldValue);

    var DtlFld = $get(HndFldName);
    var xmlDoc;

    var UserSerializer = false;
    if (window.DOMParser) {
        parser = new DOMParser();
        xmlDoc = parser.parseFromString(DtlFld.value, "text/xml");
        UserSerializer = true;
    }
    else // Internet Explorer
    {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(DtlFld.value);
    }

    var xmlTbl = xmlDoc.childNodes[0];
    if (xmlTbl == null) {
        xmlTbl = xmlDoc.createElement("Table");
        xmlDoc.appendChild(xmlTbl);
    }

    var xmlRec = searchForElement(xmlTbl, "KeyValue", DtlId, xmlDoc, "Record");
    var xmlFld = searchForElement(xmlRec, "FieldName", FieldNum, xmlDoc, "Field");

    if (UserSerializer) {
        xmlFld.textContent = FldValue.toString();
        DtlFld.value = (new XMLSerializer()).serializeToString(xmlDoc);
    }
    else {
        xmlFld.text = FldValue.toString();
        DtlFld.value = xmlDoc.xml;
    }
}
function searchForElement(xmlParentElement, AttrName, AttrValue, xmlDoc, ElementName, AddElement) {
    var xmlChildNodes = xmlParentElement.childNodes;
    if (xmlChildNodes.length != 0) {
        for (var i = 0; i < xmlChildNodes.length; i++) {
            if (xmlChildNodes[i].getAttribute(AttrName) == AttrValue) {
                return xmlChildNodes[i];
            }
        }
    }

    if (AddElement != undefined
        || AddElement == false) return undefined;
    
    var newElement = xmlDoc.createElement(ElementName);
    newElement.setAttribute(AttrName, AttrValue);
    xmlParentElement.appendChild(newElement);

    return newElement;
}
function formatCurrency(num) {
    return formatCurrencyCents(num, true);
}
function formatCurrencyCents(num, includeCents) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
                num.substring(num.length - (4 * i + 3));
    if (includeCents)
        return (((sign) ? '' : '-') + '$' + num + '.' + cents);
    else
        return (((sign) ? '' : '-') + '$' + num);
}
function convertNumber(strVal) {
    if (!strVal)
        return 0;
    var strNum = strVal.toString().replace(/\$|\,/g, "");
    if (isNaN(strNum))
        return 0;
    else
        return strNum * 1;
}
function DisplayDialog(Url, hght, wdth) {
    DisplayDialog(Url, hght, wdth, true);
}

function DisplayDialog(Url, hght, wdth, isModal) {
    try {
        var thissound = eval("document.ClickLink");
        if (thissound != null)
            thissound.Play();
    }
    catch (err) {
    }
    if (isModal) {
        settgs = "status=1; dialogHeight=" + hght + "px; dialogWidth=" + wdth + "px; center:yes";
        var retVal = window.showModalDialog(Url, null, settgs);
    }
    else {
        settgs = "status=yes, height=" + hght + " px, width=" + wdth + " px, resizable=no, top=10 px, left=10 px ";
        var retVal = window.open(Url, "_blank", settgs);
    }
        
    return false;
}

function SetTextBoxRows(FldValue, LineWith) {
    if (LineWith == undefined)
        LineWith = 47;
    var DelimitList = '!%()-[]{}? \n';
    var LineLength = 0;
    var WordLength = 0;
    var RowCnt = 1;

    for (i = 0; i < FldValue.value.length; i++) {
        if (DelimitList.indexOf(FldValue.value[i]) != -1) {
            if (LineLength + WordLength + 1 > LineWith
                    || FldValue.value[i] == '\n') {
                RowCnt++;
                LineLength = 0;
            }

            LineLength = LineLength + WordLength + 1;
            WordLength = 0;
        }
        else {
            WordLength++;
        }
    }

    if (LineLength + WordLength > LineWith)
        RowCnt++;

    FldValue.rows = RowCnt;
}

function ValidateTextAreaLength(Fld, length, FieldName) {
    if (Fld.value.length > length)
        alert('A maximum of ' + length + ' characters are allowed in the ' + FieldName + '.  You have ' + Fld.value.length);
}
