/*CopyControlValue
	This function copies parameter 1 (string) to the value property of
	the second parameter (object)

	Author: Einar Kvandahl
	Date: 1/26/2004							 	 	 						 		 			 							*/
function CopyControlValue(tcStringToCopy,toDestination,tlOverwrite)
{
	if (typeof toDestination == "object")
	{
		if (toDestination.value=='' || tlOverwrite)
		{
 			toDestination.value=tcStringToCopy;
		}
		return true
	}
	else
	{
		alert('Error in CopyControlValue() in TaskAsapJavaScript.js\nParameter \"toDestination\" is not an object.\n\nPlease Contact I/O Technologies!');
		return false
	}
}
/*CopyEmailIfValid

	Author: Einar Kvandahl
	Date: 2/24/2004							 	 	 						 		 			 							*/
function CopyEmailIfValid(tcStringToCopy,toDestination,tlOverwrite)
{
	if (ValidateEmail(tcStringToCopy,true))
	{
		if (typeof toDestination == "object")
		{
			if (toDestination.value=='' || tlOverwrite)
			{
	 			toDestination.value=tcStringToCopy;
			}
			return true
		}
		else
		{
			alert('Error in CopyEmailIfValid() in TaskAsapJavaScript.js\nParameter \"toDestination\" is not an object.\n\nPlease Contact I/O Technologies!');
			return false
		}
	}
}

//
/*ValidatePassWd
	Checks if password1 is the same as password2. Alerts the user is they do not match.
	Author: Einar Kvandahl
	Date: 2/17/2004							 	 	 						 		 			 							*/
function ValidatePassWd (tcPass1,tcPass2)
{
	if (tcPass1==tcPass2)
	{
		return true;
	}
	else
	{
		alert('\"Password\" and \"Retype Password\" do not match!\n\nPlease retype passwords.');
		return false;
	}
}
//
//
//
/*ValidateEmail
	Checks if passed parameter is a valid email address.
	Pretty simple function. It checks for 'at' and 'dot'
	Author: Einar Kvandahl
	Date: 2/17/2004							 	 	 						 		 			 							*/
function ValidateEmail (tcEmailAddr,tlQuiet)
{
	if (tcEmailAddr.indexOf("@")>0)
	{
		if (tcEmailAddr.indexOf(".")>0)
		{
			return true;
		}
		else
		{
			if (!tlQuiet)
			{
				alert('Invalid \"Email Address\"!\n\nPlease enter vaild \"Email Address\".');
			}
			return false;
		}
	}
	else
	{
		if (!tlQuiet)
		{
			alert('Invalid \"Email Address\"!\n\nPlease enter vaild \"Email Address\".');
		}
		return false;
	}
}
//
//
//
/*ThisWeek
	Outputs this week to passed objects' value property
	Author: Einar Kvandahl
	Date: 2/19/2004							 	 	 						 		 			 							*/
function ThisWeek (toOutBeg,toOutEnd,tcSeparatorChar)
{
	if (tcSeparatorChar==undefined || tcSeparatorChar=='' || tcSeparatorChar.length > 1)
	{
		tcSeparatorChar = '/';
	}

	var dToday = new Date();
	var dLastSunday = new Date(dToday.getFullYear(),dToday.getMonth(),dToday.getDate() - dToday.getDay());
	var dNextSaturday = new Date(dLastSunday.getFullYear(),dLastSunday.getMonth(),dLastSunday.getDate()+6);


	if (typeof toOutBeg == "object")
	{
		toOutBeg.value=dLastSunday.getMonth()+1+tcSeparatorChar+dLastSunday.getDate()+tcSeparatorChar+dLastSunday.getFullYear()
	}
	if (typeof toOutEnd == "object")
	{
		toOutEnd.value=dNextSaturday.getMonth()+1+tcSeparatorChar+dNextSaturday.getDate()+tcSeparatorChar+dNextSaturday.getFullYear()
	}
}


