﻿var tinyMCE_editors;
function WireupTinyMCE(textbox) {
    if (tinyMCE_editors == null) {
        tinyMCE_editors = [textbox];
    } else {
        tinyMCE_editors = tinyMCE_editors.concat(textbox);
    }
}

var yuiloader = null;
var yuiloader_onLoadComplete_handlers = null;
function LoadYUILoaderIfNeeded(baseUrl) {
    if (yuiloader == null) {
        yuiloader = new YAHOO.util.YUILoader();
        yuiloader.loadOptional = true;
        yuiloader.base = baseUrl;
        yuiloader.onSuccess = function() {
            if (yuiloader_onLoadComplete_handlers != null) {
                for (i = 0; i < yuiloader_onLoadComplete_handlers.length; i++) {
                    yuiloader_onLoadComplete_handlers[i]();
                }
            }
        }
    }
}

function AddYUIRequirement(component) {
    if (yuiloader != null) {
        yuiloader.require(component);
    }
}

function AddYUI_onLoadComplete_handler(func) {
    if (yuiloader_onLoadComplete_handlers == null) {
        yuiloader_onLoadComplete_handlers = [func];
    } else {
        yuiloader_onLoadComplete_handlers = yuiloader_onLoadComplete_handlers.concat(func);
    }
}

function AddAutocompleteContainers(txtid, newcontainerid) {
    var parentDiv = document.createElement('div');
    var containerDiv = document.createElement('div');
    var txt = document.getElementById(txtid);

    if (txt == null)
        return;

    //parentDiv.className = 'autocomplete_parent';
    SetStyle(parentDiv, 'display:inline;');
    containerDiv.setAttribute('id', newcontainerid);
    //containerDiv.className = 'autocomplete_container';
    //SetStyle(containerDiv, 'display:none;');

    txt.parentNode.insertBefore(parentDiv, txt);
    txt.parentNode.removeChild(txt);
    parentDiv.appendChild(txt);
    parentDiv.appendChild(containerDiv);
}

function SubmitForm(form_name) {
    var frm = document.getElementById(form_name);
    if (frm != null) {
        frm.submit();
    }
}

var gmap_markers;
function InsertGmapMarker(latitude, longitude, zoom, tooltip) {
    if (gmap_markers == null) {
        gmap_markers = [latitude, longitude, zoom, tooltip];
    } else {
        gmap_markers = gmap_markers.concat(latitude);
        gmap_markers = gmap_markers.concat(longitude);
        gmap_markers = gmap_markers.concat(zoom);
        gmap_markers = gmap_markers.concat(tooltip);
    }
}

function InitGmaps(containerName, lat, lon, zoom) {
    window.onload = function() {
        if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById(containerName));

            map.addControl(new GSmallMapControl());
            if (gmap_markers != undefined && gmap_markers.length <= 4) {
                map.setCenter(new GLatLng(gmap_markers[0], gmap_markers[1]), gmap_markers[2]);
            } else {
                map.setCenter(new GLatLng(lat, lon), zoom);
            }

            for (i = 0; i < gmap_markers.length; i = i + 4) {
                var point = new GLatLng(gmap_markers[i], gmap_markers[i + 1]);

                function createMarker(latlng, index) {
                    var marker = new GMarker(latlng);
                    marker.value = index;
                    GEvent.addListener(marker, "click", function() {
                        var tooltip = gmap_markers[index + 3];
                        map.openInfoWindowHtml(latlng, tooltip);
                    });
                    return marker;
                }
                var m = createMarker(point, i);
                map.addOverlay(m);
                //                if (gmap_markers.length <= 4) {
                //                    var tooltip = gmap_markers[index + 3];
                //                    map.openInfoWindowHtml(point, tooltip);
                //                }
            }
        }
    }
    window.onunload = GUnload
}

function SetStyle(element, css) {
    if (element != null) {
        element.style.cssText = css;
    }
}

function upload() {
    uploadWin = window.open('upload.aspx', 'Upload', 'toolbar=0,status=0,scrollbars=0,resizable=1, width=390, height=200');
    uploadWin.blur();
    uploadWin.focus();
}

function fileupload(textbox, previewimage) {
    if (previewimage == null) {
        previewimage = '';
    }
    uploadWin = window.open('fileupload.aspx?textbox=' + textbox + '&preview=' + previewimage, 'Upload', 'toolbar=0,status=0,scrollbars=0,resizable=1, width=390, height=200');
    uploadWin.blur();
    uploadWin.focus();
}

/*
* This is the function that actually highlights a text string by
* adding HTML tags before and after all occurrences of the search
* term. You can pass your own tags if you'd like, or if the
* highlightStartTag or highlightEndTag parameters are omitted or
* are empty strings then the default <font> tags will be used.
*/
function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag) {
    // the highlightStartTag and highlightEndTag parameters are optional
    if ((!highlightStartTag) || (!highlightEndTag)) {
        highlightStartTag = "<font class='highlight'>";
        highlightEndTag = "</font>";
    }

    // find all occurences of the search term in the given text,
    // and add some "highlight" tags to them (we're not using a
    // regular expression search, because we want to filter out
    // matches that occur within HTML tags and script blocks, so
    // we have to do a little extra validation)
    var newText = "";
    var i = -1;
    var lcSearchTerm = searchTerm.toLowerCase();
    var lcBodyText = bodyText.toLowerCase();

    while (bodyText.length > 0) {
        i = lcBodyText.indexOf(lcSearchTerm, i + 1);
        if (i < 0) {
            newText += bodyText;
            bodyText = "";
        } else {
            // skip anything inside an HTML tag
            if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
                // skip anything inside a <script> block
                if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
                    newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
                    bodyText = bodyText.substr(i + searchTerm.length);
                    lcBodyText = bodyText.toLowerCase();
                    i = -1;
                }
            }
        }
    }

    return newText;
}


/*
* This is sort of a wrapper function to the doHighlight function.
* It takes the searchText that you pass, optionally splits it into
* separate words, and transforms the text on the current web page.
* Only the "searchText" parameter is required; all other parameters
* are optional and can be omitted.
*/
function highlightSearchTerms(parentElement, searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag) {
    // if the treatAsPhrase parameter is true, then we should search for 
    // the entire phrase that was entered; otherwise, we will split the
    // search string so that each word is searched for and highlighted
    // individually
    if (treatAsPhrase) {
        searchArray = [searchText];
    } else {
        searchArray = searchText.split(" ");
    }

    if (parentElement == null || parentElement == '') {
        parentElement = document.body;
    } else {
        parentElement = document.getElementById(parentElement);
        if (parentElement == null) {
            return false;
        }
    }

    var bodyText = parentElement.innerHTML;
    for (var i = 0; i < searchArray.length; i++) {
        bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
    }

    parentElement.innerHTML = bodyText;
    return true;
}
