//Check if field is empty
function isEmpty(sField)
{
	if (sField.length <= 0)
	{
		return true;
	}
	return false;
}

//check if field contains blank spaces
function isSpace(sField)
{
	var empty_str, result;
	empty_str = ' ';
	result = sField.search(empty_str);
	return result;
}

// sField must be all digits
function isAllDigit(sField)
{
	for (var i=0; i<sField.length; i++)
	{
		var cChar = sField.charAt(i);
		if (cChar < '0' || cChar > '9')
		{
			return false;
		}
	}
	return true;
}

// sField must be all alphaberts
function isAllAlphabet(sField)
{
	for (var i=0; i<sField.length; i++)
	{
		var cAscii = sField.charCodeAt(i);
		if (cAscii < 65 || (cAscii > 90 && cAscii < 97) || cAscii > 122)
		{
			return false;
		} 
	}
}

//validate telephone field
function cktelephone(sField)
{
	var no_count, no_signs;
	no_count = 1;
	no_signs = new Array("1","2","3","4","5","6","7","8","9","0","+","-","(",")");

	
	for (var i=0; i<sField.length; i++)
	{
		var flag;
		flag = 1;

		var cChar = sField.charAt(i);
		for (no_count = 0; no_count < 14; no_count ++)
		{
			if (cChar.indexOf(no_signs[no_count], 0) >= 0)
			{
				flag = 2;
			}
		}
		
		if (flag == 1)
		{
			alert("Telephone number can only contain digits and symbols ' + - ( ) '.");
			return false;
		}
	}
	return true;
}

//validate login id and password for invalid character
function cklogin(sField,sType)
{
	// make sure invalid ascii characters is not in the string
	var no_count, no_signs, result;
	no_count = 0;
	if (sType == "ID")
		no_signs = new Array("[","]","?","\\","/",";",":","|","=","'",'"');
	else
	if (sType == "PWD")
		no_signs = new Array("'",'"');

	for (no_count = 0; no_count < no_signs.length; no_count ++)
	{
		if (sField.indexOf(no_signs[no_count], 0) >= 0)
		{
			if (sType == "ID")
				alert("Invalid characters:\n" + " [ " + " ] " + " ? " + " \\ " + " / " + " ; " + " : " + " | " + " = " + ' " ' + " ' ");
			else
			if (sType == "PWD")
				alert("Invalid characters:\n" + ' " ' + " ' ");
			
			return false;
		}
	}
}

//validate filename for invalid character
function ckfilename(sField)
{
	// make sure invalid ascii characters is not in the string
	var no_count, no_signs, result;
	no_count = 0;
	no_signs = new Array("\\","/",":","*","?","'",'"',"<",">","|");

	for (no_count = 0; no_count < 10; no_count ++)
	{
		if (sField.indexOf(no_signs[no_count], 0) >= 0)
		{
			alert("Invalid characters:\n" + '\\ / : * ' + "'" + ' " < > |');
			return false;
		}
	}
}


//validate email address
function ckemail(input)
{
	if (input == "")
	{
		alert("Email address field cannot be left empty.");
		return false;
	}
	else
	if (input.indexOf(" ", 0) >= 0)
	{
		alert("Space ' ' is not valid in an email address.");
		return false;
	}
	else
	if(input.indexOf('@', 0) == -1)
	{
		alert("Missing '@' in the email address!");
		return false;
	}
	else
	if(input.indexOf('.', 0) == -1)
	{
		alert("Missing '.' in the email address!");
		return false;
	}
	else
	if (input.length < 6)
	{
		alert("Email address is of incorrect length!\nMinimum of 6 characters required!");
		return false;
	}
	else
	{
		var counter;
		var count = 0;
		var real_count = 0;

		for (counter = count; counter < input.length; counter ++)
		{
			if (input.indexOf("@", count) >= 0)
			{
				count = input.indexOf("@", count) + 1;
				real_count = real_count + 1;
			}
		}

		if (real_count > 1)
		{
			alert("There cannot be more than one '@' sign in the email address!");
			return false;
		}
	}


	// make sure the first part of the email address is at least 1 character long
	if (input.lastIndexOf('@') < 1)
	{
		alert("The part of email address before '@' is of invalid length!\nMinimum of 1 characters required!");
		return false;
	}
	
	// make sure the last part of the email address is at least 2 character long
	if (input.length - input.lastIndexOf('.') - 1 < 2)
	{
		alert("The part of email address after the last '.' is of invalid length!\nMinimum of 2 characters required!");
		return false;
	}
	
	// make sure the last part of the email address is not more than 3 characters long
	if (input.length - input.lastIndexOf('.') - 1 > 3)
	{
		alert("The part of email address after the last '.' is of invalid length!\nMaximum of 3 characters only!");
		return false;
	}

	// make sure invalid ascii characters is not in the string
	var no_count, no_signs;
	no_count = 0;
	no_signs = new Array("~","`","!","#","$","%","^","&","*","(",")","+","=","|","\\","[","]","{","}",":",";","'",'"',"<",">",",","?","/");

	for (no_count = 0; no_count < 28; no_count ++)
	{
		if (input.indexOf(no_signs[no_count], 0) >= 0)
		{
			alert("' " + no_signs[no_count] + " ' sign is not valid in an email address.");
			return false;
		}
	}


	// make sure no two valid characters are together
	var dup_count;
	var dup_signs;
	//dup_signs = new Array("@@","@.","@_","@-",".@","..","._",".-","_@","_.","__","_-","-@","-.","-_","--");
    // changed on 090409 so that xxx_@hotmail.com is considered as a valid email address.
    //changed on 220409 so that xxx-@hotmail.com is considered a valid email address.
    
    dup_signs = new Array("@@","@.","@_","@-",".@","..","._",".-","_.","__","_-","-.","-_","--");
    
	for (dup_count = 0; dup_count < 16; dup_count ++)
	{
		if (input.indexOf(dup_signs[dup_count], 0) >= 0)
		{
			alert("'" + dup_signs[dup_count] + "' sign is not valid in an email address.");
			return false;
		}
	}
}


