/* ###########################################
	 GENERAL FUNCTIONS
########################################### */

// ############ validate formelements ###############

/* 
	checks if formelement (obj) is array
	in:			obj			formelement
	out:		boolean		
*/
function isArray(obj){
	return(typeof(obj)=="object"?(typeof(obj.length)=="undefined"?false:true):false);
}
/*
	validates email-address
	in:			email		string with email-adress in it
	out:		boolean
	(does not check domains etc...)
*/
function isEmailAddr(email) {
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0) {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}
/*
	checks if a requested formfield has data
	in:			formfield		formelement
				fieldlabel		needed to display an error if no data inserted
	out:		boolean
*/
function validRequired(formField,fieldLabel) {
	var result = true;
	if (formField.value == "") {
		alert('Please enter a value for the ' + fieldLabel +' field.');
		formField.focus();
		result = false;
	}
	return result;
}
/* 
	checks if a string is a valid double float int
	in:				str			data to parse
	out:			boolean
	(returns false if a character is not valid, or the . is found more than once)
*/
function allDigits(str) {
	return inValidCharSet(str,".0123456789");
}
/*
	checks string has only chars from a given charset
	in:				str			data to parse
					charset		string with single characters (see alldigits-function)
	out:			boolean
*/
function inValidCharSet(str,charset) {
	var result = true;
	var point = 0;
	for (var i=0;i<str.length;i++) {
		if (charset.indexOf(str.substr(i,1))<0) {
			result = false;
			break;
		}
		if (str.substr(i,1)=='.'){
			point++;
		}
	}
	if ( point > 1) {
		result = false;
	}
	return result;
}
/*
	checks if email of a formfield is valid
	in:				formfield			formelement to check
					formlable			needed for error-message
					required			boolean, true if field is required
	out:			boolean
*/
function validEmail(formField,fieldLabel,required) {
	var result = true;
	if (required && !validRequired(formField,fieldLabel))
		result = false;
	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) ) {
		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		formField.focus();
		result = false;
	}
  return result;
}
/*
	checks if a formfield has valid numerical value
	in:				formfield			formelement to check
					fieldlabel			needed for error-message
					required			boolean, true if field is required
	out:			boolean
*/
function validNum(formField,fieldLabel,required) {
	if (formField.value=='') {formField.value=0;}
	var result = true;
	if (required && !validRequired(formField,fieldLabel))
		result = false;
 	if (result){
 		if (!allDigits(formField.value)){
 			alert('Please enter a number for the ' + fieldLabel +' field.');
			formField.focus();		
			result = false;
		}
	} 
	return result;
}
/*
	checks if a formfield has valid integer value
	in:				formfield			formelement to check
					fieldlabel			needed for error-message
					required			boolean, true if field is required
	out:			boolean
*/
function validInt(formField,fieldLabel,required){
	var result = true;
	if (required && !validRequired(formField,fieldLabel))
		result = false;
 	if (result){
 		var num = parseInt(formField.value,10);
 		if (isNaN(num)){
 			alert('Please enter a number for the ' + fieldLabel +' field.');
			formField.focus();		
			result = false;
		}
	} 
	return result;
}
/*
	checks if a formfield has valid date value (yyyy-mm-dd)
	in:				formfield			formelement to check
					fieldlabel			needed for error-message
					required			boolean, true if field is required
	out:			boolean
	(could creat problems!!!! e.g. 2004-02-31)
*/
function validDate(formField,fieldLabel,required){
	var result = true;
	/*if (required && !validRequired(formField,fieldLabel))
		result = false;
 	if (result){
 		var elems = formField.value.split("-");
 		result = (elems.length == 3); // should be three components
 		if (result){
 			var month = parseInt(elems[1],10);
  			var day = parseInt(elems[2],10);
 			var year = parseInt(elems[0],10);
			result = allDigits(elems[1]) && (month > 0) && (month < 13) &&
					 allDigits(elems[2]) && (day > 0) && (day < 32) &&
					 allDigits(elems[0]) && ((elems[0].length == 2) || (elems[0].length == 4));
			// check if inserted date is a valid date

			if (result)	{
				tempdate = new Date(year,month,day);
				if (tempdate.getFullYear() != year || tempdate.getMonth() != month || tempdate.getDate() != day) {
					result=false;
				}
			}
 		}
  		if (!result){
 			alert('Please enter a date in the format YYYY-MM-DD for the ' + fieldLabel +' field.');
			formField.focus();		
		}
	} */
	return result;
}

/*
	checks if a string contains valid date (yyyy-mm-dd)
	in:				formfield			formelement to check
					fieldlabel			needed for error-message
					required			boolean, true if field is required
	out:			boolean
	(todo: merge with validDate)
*/
function validDateString(dateString){
	var result = true;
	var elems = dateString.split("-");
	result = (elems.length == 3); // should be three components
	if (result){
		var month = parseInt(elems[1],10);
		var day = parseInt(elems[2],10);
		var year = parseInt(elems[0],10);
		result = allDigits(elems[1]) && (month > 0) && (month < 13) &&
				 allDigits(elems[2]) && (day > 0) && (day < 32) &&
				 allDigits(elems[0]) && ((elems[0].length == 2) || (elems[0].length == 4));
	}
	return result;
}


