// DEFINE VARIABLES
// whitespace characters



// Check blank fields
function checkRequiredFields (fieldList){
	var inValidFields = new Array() ;
	var messageText = "" ;
	var itemCount = 0;
	msg = "Please fill all the required fields." ;

	for (i=0; i<fieldList.length; i++)
	{			
		var valueToCheck = getValue(fieldList[i]) ;	
		setFieldColor(fieldList[i], '#FFFFFF') ;

		// Check if field has no value
		if (valueToCheck == null || valueToCheck.length == 0 || valueToCheck == "AllFiltersOff")
		{			
			inValidFields[itemCount] = fieldList[i] ;			
			itemCount = itemCount + 1 ;			
		}			
	}

	if (inValidFields.length > 0)
	{
		/* Colour empty fields' background */
		setFocus(inValidFields[0]) ;
		paintFieldColor(inValidFields, '#EAEFF8') ;
		alert(msg) ;
		return false ;
	}

	return true ;
}

function getValue (element){
	var valueIs = eval('document.forms[0].' + element + '.value') ;
	return valueIs ;
}


// Check email formatting
function checkEmailFormat (fieldList)
{ 	
	var inValidFields = new Array() ;
	var itemCount = 0;
	var msg = "The email address that you provided is not in correct format. Please check it and try again."

	for (i=0; i<fieldList.length; i++)
	{		
		setFieldColor(fieldList[i], '#FFFFFF') ;
		var value = getValue(fieldList[i]);

		if (!isEmail(value))
		{			
			inValidFields[itemCount] = fieldList[i] ;			
			itemCount = itemCount + 1 ;			
		}
	}

	if (inValidFields.length > 0)
	{	
		/* Colour empty fields' background */
		setFocus(inValidFields[0]) ;
		paintFieldColor(inValidFields, '#EAEFF8') ;
		alert(msg);
		return false ;
	}		

	return true ;
}

// Check numeric data
function checkNumericData (fieldList)
{	
	var inValidFields = new Array() ;
	var itemCount = 0;
	var msg = "Please use numbers only. Commas or dollar signs or any non-numeric symbols are not allowed." ;

	for (i=0; i<fieldList.length; i++)
	{		
		setFieldColor(fieldList[i], '#FFFFFF') ;
		var value = getValue(fieldList[i]);

		if (!isNumeric(value))
		{			
			inValidFields[itemCount] = fieldList[i] ;			
			itemCount = itemCount + 1 ;			
		}
	}

	if (inValidFields.length > 0)
	{	
		/* Colour empty fields' background */
		setFocus(inValidFields[0]) ;
		paintFieldColor(inValidFields, '#EAEFF8') ;
		alert(msg);
		return false ;
	}		

	return true ;
}

// Check decimal data
function checkDecimalData (fieldList)
{	
	var inValidFields = new Array() ;
	var itemCount = 0;
	var msg = "Please use numbers and decimal point only. Commas or dollar signs or any non-numeric symbols are not allowed." ;

	for (i=0; i<fieldList.length; i++)
	{		
		setFieldColor(fieldList[i], '#FFFFFF') ;
		var value = getValue(fieldList[i]);

		if (!isDecimal(value))
		{			
			inValidFields[itemCount] = fieldList[i] ;			
			itemCount = itemCount + 1 ;			
		}
	}

	if (inValidFields.length > 0)
	{	
		/* Colour empty fields' background */
		setFocus(inValidFields[0]) ;
		paintFieldColor(inValidFields, '#EAEFF8') ;
		alert(msg);
		return false ;
	}		

	return true ;
}

