///////////////////////////////
//           Shop            //
///////////////////////////////

function changeProductOption(fld, frm, hasDisablingOptionItems){
	// build your own - first option has disablingOptionItems
	if (hasDisablingOptionItems){
		var productId = document.getElementById('ProductId').value;
		// get options that are not dependant and set to disabled.
		DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getDisabledOptions', productId, fld.value, resultDisabledOptions);
	} else {
		getPackagePrice(frm);		
	}

	function resultDisabledOptions(r) {
		var optionIdArray = r.productoptionid;
		var disabledArray = r.disabled;
		for (var i = 0; i < optionIdArray.length; i++) {
			var objField = document.getElementById('Option_' + optionIdArray[i]);
			if (disabledArray[i] == 1) {
				objField.selectedIndex = 0;
				objField.disabled = true;
			} else  {
				objField.disabled = false;
			}
		}
		// after all options changed get new package price.
		getPackagePrice(frm);		
	}
}


function getPackagePrice(frm){
	var objForm = document.getElementById(frm);
	
	if(ValidateField(objForm.Quantity,true,"quantity","Please enter a vaild quantity.")){			
		DWRUtil.useLoadingMessage();
		DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getPackagePrice', DWRUtil.serializeForm(objForm), resultPackagePrice);
	}
	
	function resultPackagePrice(r) {
		var objPackagePrice = document.getElementById('PackagePrice');
		objPackagePrice.innerHTML = r;
	}
}

///////////////////////////////
//          Shipping         //
///////////////////////////////

function getStates(fld,addressType){
	var countryId = document.getElementById(fld).value;
	DWREngine.setAsync(false);
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getStates', countryId, resultStates);
	
	function resultStates(r) {
		
		StateName = document.getElementById(addressType+'State');
		StateId = document.getElementById(addressType+'StateId');
		
		if(r.getRowCount() > 0){
			StateName.value = "";
			StateName.style.display = "none";
			StateId.style.display = "";
			var getText = function (thisRow) { return thisRow.name; }
			var getValue = function (thisRow) { return thisRow.id; }
			DWRUtil.removeAllOptions(StateId);
			DWRUtil.addOptions(StateId, [{ name:'Please Select', value:'0' }], "value", "name"); 
			DWRUtil.addOptions(StateId, r, [getValue, getText], null);
		}
		else {
			DWRUtil.removeAllOptions(StateId);
			StateName.value = "";
			StateName.style.display = "";
			StateId.style.display = "none";
		}
	}
}
