﻿/*
* =========================================================================
* ========= Copyright (C) 2011, Randy Suvada, all rights reserved =========
* =========================================================================
*/

/* This file is a RGEN core file. Only edit in the RGEN project and copy out to using sites. */

var editableDivFound = false;

function pageWidth() { return window.innerWidth != null ? window.innerWidth : document.documentElement != null ? document.documentElement.clientWidth : null; }
function pageHeight() { return window.innerHeight != null ? window.innerHeight : document.documentElement != null ? document.documentElement.clientHeight : null; }

function BackupPageInHistory() { history.back(); }

function ClearValue(obj, cssClass) {
    if (obj) {
        obj.value = "";
        obj.className = cssClass;
    }
}

function CallBack(obj) {
    __doPostBack('__Page', obj);
}

function NewWindow(obj) {
    window.open(obj.toString());
}

function SizeChange(spanObj) {
    var otherSizeSpanObj;
    if (spanObj.id == "large_size") {
        document.getElementsByTagName('body')[0].style.fontSize = "96%";
        spanObj.className = "size_button";
        otherSizeSpanObj = document.getElementById('default_size');
    }
    else {
        document.getElementsByTagName('body')[0].style.fontSize = "76%";
        spanObj.className = "size_button";
        otherSizeSpanObj = document.getElementById('large_size');
    }

    if (otherSizeSpanObj)
        otherSizeSpanObj.className = "size_button content_clickable";
}

function EditData() {
    editableDivFound = false;
    SetEditableObj(document.getElementById('mainContentDiv'));

    if (editableDivFound) {
        var editDiv = document.getElementById('edit');
        editDiv.innerHTML = "Cancel";
        var cancelFunc = 'CancelEditData();';
        editDiv.onclick = new Function(cancelFunc);
    }
}

function SetEditableObj(parObj) {
    for (var idx = 0; idx < parObj.childNodes.length; idx++) {
        if (parObj.childNodes[idx].id && parObj.childNodes[idx].id.indexOf(divNumPrefix_static) != -1) {
            editableDivFound = true;

            var editableObj = parObj.childNodes[idx];

            /* If the element is empty, put in the class name to help identify what goes there. */
            if (editableObj.innerHTML.length == 0) {
                editableObj.innerHTML = editableObj.className;
            }

            if (editableObj.childNodes.length > 0) {
                editableObj.className = editableObj.className + " topLayerForEditSelection";
                editableObj.style.border = '3px solid Red';
            }
            else {
                editableObj.className = editableObj.className + " topLayerForEditSel_NoChildElem";
            }

            /* Turn off links since they confuse the picking of the element to edit. */
            var inputObjs = editableObj.getElementsByTagName('a');
            for (var aIdx = 0; aIdx < inputObjs.length; aIdx++) {
                var aParObj = inputObjs[aIdx].parentNode;

                spanObj = document.createElement("span");
                spanObj.innerHTML = inputObjs[aIdx].innerHTML;
                spanObj.className = inputObjs[aIdx].className;
                aParObj.appendChild(spanObj);

                inputObjs[aIdx].innerHTML = '';
            }

            if (editableObj.getElementsByTagName('img').length > 0) {
                var width = editableObj.offsetWidth;
                var height = editableObj.offsetHeight;

                if (!width) {
                    childDivObj = editableObj.getElementsByTagName('div')[0];
                    if (childDivObj) {
                        width = childDivObj.offsetWidth;
                        height = childDivObj.offsetHeight;
                    }
                }

                if (width) {
                    var sizeText = document.createElement("span");
                    sizeText.setAttribute("style",
                        "position: absolute;  left: 5px; top: 5px; font-size: larger; font-weight: bold;");
                    sizeText.innerHTML = width + " X " + height + " (W x H)";

                    editableObj.appendChild(sizeText);
                }
            }

            var func = 'EditCallback("' + editableObj.id + '")';
            editableObj.onclick = new Function(func);
        }
        else {
            SetEditableObj(parObj.childNodes[idx]);
        }
    }

    return false;
}

function CancelEditData() {
    window.location = window.location;
    /*window.location.href = window.location.href;*/
}

// use this if the visitor must be signed on
function EditCallback_wSO(id) {
    if (userSignonName == '') {
        var gotoPageObj = document.getElementById('gotoPageOnLogin');
        if (gotoPageObj)
            gotoPageObj.value = id;
        SignOn();
    }
    else {
        EditCallback(id);
    }
}

