
//____________________________________________________________________
//
//
//		[[[[[    CODE API    ]]]]]
//
//
// ltrim(str)
//		Remove leading spaces from string
// rtrim(str)
//		Remove trailing spaces from string
// trim(str)
//		Remove leading and trailing spaces from string
// pause(milliseconds)
//		Pauses execution for so many milliseconds
// MAX(a, b)
//		Returns the greater
// MIN(a, b)
//		Returns the lesser
// gotoPage(url)
//		Goes to the specified page
// getPosX(obj)
//		Get the x-position of an html object.
// getPosY(obj)
//		Get the y-position of an html object.
// validateRequired(ctrl)
//		Success when control contains some data.
// validateNumber(ctrl, [flags])
//		Success when control contains a number valid according to the flags. The
//		flags are a string containging the following characters: 'i' to ensure
//		it's an integer; 'f' to ensure a float; 'p' to ensure positive; 'z' to
//		ensure it's *not* zero.
// validateYear(ctrl)
//		Success when control contains a valid year.
// validateDate(ctrld, ctrlm, ctrly)
//		Success when d/m/y controls contain a valid date.
// validateEmail(ctrl)
//		Success when control contains a valid email address.
// popupTable(anchorobj, name)
//		call to make a <div> block (called 'div'+name) visible. It must contain
//		a <table> called 'tbl'+name. It is positioned centrally over anchorobj's
// 		co-ordinates (note anchorobj's style *must* have "position: relative;"
//		specified!).
// popupSelector(anchorobj, name, formobj)
//		call to make a <div> block (called 'div'+name) visible. It must contain
//		a <table> called 'tbl'+name, and within that, a <select> called 'sel'+name.
//		It is positioned centrally over anchorobj's co-ordinates (note anchorobj's
//		style *must* have "position: relative;" specified!). 'formobj' is the name
//		of the object that will recieve the <select>'s value as it changes.
// popupHide()
//		Call to hide the current <div> block immediately.
// popupOnChange()
//		Copys the changed value to the form object
// popupOnClick()
//		Ends the popup, refocusing the another object.
// popupOnBlur()
//		Hide the popup immediately

//____________________________________________________________________
//                                                    .timinc.js rev 5


// Fudge me, you know ya wanna!
if(document.all && !document.getElementById) {
    document.getElementById = function(id) {
         return document.all[id];
    }
}

// Popup's object pointers
var popupDivObj = 0;
var popupFormObj = 0;
var popupSelectObj = 0;
var popupOffsetX = 0;
var popupOffsetY = 0;

//____________________________________________________________________
//                                                             C O D E

// ltrim(str)
//		Remove leading spaces from string
function ltrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

// rtrim(str)
//		Remove trailing spaces from string
function rtrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }

   return s;
}

// trim(str)
//		Remove leading and trailing spaces from string
function trim(str)
{
   return rtrim(ltrim(str));
}

// pause(milliseconds)
//		Pauses execution for so many milliseconds
function pause(milliseconds)
{
	var now = new Date();
	var exitTime = now.getTime() + milliseconds;
	while(true) {
		var now = new Date();
		if(now.getTime() >= exitTime) break;
	}
}

// MAX(a, b)
//		Returns the greater
function MAX(a, b)
{
	return a > b? a : b;
}

// MIN(a, b)
//		Returns the lesser
function MIN(a, b)
{
	return a < b? a : b;
}

// gotoPage(url)
//		Goes to the specified page
function gotoPage(url)
{
	window.location.href = url;
}

// getPosX(obj)
//		Get the x-position of an html object.
function getPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

// getPosY(obj)
//		Get the y-position of an html object.
function getPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

// validateRequired(ctrl)
//		Success when control contains some data.
function validateRequired(ctrl)
{
	if(trim(ctrl.value) == "") {
		alert("This is a required field, please enter a value!");
		ctrl.focus();
		return false;
	}
	return true;
}

// validateNumber(ctrl)
//		Success when control contains a valid number.
function validateNumber(ctrl)
{
	return validateNumberX(ctrl, "");
}