function ThisPayPeriod (toOutBeg,toOutEnd,tcSeparatorChar,tnPayType,tdPayPeriodStart,tnPeriodLengthInDays,tdBaseDate)
{
	if (tcSeparatorChar==undefined || tcSeparatorChar=='' || tcSeparatorChar.length > 1)
	{
		tcSeparatorChar = '/';
	}
	
	if (tdBaseDate==undefined)
	{
		tdBaseDate = new Date();
	}

	tdBaseDate = new Date(tdBaseDate.getFullYear(),tdBaseDate.getMonth(),tdBaseDate.getDate()); // To remove the time element
	var dNewStart = new Date(tdBaseDate.getFullYear(),tdBaseDate.getMonth(),tdBaseDate.getDate());
	var dNewEnd = new Date(tdBaseDate.getFullYear(),tdBaseDate.getMonth(),tdBaseDate.getDate());
	var one_day=1000*60*60*24;
	
	switch(tnPayType)
	{
		case 1:
			if (tdBaseDate.getDate()<=15)
			{
				dNewStart.setDate(1);
				dNewEnd.setDate(15);
			}
			else
			{
				dNewStart.setDate(16);
				dNewEnd.setDate(1);
				dNewEnd.setMonth(dNewEnd.getMonth()+1);
				dNewEnd.setDate(dNewEnd.getDate()-1);
			}
			break;
		case 2:
			// Changed from Math.ceil to Math.round due to the fact that daylight savings can result in
			// the date arithmetic being off by an hour in either direction.
			var DaysIntoPeriod=Math.round((tdBaseDate-tdPayPeriodStart)/one_day)%tnPeriodLengthInDays;
			if (DaysIntoPeriod<0)
				DaysIntoPeriod+=tnPeriodLengthInDays;
			dNewStart.setDate(dNewStart.getDate()-DaysIntoPeriod);
			dNewEnd.setDate(dNewEnd.getDate()+(tnPeriodLengthInDays-DaysIntoPeriod)-1);
			break;
	}

	if (typeof toOutBeg == "object")
	{
		toOutBeg.value=dNewStart.getMonth()+1+tcSeparatorChar+dNewStart.getDate()+tcSeparatorChar+dNewStart.getFullYear();
		ValidateDate(toOutBeg.value,toOutBeg,'default',true,false,false,'-');		
	}
	if (typeof toOutEnd == "object")
	{
		toOutEnd.value=dNewEnd.getMonth()+1+tcSeparatorChar+dNewEnd.getDate()+tcSeparatorChar+dNewEnd.getFullYear();
		ValidateDate(toOutEnd.value,toOutEnd,'default',true,false,false,'-');
	}
}

//
//
//
/*Tody
	Outputs todays date to passed object's value property
	Author: Einar Kvandahl
	Date: 2/19/2004							 	 	 						 		 			 							*/
function Today (toOutput,tcSeparatorChar)
{
	if (tcSeparatorChar==undefined || tcSeparatorChar=='' || tcSeparatorChar.length > 1)
	{
		tcSeparatorChar = '/';
	}
	var dToday = new Date();
	if (typeof toOutput == "object")
	{
		toOutput.value=dToday.getMonth()+1+tcSeparatorChar+dToday.getDate()+tcSeparatorChar+dToday.getFullYear()
	}
}
//
//
//
/*NavigateToReport
	The name of the function says it all
	Author: Einar Kvandahl
	Date: 2/19/2004							 	 	 						 		 			 							*/
function NavigateToReport (toFrm,tcRptId)
{
	/*
		Controls that are used:
			txt_dBeg
			txt_dEnd
	*/
	window.location.href = '?action=printreport&lcRptId='+tcRptId+'&lcDateBeg='+toFrm.txt_dBeg.value+'&lcDateEnd='+toFrm.txt_dEnd.value;
}
//
//
//
/*NavigateToNewTaskForProj
	The name of the function says it all
	Author: Einar Kvandahl
	Date: 2/20/2004							 	 	 						 		 			 							*/