function EditCallback(id) {
    /* Pass back the setting for the window size since any possible change is made wo/a callback to the server */
    defaultSizeObj = document.getElementById('default_size');
    if (defaultSizeObj && defaultSizeObj.className.indexOf('content_clickable') != -1)
        id = id + largeSizeStr_static;

    __doPostBack('__Page', id);
}

function ShowDiv(divId) {
    var divObj = document.getElementById(divId);
    if (divObj) {
        var fadeDivObj = document.getElementById("fadeForPopupDiv");
        if (fadeDivObj)
            fadeDivObj.style.visibility = "visible";

        divObj.style.visibility = "visible";
    }

    return false;
}

function HideDiv(divId) {
    var divObj = document.getElementById(divId);
    if (divObj) {
        divObj.style.visibility = "hidden";
    }

    var fadeDivObj = document.getElementById("fadeForPopupDiv");
    if (fadeDivObj)
        fadeDivObj.style.visibility = "hidden";

    return false;
}

function SignOn() {
    ShowDiv('signonDiv');

    signonKeyTBObj.focus();
    signonKeyTBObj.value = "";
    signonPasswordTBObj.value = "";
}

function SignUp(isNewSignup) {
    HideDiv('signonDiv');
    ShowDiv('signupDiv');

    if (isNewSignup) {
        signupUsernameTBObj.focus();
        document.getElementById("signup_title").innerHTML = "Signup for Site";
        document.getElementById("signup_footnote").innerHTML = "- All fields are required.";
        signupBtnObj.style.zIndex = "2";
        signupChangeBtnObj.style.zIndex = "1";

        signupUsernameTBObj.value = "";
        signupEmailTBObj.value = "";
        signupPasswordTBObj.value = "";
        signupPassword2TBObj.value = "";
        signupFirstNameTBObj.value = "";
        signupLastNameTBObj.value = "";
    }
    else {
        document.getElementById("signup_title").innerHTML = "Change Your Information";
        document.getElementById("signup_footnote").innerHTML = "- Enter only the fields you want to change.\n<span class='smaller_font'>(enter 'Password' & 'Reenter Pass' to change the password)</span>";
        signupBtnObj.style.zIndex = "1";
        signupChangeBtnObj.style.zIndex = "2";
    }

    return false;
}

function ForgotPassword() {
    HideDiv('signonDiv');
    ShowDiv('forgotPasswordDiv');
    forgotPasswordBtnObj.focus();

    return false;
}

function ShowUserInputDiv(contactDivNum) {
    ShowDiv('userInputDiv');

    userInputContactDivNumObj.value = contactDivNum;
    userInputTBObj.focus();
    userInputTBObj.value = "";
    userInputEmailTBObj.value = "";

    return false;
}

function ShowTextPopupDiv_withVarNames(title_varName, content_varName) {
    ShowTextPopupDiv(window[title_varName], window[content_varName])
}

function ShowTextPopupDiv(titleText, contentText) {
    ShowDiv('textPopupDiv');

    document.getElementById('textPopup_title').innerHTML = titleText;
    document.getElementById('textPopup_content').innerHTML = contentText;

    return false;
}

function ShowPopup(id, desc) {
    var popupObj = document.getElementById(id);
    if (popupObj) {
        popupObj.style.visibility = "visible";
        var spanChild = popupObj.childNodes[0];
        spanChild.innerHTML = desc;
    }
}

function HidePopup(id) {
    document.getElementById(id).style.visibility = "hidden";
}

/* endDate_month- 1 to 12 for month; endDate_dayOfMonth- 1 to 31 for day of month; endDate_24Hour- 0 to 23; */
function StartCountdown(countdown_varName, endDate_month, endDate_dayOfMonth, endDate_24Hour, dayElemId, hmsElemId) {
    var countdownDate = new Date();
    countdownDate.setMonth(--endDate_month, endDate_dayOfMonth);
    countdownDate.setHours(endDate_24Hour, 0, 0, 0);

    var now = new Date();
    var time_diff = countdownDate.getTime() - now.getTime();

    DoCountdown(countdown_varName, time_diff, dayElemId, hmsElemId);
}

