var lastSubmitTime=0;

function showMessages() {

	new Ajax.Request('?',
	{
		method:'get',
		parameters: {ajax: 1, m: 'kklMessage'},
		onSuccess: function(transport){
			var response = transport.responseText ;

			if (!response) return true;

			set_overlay(1);

			document.documentElement.onscroll = set_overlay;
			document.onscroll = set_overlay;
			window.onresize = set_center_overlay;

			$('kklmsgbox').style.display = "block";
			$('kklmsgboxClose').style.display = 'block';
			//$('overlay').onclick = hideBox;


			$('kklmsgboxContent').update(response);

			setTimeout("center($('box'));", 50);

			return true;
		}
		/*onFailure: function(){ alert('Something went wrong...') }*/
	});

}

function checkSubs(id,aChecked) {
	var collection = document.getElementById(Id).getElementsByTagName('input');
    for (var x=0; x<collection.length; x++) {
        if (collection[x].type.toUpperCase()=='CHECKBOX')
            collection[x].checked = aChecked;
    }
}

function showHide(id){
	ID = document.getElementById(id);

	if(ID.style.display == "")
	ID.style.display = "none";
	else
	ID.style.display = "";
}

function showHideB(id){
	var ID = document.getElementById(id);

	if(ID.style.display != "block")
	ID.style.display = "block";
	else
	ID.style.display = "none";
}

function showHideChildren(id){
	elem = document.getElementById(id);

	var elems = elem.getElementsByTagName('ul');
	nm = elems.length;
	
	for (i=0; i<nm; i++)
	{
		
		if(elems[i].style.display == "block") {
			elems[i].style.display = "none";
		}
		else {
			elems[i].style.display = "block";
		}
	}


}

function swapImg(id,img1,img2) {
	
	if (document.getElementById(id).src==img1) {
		document.getElementById(id).src=img2;
	}
	else document.getElementById(id).src=img1;
	
	
}

function showhideById(id) {
	el = document.getElementById(id);
	el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
}

function ConfirmMsg(newurl,message) {
	if (confirm( message ) )
	{
		parent.location=newurl;

	}

}

function doLinkConfirm( oObj, message ) {
	if ( !oObj.getAttribute('o_href') ) {
		var oAttr = document.createAttribute("o_href");
		oAttr.value = oObj.href;
		oObj.setAttributeNode(oAttr);
		oObj.href = '#';
	}

	if ( !message ) {
		message = 'Kustuta ?';
	}

	if ( confirm(message) ) {
		var oAttr = oObj.getAttribute('o_href');
		window.location.href = oAttr;
	}
	return false;
}

function findForm ( oObj ) {
	var notFound = true;
	var oForm = null;
	while ( notFound ) {
		if ( !oObj ) {
			notFound = false;
			continue;
		}

		if ( !oObj.parentNode ) {
			notFound = false;
			continue;
		}

		if ( oObj.tagName.toLowerCase() == 'form' ) {
			oForm = oObj;
			notFound = false;
			continue;
		}
		oObj = oObj.parentNode;
	}

	return oForm;
}


function submitForm ( oImage, sSubmitUrl ) {

	/**
	*
	* Double click trapping
	*/

	var tDate = new Date;
	var localTrapTime = tDate.valueOf();

	if (lastSubmitTime>0 && (localTrapTime - lastSubmitTime) < (1 * 1000)) {

	}
	else {
		lastSubmitTime=localTrapTime;
		var oForm = findForm ( oImage );

		if ( oForm == null ) {
			return false;
		}

		if ( sSubmitUrl && sSubmitUrl.length ) {
			oForm.action = sSubmitUrl;
		}

		if ( oForm.onsubmit ) {
			oForm.onsubmit();
		}

		oForm.submit();
		return true;
	}
}


function toggleRowsOn(tableId, start, end){

	tbl = document.getElementById(tableId);

	var vStyle = "";

	for(var i=start ; i< end; i++){
		if (tbl.rows[i]) tbl.rows[i].style.display = vStyle;
	}
}

