//document.attachEvent('onload', doInit);

// call this when a user want to delete something





function ActionConfirm(message)
{
	if(confirm(message)) return true;
	else return false;
}

function setOffset(offset,form)
{
	var count = form.count.options[form.count.selectedIndex].value;
	form.offset.value = offset > 0 ? Math.floor(offset / count) * count : 0;
	return;
}

// response a new location
function ResponseLocation(URL)
{
	if (URL) document.location.href = URL;
}

// page count function
function CheckPageCount(obj,total)
{
	if (Number(obj.value) != obj.value || Number(obj.value) > total)
	{
		obj.value = total;
		return false;
	} else return true;
}

// scroll document on top
function onTop()
{
	window.scroll(0, 0);
}

// open dialog
function OpenDialog (file, linkState, width, height)
{
	if (linkState <= 0) return false;
	var width  = width ? width : 640;
	var height = height ? height : 550;
	window.open(
		file, "dialog", "width=" + width + ", height=" + height + ", " +
		"menubar=no, resizable=no, scrollbars=yes, status=no, titlebar=no, toolbar=no, help=no"
	);
}

function OpenWindow(file,width,height,scrollbars)
{
	var width  = width ? width : 640;
	var height = height ? height : 550;
	var scrollbars = scrollbars ? scrollbars : 'yes';
	var x = (screen.width - width)/2;
	var y = (screen.height - width)/2;
	window.open(
		file, 'dialog', 'left=' + x + ', top=' + y + ', width=' + width + ', height=' + height + ', ' +
		'menubar=no, resizable=yes, scrollbars=' + scrollbars + ', status=yes, titlebar=yes, toolbar=yes, help=no'
	);
}




function setCookie(name, value, days, hours, minutes){
	if (days || hours || minutes) {
		var date = new Date();
		days = days ? days : 0;
		hours = hours ? hours :0;
		minutes = minutes ? minutes : 0;
		date.setTime(date.getTime() + 60 * 1000 * (minutes + 60 * (hours + 24 * days)));
		var expires = "; expires=" + date.toGMTString();
	}
	else expires = "";
	document.cookie = name + "=" + escape(value) + expires + "; path=/";
}

function getCookie(name)
{
	var c = document.cookie;
	if (c.indexOf(name) == -1) return null;
	else
	{
		var start = c.indexOf(name) + name.length + 1;
		for (var end = start; end < c.length; end++) {
			if (c.charAt(end) == ";") break;
		}
		return unescape(c.substring(start, end))
	}
}

function deleteCookie(name)
{
	setCookie(name, "", -1)
}

function MatchFileds(form)
{
	with(form)
	{
		var Year = (fYear.value > tYear.value ? tYear.value : fYear.value);
		var Month = (fMonth.value > tMonth.value && Year == tYear.value ? tMonth.value : fMonth.value);
		var Day = (fDay.value > tDay.value && Month == tMonth.value ? tDay.value : fDay.value);

		from.value = Year + '-' + Month + '-' + Day;
		to.value = tYear.value + '-' + tMonth.value + '-' + tDay.value;
		fYear.disabled = true;
		fMonth.disabled = true;
		fDay.disabled = true;
		tYear.disabled = true;
		tMonth.disabled = true;
		tDay.disabled = true;
	}
	return true;
}

/**
 * Opens calendar window.
 *
 * @param   string      calendar.php parameters
 * @param   string      form name
 * @param   string      field name
 * @param   string      edit type - date/timestamp
 */
var dateField;
var dateType;

function openCalendar(dfield,dtype){
 dw_height=290;
 df = document.getElementById(dfield);
 dateField = df.value;
 dateType = dtype;
 if(dtype == 'datetime') dw_height=340;
 newwin = window.open(
    "/tools/calendar.php?dvalue="+dateField+'&dtype='+dateType+'&dfield='+dfield,
    "calendar_select",
    "height=" + dw_height + ", width=300, top=100, left=100, status=yes, toolbar=no, menubar=no, location=no, resize=yes"
  );
}

	/**
	*
	* IMAGES
	*
	**/

	function openDynamicImageWindow(file,w,h,typ,closeText)
	{
		if(typ == "dhtml")
		{
			var windowWidth = getClientWidth();
			var windowHeight = getClientHeight();
			var scrolltop = document.body.scrollTop;
			var scrollleft = document.body.scrollLeft;
			var postop =(windowHeight/2)-(h/ 2)+scrolltop+document.body.style.padding; 
			var posleft =(windowWidth/2)-(w/ 2)+scrollleft+document.body.style.padding; 

			//alert(windowWidth + ":" + windowHeight);

			if(postop >= 20) { postop = postop-20; } 
			var bodyObj = document.getElementsByTagName('BODY')[0]; 
			if(!document.getElementById("dynamicImageContainer"))
			{ 
				divObjContainer = document.createElement("div"); 
				divObjContainer.setAttribute("id", "dynamicImageContainer"); 
				bodyObj.appendChild(divObjContainer); 
				//bodyObj.insertBefore(divObjContainer, bodyObj.firstChild); 
			} 
			else 
			{ 
				divObjContainer = document.getElementById("dynamicImageContainer"); 
			} 
			divObjContainer.style.display = "block"; 
			divObjContainer.innerHTML = '<div class="imageContainer" style="top:' + postop + 'px; left:' + posleft + 'px; position: absolute; display:block;"><div class="imageBorder" style="background-color: #fff; padding: 4px; border: solid 1px #cccccc;"><img onclick="closeDynamicImageWindow();" style="cursor: pointer; border: solid 1px #000000;" src="' + file + '" alt="' + closeText + '" width="' + w + '" height="' + h +'"/></div></div>'; 
		} 
		else if(typ == "javascript")
		{ 
			// center popup 
			var postop =(screen.height /2)-(h/ 2); 
			var posleft =(screen.width /2)-(w/ 2); 
			window.open(file+'&closeText='+encodeURI(closeText)+ 
			'', '', 'dependent=yes,width='+w+',height='+h+',left='+posleft+',top='+postop+',scrollbars=no,resizable=no,menubar=no,location=no,status=no'); 
		}
	}
		
	function closeDynamicImageWindow()
	{ 
		document.getElementById("dynamicImageContainer").style.display = "none"; 
	}
	
	
	/**
	/* GAME WINDOW
	**/
	
	
	function openGameDialog(){

		var windowWidth = self.document.body.clientWidth;
		var windowHeight = self.document.body.clientHeight;
		var scrolltop = document.body.scrollTop;
		var scrollleft = document.body.scrollLeft;
		var postop =(windowHeight/2)-(600/ 2)+scrolltop+document.body.style.padding; 
		var posleft =(windowWidth/2)-(800/ 2)+scrollleft+document.body.style.padding; 
		if(postop >= 20) { postop = postop-20; } 
		if(gameDiv = document.getElementById('gameDialog')){
			gameDiv.style.top = postop;
			gameDiv.style.left = posleft;
			gameDiv.style.display = 'block';
		}
	}
	
	function closeGameDialog()
	{ 
		document.getElementById("gameDialog").style.display = "none"; 
	}
	
	function getClientWidth()
	{
	  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
	}
	
	function getClientHeight()
	{
	  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
	}		
