/********************************************************************
 Program Name  : TradingWeb.js
 Program Type  : Javascript
 Dependency    : JavaScript 1.2
 Description   : Client Side JavaScript For Basic Form Validation.
 Author        : Dennis Mak
 Creation Date : 19 Aug 2003
 Amendment His.: DD MMM YYYY (XXXXX) <<Resp. Person>> <<Ref. No.>>
                 <<Brief Descriptions>>
 Last Revision : 20 Aug 2003

 Function List :
 
	- function ValidateRequired(objfield, fielddesc)
		Description: 
			Validate a required field. Add message to Message Array if any.
		Input:
			objfield (object) = form object
			fielddesc (string) = Error Description will be show in message.
		Return:		bool
		
	- function ValidateLength(objfield, fielddesc, len)
		Description:
			Validate if a field exceed a specified length
		Input:
			objfield (object) = form object.
			fielddesc (string) = Error Description will be show in message.
			len (int) = maximum length objfield.
		Return:		bool
		
	- function ValidateYear(objfield, fielddesc)
		Description:
			Validate if a text field contains valid year value
		Input:
			objfield (object) = form object.
			fielddesc (string) = Error Description will be show in message.
		Return:		bool
			
	- function ValidateRange(objfieldFm, objfieldTo, fielddesc)
		Description: 
			Validate a required field range whether the value of objfieldFm is smaller then that of objfieldTo. 
			Add message to Message Array if any.
		Input:
			objfieldFm (object) = form object
			objfieldTo (object) = form object
			fielddesc (string) = Error Description will be show in message.
		Return:		bool
	
	- function ValidateSyn(objfield1, objfield2, fielddesc1, fielddesc2)

		Description: 
			Validate if two field have same value
		Input:
			objfield1 (object) = form object
			objfield2 (object) = form object
			fielddesc1 (string) = objfield1 customized name shown in message
			fielddesc2 (string) = objfield2 customized name shown in message
		Return: bool
		
	- function ValidateDate(objfield, fielddesc, required)
		Description:
			Validate Date Input. Add message to Message Array if any.
		Input:		objfield (object) = form object
					fielddesc (string) = Error Description will be show in message.
					required (bool) = If the field is required (1 = true, 0 = false)
		Return:		bool 
	
	- function ValidateDateRange2(yrFr, monFr, dayFr, yrTo, monTo, dayTo, objfield, fielddesc)
		Description:
			Validate Date Range if Date To is later than Date From
		Input:		yrFr (int) = year from
					monFr (int) = month from
					dayFr (int) = day from
					yrTo (int) = year To
					monTo (int) = month To
					dayTo (int) = day To
					objfield (object) = form object to be focused
					fielddesc (string) = Error Description will be show in message.
		Return:		bool
		
	- function ValidateNum(objfield, fielddesc, digit, dp, required)
		Description:
			Validate Numeric Input. Add message to Message Array if any.
		Input:		objfield (object)= form object
					fielddesc (string)= Error Description will be show in message.
					digit (int) = Digit
					dp (int) = Decimal Place
					required (bool) = if the field is required (1 = true, 0 = false)						
		Return:		bool 
		
	- function AddDetailMsg(message, objErrorField)
		Description:
			Add Message to Message Array.
		Input:		message (string) = message text
					objErrorField (object) = Form object associate with the Error. For 
					                setting Focus.
		Return:		N/A
		
	- function ShowValidation()
		Description:
			Show Message in Message Array if any.
		Input:		N/A
		Return:		N/A
			
	- function ClearValidation()
		Description:
			Clear Message Array.
		Input:		N/A
		Return:		N/A
	 
	 - function getSelectText()
		Description:
			Get Selected Text of List Item
		Input: List object
		Return: selected Text
	
	- function getSelectValue()
		Description:
			Get Selected Value of List Item
		Input: List object
		Return: selected Value
		
	- function ValidateEmail (objfield, fieldDesc)
		Description:
			Validate Email Format
		Input:		objfield (object) = form object
					fielddesc (string) = Error Description will be show in message.
		Return:		bool
	
	- function ChkExt (objfield, fieldDesc)
		Description:
			Validate if a File Extension is Valid
		Input:		objfield (object) = form object
					fielddesc (string) = Error Description will be show in message.
		Return:		bool
		
 **********************************************************************/