function validTimeString(timeString){
	var result = true;
	var elems = timeString.split(":");
	result = (elems.length >= 2); // should be two or three components
	if(!result)	{
		var elems = timeString.split(".");
		result = (elems.length >= 2); // should be two or three components
	}
	if (result){
		var hour = parseInt(elems[0],10);
		var minute = parseInt(elems[1],10);
		if(elems.length == 3)	{
			var second = parseInt(elems[2],10);
		}
		else	{
			var second = 0;
			elems[2] = 0;
		}
		result = allDigits(elems[0]) && (hour >= 0) && (hour < 24) &&
				 allDigits(elems[1]) && (minute >= 0) && (minute < 60) &&
				 allDigits(elems[2]) && (second >= 0) && (second < 60);
	}
	return result;
}


function checkTALength(obj, maxlen) {
	if (obj.value.length >= maxlen) {
		obj.value = obj.value.substring(0, maxlen);
	}
}

// ############## currency format ###############
/*
	in:				amount				value to format
	out:			s					formated value
	(could creat problems!!!! e.g. 2004-02-31)
*/
function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

// ############## checkbox marker ###############
/*
	in:				t				the checkbox of "select all" (this)
	out:			obj				object/array to check uncheck
	(could creat problems!!!! e.g. 2004-02-31)
*/
function checkAll(t, obj) {
	marker = true;
	if (t.checked == false){
		marker = false;
	}
	if(isArray(obj)) {
		for (var i=0; i< obj.length ; i++) {
			obj[i].checked = marker;
		}
	} else if(typeof(obj)=="object"){
		obj.checked = marker;
	}
}
// ############ calc day-diff ###############

/*
	calculates days between 2 dates
	in:				startdate			string
					enddate				string
	out:			integer
	(at the moment without sat and sundays)
	(todo: parameter countWeekDays [true/false])
*/
function weekdaysBetween(startDate, endDate) {
	if (validDateString(startDate) && validDateString(endDate)) {
		if (startDate < endDate) {
		  var s = new Date(startDate.substring(0,4),startDate.substring(5,7)-1,startDate.substring(8,10));//startDate;
		  var e = new Date(endDate.substring(0,4),endDate.substring(5,7)-1,endDate.substring(8,10));//endDate;
		} else {
		  var s = new Date(endDate.substring(0,4),endDate.substring(5,7)-1,endDate.substring(8,10));//endDate;
		  var e = new Date(startDate.substring(0,4),startDate.substring(5,7)-1,startDate.substring(8,10));//startDate;
		}
		var diffDays = Math.floor((e - s) / 86400000);
		var weeksBetween = Math.floor(diffDays / 7);
		if (s.getDay() == e.getDay()) {
		  var adjust = 0;
		} else if (s.getDay() == 0 && e.getDay() == 6) {
		  var adjust = 5;
		} else if (s.getDay() == 6 && e.getDay() == 0) {
		  var adjust = 0;
		} else if (e.getDay() == 6 || e.getDay() == 0) {
		  var adjust = 5-s.getDay();
		} else if (s.getDay() == 0 || s.getDay() == 6) {
		  var adjust = e.getDay();
		} else if (e.getDay() > s.getDay() ) {
		  var adjust = e.getDay()-s.getDay();
		} else {
		  var adjust = 5+e.getDay()-s.getDay();
		}
		return (weeksBetween * 5) + adjust;
	} else if (!validDateString(startDate) || !validDateString(endDate)) {
		return 1;
	} else {
		return 0;
	}
}
function dateToString(date) {
	y = date.getYear();
	m = date.getMonth() + 1;
	d = date.getDate();
	if (d < 10) d = '0' + d;
	if (m < 10) m = '0' + m;
	return (y + "-" + m + "-" + d);
}
function stringToDate(str) {
	return (new Date(str.substring(0,4),str.substring(5,7)-1,str.substring(8,10)));
}
function calcEnddate(startDate, duration) {
	var s = stringToDate(startDate);
	var buffer = Date.parse(s);
	if (duration >= 0) {
		number = eval(duration + (2*parseInt(duration/7))) * 24 * 60 * 60 * 1000;
	} else {
		number = eval(duration - (2*parseInt(duration/7))) * 24 * 60 * 60 * 1000;
	}
	var e = new Date(buffer + number);
	return (dateToString(e));
}
function calcTimeline(objDuration, objStart, objEnd) {
	if (eval(objDuration.value) < 0) {
		alert('please enter positive duration value!');
		return false;
	}

	var retend   = new Date();
	var start	 = objStart.value;
	var duration = objDuration.value;
	var fix = 1
	var fix_sat = 2;
	var fix_sun = 1;
	if (duration < 0) {
		fix = -1
		fix_sat = -1;
		fix_sun	= -2;
	}
	
	var start	= stringToDate(start);

	if (start.getDay() == 6) {	// if start is saturday add 2 days (-> monday)
		start = calcEnddate(dateToString(start), fix_sat);	
		objStart.value = start;
		start = stringToDate(start);
	} else if (start.getDay() == 0) {	// if start is sunday add 1 days (-> monday)
		start = calcEnddate(dateToString(start), fix_sun);	
		objStart.value = start;
		start = stringToDate(start);
	}
	// double weekends check
	if (duration > 0) {
		if ( start.getDay() > 7-parseInt(duration % 7)) {
			duration = parseInt(duration) + 2;
		}
	} else {
		if ( start.getDay() < parseInt(duration) % 7) {
			alert(duration);
			duration = eval(duration - 2);
			alert(duration);
		}
	}

	var end		= stringToDate(calcEnddate(dateToString(start), eval(duration-fix)));

	if (!isNaN(end.getDate()))
	{
		if (end.getDay() == 6) {	// saturday
			retend		 = calcEnddate(dateToString(end), fix_sat);
		} else if (end.getDay() == 0) { // sunday
			retend		 = calcEnddate(dateToString(end), fix_sun);
		} else {
			retend = end;
		}
		objEnd.value	= y + "-" + m + "-" + d;
	}else{
		objEnd.value	= '';
	}
}


