﻿function S4() 
{
   return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}

function guid() 
{
   return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

function NewWindow(href, windowName, width, height, scrollable) {
    var posLeft = (screen.width - width) / 2;
    var posTop = (screen.height - height) / 2;
    var properties = 'height = ' + height + ', width = ' + width + ', top = ' + posTop + ', left = ' + posLeft + ', scrollbars = ' + scrollable + ', resizable';
    var win = window.open(href, windowName, properties);
    win.window.focus();
}

var ImageHelper =
{
    tmpImages: new Array(),

    newImage: function(document, id, url) {
        this.tmpImages[id] = new Image();
        this.tmpImages[id].src = url;
        return this.tmpImages[id];
    },

    changeImage: function(document, id, url) {
        document.getElementById(id).src = url;
    },

    setMenuItemMouseHandlers: function(document, id, normalUrl, hoverUrl) {
        var image = document.getElementById(id);
        if (image) {
            image.onmouseover = function() { ImageHelper.changeImage(document, id, hoverUrl); return true; }
            image.onmouseout = function() { ImageHelper.changeImage(document, id, normalUrl); return true; }
        }
    }
};

CheckHelper.prototype.addRow = function(checkControl, childHelper, dontChangeChecked) {
    this.checkRows.push(checkControl);
    this.childHelpers.push(childHelper);
    if (!dontChangeChecked) checkControl.checked = false;
}

CheckHelper.prototype.updateCheckAllState = function() {
    var checked = true;
    for (var i = 0; i < this.checkRows.length; i++) {
        if (this.checkRows[i].checked == false) {
            checked = false;
            break;
        }
    }
    this.checkAllControl.checked = checked;

    if (this.parentHelper) this.parentHelper.updateCheckAllState();
}

CheckHelper.prototype.setCheckAllState = function(state, update) {
    for (var i = 0; i < this.checkRows.length; i++) {
        this.checkRows[i].checked = state;
        var childHelper = this.childHelpers[i];
        if (childHelper) childHelper.setCheckAllState(state, false);
    }
    if (update) this.updateCheckAllState();
    else if (this.parentHelper) this.parentHelper.updateCheckAllState();
}

CheckHelper.prototype.checkSelectedRows = function() {
    var checked = false;
    for (var i = 0; i < this.checkRows.length; i++) {
        if (this.checkRows[i].checked == true) {
            checked = true;
            break;
        }
    }
    return checked;
}

CheckHelper.prototype.processSelectedRows = function(question, error) {
    result = this.checkSelectedRows();
    if (result) result = confirm(question);
    else if (error) alert(error);
    return result;
}

function CheckHelper(checkAllControl, parentHelper) {
    this.parentHelper = parentHelper;
    this.checkRows = new Array();
    this.childHelpers = new Array();
    this.checkAllControl = checkAllControl;
}

function urlencode(str) {
    if (!str) return "";
    var histogram = {}, tmp_arr = [];
    var ret = str.toString();
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    histogram["'"] = '%27';
    histogram['('] = '%28';
    histogram[')'] = '%29';
    histogram['*'] = '%2A';
    histogram['~'] = '%7E';
    histogram['!'] = '%21';
    histogram['%20'] = '+';

    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret)
    }
    
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%" + m2.toUpperCase();
    });

    return ret;
}

if (!top.__all_frames) {
    top.__all_frames = new Array();
}

function AttachFrameOnRemoveHandler(_fr) {
    top.__all_frames[top.__all_frames.length] = _fr;

    _fr._onremove = function() {
        var list = top.__all_frames;
        if (list) {
            for (var i = 0; i < list.length; i++) {
                var item = list[i];
                if (item == _fr) {
                    item._onremove = null;
                    list.splice(i, 1);
                    delete item;
                    break;
                }
            }
        }
    };
}

$(document).ready(function() {
    var doc = this;
    var win = window;

    function IsTextInput(e) {
        var el = e.srcElement || e.target;
        if (el) {
            if (el.nodeName.toLowerCase() == "input") {
                var t = $(el).attr("type");
                return (!t || t.toLowerCase() == "text");
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    $(doc).keypress(function(e) {
        if (e.keyCode == 13 && !IsTextInput(e)) {
            try {
                _fr = top.__all_frames.length > 0 ? top.__all_frames[top.__all_frames.length - 1] : null;
                if (_fr) {
                    var doc2 = _fr.contentWindow.document;
                    var list = $(".default_submit", doc2);
                    if (list.length > 0) {
                        var _el = list[0];
                        if (_el.nodeName.toLowerCase() == "a") {
                            var _res = true;
                            var _onclick = _el.onclick;
                            if (_onclick) {
                                _res &= _onclick();
                            }
                            if (_res) {
                                var href = _el.href;
                                if (href) {
                                    _fr.contentWindow.document.location = href;
                                }
                            }
                        } else {
                            $(_el).click();
                        }

                        if (e.stopPropagation)
                            e.stopPropagation();
                        else
                            e.cancelBubble = true;

                        if (e.preventDefault)
                            e.preventDefault();
                        else
                            e.returnValue = false;

                        if (e.stopImmediatePropagation) {
                            e.stopImmediatePropagation();
                        }
                    }
                }
            } catch (ex) {
            }
        }
        else if (e.keyCode == 13) return false;
    });

    var _fr = win.frameElement;
    if (_fr) {
        if (!_fr._onremove) {
            top.AttachFrameOnRemoveHandler(_fr);
        }
    }
});

function disablePageComponents(pageType) {
    $("#header a").each(function() {
        disableLink(this);
    });
    $("#footer a").each(function() {
        disableLink(this);
    });
    $(".moduletype8 a").each(function() {
        disableLink(this);
    });
    $(".moduletype2 a").each(function() {
        disableLink(this);
    });
    switch (pageType.toLowerCase()) {
        case "article":
            {
                $("#optionmenu a").each(function() {
                    disableLink(this);
                });
                $("#linearnav a").each(function() {
                    disableLink(this);
                });
                $(".buttonarea a").each(function() {
                    disableLink(this);
                });
                break;
            }
        case "media":
        case "documents":
            {
                $(".cleft a").each(function() {
                    disableLink(this);
                });
                $(".moduletype4 a").each(function() {
                    disableLink(this);
                });
                break;
            }
        case "arts":
        case "dailylife":
        case "places":
        case "literature":
        case "history":
        case "religion":
            {
                $(".alphabet a").each(function() {
                    disableLink(this);
                });
                break;
            }
        case "forresearches":
            {
                $("#moduletype3 a").each(function() {
                    disableLink(this);
                });
                break;
            }
        case "default":
            {
                $("#topicmenu a").each(function() {
                    disableLink(this);
                });
                $(".buttonarea a").each(function() {
                    disableLink(this);
                });
                $(".tabcontent a").each(function() {
                    disableLink(this);
                });
                break;
            }
    }
}

function disableLink(link){
    if ($(link).attr["href"] == "javascript:void(0);") {
        $(link).unbind();         
    }
    $(link).attr("href", "javascript:void(0);");
}
