﻿
function image_swap(element, filename) {
    var img = ((typeof element).toLowerCase() == "object") ? element : document.getElementById(element);
    img.src = get_image_path(img.src) + filename;
}

//calendar function
function ToggleDatePicker(bCheckBox, sPickerId) {
    var oDiv1 = document.getElementById(sPickerId + "-Div1");
    var oDiv2 = document.getElementById(sPickerId + "-Div2");
    if (bCheckBox) {
        oDiv1.style.display = "none";
        oDiv2.style.display = "";
    }
    else {
        oDiv1.style.display = "";
        oDiv2.style.display = "none";
    }
}

function get_image_path(src) {
    return src.substring(0, (src.lastIndexOf("/") + 1));
}

function replaceButtonText(buttonId, text) {
    if (document.getElementById) {
        var button = document.getElementById(buttonId);
        if (button) {
            if (button.childNodes[0]) {
                button.childNodes[0].nodeValue = text;
            }
            else if (button.value) {
                button.value = text;
            }
            else //if (button.innerHTML)
            {
                button.innerHTML = text;
            }
        }
    }
}


var aAllDivs = new Array('rQueryAsAge', 'rMultiLine', 'rRows', 'rCols', 'rMaxLength', 'rCountryCode', 'rMin', 'rMax', 'rShowDropDown', 'rRepeatCols', 'rRepeatDir', 'rWidth', 'rListData', 'rPreMessage', 'rMidMessage', 'rPostMessage', 'rShowStreet', 'rShowCity', 'rShowState', 'rShowStateList', 'rShowPostCode', 'rShowCountry', 'rErrorMessage');


function SetDivState(oDiv, ValidatorState, Visibility, Display) {
    if (oDiv == null) {
        alert('null div');
        return;
    }
    var oValidator = document.getElementById(oDiv.id + 'Val');
    if (oValidator != null)
        ValidatorEnable(oValidator, ValidatorState);
    oValidator = document.getElementById(oDiv.id + 'Val1');
    if (oValidator != null)
        ValidatorEnable(oValidator, ValidatorState);

    oDiv.style.visibility = Visibility;
    oDiv.style.display = Display;

}

function ddlDataTypeChanged() {
    var oSettings = new Array();
    var sVal = document.getElementById(ddlDataTypeID).value;

    if (sVal == 'Text')
        oSettings = new Array(0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0);
    else if (sVal == 'Integer')
        oSettings = new Array(0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1);
    else if (sVal == 'Decimal')
        oSettings = new Array(0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1);
    else if (sVal == 'Currency')
        oSettings = new Array(0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1);
    else if (sVal == 'DateTime')
        oSettings = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0);
    else if (sVal == 'Address')
        oSettings = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0);
    else if (sVal == 'SingleSelectionList')
        oSettings = new Array(0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
    else if (sVal == 'MultipleSelectionList')
        oSettings = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
    else if (sVal == 'HierarchicalList')
        oSettings = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
    else if (sVal == 'IntegerList')
        oSettings = new Array(0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1);
    else if (sVal == 'DecimalList')
        oSettings = new Array(0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1);
    else if (sVal == 'CurrencyList')
        oSettings = new Array(0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1);
    else if (sVal == 'MonthYear')
        oSettings = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0);
    else
        oSettings = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

    if (oSettings.length != aAllDivs.length) {
        alert('length mismatch');
        return false;
    }
    var iMax = oSettings.length;

    for (var i = 0; i < iMax; i++) {
        //check the settings mode and then pass the right stuff to my show/hide function
        if (oSettings[i] == 0)//0=hide
        {
            SetDivState(document.getElementById(dvBoxId + '_' + aAllDivs[i]), false, 'hidden', 'none');
        }
        else if (oSettings[i] == 1)//1=show
        {
            SetDivState(document.getElementById(dvBoxId + '_' + aAllDivs[i]), true, 'visible', 'inline');
        }
    }



    return false; //for the postback

}

function widgetCallbackErrorHandler(sender, eventargs) {
    //do nothing"
}


function UncheckAllNotThis(sBaseId, nItems, oThis) {
    var sId = oThis.id;
    var sCheck;
    var nItem;
    for (nItem = 0; nItem < nItems; nItem++) {
        sCheck = sBaseId + '_' + nItem;
        if (sCheck != sId) {
            var oObject = document.getElementById(sCheck);
            if (oObject != null) {
                oObject.checked = false;
            }
        }
    }
}

function UncheckNoneNotThis(sBaseId, sItems, oThis) {
    var sId = oThis.id;
    var sCheck;
    var aItems;
    var nItem;
    var nItems;
    aItems = sItems.split(';');
    nItems = aItems.length
    for (nItem = 0; nItem < nItems; nItem++) {
        sCheck = sBaseId + '_' + aItems[nItem];
        if (sCheck != sId) {
            var oObject = document.getElementById(sCheck);
            if (oObject != null) {
                oObject.checked = false;
            }
        }
    }
}

function getElementsByAttribute(sTagName, sAttribute, sAttributeValue) {
    var aElements = (document.all) ? document.all : document.getElementsByTagName(sTagName);
    var nLength = aElements.length;
    var aReturn = new Array();
    for (var i = 0; i < nLength; i++) {
        var oElement = aElements[i];
        var oAttribute = oElement.getAttribute(sAttribute);
        if (typeof oAttribute == 'string' && oAttribute.length > 0) {
            if (oAttribute == sAttributeValue) {
                aReturn.push(oElement);
            }
        }
    }
    return aReturn;
}

function replaceSubstring(inputString, fromString, toString) {
    var temp = inputString;
    if (fromString == "") {
        return inputString;
    }
    if (toString.indexOf(fromString) == -1) { 
        while (temp.indexOf(fromString) != -1) {
            var toTheLeft = temp.substring(0, temp.indexOf(fromString));
            var toTheRight = temp.substring(temp.indexOf(fromString) + fromString.length, temp.length);
            temp = toTheLeft + toString + toTheRight;
        }
    } else { 
        var midStrings = new Array("~", "`", "_", "^", "#");
        var midStringLen = 1;
        var midString = "";
        
        while (midString == "") {
            for (var i = 0; i < midStrings.length; i++) {
                var tempMidString = "";
                for (var j = 0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
                if (fromString.indexOf(tempMidString) == -1) {
                    midString = tempMidString;
                    i = midStrings.length + 1;
                }
            }
        } 
        while (temp.indexOf(fromString) != -1) {
            var toTheLeft = temp.substring(0, temp.indexOf(fromString));
            var toTheRight = temp.substring(temp.indexOf(fromString) + fromString.length, temp.length);
            temp = toTheLeft + midString + toTheRight;
        }
        while (temp.indexOf(midString) != -1) {
            var toTheLeft = temp.substring(0, temp.indexOf(midString));
            var toTheRight = temp.substring(temp.indexOf(midString) + midString.length, temp.length);
            temp = toTheLeft + toString + toTheRight;
        }
    }
    return temp;
}

function insertAtCursor(myField, myValue) {
    //IE support  
    if (document.selection) {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
    }
    //Mozilla/Firefox/Netscape 7+ support  
    else if (myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos) + myValue
                 + myField.value.substring(endPos, myField.value.length);
    } else {
        myField.value += myValue;
    }
}

function DefaultPageTitleText(Title, PageTitle) {
    var oTitle = document.getElementById(Title);
    var oPageTitle = document.getElementById(PageTitle);
    if (oTitle != null && oPageTitle != null && oPageTitle.value == "") {
        oPageTitle.value = oTitle.value;
    }
}