// ############ tooltips (div) ###############
info = null;

/*
	toggle hide/show div
	in:			x				row of text-array to display
				y				column of text-array to display
*/
function showinfo(x, y) {
	info = document.getElementById("divText");
	info.innerHTML=tooltiparr[x][y];
	info.style.display = "block";
}
function hideinfo() {
	info.style.display = "none";
}

document.onmousemove = updateinfo;

function updateinfo(e) {
	x = (document.all) ? window.event.x + document.body.scrollLeft : e.pageX;
	y = (document.all) ? window.event.y + document.body.scrollTop  : e.pageY;
	if (info != null) {
		info.style.left = (x + 20) + "px";
		info.style.top 	= (y - 5) + "px";
	}
}

/* ###########################################
	 drag&drop
########################################### */

/**
var srcObj = new Object;
var dummyObj;

function startDrag(){
	srcObj = window.event.srcElement;
	dummyObj = srcObj.outerHTML;
	var dragData = window.event.dataTransfer;
	dragData.setData('Text', window.event.srcElement.src);
	dragData.effectAllowed = 'linkMove';
	dragData.dropEffect = 'move';
}

function enterDrag() {
	window.event.dataTransfer.getData('Text');
}

function endDrag() {
	window.event.dataTransfer.clearData();
}

function overDrag() {
	window.event.returnValue = false;
}

function drop(destination,source) {
	window.event.returnValue = false;
	if (source == 'definition')
	{
		if (destination=='recycle'){ // trash element(s)
			document.f_prjdata.method.value = 'deleteelement';
			document.f_prjdata.h_id.value = srcObj.elementid;
			document.f_prjdata.h_type.value = srcObj.elementtype;
			checkFields();
			check = confirm('Wollen Sie das Element mit \nallen Unterelementen wirklich entfernen?');
			if(check == true) document.f_prjdata.submit();
		} else { // move element(s)
			document.f_prjdata.method.value = 'moveelement';
			document.f_prjdata.h_type.value = srcObj.elementtype;
			document.f_prjdata.h_id.value = srcObj.elementid;
			document.f_prjdata.h_destid.value = destination;
			checkFields();
			check = confirm('Wollen Sie das Element mit \nallen Unterelementen wirklich verschieben?');
			if(check == true) document.f_prjdata.submit();
		}
	}
	if (source == 'files')
	{
		move_file(srcObj.myLabel,destination);
	}
}

function addAttribute(oObj, sVal) {
	var loc = oObj.indexOf(">");
	return oObj.substring(0, loc) + ' ' + sVal + '>';
}
**/

function deleteElement(elemid,elemtype){
	document.f_prjdata.method.value = 'deleteelement';
	document.f_prjdata.h_id.value = elemid;
	document.f_prjdata.h_type.value = elemtype;
	checkFields();
	check = confirm('Wollen Sie das Element mit \nallen Unterelementen wirklich entfernen?');
	if(check == true) document.f_prjdata.submit();
}