function DoCountdown(countdown_varName, time_diff, dayElemId, hmsElemId) {
    if (time_diff < 0) {
        return;
    }

    var numDays = Math.floor(time_diff / (1000 * 60 * 60 * 24));
    var remDate = new Date(time_diff - (numDays * (1000 * 60 * 60 * 24)));

    document.getElementById(dayElemId).innerHTML = numDays;
    document.getElementById(hmsElemId).innerHTML = padNumber(remDate.getUTCHours(), 2) + ":" +
                                                   padNumber(remDate.getUTCMinutes(), 2) + ":" +
                                                   padNumber(remDate.getUTCSeconds(), 2);

    time_diff -= 1000;
    var func = 'DoCountdown("' + countdown_varName + '",' + time_diff + ',"' + dayElemId + '","' + hmsElemId + '")';
    window[countdown_varName] = setTimeout(func, 1000);
}

function padNumber(value, numDigits) {
    var strVal = value.toString();
    while (strVal.length < numDigits) {
        strVal = '0' + strVal;
    }
    return strVal;
}

function StartSlideShow(divControlId, popupDivId, timeoutVal, timer_varName, onFlag_varName,
                        counter_varName, fadeCounter_varName, ary_varName, centerFlag) {
    if (window[onFlag_varName])
        return;

    var parDivObj = FindFirstObjWithId(document.getElementById('mainContentDiv'), divControlId);
    if (parDivObj == false)
        return;

    if (centerFlag) {
        var offsetLeft = (GetBrowserWidth() - parDivObj.offsetWidth) / 2;
        parDivObj.style.left = ((offsetLeft > 0) ? offsetLeft : 0).toString() + "px";
    }

    var divObjs = parDivObj.getElementsByTagName('div');
    var imgObjs = parDivObj.getElementsByTagName('img');
    var pageNumSpanObjs = parDivObj.getElementsByTagName('span');

    var pageNumObj = FindFirstObjWithId(parDivObj, slideShowPopupPageNumSuffix_static);
    var pageNumObjId;
    if (pageNumObj)
        pageNumObjId = pageNumObj.id;

    if (imgObjs.length > 0 && divObjs.length > 0) {
        window[onFlag_varName] = 1;
        ShowSlideShow(divObjs[0].id, imgObjs[0].id, popupDivId, pageNumObjId, timeoutVal,
                      timer_varName, counter_varName, fadeCounter_varName, ary_varName);
    }
}

function ShowSlideShow(divObjId, imgObjId, popupDivId, pageNumObjId, timeoutVal,
                       timer_varName, counter_varName, fadeCounter_varName, ary_varName) {
    var divObj = document.getElementById(divObjId);
    var imgObj = document.getElementById(imgObjId);
    var popupDivObj = document.getElementById(popupDivId);
    var pageNumObj = document.getElementById(pageNumObjId);

    var oldImgObj = imgObj.cloneNode(false);
    oldImgObj.id = imgObjId + "_temp";
    divObj.appendChild(oldImgObj);

    if (window[ary_varName].length == window[counter_varName])
        window[counter_varName] = 0;

    if (pageNumObj) {
        pageNumObj.innerHTML = (window[counter_varName] + 1).toString() + " of " + window[ary_varName].length.toString();
    }

    oldImgObj.style.opacity = 0.9;
    oldImgObj.style.filter = 'alpha(opacity=90)';

    imgObj.style.opacity = 0.1;
    imgObj.style.filter = 'alpha(opacity=10)';

    imgObj.src = window[ary_varName][window[counter_varName]][0];
    if (popupDivObj)
        popupDivObj.innerHTML = window[ary_varName][window[counter_varName]][1];

    imgObj.style.top = window[ary_varName][window[counter_varName]][2];
    imgObj.style.left = window[ary_varName][window[counter_varName]][3];
    imgObj.style.width = (parseInt(window[ary_varName][window[counter_varName]][4]) *
                          parseInt(window[ary_varName][window[counter_varName]][5]) / 100).toString() + 'px';
    imgObj.style.height = (parseInt(window[ary_varName][window[counter_varName]][4]) *
                           parseInt(window[ary_varName][window[counter_varName]][6]) / 100).toString() + 'px';

    window[counter_varName] += 1;

    var func = 'ShowSlideShow("' + divObjId + '","' + imgObjId + '","' + popupDivId + '","' +
                                   pageNumObjId + '","' + timeoutVal + '","' +
                                   timer_varName + '","' + counter_varName + '","' +
                                   fadeCounter_varName + '","' + ary_varName + '")';

    window[timer_varName] = setTimeout(func, timeoutVal * 1000);

    window[fadeCounter_varName] = 0;
    FadeSlideShow(divObjId, oldImgObj.id, imgObjId, fadeCounter_varName);
}

