// 
// 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.value=""; 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(); blnValid = 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(); blnValid = false;
			}
		}
	}
	field.value = field.value.toLowerCase();
	return blnValid;
}

function ValidateNumOnly(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 a proper number.");
			field.focus(); blnValid = false;
		}
	}
	if((blnValid)&&(strString.length>1)&&(strString.charAt(0)=='0'))
	{
		alert("Please enter a proper number.");
		field.focus(); blnValid = false;
	}
	
	return blnValid;
}

function ValidateUsername(strString)
{
	var strValidChars = "_0123456789abcdefghijklmnopqrstuvwxyz";
	var strChar;
	var blnResult = true;
	var strString = strString.replace(/^\s*/, '').replace(/\s*$/, '').toLowerCase()
		
	for (i = 0; i < strString.length && blnResult == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			blnResult = false;
		}
	}
	return blnResult;
}

function ValidatePassword(strString)
{
	var strValidChars = "0123456789abcdefghijklmnopqrstuvwxyz";
	var strChar;
	var blnResult = true;
		
	for (i = 0; i < strString.length && blnResult == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			blnResult = false;
		}
	}
	return blnResult;
}

function ValidateFilename(blnValid, field)
{
	// initialise variables
	var strValidChars = "~!@$%^&()_+`{}[]\";',. 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var strChar;
	var blnValid = true;
	var strPath = field.value;
	var strFile = strPath
	
	// determine on directory character
	cDir = "\\"; // assume PC user
	if (strFile.indexOf(cDir) == -1) {
		// maybe it's a Mac user
		cDir = "/";
	}
	
	// extract filename from full path
	pos = 0;
	while(pos>-1){
		pos = strFile.indexOf(cDir);
		strFile = strFile.substr(pos+1);
		//alert(pos + "|" + strFile);
	}
	
	// check filename against valid characters
	for (i = 0; i < strFile.length && blnValid == true; i++)
	{
		strChar = strFile.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			blnValid = false;
		}
	}
	if (blnValid == false)
	{
		alert("The filename is invalid, please rename the file to only alpha-numeric characters.");
		field.focus();
	}
	
	// check filename length
	if (blnValid == true && strFile.length > 45) {
		alert("The filename is too long, please shorten the filename.");
		field.focus(); blnValid = false;
	}
	
	return blnValid;
}

function ValidateNRIC(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 valid NRIC/PP number.\nLast 4 digits only.");
			field.focus(); blnValid = false;
		}
	}
	
	return blnValid;
}

function ValidateContact(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 valid Contact Number."); field.focus(); field.select(); blnValid = false;}
	}
	
	if ((blnValid)&&(strValidStart.indexOf(strString.charAt(0)) == -1 || strString.length < 8))
		{alert("Please enter valid Contact Number."); field.focus(); field.select(); blnValid = false;}
	
	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;

