/**
* script.js 2004-08-23
*/
now = new Date();
window.name = "main"+now.getTime();

var bAllPara
var bDone = false;
var sMenu;
var frm
var bAdminMode = false;

function popUp(URL, name, dx, dy, iNoScrollbars) {
	if (iNoScrollbars) {
		iScrollbars = 0
	} else {
		iScrollbars = 1
	}
	sOptions = 'toolbar=0,scrollbars='+iScrollbars+',location=0,statusbar=0,menubar=0,resizable=1,width=' + dx + ',height=' + dy
	sOptions += ',left=' + ((screen.width-dx)/2) + ',top=' + ((screen.height-dy)/2) + ',screenX=' + ((screen.width-dx)/2) + ',screenY=' + ((screen.height-dy)/2) + ''
	popupwindow = window.open(URL, name, sOptions);
	return popupwindow;
}

function submitToPopUp(action, dx, dy) {
	popUp('','popupwindow',dx,dy);
	document.docmgt.action="index.php?action="+action;
	document.docmgt.submit();
}

function submitToMain(action) {
	document.docmgt.target = window.opener ? window.opener.name : "";
	document.docmgt.action = action;
	document.docmgt.submit();
}

function checkRemove(type, message, item) {
	switch (type) {
		case "resource":
			if (confirm(message)) {
				submitToMain('index.php?action=removeresource');
			}
			break;
		case "item":
		case "page":
		case "image":
			if (confirm(message)) {
				return false;
			} else {
				return true;
			}
			break;
	}
}

function addConfirmToLinks(sMessage){
	if (bDone == false) {
		// links
		for (var a=0; a<document.links.length; a++) {
			if (A = document.links[a]) {
				if (isItemInHtmlarea(A)) {
					//aert(A.href)
				} else {
					sOldHref = A.href
					// Do not do anything if it is a mailto: link (found at index 0)
					if (A.href.search('mailto:')) {
						A.href = "javascript:doConfirm('"+sOldHref+"', '"+sMessage+"')";
					} else {
						// do nothing
					}
				}
			}
		}
		// forms
		for (var a=0; a<document.forms.length; a++) {
			if (oForm = document.forms[a]) {
				if (isItemInHtmlarea(oForm)){
					// do nothing
				} else {
					// @todo not ns compatible, slashes
					oForm.onsubmit = function(event) { return confirm(sMessage);}
				}
			}
		}
	}
	enable(document.getElementById('savebutton'));
	enable(document.getElementById('savebutton_footer'));
	bDone = true
}

function doConfirm(sOldHref, sMessage) {
	if (confirm(sMessage)) {
		window.location = sOldHref;
	}
}

function isItemInHtmlarea(oLink) {
	var elem = oLink
	while (elem.name != "htmlarea" && elem.name != "htmlarea2") {
		elem = elem.parentElement
		if (elem == null)
			break
	}
	if (elem) {
		return true
	}
}

/**
* Toolbar button functions
*/
function selOn(ctrl) {
	if (ctrl && ctrl.disabled != true) {
		ctrl.className = "selOn";
	}
}

function selOff(ctrl) {
	if (ctrl) {
		ctrl.className = "selOff";
	}
}

function selDown(ctrl) {
	if (ctrl && ctrl.disabled != true) {
		ctrl.className = "selDown";
	}
}

function selUp(ctrl) {
	if (ctrl && ctrl.disabled != true) {
		ctrl.className = "selUp";
	}
}

function disable(ctrl) {
	if (ctrl && !ctrl.disabled) {
		ctrl.style.color = "graytext";
		//if (!ctrl.onclicktemp && ctrl.onclick != "") {
			ctrl.onclicktemp = ctrl.onclick
			ctrl.onclick = "";
		//}
		ctrl.disabled = true;
		// check if the control contains an image
		makeGray(ctrl, true);
	}
}

function enable(ctrl) {
	if (ctrl) {
		ctrl.style.color = "menutext";
		if (ctrl.onclicktemp && ctrl.onclick == "") {
			ctrl.onclick = ctrl.onclicktemp
		}
		ctrl.disabled = false;
		// check if the control contains a images
		makeGray(ctrl, false);
	}
}

function makeGray(el,b) {
	var sInter;
	var sPath = "";

	sInter = (b) ? "disabled/disabled_" : "";

	var imgs = findChildren(el, "tagName", "IMG");
	for (var i=0; i<imgs.length; i++) {
		//imgs[i].style.filter = filtval;
		if (b) {
			if (!imgs[i].orig_src) {
				// todo check if img is disabled
				sFull = imgs[i].orig_src = imgs[i].src;
				aFull = sFull.split('/');
				sFilename = aFull[aFull.length-1];
				for (var j=0; j < (aFull.length-1); j++) {
					sPath = sPath + aFull[j]+"/";
				}
				imgs[i].src = sPath+sInter+sFilename;
			}
		} else {
			if (imgs[i].orig_src) {
				imgs[i].src = imgs[i].orig_src;
				imgs[i].orig_src = null;
			}
		}
	}
}