// Display Alert before deleting record
function ConfirmDelete(myform)
{
    var flag = 0;
	var checkbox_cnt = 0;
	
	if (myform.DelList == "undefined" || myform.DelList == undefined)
		return false;
	
	if (myform.DelList.length == "undefined" || myform.DelList.length == undefined)
		checkbox_cnt = 1;
	else
		checkbox_cnt = myform.DelList.length;
	
	if (checkbox_cnt == 1)
	{
		if (myform.DelList.checked == true)
			flag = 1;
	}
	else
	{
	    for (var i=0; i < checkbox_cnt; i++)
	    {
	         if (myform.DelList[i].checked == true)
	              flag = 1;
	    }
	}

    if (flag == 0)
    {
         alert("No record selected for removal.");
		 return false;
    }
	else
	{
		if (confirm("The selected record will be removed from the system.\nDo you wish to continue?") == false)
			return false;
		else
			return true;
	}
}

//validate date
function ckedate(input)
{
	if (input == "")
	{
		alert("Date field cannot be left empty.");
		return false;
	}
	else
	if (input.indexOf(" ", 0) >= 0)
	{
		alert("Space ' ' is not valid in an email address.");
		return false;
	}
	else
	if(input.indexOf('/', 0) == -1)
	{
		alert("Missing '/' in the date!");
		return false;
	}
	else
	if (input.length < 8)
	{
		alert("Date is of incorrect length!\nMinimum of 8 characters required!");
		return false;
	}
	else
	{
		var counter;
		var count = 0;
		var real_count = 0;

		for (counter = count; counter < input.length; counter ++)
		{
			if (input.indexOf("/", count) >= 0)
			{
				count = input.indexOf("/", count) + 1;
				real_count = real_count + 1;
			}
		}

		if (real_count > 2)
		{
			alert("There cannot be more than one '/' sign in the date");
			return false;
		}
	}

	// make sure invalid ascii characters is not in the string
	var no_count, no_signs;
	no_count = 0;
	no_signs = new Array("~","`","!","#","$","%","^","&","*","(",")","+","=","|","\\","[","]","{","}",":",";","'",'"',"<",">",",","?");

	for (no_count = 0; no_count < 28; no_count ++)
	{
		if (input.indexOf(no_signs[no_count], 0) >= 0)
		{
			alert("' " + no_signs[no_count] + " ' sign is not valid in a date");
			return false;
		}
	}

	// make sure alphabets is not in the string
	var nocount, nosigns;
	nocount = 0;
	nosigns = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");

	for (nocount = 0; nocount < 28; nocount ++)
	{
		if (input.indexOf(nosigns[nocount], 0) >= 0)
		{
			alert("Alphabets is not valid in a date");
			return false;
		}
	}

	// make sure no two valid characters are together
	var dup_count;
	var dup_signs;
	dup_signs = new Array("//");

	for (dup_count = 0; dup_count < 1; dup_count ++)
	{
		if (input.indexOf(dup_signs[dup_count], 0) >= 0)
		{
			alert("'" + dup_signs[dup_count] + "' sign is not valid in a date");
			return false;
		}
	}
}

function isNRIC(strNRIC) {
	if (strNRIC.length!=9) {
		alert("NRIC number must be 9 characters e.g x1234567y");
		return false;
	}
	if (isAllDigit(strNRIC.substr(0,1)))
	{
		alert("First character of NRIC must be an alphabet e.g x1234567y");
		return false;
	}
	if (isAllDigit(strNRIC.substr(8,1)))
	{
		alert("Last character of NRIC must be an alphabet e.g x1234567y");
		return false;
	}
	if (!isAllDigit(strNRIC.substr(1,7)))
	{
		alert("The middle 7 characters of NRIC must be numeric e.g x1234567y");
		return false;
	}
	
	return true;
}