var emailexp = /(.*)\@(.*)\.(.*)/;
var emailsp = /\s/;
var wholenumbersonly = /[^0-9]/;
var numbersonly = /[^0-9\.]/;
function isValid(pattern, str) {
	return pattern.test(str)
}

function doButtons(image, name) 
{
    document[name].src=image;
}

function detectEnter(e, what) {
	var Key='';
	if (window.event) {
		Key = window.event.keyCode;
	}
	else {
		Key = e.keyCode
	}
	if (Key == 13) {
		if (window.event) {
			event.returnValue = false;
			VerifyFormData(what)
		}
		else {
			VerifyFormData(what)
			event.cancel = true;
		}
	}
}

function VerifyFormData(what) {
	var myName = '';
	var myType = '';
	var valid = true;
	var rExp = new RegExp ('FIELD_', 'g');
	for (var i=0, j=what.elements.length; i<j; i++) {
		myType = what.elements[i].type;
		myName = what.elements[i].name;
		var myMatch = myName.match(rExp);
		if (myMatch) {
			var myRequired = myName.replace(rExp, 'REQUIRED_');
			if (what[myRequired]) {
				if (myType == 'text') {
					if (what.elements[i].value == what.elements[i].defaultValue || what.elements[i].value == "Enter Data") {
						valid = false;
						what[myName].value = "Enter Data";
						what[myName].select()
						what[myName].focus()
						alert("Please enter " + what[myRequired].value)
						break;
					}
				}
				if (myType == 'radio') {
					var radioSelected = 0;
					for (var a=0; a<what[myName].length; a++){ 
						if (what[myName][a].checked) {
							radioSelected++;
						}
					}
					if (radioSelected < 1) {
						valid = false;
						alert("Please select " + what[myRequired].value)
						break;
					}
				}
				if (myType == 'select-one') {
					if (what.elements[i].selectedIndex == 0) {
						valid = false;
						what[myName].focus()
						alert("Please select " + what[myRequired].value)
						break;
					}
				}
				if (myType == 'textarea') {
					if (what.elements[i].value == what.elements[i].defaultValue || what.elements[i].value == "Enter Data") {
						valid = false;
						what[myName].value = "Enter Data";
						what[myName].select()
						what[myName].focus()
						alert("Please enter " + what[myRequired].value)
						break;
					}
				}
			}
		}
	}
	if (valid == true) {
		if (what.FIELD_security) {
			if (what.FIELD_security.value != "intellect") {
				valid = false;
				what.FIELD_security.focus()
				alert("Please enter the security code")
			}
		}
	}
	if (valid == true) {
		what.submit();
	}
	else {
		return false;
	}
}

function captureStepOne(e, what) {
	var Key='';
	if (window.event) {
		Key = window.event.keyCode;
	}
	else {
		Key = e.keyCode
	}
	if (Key == 13) {
		if (window.event) {
			event.returnValue = false;
			VerifyStepOne(what)
		}
		else {
			 event.cancel = true;
			 VerifyStepOne(what)
		}
	}
}

function captureStepTwo(e, what) {
	var Key='';
	if (window.event) {
		Key = window.event.keyCode;
	}
	else {
		Key = e.keyCode
	}
	if (Key == 13) {
		if (window.event) {
			event.returnValue = false;
			VerifyStepTwo(what)
		}
		else {
			 event.cancel = true;
			 VerifyStepTwo(what)
		}
	}
}

