/* 
	Function used with a combination of PogoTextField's inner error display
	and the custom JSForm template.
 */
function dsp_inline_error(textfield) {
	formname = textfield.form.name;
	fieldname = textfield.name
	fieldId = "error" + fieldname;
	if (document.getElementById(fieldId) != null) {
		fnToCall = "validate_" + formname + "_" + fieldname + "()";
		errormsg = eval(fnToCall);
		if (errormsg == null) {
			errormsg = "|";
		}
		errormsg = errormsg.split("|", 1);
		if (errormsg != '') {
			document.getElementById(fieldId).innerHTML = errormsg;
			document.getElementById(fieldId).style.display = "block";
		} else {
			document.getElementById(fieldId).innerHTML = errormsg;
		}

	}

}

// get the name of the page
var sPath = window.location.pathname;
var sPage = URLDecode(sPath.substring(sPath.lastIndexOf('/') + 1));

// all the valid pages in the reference library menu
var indexPages = new Array();
indexPages[0] = "Nutrient Profiles";
indexPages[1] = "Health Conditions";
indexPages[2] = "Drug Interactions";
indexPages[3] = "Popular Topics";
indexPages[4] = "labelAss";
indexPages[5] = "chooseAProd";
// set the cookie for the right page in the reference library
if (indexPages.find(sPage)) {
	// set a cookie for the selected page
	var cookieName = sPage.replace(' ', '_');
	setCookie("selectedSection", cookieName, 1);
} else if (sPage == 'index.htm') {
	// now for the outlier pages in the consumer portal that are part of the ref
	// lib
	(function($) {
		setCookie("selectedSection", $(document).getUrlParam("sType"), 1);
	})(jQuery);
}
// validateDSForm
var productUpcs = [];
var productDescs = [];
function validateDSForm() {
	var aIndex = 0;
//	productUpcs[0]=" ";
//	productDescs[0]=" ";
	var aForm = document.getElementById("shoppingCartForm");

	for ( var i = 0; i < aForm.elements.length; i++) {
		var element = aForm.elements[i];
		if (element.checked) {
			productUpcs[aIndex] = element.id;
			productDescs[aIndex] = element.name;
			aIndex++;
		}
	}
	// make sure items are selected
	if(aIndex < 1) {
		alert("Please check off those products you'd like to add to your shopping cart.");
		return false;
	}
	
	processDSForm();
	
	// temporary message
//	alert("Upcs to send: " + productUpcs);
//	alert("Product descriptions:" + productDescs);
	
	return true;
	
}

function processDSForm() {
	if (document.getElementById('DSForm')) {
		var dsform = document.getElementById('DSForm')

		var appendChild = false;
		// remove items if present
		var hdnProdList = document.getElementById('hdnProdList');
		if (!hdnProdList) {
			hdnProdList = document.createElement("input");
			appendChild = true;
		}
		hdnProdList.name = "hdnProdList";
		hdnProdList.type = "hidden";
		hdnProdList.value = productUpcs;
		if (appendChild)
			dsform.appendChild(hdnProdList);

		appendChild = false;
		var hdnProdDescList = document.getElementById('hdnProdDescList')
		if (!hdnProdDescList) {
			hdnProdDescList = document.createElement("input");
			appendChild = true;
		}
		hdnProdDescList.name = "hdnProdDescList";
		hdnProdDescList.type = "hidden";
		hdnProdDescList.value = productDescs;
		if (appendChild == true)
			dsform.appendChild(hdnProdDescList);

		appendChild = false;
		var hdnQuantList = document.getElementById('hdnQuantList')
		if (!hdnQuantList) {
			hdnQuantList = document.createElement("input");
			appendChild = true;
		}
		// build quantity list
		var x = [];
		for ( var i = 0; i < productUpcs.length; i++) {
			x[i] = 1;
		}
		hdnQuantList.name = "hdnQuantList";
		hdnQuantList.type = "hidden";

		hdnQuantList.value = x;
		if (appendChild == true)
			dsform.appendChild(hdnQuantList);
	}
	// build form

}