function NavigateToNewTaskForProj (tcProjId)
{
	window.location.href = '?action=newtask&lcProjId='+tcProjId
}
//
//
//
/*CheckIfEmpty
	The name of the function says it all
	Author: Einar Kvandahl
	Date: 2/24/2004							 	 	 						 		 			 							*/
function CheckIfEmpty (tcString)
{
	if (tcString.length == 0)
	{
		alert('Please enter something!');
		return false
	}
	else
	{
		return true
	}
}
//
//
//
/*InvalidUserInformationMessage
	The name of the function says it all
	Author: Einar Kvandahl
	Date: 2/26/2004							 	 	 						 		 			 							*/
function InvalidUserInformationMessage (tcDisplayString)
{
	if (tcDisplayString.length == 0)
	{
		return true
	}
	else
	{
		alert('User information could not be saved:\n'+tcDisplayString+'\n\nPlease update information and save.');
		return true
	}
}
//
//
//
/*ReloadNewTaskPage
	The name of the function says it all
	Author: Einar Kvandahl
	Date: 3/5/2004							 	 	 						 		 			 							*/
function ReloadNewTaskPage (tcClientId)
{
	//alert('You are about to reload the page with new client information.\n\nPlease make sure that you only fill in the \"Requestor\" items, i.e. do not do any of the \"Manager\" options.\n\n Press \"OK\" to continue.')
	window.location.href = '?action=newtask&lcProjId=0&lcClientId='+tcClientId
}
//
//
//
/*ReloadNewHourPage
	The name of the function says it all
	Author: Einar Kvandahl
	Date: 3/9/2004							 	 	 						 		 			 							*/
function ReloadNewHourPage (toForm)
{

	var lcString = '?action=changeclientforhours'+
									'&cClientId='+toForm.cbo_nClientID.value+
									'&cTaskId='+toForm.cbo_nTaskID.value+
									'&cDetail='+toForm.edt_cDetail.value+
									'&cWorkDone='+toForm.txt_dWorkDone.value+
									'&cHours='+toForm.txt_nHours.value+
									'&cMinutes='+toForm.txt_nMinutes.value+
									'&cStart='+toForm.txt_Start.value+
									'&cStop='+toForm.txt_Stop.value;

	if (toForm.chk_lBillable.checked)
	{
		lcString = lcString + '&cBillable=yes' ;
	}

	//	alert(lcString);

	window.location.href = lcString
}
//
//
//
/*AddMatLog
	The name of the function says it all
	Author: Einar Kvandahl
	Date: 5/19/2004							 	 	 						 		 			 							*/
function AddMatLog (toForm)
{

	// check if material and/or qty is selected/entered

	if (!fnValidNumber(toForm.txtMatQty.value))
	{
		alert('Please enter a valid (positive) quantity.');
		return false;
	}

	if (toForm.cboMatID.value <= 0 )
	{
		alert('Please select a valid meterial.');
		return false;
	}


	var lcString = '?action=addmatlog'+
									'&cMatId='+toForm.cboMatID.value+
									'&cQty='+toForm.txtMatQty.value+
									'&cClientId='+toForm.cbo_nClientID.value+
									'&cTaskId='+toForm.cbo_nTaskID.value+
									'&cCatId='+toForm.cbo_nCatID.value+
									'&cSubCatId='+toForm.cbo_nSubCatID.value+
									'&cDetail='+toForm.edt_cDetail.value+
									'&cWorkDone='+toForm.txt_dWorkDone.value+
									'&cHours='+toForm.txt_nHours.value+
									'&cMinutes='+toForm.txt_nMinutes.value+
									'&cStart='+toForm.txt_Start.value+
									'&cStop='+toForm.txt_Stop.value;

	if (toForm.chk_lBillable.checked)
	{
		lcString = lcString + '&cBillable=yes' ;
	}


	//	alert(lcString);

	window.location.href = lcString

	return true;
}


