// 
// Auto validation Pack v1.4 (Jul 04)
// by Judah Lim (i.am@directory.per.sg), Copyright 2001,2002
//
// Copyright (c) 2001, all rights reserved
// No part of this script may be copied, modified, used or distributed without
// prior knowledge or approval of the author.
//
// Please send all requests and correspondences to the above email address.
//
// ---------------------------------------------------------------------------

bIntelliCase = true;

intIE	= (parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5, navigator.userAgent.indexOf( ";", navigator.userAgent.indexOf("MSIE")+5))));

IsIE	= (navigator.userAgent.indexOf("MSIE")!=-1) ? true : false;
IsIE4	= (IsIE && intIE >= 4.0);
IsIE6	= (IsIE && intIE >= 6.0);

IsNS4	= (document.layers);
IsIENS6	= (document.all||document.getElementById);

dtToday = new Date();

function IsEmpty(blnValid, objElement, vMsg)
{
	// Description - Check if form text field is empty
	// Requires - boolean on validity status, form field object, error message	
	
	// if form is still valid, continue test
	if (blnValid)
	{
		if (objElement.value=="")
		{
			// form is no longer valid
			blnValid = false;
			alert(vMsg); objElement.focus();
		}
	}
	// return validity result
	return blnValid;
}

function IsSelected(blnValid, objElement, iIndex, vMsg)
{
	// Description - Check if form select field is selected at particular index
	// Requires - boolean on validity status, form field object, index number, error message	

	// if form is still valid, continue test
	if (blnValid)
	{
		if (objElement.selectedIndex==iIndex)
		{
			// form is no longer valid
			blnValid = false;
			alert(vMsg); objElement.focus();
		}
	}
	// return validity result
	return blnValid;
}

function IsChecked(blnValid, objElement, iIndex, vMsg)
{
	// Description - Check if form checkbox field is selected at particular index, -1 if checked at any
	// Requires - boolean on validity status, form field object, index number, error message	
	
	// if form is still valid, continue test
	if (blnValid)
	{
		if (iIndex==-1)
		{
			// check if any of the checkbox checked (full range)
			blnChecked = false;
			
			// if only one element, use simple check, else use array check
			if (isNaN(objElement.length)) {
				blnChecked=objElement.checked;
			} else {
				for (var i=0; i<objElement.length; i++)
				{
					if (!blnChecked) blnChecked=objElement[i].checked;
				}
			}
			
			if (!blnChecked)
			{
				// form is no longer valid
				blnValid = false;
				alert(vMsg); //objElement.select();
			}
		} else {
			// check if particular checkbox checked
			if (!objElement[iIndex].checked)
			{
				// form is no longer valid
				blnValid = false;
				alert(vMsg);// objElement.focus();
			}
		}
	}
	// return validity result
	return blnValid;
}

function ValidateEMail(blnValid, field)
{
	// check only if not empty, to ensure entry, use IsEmpty
	if ((field.value!="")&&(blnValid))
	{
		if (window.RegExp)
		{
			// browser support RegExp
			var rx1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)");
			var rx2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
				
			if (!rx1.test(field.value) && rx2.test(field.value)) 
			{
			}
			else
			{
				alert("The email address provided is invalid. Examples of proper emails are,\n\n     user@singnet.com.sg        office@pacific.net.sg\n     staff@company.com.sg     manager@business.com");
				field.focus(); field.select(); return false;
			}
		}
		else
		{
			// browser don't support RegExp
			if (field.value.indexOf("@")>=0)
			{
				var validchar=".@-_0123456789abcdefghijklmnopqrstuvwxyz"
				var strin = field.value.toLowerCase();
				var strlen = field.value.length;
				var strout = "";
			
				for (var i=0; i<strlen; i++)
				{
					tempchar = strin.substring(i, i+1);
					if (validchar.indexOf(tempchar)!=-1)
						strout = strout + tempchar;
				}
				field.value = strout;
			}
			else
			{
				alert("The email address provided is invalid. Examples of proper emails are,\n\n     user@singnet.com.sg        office@pacific.net.sg\n     staff@company.com.sg     manager@business.com");
				field.focus(); field.select(); return false;
			}
		}
	}
	field.value = field.value.toLowerCase();
	return blnValid;
}

function ValidateNum(field)
{
	var validchar=".-0123456789"
	var strin = field.value.toLowerCase();
	var strlen = field.value.length;
	var strout = "";

	for (var i=0; i<strlen; i++)
	{
		tempchar = strin.substring(i, i+1);
		if (validchar.indexOf(tempchar)!=-1)
			strout = strout + tempchar;
	}
	
	field.value = strout;
}

function ValidateUserID(field)
{
	var validchar="_0123456789abcdefghijklmnopqrstuvwxyz"
	var strin = field.value.toLowerCase();
	var strlen = field.value.length;
	var strout = "";

	for (var i=0; i<strlen; i++)
	{
		tempchar = strin.substring(i, i+1);
		if (validchar.indexOf(tempchar)!=-1)
			strout = strout + tempchar;
	}
	
	field.value = strout;
}

function ValidateTelNum(field)
{
	var validchar="+()-0123456789";
	var strin = field.value.toLowerCase();
	var strlen = field.value.length;
	var strout = "";

	for (var i=0; i<strlen; i++)
	{
		tempchar = strin.substring(i, i+1);
		if (validchar.indexOf(tempchar)!=-1)
			strout = strout + tempchar;
	}
	
	field.value = strout;
}

function InitCap(field)
{
	if (bIntelliCase)
	{
		var strin = field.value.toLowerCase();
		var strlen = field.value.length;	
		var tempchar, strtmp1, strtmp2;
		
		tempchar = strin.substring(0,1).toUpperCase();
		strtmp1 = strin.substring(1,strlen);
		strin = tempchar + strtmp1;
		
		for (i = 1; i < strlen; i++)
		{
			tempchar = strin.substring(i, i+1);
			if (tempchar == " " && i < (strlen-1))
			{
				tempchar = strin.substring(i+1, i+2).toUpperCase();
				strtmp1 = strin.substring(0, i+1);
				strtmp2 = strin.substring(i+2,strlen);
				strin = strtmp1 + tempchar + strtmp2;
			}
		}
		field.value = strin;
	}
}


// generate unique number (vRunner)
vRunner = "";
if (dtToday.getMonth()<10-1) vRunner += "0";
vRunner += dtToday.getMonth()+1;

if (dtToday.getDate()<10) vRunner += "0";
vRunner += dtToday.getDate() + "-";

if (dtToday.getHours()<10) vRunner += "0";
vRunner += dtToday.getHours();

if (dtToday.getMinutes()<10) vRunner += "0";
vRunner += dtToday.getMinutes();

iTmp = dtToday.getMilliseconds();
if (iTmp<100) vRunner += "0";
if (iTmp<10) vRunner += "0";
vRunner += iTmp;
