var ns4=document.layers?1:0
var ie4=document.all?1:0
var ns6=document.getElementById&&!document.all?1:0

function validateNumber(obj, strMessage)
{
	if (obj.value == "")
	{
		alert(strMessage);
		obj.focus();
		return false;
	}

   var ValidChars = "0123456789., ";
   var IsNumber = true;
 
   for (i = 0; i < obj.value.length && IsNumber; i++) 
	{ 
		if (ValidChars.indexOf(obj.value.charAt(i)) == -1) 
		{
			IsNumber = false;
		}
	}
	if (!IsNumber)
	{
		alert(strMessage);
		obj.focus();
		return false;
	}
	return true;
}
function validateCurrency(obj, strMessage)
{
	if (obj.value == "")
	{
		alert(strMessage);
		obj.focus();
		return false;
	}

   var ValidChars = "$0123456789., ";
   var IsNumber = true;
 
   for (i = 0; i < obj.value.length && IsNumber; i++) 
	{ 
		if (ValidChars.indexOf(obj.value.charAt(i)) == -1) 
		{
			IsNumber = false;
		}
	}
	if (!IsNumber)
	{
		alert(strMessage);
		obj.focus();
		return false;
	}
	return true;
}
function validateText(obj, strMessage)
{
	if (obj.value == "")
	{
		alert(strMessage);
		obj.focus();
		return false;
	}
	return true;
}

function validateName(obj, strMessage)
{
   var filter = /[a-zA-Z0-9]+[a-zA-Z0-9]+[a-zA-Z0-9]+/
	if (obj.value.length < 3)
	{
		alert(strMessage);
		obj.focus();
		return false;
	}
	if (!filter.test(obj.value))
	{
		alert(strMessage);
		obj.focus();
		return false;
	}
	return true;
}
function validateEmail(obj, strMessage)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (obj.value.length==0 || !filter.test(obj.value))
	{
		alert(strMessage);
		obj.focus();
		return false;
	}
	return true;
}
function validateFutureDate(obj, strMessage)
{
	if (!isDate(obj.value))
	{
		alert(strMessage);
		obj.focus();
		return false;
	}
	var daysInMonth = DaysArray(12)
	var pos1=obj.value.indexOf("/")
	var pos2=obj.value.indexOf("/",pos1+1)
	var strDay=obj.value.substring(0,pos1)
	var strMonth=obj.value.substring(pos1+1,pos2)
	var strYear=obj.value.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++)
	{
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)-1
	day=parseInt(strDay)
	year=parseInt(strYr)
	var now = new Date();
	var d2 = new Date(year, month, day);
	var day = 1000*60*60*24;
	var diff = d2.getTime() - now.getTime();
	if (diff < 0)
	{
		alert(strMessage + " Date must be in the future.");
		obj.focus();
		return false;
	}
	return true;
}
function validatePastDate(obj, strMessage)
{
	if (!isDate(obj.value))
	{
		alert(strMessage);
		obj.focus();
		return false;
	}
	var daysInMonth = DaysArray(12)
	var pos1=obj.value.indexOf("/")
	var pos2=obj.value.indexOf("/",pos1+1)
	var strDay=obj.value.substring(0,pos1)
	var strMonth=obj.value.substring(pos1+1,pos2)
	var strYear=obj.value.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++)
	{
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)-1
	day=parseInt(strDay)
	year=parseInt(strYr)
	var now = new Date();
	var d2 = new Date(year, month, day);
	var day = 1000*60*60*24;
	var diff = d2.getTime() - now.getTime();
	if (diff > 0)
	{
		alert(strMessage + " Date must be in the past.");
		obj.focus();
		return false;
	}
	return true;
}
function validateDate(obj, strMessage)
{
	if (!isDate(obj.value))
	{
		alert(strMessage);
		obj.focus();
		return false;
	}
	return true;
}
function validateCheckbox(obj, strMessage)
{
	var bFound = false;
	if (obj.length)
	{
		for (i=0;i<obj.length;i++)
		{
			if (obj[i].checked)
			{
				bFound = true;
				break;
			}
		}
	}
	else
	{
		bFound = obj.checked;
	}
	if (!bFound)
	{
		alert(strMessage);
		if (obj.length)
			obj[0].focus();
		else
			obj.focus();
		return false;
	}
	return true;
}

