//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// color processing

var colHexChars=new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");

// color object creation
// creates handle on color bar and processes it
// concats name of fields/objects by given object name and part (R,G,B)
// needs prototype and scripttacilous framework
// params: name of slider id, name of handle id, name of color field id, current value integer 0-255
function col_CreateSliderObject(obj,part,initVal) {
	// create slider object
	curObject = new Control.Slider($(obj+'_colSlider'+part).down('.handle'), $(obj+'_colSlider'+part), {
		range: $R(0,255),
		sliderValue: initVal,
		onSlide: function(value) { $(obj+'_colPartField'+part).value=Math.round(value); col_UpdatePreview(obj); },
		onChange: function(value) { $(obj+'_colPartField'+part).value=Math.round(value); col_UpdatePreview(obj); }
	});	
	
	// set init value to field and preview
	$(obj+'_colPartField'+part).value=initVal;
	col_UpdatePreview(obj);
	
	// return object
	return(curObject);
}

// color field handling
// checks input in given color field and processes it accordingly
// concats name of fields/objects by given object name and part (R,G,B)
// needs prototype and scripttacilous framework
function col_SetSliderByPartFieldVal(obj,part) {
	var curVal=parseInt($(obj+'_colPartField'+part).value);		// get current val from field and parse it
	if (isNaN(curVal)) curVal=0;								// if not a number, set to 0
	if (curVal<0) curVal=0;										// if smaller 0, set to 0
	if (curVal>255) curVal=255;									// if bigger 255, set to 255
	var colControl=eval(obj+'_colControlSlider'+part);
	colControl.setValue(curVal);								// set slider position
}

// color field handling in hex
// checks input of hex color field and processes it
// if error occurs, the hex code is corrected by decimal values
// needs prototype and scripttacilous framework
function col_SetSliderByHexFieldVal(obj) {
	var curVal=$(obj+'_colHexField').value;								// get entry from field
	curVal=curVal.toUpperCase();										// make string uppercase
	
	// check on errors
	if (curVal.length!=6) { col_UpdatePreview(obj); return; }			// if size doesn't match, reset and break function
	var cntMatch=0;
	for (i=0;i<6;i++) {													// check each individual char if it is correct
		for (x=0;x<colHexChars.length;x++) {
			if (curVal.substring(i,i+1)==colHexChars[x]) {
				cntMatch++;
				break;
			} // end if
		} // end for x
	} // end for i
	
	if (cntMatch!=6) {													// on mismatch correct hex code and break
		col_UpdatePreview(obj);
	} else {
		var colControlR=eval(obj+'_colControlSliderR');					// get target object
		var colControlG=eval(obj+'_colControlSliderG');
		var colControlB=eval(obj+'_colControlSliderB');
		colControlR.setValue(misc_ConvHexToDec(curVal.slice(0,2)));		// set new values in fields and update bars
		colControlG.setValue(misc_ConvHexToDec(curVal.slice(2,4)));
		colControlB.setValue(misc_ConvHexToDec(curVal.slice(4,6)));
	} // end if error found
}

// sets background color of preview element based on field values
// concats name of fields/objects by given object name
// needs prototype and scripttacilous framework
function col_UpdatePreview(obj) {
	var colRd=$(obj+'_colPartFieldR').value;							// get values from fields
	var colGd=$(obj+'_colPartFieldG').value;
	var colBd=$(obj+'_colPartFieldB').value;
	var colR=misc_ConvDecToHex(colRd);								// convert values to hex
	var colG=misc_ConvDecToHex(colGd);
	var colB=misc_ConvDecToHex(colBd);

	// concat hex value
	if (colR.length==1) colR="0"+colR;									// add missing zeros if needed
	if (colG.length==1) colG="0"+colG;
	if (colB.length==1) colB="0"+colB;
	var hexVal=colR+colG+colB;
	
	// set background color of preview element
	$(obj+'_colPreview').setStyle({ backgroundColor: "#"+hexVal });
	// set content of hex field
	$(obj+'_colHexField').value=hexVal;
	// set text color in preview field to adjust contrast (if 2 of 3 color chanels are bigger 130 decimal)
	var contrastCount=0;
	var prevTextColor="#fff";
	if (colRd>130) contrastCount++;
	if (colGd>130) contrastCount++;
	if (colBd>130) contrastCount++;
	if (contrastCount>=2) prevTextColor="#000";
	$(obj+'_colPreview').setStyle({ color: prevTextColor });
}

