// JavaScript Document

function navBasicFeatures(){
	document.navform.action.value = "basicFeatures";
	document.navform.currentPage.value = "1";
	if (parseInt(document.navform.progress.value) < 2) {
		document.navform.progress.value = "1";
	}
	document.navform.submit();
}

function navEncoding(){
	document.navform.action.value = "encoding";
	document.navform.currentPage.value = "2";
	if (parseInt(document.navform.progress.value) < 2) {
		document.navform.progress.value = "2";
	}
	document.navform.submit();
}

function navCardStock(){
	document.navform.action.value = "cardStock";
	document.navform.currentPage.value = "3";
	if (parseInt(document.navform.progress.value) < 3) {
		document.navform.progress.value = "3";
	}
	document.navform.submit();
}

function navSecurityFeatures(){
	document.navform.action.value = "securityFeatures";
	document.navform.currentPage.value = "4";
	if (parseInt(document.navform.progress.value) < 4) {
		document.navform.progress.value = "4";
	}
	document.navform.submit();
}

function navSoftware(){
	document.navform.action.value = "software";
	document.navform.currentPage.value = "5";
	if (parseInt(document.navform.progress.value) < 5) {
		document.navform.progress.value = "5";
	}
	document.navform.submit();
}

function navResults(){
	document.navform.action.value = "results";
	document.navform.currentPage.value = "6";
	if (parseInt(document.navform.progress.value) < 6) {
		document.navform.progress.value = "6";
	}
	document.navform.submit();
}



// Start form field validation
function validateFormOnSubmit(theForm,scenario) {
	var reason = "";
	if (scenario == 'product') {
		reason += validateNotNone(theForm.brand_id);
		reason += validateNotNone(theForm.product_type_id);
	  reason += validateEmpty(theForm.internal_item_code);
		reason += validateEmpty(theForm.product_name);
		reason += validateCurrency(theForm.msrp_price);
	} else if (scenario == 'websiteproduct') {
		reason += validateEmpty(theForm.url);
		reason += validateEmpty(theForm.display_name);
	} else if (scenario == 'printercomparison') {
		reason += validateInt(theForm.mfgr_warranty_years);
		reason += validateInt(theForm.loaner_warranty_years);
		reason += validateInt(theForm.printhead_warranty_years);
		reason += validateNotNone(theForm.feed_type);
		reason += validateNotNone(theForm.print_method);
		reason += validateInt(theForm.input_hopper_capacity);
		reason += validateInt(theForm.Output_hopper_capacity);
	} else if (scenario == 'supplylocator') {
		reason += validateAtleastOneNotNone(theForm.category4, theForm.category5, theForm.category6, theForm.category7, theForm.category8);
		reason += validateInt(theForm.displayRank);
	}
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validateInt(fld) {
	var error = "";

//	if (isInteger(fld.value)) {
	if(isNaN(parseInt(fld.value))) {
			fld.style.background = 'Yellow'; 
			error = "The required field "+fld.name.replace(/_/g," ").toUpperCase()+" has not been filled in.\n"
	} else {
			fld.style.background = 'White';
	}
	return error;   
}

function validateEmpty(fld) {
	var error = "";

	if (fld.value.length == 0) {
			fld.style.background = 'Yellow'; 
			error = "The required field "+fld.name.replace(/_/g," ").toUpperCase()+" has not been filled in.\n"
	} else {
			fld.style.background = 'White';
	}
	return error;   
}

function validateNotNone(fld) {
	var error = "";
	if (fld.value == "none") {
			fld.style.background = 'Yellow'; 
			error = "The required field "+fld.name.replace(/_/g," ").toUpperCase()+" has not been filled in.\n"
	} else {
			fld.style.background = 'White';
	}
	return error;   
}

function validateAtleastOneNotNone(fld1, fld2, fld3, fld4, fld5) {
	var error = "";
	if ((fld1.value == "none") && (fld2.value == "none") && (fld3.value == "none") && (fld4.value == "none") && (fld5.value == "none")) {
			fld1.style.background = 'Yellow';
			fld2.style.background = 'Yellow';
			fld3.style.background = 'Yellow';
			fld4.style.background = 'Yellow';
			fld5.style.background = 'Yellow';
			error = "At least one pulldown must be used.\n"
	} else {
			fld1.style.background = 'White';
			fld2.style.background = 'White';
			fld3.style.background = 'White';
			fld4.style.background = 'White';
			fld5.style.background = 'White';
			var fieldCount = 0; var goodField = "";
			if (fld1.value != "none") {fieldCount = fieldCount + 1; goodField = fld1.value;}
			if (fld2.value != "none") {fieldCount = fieldCount + 1; goodField = fld2.value;}
			if (fld3.value != "none") {fieldCount = fieldCount + 1; goodField = fld3.value;}
			if (fld4.value != "none") {fieldCount = fieldCount + 1; goodField = fld4.value;}
			if (fld5.value != "none") {fieldCount = fieldCount + 1; goodField = fld5.value;}
			if (fieldCount > 1) {
				error = "Only one pulldown must be used.\n"
				if (fld1.value != "none") {fld1.style.background = 'Yellow';}
				if (fld2.value != "none") {fld2.style.background = 'Yellow';}
				if (fld3.value != "none") {fld3.style.background = 'Yellow';}
				if (fld4.value != "none") {fld4.style.background = 'Yellow';}
				if (fld5.value != "none") {fld5.style.background = 'Yellow';}
			} else {
				doAddSupplyLocator(goodField);
//				alert(goodField);
			}
	}
	return error;   
}

function validateCurrency(fld) {
	var error = "";

	if (fld.value == "") {
			fld.value = 0;
			fld.style.background = 'White';
	} else {
		if(!isNumber(fld.value)) { 
			fld.style.background = 'Yellow'; 
			error = "The field "+fld.name.replace(/_/g," ").toUpperCase()+" requires a valid amount (eg. 987 or 846.87 or 0.75).\n" 
		} else {
			fld.style.background = 'White';
		}
	}
	return error;  
}

function isNumber(str) { 
	isPrice = /^\d+(\.\d{2})?$/;
	return isPrice.test( str ); 
} 

// End form field validation