//Global Variables
var isProcessing;		//Is The Form is Being Processed.
var msgArray;			//Array Holding the Message.
var firstErrorObj;		//First Form object in Validation
var dataChanged;		//Is DataChanged Within a Form.
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function JsFormLoad()
{
	isProcessing = false;
	dataChanged = false;
}

function BodyLoad(){
	if (document.forms[0].txhCmd != null){
		document.forms[0].txhCmd.value = "";
	}
}

function getSelectValue(selectObject)
{
	return selectObject.options[selectObject.selectedIndex].value;
}

function getSelectText(selectObject)
{
	return selectObject.options[selectObject.selectedIndex].text;
}

function OptionSelectByValue(selectObj, selectValue)
{
	for (var i=0;i<selectObj.options.length-1; i++)
	{
		if (selectObj.options[i].value == selectValue)
		{
			selectObj.selectedIndex = i;
		}
	}
}

function CheckForm()
{
	if (ClientValidation() == true)
	{
		if(!isProcessing)
		{
			isProcessing = true;	
			window.status = "Processing Request ...";			
			return true;
		}
		else
		{
			alert("Unable to process your request due to another request is being processed.");
			return false;
		}
	}
	else
	{
		return false;
	}	
}

function ValidateRequired(objfield, fielddesc)
{
	if (trim(objfield.value) == "")
	{
		AddDetailMsg(fielddesc + " should not be blank.", objfield);
		return false;
	}
	else
	{
		return true;
	}
}

function ValidateLength(objfield, fielddesc, len)
{
	if (objfield.value.length <= len)
	{
		return true;
	}
	else
	{
		AddDetailMsg(fielddesc + " should not exceeds " + len + " characters", objfield);
		return false;
	}
}

function ValidateRange(objfieldFm, objfieldTo, fielddesc)
{
	if (objfieldFm.value != "" && objfieldTo.value != "")
	{
		if (objfieldFm.value > objfieldTo.value)
		{
			AddDetailMsg(fielddesc + " From should not be greater than " + fielddesc + " To.", objfieldFm);
			return false;
		}
		else
		{
			return true;
		}
	}
}

function ValidateDateRange2(yrFr, monFr, dayFr, yrTo, monTo, dayTo, objfield, fielddesc)
{	
	if(yrFr != "" && yrTo != "")
	{
		if(yrFr > yrTo)
		{
			AddDetailMsg(fielddesc + " To should be later than " + fielddesc + " From.", objfield);
			return false;
		}
		else
		{
			if(monFr != "" && monTo != "" && yrFr == yrTo)
			{
				if(monFr > monTo)
				{
					AddDetailMsg(fielddesc + " To should be later than " + fielddesc + " From.", objfield);
					return false;
				}
				else
				{
					if(dayFr != "" && dayTo != "" && monFr == monTo)
					{
						if(dayFr > dayTo)
						{
							AddDetailMsg(fielddesc + " To should be later than " + fielddesc + " From.", objfield);
							return false;
						}
						else
							return true;
					}
					else
						return true;
				}				
			}
			else
				return true;				
		}		
	}
	else
	{
		return true;		
	}
	
}

function ValidateSyn(objfield1, objfield2, fielddesc1, fielddesc2)
{
	if (objfield1.value == objfield2.value)
	{
		return true;
	}
	else
	{
		AddDetailMsg(fielddesc1 + " should be equal to " + fielddesc2, objfield1);
		return false;
	}
}

