//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 = document.getElementById('validation').value;
	//alert(sValidationString);
	
	//question submission
	bConfirm = confirm('Are you sure you wish to submit this form?')
	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. Please contact Budget-Pack on 01453 793020 for advice on submitting negative amounts.\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else {
	//				sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
					oForm.elements[i].style.backgroundColor = '';
				}
				
			//ajax call for captcha			
				
			} else if (sValidationString.indexOf(sFieldName + '[isCaptcha]') >= 1 && sFieldName != '') {
				fieldChecked = true;
				
				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 = 'process.asp?action=checkcaptcha';
					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';
							}	
						}
				
	
			} 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 = '';
				}	



				
	//		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)
	}	
		
}

function validateFormNoConfirm(oForm) {
	sValidationString = document.getElementById('validation').value;
	//alert(sValidationString);
	
	//question submission
	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 {
	//				sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
					oForm.elements[i].style.backgroundColor = '';
				}
				
				
				
			//ajax call for captcha			
				
			} else if (sValidationString.indexOf(sFieldName + '[isCaptcha]') >= 1 && sFieldName != '') {
				fieldChecked = true;
				
				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 = 'process.asp?action=checkcaptcha';
					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';
							}	
						}
					
				
				
				
				
				
				
			} 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 + '[isDateDDMMYYYY]') >= 1 && sFieldName != '') { 
			
				if (isDate(sFieldValue)==false){
					sReturn += '- ' + sFieldTitle + ' must be a date in DD/MM/YYYY format\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				}
	
			
			} 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 = '';
				}			
	//		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)
	}	
		
}	


