function changeProductDescriptor(desID){
	
	var theDivs = $(".divProductDescriptor");
	var theDiv = $("#divProductDescriptor_"+desID);
	var theLinks = $(".aProductDescriptor");
	var theLink = $("#aProductDescriptor_"+desID);
	
	theDivs.hide();
	theDiv.show();
	theLinks.removeClass("pd_on");
	theLink.addClass("pd_on");
	
}

function toggleReviewForm(){
	
	var theDiv = $("#divWriteReview");
	
	theDiv.slideToggle();
	
}

function addToBasket(pr_id){

	toggleBuyButtonEnabled();

	var theForm = $("#frmProduct");
	var theQuantity = $("#fmQuantity");
	
	if (!theQuantity.length){
		addToBasketError("Error: Product is out of stock");
		toggleBuyButtonEnabled();
		return false;
	}
	
	if (theQuantity.is(":disabled")){
		addToBasketError("Error: Please make a selection from the options");
		toggleBuyButtonEnabled();
		return false;
	}
	
	if (!parseInt(theQuantity.val())){
		addToBasketError("Error: Please enter a quantity");
		toggleBuyButtonEnabled();
		return false;
	}
	
	for (i=0;i<mandatory_option_dropdowns.length;i++){
		var theDropdown = $("#fmOption_"+mandatory_option_dropdowns[i]);
		if (theDropdown.val() == "0"){
			addToBasketError("Error: Please complete the '"+mandatory_option_dropdown_labels[i]+"' option");
			toggleBuyButtonEnabled();
			return false;
		}
	}
	
	for (i=0;i<mandatory_option_text.length;i++){
		var theText = $("#fmOption_"+mandatory_option_text[i]);
		if (theText.val() == theText.attr("title") || theText.val() == ""){
			addToBasketError("Error: Please complete the '"+mandatory_option_text_labels[i]+"' option");
			toggleBuyButtonEnabled();
			return false;
		}
	}
	
	theForm.submit();
	
}

function addToBasketError(msg){
	
	var spnError = $("#spnAddToBasketError");
	
	spnError.html(msg);
	spnError.fadeIn();
	
	t = setTimeout('$("#spnAddToBasketError").fadeOut();',5000);
	
}

function updateInventoryOption(igNum){
	if (igNum == numInventoryGroups)
		updateProductQuantity();
	else
		fillInventoryOptions(igNum + 1);
}

function fillInventoryOptions(igNum){

	toggleLoadingImage('on');
	toggleBuyButtonEnabled();
	
	var postVars = '';
	var theTD = $("#tdInventory_"+igNum);
	
	for (i=1;i<igNum;i++)
		postVars += 'fmGroup_'+i+'='+$("#fmInventory_"+i).val()+'&';
		
	ajax = create_ajax();
	
	ajax.onreadystatechange=function(){
		if (ajax.readyState==4 && ajax.status==200){
			response = ajax.responseText;
			theTD.html(response);
			disableChildInventoryOptions(igNum);
			toggleBuyButtonEnabled();
			updateProductQuantity();
			//toggleLoadingImage('off');
		}
	}
	
	run_ajax(ajax,"POST","/_ajax/fillInventoryOptions.php",postVars);
	
}

function updateProductQuantity(){
	
	toggleLoadingImage('on');
	toggleBuyButtonEnabled();
	
	var postVars = '';
	var theTD = $("#tdQuantity");

	for (i=1;i<=numInventoryGroups;i++)
		postVars += 'fmGroup_'+i+'='+$("#fmInventory_"+i).val()+'&';
	
	ajax = create_ajax();
	
	ajax.onreadystatechange=function(){
		if (ajax.readyState==4 && ajax.status==200){
			response = ajax.responseText;
			theTD.html(response);
			toggleLoadingImage('off');
			toggleBuyButtonEnabled();
		}
	}
	
	run_ajax(ajax,"POST","/_ajax/updateProductQuantity.php",postVars);
	
}

function disableChildInventoryOptions(igNum){
	for (i=(igNum+1);i<=numInventoryGroups;i++){
		var theSelect = $("#fmInventory_"+i);
		theSelect.children().remove();
		theSelect.append('<option value="0">Select...</option>');
		theSelect.attr("disabled","disabled");
	}
}

function toggleLoadingImage(onoroff){
	var theImg = $("#imgProductLoading");
	switch (onoroff){
		case "on":
			theImg.show();
			break;
		case "off":
			theImg.hide();
			break;
	}
}

function toggleBuyButtonEnabled(){
	var theButton = $("#butAddToBasket");
	if (theButton.is(":disabled"))
		theButton.attr("disabled","");
	else
		theButton.attr("disabled","disabled");
}