function ValidateNum(objfield, fielddesc, digit, dp, required)
{
	var numPart;
	
	if (objfield.value != ""){
	
		if (objfield.value.substring(0,1) == "-")
		{
			numPart = objfield.value.substring(1,objfield.value.length);
		}
		else
		{
			numPart = objfield.value;
		}
	
		if (!IsNumeric(numPart, digit, dp))
		{
			if (dp > 0)
			{
				AddDetailMsg(fielddesc + " should be numeric in "+ digit + " digits and " + dp + " decimal place.", objfield);			
			}
			else
			{
				AddDetailMsg(fielddesc + " should be numeric in "+ digit + " digits.", objfield);			
			}
			
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		if (required)
		{
			AddDetailMsg(fielddesc + " should not be blank.", objfield);
			return false;
		}
		else
		{
			return true;
		}
	}
}

function IsNumeric(numStr, digit, dp)
{
	var pos1;
	var pos2;
	var strInt;
	var strDec;
	
	if (numStr == "")
	{
		return false;
	}
	else
	{
		pos1=numStr.indexOf(".");
		pos2=numStr.indexOf(".",pos1+1);
		if (pos1 == -1){
			strInt = numStr;
			strDec = "";
		}
		else{
			strInt=numStr.substring(0,pos1);
			if (pos1 != -1){
				strDec=numStr.substring(pos1+1);
			}
			else{
				strDec = "";
			}
		}
			
		if (pos2 != -1)
		{
			return false;
		}
		else
		{
			if (!IsIntPart(strInt, digit)) return false;
			if (!IsDecPart(strDec, dp)) return false;
			return true;
		}
	}
}

function IsIntPart(strString, digit)
//  check for valid numeric strings	
{
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;
	if (strString.length > digit) return false;

	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
		{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			blnResult = false;
		}
	}
	return blnResult;
}

//  check for valid numeric strings	
function IsDecPart(strString, dp)
{
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;
	
	if (strString != null && strString != "")
	{	
		if (strString.length > dp) return false;

		//  test strString consists of valid characters listed above
		for (i = 0; i < strString.length && blnResult == true; i++)
			{
			strChar = strString.charAt(i);
			if (strValidChars.indexOf(strChar) == -1)
			{
				blnResult = false;
			}
		}
	}
	else
	{
		blnResult = true;
	}
	return blnResult;
}

function ValidateDate(objfield, fielddesc, required)
{
	if (objfield.value != "")
	{
		if (!isDate(objfield.value))
		{
			AddDetailMsg(fielddesc + " should be in date format(dd/mm/yyyy).", objfield);
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		if (required)
		{
			AddDetailMsg(fielddesc + " should not be blank.", objfield);
			return false;
		}
		else
		{
			return true;
		}	
	}
}

function ClearValidation()
{
	msgArray = new Array();
	firstErrorObj = null;
}

function AddDetailMsg(message, objErrorField)
{
	msgArray[msgArray.length] = message;
	if (msgArray.length ==1) {
		firstErrorObj = objErrorField;
	}
}

function ShowValidation()
{
	var strDetailMsg;
	strDetailMsg = "";
	for (var i=0; i<msgArray.length ; i++)
	{
		strDetailMsg = strDetailMsg + "- " + msgArray[i] + "\n";
	}				
	alert("Unable to process your request due to the following reason:" + "\n\n" + strDetailMsg);
	if (firstErrorObj != null && firstErrorObj.type != "hidden")
	{
		firstErrorObj.focus();
	}
}

function isInteger(s){
	var i;
	for (i = 0; i < s.length; i++){   
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}

function stripCharsInBag(s, bag){
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++){   
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
	} 
	return this;
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear

	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false; 
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false;
	}
	return true;
}

function ValidateYear(objfield, fieldDesc)
{
	if(objfield.value != "")
	{
		if (ValidateNum(objfield, fieldDesc, 4, 0, 0))	
		{		
			strYear = objfield.value;
			year=parseInt(strYear);
			if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
			{
				AddDetailMsg(fieldDesc + " is not a valid year.", objfield);
				return false;
			}
			else
			{
				return true;
			}		
		}
	}
	else
		return true;
}