function WriteStringToNewWindow (tcString)
{
	newWindow = window.open("", "", "width=400,height=250,status=no,resizable=yes,top=200,left=200,scrollbars=yes");

	newWindow.document.write('<pre>'+tcString+'</pre>');
	newWindow.document.title='Display Information';
}


function MemoPopUp (toMaster,tcMaster,tcWndTitle)
{

	newWindow = window.open("", "", "width=700,height=475,status=no,resizable=yes,top=200,left=200,scrollbars=yes") ;

	newWindow.document.write('<body><form name="popfrm" style="width:100%;background:#FFFFFF;"><center><textarea style="width:100%;height:100%;" rows="25" cols="80" name="edt_PopUp" onchange="javascript:self.opener.'+tcMaster+'.value=document.popfrm.edt_PopUp.value;" ></textarea></center></form></body>') ;
	newWindow.document.popfrm.edt_PopUp.value = toMaster.value ;
	newWindow.document.popfrm.edt_PopUp.readOnly = toMaster.readOnly ;

//  newWindow.document.write('<center><input type="button" onclick="javascript:self.opener.'+tcMaster+'.value=document.popfrm.edt_PopUp.value;" value="&nbsp;&nbsp;&nbsp;Submit&nbsp;&nbsp;&nbsp;" /></center>') ;

	newWindow.document.title=tcWndTitle ;
}

function ChangeDisabledState (toControl,tlState)
{
	//alert(tlState);
	toControl.disabled = tlState

}


function fnAddTime ()
{
//	alert('fnAddTime');
	var llContinue = false;

	llContinue = fnCheckIfTimeWindowIsOpen();

	if (llContinue)
	{
		HoursLogInfoWindow = window.open("?action=addtime", "ChildWin", "width=640,height=465,status=no,resizable=yes,top=200,left=200,scrollbars=yes") ;
	}
	else
	{
		alert('Window is open already!');
	}

	// this function must always return false otherwise the <a href=""></a> will fire instead of the onclick
	return false;
}

function fnAddTaskTime (tnTaskId)
{
//	alert('fnAddTaskTime');
	var llContinue = false;

	llContinue = fnCheckIfTimeWindowIsOpen();

	if (llContinue)
	{
		HoursLogInfoWindow = window.open("?action=addtime&lcTask="+tnTaskId, "ChildWin", "width=640,height=465,status=no,resizable=yes,top=200,left=200,scrollbars=yes") ;
	}
	else
	{
		alert('Window is open already!');
	}

	// this function must always return false otherwise the <a href=""></a> will fire instead of the onclick
	return false;
}

function fnTaskMsgThread (tnTaskId)
{
//	alert('fnTaskMsgThread');
	var llContinue = false;

	llContinue = fnCheckIfTaskMsgWindowIsOpen();

	if (llContinue)
	{
		TaskMsgWindow = window.open("?action=taskmsgthread&nTaskId="+tnTaskId, "TaskMsgChildWin", "width=700,height=475,status=no,resizable=yes,top=200,left=200,scrollbars=yes") ;
		return true;
	}
	else
	{
		alert('Window is open already!');
		return false;
	}

}

function fnAddTasksToProject (tnProjID)
{
//	alert('fnTaskMsgThread');
	var llContinue = false;

	var Tasks2ProjWindow=window.open("?action=addtasktoproject&nProjID="+tnProjID, "Tasks2ProjWindow", "width=650,height=200,status=no,resizable=yes,top=200,left=200,scrollbars=yes") ;
	return true;
}