function VerifyStepTwo(what) {
	var myName = '';
	var valid = true;
	var alertcolor = "#f5e9ef";
	if (what.quote_quantity.value != "") {
		if (isValid(wholenumbersonly, what.quote_quantity.value)) {
			var valid = false;
			what.quote_quantity.focus()
			what.quote_quantity.style.backgroundColor = alertcolor;
			alert("The quantity field accepts whole numbers only.")
			return false;
		}
		else if (what.quote_quantity.value <= 0) {
			var valid = false;
			what.quote_quantity.focus()
			what.quote_quantity.style.backgroundColor = alertcolor;
			alert("The quantity must be greater than 0.")
			return false;
		}
		else {
			what.quote_quantity.style.backgroundColor = "#ffffff";
		}
	}
	else {
		what.quote_quantity.style.backgroundColor = "#ffffff";
	}
	if (what.quote_turn.value != "") {
		if (isValid(wholenumbersonly, what.quote_turn.value)) {
			var valid = false;
			what.quote_turn.focus()
			what.quote_turn.style.backgroundColor = alertcolor;
			alert("The turn-time field accepts whole numbers only.")
			return false;
		}
		else if (what.quote_turn.value <= 0) {
			var valid = false;
			what.quote_turn.focus()
			what.quote_turn.style.backgroundColor = alertcolor;
			alert("The turn-time must be greater than 0.")
			return false;
		}
		else {
			what.quote_turn.style.backgroundColor = "#ffffff";
		}
	}
	else {
		what.quote_turn.style.backgroundColor = "#ffffff";
	}
	if (what.quote_width.value != "") {
		if (isValid(numbersonly, what.quote_width.value)) {
			var valid = false;
			what.quote_width.focus()
			what.quote_width.style.backgroundColor = alertcolor;
			alert("The board width field accepts numbers only.")
			return false;
		}
		else if (what.quote_width.value <= 0) {
			var valid = false;
			what.quote_width.focus()
			what.quote_width.style.backgroundColor = alertcolor;
			alert("The board width must be greater than 0.")
			return false;
		}
		else {
			what.quote_width.style.backgroundColor = "#ffffff";
		}
	}
	else {
		what.quote_width.style.backgroundColor = "#ffffff";
	}
	if (what.quote_height.value != "") {
		if (isValid(numbersonly, what.quote_height.value)) {
			var valid = false;
			what.quote_height.focus()
			what.quote_height.style.backgroundColor = alertcolor;
			alert("The board height field accepts numbers only.")
			return false;
		}
		else if (what.quote_height.value <= 0) {
			var valid = false;
			what.quote_height.focus()
			what.quote_height.style.backgroundColor = alertcolor;
			alert("The board height must be greater than 0.")
			return false;
		}
		else {
			what.quote_height.style.backgroundColor = "#ffffff";
		}
	}
	else {
		what.quote_height.style.backgroundColor = "#ffffff";
	}
	if (what.quote_thickness.value != "") {
		if (isValid(numbersonly, what.quote_thickness.value)) {
			var valid = false;
			what.quote_thickness.focus()
			what.quote_thickness.style.backgroundColor = alertcolor;
			alert("The board thickness field accepts numbers only.")
			return false;
		}
		else if (what.quote_thickness.value <= 0) {
			var valid = false;
			what.quote_thickness.focus()
			what.quote_thickness.style.backgroundColor = alertcolor;
			alert("The board thickness must be greater than 0.")
			return false;
		}
		else {
			what.quote_thickness.style.backgroundColor = "#ffffff";
		}
	}
	else {
		what.quote_thickness.style.backgroundColor = "#ffffff";
	}
	if (what.quote_tracewidth.value != "") {
		if (isValid(numbersonly, what.quote_tracewidth.value)) {
			var valid = false;
			what.quote_tracewidth.focus()
			what.quote_tracewidth.style.backgroundColor = alertcolor;
			alert("The min. trace width field accepts numbers only.")
			return false;
		}
		else if (what.quote_tracewidth.value <= 0) {
			var valid = false;
			what.quote_tracewidth.focus()
			what.quote_tracewidth.style.backgroundColor = alertcolor;
			alert("The min. trace width must be greater than 0.")
			return false;
		}
		else {
			what.quote_tracewidth.style.backgroundColor = "#ffffff";
		}
	}
	else {
		what.quote_tracewidth.style.backgroundColor = "#ffffff";
	}
	if (what.quote_airgap.value != "") {
		if (isValid(numbersonly, what.quote_airgap.value)) {
			var valid = false;
			what.quote_airgap.focus()
			what.quote_airgap.style.backgroundColor = alertcolor;
			alert("The min. air gap field accepts numbers only.")
			return false;
		}
		else if (what.quote_airgap.value <= 0) {
			var valid = false;
			what.quote_airgap.focus()
			what.quote_airgap.style.backgroundColor = alertcolor;
			alert("The min. air gap must be greater than 0.")
			return false;
		}
		else {
			what.quote_airgap.style.backgroundColor = "#ffffff";
		}
	}
	else {
		what.quote_airgap.style.backgroundColor = "#ffffff";
	}
	if (what.quote_drillsize.value != "") {
		if (isValid(numbersonly, what.quote_drillsize.value)) {
			var valid = false;
			what.quote_drillsize.focus()
			what.quote_drillsize.style.backgroundColor = alertcolor;
			alert("The min. drill size field accepts numbers only.")
			return false;
		}
		else if (what.quote_drillsize.value <= 0) {
			var valid = false;
			what.quote_drillsize.focus()
			what.quote_drillsize.style.backgroundColor = alertcolor;
			alert("The min. drill size must be greater than 0.")
			return false;
		}
		else {
			what.quote_drillsize.style.backgroundColor = "#ffffff";
		}
	}
	else {
		what.quote_drillsize.style.backgroundColor = "#ffffff";
	}
	var rExp = new RegExp ('ic_quote', 'g');
	for (var i=0, j=what.elements.length; i<j; i++) {
		myName = what.elements[i].name;
		var myMatch = myName.match(rExp);
		if (myMatch) {
			if (what.elements[i].selectedIndex == 0) {
				valid = false;
				what[myName].focus()
				what[myName].style.backgroundColor = alertcolor;
				alert("Please make a selection for the highlighted impedance control field.")
				break;
			}
			else {
				what[myName].style.backgroundColor = "#ffffff";
			}
		}
	}
	if (valid == true) {
		if (FindDuplicates(what)) {
			what.submit();
		}
		else {
			return false;
		}
	}
	else {
		return false;
	}
}

