String.prototype.trim = function()
{
    return this.replace(/^\s+|\s+$/g, '');
}

String.prototype.replaceAll = function(find, replace) {
    var str = this;
    while (str.indexOf(find) > -1) {

        str = str.replace(find, replace);
    }
    return str;
}

String.prototype.startsWith = function(str) {
    return (this.match("^" + str) == str)
}
String.prototype.endsWith = function(str) {
    return (this.match(str + "$") == str)
}
String.prototype.count = function(match) {
    var count = 0;
    for (var i = 0; i < this.length; i++) {
        if (this.charAt(i) == match)
            count++;
    }

    return count;
}
String.prototype.isNumeric = function() {
    var ValidChars = "0123456789.";
    var IsNumber=true;
    var Char;

    for (i = 0; i < this.length && IsNumber == true; i++) {
    Char = this.charAt(i);
    if (ValidChars.indexOf(Char) == -1) {
        IsNumber = false;
    }
    }
    return IsNumber;
}

Array.prototype.contains = function (element) {
    for (var i = 0; i < this.length; i++)
        if (this[i] == element)
            return true;

    return false;
}
Array.prototype.unique = function() {
    var a = [];
    var l = this.length;
    for (var i = 0; i < l; i++) {
        for (var j = i + 1; j < l; j++) {
            if (this[i] === this[j])
                j = ++i;
        }
        a.push(this[i]);
    }
    return a;
};
function Hashtable() {
    this.table = new Object;
    this.keys = new Array();
    this.values = new Array();
}

Hashtable.prototype.get = function(key) {
    if (this.table.hasOwnProperty(key))
        return this.table[key];
    else
        return null;
}
Hashtable.prototype.put = function(key, value) {
    this.table[key] = value;
    this.keys.push(key);
    this.values.push(value);
}

Hashtable.prototype.remove = function(key) {
    if (this.table.hasOwnProperty(key)) {
        // value of the key
        value = this.table[key];

        //removing key from keys array
        var indx = getIndexOf(this.keys, key);
        if (indx != -1) {
            this.keys.splice(indx, 1);
        }

        //removing value from values array
        indx = getIndexOf(this.values, value);
        if (indx != -1) {
            this.values.splice(indx, 1);
        }

        //removing from the table
        this.table[key] = null;
    }
    else
        return null;
}

function getIndexOf(array, value) {
    for (i = 0; i < array.length; i++) {
        if (array[i] == value) {
            return i;
        }
    }
    return -1;
}

function byId(id)
{
    return document.getElementById(id);
}
function byName(name)
{

    if (isIE()) {
        var tag = '*';
        var nodes = getElementsByName_iefix(tag, name)
        return nodes;
    } else {
        return document.getElementsByName(name);
    }
}

function getElementsByName_iefix(tag, name) {

    var elem = document.getElementsByTagName(tag);
    var arr = new Array();
    for (i = 0,iarr = 0; i < elem.length; i++) {
        att = elem[i].getAttribute("name");
        if (att == name) {
            arr[iarr] = elem[i];
            iarr++;
        }
    }
    return arr;
}

function showHide(id) {
    el = byId(id);
    if (el.style.display == "none") {
        el.style.display = "";
    }
    else {
        el.style.display = "none";
    }
}
function show(id) {
    el = byId(id);
    if (el != null)
        el.style.display = ""
}
function hide(id) {
    el = byId(id);
    if (el != null)
        el.style.display = "none";
}
function isIE() {
    var browser_type = navigator.appName
    if (browser_type == "Microsoft Internet Explorer") {
        return true;
    }
    return false;
}
function isFF_Sa_Op_Ch() {
    var browser_type = navigator.appName
    if (browser_type == "Netscape" || browser_type == "Opera") {
        return true;
    }
    return false;
}
function resizeArea(id) {
    newHeight = window.screen.availHeight;
    byId(id).style.height = (newHeight - 265) + "px";
}
function resizeAreaByShift(id, shift) {
    newHeight = window.screen.availHeight;
    byId(id).style.height = (newHeight - shift) + "px";
}

function validateHTML(src) {
    if (src.indexOf("<") == -1 || src.indexOf(">") == -1) {
        return false;
    }
    if (src.indexOf("</html>") != -1 ||
        src.indexOf("</td>") != -1 ||
        src.indexOf("</p>") != -1 ||
        src.indexOf("</a>") != -1 ||
        src.indexOf("</HTML>") != -1 ||
        src.indexOf("</TD>") != -1 ||
        src.indexOf("</P>") != -1 ||
        src.indexOf("</A>") != -1) {
        return true;
    }
    return false;
}

function validateUNL() {
    var tei = byId("teInput");
    if (tei.value.indexOf("[S:") == -1) {
        return false;
    }
    return true;
}
function validateSize() {
    var tei = byId("teInput").value.length;
    if (tei > 400000) {
        return false;
    }
    return true;
}
function validateUNLforDeco() {
    var tei = byId("teInput");
    if (tei.value.indexOf("{unl}") == -1) {
        return false;
    }
    return true;
}

