// JavaScript Document
function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result.toFixed(2);
}

function addProductToCart(productID,node){
	document.getElementById('product_cartStatus').innerHTML = 'Adding Product...';
	document.getElementById('addToCartButton').disabled = 1;

	if(document.getElementById('shopMiniCart')){
		var buffer = document.getElementById('shopMiniCart').innerHTML;
		document.getElementById('shopMiniCart').innerHTML = "<div style='filter:alpha(opacity=50); -moz-opacity:0.5; -khtml-opacity: 0.5; opacity: 0.5;'>"+buffer+"</div>";
	}


	var productAttributeID;
	var attributePostVars = '';
	//this wee for in loop is going to go through each of the attributes that are assigned to the product we want to add to
	//our cart, it gets the product attribute id from this
	for(productAttributeID in shop_product_attributes){
		
		//alert(productAttributeID+" - - "+shop_product_attributes[productAttributeID]);
		if(!isNaN(productAttributeID)){
			if(attributeField = document.getElementById('attribute_'+productAttributeID)){
				//basicly if theres a simple form field thats named then we can get the value from that
				attributePostVars += "&productAttribute"+productAttributeID+"="+attributeField.value;
			}else{
				//else we have some options to itterate through and get the values from that.
				var optionID;
				var attributePostVarOptions='';
				for(optionID in shop_product_attributes[productAttributeID]){
					if(!isNaN(optionID)){
						if(document.getElementById('attribute_'+productAttributeID+'_'+optionID).checked == 1){
							attributePostVarOptions += optionID+",";
						}
					}
				}
				var varLen = attributePostVarOptions.length;
				varLen = varLen - 1;
				attributePostVarOptions = attributePostVarOptions.substr(0,varLen);
				attributePostVars += "&productAttribute"+productAttributeID+"="+attributePostVarOptions;
			}
		}
		
	}
	
	
	var add = "";
	var refreshCart = "";
	var quantity = document.getElementById('addToCartQuantity').value > 0 ? document.getElementById('addToCartQuantity').value : 1;
	quantity = Math.floor(quantity);
	
	if(add = new executeAjax('components/com_shop/ajax/cart.php?action=addProductToCart&node='+node+'&productID='+productID+'&quantity='+quantity+attributePostVars,'product_cartStatus')){
		document.getElementById('addToCartButton').value = 'Add to Cart';
		document.getElementById('addToCartButton').disabled = 0;
	}
	
	if(document.getElementById('shopMiniCart')){
		setTimeout("refreshCart = new executeAjax('components/com_shop_cart/ajax.php?action=test','shopMiniCart')",1000);
		//document.getElementById('product_cartStatus').innerHTML = "";
		//refreshCart = new executeAjax('components/com_shop_cart/ajax.php?action=test','shopMiniCart');
	}
	
	//return false because we have added the product using ajaxual shiznik so theres no need to submit the form now traditional styles
	return false;
}



function updateAttributeField(optionID, productAttributeID){
	
	//here we go through a small loop that resets the values to 0 for the attributes
	//that dont have a value set to it cos then it wont revert back
	var fieldID;
	for(fieldID in customFieldIDs){
		if(!isNaN(fieldID)){
			if(document.getElementById(productAttributeID+"_"+fieldID)){
				document.getElementById(productAttributeID+"_"+fieldID).value='0';
				updateField(fieldID);
			}
		}
	}
	
	if(customFields[productAttributeID] != undefined){
		var fields = customFields[productAttributeID][optionID];
		var fieldID;
		for(fieldID in fields){
			if(!isNaN(fieldID)){
				if(document.getElementById(productAttributeID+"_"+fieldID)){
					document.getElementById(productAttributeID+"_"+fieldID).value=fields[fieldID];
					updateField(fieldID);
				}
			}
		}
	}
}

function updateAttributePrice(optionID, productAttributeID){
	//call the update attributeField function to go through and get the custom field values for
	//this attribute
	updateAttributeField(optionID, productAttributeID);
	
	//array Key is the option ID which is the value in the dropdown or form field
	document.getElementById('price_'+productAttributeID).value = shop_product_attributes[productAttributeID][optionID];
	if(document.getElementById('product_price')){
		updatePrice();
	}
}

