/***************************************************************************************************
 * This function validates the ImageMax quoter form. If there any errors, display them in a JS alert
 * and put a red box around the affected controls.
 **************************************************************************************************/
function checkFormFields(form) {
		
	// Make sure the error string is empty
	var i = 0;
	var error_string = '';
		
	// Get the style info for the required fields and set them to no border
	var quantityStyle = getStyleObject("quantity_box");
	var sizeStyle = getStyleObject("size_box");
	var materialStyle = getStyleObject("material_box");
	var colorStyle = getStyleObject("color_box");
	var pmsStyle = getStyleObject("pms_box");
	var cmykStyle = getStyleObject("cmyk_box");
		
	// reset the border styles of fields that were corrected
	quantityStyle.border = "solid gray 1px";
	sizeStyle.border = "solid gray 1px";
	materialStyle.border = "solid gray 1px";
	colorStyle.border = "solid gray 1px";
	pmsStyle.border = "solid gray 1px";
	cmykStyle.border = "solid gray 1px";
		
	// Make sure that the quantity is a number
	var qty = document.quote.quantity.value;
	var split_qty = qty.split('/');
	if (split_qty.length == 1) {
		var quantity = document.quote.quantity.value.replace(/([^0-9])/g, "");
		
		// Check the quantity field
		if (quantity > 1500) {
			// Make sure to use the affialtes phone number
			alert("For quantities over 1,500\nPlease contact a Customer Care Rep\n" + "");
			quantityStyle.border = "solid red 1px";
			return false;
		}		
		else if (quantity < 1) {
			i++;
			error_string +=  i + ") Please enter a quantity of 1 to 1,500\n";
			quantityStyle.border = "solid red 1px";
		}
		else if (quantity == '') {
			i++;
			error_string += i + ") Please enter a quantity\n";
			quantityStyle.border = "solid red 1px";
		}	
	}
	
	// Make sure a material was selected
	if (document.quote.material.value == '') {
		i++;
		error_string += i + ") Please select a Material\n";
		materialStyle.border = "solid red 1px";	
	}	
		
	// Make sure a height was entered
	if (document.quote.height.value == '') {
		i++;
		error_string += i + ") Please enter a height value\n";
		sizeStyle.border = "solid red 1px";		
	}			

	// Make sure a height was entered
	if (document.quote.width.value == '') {
		i++;
		error_string += i + ") Please enter a width value\n";
		sizeStyle.border = "solid red 1px";		
	}	
		
		
	// Check to see if at least one color was chosen
	var ink = getControlValue(form,'1');
	if (ink.length == 0 && document.quote.pms_one.value == '' && document.quote.pms_two.value == '' && document.quote.pms_three.value == '' && document.quote.pms_four.value == '' && document.quote.cmyk.checked != true) {
		i++;
		error_string += i + ") You must select a color\n\tor enter a PMS color\n\tor mark that your job is CMYK\n";
		colorStyle.border = "solid red 1px";
		pmsStyle.border = "solid red 1px";	
		cmykStyle.border = "solid red 1px";	
	}			
		
		
	var pmsOne = document.quote.pms_one.value.replace(/([^0-9])/g, "");
	var pmsTwo = document.quote.pms_two.value.replace(/([^0-9])/g, "");
	var pmsThree = document.quote.pms_three.value.replace(/([^0-9])/g, "");
	var pmsFour = document.quote.pms_four.value.replace(/([^0-9])/g, "");
	var numberOfInks = 0;
	if (pmsOne.length > 0 || pmsTwo.length > 0 || pmsThree.length > 0 || pmsFour.length > 0) {
		
		var pms_error_string = '';
			
		if (pmsOne.length > 0 && (pmsOne.length > 4 || pmsOne.length < 3)) {
			i++;
			pms_error_string = ") PMS colors are 3 or 4 digits\n";
			pmsStyle.border = "solid red 1px";
		}
		else if (pmsOne.length > 2 && pmsOne.length < 5) { 
			numberOfInks++; 
		}			
			
		if (pmsTwo.length > 0 && (pmsTwo.length > 4 || pmsTwo.length < 3)) {
			i++;
			pms_error_string = ") PMS colors are 3 or 4 digits\n";
			pmsStyle.border = "solid red 1px";
		}
		else if (pmsTwo.length > 2 && pmsTwo.length < 5) { 
			numberOfInks++; 
		}
			
		if (pmsThree.length > 0 && (pmsThree.length > 4 || pmsThree.length < 3)) {
			i++;
			pms_error_string = ") PMS colors are 3 or 4 digits\n";
			pmsStyle.border = "solid red 1px";
		}	
		else if (pmsThree.length > 2 && pmsThree.length < 5) { 
			numberOfInks++; 
		}
			
		if (pmsFour.length > 0 && (pmsFour.length > 4 || pmsFour.length < 3)) {
			i++;
			pms_error_string = ") PMS colors are 3 or 4 digits\n";
			pmsStyle.border = "solid red 1px";
		}	
		else if (pmsFour.length > 2 && pmsFour.length < 5) { 
			numberOfInks++; 
		}
		
		if (pms_error_string != '') {
			error_string += i + pms_error_string;
		}
	}		
	
	// If there were any errors, make an alert box and prevent submission
	if (error_string != "") {
		var fieldCount;
		if (i == 1) { fieldCount = "field"; }
		else { fieldCount = i + " fields"; }
		alert(error_string + "\nThe error/missing required " + fieldCount + " will be outlined in red"); 
		return false;
	}
}