function validateEmptyInput() {
    var tei = byId("teInput");
    if (tei.value == "") {
        return false;
    }
    return true;
}

function cleanInputValue(id) {
    byId(id).value = "";
}
var selectedTab;

function selectTab(selTabId, allTabIds) {
    if (allTabIds == null) {
        return;
    }
    for (var i = 0; i < allTabIds.length; i++) {
        hide(allTabIds[i] + "Container");
        var tab = byId(allTabIds[i]);
        tab.className = "tabborderleft tabbordertop tabborderbottom cellbg2";
    }
    show(selTabId + "Container");
    var tab = byId(selTabId);
    tab.className = "tabborderleft tabbordertop cellbg1";
    selectedTab = selTabId;
}


function getSelectedRadio(buttonGroup) {
    // returns the array number of the selected radio button or -1 if no button is selected
    if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
        for (var i = 0; i < buttonGroup.length; i++) {
            if (buttonGroup[i].checked) {
                return i
            }
        }
    } else {
        if (buttonGroup.checked) {
            return 0;
        } // if the one button is checked, return zero
    }
    // if we get to this point, no radio button is selected
    return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
    // returns the value of the selected radio button or "" if no button is selected
    var i = getSelectedRadio(buttonGroup);
    if (i == -1) {
        return "";
    } else {
        if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
            return buttonGroup[i].value;
        } else { // The button group is just the one button, and it is checked
            return buttonGroup.value;
        }
    }
} // Ends the "getSelectedRadioValue" function
var currContainerBodyId = null;
var callAfter = null;

function loadContent(url, containerId, containerBodyId, method, datastr, callAft, loadIntoTextArea, keepContainerHidden) {
    var container = byId(containerId);
    var containerBody = byId(containerBodyId);
    if (container == null || containerBody == null) {
        return;
    }
    if (container.style.display == "none" && !keepContainerHidden) {
        container.style.display = "";
    }
    if (containerBody.style.display == "none" && !keepContainerHidden) {
        containerBody.style.display = "";
    }
    if (currContainerBodyId != null) {
        alert("Another operation is in process, please wait.");
        return;
    }
    currContainerBodyId = containerBodyId;
    if (!loadIntoTextArea) {
        containerBody.innerHTML = "<img src='images/loading.gif'>&nbsp;loading..."
    }
    if (method == null) {
        method = "GET";
    }

    tstr = new Date().getTime();
    tstr = "t=" + tstr;

    if (method == "POST") {
        if (datastr != "") {
            tstr = "&" + tstr;
        }
        if (url.indexOf("?") != -1) {
            datastr += "&" + url.substring(url.indexOf("?") + 1, url.length)
        }
        datastr += tstr;
    } else {
        if (url.indexOf("?") > 0) {
            tstr = "&" + tstr;
        } else {
            tstr = "?" + tstr;
        }
        url += tstr;
    }
    callAfter = callAft;

    if (loadIntoTextArea) {
        Request.send(unescape(escape(url)), method, datastr, loadContentCallbackForTextArea);
    } else {
        Request.send(unescape(escape(url)), method, datastr, loadContentCallback);
    }
}

function loadContentCallback(resp) {
    byId(currContainerBodyId).innerHTML = resp.trim();
    currContainerBodyId = null;
    if (callAfter != null) {
        var ca = callAfter;
        callAfter == null;
        eval(ca);
    }
}

function loadContentCallbackForTextArea(resp) {
    byId(currContainerBodyId).value = resp.trim();
    currContainerBodyId = null;
    if (callAfter != null) {
        var ca = callAfter;
        callAfter == null;
        eval(ca);
    }
}
var tabMap = new Hashtable();

function selectFrameTab(selTabId, iframId, urlStr, tabGroupName) {
    var previousSelectedTabId = tabMap.get(tabGroupName)
    if (previousSelectedTabId != null) {
        var tab = byId(previousSelectedTabId);
        tab.className = "tabitem tabitembg tabborderleft tabbordertop tabborderbottom";
    }
    tab = byId(selTabId);

    tab.className = "tabitemselected tabborderleft tabbordertop";
    tabMap.put(tabGroupName, selTabId)
    tstr = new Date().getTime();
    tstr = "&t=" + tstr;
    urlStr += tstr;
    var frame = byId(iframId);
    frame.src = urlStr;
}

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/, "");
}


String.prototype.unicodeEncode = function() {
    var str = this;
    q = '';
    for (i = 0; i < str.length; i++) {
        j = str.charCodeAt(i);
        q += (j == 38) ? '&amp;' : (j < 128) ? str.charAt(i) : '&#' + j + ';';
    }
    return q;
}