function fnHoursDetail (tnId)
{
//	alert('fnHoursDetail');

	var llContinue = false;

	llContinue = fnCheckIfTimeWindowIsOpen();

	if (llContinue)
	{
		HoursLogInfoWindow = window.open("?action=hoursdetail&nid="+tnId, "ChildWin", "width=640,height=465,status=no,resizable=yes,top=200,left=200,scrollbars=yes") ;
	}
	else
	{
		alert('Window is open already!');
	}

	// this function must always return false otherwise the <a href=""></a> will fire instead of the onclick
	return false;
}



function fnCheckIfTimeWindowIsOpen ()
{
	// Changed to check the location instead of looping so there will be no more blank popups
	// REJ 03/15/06
	
	checkif = window.open("", "ChildWin", "width=640,height=465,status=no,resizable=yes,top=200,left=200,scrollbars=yes");

	return checkif.location.href=="about:blank";
	
	
	// I need to wait here until checkif.document is loaded
	// I guess this could be done a little more elegant but right now this works
	for (i=0;i<100000;i++) ; // do nothing but eat CPU
	for (i=0;i<100000;i++) ; // do nothing but eat CPU
	for (i=0;i<100000;i++) ; // do nothing but eat CPU
	for (i=0;i<100000;i++) ; // do nothing but eat CPU
	for (i=0;i<100000;i++) ; // do nothing but eat CPU
	for (i=0;i<100000;i++) ; // do nothing but eat CPU
	

//	alert('wait a little');

	if(checkif.document.body.innerHTML=="")
	{
		// Window is not open so return true
		//alert('checkif.document.body.innerHTML is empt');
		return true;
	}
	else
	{
		// window is open so return false
		//alert('checkif.document.body.innerHTML is not empty');
		return false;
	}

	// if you get here for whatever reason just return false better safe than sorry
	return false;
}


function fnCheckIfTaskMsgWindowIsOpen ()
{
	checkif = window.open("", "TaskMsgChildWin", "width=640,height=465,status=no,resizable=yes,top=200,left=200,scrollbars=yes");

	return checkif.location.href=="about:blank";

	// I need to wait here until checkif.document is loaded
	// I guess this could be done a little more elegant but right now this works
	for (i=0;i<100000;i++) ; // do nothing but eat CPU
	for (i=0;i<100000;i++) ; // do nothing but eat CPU
	for (i=0;i<100000;i++) ; // do nothing but eat CPU
	for (i=0;i<100000;i++) ; // do nothing but eat CPU
	for (i=0;i<100000;i++) ; // do nothing but eat CPU
	for (i=0;i<100000;i++) ; // do nothing but eat CPU


//	alert('wait a little');

	if(checkif.document.body.innerHTML=="")
	{
		// Window is not open so return true
		//alert('checkif.document.body.innerHTML is empt');
		return true;
	}
	else
	{
		// window is open so return false
		//alert('checkif.document.body.innerHTML is not empty');
		return false;
	}

	// if you get here for whatever reason just return false better safe than sorry
	return false;
}



function fnValidNumber (string)
{
	if (!string)
	{
		return false;
	}

	var Chars = "0123456789.";

  for (var i = 0; i < string.length; i++)
	{
  	if (Chars.indexOf(string.charAt(i)) == -1)
		{
    	return false;
		}
	}
  return true;
}




function TestChk(tlState)
{
	alert(tlState.value);
	if (tlState.value)
	{
		document.myfrm.txt1.value="einar";
		tlState.value = ""
	}
	else
	{
		document.myfrm.txt1.value="";
		tlState.value = "on"
	}
}

// Task 2903
function handleBackSpace(e)
{
	var ev = e||window.event; // Mozilla||IE
	var src = ev.target||ev.srcElement; // Mozilla||IE
	if (src.tagName!="TEXTAREA" && src.getAttribute("type") != "text") // Ignore for textareas and input textboxes
	{
		var iCode = ev.keyCode;
		if (iCode == 8) // Backspace
		{
			ev.returnValue = false; // IE
			return false; // Mozilla
		}
	}
}
