/* global var for content resizing */
var NOWRAP;

var onloadFunctions=new Array();

function newWin(url, name) {

	if (typeof(name)=="undefined") {	
		name="_blank";
	}

	return window.open(url, name);
}

function targetBlank (url) { // not sure if we really need this here in this form
	blankWin = window.open(url,'_blank','menubar=no,toolbar=no,location=no,directories=no,fullscreen=no,titlebar=no,hotkeys=no,status=yes,scrollbars=no,resizable=yes');
}

function addOnLoadFunction(functionToAdd) { // This appears not to work in Firefox and Google Chrome
	if (typeof(functionToAdd)!='function') {
		window.alert('Trying to add non function for onload in scripts.js');
		return false;
	}
	else {
		onloadFunctions[onloadFunctions.length]=functionToAdd;
		return true;
	}
}

function onBodyLoad() { // This appears not to work in Firefox and Google Chrome
	l=onloadFunctions.length;
	for (i=0;i<l;i++) {
		onloadFunctions[i]();
	}
}

function verticalAlign(subject) {
	// aligns the subject (or this) to it's parents vertically. Needed because the vertical-align style does not always do what we want.
	// let's skip the error checking for now
	// does not work for absolute positioned items (but would not make sense either for that
	//window.alert(subject.parentNode.innerHTML);
	if (typeof(subject)!="object") // added by Guus on June 19, 2009
		return false;
	else {
		myHeight=subject.offsetHeight;
		parentHeight=subject.parentNode.offsetHeight;
		subject.style.position="relative";
		subject.style.top=Math.round((parentHeight-myHeight)/2)+"px";
	}
}

function divNumber(val1, val2, decimals) {

	if (val1==0) 
		if (val2!=0)
			return '&#8734;';
		else
			return '-';
	else {
		multiply=1;
		for (i=0;i<decimals;i++)
			multiply=multiply*10;
		return Math.round(val2/val1*multiply)/multiply;
	}
}

function setToday() {
	
	var f, periodElement, year;
	
	f = document.getElementById('adminForm');
			
	if(typeof(f)=="object") {
		periodElement=document.getElementById("periodSelector");
		yearElement=document.getElementById("yearSelector");
		monthElement=document.getElementById("monthSelector");
		weekElement=document.getElementById("weekSelector");
		dayElement=document.getElementById("daySelector");

		if (typeof(yearElement)=="object")
			yearElement.value=myToday.getFullYear(); // defined in js/includes.php
		if (typeof(monthElement)=="object")
			monthElement.value=myToday.getMonth();
		if (typeof(weekElement)=="object")
			weekElement.value=myToday.getWeek();
		if (typeof(dayElement)=="object")
		dayElement.value=myToday.getDate();
		
		f.submit();
		
		return true;
	}
	else
		return false;
}

function changeDynaList( listname, source, key, orig_val ) {
	var list = eval( 'document.adminForm.' + listname );

	// empty the list
	for (i in list.options.length) {
		list.options[i] = null;
	}
	
	i = 0;
	for (x in source) {
		if (source[x][0] == key) {
			opt = new Option();
			opt.value = source[x][1];
			opt.text = source[x][2];

			if ((orig_val == opt.value) || i == 0) {
				opt.selected = true;
			}
			list.options[i++] = opt;
		}
	}
	list.length = i;
}

function popwin(filename, windowWidth, windowHeight) {
	var windowLeft = (screen.width / 2) - (windowWidth / 2);
	var windowTop = (screen.height / 2) - (windowHeight / 2);
	
	popwnd = window.open('','popwnd','scrollbars=no,directories=no,status=no,resizable=no,toolbar=no,location=no,menubar=no,menu=no,width='+windowWidth+',height='+windowHeight+',top='+windowTop+',left='+windowLeft);
	
	popwnd.resizeTo(popwnd.document.body.clientWidth + (document.all ? 10 : 5), windowHeight + 50);
	popwnd.location.href = filename;
	
	popwnd.focus();
	
	return popwnd;
}
function popwinr(filename, windowWidth, windowHeight) {
	var windowLeft = (screen.width / 2) - (windowWidth / 2);
	var windowTop = (screen.height / 2) - (windowHeight / 2);
	
	popwnd = window.open('','popwnd','scrollbars=yes,directories=no,status=no,resizable=yes,toolbar=no,location=no,menubar=yes,menu=no,width='+windowWidth+',height='+windowHeight+',top='+windowTop+',left='+windowLeft);
	
	popwnd.resizeTo(popwnd.document.body.clientWidth + (document.all ? 10 : 5), windowHeight + 50);
	popwnd.location.href = filename;
	
	popwnd.focus();
	
	return popwnd;
}

function selectAllItems( senderForm, isChecked ) {
	var formName = senderForm.name;
	var fldName = 'cb'+formName;
	var f = senderForm;
												
	if(f) {
		var cbItems = f['cbItems[]'];
		var n = (cbItems.length==null ? 1 : cbItems.length);
								
		for (i=0; i <= n; i++) {
			cb = eval( 'f.' + fldName + i );
			if (cb) {
				cb.checked = isChecked;
					
				setObjectHighlight( cb, 'tab'+formName+i );
			}
		}
	}
}

function setObjectHighlight( sender, id ) {
	var obj = document.getElementById(id);
												
	if(sender) {
		if(obj) {
			if(sender.checked)
				obj.style.background = "#efd52f";
			else
				obj.style.background = "#fff";
		}
	}
}