function evaluateValues(oForm,iLoop,sFieldPrefix) 
{
	
	document.getElementById('submit').style.display='none';
	document.getElementById('loadingbar').style.visibility='visible';
	
	
	//we store the original validation string for use at the end to reset it
	var originalValidationString = document.getElementById('validation').value;
	
	if (validateForm(oForm) == false)
		{
		document.getElementById('submit').style.display='';
		document.getElementById('loadingbar').style.visibility='hidden';
		return(false);
		}
	else
		{
		
	//passed mand field checks, so go ahead and work out comparison of values
	//firstly we disregard doing any checks if they have submitted data for all year (period = 0)
	
		if (document.getElementById('Period').value==0) 
			{
			//its already in the chunk where its passed the basic form validation, so lets post it
				return(true);
			}
		
			
		var dynamicFields //used globally by whole function for dynamic mand fields 	
			
		//split sFieldPrefix by Pipe
		var aFieldPrefix = new Array();
		aFieldPrefix = sFieldPrefix.split("|")
		var p;
		//for (p in aFieldPrefix)
		//{
		//document.write(aFieldPrefix[p] + "<br />"); //for testing - remove
		//}
		for (p in aFieldPrefix)
			{
			//loop thru all fieldprefixes and store as aFieldPrefix array made above. this allows us to loop thru any number of ;sets' of fields e.g. b2b or b2c. the loop within, below adds the numbers for the end fo the form names up to the value of iloop.
				for (var i=1;i < (iLoop+1);i++) 
					{
					//loops through adding the appropriate number (i)to the end of the form name element. we start at 1 as in the array usually starts at 0 which doesnt relate to a form name element
						var sName = aFieldPrefix[p]+i
						//this is the name fo form element to get the value of, as it loops thru
						//alert(sName)
						var sSubmittedValue = oForm[sName].value;
							//call ajax asp page and pass submitted value
							//alert(sSubmittedValue)
									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 theYear = getCheckedValue(oForm['Year']);
									var thePeriod = getCheckedValue(oForm['Period']);
							
									var url = 'process.asp';
									url = url + '?year=' + theYear;
									url = url + '&period=' + thePeriod;
									url = url + '&category=' + i;
									url = url + '&submittedValue=' + sSubmittedValue
									url = url + '&dbFieldName=' + aFieldPrefix[p] //the current value for the looping field prefix - e.g. B2B or B2C etc 
									url = url + '&action=evaluateData'
									url = url + '&reg=WEEE'
									url = url + '&random='+Math.random() //prevent caching
									
									//alert(url)
									xmlHttp.open("GET",url,false); //false - waits for the outcome before proceeding
									xmlHttp.send(null);
									
									var sBaseName = aFieldPrefix[p] //used for the field names below
									var sReasonDivName = sName+'Reason';
									
									//alert(xmlHttp.responseText);
									
									if (xmlHttp.responseText != "-1") 
										{
												// needs to flag a reason -  add text, or show fields after the input field - easier to just show css hidden fields
												
												//below we modify the first option to reflect the reason teh box is shown at all. 
												if ((xmlHttp.responseText == "0") && (oForm[sBaseName+'Diff'+i].type=='select-one'))  
													{
													oForm[sBaseName+'Diff'+i].options[0].text = "Same value as last quarter. Please Explain";
													}
												else
													{
												//alert(sBaseName+'Diff'+i); note that diffDetails7 always gets flagged as incorrect, but passes a 1 for N/A by default
												oForm[sBaseName+'Diff'+i].options[0].text = "Substantial change from last quarter. Please Explain";
													}
												document.getElementById(sReasonDivName).style.display = '' ;  //makes a B2B1Reason div show
												//add to the mand fields for the validation
												dynamicFields += ', '+sBaseName+'Diff'+i+'[isNotZero]'
												dynamicFields += ', '+sBaseName+'DiffDetails'+i //these are added together and added to the validation string at the end of the function. not all comments fields are mandatory now
										} 
									else if (xmlHttp.responseText == "-1" ) 
										{
											// this line not requiring a reason
											//need to set the two fields as 1 (for n/a) and a space for the other so it passes mandatory validation
											//we need to set the pulldown menu to N/A which has the corresponding id of 1 in the db. however, as this doesnt exist in the drop down list at e.g. B2BDiff we need to add it in, then set the passed value to it
											oForm[sBaseName+'Diff'+i].options[0] = new Option("N/A",1);
											oForm[sBaseName+'Diff'+i].value = '1'//this is the select form for the reason
											oForm[sBaseName+'DiffDetails'+i].value=' ' //a space to pass mandatory validation - it gets replaced later
											//in case they changed their data and no longer should show a mand reason field, we also need to hide the reason boxes
											document.getElementById(sReasonDivName).style.display = 'none' ;	
										} 
									else 
										{
											//didnt get anything back or an error - let them submit, but flag somehow to BP?
										}	
					   }//close for the iloop

			  }//close loop for fieldprefixes
				  
			  //now we add on any values that need to be mandatory to the validation value

			 //due to the way its added, the word undefined gets added to the dynamic fields var
			 dynamicFields = dynamicFields.replace(/undefined/,'')
			  
			 sValidationString = document.getElementById('validation').value;
			 document.getElementById('validation').value = sValidationString + dynamicFields
			 
			 //alert(document.getElementById('validation').value);
			 
			 //alert(document.getElementById('validation').value)
			 //call the validate function again..
			if (validateFormNoConfirm(oForm) == true)
				{
					//document.getElementById('validation').value = originalValidationString;
					return(true);
				}
			else 
				{
					//sounds odd, but we need to remove the reason fields from the validation string in case the client alters their data to a lower figure etc. they would not be able to do that without entering a reason (as its mandatory) and a reason may not be required.. so the first sweep of the validation does not include the mandatory reason fields.. only the 2nd sweep does
					
					var toRemove = dynamicFields;
					var remove = new RegExp(toRemove,'gi');
					//alert(remove)				
					document.getElementById('validation').value = document.getElementById('validation').value.replace(remove,''); //THIS DOESNT WORK due to the [] used in the validation names - therefore, we kept a var with the orignal madatory fields in, so we can just reset it back to those again.. any subsequent changes will get added on again as before
					
					document.getElementById('validation').value = originalValidationString;
					
					//rename the submit back to what it was
					document.getElementById('submit').style.display='';
					document.getElementById('loadingbar').style.visibility='hidden';
					//alert(document.getElementById('validation').value);
					return(false);
			}
}

}//removed a closer from here