function updateAction(){
	var result = CheckForm();
	if (result == true){
		document.forms[0].txhCmd.value = "save";
	}
	return result;
}
function deleteAction(){
	if (confirm ("Are you sure to delete the record(s)?")){
		document.forms[0].txhCmd.value = "delete";
		return true;
	}
	else
		return false;
}

function cancelAction(){
	if (confirmAction())
	{
		document.forms[0].txhCmd.value = "cancel";
		return true;
	}
	else
		return false;
}

function confirmAction(){
    return (confirm ("Unsave data will be lost. Continues?"));
}

function printActionPost(){	
	var result = ClientValidation();
	if (result){
		document.forms[0].txhCmd.value = "print";
	}
	return result;	
}


function printAction(pageStr, pageSource, winAttr)
{	
	var id = window.open(pageStr, pageSource, winAttr);
	id.focus();		
	return false;
}


//return Date, input dateString
function ToDate(dateString)
{
	var dates = dateString.split("/");
	//var date = new Date(dates[0], dates[1], dates[2]);	
	var date = new Date(parseInt(dates[2]), parseInt(dates[1]), parseInt(dates[0]));
	return date;
}


function ValidateDateRange(field1, field2, fieldDesc1, fieldDesc2)
{
	if (isDate(field1.value) && isDate(field2.value))
	{
		var date1 = ToDate(field1.value);
		var date2 = ToDate(field2.value);

		if (date1 > date2)	
		{
			AddDetailMsg(fieldDesc2 + " should be later than " + fieldDesc1 + ".", field2);
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return true;
	}
}

function IsDateBehind(date1, date2)
{
	return (date1 > date2);
}

function ValidateEmail (objfield, fieldDesc) {

emailStr = objfield.value;
emailStr = emailStr.toLowerCase();
if (emailStr != "")
{

	/* The following variable tells the rest of the function whether or not
	to verify that the address ends in a two-letter country or well-known
	TLD.  1 means check it, 0 means don't. */

	var checkTLD=1;

	/* The following is the list of known TLDs that an e-mail address must end with. */

	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */

	var emailPat=/^(.+)@(.+)$/;

	/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ] */

	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

	/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed.*/

	var validChars="\[^\\s" + specialChars + "\]";

	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */

	var quotedUser="(\"[^\"]*\")";

	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */

	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

	/* The following string represents an atom (basically a series of non-special characters.) */

	var atom=validChars + '+';

	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */

	var word="(" + atom + "|" + quotedUser + ")";

	// The following pattern describes the structure of the user

	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */

	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

	/* Finally, let's start trying to figure out if the supplied address is valid. */

	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */

	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {

	/* Too many/few @'s or something; basically, this address doesn't
	even fit the general mould of a valid e-mail address. */

	AddDetailMsg(fieldDesc + " format incorrect.", objfield);
	return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];

	// Start by checking that only basic ASCII characters are in the strings (0-127).

	for (i=0; i<user.length; i++) {
	if (user.charCodeAt(i)>127) {
	AddDetailMsg("Ths username of " +  fieldDesc + " contains invalid characters.", objfield);
	return false;
	}
	}
	for (i=0; i<domain.length; i++) {
	if (domain.charCodeAt(i)>127) {
	AddDetailMsg("Ths domain name of " + fieldDesc + " contains invalid characters.", objfield);
	return false;
	}
	}

	// See if "user" is valid 

	if (user.match(userPat)==null) {

	// user is not valid

	AddDetailMsg("The username of " + fieldDesc + " doesn't seem to be valid.", objfield);
	return false;
	}

	/* if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {

	// this is an IP address

	for (var i=1;i<=4;i++) {
	if (IPArray[i]>255) {
	AddDetailMsg("Destination IP address of " + fieldDesc + " is invalid!", objfield);
	return false;
	}
	}
	return true;
	}

	// Domain is symbolic name.  Check if it's valid.
	 
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
	if (domArr[i].search(atomPat)==-1) {
	AddDetailMsg("The domain name of " + fieldDesc + " does not seem to be valid.", objfield);
	return false;
	}
	}

	/* domain name seems valid, but now make sure that it ends in a
	known top-level domain (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */

	if (checkTLD && domArr[domArr.length-1].length!=2 && 
	domArr[domArr.length-1].search(knownDomsPat)==-1) {
	AddDetailMsg(fieldDesc + " must end in a well-known domain or two letter " + "country.", objfield);
	return false;
	}

	// Make sure there's a host name preceding the domain.

	if (len<2) {
	AddDetailMsg(fieldDesc + " is missing a hostname!", objfield);

	return false;
	}

	// If we've gotten this far, everything's valid!
	
	return true;
}
else
{
	return true;
}
}