/* ###########################################
	 tree-navigation 
########################################### */
var openImg = new Image();
openImg.src="/gfx/salce/node-minimize.gif";
var closedImg = new Image();
closedImg.src="/gfx/salce/node-maximize.gif";
var bugImg = new Image();
/*
	show/hide html-element
	in:				branch				tag-id 
*/
function showBranch(branch){
	var objBranch = document.getElementById(branch);
	if (objBranch) {
		if(objBranch.style.display == "block" || objBranch.style.display == "table-cell" || objBranch.style.display == "table-row")
			objBranch.style.display = "none";
		else
			{
			try
				{
				objBranch.style.display = ( objBranch.tagName == "TR" ? "table-row" : ( objBranch.tagName == "TD" ? "table-cell" : "block" ) );
				}
			catch( e )
				{
				objBranch.style.display = "block";
				};
			};
	}
}
/*
	changes image +/-
	in:				img					image-id
					load				load site  [0=none ; 1=projectNavigation ; 2=work analysis]
					params				url-params for request
*/
function swapFolder(img,load,params){
	objImg = document.getElementById(img);
	if(objImg.src.indexOf('node-maximize.gif')>-1) {
		objImg.src = openImg.src;
		if (load==1){
			bugImg.src = "/system/cflibs/projectNavigation.cfm?prj="+img+"&action=open"+params;
		}else if(load==2){
			bugImg.src = "/system/cflibs/dashwork.cfm?action=open"+params;
		}
	} else {
		objImg.src = closedImg.src;
		if (load==1){
			bugImg.src = "/system/cflibs/projectNavigation.cfm?prj="+img+"&action=close"+params;
		}else if(load==2){
			bugImg.src = "/system/cflibs/dashwork.cfm?action=close"+params;
		}
	}
}
/*
	function to expand whole tree
	in:				array				id-array
					load				load site  [0=none ; 1=projectNavigation ; 2=...]
					params				url-params for request
*/
var prjArr = new Array(); // array for projects
function tree_expand(array, load, params) {
	for(i=0;i<=array.length-1;i++) {
		var objBranch = document.getElementById('tab_' + array[i]);
		if (objBranch != null) {
			objBranch.style.display="block";
			objImg = document.getElementById('img_' + array[i]);
			if (objImg!=null){
				objImg.src = openImg.src;
				if (load==1) {
					bugImg.src = "/system/cflibs/projectNavigation.cfm?prj=img_"+array[i]+"&action=open"+params;
				}
			}
		}	
	}
}
/*
	function to collapse whole tree
	in:				array				id-array
					load				load site  [0=none ; 1=projectNavigation ; 2=...]
					params				url-params for request
*/
function tree_collapse(array, load, params) {
	for(i=array.length-1;i>=0;i--) {
		var objBranch = document.getElementById('tab_' + array[i]);
		if (objBranch != null) {
			objBranch.style.display="none";
			objImg = document.getElementById('img_' + array[i]);
			if (objImg!=null){
				objImg.src = closedImg.src;
				if (load==1) {
					bugImg.src = "/system/cflibs/projectNavigation.cfm?prj=img_"+array[i]+"&action=close"+params;
				}
			}
		}
	}
}

/* ###########################################
	 definition (projectelements & activitycheck)
########################################### */
function checkFields() {
	if(document.f_prjdata.i_code) {
		if(isArray(document.f_prjdata.i_code)) {
			for (i=0;i<=document.f_prjdata.i_code.length-1;i++) {
				if(document.f_prjdata.i_code[i].value.length == 0) {
					document.f_prjdata.i_code[i].value = "n/a";
				}
				if(document.f_prjdata.i_name[i].value.length == 0) {
					document.f_prjdata.i_name[i].value = "n/a";
				}
			}
		} else {
			if(document.f_prjdata.i_code.value.length == 0) {
				document.f_prjdata.i_code.value = "n/a";
			}
			if(document.f_prjdata.i_name.value.length == 0) {
				document.f_prjdata.i_name.value = "n/a";
			}
		}
	}
	if(document.f_prjdata.i_actcode) {
		if(isArray(document.f_prjdata.i_actcode)) {
			for (i=0;i<=document.f_prjdata.i_actcode.length-1;i++) {
				if(document.f_prjdata.i_actcode[i].value.length == 0) {
					document.f_prjdata.i_actcode[i].value = "n/a";
				}
				if(document.f_prjdata.i_actname[i].value.length == 0) {
					document.f_prjdata.i_actname[i].value = "n/a";
				}
			}
		} else {
			if(document.f_prjdata.i_actcode.value.length == 0) {
				document.f_prjdata.i_actcode.value = "n/a";
			}
			if(document.f_prjdata.i_actname.value.length == 0) {
				document.f_prjdata.i_actname.value = "n/a";
			}
		}
	}
}

// keyhandler 
function checkEvent(id,row,mode) {
	var doAction = false;
	doAction=true;
	/*if (id == 0 && row == 0) {	// no special action -> save all
		if (window.event.keyCode == 13) {
			document.f_prjdata.method.value = "saveelements";
			doAction=true;
		}
	} else {
		document.f_prjdata.h_id.value = id;
		if(window.event.ctrlKey && window.event.keyCode == 13) {	// [CTRL][ENTER] -> add new Projectelement
			document.f_prjdata.method.value = "addproject"
			doAction = true;
		} else if(window.event.shiftKey && window.event.keyCode == 13) {	// [SHIFT][ENTER] -> add new Activityelement
			document.f_prjdata.method.value = "addactivity";
			doAction = true;
		} else if (window.event.keyCode == 13) {	// [ENTER] -> save all elements
			document.f_prjdata.method.value = "saveelements";
			doAction = true;
		}
	}*/
	document.f_prjdata.h_id.value = id;
	if (mode == 'prj') document.f_prjdata.method.value = "addproject"
	if (mode == 'act') document.f_prjdata.method.value = "addactivity";
	if (mode == 'art') document.f_prjdata.method.value = "addArticle";
	if (mode == 'sv') document.f_prjdata.method.value = "saveelements";
	
	if (doAction) {		// just do it
		checkIsBudTL();
		checkFields();
		document.f_prjdata.submit();
	}
}