function evaluatePackagingValues(oForm,iLoop,sFieldPrefix) 
{
	//alert("test1")
	document.getElementById('submit').style.display='none';
	document.getElementById('loadingbar').style.visibility='visible';
	
	var comments = oForm['comments'].value;
	
	//we store the original validation string for use at the end to reset it
	var originalValidationString = document.getElementById('validation').value;
	
	if (validateForm(oForm) == false)
		{
			document.getElementById('submit').style.display='';
			document.getElementById('loadingbar').style.visibility='hidden';
			return(false);
		}
	else
		{	//alert("test2")
			//passed mand field checks, so go ahead and work out comparison of values
			//firstly we disregard doing any checks if they have submitted data for all year (period = 0)
			var dynamicFields //used globally by whole function for dynamic mand fields 		
			var flaggedFields //used globally by this function to say t/f whether any fields have been flagged during this validation check. 
				
			//split sFieldPrefix by Pipe
			var aFieldPrefix = new Array();
			aFieldPrefix = sFieldPrefix.split("|")
			var p;
		
			for (p in aFieldPrefix)
			{
			//loop thru all fieldprefixes and store as aFieldPrefix array made above. this allows us to loop thru any number of ;sets' of fields e.g. b2b or b2c. the loop within, below adds the numbers for the end fo the form names up to the value of iloop.
				for (var i=1;i < (iLoop+1);i++) 
				{
					//loops through adding the appropriate number (i)to the end of the form name element. we start at 1 as in the array usually starts at 0 which doesnt relate to a form name element
					var sName = aFieldPrefix[p]+i
					//this is the name fo form element to get the value of, as it loops thru
					//alert(sName)
					var sSubmittedValue = oForm[sName].value;
						//call ajax asp page and pass submitted value
						//alert(sSubmittedValue)
	
								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 theYear = getCheckedValue(oForm['Year']);
								var thePeriod = oForm['Period'].value;
								var iCategory = oForm['Category'+i].value //the actual category id is stored in this hidden field (one for each table, name category17, category18  etc)
								
								var iBusinessActivity = oForm['BusinessActivity'+i].value; //this is used to pass a lookup value for business activity to the evaluation script, so it can display the appropriateactivity on the validation form - e.g. Manufacturing, Conversion etc
						
								var url = 'process.asp';
								url = url + '?year=' + theYear;
								url = url + '&period=' + thePeriod;
								url = url + '&category=' + iCategory;
								url = url + '&submittedValue=' + sSubmittedValue
								url = url + '&dbFieldName=' + aFieldPrefix[p] //the current value for the looping field prefix - e.g. steeltons etc 
								url = url + '&action=evaluateData'
								url = url + '&reg=Packaging'
								url = url + '&businessActivity='+ iBusinessActivity
								url = url + '&random='+Math.random() //prevent caching
								
								//confirm(url)
								xmlHttp.open("GET",url,false); //false - waits for the outcome before proceeding
								xmlHttp.send(null);
								
								
								
								
								var sBaseName = aFieldPrefix[p] //used for the field names below
								//var sReasonDivName = sName+'Reason';
								
								//alert(url + '/n' + xmlHttp.responseText);
								
								if (xmlHttp.responseText.indexOf('decreased')!=-1||xmlHttp.responseText.indexOf('increased')!=-1) 
								{
										// needs to flag a reason -  add text to the comments field etc
		
										//we check for the existence of it before adding it to the comments box
									//	alert(xmlHttp.responseText)
									//alert('dfgdfg'+comments)
									oForm[sBaseName+i].style.backgroundColor='#CCFF99';
									
									if (comments.indexOf(xmlHttp.responseText)==-1)
									{ //not found in current comments box - we add it in
	
											//if the message was found in the comments field - e.g. 'Table 1's packaging for Paper has significantly increased from last year, please explain;' then it stands to reason that the opposite message that can be returned cannot possibly by true (thats its decreased). So - we do that check here and remove the opposing message if appropritate
										if (xmlHttp.responseText.indexOf('decreased')!=-1)
										{
											//the new message is a decreased message. we swap the decreased for increased and check the comments field for an occurence
											var strRemove = xmlHttp.responseText.replace('decreased','increased');
										} 
										else
										{//else, we check for the opposite. remember that one of these conditions is going to be true but we havent updated the xmlhttp response to the comments textarea yet so it wont be found.
											var strRemove = xmlHttp.responseText.replace('increased','decreased');
										}
											
										var regExpRemove = new RegExp(strRemove,'gi');
										
										comments = comments.replace(regExpRemove,'');
										//run the remove script here and update the ffields var below if was found..
										//we need to keep a record of which fields were flagged so we can easily see changes on the next sweep
										flaggedFields=true;
									
										comments = comments + xmlHttp.responseText+'\r\r\r';
										
									}
							    }
								else  
								{
									// this line not requiring a reason and returns the base portion of the relevant question
									
									//if not found, remove the message if previously added to comments. check both permutations - return the base of the message in order to easily check for both variations and remove them from the comments field as necessary
									
									var baseComment = xmlHttp.responseText;
									var baseCommentDecreased = baseComment+' decreased significantly since last year, please explain;';
									var baseCommentIncreased = baseComment+' increased significantly since last year, please explain;';
									
									var strRemoveDecreased = baseCommentDecreased;
									var regExpRemoveDecreased = new RegExp(strRemoveDecreased,'gi');
									
									var strRemoveIncreased = baseCommentIncreased;
									var regExpRemoveIncreased = new RegExp(strRemoveIncreased,'gi');
									
									if (comments.indexOf(strRemoveDecreased)!=-1){
									//remove the text from teh comments box
										comments = comments.replace(regExpRemoveDecreased,'');
										flaggedFields=true; //the message has changed, so dont submit it
										oForm[sBaseName+i].style.backgroundColor='';
									}
									if (comments.indexOf(strRemoveIncreased)!=-1){
									//remove the text from teh comments box
										comments = comments.replace(regExpRemoveIncreased,'');
										flaggedFields=true; //the message has changed, so dont submit it
										oForm[sBaseName+i].style.backgroundColor='';
									}
									
	
								} 
								
							
							} //close for iloop
				
				}//close loop for fieldprefixes
				
			
	
			//if any fields have been flagged as different then the flaggedfields variable would now be set to true. this is all we need to check in order to return the form as false or not. if they firstly submit the form, and the relevant field messages appear for them to enter a reason against, they cannot be mandatory so on the 2nd sweep providing no data had changed, the form will submit.
			
			//we must put back any changes to the comments variable to the actual form
			oForm['comments'].value = comments;
			
			if (flaggedFields==true)
			{
			
				//rename the submit back to what it was
				document.getElementById('submit').style.display='';
				document.getElementById('loadingbar').style.visibility='hidden';
				//alert(document.getElementById('validation').value);
				return(false);
					//}	
			}
			else
			{
				//no field messages have been added during this validation excercise. therefore the form is ok to submit - theres no new comments required from them
				//alert('return true')
				return(true);
				//return(false)
			}

}
	}




// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) 
{
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) 
	{
		if(radioObj[i].checked) 
		{
			return radioObj[i].value;
		}
	}
	return "";
}
//} removed this as was acting as a nested function to the evaluatevalues function


//isdate checks

				var dtCh= "/";
				var minYear=1900;
				var maxYear=2100;

				function isInteger(s){
					var i;
					for (i = 0; i < s.length; i++){   
						// Check that current character is number.
						var c = s.charAt(i);
						if (((c < "0") || (c > "9"))) return false;
					}
					// All characters are numbers.
					return true;
				}
				
				function stripCharsInBag(s, bag){
					var i;
					var returnString = "";
					// Search through string's characters one by one.
					// If character is not in bag, append to returnString.
					for (i = 0; i < s.length; i++){   
						var c = s.charAt(i);
						if (bag.indexOf(c) == -1) returnString += c;
					}
					return returnString;
				}
				
				function daysInFebruary (year){
					// February has 29 days in any year evenly divisible by four,
					// EXCEPT for centurial years which are not also divisible by 400.
					return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
				}
				function DaysArray(n) {
					for (var i = 1; i <= n; i++) {
						this[i] = 31
						if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
						if (i==2) {this[i] = 29}
				   } 
				   return this
				}
				
				function isDate(dtStr){
					var daysInMonth = DaysArray(12)
					var pos1=dtStr.indexOf(dtCh)
					var pos2=dtStr.indexOf(dtCh,pos1+1)
					var strDay=dtStr.substring(0,pos1)
					var strMonth=dtStr.substring(pos1+1,pos2)
					var strYear=dtStr.substring(pos2+1)
					strYr=strYear
					if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
					if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
					for (var i = 1; i <= 3; i++) {
						if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
					}
					month=parseInt(strMonth)
					day=parseInt(strDay)
					year=parseInt(strYr)
					if (pos1==-1 || pos2==-1){
						//alert("The date format should be : mm/dd/yyyy")
						return false
					}
					if (strMonth.length<1 || month<1 || month>12){
						//alert("Please enter a valid month")
						return false
					}
					if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
						//alert("Please enter a valid day")
						return false
					}
					if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
						//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
						return false
					}
					if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
						//alert("Please enter a valid date")
						return false
					}
				return true
				}
