function validateFieldData(fieldName,validateType){

	if(!fieldName){
		alert('validateFieldData Error: You must enter a field name to validate');
		return false;
	}
	
	if(!document.getElementById(fieldName)){
		alert('validateFieldData Error: the element: ' + fieldName + ' does not exist in this page.');
		return false;
	}
	
	// Setup Error message space in the DOM
	var msg = '';
	var msgBox = document.createElement('span');
	var msgID = fieldName + '_msg';
	msgBox.setAttribute('id',msgID);	
	var highlightStyle = 'display: inline; float: right; color: RED;'
	var field = document.getElementById(fieldName);
	
	if(validateType != null){
		var pattern;
		var RegEx;
		
		switch(validateType){
			case 'address ':
				pattern = /^([a-zA-Z''-'\s0-9]+)$/;
			break;
			case 'name' :
				pattern = /^[a-zA-Z''-'\s]{1,40}$/;
			break;
			case 'telephone' :
				pattern = /(((\+44)? ?(\(0\))? ?)|(0))( ?[0-9]{3,4}){3}/;
			break;
			case 'email' :
				pattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
			break;
			case 'url' :
				pattern = /^(?:http:\/\/)?(?:[\w-]+\.)+[a-z]{2,6}$/;
			break;
			case 'postcode' :
				pattern = /^[a-zA-Z]{1,2}[0-9]{1,2}([\s])?[0-9]{1,2}[A-Za-z]{2}$/;
			break;
			case 'textonly' :
				pattern = /^[a-zA-Z]$/;
			break;
			case 'textnums' :
				pattern = /^[a-zA-Z0-9]{5}$/;
			break;
			case 'numbersonly' :
				pattern = /^[0-9]+$/;
			break;
			case 'creditcard' :
				pattern = /^((4\d{3})|(5[1-5]\d{2}))[ -]?(\d{4}[ -]?){3}$|^(3[4,7]\d{2})[ -]?\d{6}[ -]?\d{5}$/;
			break;
			case 'date' :
				pattern = /^\d{1,2}[\/]?[\-]?\d{1,2}[\/]?[\-]?\d{4}$/;
			break;
			case 'password' : // minimum 2 different characters & 2 different digits
				pattern = /^(?=.*[a-zA-Z].*[a-zA-Z])(?=.*\d.*\d)[a-zA-Z0-9]{4,20}$/;
			break;
			case 'currency' : // minimum 2 different characters & 2 different digits
				pattern = /^[1-9]\d*(\.\d+)?$/;
			break;
			
		}
	}
	
	
	if(field.value == ''){
		if(document.getElementById(msgID)){
			removeSpan = document.getElementById(msgID);
				removeSpan.parentNode.removeChild(removeSpan);
		}
		msgBox.setAttribute('id',msgID);
		if(msgBox.style.setAttribute){ 
			msgBox.style.setAttribute("cssText", highlightStyle); 
		}else{ 
			msgBox.setAttribute("style", highlightStyle);
		}
		msg = '&lArr; REQUIRED FIELD';
		msgBox.innerHTML = msg;
		field.parentNode.insertBefore(msgBox, field.nextSibling);
		field.style.background = '#FF9999';
		field.style.color = '#666';
		return false;		
	}
	
	if(validateType != null){
		RegEx = new RegExp(pattern);
		fieldValue = new String(field.value);
		var answer = RegEx.test(fieldValue);
		
		if(!answer){
			if(!document.getElementById(msgID)){
				msgBox.setAttribute('id',msgID);
				if(msgBox.style.setAttribute){ 
					msgBox.style.setAttribute("cssText", highlightStyle); 
				}else{ 
					msgBox.setAttribute("style", highlightStyle);
				}
				msg = '&lArr; INVALID DATA';
				msgBox.innerHTML = msg;
				field.parentNode.insertBefore(msgBox, field.nextSibling);
				field.style.background = '#FF9999';
				field.style.color = '#666';
				return false;		
			}else{
				msg = '&lArr; INVALID DATA';
				document.getElementById(msgID).innerHTML = msg;
				field.style.background = '#FF9999';
				field.style.color = '#666';
				return false;		
			}
			return false;
		}
	}
	
	field.style.background = '';
	field.style.color = '';

	if(document.getElementById(msgID)){
		removeSpan = document.getElementById(msgID);
			removeSpan.parentNode.removeChild(removeSpan);
	}	
	return true;
}

// Matches two
function matchFieldDataCheck(fieldone,fieldtwo){
	var objOne;
	var objTwo;
	
	// Setup Error message space in the DOM
	var msg = '';
	var msgBox1 = document.createElement('span');
	var msgBox2 = document.createElement('span');
	var msgID1 = fieldone + '_msg';
	var msgID2 = fieldtwo + '_msg';

	msgBox1.setAttribute('id',msgID1);	
	msgBox2.setAttribute('id',msgID2);	

	var highlightStyle = 'display: block; color: #FF0000;';
	
	if(!document.getElementById(fieldone)){
		alert('matchFieldData Error: the element: ' + fieldone + ' does not exist in this page.');

	}else{
		objOne = document.getElementById(fieldone);
	}
	
	if(!document.getElementById(fieldtwo)){
		alert('matchFieldData Error: the element: ' + fieldtwo + ' does not exist in this page.');	
	}else{
		objTwo = document.getElementById(fieldtwo);
	}
	
	if(objOne.value != objTwo.value){
		msgBox1.setAttribute('id',msgID1);
		msgBox2.setAttribute('id',msgID2);
		if(msgBox1.style.setAttribute){ 
			msgBox1.style.setAttribute("cssText", highlightStyle); 
		}else{ 
			msgBox1.setAttribute("style", highlightStyle);
		}
		if(msgBox2.style.setAttribute){ 
			msgBox2.style.setAttribute("cssText", highlightStyle); 
		}else{ 
			msgBox2.setAttribute("style", highlightStyle);
		}
		msg = "These two fields do not match!";
		msgBox1.innerHTML = msg;
		msgBox2.innerHTML = msg;
		objOne.parentNode.insertBefore(msgBox1, objOne);
		objTwo.parentNode.insertBefore(msgBox2, objTwo);
		objOne.style.background = '#FFFFCC';
		objTwo.style.color = '#FF0000';
		return false;
	}	
	return true;
}

function toggleErrorMsg(fieldName, fieldStatus){
	if(fieldName != ''){
		
		if(document.getElementById(fieldName)){
			var msg = '';
			var msgBox = document.createElement('span');
			var msgID = fieldName + '_msg';
			msgBox.setAttribute('id',msgID);	
			var highlightStyle = 'display: inline; float: right; color: RED;'
			var field = document.getElementById(fieldName);
		
			if(fieldStatus == 'true'){
				if(document.getElementById(msgID)){
					removeSpan = document.getElementById(msgID);
						removeSpan.parentNode.removeChild(removeSpan);
				}
				msgBox.setAttribute('id',msgID);
				if(msgBox.style.setAttribute){ 
					msgBox.style.setAttribute("cssText", highlightStyle); 
				}else{ 
					msgBox.setAttribute("style", highlightStyle);
				}
				msg = '&lArr; REQUIRED FIELD';
				msgBox.innerHTML = msg;
				field.parentNode.insertBefore(msgBox, field.nextSibling);
				field.style.background = '#FF9999';
				field.style.color = '#666';
			}else{
				field.style.background = '';
				field.style.color = '';
			
				if(document.getElementById(msgID)){
					removeSpan = document.getElementById(msgID);
						removeSpan.parentNode.removeChild(removeSpan);
				}	
			}
		}
	}
}


// Fixes forms enc types
function fixFormEnc(){
	if(window.document.forms){
		if(window.document.forms.length > 0){
			for(fidx=0; fidx < window.document.forms.length; fidx++){
				for(eidx=0; eidx < window.document.forms[fidx].elements.length; eidx++){
					if(window.document.forms[fidx].elements[eidx]){
						if(window.document.forms[fidx].elements[eidx].type == 'file'){
							if(window.document.forms[fidx].enctype != 'multipart/form-data'){
								window.document.forms[fidx].enctype = 'multipart/form-data';
								window.document.forms[fidx].encoding = 'multipart/form-data';
							}
						}
					}
				}
			}
		}		
	}
}

function checkContForm(){
	var ansArray = new Array();
	ansArray[0] = validateFieldData('name','name');
	ansArray[1] = validateFieldData('email','email');
	ansArray[2] = validateFieldData('message');
	ansArray[3] = validateFieldData('code','textnums');
           
	for(i=0;i<=ansArray.length;i++){
		if(ansArray[i] == false){
			return false;
		}
 	}
	return true;
 
}