function validateSelect(obj, strMessage)
{
	var nIndex = obj.selectedIndex;
	if (obj.options[nIndex].value == "")
	{
		alert(strMessage);
		obj.focus();
		return false;
	}
	return true;
}

function doLogout()
{
	var strLogoutLink = "/cmws/logout.jsp";
	if (location.href.indexOf("cmws")==-1)
	{
		strLogoutLink = "/logout.jsp";
	}
	var msg = (strRegistrationStatus.length==0 || (strRegistrationStatus.indexOf("o")>=0 && strRegistrationStatus.indexOf("i")>=0 && strRegistrationStatus.indexOf("p")>=0)) ?
		"Are you sure you want to log out from the system?" :
		"You have not finished the registration process. Please note your services can not be used by associated clients until registration is completed.\rAre you sure you want to log out from the system?";
	if (confirm(msg))
	{
		xmlHttpReq=null
		if (window.XMLHttpRequest)
		{
			xmlHttpReq=new XMLHttpRequest()
		}
		else if (window.ActiveXObject)
		{
			xmlHttpReq=new ActiveXObject("Microsoft.XMLHTTP")
		}
	
		if (xmlHttpReq)
		{
			try
			{
				xmlHttpReq.open('POST', strLogoutLink, true);
				xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				xmlHttpReq.send("t="+(new Date()).getTime());
			}
			catch(e)
			{
				alert(e);
			}
		}
		else
		{
			alert("Error: Unable to logout.");
		}
		if (location.href.indexOf("cmws")==-1)
		{
			location.href="/";
		}
		else
		{
			location.href="/cmws/";
		}
		
	}
}

/**
 * DHTML date validation script for dd/mm/yyyy. Courtesy of SmartWebby.com
(http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/"; var minYear=1965; var maxYear=2099;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 ==
0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dtStr)
{
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++)
	{
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1)
	{
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
	{
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
	{
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
	return true
}

var ns4 = (document.layers);
var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById);
var ns6 = (!document.all && document.getElementById);
function getById(id)
{
	// Netscape 4
	if(ns4){
		return document.layers[id];
	}
	// Explorer 4
	else if(ie4){
		return document.all[id];
	}
	// W3C - Explorer 5+ and Netscape 6+
	else if(ie5 || ns6){
		return document.getElementById(id);
	}
}
























/* http://www.alistapart.com/articles/zebratables/ */
function removeClassName (elem, className) {
	elem.className = elem.className.replace(className, "").trim();
}

function addCSSClass (elem, className) {
	removeClassName (elem, className);
	elem.className = (elem.className + " " + className).trim();
}

String.prototype.trim = function() {
	return this.replace( /^\s+|\s+$/, "" );
}

function stripedTable() {
	if (document.getElementById && document.getElementsByTagName) {  
		var allTables = document.getElementsByTagName('table');
		if (!allTables) { return; }

		for (var i = 0; i < allTables.length; i++) {
			if (allTables[i].className.match(/[\w\s ]*scrollTable[\w\s ]*/)) {
				var trs = allTables[i].getElementsByTagName("tr");
				for (var j = 0; j < trs.length; j++) {
					removeClassName(trs[j], 'alternateRow');
					addCSSClass(trs[j], 'normalRow');
				}
				for (var k = 0; k < trs.length; k += 2) {
					removeClassName(trs[k], 'normalRow');
					addCSSClass(trs[k], 'alternateRow');
				}
			}
		}
	}
}

function disableForm(f)
{
	for (var i=0; i<f.elements.length; i++)
	{
		f.elements[i].disabled = true;
	}
}

function hideElement(eleId)
{
	var ele = document.getElementById(eleId);
	if (ele) ele.style.display = "none";
}