function updateAttributeManyPrice(productAttributeID){
	updateAttributeManyField(productAttributeID);
	//call the update attributeField function to go through and get the custom field values for
	//this attribute
	var optionID;
	var price = 0;
	var total = 0;
	for(optionID in shop_product_attributes[productAttributeID]){
		if(!isNaN(optionID)){
			if(document.getElementById('attribute_'+productAttributeID+'_'+optionID).checked == 1){
				total = total + parseInt(shop_product_attributes[productAttributeID][optionID]);
			}
		}
	}
	//array Key is the option ID which is the value in the dropdown or form field
	document.getElementById('price_'+productAttributeID).value = total;
	if(document.getElementById('product_price')){
		updatePrice();
	}
}

function updateAttributeManyField(productAttributeID){
	//here we go through a small loop that resets the values to 0 for the attributes
	//that dont have a value set to it cos then it wont revert back
	var fieldID;
	for(fieldID in customFieldIDs){
		if(!isNaN(fieldID)){
			document.getElementById(productAttributeID+"_"+fieldID).value='0';
			updateField(fieldID);
		}
	}
	
	if(customFields[productAttributeID] != undefined){
		var optionID;
		
		for(fieldID in customFieldIDs){
			if(!isNaN(fieldID)){
				var total = 0;
				for(optionID in shop_product_attributes[productAttributeID]){
					if(!isNaN(optionID)){
						if(customFields[productAttributeID][optionID] != undefined){
							var fields = customFields[productAttributeID][optionID];
							if(document.getElementById('attribute_'+productAttributeID+'_'+optionID).checked == 1){
								
								if(isNaN(parseInt(fields[fieldID]))){
									total = total + 0;
								}else{
									total = total + parseInt(fields[fieldID]);
								}
								
								document.getElementById(productAttributeID+"_"+fieldID).value=total;
								updateField(fieldID);
							}
						}
					}//end of if not is not a numb
				}//end of for
			}
		}//end of for

	}
}

//this is a one off function that sets the value for the extras price to 0
function setAttributeManyPrice(productAttributeID){
	document.getElementById('price_'+productAttributeID).value = 0;
	if(document.getElementById('product_price')){
		updatePrice();
	}
}

//update field takes a fieldID and then goes through each attribute, gets the value that it may have
//assigned to that field and adds them all up as well as the initial value, then it displays the total
function updateField(fieldID){

	var total = 0;
	var fieldValue = 0;
	var productAttributeID = 0;
	var initialValue = parseFloat(document.getElementById('field_'+fieldID).value);
	
	//fore each product attribute get the value of this field, add it to the total
	for(productAttributeID in shop_product_attributes){
		if(!isNaN(productAttributeID)){
			if(document.getElementById(productAttributeID+"_"+fieldID)){
				fieldValue = parseFloat(document.getElementById(productAttributeID+"_"+fieldID).value);
				total = total + fieldValue;
			}
		}
	}
	
	//add the initial value to the total and then show that in the display field
	total = total + initialValue;
	
	document.getElementById('fieldDisplay_'+fieldID).innerHTML = total;
}

//this is the funtion that goes through each of the atrributes and then adds up the price for each one
function updatePrice(){	
	var attribute;
	var attributePrice;
	var total = 0;
	var price = parseFloat(document.getElementById('product_price').value);	
	if(document.getElementById('product_taxRate') && document.getElementById('product_currency')){
		var taxRate = parseFloat(document.getElementById('product_taxRate').value);
		var currencyRate = parseFloat(document.getElementById('product_currency').value);
	}
	
	for(attribute in shop_product_attributes){
		if(!isNaN(attribute)){
			if(document.getElementById('price_'+attribute)){
				attributePrice = document.getElementById('price_'+attribute).value;
			}
			attributePrice = attributePrice == '' ? 0 : attributePrice;
			total = total + parseFloat(attributePrice);
		}
	}

	var newPrice = price + total;
	newPrice = newPrice + (newPrice * taxRate);
	newPrice = newPrice * currencyRate;
	
	if(document.getElementById('product_price_display')){
		document.getElementById('product_price_display').innerHTML = roundNumber(newPrice,2);
	}
	
	//if there is a old price field than update that with the old price
	if(document.getElementById('product_price_old')){
		var oldPrice = parseFloat(document.getElementById('product_price_old').value);
		var newOldPrice = oldPrice + total;
		newOldPrice = newOldPrice + (newOldPrice * taxRate);
		newOldPrice = newOldPrice * currencyRate;
		document.getElementById('prod_detail_old_display').innerHTML = roundNumber(newOldPrice,2);
	}
}