var onload_functions = new Array();
var onunload_functions = new Array();
var onscroll_functions = new Array();
var onclick_functions = new Array();
var xmlHttp;
var scriptHideElements = new Array();

String.prototype.trim=function(){var x=this; x=x.replace(/^\s*(.*)/, "$1"); x=x.replace(/(.*?)\s*$/, "$1"); return x;}
window.onload=function(){for(i=0;i<=onload_functions.length-1;i++) eval(onload_functions[i]);}
window.onunload=function(){for(i=0;i<=onunload_functions.length-1;i++) eval(onunload_functions[i]);}
window.onscroll=function(){for(i=0;i<=onscroll_functions.length-1;i++) eval(onscroll_functions[i]);}
window.onclick=function(){for(i=0;i<=onclick_functions.length-1;i++) eval(onclick_functions[i]);}

function objPos(obj){
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function mouseXY(evt) {
    var x=0;var y=0;
    if (evt.pageX) x=evt.pageX;
    else if (evt.clientX)
    x=evt.clientX + (document.documentElement.scrollLeft ?
        document.documentElement.scrollLeft :
        document.body.scrollLeft);
    else x=null;
    if (evt.pageY) y=evt.pageY;
    else if (evt.clientY)
    y=evt.clientY + (document.documentElement.scrollTop ?
        document.documentElement.scrollTop :
        document.body.scrollTop);
    else y=null;
    return[x,y];
}

function validateEmail(s){
	var patn = /^[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]*)*@[a-zA-Z0-9\-]+([\.][a-zA-Z0-9\-]+)+$/;
	if(patn.test(s))return 0; else return 1;
}

function isIE() {
	if(document.all) return true;
	return false;
}

function getWindowHeight() {
    if(window.innerWidth || window.innerHeight)docheight = window.innerHeight;
    if(document.body.clientWidth || document.body.clientHeight)docheight = document.body.clientHeight;
    return docheight;
}    
function getWindowWidth() {
    if(window.innerWidth || window.innerHeight) docwidth = window.innerWidth;
    if(document.body.clientWidth || document.body.clientHeight)docwidth = document.body.clientWidth;
    return docwidth;
}
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function randomText(len){
    var available_chars = [];
    for(var i = 48; i < 58; i++) available_chars[available_chars.length] = String.fromCharCode(i);     for(var i = 65; i < 91; i++) available_chars[available_chars.length] = String.fromCharCode(i);
    for(var i = 97; i < 123; i++)available_chars[available_chars.length] = String.fromCharCode(i); 
    if(0==available_chars.length) return false;
    var result="";   
    for(var j=0; j<len; j++){
        var ch=available_chars[Math.floor(Math.random()*available_chars.length)];
        result+=ch;
    }
    return result;
}

function delete_alert(param){
     return confirm("Are you sure you want to delete "+param);
}

function disableWindow() {
    scriptHideElements =  Array();
    var inputs = document.getElementsByTagName("input");
    var selects = document.getElementsByTagName("select");

    for (var i = 0; i < inputs.length-65; i++) {
        if (inputs[i].style.visibility != "hidden") {
            if (!inputs[i].attributes["id"] || 
                    inputs[i].attributes["id"] == "" || 
                    typeof (inputs[i].attributes["id"]) == "undefined" ||
                    !inputs[i].id  ) 

            inputs[i].id = "inp_" + i + "_" + randomText(15);
            inputs[i].style.visibility = "hidden";
            scriptHideElements.push(inputs[i].id);
        }
    }

    for (var i = 0; i < selects.length; i++) {
        if (selects[i].style.visibility != "hidden") {
          if (!selects[i].attributes["id"] || 
                    selects[i].attributes["id"] == "" || 
                    typeof (selects[i].attributes["id"]) == "undefined") 
            selects[i].id =  "sel_" + i + "_" + randomText(15);
            selects[i].style.visibility = "hidden";
            scriptHideElements.push(selects[i].id);
        }
    }
    var a = document.body;
    document.getElementById("all").style.height = a.offsetHeight + "px";
    document.getElementById("all").style.visibility = "visible";
}

function enableWindow() {
    for (var i = 0; i < scriptHideElements.length; i++) {
        var el = document.getElementById(scriptHideElements[i])
        if (el) el.style.visibility = "visible";
    }
    scriptHideElements =  Array();
    document.getElementById("all").style.height = "0px";
    document.getElementById("all").style.visibility = "hidden";
}

function getXmlHttp(){
    var xmlH;
    if(window.XMLHttpRequest) xmlH=new XMLHttpRequest();
    else if (window.ActiveXObject) xmlH=new ActiveXObject("Microsoft.XMLHTTP");
    if(xmlH==null){
        alert ("Browser does not support HTTP Request")
        return null;
    }else{
        return xmlH;
    }
}


function setLoadingAnim(obj_id, text, style) {
    var obj = document.getElementById(obj_id);
    if (style) style=" style=\"" + style + "\"";
    if (obj) {
        obj.innerHTML = "<div class=\"loading_small\"" + style + ">" + text + "</div><div class=\"clear\"></div>";
    }
}

function changeTab(obj_id, id, prefix) {
    var obj=document.getElementById(obj_id);
    if (obj && obj.tagName == "UL") {
        for (i=0; i<obj.childNodes.length; i++) {
            if (obj.childNodes[i].tagName == "LI") {
                var obj_li = obj.childNodes[i];
                for (j=0; j<obj_li.childNodes.length; j++) {
                    if (obj_li.childNodes[j].id == prefix + id) {
                        obj_li.childNodes[j].className = "active";
                    } else {
                        obj_li.childNodes[j].className = "";
                    }

                }
            }
        }
    }
}