// validateNumber(ctrl, [flags])
//		Success when control contains a number valid according to the flags. The
//		flags are a string containging the following characters: 'i' to ensure
//		it's an integer; 'p' to ensure positive; 'z' to ensure it's *not* zero.
function validateNumber(ctrl, flags)
{
	allowzero = allownegative = allowfloat = true;
	valid = true;
	ctrl.value = str = trim(ctrl.value);
	
	// Work out flags
	for(a = 0; a < flags.length; a++)
		switch(flags.charAt(a)) {
		case 'i': allowfloat = false; break;
		case 'p': allownegative = false; break;
		case 'z': allowzero = false; break;
		default:
			alert("JavaScript Error: validateNumberX(): Bad flags!");
			return true;	// validation passes
		}
	
	// Test negative and remove '-'
	if(str.charAt(0) == "-") {
		str = str.substr(1);
		if(!allownegative) valid = false;
	}
	
	// Test chars
	chars = "0123456789"; canseedot = allowfloat;
	for(a = 0; a < str.length; a++)
		if(chars.indexOf(str.charAt(a)) == -1) {
			if(canseedot && str.charAt(a) == ".")
				canseedot = false;		// allow only one
			else
				valid = false;
		}

	// Test zero
	if(valid && !allowzero) {
		res = allowfloat? parseFloat(str) : parseInt(str);
		if(res == 0 || isNaN(res)) valid = false;
	}
	
	// Valid?
	if(!valid) {
		alert("Please enter a valid number!");
		ctrl.focus();
	}
	return valid;
}

// validateYear(ctrl)
//		Success when control contains a valid year.
function validateYear(ctrl)
{
	chars = "0123456789"; valid = true;
	ctrl.value = trim(ctrl.value);
	for(a = 0; a < ctrl.value.length; a++)
		if(chars.indexOf(ctrl.value.charAt(a)) == -1)
			valid = false;
	y = ctrl.value;	
	if(y.length == 2) {
		if(0 + y < 20)	y = "20" + y;
		else			y = "19" + y;
	}
	if(y.length != 4 && y.length != 0) valid = false;
	if(!valid) {
		alert("Please enter a valid year!");
		ctrl.focus();
		return false;
	}
	ctrl.value = y;
	return true;
}

// isLeapYear(year)
//		Returns the number of days in february for a given year. This is usually
//		29. It is only 28 when the year is divisible by 4, UNLESS it is also
//		divisible by 100, BUT NOT where it is also divisible by 400. Follow?
function isLeapYear(year)
{
	return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}