// sets a predefined color (given as hex)
// needs obj to assign it correctly
// needs prototype and scripttacilous framework
function col_SetSelectedColor(obj,color) {
	var colControlR=eval(obj+'_colControlSliderR');
	var colControlG=eval(obj+'_colControlSliderG');
	var colControlB=eval(obj+'_colControlSliderB');
	colControlR.setValue(misc_ConvHexToDec(color.slice(0,2)));			// set new values in fields and update bars
	colControlG.setValue(misc_ConvHexToDec(color.slice(2,4)));
	colControlB.setValue(misc_ConvHexToDec(color.slice(4,6)));
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// calender processing

var cal_DaysInMonths=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var cal_MonthNames=new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var cal_DayNames=new Array('Mon','Tue','Wed','Thu','Fri','Sat','Sun');

// check if given year is a leap year
function cal_IsLeapYear(inputYear) {
	if(inputYear%400==0||(inputYear%4==0&&inputYear%100!=0)) return true;
	return false;
}

// corrects conten of fields on input error
// set everything to zero if any error occured
// needs object name of calender element and mode flag (0=date only, 1=date and time, 2=time only (not possible))
function cal_CorrectFieldContent(obj,mode) {
	// check and correct input if needed
	var d=$(obj+'_calDayField').value;
	var m=$(obj+'_calMonthField').value;
	var y=$(obj+'_calYearField').value;
	var h="00",i="00",s="00";
	if (mode>0) {
		h=$(obj+'_calHourField').value;
		i=$(obj+'_calMinuteField').value;
		s=$(obj+'_calSecondField').value;
	}
	
	// check empty fields
	if (d.length==0 || m.length==0 || y.length==0 || h.length==0 || i.length==0 || s.length==0) {
		cal_ResetFields(obj,mode);					// reset field content to 0
	} else {
		// convert to numbers and check on range
		d=Number(d); m=Number(m); y=Number(y);
		h=Number(h); i=Number(i); s=Number(s);
		if (isNaN(d) || isNaN(m) || isNaN(y) || isNaN(h) || isNaN(i) || isNaN(s)) {
			cal_ResetFields(obj,mode);					// reset field content to 0
		} else {
			// if values out of range
			if (d<0 || d>31 || m<0 || m>12 || y<0 || h<0 || h>23 || i<0 || i>59 || s<0 || s>59) cal_ResetFields(obj,mode);
			
			// if in date block is an logical error
			if ((d>0 && m==0) || (d==0 && m>0)) cal_ResetFields(obj,mode);
			
			// if year is set but not day and month
			if (d==0 && m==0 && y>0) cal_ResetFields(obj,mode);
		
			// work on year. has to be 4 digits long. If smaller digit is entered, value is changed to 2000+value (only if day and month are set)
			if (d>0 && m>0 && y<1000) $(obj+'_calYearField').value=(y+2000);
		}
	}
}

// create calender with all needed functionality
// needs object name of calender element and mode flag (0= date only, 1=date and time, 2=time only (not possible))
// checks the fields, on faulty imput it shows error msg
function cal_ShowCalender(obj,mode) {
	// check and correct field input if needed
	cal_CorrectFieldContent(obj,mode);

	// get content from fields
	var d=Number($(obj+'_calDayField').value);
	var m=Number($(obj+'_calMonthField').value);
	var y=Number($(obj+'_calYearField').value);
	var h=0,i=0,s=0;
	
	// get time part if set
	if (mode>0) {
		h=Number($(obj+'_calHourField').value);
		i=Number($(obj+'_calMinuteField').value);
		s=Number($(obj+'_calSecondField').value);
	}
	
	// if zero is defined get current date for calender generation
	if (d==0 || m==0 || y==0) {
		var now=new Date();
		cal_ShowElement(obj,now.getMonth(),now.getFullYear(),now.getDate(),now.getMonth(),now.getFullYear());
	} else {
		cal_ShowElement(obj,m-1,y,d,m-1,y);
	}
}

// makes the main part, outputs the calender block view
// needs object name and other params
// ATTENTION Month 0=January!!!
function cal_ShowElement(obj,viewMonth,viewYear,setDay,setMonth,setYear) {
	// vars
	var outputCode="";
	var prevMonth=-1,prevYear=-1;
	var nextMonth=-1,nextYear=-1;

	// output headline
	if (viewMonth>0) { prevMonth=viewMonth-1; prevYear=viewYear; } else { prevMonth=11; prevYear=viewYear-1; }
	if (viewMonth<11) { nextMonth=viewMonth+1; nextYear=viewYear; } else { nextMonth=0; nextYear=viewYear+1; }
	outputCode+='<div class="dayEmpty" onClick="javascript:cal_ShowElement(\''+obj+'\','+prevMonth+','+prevYear+','+setDay+','+setMonth+','+setYear+');"><img src="imgs/left.gif" alt="Previous Month" title="Previous Month" /></div>\n';
	outputCode+='<div class="monthInfo">'+cal_MonthNames[viewMonth]+' '+viewYear+'</div>\n';
	outputCode+='<div class="dayEmpty" onClick="javascript:cal_ShowElement(\''+obj+'\','+nextMonth+','+nextYear+','+setDay+','+setMonth+','+setYear+');"><img src="imgs/right.gif" alt="Next Month" title="Next Month" /></div>\n';
	
	// adjust number of februar days if we have a leap year
	if (cal_IsLeapYear(viewYear)==true) cal_DaysInMonths[1]+=1;

	// get weekday of 1.st of month
	var fom=new Date(viewYear,viewMonth,1);
	var fomIdx=fom.getDay();
	if (fomIdx==0) fomIdx=6; else fomIdx--;
	
	// concat week day heading
	for (i=0;i<=6;i++) {
		if (i>=5) cc="dayWeekend"; else cc="dayWeekday";
		outputCode+='<div class="'+cc+'"><b>'+cal_DayNames[i]+'</b></div>';
	} // end for i
	outputCode+='\n';
	
	// output empty fields if 1st of month is not a monday
	for (i=0;i<fomIdx;i++) outputCode+='<div class="dayEmpty"></div>\n';
	
	// output days of month
	curWeekday=fomIdx;
	for (i=1;i<=cal_DaysInMonths[viewMonth];i++) {
		if (curWeekday>=5) cc="dayWeekend"; else cc="dayWeekday";
		if (i==setDay && viewMonth==setMonth && viewYear==setYear) cc="daySelected";
		outputCode+='<div class="'+cc+'" onClick="javascript:cal_setFieldsToThis(\''+obj+'\','+i+','+(viewMonth+1)+','+viewYear+');" onMouseOver="javascript:cal_UpdateDayInfo(\''+obj+'\','+i+','+viewMonth+','+viewYear+');" onMouseOut="javascript:cal_UpdateDayInfo(\''+obj+'\','+setDay+','+setMonth+','+setYear+');">'+i+'</div>\n';
		if (curWeekday==6) curWeekday=0; else curWeekday++;
	}
	
	// output empty fields if end of month is not a sunday
	if (curWeekday>0) {
		for (i=curWeekday;i<=6;i++) outputCode+='<div class="dayEmpty"></div>\n';
	}
	
	// output info field
	outputCode+='<div class="dayInfo" id="'+obj+'_dayInfo"></div>\n';
	outputCode+='<div class="dayInfo">Go to <select id="'+obj+'_calMonthSelect" onChange="javascript:cal_SetToThisDate(\''+obj+'\','+setDay+','+setMonth+','+setYear+');">';
	for (i=0;i<cal_MonthNames.length;i++) {
		outputCode+='<option value="'+i+'"';
		if (i==viewMonth) outputCode+=' selected="selected"';
		outputCode+='>'+cal_MonthNames[i]+'</option>\n';
	}
	outputCode+='</select>\n<select id="'+obj+'_calYearSelect" onChange="javascript:cal_SetToThisDate(\''+obj+'\','+setDay+','+setMonth+','+setYear+');">';;
	for (i=viewYear-10; i<=viewYear+10; i++) {
		outputCode+='<option value="'+i+'"';
		if (i==viewYear) outputCode+=' selected="selected"';
		outputCode+='>'+i+'</option>\n';
	}
	outputCode+='</select>\n</div>\n';
	
	misc_WriteElementContent(obj+'_calBlock',outputCode);
	cal_UpdateDayInfo(obj,setDay,setMonth,setYear);
	if (!$(obj+'_calBlock').visible()) $(obj+'_calBlock').appear();
}

// set calender to selected month/year by dropdown box
// neds id and set dates to process
function cal_SetToThisDate(obj,d,m,y) {
	var vm=Number($(obj+'_calMonthSelect').value);
	var vy=Number($(obj+'_calYearSelect').value);
	cal_ShowElement(obj,vm,vy,d,m,y);
}

// update the info object (at end of calender)
// needs object name, day, month and year
function cal_UpdateDayInfo(obj,d,m,y) {
	var cont="";
	if (d<10) cont+="0";
	cont+=d+". "+cal_MonthNames[m]+" "+y;
	misc_WriteElementContent(obj+'_dayInfo',cont);
}

// hides the block if user clicks in a date field
// needs object name
function cal_HideCalender(obj) {
	if ($(obj+'_calBlock').visible()) $(obj+'_calBlock').fade();
}

// sets calender fields and closes calender
// needs object name, day, month and year
function cal_setFieldsToThis(obj,d,m,y) {
	cal_HideCalender(obj);
	d=String(d); m=String(m); y=String(y);
	if (d.length==1) d="0"+d;
	if (m.length==1) m="0"+m;
	$(obj+'_calDayField').value=d;
	$(obj+'_calMonthField').value=m;
	$(obj+'_calYearField').value=y;
}

// sets the date and time fields to current timestamp
// needs obj name and mode (0=date only, 1=date and time, 2=time only)
function cal_SetFieldsToNow(obj,mode) {
	// hide calender block (if needed)
	cal_HideCalender(obj);
	
	// get current timestamp and convert to mm/dd/yyyy hh:mm:ss
	var now=new Date();
	var v="";
	
	if (mode<=1) {
		v=String(now.getDate());
		if (v.length==1) v="0"+v;
		$(obj+'_calDayField').value=v;
		v=String(now.getMonth()+1);
		if (v.length==1) v="0"+v;
		$(obj+'_calMonthField').value=v;
		$(obj+'_calYearField').value=String(now.getFullYear());
	}
	
	if (mode>=1) {
		v=String(now.getHours());
		if (v.length==1) v="0"+v;
		$(obj+'_calHourField').value=v;
		v=String(now.getMinutes());
		if (v.length==1) v="0"+v;
		$(obj+'_calMinuteField').value=v;
		v=String(now.getSeconds());
		if (v.length==1) v="0"+v;
		$(obj+'_calSecondField').value=v;
	}
}

// resets the date and time fields
// needs obj name and mode (0=date only, 1=date and time, 2=time only)
function cal_ResetFields(obj,mode) {
	// hide calender block (if needed)
	cal_HideCalender(obj);

	if (mode<=1) {								// work on date
		$(obj+'_calDayField').value="00";
		$(obj+'_calMonthField').value="00";
		$(obj+'_calYearField').value="0000";
	}
	if (mode>=1) {								// work on time
		$(obj+'_calHourField').value="00";
		$(obj+'_calMinuteField').value="00";
		$(obj+'_calSecondField').value="00";
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// relation processing

// hides the block if user clicks in the link
// needs object name
function rel_HideSelector(obj) {
	if ($(obj+'_relSelector').visible()) $(obj+'_relSelector').fade();
}

// function checks if given element is selected
// needs object name and id of element
function rel_IsIdSelected(obj,id) {
	if ($(obj+'_relIdSelected'+id)!=null) return(true);
	return(false);
}

// function unsets all ids
// needs object name
function rel_UncheckAllIds(obj) {
	misc_WriteElementContent(obj+'_relHidden',"");
	misc_WriteElementContent(obj+'_relVisibleData',"No Relation defined");
	rel_ShowRelationSelectorData(obj);
}

// function sets all ids
// needs object name
function rel_CheckAllIds(obj) {
	var outputCode="";
	var outputCode2="";
	var data=eval(obj+'_relData');
	for (i=0;i<data.length;i++) {
		outputCode+='<input type="hidden" name="'+obj+'_relation[]" id="'+obj+'_relIdSelected'+data[i][0]+'" value="'+data[i][0]+'" />\n';
		outputCode2+=data[i][1]+" ";
	}
	misc_WriteElementContent(obj+'_relHidden',outputCode);
	misc_WriteElementContent(obj+'_relVisibleData',outputCode2);
	rel_ShowRelationSelectorData(obj);
}

// function unsets a clicked on id
// needs object name and id of element
function rel_UncheckThisId(obj,id) {
	var outputCode="";
	var outputCode2="";
	var data=eval(obj+'_relData');
	for (i=0;i<data.length;i++) {
		if (rel_IsIdSelected(obj,data[i][0]) && data[i][0]!=id) {
			outputCode+='<input type="hidden" name="'+obj+'_relation[]" id="'+obj+'_relIdSelected'+data[i][0]+'" value="'+data[i][0]+'" />\n';
			outputCode2+=data[i][1]+" ";
		}
	}
	
	if (outputCode2.length==0) outputCode2="No Relation defined"; 
	
	misc_WriteElementContent(obj+'_relHidden',outputCode);
	misc_WriteElementContent(obj+'_relVisibleData',outputCode2);
	rel_ShowRelationSelectorData(obj);
}

// function sets a clicked on id
// needs object name, id of element
function rel_CheckThisId(obj,id) {
	var outputCode="";
	var outputCode2="";
	var data=eval(obj+'_relData');
	for (i=0;i<data.length;i++) {
		if (rel_IsIdSelected(obj,data[i][0]) || data[i][0]==id) {
			outputCode+='<input type="hidden" name="'+obj+'_relation[]" id="'+obj+'_relIdSelected'+data[i][0]+'" value="'+data[i][0]+'" />\n';
			outputCode2+=data[i][1]+" ";
		}
	}
	misc_WriteElementContent(obj+'_relHidden',outputCode);
	misc_WriteElementContent(obj+'_relVisibleData',outputCode2);
	rel_ShowRelationSelectorData(obj);
}

// function shows possible relations based on pool
// needs object name for creation
function rel_ShowRelationSelectorData(obj) {
	// vars
	var outputCode="";
	var outputClass="";
	var data=eval(obj+'_relData');
	
	// output data elements
	for (i=0;i<data.length;i++) {
		if (rel_IsIdSelected(obj,data[i][0])) {
			outputCode+='<div class="relEC" onClick="javascript:rel_UncheckThisId(\''+obj+'\',\''+data[i][0]+'\');">'+data[i][1]+'</div>\n';
		} else {
			outputCode+='<div class="relEUC" onClick="javascript:rel_CheckThisId(\''+obj+'\',\''+data[i][0]+'\');">'+data[i][1]+'</div>\n';
		}
	}

	misc_WriteElementContent(obj+'_relSelectorData',outputCode);
	if (!$(obj+'_relSelector').visible()) $(obj+'_relSelector').appear();
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AJAX processing
var ajaxXmlHttpObj;
var ajaxTargetElem="";
var ajaxSendString="";

function ajax_GetXmlHttpObject() {
	var ajaxXmlHttpObj=null;
	try	{	// Firefox, Opera 8.0+, Safari
		ajaxXmlHttpObj=new XMLHttpRequest();
	} catch (e)	{	//Internet Explorer
		try	{
			ajaxXmlHttpObj=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e)	{
			ajaxXmlHttpObj=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return ajaxXmlHttpObj;
}

// main function for ajax processing
// needs sourceFields and target object, also definition of which php script to start
// sky (Session Key) is needed to confirm security in php script
// if additional params are given they are added, standard for content of source element is VALUE as request variable
function ajax_ProcessRun(sourceFields,target,targetfile,addParams,sky) {
	ajaxTargetElem=target;						// init target object
	ajaxSendString=targetfile;					// init send string

	if (sourceFields.length>0) {
		$A(sourceFields).each(function(curField) {
			if ($(curField).value.length>0) {
				ajaxSendString=misc_AddParamToURL(ajaxSendString,curField+"="+encodeURI($(curField).value));
			} else {
				ajaxSendString=misc_AddParamToURL(ajaxSendString,curField);
			} // end if
		});
	} // end if sourcefields defined

	if (addParams.length>0) ajaxSendString=misc_AddParamToURL(ajaxSendString,encodeURI(addParams));
	
	ajaxSendString=misc_AddParamToURL(ajaxSendString,"SKY="+sky);

	ajaxXmlHttpObj=ajax_GetXmlHttpObject();		// init xml Object
	if (ajaxXmlHttpObj==null || ajaxTargetElem.length==0 || ajaxSendString.length==0) {
		alert("Browser does not support HTTP Request or NOT all data is set - AJAX NOT POSSIBLE");
		return;
	}

	// do processing
	ajaxXmlHttpObj.onreadystatechange=ajax_ProcessReturn;
	ajaxXmlHttpObj.open("GET",ajaxSendString,true);
	ajaxXmlHttpObj.send(null);
}

// output the ajax result to given element
function ajax_ProcessReturn() {
	if (ajaxXmlHttpObj.readyState==4 || ajaxXmlHttpObj.readyState=="complete") {
		misc_WriteElementContent(ajaxTargetElem,ajaxXmlHttpObj.responseText);
	} 
}

// function for auto completer (db driven)
var curAutocompleterObj=null;
function ajax_AutoCompleter(in_id,out_id,url,sessid,mininputtedchars) {
	var newsessid="";
	if (sessid.length>0) {
		for (i=sessid.length-1;i>=0;i--) newsessid=newsessid+sessid.charAt(i);
	}

	curAutocompleterObj=new Ajax.Autocompleter(in_id,out_id,url, {
		paramName: in_id,
		parameters: 'SECID='+newsessid,
		minChars: mininputtedchars
		});
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// misc processing

// show or hide element
// needs the element name
function misc_ShowHideElement(obj) {
	if (!$(obj).visible()) $(obj).appear(); else $(obj).fade();
}

// show or hide a defined element, 1=show, else hide
function misc_ShowHideElementDefined(obj,code) {
	if (code==1) $(obj).appear(); else $(obj).fade();
}

// toggle for hide other object and show this one
function misc_SetVisibilityOfElement(thisobj,otherobj) {
	if (!$(thisobj).visible()) {
		$(thisobj).appear();
		$(otherobj).fade();
	} else {
		$(thisobj).fade();
	}
}

// write content to an element
// needs the element name and new code to be outputted
function misc_WriteElementContent(obj,outputCode) {
	$(obj).update(outputCode);
}

// converts decimal value to hex
// NO check is done !!
function misc_ConvDecToHex(d) {
	var z=colHexChars;
	var x="";
	var i=1,v=d,r=0;
	while (v>15) {
		v=Math.floor(v/16);
		i++;
	}
	v=d;
	for (j=i;j>=1;j--) {
		x=x+z[Math.floor(v/Math.pow(16,j-1))];
		v=v-(Math.floor(v/Math.pow(16, j-1))*Math.pow(16, j-1));
	}
	return(x);
}
	
// converts hex to decimal
// NO check is done !!
function misc_ConvHexToDec(x) {
	var e=new Array();
	var z=colHexChars
	var d=0;
	x=x.toUpperCase();

	for(i=0;i<x.length;i++) {
		for (j=0;j<=16;j++) {
			if (x.substring(i,i+1)==z[j]) e[i]=j;
		}
	}
	for (i=0;i<x.length;i++) d=d+e[i]*Math.pow(16,x.length-i-1);
	return(d);
}

// update the clock div (running continiously)
function misc_RunClock() {
	new PeriodicalExecuter(function(pe) {

	var now=new Date();
	var out="";
	if (now.getDate()<10) out+="0";
	out+=now.getDate()+".";
	if ((now.getMonth()+1)<10) out+="0";
	out+=(now.getMonth()+1)+".";
	out+=now.getFullYear()+" ";
	if (now.getHours()<10) out+="0";
	out+=now.getHours()+":";
	if (now.getMinutes()<10) out+="0";
	out+=now.getMinutes()+":";
	if (now.getSeconds()<10) out+="0";
	out+=now.getSeconds();
	
	misc_WriteElementContent('clock',out);
	}, 1);
}

// set all line checkboxes on or off
function misc_SetLineBoxes(form,top,line,command) {
	var state=$(top).checked;								// get state of top box
	var allFormElems=$(form).getElements();					// get all elems in form
	// filter out line select boxes and set them to state of top box
	for (i=0;i<allFormElems.length;i++) if (allFormElems[i].identify().startsWith(line)) $(allFormElems[i].identify()).checked=state;
	if (command!='') misc_SetCommandBox(command,state);			// activate or deactivate command elem
}

// set top selector checkbox on or off in view
// only if all line checkboxes are set
function misc_SetTopBox(form,top,line,command) {
	var cntTotal=0;											// indexes
	var cntChecked=0;
	var state=true;											// state of top box init=true
	var allFormElems=$(form).getElements();					// get all elems in form
	// filter out line select boxes and count them
	for (i=0;i<allFormElems.length;i++) {
		if (allFormElems[i].identify().startsWith(line)) {
			cntTotal++;
			if ($(allFormElems[i].identify()).checked) cntChecked++;	
		}
	} // end for i
	
	if (cntTotal!=cntChecked) state=false;												// decide to check or uncheck top box based on counts
	$(top).checked=state;																// check/uncheck state of top box
	if (command!='') { 
		if (cntChecked>0) misc_SetCommandBox(command,true); else misc_SetCommandBox(command,false);	// activate or deactivate command elem
	}
}

// disable or enable command box (command means processing of line check boxes)
// name is needed and mode false=disable, true=enable
function misc_SetCommandBox(elem,mode) {
	if (elem!='') {
		if (mode==1) $(elem).enable(); else $(elem).disable();
	} // end if elem given
}

// add a param / params to a given url, checks if ? is already in there
function misc_AddParamToURL(url,param) {
	if (param!="") {												// if param given
		if (param.charAt(0)!="&" && param.charAt(0)!="?") {			// only add join sign if not already in string
			// if no ? found, add it, else add with &
			var joinsign="&";
			if (url.indexOf("?")==-1) var joinsign="?";
			url=url+joinsign;
		} // end if join sign not already given
		url=url+param;
	} // end if param defined
	return(url);
}

