//enabled submit on if tickbox is ticked
function flipSubmit(oObject) {

	if(navigator.userAgent.indexOf("MSIE")!=-1){
		//IE
		if (oObject.checked == true) {
			//enable submit button			
			document.all.submit.disabled = false; 
		} else { 
			//disabled submit button			
			document.all.submit.disabled = true; //IE
		}		
	} else { 
		//Firefox, and others
		if (oObject.checked == true) {
			//enable submit button			
			document.getElementById('submit').disabled = false;
		} else {
			//disabled submit button
			document.getElementById('submit').disabled = true; //Firefox
		}		
	}
	
}

function validateForm(oForm) {
	sValidationString = oForm.elements['validation'].value
	//sValidationString = document.getElementById('validation').value;
	//alert(sValidationString);
	
	//question submission
	//bConfirm = confirm('Are you sure you wish to submit this form?')
	bConfirm = true;
	if (bConfirm) {
		//loop through all form fields
		sFieldName = '';
		sReturn = '';
		iTotal = 0;
		bTest = false;
		for (var i=0;i < oForm.length;i++) {
			//check if field is require for validation
			
			
			//for (var iSub=0;iSub < oForm.getElementsByTagName('label').length;iSub++) {			
				
				//if (oForm.elements[i].type == 'label' && oForm.elements[i].name == oForm.elements[iSub].getAttribute('for')) {
				//if (oForm.elements[i].type == 'label') {
					//sReturn += '-' + oForm.elements[iSub].getAttribute('for')
				//}
			//}
				
			sFieldName = oForm.elements[i].name
			sFieldTitle = oForm.elements[i].title
			sFieldValue = oForm.elements[i].value
			if (sFieldTitle == '') {
				sFieldTitle = sFieldName
			}	
			bTest = false;
	//		alert(sFieldName)
			if (sValidationString.indexOf(sFieldName + '[isEmail]') >= 1 && sFieldName != '') {
				if (sFieldValue == '') {
					sReturn += '- ' + sFieldTitle + ' is required\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else if (sFieldValue.indexOf('@') <= 0) {
					sReturn += '- ' + sFieldTitle + ' must be an e-mail address\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else {
	//				sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
					oForm.elements[i].style.backgroundColor = '';
				}
			} else if (sValidationString.indexOf(sFieldName + '[isNum]') >= 1 && sFieldName != '') {
				if (sFieldValue == '') {
					sReturn += '- ' + sFieldTitle + ' is required\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else if (isNaN(sFieldValue)) {
					sReturn += '- ' + sFieldTitle + ' is not a number\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else {
	//				sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
					oForm.elements[i].style.backgroundColor = '';
				}
			} else if (sValidationString.indexOf(sFieldName + '[isWholeNum]') >= 1 && sFieldName != '') {
				if (sFieldValue == '') {
					sReturn += '- ' + sFieldTitle + ' is required\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else if (isNaN(sFieldValue)) {
					sReturn += '- ' + sFieldTitle + ' is not a number\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else if (sFieldValue.indexOf('.') > -1) {
					sReturn += '- ' + sFieldTitle + ' must be a whole number\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else if (sFieldValue < 0) {
					sReturn += '- ' + sFieldTitle + ' must be a positive whole number.\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else {
	//				sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
					oForm.elements[i].style.backgroundColor = '';
				}
			} else if (sValidationString.indexOf(sFieldName + '[isIdenticalTo]') >= 1 && sFieldName != '') {

				if (sFieldValue == '') {
					sReturn += '- ' + sFieldTitle + ' is required\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
					
				} else if (sFieldValue != oForm.elements[sFieldName+'2'].value) {  //    oForm.elements['Password2'].value
					sReturn += '- ' + sFieldName + 's do not match\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else {
	//				sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
					oForm.elements[i].style.backgroundColor = '';
					
				}
			} else if (sValidationString.indexOf(sFieldName + '[isNotZero]') >= 1 && sFieldName != '') {
				if (sFieldValue == '') {
					sReturn += '- ' + sFieldTitle + ' is required\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else if (sFieldValue == 0) {
					sReturn += '- ' + sFieldTitle + ' must be selected\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else {
	//				sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
					oForm.elements[i].style.backgroundColor = '';
				}				
			} else if (sValidationString.indexOf(sFieldName + '[isSelected]') >= 1 && sFieldName != '') {
				//loop through entire form again to find checked radio buttons
				for (var iSub=0;iSub < oForm.length;iSub++) {				
					if (oForm.elements[iSub].name == sFieldName && oForm.elements[iSub].checked) {
						//if field is checked then set bol
						bTest = true
					}
				}
				//output message
				if (bTest && sFieldName != sPreviousFieldName) {
	//				sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
					oForm.elements[i].style.backgroundColor = '';
				} else if (!bTest && sFieldName != sPreviousFieldName) {
					sReturn += '- ' + sFieldTitle + ' is required\n';					
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else if (!bTest) {
					// colour all same names check boxes
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else {
					oForm.elements[i].style.backgroundColor = '';
				}		
	//ajax call for captcha			
				
			} else if (sValidationString.indexOf(sFieldName + '[isCaptcha]') >= 1 && sFieldName != '') {
				
				
				
				if (sFieldValue == '') {
					sReturn += '- ' + sFieldTitle + ' is required\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else {
					// check if the captcha entered matches the one stored server side
					var xmlHttp;
					try {
						// Firefox, Opera 8.0+, Safari
						xmlHttp=new XMLHttpRequest();
					}
					catch (e) {
						// Internet Explorer
						try {
							xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
						}
					  	catch (e) {
							try {
								xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
							}
							catch (e) {
						  		sReturn += '- Your browser does not support AJAX, you should update to a newer, more secure browser\n'
							}
						}
					}
						
					
					
					var url = 'checkcaptcha.asp';
					url = url + '?q=' + sFieldValue;
					url = url + '&rnd=' + Math.random();
				  	xmlHttp.open("GET",url,false);
				  	xmlHttp.send(null);
					
					
					
					if (xmlHttp.responseText == "correct") {
								oForm.elements[i].style.backgroundColor = '';
	
							} else if (xmlHttp.responseText == 'wrong') {
								sReturn += '- ' + sFieldTitle + ' is incorrect\n';
								oForm.elements[i].style.backgroundColor = '#FFCCCC';
							} else {
								sReturn += '- ' + sFieldTitle + ' could not be verified at this time\n';	
								oForm.elements[i].style.backgroundColor = '#FFCCCC';
							}	
						}
				
	//		if (sValidationString.indexOf(sFieldName + '[R]') >= 1 && sFieldName != '') {
			} else if (sValidationString.indexOf(sFieldName) >= 1 && sFieldName != '') {
				if (sFieldValue == '') {
					sReturn += '- ' + sFieldTitle + ' is required\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else {
	//				sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
					oForm.elements[i].style.backgroundColor = '';
				}
			} else {
	//			sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
				oForm.elements[i].style.backgroundColor = '';
			}		
			
			sPreviousFieldName = sFieldName
		}
		
		if (sReturn) {
			alert('Please fill out all the following required fields\nbefore submitting this form.\n' + sReturn);
			return(false)
		} else {
			return(true);
		}
	} else {
			return(false)
	}	
		
}
