var getElt = function(id) { return document.getElementById(id); }

function str_RepeatedlyReplace(str, sregxToMatch, sReplacement) {
    var curr = str;
    var afterReplacement;

    while(1) {
        afterReplacement = curr.replace(sregxToMatch, sReplacement);
        
        if (curr === afterReplacement)
        {
            break;
        }
        
        curr = afterReplacement;
    }
    
    return curr; 
}

function resizeTextArea(textArea, minimumRows, resizeContainingIFrame) {
    var realMinimumRows = Math.max(1, minimumRows);
    while ((textArea.rows > realMinimumRows) && (textArea.scrollHeight <= textArea.offsetHeight)) {
        textArea.rows--;
    }
    while (textArea.scrollHeight > textArea.offsetHeight) {
        textArea.rows++;
    }

    if (resizeContainingIFrame == "true") {
        if (window.parent.IFrameResize) {
            window.parent.IFrameResize(false);
        }
        
    }
}
// START: textBoxHint
function textBoxHint_onFocus(ctrl, sHintText) {
    if (ctrl.value == sHintText) {
        ctrl.value = "";
        ctrl.style.color = "#000";
    }
}
function textBoxHint_onBlur(ctrl, sHintText) {
    if (ctrl.value == "" || ctrl.value == sHintText) {
        ctrl.value = sHintText;
        ctrl.style.color = "#aaa";
    }
}
// END: textBoxHint
function limitTextAreaCharacters(textArea, maxCharacters) {
    if (textArea.value.length > maxCharacters) {
        textArea.value = textArea.value.substring(0, maxCharacters);
        
        //Try to beep (works in firefox)
        try
        {
        java.awt.Toolkit.getDefaultToolkit().beep();
        }
        catch (Error)
        {}
    }
}
function browserIsWebKit() {
    if (navigator.userAgent.indexOf("AppleWebKit") == -1) {
        return false;
    }
    else {
        return true;
    }
}
var helpPopUpWindow;
function openHelpPopUp(url) {
    if (helpPopUpWindow)
        helpPopUpWindow.close();
    helpPopUpWindow = window.open(url, "jsHelpPopUp", "width=355,height=500,resizable=yes,scrollable=yes,location=no");
}
function addOnLoadHandler(handler) {
    var oldHandler = window.onload;
    if (oldHandler) {
        window.onload = function() {
            oldHandler();
            handler();
        }
    }
    else {
        
        window.onload = handler;
    }
}

function tryToResizeParentsIFrame() {
    if (window.parent.IFrameResize) {
        window.parent.IFrameResize(false);
    }
}
function tryToResizeParentsIFrame_withScrollToTabs() {
    if (window.parent.IFrameResize) {
        window.parent.IFrameResize(true);
    }
}

function wipeElValueIfEqual(elId, sTextToCompareTo) {
    var el = document.getElementById(elId);
    if (el) {
        if (el.value == sTextToCompareTo) {
            el.value = "";
        }
    }
}

// donate.aspx

function Page_Donate_ShowHideUI() {
    var fDisplayMe = true;
    
    var chkDontDisplayMe = getElt(ctrlId_chkDontDisplayMe);
    if (chkDontDisplayMe && chkDontDisplayMe.checked) {
        fDisplayMe = false;
    }
    
    var fnSetEnabled = function(id, value) {
        getElt(id).disabled = !value;
    }
    
    var fAnonymous = getElt(ctrlId_chkAnonymous).checked;
    
    fnSetEnabled(ctrlId_txtDisplayName, fDisplayMe && !fAnonymous);
    fnSetEnabled(ctrlId_txtComment, fDisplayMe);
    fnSetEnabled(ctrlId_chkHideAmount, fDisplayMe);
    fnSetEnabled(ctrlId_chkAnonymous, fDisplayMe);

    if (fAnonymous) {
        getElt(ctrlId_txtDisplayName).value = "Anonymous";
    }
    if (!fDisplayMe) {
        getElt(ctrlId_txtDisplayName).value = "N/A";
    }
}

// end: donate.aspx

// new_fundraising_page.aspx

