/*
ACTIVE 8 JAVASCRIPT CODING STANDARDS IN LIBRARY, updated by Ethan Chan 26 Dec, 2003

To add new functions please read this before proceeding
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Check[ field, type, description name ]

Is the naming template for validation functions in this library, which has the following parameters
1. object ( field name/id, CheckXXX(this.formName.fieldName) )
2. message (alert message)

Validates the object field value and pops up an alert box from browser with message string
----------------------------------------------------------------------------------------------------

Is[ field, type, description name ]

Is the naming template for validation functions in this library, which has the following parameters
1. value/val ( please read documentation ontop of function to understand usage )
2. other values ( as required in the specifications doc )

Validates the object field value and returns either a true or false
----------------------------------------------------------------------------------------------------

FUNCTIONS documentation template, please copy from here and paste obove new function you have just declared

Function description----------------------------------
[enter function description here]
Pre condition - 
	1. [enter pre condition of parameter passed in here, e.g. parameter type]
Post condition -
	1. [enter post condition of function here, what value is return?]
------------------------------------------------------

*/


//************************************START OF CHECK[] FUNCTIONS**************************************

function isPwdEquals(o1, o2, n, message) {
	if (o1.value != o2.value) {
		alert(message);
		if (n == 1) {
			o1.focus();
			o1.select();
		} else {
			o2.focus();
			o2.select();
		}
		return false;
	} else {
		return true;
	}
}


/*
Function description----------------------------------
Checks if the object value in the text field is blank
displays message when the selected value in the text field is blank
------------------------------------------------------
*/
function CheckBlank(object, message) {
	if (strIsBlank(object)) {
		alert(message);
		object.focus();
		object.select();
		nextflag = false;
		return false;
	}
	else
	{
		return true;	
	}
}


/*
Function description----------------------------------
Checks if the object value in the combo box is blank
displays message when the selected value in the combo box is blank
------------------------------------------------------
*/
function CheckBlankcbo(object, message) {
	if (strIsBlank(object)) {
		alert(message);
		object.focus();
		nextflag = false;
		return false;
	}
	else
	{
		return true;	
	}
}
/*
Function description----------------------------------
Checks if the object value is a number (fit for currency value)
displays message if object field is blank or fails isValidCurr
------------------------------------------------------
*/

function CheckNumber(object, message) {
	//alert(strIsBlank(object));
	if (strIsBlank(object)) {
		alert(message);
		object.focus();
		object.select();
		nextflag = false;		
		return false;
	} else if (isValidCurr(object)==false) {
	//} else if (isNaN(strTrim(object))) {
		alert(message);
		object.focus();
		object.select();
		nextflag = false;		
		return false;
	} else {
		return true;	
	}
}

/*
Function description----------------------------------
Wrapper for function isEmail()
displays message if object field value fails isEmail()
------------------------------------------------------
*/
function CheckEmail(object, message) {
	if (!isEmail(strTrim(object))) {
		alert(message);
		object.focus();
		object.select();
		nextflag = false;		
		return false;
	}
	else
	{
		return true;	
	}
}
/*
Function description----------------------------------
Checks if the (field)object value is equal to the specidifed valueLength
------------------------------------------------------
preconditions - 
	1. object is a field in the form
	2. message is a text string
	3. valueLength is an integer

postconditions - 
	1. True if object value length = valuelength
	2. False if object value length != valuelength

*/
function CheckDigit(object, message, valueLength) {
	if (strTrim(object).length != valueLength) {
		alert(message);
		object.focus();
		object.select();
		nextflag = false;		
		return false;
	}
	else
	{
		return true;	
	}
}