function findChildren(el, type, value) {
	var children = el.childNodes;
	var tmp = new Array();
	if (!children || !children.length) {
		return tmp
	}
	var j=0;

	for (var i=0; i<children.length; i++) {
		if (eval("children[i]." + type + "==\"" + value + "\"")) {
			tmp[tmp.length] = children[i];
		}
		tmp = tmp.concat(findChildren(children[i], type, value));
	}
	return tmp;
}

/**
* Toolbar pulldown menu
*/
function menuOn(ctrl) {
	if (ctrl) {
		ctrl.className = "menuOn";
	}
}

function menuOff(ctrl) {
	if (ctrl) {
		ctrl.className = "menuOff";
	}
}

function menuDown(ctrl) {
}

function menuUp(ctrl) {
}

function showMenu(oMenu, oButton)
{
	var iZIndex;
	var iBorder = 1;
	iZIndex = 150

	//var oBody = document.getElementsByTagName("BODY").item(0);
	var oBody = document.getElementById('body');

	// @todo hide the other open menu's?
	//oMenu.style.display  = 'block'
	oMenu.style.display = 'inline';
	oMenu.style.position = 'absolute'
	iPosition            = getPosition(oButton);
	oMenu.style.left     = iPosition[0];
	oButton.height       = 20;
	oMenu.style.top      = iPosition[1] + oButton.height;
	oMenu.style.zIndex = iZIndex;
	if (oMenu.id != 'filter_form') {
		oBody.appendChild(oMenu);
	}

	// disable the <select>s and other objects that are covered by the menu
	var oIframe = document.getElementById(oMenu.id + '_iFrame');
	if (!oIframe) {
		oIframe = document.createElement('iframe');
		oIframe.setAttribute("id", oMenu.id + '_iFrame');
		oIframe.style.border = 0;
		oIframe.width  = 0;
		oIframe.height = 0;
		oIframe.style.position = "absolute";
		oIframe.style.visibility = 'hidden';
		if (oBody) {
			oBody.appendChild(oIframe);
		}
	}
	if (oIframe) {
		iHeight = oMenu.clientHeight + 2*iBorder;
		iWidth  = oMenu.clientWidth  + 2*iBorder;
		oIframe.height        = parseInt(iHeight);
		oIframe.width         = parseInt(iWidth);
		oIframe.style.top     = iPosition[1] + oButton.height;
		oIframe.style.left    = iPosition[0]
		oIframe.style.visibility = 'visible';
		oIframe.style.zIndex  = iZIndex - 1;
	}
}

function hideMenu(oMenu)
{
	if (oMenu) {
		oMenu.style.display='none'
		oIframe = document.getElementById(oMenu.id + '_iFrame');
		if (oIframe) {
			oIframe.style.visibility = 'hidden';
		}
	}
}

function toggleMenu(sDropMenu, oButton)
{
	//window.event.cancelBubble = true;
	document.onclick   = onclickhandler
	sMenu              = sDropMenu;
	oMenu              = document.getElementById(sDropMenu)
	if (oMenu.style.display=='none' || !oMenu.style.display) {
		showMenu(oMenu, oButton)
	} else {
		hideMenu(oMenu)
	}
}

function onclickhandler(event) {
	if (sMenu) {
		hideMenu(document.getElementById(sMenu));
	}
}

function getPosition(oButton) {
	var posX = 0
	var posY = 0
	if( oButton.offsetParent ) {
		for ( var posX = 0, posY = 0; oButton.offsetParent; oButton = oButton.offsetParent ) {
			posX += oButton.offsetLeft;
			posY += oButton.offsetTop;
		}
		return [ posX, posY ];
	} else {
		if ( !oButton.x && !oButton.y ) {
			return [0,0];
		} else {
			return [ oButton.x, oButton.y ];
		}
	}
}

function printWindow()
{
	bV = parseInt(navigator.appVersion)
	if (bV >= 4) {
		window.print()
	}
}

function selectParagraph(sId)
{
	if (getCookie('adminmode') == 'true') {
		if (document.getElementById('toolbardiv' + sId)) {
			document.getElementById('toolbardiv' + sId).style.display = 'inline';
			if (getCookie('allpara') != 'true') {
				document.getElementById('pc_paragraph'+sId).className = 'SelectParagraph';
			}
		}
	}
}

function unselectParagraph(sId)
{
	if (
		document.getElementById('toolbardiv'+sId)
	    && (getCookie('allpara') != 'true' || getCookie('adminmode') != 'true')
	) {
		document.getElementById('toolbardiv'+sId).style.display = 'none';
		document.getElementById('pc_paragraph'+sId).className = 'UnselectParagraph';
	}
}

function toggleAllPara()
{
	var bAllPara
	if (getCookie('allpara') == 'true') {
		setAllPara('false')
		document.getElementById('allpara').className = 'selOff'
	} else {
		setAllPara('true')
		document.getElementById('allpara').className = 'selDown'
	}
}