function FadeSlideShow(divObjId, oldImgId, newImgId, fadeCounter_varName) {
    var oldImgObj = document.getElementById(oldImgId);
    var newImgObj = document.getElementById(newImgId);

    window[fadeCounter_varName] += 10;
    newImgObj.style.opacity = (window[fadeCounter_varName] / 100);
    newImgObj.style.filter = 'alpha(opacity=' + window[fadeCounter_varName].toString() + ')';

    if (window[fadeCounter_varName] >= 100) {
        var divObj = document.getElementById(divObjId);
        divObj.removeChild(oldImgObj);

        window[fadeCounter_varName] = 0;

        return;
    }

    oldImgObj.style.opacity = ((100 - window[fadeCounter_varName]) / 100);
    oldImgObj.style.filter = 'alpha(opacity=' + (100 - window[fadeCounter_varName]).toString() + ')';

    var fadeFunc = 'FadeSlideShow("' + divObjId + '","' + oldImgId + '","' + newImgId + '","' + fadeCounter_varName + '")';

    setTimeout(fadeFunc, 100);
}

function DecrementSlideShow(divControlId, popupDivId, timeoutVal, timer_varName, onFlag_varName,
                            counter_varName, fadeCounter_varName, ary_varName, centerFlag) {
    if (window[onFlag_varName]) {
        clearTimeout(window[timer_varName]);
        window[onFlag_varName] = 0;
    }

    if (window[counter_varName] == 1)
        window[counter_varName] = window[ary_varName].length - 1;
    else
        window[counter_varName] -= 2;

    StartSlideShow(divControlId, popupDivId, timeoutVal, timer_varName, onFlag_varName,
                   counter_varName, fadeCounter_varName, ary_varName, centerFlag);

    return false;
}

function IncrementSlideShow(divControlId, popupDivId, timeoutVal, timer_varName, onFlag_varName,
                            counter_varName, fadeCounter_varName, ary_varName, centerFlag) {
    if (window[onFlag_varName]) {
        clearTimeout(window[timer_varName]);
        window[onFlag_varName] = 0;
    }

    StartSlideShow(divControlId, popupDivId, timeoutVal, timer_varName, onFlag_varName,
                   counter_varName, fadeCounter_varName, ary_varName, centerFlag);

    return false;
}

function StopSlideShow(timer_varName, onFlag_varName) {
    if (window[onFlag_varName]) {
        clearTimeout(window[timer_varName]);
        window[onFlag_varName] = 0;
    }

    return false;
}

function FindFirstObjWithId(control, idToSearch) {
    if (control.id && control.id.indexOf(idToSearch) != -1) {
        return control;
    }

    for (var idx = 0; idx < control.childNodes.length; idx++) {
        var foundObj = FindFirstObjWithId(control.childNodes[idx], idToSearch);
        if (foundObj)
            return foundObj;
    }
}

function GetMousePos(e, mousePos) {
    if (!e) var e = window.event;
    if (e.pageX || e.pageY) {
        mousePos.x = e.pageX;
        mousePos.y = e.pageY;
    }
    else if (e.clientX || e.clientY) {
        mousePos.x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
        mousePos.y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    }
}

var Shadows = {};
/* from "Javascript The Definitive Guide", David Flanagan; O'Reilly.  pg. 360 */
Shadows.add = function (element, shadows) {
    if (typeof element == "string")
        element = document.getElementById(element);

    shadows = shadows.replace(/^\s+/, "").replace(/\s+$/, "");
    var args = shadows.split(/\s+/);

    var textnode = element.firstChild;
    element.style.position = "relative";

    var numshadows = args.length / 3;
    for (var idx = 0; idx < numshadows; idx++) {
        var shadowX = args[idx * 3];
        var shadowY = args[idx * 3 + 1];
        var shadowColor = args[idx * 3 + 2];

        var shadow = document.createElement("span");
        shadow.setAttribute("style", "position: absolute; " +
                            "left: " + shadowX + "; " +
                            "top: " + shadowY + "; " +
                            "color: " + shadowColor + ";");

        shadow.appendChild(textnode.cloneNode(false));
        element.appendChild(shadow);
    }

    var text = document.createElement("span");
    text.setAttribute("style", "position: relative;");
    text.appendChild(textnode);
    element.appendChild(text);
}

Shadows.addAll = function (root, tagname) {
    if (!root) root = document;
    if (!tagname) tagname = '*';
    var elements = root.getElementsByTagName(tagname);
    for (var idx = 0; idx < elements.length; idx++) {
        var shadow = elements[idx].getAttribute("shadow");
        if (shadow) Shadows.add(elements[idx], shadow);
    }
}

