function validateForm(theForm, arrayFields){
	/*
		arrayFields[fieldname, validationType, ErrorMessage]
		validation types:
		
		text
		phone
		email
		numeric
		date
		
	
	*/
	var bValid = true;
	var sTemp = "The following errors were found:\n\n";
	
	var fieldName = 0;
	var validationType = 1;
	var ErrorMessage = 2;
	
	for(i = 0; i < arrayFields.length; i++){
		switch(new String(arrayFields[i][validationType]).toLowerCase()){
			case "url":
				if(!isURL(theForm[arrayFields[i][fieldName]].value)){
					bValid = false;
					sTemp += arrayFields[i][ErrorMessage] + "\n";
				}
				break;
			case "text":
				if(isEmpty(theForm[arrayFields[i][fieldName]].value)){
					bValid = false;
					sTemp += arrayFields[i][ErrorMessage] + "\n";
				}
				break;
			case "phone":
				var working = new String(theForm[arrayFields[i][fieldName]].value).replace(/[\D]/g, "");
				if(isNumeric(working) != 10){
					bValid = false;
					sTemp += arrayFields[i][ErrorMessage] + "\n";
				}
				break;
			case "email":
				if(!isValidEmail(theForm[arrayFields[i][fieldName]].value)){
					bValid = false;
					sTemp += arrayFields[i][ErrorMessage] + "\n";
				}
				break;
			case "date":
				if(!isDate(theForm[arrayFields[i][fieldName]].value)){
					bValid = false;
					sTemp += arrayFields[i][ErrorMessage] + "\n";
				}
				break;
			case "numeric":
				if(isNaN(theForm[arrayFields[i][fieldName]].value)){
					bValid = false;
					sTemp += arrayFields[i][ErrorMessage] + "\n";
				}
				break;
			
			case "webimage":
				if(!isWebImage(theForm[arrayFields[i][fieldName]].value)){
					bValid = false;
					sTemp += arrayFields[i][ErrorMessage] + "\n";
				}				
				
				break;
			
			case "webimage_null":
				if(theForm[arrayFields[i][fieldName]].value)
				{
					if(!isWebImage(theForm[arrayFields[i][fieldName]].value)){
						bValid = false;
						sTemp += arrayFields[i][ErrorMessage] + "\n";
					}
				}
				
				break;
				
			case "match":
				var match_fields = arrayFields[i][fieldName].split('|');
				var eval_array = [];
				var all_matched = false;
				
				for(j = 0; j < match_fields.length; j++)
					eval_array.push('theForm["' + match_fields[j] + '"].value');
					
				var eval_string = 'all_matched = (' + eval_array.join(' == ') + ');';
				eval(eval_string);
				
				if(!all_matched)
				{
					bValid = false;
					sTemp += arrayFields[i][ErrorMessage] + "\n";
					for(j = 0; j < match_fields.length; j++)
						theForm[match_fields[j]].value = '';
				}
					
				break;
				
			
			default:
				//do nothing;
				break;
		}
	}
	
	if(!bValid){
		alert(sTemp);
	}
	
	return bValid;
}


function isValidEmail (strng) {
	var error = true;
	if (strng == "") {
	   error = false;
	}
	
	var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(strng))) { 
	   error = false;
	}
	else {
	   var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
		 if (strng.match(illegalChars)) {
		  error = false;
	   }
	}
	return error;    
}

function isNumeric(strng) {
	//returns the length of the numeric string if or 0 for non-numeric.
	var error = 0;
	if (strng == "") {
	   error = 0;
	}
	else {
		if (isNaN(strng)) {
			error = 0;
		}
		else {
			error = strng.length
		}
	}
	return error;
}

// non-empty textbox

function isEmpty(strng)
{
	var str = new String(strng).replace(/\s/g, "");
	var error = false;
	if (str.length == 0) {
    	error = true;
	}
	return error;	  
}

function isDate(string)
{
	/*
	var working = new String(string).replace(/[^0-9\/\-\.]/g, "").replace(/[\.\-]/g, "/");
	var year = working.substr(working.lastIndexOf("/") + 1, 4);	
	var month = working.substr(0, working.indexOf("/"));
	var day = working.substr(working.indexOf("/") + 1, working.lastIndexOf("/") - working.indexOf("/") - 1);
	
	if(year.length != 4) return false;
	if(day.length < 1 || day.length > 2) return false;
	if(month.length < 1 || month.length > 2) return false;
	
	year = new Number(year);
	month = new Number(month);
	day = new Number(day);
	
	if(day < 1 || day > 31) return false;
	if(month < 1 || month > 12) return false;
	if(year < 1) return false;
	
	if(month == 1 && day > 31) return false;	
	if((month == 2) && (((day > 28) && (year % 4)) || ((day > 29) && !(year % 4)))) return false;
	if(month == 3 && day > 31) return false;
	if(month == 4 && day > 30) return false;
	if(month == 5 && day > 31) return false;
	if(month == 6 && day > 30) return false;
	if(month == 7 && day > 31) return false;
	if(month == 8 && day > 31) return false;
	if(month == 9 && day > 30) return false;
	if(month == 10 && day > 31) return false;
	if(month == 11 && day > 30) return false;
	if(month == 12 && day > 31) return false;

	return true;
	*/
	return ! isEmpty(string);
}

function isURL(strIn)
{
	bValid = true;
	if(!isEmpty(strIn)){
		
		var protocol = new String(strIn).toLowerCase();
		protocol = protocol.substr(0, protocol.indexOf("://"));
		if((!protocol) || (protocol == "javascript")) bValid = false;
		
		var host = strIn.replace(protocol + "://", '');		
		if(!(host.match(/\w{1,}\.\w{2,}/gi))) return false;
		
	} else {
		bValid = false;
	}
	return bValid;
}

function isWebImage(strIn)
{
	if(!strIn) return false;
	var extensions = /(\.jpg\b)|(\.gif\b)|(\.jpeg\b)|(.swf\b)/gi;
	var found = strIn.match(extensions);
	if(found)
		return true;
	else
		return false;
}