//CS1.1
/*
================================================================================
* GLOBAL VARIABLES
================================================================================
* Browser identification
********************************************************************************
*/

var exclude=1;
var agt=navigator.userAgent.toLowerCase();
var win=0;var mac=0;var lin=1;
if(agt.indexOf('win')!=-1){win=1;lin=0;}
if(agt.indexOf('mac')!=-1){mac=1;lin=0;}
var lnx=0;if(lin){lnx=1;}
var ice=0;
var ie=0;var ie4=0;var ie5=0;var ie6=0;var com=0;var dcm;
var op5=0;var op6=0;var op7=0;
var ns4=0;var ns6=0;var ns7=0;var mz7=0;var kde=0;var saf=0;
if(typeof navigator.vendor!="undefined" && navigator.vendor=="KDE"){
	var thisKDE=agt;
	var splitKDE=thisKDE.split("konqueror/");
	var aKDE=splitKDE[1].split("; ");
	var KDEn=parseFloat(aKDE[0]);
	if(KDEn>=2.2){
		kde=1;
		ns6=1;
		exclude=0;
		}
	}
else if(agt.indexOf('webtv')!=-1){exclude=1;}
else if(typeof window.opera!="undefined"){
	exclude=0;
	if(/opera[\/ ][5]/.test(agt)){op5=1;}
	if(/opera[\/ ][6]/.test(agt)){op6=1;}
	if(/opera[\/ ][7-9]/.test(agt)){op7=1;}
	}
else if(typeof document.all != "undefined"&&!kde){
	exclude=0;
	ie=1;
	if(typeof document.getElementById != "undefined"){
		ie5=1;
		if(agt.indexOf("msie 6")!=-1){
			ie6=1;
			dcm=document.compatMode;
			if(dcm!="BackCompat"){com=1;}
			}
		}
	else{ie4=1;}
	}
else if(typeof document.getElementById!="undefined"){
	exclude=0;
	if(agt.indexOf("netscape/6")!=-1||agt.indexOf("netscape6")!=-1){ns6=1;}
	else if(agt.indexOf("netscape/7")!=-1||agt.indexOf("netscape7")!=-1){ns6=1;ns7=1;}
	else if(agt.indexOf("gecko")!=-1){ns6=1;mz7=1;}
	if(agt.indexOf("safari")!=-1 || (typeof document.childNodes!="undefined" && typeof document.all=="undefined" && typeof navigator.taintEnabled=="undefined")){mz7=0;ns6=1;saf=1;}
	}
else if((agt.indexOf('mozilla')!=-1)&&(parseInt(navigator.appVersion)>=4)){
	exclude=0;
	ns4=1;
	if(typeof navigator.mimeTypes['*']=="undefined"){
		exclude=1;
		ns4=0;
		}
	}
if(agt.indexOf('escape')!=-1){exclude=1;ns4=0;}
if(typeof navigator.__ice_version!="undefined"){exclude=1;ie4=0;}


/*
================================================================================
* Onlick counter. 
called by setCookieOnClick()
********************************************************************************
*/

var onClickCounter=0;