function showElement(eleId)
{
	var ele = document.getElementById(eleId);
	if (ele) ele.style.display = "block";
}

function showCursorHand(obj)
{
	obj.style.cursor = "hand";
}

function hideCursorHand(obj)
{
	obj.style.cursor = "";
}

function showBackgroundBlocker()
{
	var nWidth, nHeight;
	nWidth  = (document.body.scrollWidth>document.body.clientWidth ? document.body.scrollWidth : document.body.clientWidth);
	nHeight = (document.body.scrollHeight>document.body.offsetHeight ? document.body.scrollHeight-2 : document.body.offsetHeight);
	document.getElementById("backgroundBlocker").style.width = nWidth;
	document.getElementById("backgroundBlocker").style.height = nHeight;
	showElement("backgroundBlocker");
}

function hideBackgroundBlocker()
{
	hideElement("backgroundBlocker");
}
/*
	<style type="text/css">
	div#splash_progress        {display:none; background: #000000; position:absolute; z-index: 999999;}
	.div_progress              {background: #000000; color:#000; font: bold 17px arial; z-index: 999999;padding: 5px; text-align:center; width:280px}
	.div_progress img          {margin: 10px auto 0}
	.div_progress-inner        {background: #fff; padding: 20px}
	div#backgroundBlocker      {cursor:not-allowed; position:absolute; top:0; left:0; width:0; height:0; -moz-opacity:0.80; opacity:0.80; filter:alpha(opacity=80); background-color:#fff; z-index:100; display:none; padding:0; margin:0;}
	</style>
	<input type="button" onClick="showProgress('Please wait...');" value="Progress">

	<div id="splash_progress">
		<div class="div_progress">
			<div class="div_progress-inner"><span id="span_progress">Updating...</span><img src="images/activity.gif" align="absmiddle" style="border-width:0px;" /></div>
		</div>
	</div>
	<!-- Start Background Blocker -->
	<div id="backgroundBlocker"></div>
	<!-- End Background Blocker -->
*/

function showProgress(strMessage)
{
	if (strMessage!="") document.getElementById("span_progress").innerHTML = strMessage;
	else document.getElementById("span_progress").innerHTML = "Please wait ...";
	showBackgroundBlocker();

	var ele = document.getElementById("splash_progress");
	showElement("splash_progress");
	if (ele && ele.style.display!="none")
	{
		nWidth  = (document.body.clientWidth/2) - ele.clientWidth/2;
		nHeight = (document.body.clientHeight/2) - ele.clientHeight/2;
		ele.style["top"]  = document.body.scrollTop + 20;
		ele.style["left"] = nWidth;
	}
}

function showProgressBar(bDelay)
{
	showProgress("Processing your request ...");
}

function hideProgress()
{
	hideBackgroundBlocker();
	hideElement("splash_progress");
}



// To do with Dialog Boxes
window.onresize = _onResizeWindow = function()
{
	if (document.getElementById("auditLogDialog") && (document.getElementById("auditLogDialog").style.display == "block"))
	{
		doCloseDialog();
		doOpenDialog();
	}
};

function doRefreshDialogPosition()
{
	if (document.getElementById("auditLogDialog").style.display == "none") return;
	var nWidth  = (document.body.scrollWidth>document.body.clientWidth   ? document.body.scrollWidth : document.body.clientWidth);
	document.getElementById("auditLogDialog").style.left  = 0 + nWidth/2 - 800/2;
	document.getElementById("auditLogDialog").style.top = document.body.scrollTop + 20;
	document.getElementById("auditLogDialog").style.zIndex = 101;
}

function doOpenDialog()
{
	showBackgroundBlocker();
	showElement('auditLogDialog');
	doRefreshDialogPosition();
	
}

function doCloseDialog()
{
	hideElement('auditLogDialog');
	hideBackgroundBlocker();
	
	var listitems= document.getElementsByTagName("select");
	for (i=0; i<listitems.length; i++)
	{
		listitems[i].style.display = "none";
	}
}