/*
Function description----------------------------------
Checks if the (field)object value is a valid Singapore mobile phone number
------------------------------------------------------
preconditions - 
	1. object is a field in the form containing an integer value
	2. message is a string
	3. object value contains 9 as the first character
	4. object may also contain 'ext.' e.g. format 9xxxxxxx ext.xxx
postconditions - 
	1. True if object value is exactly 8 charactrs, the first character is a '9' 
	2. False if object is not a number, value length is less than 8, first character is not a '9', other characters are present, 'e' and '.'
*/
function CheckSGHP(object, message) {
		if ((strTrim(object).length < 8) || isNaN(strTrim(object)) || (strTrim(object).substring(0, 1) != "9") || (strTrim(object).indexOf("e") > 0) || (strTrim(object).indexOf("E") > 0) || (strTrim(object).indexOf(".") > 0))
		{
			alert(message);
			object.focus();
			object.select();
			nextflag = false;			
			return false;
		}
	else
	{
		return true;	
	}		
}

/*
Function description----------------------------------
Checks if the (field)object value is a valid Singapore 8 digit office number
------------------------------------------------------
preconditions - 
	1. object is a field in the form containing an integer value
	2. message is a string
	3. object value contains 6 as the first character
	4. object may also contain 'ext.' e.g. format 6xxxxxxx ext.xxx
postconditions - 
	1. True if object value is exactly 8 charactrs, the first character is a '6' 
	2. False if object is not a number, value length is less than 8, first character is not a '6', other characters are present, 'e' and '.'
*/

function CheckSGOffice(object, message) {
		if ((strTrim(object).length < 8) || isNaN(strTrim(object)) || (strTrim(object).substring(0, 1) != "6") || (strTrim(object).indexOf("e") > 0) || (strTrim(object).indexOf("E") > 0) || (strTrim(object).indexOf(".") > 0))
		{
			alert(message);
			object.focus();
			object.select();
			nextflag = false;			
			return false;
		}
	else
	{
		return true;	
	}		
}

/*
Function description----------------------------------
Checks if the (field)object value is => minamount, displays message when condition does not hold
------------------------------------------------------
preconditions - 
	1. object is a field in the form containing an integer value
	2. minamount is an integer
	3. message is a string
postconditions - 
	1. True if object value is => minamount
	2. False if object value is < minamount and displays message as an alert
*/
function CheckMin(object, minamount, message) {
		if (parseFloat(strTrim(object)) < parseFloat(minamount))
		{
			alert(message);
			object.focus();
			object.select();
			nextflag = false;			
			return false;
		}
	else
	{
		return true;	
	}		
}


//************************************END OF CHECK[] FUNCTIONS**********************************8


/*
Function description----------------------------------
Checks if the field is an email format type
------------------------------------------------------
preconditions - 
	1. value is a string
postconditions - 
	1. True if value contains @ and a .
	2. False if value does not contain @ and .
*/
function isEmail(value)
{
	var supported = false;

	if (window.RegExp)
	{
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);

		if (tempReg.test(tempStr))
		{
			supported = true;
		}
	}

	if (!supported)
	{
		return (value.indexOf(".") > 2) && (value.indexOf("@") > 0);
	}

	var reg1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var reg2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2})");

	return (!reg1.test(value) && reg2.test(value));
}

/*
Function description----------------------------------
Checks if the (field)object string is empty
------------------------------------------------------
preconditions - 
	1. object is the field in the form
postconditions - 
	1. True if field is empty
	2. False if field is not empty
*/
function strIsBlank(object)
{
	str = strTrim(object);

	return str.length == 0 ? true : false;
}

/*
Function description----------------------------------
Removes spaces, tab, carriage return from user inputs in form fields for the following fields
- textfield
- textarea
- password field
- hidden
- file
- checkbox
- select-one (options)
------------------------------------------------------
preconditions - 
	1. object is the field in the form either one of these field types
		- textfield
		- textarea
		- password field
		- hidden
		- file
		- checkbox
		- select-one (options)	
postconditions - returns a string which does not contain tabs, spaces, carriage returns
*/

function strTrim(object)
{
	var value, i, c, str = "";

	if (object != null)
	{

		if ((object.type == "text") || (object.type == "textarea") || (object.type == "password") || (object.type == "hidden") || (object.type == "file")|| (object.type == "checkbox"))
		{
			value = object.value;
		}
		else if (object.type == "select-one")
		{
			value = object.options[object.selectedIndex].value
		}
		else
		{
			value = object;	
		}
	}
	else
	{
		value = "";
	}

	if (value != "")
	{
		for (i = 0; i < value.length; i++)
		{
			c = value.charAt(i);
			
			if ((c != ' ') && (c != '\n') && (c != '\t'))
			{
				str += c;
			}
			else
			{
				if (i != 0 || i != value.length)
				{
					str += c;					
				}
			}
		}
	}

	return str;
}