//return false if the object's character exceed the limit
function CheckMaxLength(obj, maxLength)
{
	if (obj.value.length >= maxLength && event.keyCode != 46 && event.keyCode !=8 && !(event.keyCode >=33 && event.keyCode <=40))
		return false;
	else
		return true;
}

function IsMaxLength(obj, maxLength)
{
	if (obj.value.length > maxLength)
		return true;
	else
		return false;
}

function FormatNumber(pnumber,decimals) 
{  
	if (isNaN(pnumber)) { return 0};  
	if (pnumber=='') { return 0};  
		
	var IsNegative=(parseInt(pnumber)<0); 
	if(IsNegative)pnumber=-pnumber; 

	var snum = new String(pnumber);  
	var sec = snum.split('.');  
	var whole = parseInt(sec[0]);  
	var result = '';  
	if(sec.length > 1){  
		var dec = new String(sec[1]);  
		dec = parseInt(dec)/Math.pow(10,parseInt(dec.length-decimals-1)); 
	Math.round(dec); 
	dec = parseInt(dec)/10; 

	if(IsNegative) 
	{ 
	var x = 0-dec; 
		x = Math.round(x); 
	dec = - x; 
	} 
	else 
	{ 
		dec = Math.round(dec); 
	} 

	/* 
	* If the number was rounded up from 9 to 10, and it was for 1 'decimal' 
	* then we need to add 1 to the 'whole' and set the dec to 0. 
	*/ 
	if(dec==Math.pow(10, parseInt(decimals)))
	{ 
	whole+=1; 
	dec="0"; 
	} 

	dec = String(whole) + "." + String(dec);  
		var dot = dec.indexOf('.');  
		if(dot == -1){  
		dec += '.';  
		dot = dec.indexOf('.');  
	} 
	var l=parseInt(dot)+parseInt(decimals); 
		while(dec.length <= l) { dec += '0'; }  
		result = dec;  
	} else{  
		var dot;  
		var dec = new String(whole);  
		dec += '.';  
		dot = dec.indexOf('.');  
	var l=parseInt(dot)+parseInt(decimals); 
		while(dec.length <= l) { dec += '0'; }  
		result = dec;  
	}  
	if(IsNegative)result="-"+result; 
	return result;  
}  

function addToList(listField, newText, newValue, fieldFr, blank)
{	
	if ( ( newValue == "" ) || ( newText == "" ) ) {
		alert("You cannot add blank values!");
	} else {
		var len = listField.length++; // Increase the size of list and return the size
		listField.options[len].value = newValue;
		listField.options[len].text = newText;
		listField.selectedIndex = len; // Highlight the one just entered (shows the user that it was entered)
	} // Ends the check to see if the value entered on the form is empty
	if (blank)
		fieldFr.value = "";
}