/**
* Pops up a new window in the middle of the screen
*/

	function popupWindow(mypage, myname, w, h, scroll) {
		var winl = (screen.width - w) / 2;
		var wint = (screen.height - h) / 2;
		winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable';
		win = window.open(mypage, myname, winprops);
		if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
	}

	function replaceAll(str, from, to) {
		var idx = str.indexOf(from);
		while(idx > -1) {
			str = str.replace(from, to);
			idx = str.indexOf(from);
		}
			return str;
	}
	
	function openloginwindow() {
		var name = document.forms[0].name.value;

		//if(name!="mike" && name!="derek") {
		//	alert('Sorry but you cannot login at the moment. The website is currently still under development and the editing system is not yet complete.');
		//	return;
		//}
		
		var pass = document.forms[0].pass.value;
		var x = (screen.width-410)/2;
		var y = (screen.height-600)/2;
		helpWindow = window.open('login.jsp?name='+name+'&pass='+pass,'login_window','height=300,width=400,resizable,screenX='+x+',screenY='+y);
		helpWindow.moveTo(x,y);
	}

	function removeitemwindow(timelineLink, itemId, position) {
		inputBox = confirm("Are you sure you with to delete this item from this timeline?");
		if (inputBox) {
			var x = (screen.width-410)/2;
			var y = (screen.height-310)/2;
			helpWindow = window.open('removeitem.jsp?timeline='+timelineLink+'&itemid='+itemId+'&position='+position,'remove_item_window','height=300,width=400,scrollbars=no,resizable,screenX='+x+',screenY='+y);
			helpWindow.moveTo(x,y);
		}
	}

	function reloadwithchangedparameter(parameter, oldvalue, newvalue) {
		var href = document.location.href;
		
		var pos = href.indexOf(parameter);
		var qpos= href.indexOf('?');
		
		if(pos == -1) {
			if(qpos == -1) {
				document.location.href = href+'?'+parameter+'='+newvalue;
			}
			else {
				document.location.href = href+'&'+parameter+'='+newvalue;
			}
		}
		else {
			document.location.href = href.replace(parameter+'='+oldvalue, parameter+'='+newvalue);
		}
	}

	function opencommentswindow(itemId) {
		var x = (screen.width-520)/2;
		var y = (screen.height-520)/2;
		helpWindow = window.open('showcomments.jsp?item='+itemId,'comments_window','height=510,width=510,resizable,screenX='+x+',screenY='+y);
		helpWindow.moveTo(x,y);
	}
	
		
	function openemailtofriendwindow() {
		var docTitle = window.document.title;
		var url = window.location.href;
		var anchor = "";
		if (url.indexOf("#") != -1) {
			var pos = url.indexOf("#");
			url = url.substring(0, pos)
			anchor = url.substring(pos, url.length);
		}
			
		var x = (screen.width-520)/2;
		var y = (screen.height-500)/2;
		helpWindow = window.open('emailfriend.jsp?url=' + url + '&title=' + docTitle + anchor,'email_friend_window','height=490,width=510,resizable,screenX='+x+',screenY='+y);
		helpWindow.moveTo(x,y);
	}
	
/*
 Son of MultiSelect
 coded by Kae - kae@verens.com
 http://verens.com
*/