function doShowDetailAuditLog(sectionLabel)
{
	$('auditLogShowForm').pGoto.value = "contractorDetails.jsp";
	doShowAuditLog(sectionLabel);
}

function doShowInsuranceAuditLog(sectionLabel)
{
	$('auditLogShowForm').pGoto.value = "insurance.jsp";
	doShowAuditLog(sectionLabel);
}

function doShowOhsAuditLog(sectionLabel)
{
	$('auditLogShowForm').pGoto.value = "ohs.jsp";
	doShowAuditLog(sectionLabel);
}

function doShowNoPersonnelAuditLog(sectionLabel)
{
	$('auditLogShowForm').pGoto.value = "personnel.jsp";
	doShowAuditLog(sectionLabel);
}

function doShowPersonnelAuditLog(sectionLabel)
{
	$('auditLogShowForm').pGoto.value = "personnel_view.jsp";
	$('auditLogShowForm').pEmployeeId.value = $('personnel_view_employee').pEmployeeId.value;
	doShowAuditLog(sectionLabel);
}

function doShowAuditLog(sectionLabel)
{
	$('auditLogShowForm').pSectionLabel.value = sectionLabel;
	showProgress("Querying database ..");
	new Ajax.Request('auditLogLabelList.jsp',
	{
		method: 'post',
		parameters: $('auditLogShowForm').serialize(true),
		onSuccess: function(transport){
			var listitems= document.getElementsByTagName("select");
			for (i=0; i<listitems.length; i++)
			{
				listitems[i].style.display = "none";
			}
			var results = $('span_dialog');
			results.update(transport.responseText);
			hideProgress();
			doOpenDialog();
		},

		onFailure: function(){ alert('Something went wrong...') }
	});
}

function doUpdateAuditLog()
{
	$F('pAction').value = "update";
	if ($F('pUpdateAuditBy')=="")
	{
		alert("Please set the Personnel field");
		return;
	}
	if ($F('pUpdateComments')=="")
	{
		if (!confirm("Are you sure you do not want to leave a comment?")) return;
	}
	doCloseDialog();
	showProgress("Saving data ..");
	new Ajax.Request('auditLogSave.jsp',
	{
		method: 'post',
		parameters: $('auditLogForm').serialize(true),
		onSuccess: function(transport){
			var results = $('span_dialog');
			results.update(transport.responseText);
			hideProgress();
			doOpenDialog();
		},

		onFailure: function(){ alert('Something went wrong...') }
	});
}

function doNewAuditLog()
{
	$('pAction').value = "new";
	if ($F('pNewStatus')=="")
	{
		alert("Please set a status for the new entry");
		return;
	}
	if ($F('pNewComments')=="")
	{
		if (!confirm("Are you sure you do not want to leave a comment for the new entry?")) return;
	}
	if ($F('pNewAuditBy')=="")
	{
		alert("Please select a value for the Personnel field in the new entry.");
		return;
	}
	if ($('pUpdateComments') && $F('pUpdateComments')=="")
	{
		if (!confirm("Are you sure you do not want to leave a comment for the last entry?")) return;
	}

	if ($('pUpdateAuditBy') && $F('pUpdateAuditBy')=="")
	{
		$('pUpdateAuditBy').selectedIndex = $('pNewAuditBy').selectedIndex;
	}
	doCloseDialog();
	showProgress("Saving data ..");
	new Ajax.Request('auditLogSave.jsp',
	{
		method: 'post',
		parameters: $('auditLogForm').serialize(true),
		onSuccess: function(transport){
			var results = $('span_dialog');
			results.update(transport.responseText);
			hideProgress();
			doOpenDialog();
		},

		onFailure: function(){ alert('Something went wrong...') }
	});
}

function doNewCloseAuditLog()
{
	$('pNew').value = "Close";
	doNewAuditLog();
}


function doDownload(id)
{
	$('downloadResource').action = "download.jsp"; 
	$('downloadResource').pResourceFileId.value = id;
	$('downloadResource').submit();
}