function viewportSize() {
    var viewportwidth;
    var viewportheight;

    if (typeof window.innerWidth != 'undefined') {
        viewportwidth = window.innerWidth,
        viewportheight = window.innerHeight
    } else if (typeof document.documentElement != 'undefined'
        && typeof document.documentElement.clientWidth !=
        'undefined' && document.documentElement.clientWidth != 0) { 
        viewportwidth = document.documentElement.clientWidth,
        viewportheight = document.documentElement.clientHeight
    } else {
        viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
        viewportheight = document.getElementsByTagName('body')[0].clientHeight
    }
    return [viewportwidth, viewportheight]
}

function preloadImages(ar) {
    if (!document.images) return;
    var arguments = preloadImages.arguments;
    for (var i = 1; i < arguments.length; i++) {
        ar[i-1] = new Image();
        ar[i-1].src = arguments[i];
    }
}


function ScrollableTable (tableEl, tableHeight, tableWidth) {

	this.initIEengine = function () {

		this.containerEl.style.overflowY = 'auto';
		if (this.tableEl.parentElement.clientHeight - this.tableEl.offsetHeight < 0) {
			this.tableEl.style.width = this.newWidth - this.scrollWidth +'px';
		} else {
			this.containerEl.style.overflowY = 'hidden';
			this.tableEl.style.width = this.newWidth +'px';
		}

		if (this.thead) {
			var trs = this.thead.getElementsByTagName('tr');
			for (x=0; x<trs.length; x++) {
				trs[x].style.position ='relative';
				trs[x].style.setExpression("top",  "this.parentElement.parentElement.parentElement.scrollTop + 'px'");
			}
		}

		if (this.tfoot) {
			var trs = this.tfoot.getElementsByTagName('tr');
			for (x=0; x<trs.length; x++) {
				trs[x].style.position ='relative';
				trs[x].style.setExpression("bottom",  "(this.parentElement.parentElement.offsetHeight - this.parentElement.parentElement.parentElement.clientHeight - this.parentElement.parentElement.parentElement.scrollTop) + 'px'");
			}
		}

		eval("window.attachEvent('onresize', function () { document.getElementById('" + this.tableEl.id + "').style.visibility = 'hidden'; document.getElementById('" + this.tableEl.id + "').style.visibility = 'visible'; } )");
	};


	this.initFFengine = function () {
		this.containerEl.style.overflow = 'hidden';
		this.tableEl.style.width = this.newWidth + 'px';

		var headHeight = (this.thead) ? this.thead.clientHeight : 0;
		var footHeight = (this.tfoot) ? this.tfoot.clientHeight : 0;
		var bodyHeight = this.tbody.clientHeight;
		var trs = this.tbody.getElementsByTagName('tr');
		if (bodyHeight >= (this.newHeight - (headHeight + footHeight))) {
			this.tbody.style.overflow = '-moz-scrollbars-vertical';
			for (x=0; x<trs.length; x++) {
				var tds = trs[x].getElementsByTagName('td');
				tds[tds.length-1].style.paddingRight += this.scrollWidth + 'px';
			}
		} else {
			this.tbody.style.overflow = '-moz-scrollbars-none';
		}

		var cellSpacing = (this.tableEl.offsetHeight - (this.tbody.clientHeight + headHeight + footHeight)) / 4;
		this.tbody.style.height = (this.newHeight - (headHeight + cellSpacing * 2) - (footHeight + cellSpacing * 2)) + 'px';

	};

	this.tableEl = tableEl;
	this.scrollWidth = 16;

	this.originalHeight = this.tableEl.clientHeight;
	this.originalWidth = this.tableEl.clientWidth;

	this.newHeight = parseInt(tableHeight);
	this.newWidth = tableWidth ? parseInt(tableWidth) : this.originalWidth;

	this.tableEl.style.height = 'auto';
	this.tableEl.removeAttribute('height');

	this.containerEl = this.tableEl.parentNode.insertBefore(document.createElement('div'), this.tableEl);
	this.containerEl.appendChild(this.tableEl);
	this.containerEl.style.height = this.newHeight + 'px';
	this.containerEl.style.width = this.newWidth + 'px';


	var thead = this.tableEl.getElementsByTagName('thead');
	this.thead = (thead[0]) ? thead[0] : null;

	var tfoot = this.tableEl.getElementsByTagName('tfoot');
	this.tfoot = (tfoot[0]) ? tfoot[0] : null;

	var tbody = this.tableEl.getElementsByTagName('tbody');
	this.tbody = (tbody[0]) ? tbody[0] : null;

	if (!this.tbody) return;

	if (document.all && document.getElementById && !window.opera) this.initIEengine();
	if (!document.all && document.getElementById && !window.opera) this.initFFengine();


}


function checkEnter(e,ibox){

    var characterCode //literal character code will be stored in this variable

    if(e && e.which){ //if which property of event object is supported (NN4)
        e = e;
        characterCode = e.which; //character code is contained in NN4's which property
    }
    else{
        e = event;
        characterCode = e.keyCode; //character code is contained in IE's keyCode property
    }

    if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
        ibox.form.submit(); //submit the form BUT NOT THE FIRST ONE IN THE BLOODY PAGE
        return false;
    }
    else{
        return true;
    }

}
