/*
<!-- -->
<!-- The copyright in the work that is PublicAccess is the exclusive -->
<!-- property of CAPS Solutions Ltd, and its respective copyright owners, and is -->
<!-- protected under United Kingdom copyright law and other international copyright -->
<!-- treaties and conventions. 
<!-- © 2004. CAPS Solutions Ltd and its licensor(s). All rights reserved. -->
*/

// lookup.js
//************************************************
// opens a modal dialog at the specified URL with optional features
//************************************************
function openModalLookupDialog(szUrl, szFeatures){
	
	if (szFeatures && szFeatures != '')
		szFeatures += "center:yes;";
	else
		szFeatures = "dialogHeight:400px;dialogWidth:400px";
		
	
	var rRetVal = showModalDialog(szUrl, '', szFeatures);
	
	if (rRetVal){
		return rRetVal;
	}
	else{
		return false;
	}

}

//***********************************************************************************
// pops up a calendar lookup
//***********************************************************************************
function doCalendarLookup(elDateField){

	var szUrl=szcURL_APPROOT + '/common/calendar/calendardialog.htm?datefield='+elDateField.id;
			
	var szFeatures = "height=280,width=300";
	//var rRet = 	openModalLookupDialog(szUrl, szFeatures);
	window.open(szUrl,'calendar',szFeatures);
	
	//if (rRet){
	//	elDateField.value=rRet;
		
	//	elDateField.focus();
	//}else return false;
	
	//if (document.getElementById(elDateField.name))
	//{
	//	 elDateField.value = document.getElementById(elDateField.name).value;
	//}
				
}

//*********************************************************
// writes current time as a string to ref element
//*********************************************************
function writeCurrentTimeToField(el){
	
	if (el.value!=''){
		return false;
	}

	getCurrentTime(el);

}

function writeCurrentShortTimeToField(el){
	
	if (el.value!=''){
		return false;
	}
	getCurrentTime(el, szcTIMEFORMAT_HHMI);
}
//*********************************************************
// writes current time as a string to ref element
//*********************************************************

function getCurrentTime(el, szcTIMEFORMAT){
		
		var szcTIMEFIELD_SEPERATOR = ':';
		var today = new Date();
		var sHour = today.getHours();
		var sMins = today.getMinutes();
		var sSecs = today.getSeconds(); 
		var sTime;
		
		sHour = twoDigitMask(sHour);
		sMins = twoDigitMask(sMins);
		sSecs = twoDigitMask(sSecs);
		
		if (szcTIMEFORMAT) {
			switch (szcTIMEFORMAT){
		
				case szcTIMEFORMAT_HHMI:
				sTime = sHour + szcTIMEFIELD_SEPERATOR+ sMins;
				break;
				
				default:	
				sTime = sHour + szcTIMEFIELD_SEPERATOR+ sMins + szcTIMEFIELD_SEPERATOR + sSecs;
				break;
			}
		
		}
		else
			sTime = sHour + szcTIMEFIELD_SEPERATOR+ sMins + szcTIMEFIELD_SEPERATOR + sSecs;
		
		el.value = sTime;
		el.focus();
		
}


//**************************************************************************
// Forces a number to have two digits by prepending a "0"
//*********************************************************************
function twoDigitMask(i){
	
	if (i < 10){
		i = '0' + i;
	}
	
	return i;
}
//*********************************************************
// writes current date as a string to ref element
//*********************************************************
function writeTodaysDateToField(el){
		
		if (el.value!=''){
			return false;
		}
		
		 getTodaysDate(el);

}
//*********************************************************
function getTodaysDate(el){

	var today = new Date();
	var sDate = dateToUKString(today);

	el.value = sDate;
		
}

//*********************************************************
function dateToUKString(date){

	var szcDATEFIELD_SEPERATOR = '/';	
	var sDay = date.getDate();
	var sMonth = date.getMonth()+1;
	var sYear = date.getYear(); 
	
	
	if (sDay < 10){
		sDay = '0' + sDay;
	}
	
	if (sMonth < 10){
		sMonth = '0' + sMonth;
	}

	var sDate = sDay + szcDATEFIELD_SEPERATOR+ sMonth + szcDATEFIELD_SEPERATOR + sYear;
	return sDate;
}
//*********************************************************
function dateTo24hTimeString(date){

	var szcTIMEFIELD_SEPERATOR = ':';	
	
	var sHours = date.getHours();
	var sMinutes = date.getMinutes();
	var sSecs = date.getSeconds(); 
	
	
	if (sHours < 10){
		sHours = '0' + sHours;
	}
	
	if (sMinutes < 10){
		sMinutes = '0' + sMinutes;
	}

	if (sSecs < 10){
		sSecs = '0' + sSecs;
	}
	
	var sTime = sHours + szcTIMEFIELD_SEPERATOR + sMinutes + szcTIMEFIELD_SEPERATOR + sSecs;
	return sTime;
	

}


//*********************************************************
function dateToDateTimeString(date){

	var szcDATE_TIME_SEPERATOR = ' ';	

	var szDateTime = dateToUKString(date) + szcDATE_TIME_SEPERATOR + dateTo24hTimeString(date);
	
	return szDateTime;
}



//******************************************************************
// makes a date object from a string formatted as DD/MM/YYYY
//******************************************************************
function dateFromGBShortDateString(szDateString){

	var arr_splitDate = szDateString.split('/');
	var szDay = parseInt(arr_splitDate[0]);
	var szMonth = parseInt(arr_splitDate[1])-1;
	var szYear = parseInt(arr_splitDate [2]);
	
	var date = new Date();
	date.setMonth(szMonth);
	date.setDate(szDay);
	date.setYear(szYear);
	return date;

}

