/*
- Author: Qazi Irfan Mustafa
- Organization: East Ocean eImaging (M) Sdn. Bhd., Kuala Lumpur, Malaysia
- Date: 2007/03/15
- Description: Javascript file for general validation of form elements in EOE website
- Last updated by: Qazi Irfan Mustafa
- Last updated on: 2008/07/14
- Copyright Notice: This script has been written solely for the use of eoe Online website and not for general use. Anybody is allowed to use or redistribute this file or any part of it in any form, if it serves any purpose.
*/

/*
EXCEPTIONS
	- 	NN = PURELY A NUMBER. 0 TO 9
	- 	FN = FILE NAME
	-	NB = NO BLANK
	-	NP = NO PUNCTUATION
    -   NPS = NO PUNCTUATION & @
	-	NBP = NO BLANK OR PUNCTUATION
	-	EM = EMAIL
	- 	ES = COMMA OR SEMICOLON SEPERATED EMAIL STRING
	-	PN = PHONE NUMBER
	-	RM = CURRENCY STYLE - 888.88. 
	-	ST = STRICT - ONLY A TO Z, a TO z, 0 TO 9, -, _ AND BLANK SPACE
	-	STNB = STRICT - ONLY A TO Z, a TO z, 0 TO 9, -, AND _ (NOT EVEN BLANK SPACE)
        -       STS = STRICT - ONLY A TO Z, a TO z, 0 TO 9, -, _ AND BLANK SPACE AND ," 
	- 	DS = DONT SEE THIS FIELD
    -   RM = REPLY MESSAGE
*/
	var defaultElementOkMessage = "Ok. Please proceed.";
	var errorMessageL = "";

	function setErrormessageL(mess) {
		if (errorMessageL == "") {
			errorMessageL = mess;
		} else {
			errorMessageL = errorMessageL + "<BR>" + mess;
		}		
	}
	
	function resetErrormessageL() {
		errorMessageL = "";
		if (getprevobjnostyleG("errorMessagesDiv")) {
			getprevobjnostyleG("errorMessagesDiv").innerHTML = "";
		}
	}
		
	function showErrorMessageL() {
		if (errorMessageL != "") {
			if (getprevobjnostyleG("errorMessagesDiv")) {
				getprevobjnostyleG("errorMessagesDiv").innerHTML = "<table width=\"100%\" cellpadding=\"5\" cellspacing=\"0\"><tr><td width=\"20\" valign=\"middle\"><img src=\"/en/images/001/error.gif\" border=\"0\"></td><td valign=\"middle\"><font color=\"#FF0000\">" + errorMessageL + "</font></td></tr></table>";
			} else {
				eoeAlert(errorMessageL);
			} 
		} else {
			if (getprevobjnostyleG("errorMessagesDiv")) {
				getprevobjnostyleG("errorMessagesDiv").innerHTML = "";
			}
		}
	}
	
	function showErrorMessageP(divName) {
		if (errorMessageL != "") {
			if (getprevobjnostyleG(divName)) {
				getprevobjnostyleG(divName).innerHTML = "<table width=\"100%\" cellpadding=\"5\" cellspacing=\"0\"><tr><td width=\"20\" valign=\"middle\"><img src=\"/en/images/001/error.gif\" border=\"0\"></td><td valign=\"middle\"><font color=\"#FF0000\">" + errorMessageL + "</font></td></tr></table>";
			} else {
				eoeAlert(errorMessageL);
			} 
		} else {
			if (getprevobjnostyleG(divName)) {
				getprevobjnostyleG(divName).innerHTML = "";
			}
		}
	}

	function showElementErrorMessageL(formElement) {
		if (getprevobjnostyleG(formElement.name + '_val')) {
			getprevobjnostyleG(formElement.name + '_val').innerHTML = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\"><tr><td valign=\"middle\"><img src=\"/en/images/001/errorSmall.gif\" border=\"0\">&nbsp;<font color=\"#FF0000\">" + errorMessageL + "</font></td></tr></table>";
		} 
	}
	
	function showElementOkMessageL(formElement) {
		if (getprevobjnostyleG(formElement.name + '_val')) {
			getprevobjnostyleG(formElement.name + '_val').innerHTML = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\"><tr><td valign=\"middle\"><img src=\"/en/images/001/iccorrectSmall.gif\" border=\"0\">&nbsp;<font color=\"#008000\">" + defaultElementOkMessage + "</font></td></tr></table>";
		} 
	}
	
	function showOkMessage(divName, message) {
		if (getprevobjnostyleG(divName)) {
			getprevobjnostyleG(divName).innerHTML = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\"><tr><td valign=\"middle\"><img src=\"/en/images/001/iccorrectSmall.gif\" border=\"0\">&nbsp;<font color=\"#008000\">" + message + "</font></td></tr></table>";
		} 
	}
	
	function showErrorMessage(divName, message) {
		if (getprevobjnostyleG(divName)) {
			getprevobjnostyleG(divName).innerHTML = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\"><tr><td valign=\"middle\"><img src=\"/en/images/001/errorSmall.gif\" border=\"0\">&nbsp;<font color=\"#FF0000\">" + message + "</font></td></tr></table>";
		} 
	}
	
	function showWaitingMessage(divName, message) {
		if (getprevobjnostyleG(divName)) {
			getprevobjnostyleG(divName).innerHTML = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\"><tr><td valign=\"middle\"><img src=\"/en/images/001/indicator-small.gif\" border=\"0\">&nbsp;<font color=\"#FF0000\">" + message + "</font></td></tr></table>";
		} 
	}
	
	function eoeSubmitForm(eoeFormName) {
		var ret = false;
		resetErrormessageL();
		try {
			if (!eoeFormName) {
				alert("Form name not recognised");			
			} else if (isValid(eoeFormName)) {				
				ret = true;
			} else {
				showErrorMessageL();
			}
		} catch (err) {
			alert(err);
		} finally {
			return ret;
		}
	}
	
	function eoeSubmitFormP(eoeFormName, messDiv) {
		var ret = false;
		resetErrormessageL();
		try {
			if (!eoeFormName) {
				alert("Form name not recognised");			
			} else if (isValid(eoeFormName)) {				
				ret = true;
			} else {
				showErrorMessageP(messDiv);
			}
		} catch (err) {
			alert(err);
		} finally {
			return ret;
		}
	}
	
	function eoeSubmitForm_PL(eoeFormName, prefixMessage, suffixMessage) {
		var ret = false;
		resetErrormessageL();
		setErrormessageL(prefixMessage);
		try {
			if (!eoeFormName) {
				alert("Form name not recognised");			
			} else if (isValid(eoeFormName)) {				
				ret = true;
			} else {
				setErrormessageL(suffixMessage);
				showErrorMessageL();
			}
		} catch (err) {
			alert(err);
		} finally {
			return ret;
		}
	}
	
	function eoeElementCheck(formElement) {
		var ret = false;
		resetErrormessageL();
		try {
			if (!formElement) {
				alert("Form element name not recognised");			
			} else if (isValidElement(formElement)) {
				showElementOkMessageL(formElement);				
				ret = true;
			} else {
				showElementErrorMessageL(formElement);				
			}
		} catch (err) {
			alert(err);
		} finally {
			return ret;
		}
	}
	
	function eoeElementCheckP(formElement, messDiv) {
		var ret = false;
		resetErrormessageL();
		try {
			if (!formElement) {
				alert("Form element name not recognised");			
			} else if (isValidElement(formElement)) {
				showOkMessage(messDiv, defaultElementOkMessage);				
				ret = true;
			} else {
				showErrorMessage(messDiv, errorMessageL);				
			}
		} catch (err) {
			alert(err);
		} finally {
			return ret;
		}
	}
	
	function isValid(eoeFormName) {
		var returnValue = true;
		for (var i=0; i<eoeFormName.elements.length; i++) {			
			var thisElementType=(eoeFormName.elements[i].type).toUpperCase();
			if (eoeFormName.elements[i].id && eoeFormName.elements[i].id == "DS") {
				// do nothing
			} else if (thisElementType=="TEXT") {
				if (!checkTextPolicy(eoeFormName.elements[i])) {
					returnValue =  false;
				}
			} else if (thisElementType=="PASSWORD") {
				if (!checkPasswordPolicy(eoeFormName.elements[i])) {
					returnValue = false;
				}
			} else if (thisElementType=="TEXTAREA") {
				if (!checkContentPolicy(eoeFormName.elements[i])) {
					returnValue = false;
				}
			} else if (thisElementType=="FILE") {
				if (!checkFilePolicy(eoeFormName.elements[i])) {
					returnValue = false;
				}				
			} 
		}

		return returnValue;
	}
	
	
	function isValidElement(formElement) {
        	var thisElementType=(formElement.type).toUpperCase();
		if (formElement.id && formElement.id == "DS") {
			// do nothing
		} else if (thisElementType=="TEXT") {
			if (!checkTextPolicy(formElement)) {
				return false;
			}
		} else if (thisElementType=="PASSWORD") {
			if (!checkPasswordPolicy(formElement)) {
				return false;
			}
		} else if (thisElementType=="TEXTAREA") {
			if (!checkContentPolicy(formElement)) {
				return false;
			}
		} else if (thisElementType=="FILE") {
			if (!checkFilePolicy(formElement)) {
				return false;
			}				
		} 
		
		return true;
	}
	
	
	// THE FOLLOWING SECTION IS TEMPORARY AND USED SO THAT OTHER DEBUGGING BECOMES EASIER.
	
	function checkFilesValidity(eoeFormName) {
		resetErrormessageL();
		var allOK = true;
		var failIndex = 0;
		//var tempFailed = 0;
		var failMessage = "<b>Files highlighted in red are not acceptable!</b>";
		var fileCount = 0;
		for (var i=0; i<eoeFormName.elements.length; i++) {			
			var thisElementType=(eoeFormName.elements[i].type).toUpperCase();
			if (thisElementType=="FILE") {
				fileCount += 1;
				if (fileValidate(eoeFormName.elements[i])) {
					eoeFormName.elements[i].style.background="#FFFFFF";
				} else {
					eoeFormName.elements[i].style.background="#FF0000";
					failIndex = i;
					failMessage = failMessage + "<BR><div align=left>Only " + eoeFormName.elements[i].accept + " file format allowed for file number " + fileCount + "</div>";
					allOK = false;
				}			
			}
		}
			
		if (allOK) {
			return true;
		} else {		
			setErrormessageL(failMessage);		
			showErrorMessageL();				
			return false;
		}
	}
	
	// THE ABOVE SECTION IS TEMPORARY AND USED SO THAT OTHER DEBUGGING BECOMES EASIER.
	
	
	function checkTextPolicy(formElementName) {

		var formID=formElementName.id;
		var textEntered = formElementName.value;		
		if (formID=="") {
			return true;
		} else {		
			var split1 = new Array();
			split1 = formID.split(",");
			
			var minValue=0;
			minValue=split1[0];
			var maxValue=0;
			maxValue=split1[1];
			var exceptions="N";
			if (split1[2]) {
				exceptions=split1[2].toUpperCase();
			}
			
			//CHECK FOR MINIMUM LENGTH OF THE CONTENT
			if (textEntered.length<minValue){
				setErrormessageL(formElementName.title+" must be alteast "+minValue+" characters!");
				formElementName.focus();
				return false;
			}			
			
			if (minValue>0 && isOnlySpaces(textEntered)) {
				setErrormessageL(formElementName.title+" can not be just blank spaces!");
			}

			//CHECK FOR MAXIMUM LENGTH OF THE CONTENT
			if (textEntered.length > maxValue){				
				setErrormessageL(formElementName.title+" must not exceed "+maxValue+" characters!");
				formElementName.focus();
				return false;
			}
			
			//CHECK IF THE FIELD IS EMAIL FIELD
			//NEED TO CHECK ONLY IF THE LENGTH IS > 0			
			if (textEntered.length>0) {
				if (exceptions=="ST") {
					if (stCheckPassed(formElementName, true)) {
						return true;
					} else {
						setErrormessageL(formElementName.title+" can only contain characters from A-Z, a-Z, 0-9, -, _ and blank spaces");
						return false;
					}
				} else if (exceptions=="STNB") {
					if (stCheckPassed(formElementName, false)) {
						return true;
					} else {
						setErrormessageL(formElementName.title+" can only contain characters from A-Z, a-Z, 0-9, - and _");
						return false;
					}
                                
                } else if (exceptions=="STS") {
					if (stCheckSpecialSymbol(formElementName, false)) {
						return true;
					} else {
						setErrormessageL(formElementName.title+" can only contain characters from A-Z, a-Z, 0-9, - , \", _ , and ( )'+ - _.,:|");
						return false;
					}
				} else if (exceptions=="ES") {
					if (isValidEmailArray(formElementName)) {
						return true;
					} else {									
						return false;
					}
				} else if (exceptions=="EM") {
					if (isValidEmail(textEntered)) {
						return true;
					} else {						
						if (minValue > 0) {
							setErrormessageL(formElementName.title + " does not look like a real email address.");
						} else {
							setErrormessageL(formElementName.title + " does not look like a real email address.<BR>Note: This is not a mandatory field, you may leave it blank");							
						}
						formElementName.select(0);
						formElementName.focus();
						return false;
					}
				} else if (exceptions=="PN") {
					if (isValidPhone(textEntered)) {
						return true;
					} else {		
						if (minValue > 0) {
							setErrormessageL("Only numbers from 0 to 9 and + sign at the begining is allowed for " + formElementName.title);
						} else {
							setErrormessageL("Only numbers from 0 to 9 and + sign at the begining is allowed for " + formElementName.title + "<BR>Note: This is not a mandatory field, you may leave it blank");							
						}			
						formElementName.select(0);
						formElementName.focus();
						return false;
					}
				} else if (exceptions=="NN") {
					if (isPureNumber(textEntered)) {
						return true;
					} else {		
						if (minValue > 0) {
							setErrormessageL("Only numbers from 0 to 9 are allowed for " + formElementName.title);
						} else {
							setErrormessageL("Only numbers from 0 to 9 are allowed for " + formElementName.title + "<BR>Note: This is not a mandatory field, you may leave it blank");							
						}			
						formElementName.select(0);
						formElementName.focus();
						return false;
					}
				} else if (exceptions=="RM") {
					if (isValidAmount(textEntered)) {
						return true;
					} else {		
						if (minValue > 0) {
							setErrormessageL("Only numbers from 0 to 9 and . is allowed for " + formElementName.title + ". Fraction part, if used must be two digits.");
						} else {
							setErrormessageL("Only numbers from 0 to 9 and + sign at the begining is allowed for " + formElementName.title  + ". Fraction part, if used must be two digits.<BR>Note: This is not a mandatory field, you may leave it blank");							
						}			
						formElementName.select(0);
						formElementName.focus();
						return false;
					}
				}
			}		
			
			// CHECK FOR ANY ILLEGAL CHARACTERS IN THE CONTENT			
			var illegalChars="\\~`\"";
			var illegalMessage;
			illegalMessage=formElementName.title+" must not contain any of these characters "+illegalChars;

			if (exceptions=="FN") {
				illegalChars="\\/~`*#\"?&";
				illegalMessage=formElementName.title+" must not contain any of these characters "+illegalChars;								
			} else if (exceptions=="NB") {
				illegalChars="\\/~`\" ";				
				illegalMessage=formElementName.title+" must not contain any of these characters " + illegalChars +" or a blank space";								
			} else if (exceptions=="NBP") {
				illegalChars="\\/~` ,.:;|?<>#*'\"";	
				illegalMessage=formElementName.title+" must not contain any of these characters " + illegalChars +" or a blank space";							
			} else if (exceptions=="NP") {
				illegalChars="\\/~`,.:;|?<>'*#\"";	
				illegalMessage=formElementName.title+" must not contain any of these characters "+illegalChars;							
			}else if (exceptions=="NPS") {
				illegalChars="\\/~`,.:;|?<>'*@#\"";
				illegalMessage=formElementName.title+" must not contain any of these characters "+illegalChars;
			}
			
			if (hasIllegalChar(textEntered, illegalChars)) {
				setErrormessageL(illegalMessage);
				formElementName.focus();
				return false;
			}

		}
		
		return true;
	}
	
	
		
	function stCheckPassed(formElementName, blankAllowed) {
		var inValue = formElementName.value;
		var allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
		if (blankAllowed) {
			allowedChars = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
		}
		
		var testPassed = true;
		
		if (inValue) {
			if (inValue.length > 0) {
				for (var i=0; i<inValue.length; i++) {
					if (allowedChars.indexOf(inValue.charAt(i)) == -1) {
						testPassed = false;
						break;
					}
				}
			}		
		}		
		return testPassed;		
	}
	
       function stCheckSpecialSymbol(formElementName) {
		var inValue = formElementName.value;
		var allowedChars = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_\"( )'+ - _.,:|";

		var testPassed = true;
		
		if (inValue) {
			if (inValue.length > 0) {
				for (var i=0; i<inValue.length; i++) {
					if (allowedChars.indexOf(inValue.charAt(i)) == -1) {
						testPassed = false;
						break;
					}
				}
			}		
		}		
		return testPassed;		
	}  
	
	function checkPasswordPolicy(formElementName) {

			var minValue=6;
			
			//CHECK FOR MINIMUM LENGTH OF THE CONTENT
			if (formElementName.value.length<minValue){
				setErrormessageL(formElementName.title + " must be alteast " + minValue + " characters!");
				formElementName.select(0);
				return false;
			}
			
			var maxValue=15;
			
			//CHECK FOR MAXIMUM LENGTH OF THE CONTENT
			if (formElementName.value.length > maxValue){
				setErrormessageL(formElementName.title + " must not exceed " + maxValue + " characters!");
				formElementName.select(0);
				return false;
			}	

			// CHECK FOR ANY ILLEGAL CHARACTERS IN THE CONTENT
			var illegalChars="\\/~` ,.:;|?<>'&";
			if (hasIllegalChar(formElementName.value, illegalChars)) {
				setErrormessageL(formElementName.title + " must not contain any of these characters " + illegalChars + " or a blank space");
				formElementName.select(0);
				return false;
			}
		
		return true;
	}
	
	
	
	function checkContentPolicy(formElementName) {

		var formID=formElementName.id;
		
		// setErrormessageL("NOW CHECKING - "+formElementName.title);
		//IF THERE IS NO ID, LET THERE BE NO VALIDATION, RETURN TRUE
		if (formID=="") {
			return true;
		} else {		
			var split1 = new Array();
			split1 = formID.split(",");
			
			var minValue=0;
			minValue=split1[0];
			var maxValue=0;
			maxValue=split1[1];
			
			var exceptions="N";
			if (split1[2]) {
				exceptions=split1[2].toUpperCase();
			}
			
			var illegalMessage;
						
			//CHECK FOR MINIMUM LENGTH OF THE CONTENT
			if (formElementName.value.length<minValue){
				setErrormessageL(formElementName.title+" must be alteast "+minValue+" characters!");
				formElementName.focus();
				return false;
			}			
			
			if (isOnlySpaces(formElementName.value)) {
				illegalMessage=formElementName.title+" can not be just blank spaces!";				
				if (minValue == 0) {
					illegalMessage=illegalMessage+"<BR>Note: This is not a mandatory field, you may leave it blank";
				}				
				setErrormessageL(illegalMessage);
				formElementName.focus();
				return false;
			}
			
			//CHECK FOR MAXIMUM LENGTH OF THE CONTENT
			if (formElementName.value.length > maxValue){				
				setErrormessageL(formElementName.title + " must not exceed " + maxValue + " characters!");
				formElementName.focus();
				return false;
			}
			
			//CHECK IF THE FIELD IS EMAIL FIELD
			//NEED TO CHECK ONLY IF THE LENGTH IS > 0			
			if (formElementName.value.length>0) {
				if (exceptions=="ES") {
					if (isValidEmailArray(formElementName)) {
						return true;
					} else {									
						return false;
					}
				} else if (exceptions=="EM") {
					if (isValidEmail(formElementName.value)) {
						return true;
					} else {
						illegalMessage=formElementName.title + " does not look like a real email address!";						
						if (minValue == 0) {
							illegalMessage=illegalMessage+"<BR>Note: This is not a mandatory field, you may leave it blank"
						}
						formElementName.select(0);
						formElementName.focus();
						setErrormessageL(illegalMessage);
						return false;
					}
				} else if (exceptions=="PN") {
					if (isValidPhone(formElementName.value)) {
						return true;
					} else {					
						illegalMessage="Only numbers from 0 to 9 and + sign at the begining is allowed for " + formElementName.title;
						
						if (minValue == 0) {
							illegalMessage=illegalMessage+"<BR>Note: This is not a mandatory field, you may leave it blank"
						}
						formElementName.select(0);
						formElementName.focus();
						setErrormessageL(illegalMessage);
						return false;
					}
				} else if (exceptions=="NN") {
					if (isPureNumber(textEntered)) {
						return true;
					} else {		
						if (minValue > 0) {
							setErrormessageL("Only numbers from 0 to 9 are allowed for " + formElementName.title);
						} else {
							setErrormessageL("Only numbers from 0 to 9 are allowed for " + formElementName.title + "<BR>Note: This is not a mandatory field, you may leave it blank");							
						}			
						formElementName.select(0);
						formElementName.focus();
						return false;
					}
				} 
			}		
			
			// CHECK FOR ANY ILLEGAL CHARACTERS IN THE CONTENT			
			var illegalChars="\\/~`";
			illegalMessage=formElementName.title+" must not contain any of these characters " + illegalChars;

			if (exceptions=="NB") {
				illegalChars="\\/~` ";
				illegalMessage=formElementName.title + " must not contain any of these characters " + illegalChars + " or a blank space";								
			} else if (exceptions=="NBP") {
				illegalChars="\\/~` ,.:;|?<>'\"";	
				illegalMessage=formElementName.title+" must not contain any of these characters " + illegalChars + " or a blank space";							
			} else if (exceptions=="NP") {
				illegalChars="\\/~`,.:;|?<>'\"";	
				illegalMessage=formElementName.title+" must not contain any of these characters " + illegalChars;							
			} else if(exceptions=="NPS") {
                illegalChars="\\/~`,.:;|?<>'@\"";
                illegalMessage=formElementName.title+" must not contain any of these characters " + illegalChars;
            }
			
			if ((hasIllegalChar(formElementName.value, illegalChars)) && (!(exceptions=="RM"))) {
				setErrormessageL(illegalMessage);
				formElementName.focus();
				return false;
			}

		}
		
		return true;
	}

	
	function checkFilePolicy(formElementName) {		
		var isRequired="N";
		if (formElementName.required) {
			isRequired = ((formElementName.required).substring(0,1)).toUpperCase();
		}
		
		var fileName = formElementName.value;
		if (fileName == "") {
			if (isRequired == "Y") {				
				setErrormessageL(formElementName.title+" is a mandatory field. Please select a file and proceed!");
				return false;				
			} else {				
				return true;				
			}
		}

		if (!fileValidate(formElementName)) {
			var alertMessage="Please choose another file for "+formElementName.title+". Only " + formElementName.accept + " file formats are allowed";
			if (isRequired != "Y") {
				alertMessage=alertMessage+"<BR>Note: This is not a mandatory field. You may leave it blank.";
			}			
			setErrormessageL(alertMessage);			
			return false;
		} else {
			return true;
		}
		
		/*var strAllowed=formElementName.accept;
				
		if (strAllowed == "") {
			return true;
		}
		
		var fileExtension=fileName.substring(fileName.lastIndexOf("."));

		if (strAllowed.indexOf(fileExtension) == -1) {
			var alertMessage="Please choose another file for "+formElementName.title+". Only the following file types are allowed:<BR>"+strAllowed;
			if (isRequired != "Y") {
				alertMessage=alertMessage+"<BR>Note: This is not a mandatory field. You may leave it blank.";
			}			
			setErrormessageL(alertMessage);
			return false;
		} else {
			return true;
		}*/
		
	}
	//CHECK TO SPOT OUT ANY ILLEGAL CHARACTERS IN THE STRING.
	//FIRST PARAMETER IS THE STRING TO CHECK
	//SECOND PARAMETER IS THE STRING OF ILLEGAL CHARACTERS. 
	//THE STRING OF ILLEGAL CHARACTERS IS PASSED TO THE FUNCTION AS DIFFERENT FIELDS 
	//WILL HAVE DIFFERENT POLICIES
	function hasIllegalChar(strToCheck, strIllegalChars) {

		for(i=0; i < strIllegalChars.length; i++){
			if(strToCheck.indexOf(strIllegalChars.charAt(i),0) > -1){	
				return true;
			}
		}
		
		return false;
	}
	
	
	//THIS FUNCTION IS USED TO VALIDATE A COMMA OR SEMICOLON SEPERATE EMAIL STRING	
	function isValidEmailArray(formElementName) {
		var inString=formElementName.value;
				
		if (inString == "") {				
			var splitID = new Array();
			splitID = (formElementName.id).split(",");
			
			if (splitID[0]) {	
				var minLength=0;
				minLength=splitID[0];	
				if (minLength > 0) {			
					setErrormessageL(formElementName.title+" is a mandatory field.");
					return false;				
				} else {				
					return true;				
				}
			}
		}
				
		while (inString.indexOf(",") > -1) {
			inString=inString.replace(",", ";");
		}
		
		while (inString.indexOf(" ") > -1) {
			inString=inString.replace(" ", "");
		}
		
		var splitEmails = new Array();
		splitEmails = inString.split(";");
		
		var allPassed = true;
		for (var i=0; i < splitEmails.length; i++) {
			var thisEmailAd=splitEmails[i];
		
			if (!isValidEmail(thisEmailAd)) {				
				setErrormessageL(thisEmailAd+" does not look like a valid email address!");				
				allPassed = false;				
			} 			
		}
		
		if (!allPassed) {
			formElementName.select(0);
			formElementName.focus();
			return false;
		}
		
		return true;	
	}
	
	//THIS FUNCTION IS USED TO VALIDATE A SINGLE EMAIL ADDRESS	
	function isValidEmail(inString) {
		if (hasIllegalChar(inString, "`~!#$%^&*()={}[]:;'<>,?/\\|&\"")) {
			return false;
		}		
		
		if(inString.indexOf("@") == -1){	
			return false;
		}
		
		if(inString.indexOf(".") == -1){	
			return false;
		}
		
		var splitString = new Array();
		splitString = inString.split("@");
		
		if (splitString[2]) {
			return false;
		}
		
		if(splitString[1].indexOf(".") == -1){	
			return false;
		}
		
		if(splitString[0].length<2) {
			return false;
		}
				
		var ext=inString.substring(inString.lastIndexOf("."))		
		if(ext.length < 3) {
			return false;
		}
		
		if(ext.length > 5) {
			return false;
		}
		
		return true;		
	}
	
	function isPureNumber(inString) {

		var rawString=inString;
		var validString="0123456789";
		
		for (var i=0; i<rawString.length; i++) {
			if (validString.indexOf(rawString.charAt(i)) == -1) {
				return false;
			}
		}
		
		return true;
	}
	
	function isValidPhone(inString) {

		var rawString=inString;

		if(inString.charAt(0) == "+") {
			rawString=rawString.substring(1);			
		}
		
		var validString="0123456789";
		
		for (var i=0; i<rawString.length; i++) {
			if (validString.indexOf(rawString.charAt(i)) == -1) {
				return false;
			}
		}
		
		return true;
	}
	
	function isValidAmount(inString) {

		var validString="0123456789.";
		var ret = true;
		
		for (var i=0; i<inString.length; i++) {
			if (validString.indexOf(inString.charAt(i)) == -1) {
				ret = false;
				break
			}
		}
		
		if (ret) {
			if (inString.indexOf(".") > -1 && inString.indexOf(".") != inString.length - 3) {
				ret = false;
			}
		}
		
		return ret;
	}
	
	function restrict(formElement) {
		var splitString = new Array();
		splitString = formElement.id.split(",");
		
		if (splitString[1]) {
			var cap=0;
			cap=splitString[1];
			
			if (formElement.value.length > cap) {
				formElement.value = formElement.value.substring(0, cap);
				setErrormessageL(formElement.title+" must not exceed "+cap+" characters");
			}
			
		}
	}
	
	function isOnlySpaces(inValue) {
		if (inValue == null || inValue== "") {
			return false;
		} else {
			while (inValue.indexOf("  ") > -1) {
				inValue = inValue.replace("  ", " ");
			}
			if (inValue == " ") {
				return true;
			}
		}
	}

	
	function checkFileValidity(formElement, errorMessDiv) {
		if (fileValidate(formElement)) {
			formElement.style.background="#FFFFFF";
			if (getprevobjnostyleG(errorMessDiv)) {
				getprevobjnostyleG(errorMessDiv).innerHTML = "";
			}
		} else {			
			if (formElement && formElement.accept && getprevobjnostyleG(errorMessDiv)) {
				getprevobjnostyleG(errorMessDiv).innerHTML = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\"><tr><td width=\"20\" valign=\"middle\"><img src=\"/en/images/001/error.gif\" border=\"0\"></td><td valign=\"middle\"><font color=\"#FF0000\">Only " + formElement.accept + " file format allowed</font></td></tr></table>";
			} else if (formElement && formElement.accept) {
				setErrormessageL("Only " + formElement.accept + " file format allowed");
				showErrorMessageL();
			}
			formElement.style.background="#FF0000";
		}
	}
	
	function checkFileValidity(formElement) {
		if (fileValidate(formElement)) {
			formElement.style.background="#FFFFFF";
			if (getprevobjnostyleG("errorMessagesDiv")) {
				getprevobjnostyleG("errorMessagesDiv").innerHTML = "";
			}
		} else {			
			if (formElement && formElement.accept && getprevobjnostyleG("errorMessagesDiv")) {
				getprevobjnostyleG("errorMessagesDiv").innerHTML = "<table width=\"100%\" cellpadding=\"5\" cellspacing=\"0\"><tr><td width=\"20\" valign=\"middle\"><img src=\"/en/images/001/error.gif\" border=\"0\"></td><td valign=\"middle\"><font color=\"#FF0000\">Only " + formElement.accept + " file format allowed</font></td></tr></table>";
			} else if (formElement && formElement.accept) {
				setErrormessageL("Only " + formElement.accept + " file format allowed");
				showErrorMessageL();
			}
			formElement.style.background="#FF0000";
		}
	}
	
	function fileValidate(formElement) {
		if (formElement && formElement.accept && formElement.value && formElement.value!="") {
			var ext = "";
			if (formElement.value.indexOf(".") > -1) {
				ext = (formElement.value.substring(formElement.value.lastIndexOf("."))).toLowerCase();
			}
			
			if (formElement.accept.indexOf(",") == -1) {
				var acceptExt = removeAllSpaces(formElement.accept);
				if (acceptExt.indexOf("/") > -1 && acceptExt.indexOf("/") < acceptExt.length - 2) {
					acceptExt = acceptExt.substring(acceptExt.indexOf("/") + 1);
				}
				if ("." + acceptExt == ext){
					return true;
				} else if ("." + fileExtensionTranslate(acceptExt) == ext){
					return true;
				} else {
					return false;
				}
			} else {
				var splitAccept = new Array();
				splitAccept = formElement.accept.split(",");
				var ret = false;
				for (var i=0; i<splitAccept.length; i++) {
					var acceptExt = removeAllSpaces(splitAccept [i]);
					if (acceptExt.indexOf("/") > -1 && acceptExt.indexOf("/") < acceptExt.length - 2) {
						acceptExt = acceptExt.substring(acceptExt.indexOf("/") + 1);
					}
					if ("." + acceptExt == ext){
						ret = true;
						break;						
					} else if ("." + fileExtensionTranslate(acceptExt) == ext){						
						ret = true;
						break;
					} 
				}
				if (ret) {
					return true;
				} else {
					return false;
				}
			}		
		} else {
			var isRequired="N";
			if (formElement.required && formElement.required != "") {
				isRequired = ((formElement.required).substring(0,1)).toUpperCase();
			}
			if (isRequired == "Y") {
				return false;
			} else {
				return true;
			}
		}		
	}
	
	function removeAllSpaces(inString) {
		if (inString == null) {
			return "";
		} else {
			while (inString.indexOf(" ") > -1) {
				inString = inString.replace(" ", "");
			}
			return inString;
		}
	}
	
	function fileExtensionTranslate(ext) {
		if (ext == "jpeg") {
			return "jpg";
		} else if (ext == "x-png") {
			return "png";
		}
	}
	
	