function checkEventOS(id,row,mode) {
	var doAction = false;
	doAction=true;
	
	document.f_prjdata.h_id.value = id;
	if (mode == 'prj') document.f_prjdata.method.value = "addproject"
	if (mode == 'act') document.f_prjdata.method.value = "addactivity";
	if (mode == 'art') document.f_prjdata.method.value = "addArticle";
	if (mode == 'sv') document.f_prjdata.method.value = "saveelements";
	
	if (doAction) {		// just do it
		checkIsBudTL();
		checkFields();
	}

	return doAction;
}

// submithandler (if [enter] pressed submit form.f_edit)
function checkSubmit(chkFunction) {
	var submitform = true;
	/*if (window.event.keyCode == 13) {
		if (chkFunction=='budget'){
			calcBudgetFields(1);
			submitform=checkBudgetFields();
		} else if (chkFunction=='timeline')	{
			calcDuration();
			submitform=checkTimelineFields();
		}
		if (submitform)	{
			//document.f_edit.submit();
		}
	}*/
}


/* ###########################################
	isBudget / isTimeline ProjectStructure
########################################### */
function checkIsBudTL() {
	document.f_prjdata.h_isBudget.value='';
	document.f_prjdata.h_isTimeline.value='';
	if (document.f_prjdata.c_isBudget) { // has activities
		if (isArray(document.f_prjdata.c_isBudget)) {
			for(var i=0; i<document.f_prjdata.c_isBudget.length;i++) {
				if (document.f_prjdata.c_isBudget[i].checked) {
					document.f_prjdata.h_isBudget.value = document.f_prjdata.h_isBudget.value + "1,";
				} else {
					document.f_prjdata.h_isBudget.value = document.f_prjdata.h_isBudget.value + "0,";
				}
				if (document.f_prjdata.c_isTimeline[i].checked) {
					document.f_prjdata.h_isTimeline.value = document.f_prjdata.h_isTimeline.value + "1,";
				} else {
					document.f_prjdata.h_isTimeline.value = document.f_prjdata.h_isTimeline.value + "0,";
				}
			}
		} else {
			if (document.f_prjdata.c_isBudget.checked) {
				document.f_prjdata.h_isBudget.value = document.f_prjdata.h_isBudget.value + "1,";
			} else {
				document.f_prjdata.h_isBudget.value = document.f_prjdata.h_isBudget.value + "0,";
			}	
			if (document.f_prjdata.c_isTimeline.checked) {
				document.f_prjdata.h_isTimeline.value = document.f_prjdata.h_isTimeline.value + "1,";
			} else {
				document.f_prjdata.h_isTimeline.value = document.f_prjdata.h_isTimeline.value + "0,";
			}	
		}
	}
	return false;
}

/* ###########################################
	 bookings (ressources)
########################################### */
function checkBookingFields() {
	var result = true;
	if (document.f_prjadv.h_actresnr) {
		document.f_prjadv.h_actreslist.value	= '';
		document.f_prjadv.h_timelist.value		= '';
		document.f_prjadv.h_datelist.value		= '';
		document.f_prjadv.h_donelist.value		= '';
		document.f_prjadv.h_commentlist.value	= '';
		document.f_prjadv.h_acttypelist.value	= '';
		if (isArray(document.f_prjadv.h_actresnr)) {
			for (i=0; i<=document.f_prjadv.h_actresnr.length-1; i++) {
				// generate datalists
				if ( (document.f_prjadv.i_repworktime[i].value!=0) ) {
					// check fields
					if (!validDate(document.f_prjadv.i_repworkdate[i]," work date ", true)) { result = false; }
					if (!validNum(document.f_prjadv.i_repworktime[i], " work time ", true)) { result = false; }
					if (!validNum(document.f_prjadv.i_resworkdone[i], " work done ", true)) { result = false; }
					// create lists
					document.f_prjadv.h_actreslist.value  = document.f_prjadv.h_actreslist.value + document.f_prjadv.h_actresnr[i].value + ',';
					document.f_prjadv.h_timelist.value = document.f_prjadv.h_timelist.value + document.f_prjadv.i_repworktime[i].value + ',';
					document.f_prjadv.h_datelist.value = document.f_prjadv.h_datelist.value + document.f_prjadv.i_repworkdate[i].value + ',';
					document.f_prjadv.h_donelist.value = document.f_prjadv.h_donelist.value + document.f_prjadv.i_resworkdone[i].value + ',';
					document.f_prjadv.h_acttypelist.value = document.f_prjadv.h_acttypelist.value + document.f_prjadv.i_repacttype[i].value + ',';
					if (document.f_prjadv.i_repcomment[i].value==''){
						document.f_prjadv.h_commentlist.value = document.f_prjadv.h_commentlist.value + ' ,';
					} else {
						document.f_prjadv.h_commentlist.value = document.f_prjadv.h_commentlist.value + document.f_prjadv.i_repcomment[i].value + ',';
					}
				}
			}
		} else {
			// generate datalists
			if ( (document.f_prjadv.i_repworktime.value!=0) ) {
				// check fields
				if (!validDate(document.f_prjadv.i_repworkdate," work date ", true)) { result = false; }
				if (!validNum(document.f_prjadv.i_repworktime, " work time ", true)) { result = false; }
				if (!validNum(document.f_prjadv.i_resworkdone, " work done ", false)) { result = false; }
				// create lists
				document.f_prjadv.h_actreslist.value  = document.f_prjadv.h_actresnr.value + ',';
				document.f_prjadv.h_timelist.value = document.f_prjadv.i_repworktime.value + ',';
				document.f_prjadv.h_datelist.value = document.f_prjadv.i_repworkdate.value + ',';
				document.f_prjadv.h_donelist.value = document.f_prjadv.i_resworkdone.value + ',';
				document.f_prjadv.h_acttypelist.value = document.f_prjadv.i_repacttype.value + ',';
				
				if (document.f_prjadv.i_repcomment.value=='') {
					document.f_prjadv.h_commentlist.value = ' ,';
				} else {
					document.f_prjadv.h_commentlist.value = document.f_prjadv.i_repcomment.value + ',';
				}
			}
		}
	}
	if (result && document.f_prjadv.h_actreslist.value != '')	{
		document.f_prjadv.submit();
	} else {
		return false;
	}
}