function removeFromList(listField) {
	if ( listField.length == -1) {  // If the list is empty
		alert("There are no values which can be removed!");
	} else {
		var selected = listField.selectedIndex;
		if (selected == -1) {
			alert("You must select an entry to be removed!");
		} else {  // Build arrays with the text and values to remain
			var replaceTextArray = new Array(listField.length-1);
			var replaceValueArray = new Array(listField.length-1);
			for (var i = 0; i < listField.length; i++) {
				// Put everything except the selected one into the array
				if ( i < selected) { replaceTextArray[i] = listField.options[i].text; }
				if ( i > selected ) { replaceTextArray[i-1] = listField.options[i].text; }
				if ( i < selected) { replaceValueArray[i] = listField.options[i].value; }
				if ( i > selected ) { replaceValueArray[i-1] = listField.options[i].value; }
			}
			listField.length = replaceTextArray.length;  // Shorten the input list
			for (i = 0; i < replaceTextArray.length; i++) { // Put the array back into the list
				listField.options[i].value = replaceValueArray[i];
				listField.options[i].text = replaceTextArray[i];
			}
		} // Ends the check to make sure something was selected
	} // Ends the check for there being none in the list
}

function cpyLstTxt2Hidden(listField, hiddenField)
{
	var str = "";
	if (listField.length > 0)
	{
		str += listField.options[0].text;
		for (var i = 1; i < listField.length; i++) {
			str += "^" + listField.options[i].text;
		}
	}
	hiddenField.value = str;
}

function cpyHidden2LstTxt(hiddenField, listField)
{
	var str = hiddenField.value;
	if (str != "")
	{
		var txtArr = str.split('^');
		listField.length = txtArr.length;	
		for (var i=0; i<txtArr.length; i++)
		{			
			listField.options[i].text = txtArr[i];
		}
	}
	else
	{
		if (listField.length > 0)
		{
			listField.length = 0;
		}
	}
}

var ValidExt=/^(jpg|jpeg)$/; // Default Valid Extension

function ChkExt (objfield, fieldDesc)
{
	var	ext = objfield.value;
	ext = ext.toLowerCase();		
	if (ext.search(ValidExt) == -1)
		return false;		
	else
		return true;		
}

function ValidatePhoto (objfield, fieldDesc)
{
	ValidExt=/(jpg|jpeg|bmp|gif|png)$/;
	if (objfield.value != "")
	{		
		if (ChkExt(objfield, fieldDesc))
			return true;
		else
		{
			AddDetailMsg(fieldDesc + " should be in BMP, JPEG, GIF, PNG format!", objfield);
			return false;
		}
	}
	else
		return true;
}

function ValidateCV (objfield, fieldDesc)
{
	ValidExt=/(pdf|doc|tif|htm|html|xls)$/;
	if (objfield.value != "")
	{		
		if (ChkExt(objfield, fieldDesc))
			return true;
		else
		{
			AddDetailMsg(fieldDesc + " should be in PDF, DOC, TIF, HTM, HTML, XLS format!", objfield);
			return false;
		}
	}
	else
		return true;
}