function toggleRowsOff(tableId, start, end){

	tbl = document.getElementById(tableId);

	var vStyle = "none";

	for(i=start ; i< end; i++){
		if (tbl.rows[i]) tbl.rows[i].style.display = vStyle;
	}

}

function toggleRows(tableId, start, end){

	tbl = document.getElementById(tableId);

	var vStyle = "none";

	for(i=start ; i< end; i++){
		if (tbl.rows[i] && tbl.rows[i].style.display == vStyle) {
			tbl.rows[i].style.display = '';
		}
		else tbl.rows[i].style.display = vStyle;
	}

}

function get_selected(object){
	for (var i = 0; i < object.length; i++) {
		if (object[i].selected) return(object[i].value);
	}
}

function openPopup( link, name, width, height ) {
	if ( !width ) { width = 510; }
	if ( !height ) { height = 510; }


	var left = Math.floor( screen.availWidth / 2 ) - Math.floor( width / 2 );
	var top = Math.floor( screen.availHeight / 2 ) - Math.floor( height / 2 );
	prop = 'toolbar=0,directories=0,menubar=0,status=yes,resizable=1,location=0,scrollbars=1,copyhistory=0'

	var win = window.open( link, name, 'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',' + prop );

	if ( win ) win.focus();

	return win;
}

function cloneRow(row_id,namerep,counter_id) {
	var thisRow = document.getElementById(row_id);
	var newElement = thisRow.cloneNode(true);
	newElement.style.visibility='visible';

	var rowCount = thisRow.parentNode.getElementsByTagName("TR").length;
	rowCount = rowCount-2;

	newElement.id = newElement.id + rowCount;

	var inputs = newElement.getElementsByTagName('input');
	nm = inputs.length; //number of inputs to cycle through

	//Change names of all inputs to be used as new row
	for (i=0; i<nm; i++)
	{
		inputs[i].name = inputs[i].name.replace('{row}',rowCount);
	}

	var selects = newElement.getElementsByTagName('select');
	nm = selects.length; //number of inputs to cycle through

	//Change names of all selectboxes to be used as new row
	for (i=0; i<nm; i++)
	{
		selects[i].name = selects[i].name.replace('{row}',rowCount);
	}


	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
		thisRow.parentNode.insertBefore(newElement,thisRow .previousSibling);
	}
	else {
		thisRow.parentNode.insertBefore(newElement,thisRow .previousSibling.previousSibling);
	}

	document.getElementById(counter_id).value=parseInt(document.getElementById(counter_id).value)+1;
}

function delRowByChild(obj,counter) {
	var thisRow = obj.parentNode;
	while ( (thisRow = thisRow.parentNode)  && thisRow.tagName !="TR");
	rownr=thisRow.rowIndex;
	rownr=' '+rownr+' ';
	thisRow.parentNode.removeChild(thisRow);

	if (counter) {
		document.getElementById(counter).value=document.getElementById(counter).value + rownr;
	}
	return false;
}

function getWindowHeight()
{
	var windowHeight=0;
	if (typeof(window.innerHeight)=='number')
	windowHeight=window.innerHeight;
	else if (document.documentElement&&document.documentElement.clientHeight)
	windowHeight=document.documentElement.clientHeight;
	else if (document.body&&document.body.clientHeight)
	windowHeight=document.body.clientHeight;

	return windowHeight;
}

function getWindowWidth()
{
	var windowWidth=0;
	if (typeof(window.innerWidth)=='number')
	windowWidth=window.innerWidth;
	else if (document.documentElement&&document.documentElement.clientWidth)
	windowWidth=document.documentElement.clientWidth;
	else if (document.body&&document.body.clientWidth)
	windowWidth=document.body.clientWidth;

	return windowWidth;
}

function kklgetElementsByClassName(oElm, strTagName, strClassName){
	if (!oElm) return false;
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