function CountChar(currCount, maxChars, countSpanName) {
    var theCounterId = document.getElementById(countSpanName);
    try {
        remChars = maxChars - currCount;
        if (remChars >= 0) {
            var txt = "chars left- " + remChars;
            theCounterId.innerHTML = "<font color=black>" + txt + "</font>";
        }
        else
            theCounterId.innerHTML = "<font color=red>char limit reached</font>";
    }
    catch (e) {
        //confirm("e= " + e)
    }
}

function ZoomForSingleMapOnPage(mapId, zoomInId, zoomOutId, zoom) {
    Zoom(mapId, zoomInId, zoomOutId, zoom, 'zoomSetting_static');
}

function Zoom(mapId, zoomInId, zoomOutId, zoom, zoomSetting_varName) {
    var mapObj = document.getElementById(mapId);
    if (mapObj) {
        var srcVal = mapObj.src;
        /*http://maps.g...45671&zoom=12&size=400x400&markers=color:orange|label:*|42.34563,-88.0045671&sensor=false*/
        var zoomStr = "&zoom=";
        var zoomLoc = srcVal.indexOf(zoomStr);
        if (zoomLoc != -1) {
            if (zoom == 1) {
                if (window[zoomSetting_varName] == zoomSetting_High_static)
                    return;

                document.getElementById(zoomOutId).className = "content_clickable";
                window[zoomSetting_varName]++;
                if (window[zoomSetting_varName] == zoomSetting_High_static) {
                    document.getElementById(zoomInId).className = "";
                }
            }
            else {
                if (window[zoomSetting_varName] == zoomSetting_Low_static)
                    return;

                document.getElementById(zoomInId).className = "content_clickable";
                window[zoomSetting_varName]--;
                if (window[zoomSetting_varName] == zoomSetting_Low_static) {
                    document.getElementById(zoomOutId).className = "";
                }
            }

            zoomLoc += zoomStr.length;
            var newSrcStr = srcVal.substring(0, zoomLoc) + padNumber(window[zoomSetting_varName], 2) +
                            srcVal.substring(zoomLoc + 2, srcVal.length);

            mapObj.src = newSrcStr;
        }
    }
}

function PayPalWindow() {
    javascript: theForm.__VIEWSTATE.value = '';
    theForm.encoding = 'application/x-www-form-urlencoded';
    theForm.action = 'https://www.paypal.com/cgi-bin/webscr';
    theForm.submit();
}

function GetBrowserWidth() {
    var x = 0;
    if (self.innerHeight) {
        x = self.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientWidth) {
        x = document.documentElement.clientWidth;
    }
    else if (document.body) {
        x = document.body.clientWidth;
    }
    return x;
}

function GetBrowserHeight() {
    var y = 0;
    if (self.innerHeight) {
        y = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
        y = document.documentElement.clientHeight;
    }
    else if (document.body) {
        y = document.body.clientHeight;
    }
    return y;
}

var AsyncCommObj = {
    /*
    *  'srvMethod'       - pass the method name to call (this must match one-to-one with what the server expects)
    *  'srvInputDataAry' - pass an array of parameters the server method expects, null or '' if no parms required
    *  'reply_funcName'  - pass the name of the function to call on successful reply; this method must take in the reply value
    */
    GetData: function (srvMethod, srvInputDataAry, reply_funcName) {
        var XMLHttpRequestObj = false;
        if (window.XMLHttpRequest) {
            var XMLHttpRequestObj = new XMLHttpRequest();
            //if (isXmlFlag && XMLHttpRequestObj.overrideMimeType)
            //XMLHttpRequestObj.overrideMimeType("text/xml");
        }

        if (XMLHttpRequestObj) {
            var srvData = '';
            for (var idx = 0; idx < srvInputDataAry.length; idx++) {
                srvData += srvInputDataAry[idx] + fieldSep_static;
            }

            XMLHttpRequestObj.open("GET", '' + '?' +
                                          asyncCommSrvMethod_static + '=' + srvMethod + '&' +
                                          asyncCommParms_static + '=' + srvData, true);

            XMLHttpRequestObj.onreadystatechange = function () {
                if (XMLHttpRequestObj.readyState == 4 &&
                    XMLHttpRequestObj.status == 200) {
                    var retVal = XMLHttpRequestObj.responseText;

                    window[reply_funcName](retVal);
                }
            }

            XMLHttpRequestObj.send(null);
        }
    }
}

