
/* *******************************************************************************************
 *         INIT                                                                              *
 ******************************************************************************************* */

function init(id) {
	setMenuMarker(id) ;
	setDocFootHideLay() ;
	if (window.attachEvent) {
		window.attachEvent("onresize", function(){ setMenuMarker(id); setDocFootHideLay(); }) ;
	} else if (window.addEventListener) {
		window.addEventListener("resize", function(){ setMenuMarker(id); setDocFootHideLay(); }, true) ;
	}
}

/* *******************************************************************************************
 *         TOP MENU / DOC FOOT                                                               *
 ******************************************************************************************* */

function setMenuMarker(id) {
	var markerPosTop  = getElementTop(document.getElementById("top_menu_line")) - 5 ;
	var markerPosLeft = getElementLeft( document.getElementById("topmenu_" + id) ) - 2 ;
	var markerObj = document.getElementById("top_menu_marker") ;
	markerObj.style.top     = markerPosTop + "px" ;
	markerObj.style.left    = markerPosLeft + "px" ;
	markerObj.style.display = "block" ;
}

function setDocFootHideLay() {
	var obj = document.getElementById("docfoot_white") ;
	obj.style.top     = 0 ;
	obj.style.display = "none" ;
	var winW = (document.all) ? document.body.offsetWidth - 20 : document.body.clientWidth ;
	var winH = (document.all) ? document.body.offsetHeight - 4 : document.body.clientHeight ;
	if (document.body && document.body.scrollHeight) {
		scrollH = (document.body.scrollHeight < winH) ? winH : document.body.scrollHeight ;
	} else if (document.documentElement && document.documentElement.offsetHeight){
		scrollH = (document.documentElement.offsetHeight < winH) ? winH : document.documentElement.offsetHeight ;
	} else {
		scrollH = winH ;
	}
	obj.style.top     = (scrollH - 15) + "px" ;
	obj.style.left    = ((winW / 2) - 380) + "px" ;
	obj.style.display = "block" ;
}

/* *******************************************************************************************
 *         Get absolute position of any element                                              *
 ******************************************************************************************* */
 
 function getElementLeft(el) {
	if (!el && this) {
		el = this;
	}
	var posLeft = el.offsetLeft;
	var elPar = el.offsetParent;
	while (elPar != null) {
		posLeft += elPar.offsetLeft;
		elPar = elPar.offsetParent;
	}
	return posLeft;
}


function getElementTop(el) {
	if (!el && this) {
		el = this;
	}
	var posTop = el.offsetTop;
	var elPar = el.offsetParent;
	while (elPar != null) {
		posTop += elPar.offsetTop;
		elPar = elPar.offsetParent;
	}
	return posTop;
}


/* *******************************************************************************************
 *         Popup                                                                             *
 ******************************************************************************************* */

function popWinOpen(winW,winH,sUrl,sName,iResize,iScroll,iStatus) {
	if (screen) {
		var winX, winY ;
		if ((screen.height - winH) < 150) {
			winX = (screen.width - winW) / 2;
			winY = 0;
		} else {
			winX = (screen.width - winW) / 2;
			winY = (screen.height - winH) / 2;
		}
		var popWindow = window.open(sUrl,sName,"resizable=" + iResize + ",menubar=0,scrollbars=" + iScroll + ",status=" + iStatus + ",width=" + winW + ",height=" + winH + ",top=" + winY + ",left=" + winX + "");
		if (popWindow) popWindow.focus();
	} else {
		window.open(sUrl,sName,"resizable=" + iResize + ",menubar=0,scrollbars=" + iScroll + ",status=" + iStatus + ",width=" + winW + ",height=" + winH);
	}
}

function addEvent(sectionId) {
	popWinOpen(380,550,"/jsp/calendar/" + (sectionId != "" ? "?section_id=" + sectionId : ""),"EventWin",1,0,1) ;
}

function addCompany() {
	popWinOpen(770,600,"/jsp/companies/admin/","CompanyWin",1,1,1) ;
}
function editCompany(metaId) {
	popWinOpen(770,600,"/jsp/companies/admin/?meta_id=" + metaId,"CompanyWin",1,1,1) ;
}



/* *******************************************************************************************
 *         SelectBox                                                                         *
 ******************************************************************************************* */


function moveOptionToOtherSelectBox(fbox, tbox) {
	var arrFbox = new Array();
	var arrTbox = new Array();
	var arrLookup = new Array();
	var i;
	for (i = 0; i < tbox.options.length; i++) {
		arrLookup[tbox.options[i].text] = tbox.options[i].value;
		arrTbox[i] = tbox.options[i].text;
	}
	var fLength = 0;
	var tLength = arrTbox.length;
	for(i = 0; i < fbox.options.length; i++) {
		arrLookup[fbox.options[i].text] = fbox.options[i].value;
		if (fbox.options[i].selected && fbox.options[i].value != "") {
			arrTbox[tLength] = fbox.options[i].text;
			tLength++;
		} else {
			arrFbox[fLength] = fbox.options[i].text;
			fLength++;
		}
	}
	arrFbox.sort();
	arrTbox.sort();
	fbox.length = 0;
	tbox.length = 0;
	var c, no;
	for(c = 0; c < arrFbox.length; c++) {
		no = new Option();
		no.value = arrLookup[arrFbox[c]];
		no.text = arrFbox[c];
		fbox[c] = no;
	}
	for(c = 0; c < arrTbox.length; c++) {
		no = new Option();
		no.value = arrLookup[arrTbox[c]];
		no.text = arrTbox[c];
		tbox[c] = no;
	}
}

function moveCatOptionR() {
	moveOptionToOtherSelectBox(document.getElementById("tradeCatsAll"), document.getElementById("tradeCatsSel")) ;
}
function moveCatOptionL() {
	moveOptionToOtherSelectBox(document.getElementById("tradeCatsSel"), document.getElementById("tradeCatsAll")) ;
}


/* *******************************************************************************************
 *         Cookies                                                                           *
 ******************************************************************************************* */
 
function setCookie(name, value) {
	var sPath = '/';
	var today = new Date();
	var expire = new Date();
	expire.setTime(today.getTime() + 1000*60*60*24*365); // 365 days
	var sCookieCont = name + "=" + encodeURIComponent(value);
	sCookieCont += (expire == null) ? "" : "; expires=" + expire.toGMTString();
	sCookieCont += "; path=" + sPath;
	document.cookie = sCookieCont;
}

function getCookie(name) {
	try {
		var search = name + "=";
		if (document.cookie.length > 0) {
			var offset = document.cookie.indexOf(search);
			if (offset != -1) {
				offset += search.length;
				end = document.cookie.indexOf(";", offset);
				if (end == -1) {
					end = document.cookie.length;
				}
				return decodeURIComponent(document.cookie.substring(offset, end));
			}
		}
	} catch (e) {}
	return "" ;
}