/* The following function checks if the date string is a valid date string 
	It takes two parameters:
		1. dateAsString;
		2. dateFormat;
	Processes only short date format like DD/MM/YYYY, DD/MM/YY, DD-MM-YYYY AND DD,MM,YYYY etc.
	Return type boolean: TRUE if dateAsString is a valid data againt dateFormat
						FALSE if dateAsString is not a valid data againt dateFormat */
function checkDateValidity(dateAsString, dateFormat) {
	var allowedChars = "0123456789" + dateFormat;
	var testPassed = true;
	
	// CHECK IF THERE IS ANY UNWANTED CHARACTER
	if (dateAsString && dateAsString.length > 4) {
		for (var i=0; i<dateAsString.length; i++) {
			if (allowedChars.indexOf(dateAsString.charAt(i)) == -1) {
				testPassed = false;
				break;
			}
		}	
	} else {
		testPassed = false;
	}
	
	// FIND THE DATE SEPERATOR
	var sep;
	allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
	for (var i=0; i<dateFormat.length; i++) {
		if (allowedChars.indexOf(dateFormat.charAt(i)) == -1) {
			sep = dateFormat.charAt(i);
			break;
		}
	}
	
	var splitDate = dateAsString.split(sep);
	var splitFormat = dateFormat.split(sep);
	
	
	
	var yC;
	var mC;
	var dC;

	for (var i=0; i<splitFormat.length; i++) {
		if (splitFormat[i].toLowerCase() == "dd" || splitFormat[i].toLowerCase() == "d") {
			dC = parseInt(removeLeading(splitDate[i]));
		} else if (splitFormat[i].toLowerCase() == "mm" || splitFormat[i].toLowerCase() == "m") {
			mC = parseInt(removeLeading(splitDate[i])) - 1;
		} else if (splitFormat[i].toLowerCase() == "yyyy" || splitFormat[i].toLowerCase() == "yy") {
			yC = parseInt(removeLeading(splitDate[i]));
		}
	}

	var dt = new Date(yC,mC,dC);
	//dt.setYear(yC);
	//dt.setMonth(mC);
	//dt.setDate(dC);

	if (dt.getFullYear() == yC && dt.getMonth() == mC && dt.getDate() == dC) {
		testPassed = true;
	} else {
		testPassed = false;
	}
	
	return testPassed;	
}


function limitContent(formElement, divID) {	
	
	var formID=formElement.id;
	var textEntered = formElement.value;		
	if (formID=="") {
		return true;
	} else {		
		var split1 = new Array();
		split1 = formID.split(",");
			
		var maxValue=0;
		maxValue=parseInt(split1[1]);			
	
		//CHECK FOR MAXIMUM LENGTH OF THE CONTENT						
		if (getprevobjnostyleG(divID)) {
			var remainingChars = maxValue - textEntered.length;
			
			if (maxValue>0 && textEntered.length > maxValue){
				getprevobjnostyleG(divID).innerHTML = "Characters remaining : 0";
				formElement.value = textEntered.substring(0, maxValue);
			} else {
				getprevobjnostyleG(divID).innerHTML = "Characters remaining : " + remainingChars;
			}
		}
	}	
}

function limitContentND(formElement) {	
	limitContent(formElement, formElement.name + '_val');		
}

function removeLeading(inString) {	
	if (inString.length > 1) {
		while (inString.charAt(0) == "0" || inString.charAt(0) == " ") {
			if (inString.length == 1) {
				break;
			} else {
				inString = inString.substring(1);
			}
		}
	}
	return inString;
}