String.prototype.unicodeDecode = function() {
    //String.fromCharCode(c)
    var string = "";
    var utf = this;
    var re = new RegExp("&#\\d+;");
    var m = re.exec(utf);

    while (m != null) {

        //var s = m.substring(2, m.length-1)
        //string += String.fromCharCode(s)

        utf = utf.substring(0, m.index) + String.fromCharCode(utf.substring(m.index + 2, m.index + 6)) + utf.substring(m.index + 7)
        //alert(utf)
        m = re.exec(utf);

    }

    return utf;
}

function submitSearch(e, func) {
    if (e.keyCode == 13) {
        eval(func);
    }
}

function getInnerText(obj) {
    if (navigator.appName == "Microsoft Internet Explorer") {
        return obj.innerText.trim();
    }
    else {
        return obj.textContent.trim();
    }
}

function setInnerText(obj, txt) {
    if(obj) {
        if (navigator.appName == "Microsoft Internet Explorer") {
            obj.innerText = txt;
        } else {
            obj.textContent = txt;
        }
    }
}

function expand(objId) {
    var obj = byId(objId);
    obj.style.position = "absolute";
    obj.style.left = "0px";
    obj.style.top = "0px"
}
function collapse(objId) {
    var obj = byId(objId);
    obj.style.position = "";
}

function goTo(url) {
    window.location = url;
}

//returns tha value of the checked radio button
function getCheckedValue(radioObj) {
    if (!radioObj)
        return "";
    var radioLength = radioObj.length;
    if (radioLength == undefined)
        if (radioObj.checked)
            return radioObj.value;
        else
            return "";
    for (var i = 0; i < radioLength; i++) {
        if (radioObj[i].checked) {
            return radioObj[i].value;
        }
    }
    return "";
}

function hasClass(ele, cls) {
    return ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
}

function addClass(ele, cls) {
    if (!this.hasClass(ele, cls)) ele.className += " " + cls;
}

function removeClass(ele, cls) {
    if (hasClass(ele, cls)) {
        var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
        ele.className = ele.className.replace(reg, ' ');
    }
}

function insertAfter(newChild, refChild) {
    refChild.parentNode.insertBefore(newChild, refChild.nextSibling);
}

function Set_Cookie(name, value, expires, path, domain, secure) {
    var today = new Date();
    today.setTime(today.getTime());

    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + "=" + escape(value) +
                      ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
                      ( ( path ) ? ";path=" + path : "" ) +
                      ( ( domain ) ? ";domain=" + domain : "" ) +
                      ( ( secure ) ? ";secure" : "" );
}

function Get_Cookie(check_name) {
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (i = 0; i < a_all_cookies.length; i++) {
        a_temp_cookie = a_all_cookies[i].split('=');

        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        if (cookie_name == check_name) {
            b_cookie_found = true;
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}

function actionOnEnter(e, function_name) {
    var intKeyCode = 0;
    if (isIE()) {
        if (window.event.keyCode == 13) {
            if (function_name) {
                function_name();
            }
        }
    } else {
        intKeyCode = e.which;
        if (intKeyCode == 13) {
            if (function_name) {
                function_name();
            }
        }
    }
}

var dialogZindex = 1000001;
function incZindex(idStr) {
    byId(idStr).style.zIndex = ++dialogZindex;
}

var currDocIndex = null;
function loadDocument(file, title) {
    byId("document_container").innerHTML = "Loading ...";
    if(currDocIndex != null && byId(currDocIndex)!= null){
    	byId(currDocIndex).style.fontWeight = "normal";
    }
    byId("index_"+title).style.fontWeight = "bold";
    currDocIndex = "index_"+title;
    Request.send(file+"?t="+new Date().getTime(), "GET", null, function(response){
        byId("document_container").innerHTML = response;
        byId("document_title").innerHTML = title;
    });
}

function showDocument(file) {
    openPopupDialog(file, "Documentation", 500, 400);
}

function selectAll(el_id) {
    for (var i = 0; i < byId(el_id).options.length; i++) {
        byId(el_id).options[i].selected = true;
    }
}

function deselectAll(el_id) {
    for (var i = 0; i < byId(el_id).options.length; i++) {
        byId(el_id).options[i].selected = false;
    }
}

function openForgotPasswordDialog() {
    openPopupDialog("forgot_password.jsp", "Forgot password", 300, "auto");
}

function sendResetedPassword(dlgId) {
    var email = byId("forgot_password_email").value;
    if(email.trim() == "") {
        return alert("Please enter you email address and then click Send.");
    }
    var data = "action=send_email";
    data += "&email=" + email;
    Request.send("forgot_password.jsp", "POST", data, function(response){
        alert(response.trim());
        if(response.trim() == "Message sent, check your email.") {
            hidePopupDialog(dlgId);
        }
    });
}
function showLoading(text) {
    if(text == undefined) {
        text = "&nbsp;&nbsp;loading ...";
    }

    byId("loading_screen_text").innerHTML = text;
	byId('loading_screen').style.display="";
}
function hideLoading() {

	byId('loading_screen').style.display="none";
}
