/*=====================================================\
*	Author	: Renante D. Entera						   *
*	Email	: entlouren@yahoo.com                      *
*	Created	: May 01, 2008 (12:30AM GMT+8)             *
\=====================================================*/

function checkField(formName, fieldArray) {
	var strArray = fieldArray.split(",");
	
	for(i=0; i<strArray.length; i++) {
		obj = document.forms[formName].elements[strArray[i]];
		strMsg = strArray[i].toUpperCase();
		msg = strMsg+" is required!";
		msgInvalid = strMsg+" is not valid!";
		
		// >> monitor field and process its validation based from its name
		switch(strArray[i]) {	
			case 'email2':
				obj2 = document.forms[formName].elements['email'];
				strMsg = 'Confirm Email';
				msg = strMsg+" does not match!";
				
				if(!compElement(obj, obj2, msg)) return false;
				
			case 'email':
				if(!isEmpty(obj, msg)) {
					return false;
				}
				else {			
					msg = strMsg+" is not valid!";
					if(!isValidEmail(obj, msg)) return false;
				}
				
				break;				
			
			default:				
				if(!isEmpty(obj, msg)) {
					return false;
				}
			
		}
		// << monitor field and process its validation based from its name
	}
	
	return true;
}

function compElement(obj1, obj2, msg) {
	if (trim(obj1.value) != trim(obj2.value)) {
		alert(msg);
		obj.focus();
		obj.select();
		
		return false;
	}

	return true;	
}

function isNumeric(objField, allowNull, message, invalid) {
	var strValue = objField.value.replace(",","");		

	if(trim(strValue)=='') {
		if(allowNull)
			return true;
		else {
			alert(message)
			objField.select();
			objField.focus();			
			return false;				
		}
	}
	
	if(isNaN(strValue) || strValue.indexOf(".") != -1) {
		alert(invalid)
		objField.select();
		objField.focus();			
		return false;			
	}
	else {
		objField.value = strValue;
		return true;
	}
}

function isEmpty(obj, msg) {
	if (trim(obj.value) == '') {
		alert(msg);
		obj.focus();
		obj.select();
		
		return false;
	}

	return true;
}

function isValidEmail(obj, msg) {
	var email = obj.value;
	var regEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var isValid = regEx.test(email);
	if (!isValid) {
		alert(msg);
		obj.focus();
		obj.select();
		
		return false;
	}

	return true;
}

function trim(inputString) {
	if (typeof inputString != "string") {
		return inputString;
	}
	
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
		
	while (ch == " ") { 
		/* Check for spaces at the beginning of the string*/
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}	
	ch = retValue.substring(retValue.length-1, retValue.length);
	
	while (ch == " ") { 
		/* Check for spaces at the end of the string*/
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	
	while (retValue.indexOf("  ") != -1) { 
		/* Note that there are two spaces in the string - look for multiple spaces within the string*/
		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
	}
	
	return retValue;
}