// validateDate(ctrld, ctrlm, ctrly)
//		Success when d/m/y controls contain a valid date.
function validateDate(ctrld, ctrlm, ctrly)
{
	var days_in_month = new Array(31, 99, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	if(trim(ctrld.value) == "" && trim(ctrlm.value) == "" && trim(ctrly.value) == "") {
		ctrld.value = ctrlm.value = ctrly.value = "";
		return true;
	}
	if(!validateYear(ctrly))
		return false;
	day = 0 + ctrld.value; month = 0 + ctrlm.value; year = 0 + ctrly.value;
	if(month < 1 || month > 12) {
		alert("Please enter a valid month!")
		ctrlm.focus();
		return false;
	}
	if(day < 1 || (month == 2 && day > (isLeapYear(year)? 29 : 28)) || day > days_in_month[month]) {
		alert("Please enter a valid day!")
		ctrld.focus();
		return false
	}
	return true
}

// validateEmail(ctrl)
//		Success when control contains a valid email address.
function validateEmail(ctrl)
{
	if(trim(ctrl.value) == "")
		ctrl.value = "";
	else {
		a = ctrl.value.indexOf('@')
		if(a < 0 || ctrl.value.indexOf('.', a) < 0 ) {
			alert("Please enter an email address!");
			ctrl.focus();
			return false;
		}
	}
	return true;
}

// popupTable(anchorobj, name)
//		call to make a <div> block (called 'div'+name) visible. It must contain
//		a <table> called 'tbl'+name. It is positioned centrally over anchorobj's
//		co-ordinates (note anchorobj's style *must* have "position: relative;"
//		specified!).
function popupTable(anchorobj, name)
{
	popupHide();
	tobj = document.all['tbl' + name]
	popupDivObj = document.all['div' + name]
	popupFormObj = popupSelectObj = 0;
	if(!tobj || !popupDivObj) {
		popupOffsetX = popupOffsetY = 0;	// Reset offset
		alert("JavaScript Error: popupTable(): Couldn't find object!");
		return false;
	}
	
	// Popup
	if(popupOffsetX == -1)	desiredx = MAX(0, document.body.clientWidth - tobj.offsetWidth) / 2;
	else					desiredx = getPosX(anchorobj) + popupOffsetX;
	desiredy = getPosY(anchorobj) + popupOffsetY;
	popupDivObj.style.left = MAX(desiredx, 0);
	popupDivObj.style.top = MAX(MIN(desiredy, MAX(document.body.scrollHeight, document.body.clientHeight) - tobj.offsetHeight), 0)
	popupDivObj.style.visibility = "visible";
	popupOffsetX = popupOffsetY = 0;	// Reset offset
	return false;
}

// popupSelector(anchorobj, name, formobj)
//		call to make a <div> block (called 'div'+name) visible. It must contain
//		a <table> called 'tbl'+name, and within that, a <select> called 'sel'+name.
//		It is positioned centrally over anchorobj's co-ordinates (note anchorobj's
//		style *must* have "position: relative;" specified!). 'formobj' is the name
//		of the object that will recieve the <select>'s value as it changes.
function popupSelector(anchorobj, name, formobj)
{
	popupHide();
	tobj = document.all['tbl' + name]
	popupDivObj = document.all['div' + name]
	popupSelectObj = document.all['sel' + name]
	popupFormObj = formobj;
	if(!tobj || !popupDivObj || !popupSelectObj || !popupFormObj) {
		popupOffsetX = popupOffsetY = 0;	// Reset offset
		alert("JavaScript Error: popupSelector(): Couldn't find object!");
		return false;
	}
	popupSelectObj.selectedIndex = -1;	// Deselect before popping up
	desiredx = getPosX(anchorobj) - tobj.offsetWidth + 20 + popupOffsetX;
	desiredy = getPosY(anchorobj) + anchorobj.offsetHeight + popupOffsetY;
	popupDivObj.style.left = MAX(desiredx, 0);
	popupDivObj.style.top = MAX(desiredy, 0)
	popupDivObj.style.visibility = "visible";
	// Select *after* popping up to get IE to make selection visible.
	for(a = 0; a < popupSelectObj.options.length; a++)
		if(popupSelectObj.options[a].text.toLowerCase() == popupFormObj.value.toLowerCase()) {
//			popupSelectObj.selectedIndex = a;
			setTimeout("popupSelectObj.selectedIndex = " + a, 10);
			break;
		}
	popupSelectObj.focus();
	popupOffsetX = popupOffsetY = 0;	// Reset offset
	return false;
}

// popupHide()
//		Call to hide the current <div> block immediately.
function popupHide()
{
	if(popupDivObj) {
		popupDivObj.style.visibility = "hidden";
		popupDivObj.style.left = 0;	// Move back to (0, 0) so browsers dont include
		popupDivObj.style.top = 0;	// hidden <div> in scrollbar page size calculations.
		popupDivObj = 0;
	}
}

// popupOnChange()
//		Copys the changed value to the form object
function popupOnChange()
{
	if(popupFormObj) popupFormObj.value = popupSelectObj.value;
}

// popupOnClick()
//		Ends the popup, refocusing the another object.
function popupOnClick()
{
	popupHide();
	if(popupFormObj) popupFormObj.focus();
}

// popupOnBlur()
//		Hide the popup immediately
function popupOnBlur()
{
	popupHide();
	if(popupFormObj) popupFormObj.focus();
}