function checkBookingEdit() {
	var result = true;
	if ( (document.f_prjdata.i_repworktime.value!=0) ) {
		if (!validDate(document.f_prjdata.i_repworkdate," work date ", true)) { result = false; }
		if (!validNum(document.f_prjdata.i_repworktime, " work time ", true)) { result = false; }
		if (!validNum(document.f_prjdata.i_repworkdone, " work done ", false)) { result = false; }
	}
	if (result)	{
		document.f_prjdata.submit();
	}
}

/* ###########################################
	 bookings (bills)
########################################### */
function checkBillingFields() {
	var result = true;
	if (document.f_prjadv.h_act) {
		document.f_prjadv.h_actlist.value			= '';
		document.f_prjadv.h_invnumberlist.value		= '';
		document.f_prjadv.h_invdatelist.value		= '';
		document.f_prjadv.h_invcommentlist.value	= '';
		document.f_prjadv.h_invinvoicelist.value	= '';
		document.f_prjadv.h_invfeelist.value		= '';
		document.f_prjadv.h_invquantitylist.value	= '';
		document.f_prjadv.h_invvatlist.value		= '';
		document.f_prjadv.h_invcontractorlist.value	= '';
		if (isArray(document.f_prjadv.h_act)) {
			for (i=0; i<=document.f_prjadv.h_act.length-1; i++) {
				// create datalists
				if ( (document.f_prjadv.i_invoice[i].value!=0) ) {
					// check fields
					if (!validDate(document.f_prjadv.i_date[i]," date ", true)) { result = false; }
					if (!validNum(document.f_prjadv.i_invoice[i], " invoice ", true)) { result = false; }
					if (!validNum(document.f_prjadv.i_quantity[i], " quantity ", true)) { result = false; }
					if (!validNum(document.f_prjadv.i_fee[i], " fee ", false)) { result = false; }
					if (!validNum(document.f_prjadv.i_vat[i], " VAT ", false)) { result = false; }
					// create lists
					document.f_prjadv.h_actlist.value  = document.f_prjadv.h_actlist.value + document.f_prjadv.h_act[i].value + ' ,';
					document.f_prjadv.h_invnumberlist.value  = document.f_prjadv.h_invnumberlist.value + document.f_prjadv.i_number[i].value + ' ,';
					document.f_prjadv.h_invdatelist.value  = document.f_prjadv.h_invdatelist.value + document.f_prjadv.i_date[i].value + ' ,';
					document.f_prjadv.h_invcommentlist.value  = document.f_prjadv.h_invcommentlist.value + document.f_prjadv.i_comment[i].value + ' ,';
					document.f_prjadv.h_invinvoicelist.value  = document.f_prjadv.h_invinvoicelist.value + document.f_prjadv.i_invoice[i].value + ' ,';
					document.f_prjadv.h_invfeelist.value  = document.f_prjadv.h_invfeelist.value + document.f_prjadv.i_fee[i].value + ' ,';
					document.f_prjadv.h_invquantitylist.value  = document.f_prjadv.h_invquantitylist.value + document.f_prjadv.i_quantity[i].value + ' ,';
					document.f_prjadv.h_invvatlist.value  = document.f_prjadv.h_invvatlist.value + document.f_prjadv.i_vat[i].value + ' ,';
					document.f_prjadv.h_invcontractorlist.value  = document.f_prjadv.h_invcontractorlist.value + document.f_prjadv.s_con[i].options[document.f_prjadv.s_con[i].selectedIndex].value + ',';
				}
			}
		} else {
			// create datalists
			if ( (document.f_prjadv.i_invoice.value!=0) ) {
				// check fields
				if (!validDate(document.f_prjadv.i_date," date ", true)) { result = false; }
				if (!validNum(document.f_prjadv.i_invoice, " invoice ", true)) { result = false; }
				if (!validNum(document.f_prjadv.i_quantity, " quantity ", true)) { result = false; }
				if (!validNum(document.f_prjadv.i_fee, " fee ", false)) { result = false; }
				if (!validNum(document.f_prjadv.i_vat, " VAT ", false)) { result = false; }
				// create lists
				document.f_prjadv.h_actlist.value  = document.f_prjadv.h_act.value + ' ,';
				document.f_prjadv.h_invnumberlist.value  =  document.f_prjadv.i_number.value + ' ,';
				document.f_prjadv.h_invdatelist.value  = document.f_prjadv.i_date.value + ' ,';
				document.f_prjadv.h_invcommentlist.value  = document.f_prjadv.i_comment.value + ' ,';
				document.f_prjadv.h_invinvoicelist.value  = document.f_prjadv.i_invoice.value + ' ,';
				document.f_prjadv.h_invfeelist.value  = document.f_prjadv.i_fee.value + ' ,';
				document.f_prjadv.h_invquantitylist.value  = document.f_prjadv.i_quantity.value + ' ,';
				document.f_prjadv.h_invvatlist.value  = document.f_prjadv.i_vat.value + ' ,';
				document.f_prjadv.h_invcontractorlist.value  = document.f_prjadv.h_invcontractorlist.value + document.f_prjadv.s_con.options[document.f_prjadv.s_con.selectedIndex].value + ',';
			}
		}
	}
	if (result && document.f_prjadv.h_actlist.value != '') {
		document.f_prjadv.submit();
	} else {
		return false;
	}
}

