

function addOption(parent,child) {
	try {parent.options.add(child)}
	catch(e) {parent.appendChild(child)}
}

function getProductsByMan(man) {
	
	var out = new Array()
	
	if(!man) {
		out = products
	}
	else {
		for(var i=0;i<products.length;i++) {
			if(products[i].primary_man==man) {out.push(products[i])}
		}
	}
	
	return out
}
															

function changeProductOptions() {
	
	if(document.docSearch) {
		if(document.docSearch.man&&document.docSearch.product) {
			
			var man = document.docSearch.man
			var product = document.docSearch.product
			
			var currentProductValue = product.options[product.selectedIndex].value
			var selectOption = 0
			
			for(var i=(product.options.length-1);i>0;i--) {
				product.remove(i)
			}
			
			var prods = getProductsByMan(man.options[man.selectedIndex].value)
			
			var newElement
			
			for(var i=0;i<prods.length;i++) {
				
				newElement = document.createElement("option")
				newElement.text = prods[i].full_title
				newElement.value = prods[i].productID
				if(prods[i].productID==currentProductValue) {selectOption = i+1}
				
				addOption(product,newElement)
			}
			
			product.selectedIndex = selectOption
		
			checkValidForSubmit()
		}
		
	}
}


function checkValidForSubmit() {
	if(document.docSearch) {
		if(document.docSearch.product) {
			
			
			var product = document.docSearch.product
			
			if(product.value) {
			
				document.getElementById("search_submit").disabled=false
			}
			
			
			else {
				document.getElementById("search_submit").disabled=true
			}
		}
	}
	
	return document.getElementById("search_submit").disabled
}


function doSubmit() {
	if(checkValidForSubmit()) {
		alert("Please select a product first")
	}
}
			
loadFunctions.push(changeProductOptions)		