// Check alphabetic data
function checkAlphabetData (fieldList){
	var inValidFields = new Array() ;
	var itemCount = 0;
	var msg = "Please use alphabet characters only. Special characters or numbers are not allowed." ;

	for (i=0; i<fieldList.length; i++)
	{		
		setFieldColor(fieldList[i], '#FFFFFF') ;
		var value = getValue(fieldList[i]);

		if (includesSpecialChar(value) || includesNumeric(value))
		{			
			inValidFields[itemCount] = fieldList[i] ;			
			itemCount = itemCount + 1 ;			
		}
	}

	if (inValidFields.length > 0)
	{	
		/* Colour empty fields' background */
		setFocus(inValidFields[0]) ;
		paintFieldColor(inValidFields, '#EAEFF8') ;
		alert(msg);
		return false ;
	}		

	return true ;
}


// Check whitespaces
function checkWhitespace (fieldList){
	var inValidFields = new Array() ;
	var itemCount = 0;	
	var whatspace = " " ;
	var msg = "Data with white spaces only are not allowed.";

	for (i=0; i<fieldList.length; i++){		
		var whiteSpaceCount = 0 ;	
		var data = getValue(fieldList[i]);
	
		setFieldColor(fieldList[i], '#FFFFFF') ;

		for (pos=0; pos<data.length; pos++){
			var c = data.charAt(pos);	
			if (whatspace.indexOf(c) == 0) {
				whiteSpaceCount = whiteSpaceCount + 1 ;
			}
		}

		if (data.length > 0)
		{
			if (data.length == whiteSpaceCount)
			{
				inValidFields[itemCount] = fieldList[i] ;		
				itemCount = itemCount + 1 ;			
			}				
		}		
	}

	if (inValidFields.length > 0)
	{	
		/* Colour empty fields' background */
		setFocus(inValidFields[0]) ;
		paintFieldColor(inValidFields, '#EAEFF8') ;
		alert(msg);
		return false ;
	}		

	return true ;
}

// Check the value in dropdown list
function checkValueInList(listName, errMsg){
	var msg = errMsg ;

	obj = eval("document.forms[0]." + listName);
	index = obj.selectedIndex;
	listValue = obj[index].value;

	if (listValue == "" || listValue == " " || listValue == "0" || listValue == "-1"){
		alert(msg);
		return false;
	} else {		
		return true;
	}
}


// Check format of email address
function isEmail (s){       
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}


// Returns true if the string passed in is a valid number
//  (no alpha characters), else it displays an error message
function isNumeric(element)
{	
	var i = 0;
	var charList = "0123456789 " ;

	for (i = 0; i < element.length; i++){
		var c = element.charAt(i);

		if (charList.indexOf(c) == -1) {
			return false ;
			break ;
		}			
	}
	return true;
}

// Returns true if the string passed in is a valid decimal number
function isDecimal(element)
{	
	var i = 0;
	var charList = "0123456789." ;

	for (i = 0; i < element.length; i++){
		var c = element.charAt(i);

		if (charList.indexOf(c) == -1) {
			return false ;
			break ;
		}			
	}
	return true;
}


// Returns true if the string passed in includes special characters.
function includesSpecialChar(element)
{	
	var i = 0 ;
	var charList = "!@#$%^&*()_+-=/\|[]{};:'?.,<>`~\"" ;

	for (i = 0; i < element.length; i++){
		var c = element.charAt(i);

		if (charList.indexOf(c) >= 0) {
			return true ;
			break ;
		}			
	}

	return false ;
}


// Returns true if the string passed in includes numeric characters.
function includesNumeric(element)
{	
	var i = 0 ;
	var charList = "0123456789" ;

	for (i = 0; i < element.length; i++){
		var c = element.charAt(i);

		if (charList.indexOf(c) >= 0) {
			return true ;
			break ;
		}			
	}

	return false ;
}


// Set background colour property of a field
function setFieldColor (element, color){
	eval('document.forms[0].' + element + '.style.background="' + color + '"'); 
}


// Colour background of a field or fields.
function paintFieldColor (element, color){
	for (i=0; i<element.length; i++)	
		{	
			setFieldColor(element[i], color) ;
		}
}


// Set cursor focus on a field.
function setFocus (field){

	Field = eval('document.forms[0].' + field) ;
	Field.focus();
}


