// 
// 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.replace(/^\s*/, '').replace(/\s*$/, ''))=="")
		{
			// form is no longer valid
			blnValid = false; alert(vMsg); objElement.focus(); objElement.value = "";
		}
	}
	
	// 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 (blnValid)
	{
		if (objElement.selectedIndex==iIndex)
		{
			blnValid = false; alert(vMsg); objElement.focus();
		}
	}
	
	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 (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[0].focus();
			}
		}
		else
		{
			// check if particular checkbox checked
			if (!objElement[iIndex].checked)
			{
				// form is no longer valid
				blnValid = false; alert(vMsg); //objElement[0].focus();
			}
		}
	}
	
	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 ValidateAlphaNumeric(blnValid, field)
{
	var strString = field.value.replace(/^\s*/, '').replace(/\s*$/, '').toLowerCase();
	var strValidChars = "0123456789abcdefghijklmnopqrstuvwxyz";
	var strChar;
	for (i = 0; i < strString.length && blnValid == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
			{alert("Alphanumeric Only!"); field.focus(); field.select(); blnValid = false;}
	}
	return blnValid;
}

function ValidateAlpha(blnValid, field)
{
	var strString = field.value.replace(/^\s*/, '').replace(/\s*$/, '').toLowerCase();
	var strValidChars = "abcdefghijklmnopqrstuvwxyz";
	var strChar;
	for (i = 0; i < strString.length && blnValid == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
			{alert("Please enter only alphabets."); field.focus(); field.select(); blnValid = false;}
	}
	return blnValid;
}

function ValidateNumeric(blnValid, field)
{
	var strString = field.value.replace(/^\s*/, '').replace(/\s*$/, '');
	var strValidChars = "0123456789";
	var strChar;
		
	for (i = 0; i < strString.length && blnValid == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
			{alert("Please enter only numbers."); field.focus(); field.select(); blnValid = false;}
	}
	if((blnValid)&&(strString.length>=1)&&(strString.charAt(0)=='0'))
		{alert("Please enter a positive number."); field.focus(); field.select(); blnValid = false;}
	
	return blnValid;
}

function ValidateTel(blnValid, field)
{
	var strString = field.value.replace(/^\s*/, '').replace(/\s*$/, '');
	var strValidChars = "0123456789";
	var strValidStart = "698";
	var strChar;
		
	for (i = 0; i < strString.length && blnValid == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
			{alert("Please enter a valid telephone number."); field.focus(); field.select(); blnValid = false;}
	}
	
	if ((blnValid)&&(strValidStart.indexOf(strString.charAt(0)) == -1 || strString.length < 8))
		{alert("Please enter a valid telephone number."); field.focus(); field.select(); blnValid = false;}
	
	return blnValid;
}

function ValidateFloat(blnValid, field, canBeZero)
{
	var strString = field.value.replace(/^\s*/, '').replace(/\s*$/, '');
	var strValidChars = ".0123456789";
	var strChar;
	var intDotCount = 0;
		
	for (i = 0; i < strString.length && blnValid == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
			{alert("Please enter a proper value."); field.focus(); field.select(); blnValid = false;}
		if (strChar == '.') {intDotCount = intDotCount + 1;}
	}
	
	if (canBeZero == 'yes')
	{
		if((blnValid)&&(strString.length>=1)&&((strString.charAt(0)=='.')||(strString.charAt(strString.length-1)=='.')||(intDotCount>1)))
			{alert("Please enter a proper value."); field.focus(); field.select(); blnValid = false;}
	}
	else
	{
		if((blnValid)&&(strString.length>=1)&&((strString==0)||(strString.charAt(0)=='.')||(strString.charAt(strString.length-1)=='.')||(intDotCount>1)))
			{alert("Please enter a positive number."); field.focus(); field.select(); blnValid = false;}
	}

	return blnValid;
}

//--------------------------------------------------------------------------

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;
	}
}
function getYear(d) 
{ 
	return (d < 1000) ? d + 1900 : d;
}	
function isDate (year, month, day) 
{
	// month argument must be in the range 1 - 12
	month = month - 1; // javascript month range : 0- 11
	var tempDate = new Date(year,month,day);
	if ((getYear(tempDate.getYear()) == year) && (month == tempDate.getMonth()) && (day == tempDate.getDate()))
		return true;
	else
		return false
}
// 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;