function checkBillingEdit() {
	var result = true;
	if ( (document.f_prjadv.i_invoice.value!=0) ) {
		if (!validDate(document.f_prjadv.i_date," date ", true)) { result = false; }
		if (!validNum(document.f_prjadv.i_invoice, " invoice ", true)) { result = false; }
		if (!validNum(document.f_prjadv.i_quantity, " quantity ", true)) { result = false; }
		if (!validNum(document.f_prjadv.i_fee, " fee ", false)) { result = false; }
		if (!validNum(document.f_prjadv.i_vat, " VAT ", false)) { result = false; }
	}
	if (result)	{
		document.f_prjadv.submit();
	}
}

function calcBillingFields(index, calcFee) {
	if (document.f_prjadv.i_invoice) {
		
		if (isNaN(document.f_prjadv.i_invoice.length)==false && document.f_prjadv.i_invoice.length) {
			if ( isNaN(document.f_prjadv.i_invoice[index].value)==false && ( document.f_prjadv.i_invoice[index].value!=0) ) {
				if (calcFee) {
					document.f_prjadv.i_fee[index].value = document.f_prjadv.i_quantity[index].value * document.f_prjadv.i_invoice[index].value * 0.08;
				}
				document.f_prjadv.i_amount[index].value = ( Number(document.f_prjadv.i_invoice[index].value) * Number(document.f_prjadv.i_quantity[index].value)) + Number(document.f_prjadv.i_fee[index].value);
			}

		} else {
			if ( isNaN(document.f_prjadv.i_invoice.value)==false && ( document.f_prjadv.i_invoice.value!=0) ) {
				if (calcFee) {
					document.f_prjadv.i_fee.value = document.f_prjadv.i_quantity.value * document.f_prjadv.i_invoice.value * 0.08;
				}
				document.f_prjadv.i_amount.value = ( Number(document.f_prjadv.i_invoice.value) * Number(document.f_prjadv.i_quantity.value) ) + Number(document.f_prjadv.i_fee.value);
			}
		}

	}
}
/* ###########################################
	 add/edit budget calculation
	 mode = 0 -> calculate all 
	 mode = 1 -> calculate wo fee
########################################### */
function calcBudgetFields(mode) {
	
	var f_name;
	var act_id;
	var bud_quantity, bud_amount, bud_begin, bud_fee, bud_inv;

	for (i=0;i<document.f_prjdata.elements.length;i++)
	{
		f_name = document.f_prjdata.elements[i].name;
		if (f_name.substring(0,14) == 'i_bud_quantity')
		{	
			act_id = f_name.substring(15);
			bud_quantity = 'i_bud_quantity_' + act_id;
			bud_amount = 'i_bud_amount_' + act_id;
			bud_begin = 'i_bud_begin_' + act_id;
			bud_fee = 'i_bud_fee_' + act_id;
			bud_inv = 'i_bud_invoice_' + act_id;

			if ( isNaN(document.f_prjdata.elements[i].value)==false ) {
				if (isNaN(document.getElementById(bud_inv).value)==false && ( document.getElementById(bud_inv).value!=0 && document.getElementById(bud_begin).value==0) ) {
					if (mode == 0 || !mode) {
						document.getElementById(bud_fee).value = CurrencyFormatted(document.f_prjdata.elements[i].value * document.getElementById(bud_inv).value * 0.08);
					}
					document.getElementById(bud_amount).value = CurrencyFormatted((Number(document.getElementById(bud_inv).value) * document.getElementById(bud_quantity).value) + Number(document.getElementById(bud_fee).value));
				}
				if (isNaN(document.getElementById(bud_begin).value)==false && document.getElementById(bud_begin).value!=0 ) {
					document.getElementById(bud_amount).value = CurrencyFormatted(document.getElementById(bud_begin).value * document.getElementById(bud_quantity).value);
				}
			}
		}
	}
	
	/*
	replaced 
	if ( isNaN(document.f_edit.i_bud_quantity.value)==false ) {
		if (isNaN(document.f_edit.i_bud_invoice.value)==false && ( document.f_edit.i_bud_invoice.value!=0 && document.f_edit.i_bud_begin.value==0) ) {
			if (mode == 0 || !mode) {
				document.f_edit.i_bud_fee.value = CurrencyFormatted(document.f_edit.i_bud_quantity.value * document.f_edit.i_bud_invoice.value * 0.08);
			}
			document.f_edit.i_bud_amount.value = CurrencyFormatted((Number(document.f_edit.i_bud_invoice.value) * document.f_edit.i_bud_quantity.value) + Number(document.f_edit.i_bud_fee.value));
		}
		if (isNaN(document.f_edit.i_bud_begin.value)==false && document.f_edit.i_bud_begin.value!=0 ) {
			document.f_edit.i_bud_amount.value = CurrencyFormatted(document.f_edit.i_bud_begin.value * document.f_edit.i_bud_quantity.value);
		}
	}*/
}
function checkBudgetFields() {
	if (!validNum(f_prjdata.i_bud_quantity, " quantity ", true)) { return false; }
	if (!validNum(f_prjdata.i_bud_invoice, " invoice ", true)) { return false; }
	if (!validNum(f_prjdata.i_bud_fee, " fee ", true)) { return false; }
	if (!validNum(f_prjdata.i_bud_begin, " begin ", true)) { return false; }
	if (!validNum(f_prjdata.i_bud_amount, " amount ", true)) { return false; }
	return true;
}

