// JavaScript Document
function roundNumber(rnum) {
	var rlength = 0; // The number of decimal places to round to
	if (rnum > 8191 && rnum < 10485) {
		rnum = rnum-5000;
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
		newnumber = newnumber+5000;
	} else {
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
	}
	return newnumber;
}

function changePrice(frmobj,prod_id){
	var selIndex;
	var perincrease;
	var newprice;
	var totaladditional = 0;
	var additionalcost = new Array();
	var additionalcostname = new Array();	
	var optionname;
	var baseprice = eval('document.frm_product_' + prod_id + '.baseprice.value');
	var itemtotalobj = eval('document.frm_product_' + prod_id + '.itemtotal');
	baseprice = baseprice * 1;
	newprice = baseprice;
	//additionalcost = 0;
	x =0;

   for(i=0; i<frmobj.elements.length; i++){	
   		if(frmobj.elements[i].type == "select-one"){
			if (frmobj.elements[i].name != 'qty') {
				addcost = 0;
				optionname = frmobj.elements[i].name;
				selIndex = frmobj.elements[i].selectedIndex;
				strgetarrelement = optionname + 'arr_' + prod_id + '[' + selIndex + ']';
				perincrease = eval(strgetarrelement);
				perincrease = perincrease / 100;
				addcost = (baseprice * perincrease);
				if (addcost != 0) {
					additionalcost[x] = addcost;
					additionalcostname[x] = optionname;
					x = x+1;
				}
			}
		}
   }

   for(i=0; i<additionalcost.length; i++){	
		totaladditional = totaladditional + additionalcost[i]
	 	if(admin) {
			alert('The ' + additionalcostname[i] + ' selection added ' + additionalcost[i]);			
		}
   }

 	if(admin) {
		alert('bringing the total additional costs to:' + totaladditional);
	}
   
   
	newprice = (baseprice + totaladditional);

	newprice = roundNumber(newprice);
	newprice = newprice.toString();
	is_dot_ok=newprice.indexOf('.');
	if (is_dot_ok==-1){
		newprice = newprice + '.00';
	}
	price_element = 'price_' + prod_id;
	//evalstr = 'document.getElementById(\'' + price_element + '\')';
	evalstr = 'getElementById_s(\'' + price_element + '\')';
	priceobj = eval(evalstr);
	if(admin) {
		alert('The base price: ' + baseprice + ' plus the total additional costs: ' + totaladditional + ' equal=' + newprice);
	}
	priceobj.innerHTML  = '$' + newprice;
	itemtotalobj.value = newprice;	
}	

// getElementById Special to handle quirky browsers
// most will use getElementById()
function getElementById_s(id){
var obj = null;
if(document.getElementById){
/* Prefer the widely supported W3C DOM method, if
available:-
*/
obj = document.getElementById(id);
}else if(document.all){
/* Branch to use document.all on document.all only
browsers. Requires that IDs are unique to the page
and do not coincide with NAME attributes on other
elements:-
*/
obj = document.all[id];
}
/* If no appropriate element retrieval mechanism exists on
this browser this function always returns null:-
*/
return obj;
}

function changePrice_old(frmobj,prod_id){
alert('this is the changePrice');
	var selIndex;
	var perincrease;
	var newprice;
	var totaladditional = 0;
	var additionalcost = new Array();
	var additionalcostname = new Array();	
	var optionname;
	var baseprice = eval('document.frm_product_' + prod_id + '.baseprice.value');
	var itemtotalobj = eval('document.frm_product_' + prod_id + '.itemtotal');
	baseprice = baseprice * 1;
	newprice = baseprice;
	//additionalcost = 0;
	x =0;

   for(i=0; i<frmobj.elements.length; i++){	
   		if(frmobj.elements[i].type == "select-one"){
			if (frmobj.elements[i].name != 'qty') {
				addcost = 0;
				optionname = frmobj.elements[i].name;
				selIndex = frmobj.elements[i].selectedIndex;
				strgetarrelement = optionname + 'arr_' + prod_id + '[' + selIndex + ']';
				perincrease = eval(strgetarrelement);
				perincrease = perincrease / 100;
				addcost = (baseprice * perincrease);
				if (addcost != 0) {
					additionalcost[x] = addcost;
					additionalcostname[x] = optionname;
					x = x+1;
				}
			}
		}
   }

   for(i=0; i<additionalcost.length; i++){	
		totaladditional = totaladditional + additionalcost[i]
	 	if(admin) {
			alert('The ' + additionalcostname[i] + ' selection added ' + additionalcost[i]);			
		}
   }

 	if(admin) {
		alert('bringing the total additional costs to:' + totaladditional);
	}
   
   
	newprice = (baseprice + totaladditional);

	newprice = roundNumber(newprice);
	newprice = newprice.toString();
	is_dot_ok=newprice.indexOf('.');
	if (is_dot_ok==-1){
		newprice = newprice + '.00';
	}
	price_element = 'price_' + prod_id;
	evalstr = 'document.getElementById(\'' + price_element + '\')';
	priceobj = eval(evalstr);
	if(admin) {
		alert('The base price: ' + baseprice + ' plus the total additional costs: ' + totaladditional + ' equal=' + newprice);
	}
	priceobj.innerText = '$' + newprice;
	itemtotalobj.value = newprice;	
	alert(itemtotalobj.value);
}	

function elmLoop(){

var theForm = document.forms[0]

   for(i=0; i<theForm.elements.length; i++){
   var alertText = ""
   alertText += "Element Type: " + theForm.elements[i].type + "\n"

      if(theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea" || theForm.elements[i].type == "button"){
      alertText += "Element Value: " + theForm.elements[i].value + "\n"
      }
      else if(theForm.elements[i].type == "checkbox"){
      alertText += "Element Checked? " + theForm.elements[i].checked + "\n"
      }
      else if(theForm.elements[i].type == "select-one"){
      alertText += "Selected Option's Text: " + theForm.elements[i].options[theForm.elements[i].selectedIndex].text + "\n"
      }
   alert(alertText)
   }

} 