function strTrimValue(v)
{
	var value, i, c, str = "";
	value = v;
	if (value != "")
	{
		for (i = 0; i < value.length; i++)
		{
			c = value.charAt(i);
			
			if ((c != ' ') && (c != '\n') && (c != '\t'))
			{
				str += c;
			}
			else
			{
				if (i != 0 || i != value.length)
				{
					str += c;					
				}
			}
		}
	}

	return str;
}

/*
Function description----------------------------------
Checks if the value c is a valid currency value
Pre condition - 
	1. c is integer
Post condition -
	1. True if c is a number, is non negative and does not contain special characters 
------------------------------------------------------
*/
function isValidCurr(c){
	if ((c.value.replace(/\D/g,'') == "") || (parseFloat(c.value) < 0) || isNaN(c.value)) {
		//Invalid;
		return false;
	} else {
		return true;
	}					
}
	

/*
Function description----------------------------------
checks if the singapore nric is valid
------------------------------------------------------
preconditions - 
	1. val is string
	2. length of 9
	3. In the following format SxxxxxxxxY, S is the standard first chracter, x represent integers, Y represents an Alphabet
	
postconditions - 
	1. returns True if val is valid nric format
	2. returns Flase if val is non valid nric format
*/
function nricIsValid(val){
	if (val.length != 9 ){
				return false;
	}
	if (val.charAt(0) != 's' && val.charAt(0) != 'S' && val.charAt(0) != 't' && val.charAt(0) != 'T'){
				return false;
	}
	if (val.charAt(8) >= "0" && val.charAt(8) <= "9"){
				return false;
	}
	for(i=1; i<val.length-1; i++){
			if (val.charAt(i) < "0" || val.charAt(i) > "9"){
				return false;
			}
	}
	if (val.charAt(0) == 's' || val.charAt(0) == 'S')
	var sum =  val.charAt(1)*2+val.charAt(2)*7+val.charAt(3)*6+val.charAt(4)*5+val.charAt(5)*4+val.charAt(6)*3+val.charAt(7)*2;
	else
	var sum =  val.charAt(1)*2+val.charAt(2)*7+val.charAt(3)*6+val.charAt(4)*5+val.charAt(5)*4+val.charAt(6)*3+val.charAt(7)*2+4;
	var p_num = 11-sum%11;
	//alert(p_num);
	switch (p_num) {
		case 1:
			if (val.charAt(8) != 'a' && val.charAt(8) != 'A'){
					return false;
			}
			break
		case 2:
			if (val.charAt(8) != 'b' && val.charAt(8) != 'B'){
					return false;
			}
			break
		case 3:
			if (val.charAt(8) != 'c' && val.charAt(8) != 'C'){
					return false;
			}
			break
		case 4:
			if (val.charAt(8) != 'd' && val.charAt(8) != 'D'){
					return false;
			}
			break
		case 5:
			if (val.charAt(8) != 'e' && val.charAt(8) != 'E'){
					return false;
			}
			break
		case 6:
			if (val.charAt(8) != 'F' && val.charAt(8) != 'f'){
					return false;
			}
			break
		case 7:
			if (val.charAt(8) != 'g' && val.charAt(8) != 'G'){
					return false;
			}
			break
		case 8:
			if (val.charAt(8) != 'h' && val.charAt(8) != 'H'){
					return false;
			}
			break
		case 9:
			if (val.charAt(8) != 'i' && val.charAt(8) != 'I'){
					return false;
			}
			break
		case 10:
			if (val.charAt(8) != 'z' && val.charAt(8) != 'Z'){
					return false;
			}
			break
		case 11:
			if (val.charAt(8) != 'j' && val.charAt(8) != 'J'){
					return false;
			}
	}
	
	
	return true;	
}