function FindDuplicates(what) {
	var myName = '';
	var myNameTwo = '';
	var valid = true;
	var rExp = new RegExp ('ic_quote_layer', 'g');
	for (var i=0, j=what.elements.length; i<j; i++) {
		myName = what.elements[i].name;
		var myMatch = myName.match(rExp);
		var currentValue = '';
		var matchValue = '';
		if (myMatch) {
			currentValue = what[myName].value;
			for (var k=0, l=what.elements.length; k<l; k++) {
				myNameTwo = what.elements[k].name;
				var myMatchTwo = myNameTwo.match(rExp);
				if (myMatchTwo) {
					matchValue = what[myNameTwo].value;
					if (myName != myNameTwo && currentValue == matchValue) {
						valid = false;
						alert("Please select a unique layer number for each impedance control field.")
						break;
					}
				}
			}
		}
		if (valid == false) {
			break;
		}
	}
	return valid;
}

function VerifyStepOne(what) {
	var valid = true;
	var alertcolor = "#f5e9ef";
	if (what.quote_name.value == "") {
		var valid = false;
		what.quote_name.focus()
		what.quote_name.style.backgroundColor = alertcolor;
		alert("Please enter a contact name.")
		return false;
	}
	else {
		what.quote_name.style.backgroundColor = "#ffffff";
	}
	if (what.quote_email.value == "") {
		var valid = false;
		what.quote_email.focus()
		what.quote_email.style.backgroundColor = alertcolor;
		alert("Please enter a contact e-mail address.")
		return false;
	}
	else {
		what.quote_email.style.backgroundColor = "#ffffff";
	}
	if (!isValid(emailexp, what.quote_email.value)) {
		var valid = false;
		what.quote_email.focus()
		what.quote_email.style.backgroundColor = alertcolor;
		alert("Please enter a valid contact e-mail address.")
		return false;
	}
	else {
		what.quote_email.style.backgroundColor = "#ffffff";
	}
	if (isValid(emailsp, what.quote_email.value)) {
		var valid = false;
		what.quote_email.focus()
		what.quote_email.style.backgroundColor = alertcolor;
		alert("Please enter a valid contact e-mail address.")
		return false;
	}
	else {
		what.quote_email.style.backgroundColor = "#ffffff";
	}
	if (what.quote_phone.value == "") {
		var valid = false;
		what.quote_phone.focus()
		what.quote_phone.style.backgroundColor = alertcolor;
		alert("Please enter a contact phone number.")
		return false;
	}
	else {
		what.quote_phone.style.backgroundColor = "#ffffff";
	}
	if (what.quote_company.value == "") {
		var valid = false;
		what.quote_company.focus()
		what.quote_company.style.backgroundColor = alertcolor;
		alert("Please enter your company name.")
		return false;
	}
	else {
		what.quote_company.style.backgroundColor = "#ffffff";
	}
	if (what.quote_project.value == "") {
		var valid = false;
		what.quote_project.focus()
		what.quote_project.style.backgroundColor = alertcolor;
		alert("Please enter a project name.")
		return false;
	}
	else {
		what.quote_project.style.backgroundColor = "#ffffff";
	}
	if (what.quote_layers.value == "") {
		var valid = false;
		what.quote_layers.focus()
		what.quote_layers.style.backgroundColor = alertcolor;
		alert("Please enter the number of layers for this PCB.")
		return false;
	}
	else {
		what.quote_layers.style.backgroundColor = "#ffffff";
	}
	if (isValid(wholenumbersonly, what.quote_layers.value)) {
		var valid = false;
		what.quote_layers.value = "";
		what.quote_layers.focus()
		what.quote_layers.style.backgroundColor = alertcolor;
		alert("The PCB layers field accepts whole numbers only.")
		return false;
	}
	else {
		what.quote_layers.style.backgroundColor = "#ffffff";
	}
	if (what.quote_layers.value <= 0) {
		var valid = false;
		what.quote_layers.focus()
		what.quote_layers.style.backgroundColor = alertcolor;
		alert("The number of layers must be greater than 0.")
		return false;
	}
	else {
		what.quote_layers.style.backgroundColor = "#ffffff";
	}
	if (what.quote_impedance.value == "") {
		var valid = false;
		what.quote_impedance.focus()
		what.quote_impedance.style.backgroundColor = alertcolor;
		alert("Please enter the number of layers with impedance control. Enter 0 if no layers have impedance control.")
		return false;
	}
	else {
		what.quote_impedance.style.backgroundColor = "#ffffff";
	}
	if (isValid(wholenumbersonly, what.quote_impedance.value)) {
		var valid = false;
		what.quote_impedance.value = "";
		what.quote_impedance.focus()
		what.quote_impedance.style.backgroundColor = alertcolor;
		alert("The impedance control field accepts whole numbers only.")
		return false;
	}
	else {
		what.quote_impedance.style.backgroundColor = "#ffffff";
	}
	if (what.quote_impedance.value > what.quote_layers.value) {
		var valid = false;
		what.quote_impedance.focus()
		what.quote_impedance.style.backgroundColor = alertcolor;
		alert("The number of layers with impedance control can not be greater than the total number of layers.")
		return false;
	}
	else {
		what.quote_impedance.style.backgroundColor = "#ffffff";
	}
	if (what.quote_rohs.value == "") {
		var valid = false;
		what.quote_rohs.focus()
		what.quote_rohs.style.backgroundColor = alertcolor;
		alert("Please select yes or no for RoHS compliance.")
		return false;
	}
	else {
		what.quote_rohs.style.backgroundColor = "#ffffff";
	}
	if (valid == true) {
		what.submit();
	}
	else {
		return false;
	}
}