function Page_NewFP_InMemory_UpdateUI() {
    var ctrl_txtCommon_PageTitle = document.getElementById(ctrlId_txtCommon_PageTitle);
    var ctrl_txtCommon_VirtualUrl_Url = document.getElementById(ctrlId_txtCommon_VirtualUrl_Url);
    var ctrl_txtInMemory_DeceasedName = document.getElementById(ctrlId_txtInMemory_DeceasedName);
    var ctrl_chkInMemory_UseSuggestedDetails = document.getElementById(ctrlId_chkInMemory_UseSuggestedDetails);
    
    if (ctrl_chkInMemory_UseSuggestedDetails.checked) {
        ctrl_txtCommon_PageTitle.disabled = true;
        ctrl_txtCommon_VirtualUrl_Url.disabled = true;

        if (ctrl_txtInMemory_DeceasedName.value === "") {
            ctrl_txtCommon_PageTitle.value = "";
            ctrl_txtCommon_VirtualUrl_Url.value = "";
        }
        else {
            //User has typed something in the deceased name box
            ctrl_txtCommon_PageTitle.value = Page_NewFP_PageTitleTemplate.replace("%", ctrl_txtInMemory_DeceasedName.value);
            var sDeceasedNameNoWhiteSpace = str_RepeatedlyReplace(ctrl_txtInMemory_DeceasedName.value, ' ', '');
            ctrl_txtCommon_VirtualUrl_Url.value = Page_NewFP_PageUrlTemplate.replace("%", sDeceasedNameNoWhiteSpace);
        }
    }
    else {
        // Use suggested details is not checked

        ctrl_txtCommon_PageTitle.disabled = false;
        ctrl_txtCommon_VirtualUrl_Url.disabled = false;
    }
}

// end: new_fundraising_page.aspx

// profile.aspx

var FirstIFrameLoad = true;
var userBrowserIsWebKit;
var Control_TabsBox_idIFrame;

function GetIFrame() {
    return document.getElementById(Control_TabsBox_idIFrame);
}

function IFrameResize(scrollToTabs) {
    //alert("IFrameResize");

    if (userBrowserIsWebKit == null) {
        populateUserBrowserIsWebKit();
    }
    try {
        var theif = GetIFrame();
        if (theif == null) {
            return;
        }
        
        if (userBrowserIsWebKit) {
            var ifScrollHeight = function() { return theif.contentWindow.document.body.scrollHeight; }
            //var doHeightAlert = function() { alert("scrollHeight = " + ifScrollHeight() + "\r\n" + "theif.height = " + theif.height.toString()); }

            //doHeightAlert();
            while (ifScrollHeight() <= theif.height) {
                theif.height -= 50;
                //doHeightAlert();
            }
        }

        var scrollHeight = theif.contentWindow.document.body.scrollHeight;

        theif.height = scrollHeight;  //+ 10;
    }
    catch (err) {
        alert(err);
    }

    if (FirstIFrameLoad) {
        FirstIFrameLoad = false;
    }
    else {
        if(scrollToTabs)
            ScrollToIFrame();
    }
}

function ScrollToIFrame() {
    var tabs_box = document.getElementById("control_tabsBox_case");
    tabs_box.scrollIntoView(true);
}


function SelectTab(tab_idx) {
    var tabs = document.getElementById("control_tabsBox_tabs");

    if (tabs) {
        var li_num = 0;
        var child;

        for (var i = 0; i < tabs.childNodes.length; i++) {

            child = tabs.childNodes[i];

            if (child.tagName == "LI") {
                if (tab_idx == li_num) {
                    child.className = "control_tabsBox_tabs_selected";
                    child.setAttribute("class", "control_tabsBox_tabs_selected");
                }
                else {
                    child.className = "";
                    child.setAttribute("class", "");
                }

                li_num++;
            }
        }
    }
}

function populateUserBrowserIsWebKit() {
    userBrowserIsWebKit = browserIsWebKit();
}

// end: profile.aspx

function Tabs_TryToChangeTabCaption(sCurrCaptionPrefix, sNewCaption) {
    if (window.parent) {
        var tabs = window.parent.document.getElementById("control_tabsBox_tabs");

        if (tabs) {
            var li_num = 0;
            var child;

            for (var i = 0; i < tabs.childNodes.length; i++) {

                child = tabs.childNodes[i];

                if (child.tagName == "LI") {
                    for (var ixLiChild = 0; ixLiChild < child.childNodes.length; ixLiChild++) {
                        var liChild = child.childNodes[ixLiChild];

                        if (liChild.tagName == "A") {
                            if (liChild.innerHTML.match("^\\s*" + sCurrCaptionPrefix)) {
                                liChild.innerHTML = sNewCaption;
                            }
                        }
                    }
                    
                    li_num++;
                }
            }
        }
    }
}

// DonorsDisplay
function Control_DonorsDisplay_ScrollToTopOfTable() {
    try {
        var updatePanel = document.getElementById(ctrlId_UpdatePanel_Donors);
        updatePanel.scrollIntoView(true);
    }
    catch (Error) {

    }
}



function AjaxCalendar_GenerateCloseButton(sender, e) {                   
    if ($('.ajax__calendar_close_button').length == 0)
    {
        $(sender._header).before("<div class='ajax__calendar_close_button'></div>");                   
        $('.ajax__calendar_close_button').bind("click", sender, function(e) {
            e.data.hide();
        });
    }
}