/* edit these variables to customise the multiselect */ {
	var show_toplinks=false;
	}

	/* global variables - do not touch */ {
	var isIE=window.attachEvent?true:false;
	var selectDefaults=[];
	}
	function addEvent(el,ev,fn){
		if(isIE)el.attachEvent('on'+ev,fn);
		else if(el.addEventListener)el.addEventListener(ev,fn,false);
	}

	// to add fancy multiselect to your page, add the function to the onload
	// or add the following script to the page
	// addEvent(window,'load',buildMultiselects);
	//Multiselects should be given the id ms[0-9], i.e., ms1, ms2, ms3, etc
	
	//todo= make this serverside. shouldn't be too hard
	function buildMultiselects(){
		do{
			var found=0;
			var a=document.getElementsByTagName('select');
			var b;
			for(b=0;b<a.length,!found;b++){
				if (b>=a.length)break;
					var enabled=/ms[1-9]+/;
					var msId=a[b].id;
					if (msId.match(enabled) || msId == 'multiSelect') {
						var ms=a[b];
						if(ms==null)break;
						var name=ms.name.substring(0,ms.name.length-2);
						var height=document.getElementById(msId).style.height;
						if(ms.multiple){
							/* common variables */ {
								selectDefaults[name]=[];
								var found=1;
								var disabled=ms.disabled?1:0
								var width=100;
								/*if(width<120)width=100;*/
								/* if(height<60)height=60; */
							}
							/* set up wrapper */ {
								var wrapper=document.createElement('div');
								wrapper.style.width=width+"%";
								wrapper.style.height=height;
								wrapper.style.position='relative';
								wrapper.style.border="2px solid #000";
								wrapper.style.borderColor="#333 #ccc #ccc #333";
								wrapper.style.font="10px sans-serif";
								wrapper.id=msId+"wrapper";
							}
							if(show_toplinks){ /* reset, all, none */
								wrapper.appendChild(newLink("javascript:"+(disabled?"alert('selection disabled')":"multiselect_selectall('"+name+"','checked');"),'all'));
								wrapper.appendChild(document.createTextNode(', '));
								wrapper.appendChild(newLink("javascript:"+(disabled?"alert('selection disabled')":"multiselect_selectall('"+name+"','');"),'none'));
								wrapper.appendChild(document.createTextNode(', '));
								wrapper.appendChild(newLink("javascript:"+(disabled?"alert('selection disabled')":"multiselect_selectall('"+name+"','reset');"),'reset'));
							}
							/* setup multiselect */ {
								var newmultiselect=document.createElement('div');
								newmultiselect.style.position='absolute';
								newmultiselect.style.top=show_toplinks?'15px':'0';
								newmultiselect.style.left='0';
								newmultiselect.style.overflow='auto';
								newmultiselect.style.width=(isIE?width-4:width)+"%";
								newmultiselect.style.height=height;
								newmultiselect.id="ms_"+ms.name;
							}
							var c=ms.getElementsByTagName('option');
							var d=0;
							for(d=0;d<c.length;d++){
								var label=document.createElement('label');
								label.style.display="block";
								label.style.border="1px solid #eee";
								label.style.borderWidth="1px 0";
								label.style.font="12px 'Trebuchet MS'";
								label.style.lineHeight="14px";
								label.style.paddingLeft="20px";
								var checkbox=document.createElement('input');
								checkbox.type="checkbox";
								if(c[d].selected){
									checkbox.checked="checked";
									checkbox.defaultChecked=true;
								}
								if(c[d].disabled){
									checkbox.style.display='none';
									label.style.fontWeight='bold';
									label.style.marginLeft='-15px';
									label.style.marginTop='8px';
									label.style.marginBottom='2px';
								}
								if(c[d].id){
									label.style.fontWeight='bold';
								}
								selectDefaults[name][d]=c[d].selected?'checked':'';
								if(disabled)checkbox.disabled="disabled";
								checkbox.value=c[d].value;
								checkbox.style.marginLeft="-16px";
								checkbox.style.marginTop="-2px";
								checkbox.name=ms.name;
		
								// escape the label
								var text=c[d].innerHTML;
								text=text.replace(/\&nbsp;?/g,' ');
								text=text.replace(/\&lt;?/g,'<');
								text=text.replace(/\&gt;?/g,'>');
		
								var labelText=document.createTextNode(text);
								label.appendChild(checkbox);
								label.appendChild(labelText);
								newmultiselect.appendChild(label);
							}
							wrapper.appendChild(newmultiselect);
							ms.parentNode.insertBefore(wrapper,ms);
							ms.parentNode.removeChild(ms);
						}
					}
			} 
		} while(found);
	} 
	
	function thereAreSonsOfAMultiSelect() {
		var divs = document.getElementsByTagName("div");
		var debugMess = "There are "+divs.length+" divs on this page.\n";
		if (divs.length>0) {
			var i=0;
			var j=0;
			var k=0;
			for (i=0;i<divs.length;i++) {
				if (divs[i].id!=null) {
					j++;
					var thisSonOfAMultiSelect=divs[i];
					var msID=thisSonOfAMultiSelect.id;
					regex = /ms[0-9]wrapper/;
					if (msID.match(regex) || msID=="multiSelect") {
						k++;
					}
				}
			}
		}
		if (k>0)
			return true;
		else
			return false;
	}
	
	function getInputValueFromCheckBoxes(name) {
		var checkboxes = document.getElementsByName(name);
		var selectedCheckBoxes= new Array;
		var i=0;
		for (i=0;i<checkboxes.length;i++) {
			if (checkboxes[i].checked==true) {
				selectedCheckBoxes.push(checkboxes[i].value);
			}
		}
		if (selectedCheckBoxes.length!=0)
			return selectedCheckBoxes;
		else
			return null;
	}
	
	function multiselect_selectall(name,val){
		var els=document.getElementsByTagName('input'),found=0;
		for(var i=0;i<els.length;++i){
			if(els[i].name==name+'[]' || els[i].name==name)els[i].checked=val=='reset'?selectDefaults[name][found++]:val;
		}
	}
	
	function newLink(href,text){
		var e=document.createElement('a');e.href=href;e.appendChild(document.createTextNode(text));return e;
	}


	function writeHTML(elementid,content) {
		if (document.getElementById && !document.all){
			rng = document.createRange();
			el = document.getElementById(elementid);
			rng.setStartBefore(el);
			htmlFrag = rng.createContextualFragment(content);
			while (el.hasChildNodes())
			el.removeChild(el.lastChild);
			el.appendChild(htmlFrag);
		}
		else {
			document.getElementById(elementid).innerHTML=content;
		}
	}
	
	function thisIsEditableOn(divId) {
		document.getElementById(divId).style.border="1px solid #ccc";
	}
	
	function thisIsEditableOff(divId) {
		document.getElementById(divId).style.border="1px solid #fff";
	}
	function sHID(htmlPos){
		var sHtmlPos = "" + htmlPos;
		if (document.getElementById(sHtmlPos) != null) {
			var htmlElement = document.getElementById(sHtmlPos);
			htmlElement.style.display = "";
		}
	}
	
	/* 
	 * function		to hide the edit, history, delete, etc. links
	 * @param 		tlPos 	the number of the timeline item so the function know where to change the display
	 * @TODO: 		make more generic
	 */
	function hHID(htmlPos){
		var sHtmlPos = "" + htmlPos;
		if (document.getElementById(sHtmlPos) != null){
			var htmlElement = document.getElementById(sHtmlPos);
			htmlElement.style.display = "none";
		}
	}

	/* 
	 * function		to disable the edit, history, delete, etc. links
	 * @param 		tlPos 	the number of the timeline item so the function know where to change the display
	 */
	function dHID(htmlPos){
		var sHtmlPos = "" + htmlPos;
		var htmlElement = document.getElementById(sHtmlPos);
		
		if (htmlElement != null)
			htmlElement.style.display = "disable";
	}

	/* 
	 * function		to enable the edit, history, delete, etc. links
	 * @param 		tlPos 	the number of the timeline item so the function know where to change the display
	 */
	function eHID(htmlPos){
		var sHtmlPos = "" + htmlPos;
		var htmlElement = document.getElementById(sHtmlPos);
		
		if (htmlElement != null)
			htmlElement.style.display = "enable";
	}