function ValidateCode(codeObj, fieldDesc)
{
	var errorFound = false;
	var codeStr = trim(codeObj.value);
	if(codeObj != null && codeStr != "")
	{
		if (codeStr.indexOf("<") == 0)
		{
			AddDetailMsg(fieldDesc + " should not start with character '<'.", codeObj);
			errorFound = true;
		}
		else if (codeStr.indexOf("^") != -1)
		{
			AddDetailMsg(fieldDesc + " should not contain character '^'", codeObj);	
			errorFound = true;
		}
		else if (codeStr.indexOf(" ") != -1)
		{
			AddDetailMsg(fieldDesc + " should not contain space character ", codeObj);	
			errorFound = true;
		}		
		else if (codeStr.indexOf("+") != -1)
		{
			AddDetailMsg(fieldDesc + " should not contain character '+' ", codeObj);	
			errorFound = true;
		}
		else if (codeStr.indexOf("&") != -1)
		{
			AddDetailMsg(fieldDesc + " should not contain character '&' ", codeObj);	
			errorFound = true;
		}				
		else if (codeStr.indexOf("!") != -1)
		{
			AddDetailMsg(fieldDesc + " should not contain character '!' ", codeObj);	
			errorFound = true;
		}	
		else if (codeStr.indexOf("@") != -1)
		{
			AddDetailMsg(fieldDesc + " should not contain character '@' ", codeObj);	
			errorFound = true;
		}	
		else if (codeStr.indexOf("#") != -1)
		{
			AddDetailMsg(fieldDesc + " should not contain character '#' ", codeObj);	
			errorFound = true;
		}	
		else if (codeStr.indexOf("$") != -1)
		{
			AddDetailMsg(fieldDesc + " should not contain character '$' ", codeObj);	
			errorFound = true;
		}	
		else if (codeStr.indexOf("%") != -1)
		{
			AddDetailMsg(fieldDesc + " should not contain character '%' ", codeObj);	
			errorFound = true;
		}	
		else if (codeStr.indexOf("*") != -1)
		{
			AddDetailMsg(fieldDesc + " should not contain character '*' ", codeObj);	
			errorFound = true;
		}	
		else if (codeStr.indexOf("-") != -1)
		{
			AddDetailMsg(fieldDesc + " should not contain character '-' ", codeObj);	
			errorFound = true;
		}	
		else if (codeStr.indexOf("|") != -1)
		{
			AddDetailMsg(fieldDesc + " should not contain character '|' ", codeObj);	
			errorFound = true;
		}																	
	}
	return !errorFound;
}

function submitForm(url, hasOptList, isPrvlgMap){ 
	
	if (hasOptList){
		var t = document.forms[0].txtTotal.value;
		for (var i=1;i<t;i++){
			if (document.forms[0].item("chk"+i).checked == true){
				document.forms[0].item("chkFlag"+i).value = 'on';
			}else{
				document.forms[0].item("chkFlag"+i).value = 'off';
			}			
		}
	}

	if (isPrvlgMap){
		var p = document.forms[0].txtPrvlgTotal.value;
		var m = document.forms[0].txtModuleTotal.value;
		for (var i=0;i<=p-1;i++){
			for (var j=1;j<=m;j++){
				if (i>0 || j==2){
					if (document.forms[0].item("chkPrvlg"+i+"Module"+j).checked == true){
						document.forms[0].item("chkFlagPrvlg"+i+"Module"+j).value = 'on';
					}else{
						document.forms[0].item("chkFlagPrvlg"+i+"Module"+j).value = 'off';
					}			
				}
			}
		}
	}
	
	document.forms[0].action = url; 
	document.forms[0].submit(); 
	return true; 
}

function ValidateSelectNum()
{	
	var count = 0;
	for (var i=1; i<= 20 ; i++)
	{
		if (document.forms[0].item("chk"+i) != null){
			if (document.forms[0].item("chk"+i).checked)
			{
				count ++;
			}
		}

	}

	if (count == 0)
	{
		AddDetailMsg("At least one option can be selected");
		return false;
	}	
	return true;
}

function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function checkAllModule(myField, prvlgID, moduleNum)	
{
	for (var i = 1; i < moduleNum + 1; i++){
		document.forms[0].item("chkPrvlg"+prvlgID+"Module"+i).checked = myField.checked ;
	}		
}

function addtext(userName) {
	var newtext = document.forms[0].notesAdd.value;
	if (trim(newtext) != "") {
		var history = document.forms[0].notesHistory.value;
		document.forms[0].notesHistory.value = newtext;
		document.forms[0].notesHistory.value += "\nBy ";
		document.forms[0].notesHistory.value += userName;
		document.forms[0].notesHistory.value += "\n";
		document.forms[0].notesHistory.value += history;
		document.forms[0].notesAdd.value = "";
	}
}


JsFormLoad();