function setAllPara(bAllPara)
{
	if (bAllPara == 'true') {
		setCookie('allpara', 'true', 7);
		setParaIds(aParaIds);
	} else {
		removeCookie('allpara');
		setParaIds(aParaIds);
	}
}

function setParaIds(aParaIds)
{
	bShow = isShowAll()
	for (var i = 0; i < aParaIds.length; i++) {
		if (bShow == true && getCookie('adminmode') == 'true') {
			selectParagraph(aParaIds[i])
		} else {
			unselectParagraph(aParaIds[i])
		}
	}
}

function isShowAll()
{
	if (getCookie('allpara') == 'true' && getCookie('adminmode') == 'true') {
		bShow = true;
		if (document.getElementById('allpara')) {
			document.getElementById('allpara').className = 'selDown'
		}
	} else {
		bShow = false;
		if (document.getElementById('allpara')) {
			document.getElementById('allpara').className = 'selOff'
		}
	}
	return bShow
}

function setAdminMode(bAdminMode)
{
	oMenu = document.getElementById('toptoolbar')
	if (oMenu) {
		if (bAdminMode == 'true') {
			oMenu.style.display = 'block'
			setCookie('adminmode', 'true', 7);
		} else {
			oMenu.style.display='none'
			removeCookie('adminmode');
		}
	}
	oAddParagraph = document.getElementById('addparagraphtoolbar')
	if (oAddParagraph) {
		if (bAdminMode == 'true') {
			oAddParagraph.style.display = 'block'
		} else {
			oAddParagraph.style.display='none'
		}
	}
	setParaIds(aParaIds);
}

function toggleAdminMode()
{
	if (getCookie('adminmode') == 'true') {
		setAdminMode('false')
	} else {
		setAdminMode('true')
	}
}

// Cookie handling
function setCookie( sName, sValue, nDays ) {
	var expires = "";
	if ( nDays ) {
		var d = new Date();
		d.setTime( d.getTime() + nDays * 24 * 60 * 60 * 1000 );
		expires = "; expires=" + d.toGMTString();
	}
	document.cookie = sName + "=" + sValue + expires + "; path=/";
};

function getCookie(sName) {
	var re  = new RegExp( "(\;|^)[^;]*(" + sName + ")\=([^;]*)(;|$)" );
	var res = re.exec( document.cookie );
	return res != null ? res[3] : null;
};

function removeCookie( name ) {
	setCookie( name, "", -1 );
};





// Cookie Functions  ////////////////////  (:)

// Set the cookie.
// SetCookie('your_cookie_name', 'your_cookie_value', exp);

// Get the cookie.
// var someVariable = GetCookie('your_cookie_name');

// <body onload="cookieForms('open', 'yourform')" onunload="cookieForms('save', 'yourform')">

var expDays = 100;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) { endstr = document.cookie.length; }
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}

function SetCookie (name, value) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}

// cookieForms saves form content of a page.


// use the following code to call it:
//  <body onLoad="cookieForms('open', 'form_1', 'form_2', 'form_n')" onUnLoad="cookieForms('save', 'form_1', 'form_2', 'form_n')">

// It works on text fields and dropdowns in IE 5+
// It only works on text fields in Netscape 4.5


function cookieForms() {
	var mode = cookieForms.arguments[0];

	for(f=1; f<cookieForms.arguments.length; f++) {
		formName = cookieForms.arguments[f];

		if(mode == 'open') {
			cookieValue = GetCookie('saved_'+formName);
			if(cookieValue != null) {
				var cookieArray = cookieValue.split('#cf#');

				if(cookieArray.length == document[formName].elements.length) {
					for(i=0; i<document[formName].elements.length; i++) {

						if(cookieArray[i].substring(0,6) == 'select') { document[formName].elements[i].options.selectedIndex = cookieArray[i].substring(7, cookieArray[i].length-1); }
						else if((cookieArray[i] == 'cbtrue') || (cookieArray[i] == 'rbtrue')) { document[formName].elements[i].checked = true; }
						else if((cookieArray[i] == 'cbfalse') || (cookieArray[i] == 'rbfalse')) { document[formName].elements[i].checked = false; }
						else { document[formName].elements[i].value = (cookieArray[i]) ? cookieArray[i] : ''; }
					}
				}
			}
		}

		if(mode == 'save') {
			cookieValue = '';
			for(i=0; i<document[formName].elements.length; i++) {
				fieldType = document[formName].elements[i].type;

				if(fieldType == 'password') { passValue = ''; }
				else if(fieldType == 'checkbox') { passValue = 'cb'+document[formName].elements[i].checked; }
				else if(fieldType == 'radio') { passValue = 'rb'+document[formName].elements[i].checked; }
				else if(fieldType == 'select-one') { passValue = 'select'+document[formName].elements[i].options.selectedIndex; }
				else { passValue = document[formName].elements[i].value; }

				cookieValue = cookieValue + passValue + '#cf#';
			}
			cookieValue = cookieValue.substring(0, cookieValue.length-4); // Remove last delimiter

			SetCookie('saved_'+formName, cookieValue, exp);
		}
	}
}