var defaultFontSize = 90;
var currentFontSize = defaultFontSize;
//taken from joomla
function changeFontSize(sizeDifference){
	currentFontSize = parseInt(currentFontSize) + parseInt(sizeDifference * 6);

	if(currentFontSize > 108){
		currentFontSize = 108;
	}else if(currentFontSize < 72){
		currentFontSize = 72;
	}

	setFontSize(currentFontSize);
};

function setFontSize(fontSize){
	if (currentFontSize == 100)
	document.getElementById('contentPane').style.fontSize = '.9em';
	else
	document.getElementById('contentPane').style.fontSize = fontSize + '%';
};

function writeHTML(elementid, content) {
	if (document.getElementById && !document.all){
		rng = document.createRange();
		el = document.getElementById(elementid);
		rng.setStartBefore(el);
		htmlFrag = rng.createContextualFragment(content);
		while (el.hasChildNodes())
			el.removeChild(el.lastChild);
		el.appendChild(htmlFrag);
	}
	else {
		document.getElementById(elementid).innerHTML=content;
	}
}

function toggle(obj, toggleLink) {
	var el = document.getElementById(obj);
	var toggleLink = document.getElementById(toggleLink);
	el.style.display = (el.style.display != 'none' ? 'none' : '' );
	toggleLink.className = (el.style.display != 'none' ? 'toggleOpen' : 'toggleClosed' );
}


function setCookieOnFirstClick(nameAndValue) {
	if (onClickCounter==0) {
		setCookie(nameAndValue);
		onClickCounter++;
	}
}

function setCookie(nameAndValue) {
	document.cookie = nameAndValue;
}

/**
 * Supresses error warning
 * Use sparingly and list where it's used
 */
 
function ignoreError(msg,url,l) {
	return true;
}

function capitalize(string) {
	capitalize(string, " ");
}

function capitalize(string, delimiter) {
	var val = string;
	var newVal = '';
	val = val.split(delimiter);
	for(var c=0; c < val.length; c++) {
		newVal += val[c].substring(0,1).toUpperCase() +
		val[c].substring(1,val[c].length) + ' ';
	}
	return newVal;
}

/* 
 * function	to disable the edit, history, delete, etc. links
 * @param tlPos the number of the timeline item so the function know where to change the display
 */
function disableField(id){
	document.getElementById(id).disabled = 'disabled';
}

/* 
 * function	to disable the edit, history, delete, etc. links
 * @param tlPos the number of the timeline item so the function know where to change the display
 */
function enableField(id){
	document.getElementById(id).disabled = false;
}

/* 
 * function	to copy text to a clipboard
 * @param text2copy the text to copy to clipboard
 */
function copy(text2copy) {
  if (window.clipboardData) {
	window.clipboardData.setData("Text",text2copy);
  } else {
	var flashcopier = 'flashcopier';
	if(!document.getElementById(flashcopier)) {
	  var divholder = document.createElement('div');
	  divholder.id = flashcopier;
	  document.body.appendChild(divholder);
	}
	document.getElementById(flashcopier).innerHTML = '';
	var divinfo = '<embed src="/lib/scripts/flash/_clipboard.swf" FlashVars="clipboard=' + escape(text2copy) + '" width="0" height="0" type="application/x-shockwave-flash"></embed>';
	document.getElementById(flashcopier).innerHTML = divinfo;
  }
}
