/*
 JavaScript Functions for validating the form entries and for submitting all of the fields.
*/

// function to add elements in the <DIV> line
// this is not currently used....
function addElement() {
  var ni = document.getElementById('myOptions');
  var numi = document.getElementById('optionproduct1');
  var num = (document.getElementById('optionproduct1').value -1)+ 2;
  numi.value = num;
  var newdiv = document.createElement('div');
  var divIdName = 'my'+num+'Div';
  newdiv.setAttribute('id',divIdName);
  newdiv.innerHTML = 'Element Number '+num+' has been added! <a href=\'#\' onclick=\'removeElement('+divIdName+')\'>Remove the div "'+divIdName+'"</a>';
  ni.appendChild(newdiv);
}

function calcPrice(Obj,optioncat,no_option_groups,base_product_price, VATrate, base_netprice)
{
	  var option = eval('document.ProductOptions.option' + optioncat);
	  var netoption = eval('document.ProductOptions.optionnetprice' + optioncat);
	  // var optionprice = eval('document.options.optionprice' + optioncat + '.value');
	  var optionprice = Obj.value;
	  var index = option.selectedIndex;
	  var product_id = option.value;
	  // alert ( "OptionCat: " +optioncat+", Selected Index: " + index + " ; Product ID: " + product_id + " ; Existing Option Price: " + optionprice );

	  var selectedproduct = eval('document.ProductOptions.optionselected' + optioncat);

	  // if there is a valid Product_ID selected from the drop-down box
	  if (product_id > 0)
	  {
	  	  // read the cost of the option from specifically created Form fields "pid<product_id>"
		  var optioncost = eval('document.ProductOptions.pid'+product_id+'.value');
		  var optionnetcost = eval('document.ProductOptions.pidnet'+product_id+'.value');
		  // alert ( "New Cost: " + optioncost + " NET: " + optionnetcost);

		  // set the value to Product Options Form, first what option has been selected
		  selectedproduct.value = product_id;
		  // and then set the option cost
  		  Obj.value = optioncost;
		  netoption.value = optionnetcost;
			
		  // then also update the relevant AddToCart Form field with the selected product_id
		  // this is used when buy button is pressed to transmit the selected options to cart_add_option.php
		  // ideally should use field names if but could not get it working !!! others changes to product_option.php might 
		  // make the system to fail
		  var myForm = document.forms.AddToCart;
		  var formElements = myForm.elements;
		  var i = 7 + optioncat; 
		  formElements[i].value = product_id;
		  
		  // set the value to AddtoCart Form
		 // var all = formElements.length;
		 // for( var i = 2; i < all; i++ ) {
		//	alert( myForm.optionproduct1.value );
		//	alert( formElements[i].value );
			//	formElements[i].value = 20000;
		//  }
	  }
	  // if "None" as product option has been selected
	  // then we set Product_ID to 0
	  else { Obj.value = 0; 
	  		 selectedproduct.value = 0;
			 netoption.value = 0;
			 product_id = 0; 
			 
			 var myForm = document.forms.AddToCart;
			 var formElements = myForm.elements;
			 var i = 7 + optioncat; 
			 formElements[i].value = product_id;
	   }
	  
	  // let's sum up all option costs
	  //alert ( "No of Groups: " + no_option_groups);
	  var option_group = 0;
	  var totalcost = base_product_price;
	  var totalnetcost = base_netprice;
	  var VATcost = 0;
	  for (i=0; i < no_option_groups; i++)
	  {
		  option_group = i+1;
		  optioncost = eval('document.ProductOptions.optionprice'+ option_group+'.value');
		  optionnetcost = eval('document.ProductOptions.optionnetprice'+ option_group+'.value');
		  optioncost = parseFloat(optioncost);
		  optionnetcost = parseFloat(optionnetcost);
		  // alert ('OPTION NET: ' +  optionnetcost);
		  totalcost = totalcost + optioncost;
		  totalnetcost = totalnetcost + optionnetcost;
		  // alert ( "Index: " +  option_group + " : optioncost " + optioncost );
		  // alert ('OPTION TOTAL NET: ' +  totalnetcost);
	  }
	  
	  totalcost =  totalcost.toFixed(2); 
	  VATcost = totalcost * (1 + VATrate);
	  VATcost = VATcost.toFixed(2);
	  VATcost = "£" + VATcost + " inc VAT";
	  totalnetcost =  "  NET PRICE: £" +  totalnetcost.toFixed(2) + " + VAT"; ; 
	  document.AddToCart.calcprice.value = totalcost;
	  document.AddToCart.vatprice.value = VATcost;
  	  document.AddToCart.calcnetprice.value = totalnetcost;
}

function addForms(i,optionproductid)
{
				//Create an inputs type dynamically.  
				var element = document.createElement("input");  
				var name = "optionproduct" + i;
				//Assign different attributes to the element.  
				element.setAttribute("type", "hidden");  
				element.setAttribute("value", optionproductid);  
				element.setAttribute("name", name);  
				var dynField = document.getElementById("dynFields");  
				//Append the element in page (in span).  
				dynField.appendChild(element);  
}
	
function startValues(no_option_groups,VATrate,totalcost, totalnetprice )
{
	
	// alert ( totalnetprice ); 
	// Let's calculate the inital summed cost of all components	
	  for (i=0; i < no_option_groups; i++)
	  	{
		  option_group = i+1;
		  optioncost = eval('document.ProductOptions.optionprice'+ option_group+'.value');
		  optionnetcost = eval('document.ProductOptions.optionnetprice'+ option_group+'.value');
		  optioncost = parseFloat(optioncost);
		  optionnetcost = parseFloat(optionnetcost);
		  totalcost = totalcost + optioncost;
		  totalnetprice = totalnetprice + optionnetcost;
		  //alert ( "Index: " +  option_group + " : optioncost " + optioncost );
		
		 // add to AddtoCart FORM new fields based on the dynamically created one
		 // these are needed for submit function, these fields need only contain Option Product_ID
		 optionproductid = eval('document.ProductOptions.optionselected'+ option_group+'.value');
		 addForms(option_group,optionproductid);
	    }
	  
	  totalcost =  totalcost.toFixed(2); 
	  totalnetprice =  "  NET PRICE: £" +  totalnetprice.toFixed(2) + " + VAT"; ; 
	  VATcost = totalcost * (1 + VATrate);
  	  VATcost = VATcost.toFixed(2);
	  VATcost = "£" + VATcost + " inc VAT";
	  document.AddToCart.calcprice.value = totalcost;
	  document.AddToCart.vatprice.value = VATcost;
	  document.AddToCart.calcnetprice.value = totalnetprice;

}