function toggleAllItems( sender,formObj ) {
	var fldName = 'cb';
	var isChecked = null;
	var f = formObj;
	if (!f) {
		f = document.adminForm;
	} else {
		fldName = formObj.name + '_cb';
	}
	
	if( sender ) {
		isChecked = sender.checked;
	} else {
		return;
	}
	
	if(f) {
		var cbItems = f['cbItems[]'];
		var n = (cbItems.length==null ? 1 : cbItems.length);
		
		for (i=0; i <= n; i++) {
			cb = eval( 'f.' + fldName + i );
			if (cb) {
				if( !cb.disabled ) cb.checked = isChecked;
			}
		}
	}
}
	
function getAllSelected(formObj) {
	var fldName = 'cb';
	var checkedItems = 0;
	var f = formObj;
	if (!f) {
		f = document.adminForm;
	} else {
		fldName = formObj.name + '_cb';
	}
	
	if(f) {
		var cbItems = f['cbItems[]'];
		
		if( cbItems != null ) {
			var n = (cbItems.length==null ? 1 : cbItems.length);
			
			for (i=0; i <= n; i++) {
				cb = eval( 'f.' + fldName + i );
				
				if(cb) {
					if (cb.checked) checkedItems++;
				}
			}
		}
	}
	
	return checkedItems;
}

function doListTask( taskname,formObj ) {
	var f = formObj;
	if (!f) {
		f = document.adminForm;
	}
	
	if(f) {
		if (taskname == '') {
			return false;
		}
	 
		if( getAllSelected(f) > 0 ) {
			f.task.value = taskname;
			f.submit();
		} else {
			alert(_NO_ITEM_SELECTED);
			return false;
		}
	 
		return true;
	}
	
	return false;
}

function doTask( taskname,formObj ) {
	var f = formObj;
	if (!f) {
		f = document.adminForm;
	}
	
	if(f) {
		if (taskname == '') {
			return false;
		} else {
			f.task.value = taskname;
			f.submit();
			
			return true;
		}
	}
	
	return false;
}

function showHide(sender, id, lookup) {
	var obj, visible;
			
	if(lookup)
		obj = document.getElementById(id);
	else
		obj = id;
	visible = (obj.style.display != "none");
	if(sender.src) {
		sender.src = (visible ? "images/dd_expand.gif" : "images/dd_collapse.gif");
	}
	
	obj.style.display = (visible ? "none" : "inline");
	
	return (visible ? 0 : 1);
}

/* Cookie functions */
function GetCookie(cookie_name) {
	if (document.cookie.length > 0) {
		_start = document.cookie.indexOf(cookie_name + "=");
		
		if (_start != -1) { 
			_start = _start + cookie_name.length+1;
			_end = document.cookie.indexOf(";", _start);
			
			if (_end == -1) c_end = document.cookie.length;
    		
			return unescape(document.cookie.substring(_start,_end));
		} 
	}
	return null;
}

function SetCookie(cookieName,cookieValue,nDays) {
	var today = new Date();
	var expire = new Date();
	
	if (nDays==null || nDays==0) nDays=1;
	
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}

function setCEState(cookie_name, val) {
	var val2 = GetCookie(cookie_name);
		
	if (val2) {
		if (val2 == val) {
			if (val == 1) 
				val = 0;
			else
				val = 1;
		}
	}
		
	SetCookie(cookie_name, val, 1);
}


function getWindowSize(loc) {
	switch(loc) {
		case 'page':
			return [Math.max(this.document.body.clientWidth, this.document.body.scrollWidth), Math.max(this.document.body.clientHeight, this.document.body.scrollHeight)];
		case 'scroll':
			return [this.document.body.scrollLeft, this.document.body.scrollTop];
		default:
			return [this.document.body.clientWidth, this.innerHeight || this.document.body.clientHeight];
	}
}
function show_on(a)
{
	
	if(a)
		{
			var getStyle = document.getElementById('show_on').style.visibility;
			if(getStyle == "visible")
			document.getElementById('show_on').style.visibility = "hidden";
			else
			document.getElementById('show_on').style.visibility = "visible";
		}
		else
		{
		document.getElementById('show_on').style.visibility = "hidden";
		}
}
function show_off(b){
	if(b)
		{
			var getStyle = document.getElementById('show_on').style.visibility;
			if(getStyle == "hidden")
			document.getElementById('show_on').style.visibility = "visible";
			else
			document.getElementById('show_on').style.visibility = "hidden";
		}
		else
		{
		document.getElementById('show_on').style.visibility = "visible";
		}
}
function hide_on(c)
{
	if(c)
		{
			var getStyle = document.getElementById('show_off').style.visibility;
			if(getStyle == "visible")
			document.getElementById('show_off').style.visibility = "hidden";
			else
			document.getElementById('show_off').style.visibility = "visible";
		}
		else
		{
		document.getElementById('show_off').style.visibility = "hidden";
		}
}
function hide_off(d){
	if(d)
		{
			var getStyle = document.getElementById('show_off').style.visibility;
			if(getStyle == "hidden")
			document.getElementById('show_off').style.visibility = "visible";
			else
			document.getElementById('show_off').style.visibility = "hidden";
		}
		else
		{
		document.getElementById('show_off').style.visibility = "visible";
		}

}