function calcDuration(actid){
	var act_duration, act_start, act_end;
	
	act_duration	= 'i_actduration_' + actid
	act_start		= 'i_actstartdate_' + actid
	act_end			= 'i_actenddate_' + actid

	document.getElementById(act_duration).value=weekdaysBetween(document.getElementById(act_start).value,document.getElementById(act_end).value);
}

function checkTimelineFields() {
	if (f_prjdata.i_actstartdate.value!='') {
		if (!validDate(f_prjdata.i_actstartdate," startdate ", false)) { return false; }
	}
	if (f_prjdata.i_actenddate.value!='') {
		if (!validDate(f_prjdata.i_actenddate," enddate ", false)) { return false; }
	}
	if (!validNum(f_prjdata.i_actduration, " duration ", false)) { return false; }
	if (!validNum(f_prjdata.i_actpriority, " priority ", false)) { return false; }
	return true;
}
 

function calc_np(bud_quantity,bud_begin,bud_amount){
	
	document.getElementById(bud_begin.name).value = document.getElementById(bud_amount.name).value / document.getElementById(bud_quantity.name).value;

}

function moveAct(prjid, actid, direction){
	
	document.f_prjdata.h_id.value = prjid;
	document.f_prjdata.h_key.value = actid;
	document.f_prjdata.h_type.value = direction;
	document.f_prjdata.method.value = 'moveavivity';
	document.f_prjdata.submit();

}

function prjUp(sourceLeft,sourceRight,destLeft,destRight,camID){
	document.f_prjdata.method.value = 'movePrj';
	document.f_prjdata.h_type.value = 'up';
	document.f_prjdata.h_srcRight.value = sourceRight;
	document.f_prjdata.h_srcLeft.value = sourceLeft;
	document.f_prjdata.h_destRight.value = destRight;
	document.f_prjdata.h_destLeft.value = destLeft;
	document.f_prjdata.h_id.value=camID;

	document.f_prjdata.submit();
}

function prjDown(sourceLeft,sourceRight,destLeft,destRight,camID){
	document.f_prjdata.method.value = 'movePrj';
	document.f_prjdata.h_type.value = 'down';
	document.f_prjdata.h_srcRight.value = sourceRight;
	document.f_prjdata.h_srcLeft.value = sourceLeft;
	document.f_prjdata.h_destRight.value = destRight;
	document.f_prjdata.h_destLeft.value = destLeft;
	document.f_prjdata.h_id.value=camID;

	document.f_prjdata.submit();
}

function prjLeft(sourceLeft,sourceRight,destLeft,destRight,camID){
	document.f_prjdata.method.value = 'movePrj';
	document.f_prjdata.h_type.value = 'left';
	document.f_prjdata.h_srcRight.value = sourceRight;
	document.f_prjdata.h_srcLeft.value = sourceLeft;
	document.f_prjdata.h_destRight.value = destRight;
	document.f_prjdata.h_destLeft.value = destLeft;
	document.f_prjdata.h_id.value=camID;

	document.f_prjdata.submit();
}

function prjRight(sourceLeft,sourceRight,destLeft,destRight,camID){
	document.f_prjdata.method.value = 'movePrj';
	document.f_prjdata.h_type.value = 'right';
	document.f_prjdata.h_srcRight.value = sourceRight;
	document.f_prjdata.h_srcLeft.value = sourceLeft;
	document.f_prjdata.h_destRight.value = destRight;
	document.f_prjdata.h_destLeft.value = destLeft;
	document.f_prjdata.h_id.value=camID;

	document.f_prjdata.submit();
}

function relocAct(actID){

	window.open('/popup.cfm?popuptoload=moveact&actID='+actID,'popmovact','width=250,height=200,left=280,top=220,toolbar=no,scrollbars=no,resizable=yes');
	
}

function relocPrj(prjID){
	alert(prjID);
}

function showChgWarn(){
	document.getElementById('pdc').style.visibility = "visible";
}