function disp_text()
{
	var w, selected_text, tmp1, tmp2, price_show, price, att="", fos, id_tmp, id_tmp_2;
  var TagElements = document.getElementsByTagName("select");
  var fos = document.getElementById('fos').value;

	for (j = 0; j < TagElements.length; j++) 
	{
 	  w = TagElements[j].selectedIndex;
	  selected_text = TagElements[j].options[w].text;
	  selected_value = TagElements[j].options[w].value;

		var id_tmp = TagElements[j].id.split("id[");
		var id_tmp_2 = id_tmp[1].split("]");
		var id = id_tmp_2[0];
		
		att = att +""+ id + ":" + selected_value + "|";

  	var tmp1 = selected_text.split("+$");
	
		if (tmp1[1])
		{
			var tmp2 = tmp1[1].split(")");
			price = tmp2[0];
		}
	}
	var product_id = document.getElementById('product_id').value;
	if (fos == "yes")
	{
		loadXMLDoc("js/change_price_whole.php?att=" + att + "&product_id=" + product_id, ajaxCallback_whole);
	}
	else
	{
		loadXMLDoc("js/change_price.php?att=" + att + "&product_id=" + product_id, ajaxCallback);
	}
 
}

function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}

var req = false;

function loadXMLDoc(url, funcProcess) {
   req = false;
   if(window.XMLHttpRequest) {
      try {
         req = new XMLHttpRequest();
      } catch(e) {
         req = false;
      }
   } else if(window.ActiveXObject) {
      try {
         req = new ActiveXObject('Msxml2.XMLHTTP');
      } catch(e) {
         try {
            req = new ActiveXObject('Microsoft.XMLHTTP');
         } catch(e) {
            req = false;
         }
      }
   }
   if(req) {
      req.onreadystatechange = funcProcess;
      req.open('GET', url, true);
      req.send(null);
   }
}

function ajaxCallback()
 {
	 if(req.readyState == 0) { document.getElementById('price').innerHTML = "Price Sending Request ..."; } 
	 if(req.readyState == 1) { document.getElementById('price').innerHTML = "Price Loading ..."; } 
	 if(req.readyState == 2) { document.getElementById('price').innerHTML = "Price Loading ..."; } 
	 if(req.readyState == 3) { document.getElementById('price').innerHTML = "Price Loading ..."; } 
   if (req.readyState == 4)
   {
      if (req.status == 200)
      {
         if (req.responseText.length)
         {
            if (req.responseText != '1')
            {
							//document.getElementById('price').innerHTML = req.responseText;
							var quantity=document.getElementById('cart_quantity_a').value;
							//document.getElementById('price').innerHTML = 'Price '+formatCurrency((req.responseText*1) * (quantity*1));
							var sel_1=document.getElementById("id[2]");
							var sel_2=document.getElementById("id[3]");
							var test = document.getElementById("id[3]").options;
							sel_1.length = 0;
							sel_2.length = 0;

							//removeOldSchool(sel_1);
							//removeOldSchool(sel_2);
							//ClearOptions(document.forms['cart_quantity'].elements['mario']);
							//ClearOptions(document.cart_quantity.id[3]);
							var tmp=req.responseText.split("~");
							for (j = 0; j < tmp.length; j++) 
							{
								if(j>0)
								{
									var idsel=tmp[j].split(":");//idu selectului idsel[0]
									var opt_val = idsel[1].split("=");//separ idurile de optiuni
									var id = opt_val[0].split("|");
									var opt = opt_val[1].split("|");
									for (i = 0; i < id.length-1; i++)
									{
										if (id[i] != null)
										{
											document.getElementById("id["+idsel[0]+"]").options[i] = new Option(opt[i], id[i]);
										}
									}
									
								}
							}
							sel_1.options[0].selected="selected";
							sel_2.options[0].selected="selected";
							var final_price = formatCurrency((tmp[0]*1)*(quantity*1));
							document.getElementById('price').innerHTML = "Price  " + final_price;
            }
         }
      }
      else
      {
         alert('There was a problem retrieving the XML data:n' + req.statusText);
      }
   }
	 
}
function ajaxCallback_whole()
 {
   if (req.readyState == 4)
   {
      if (req.status == 200)
      {
         if (req.responseText.length)
         {
            if (req.responseText != '1')
            {
							var quantity=document.getElementById('cart_quantity_a').value;
							//document.getElementById('price').innerHTML = 'Price '+formatCurrency((req.responseText*1) * (quantity*1));
							var sel_1=document.getElementById("id[2]");
							var sel_2=document.getElementById("id[3]");
							var test = document.getElementById("id[3]").options;
							sel_1.length = 0;
							sel_2.length = 0;

							//removeOldSchool(sel_1);
							//removeOldSchool(sel_2);
							//ClearOptions(document.forms['cart_quantity'].elements['mario']);
							//ClearOptions(document.cart_quantity.id[3]);
							var tmp=req.responseText.split("~");
							for (j = 0; j < tmp.length; j++) 
							{
								if(j>0)
								{
									var idsel=tmp[j].split(":");//idu selectului idsel[0]
									var opt_val = idsel[1].split("=");//separ idurile de optiuni
									var id = opt_val[0].split("|");
									var opt = opt_val[1].split("|");
									for (i = 0; i < id.length-1; i++)
									{
										if (id[i] != null)
										{
											document.getElementById("id["+idsel[0]+"]").options[i] = new Option(opt[i], id[i]);
										}
									}
									
								}
							}
							
							document.getElementById('price').innerHTML = "Price  " + formatCurrency((tmp[0]*1)*(quantity*1));
            }
         }
      }
      else
      {
         alert('There was a problem retrieving the XML data:n' + req.statusText);
      }
   }
	 
}

function removeOldSchool(theSel)
{
  var selIndex = theSel.selectedIndex;
  if (selIndex != -1) 
	{
    for(i=theSel.length-1; i>=0; i--)
    {
//      if(!theSel.options[i].selected)
//      {
//        theSel.options[i] = null;
//      }
			theSel.options[i] = null;
    }
  }
	disp_text();
}

function optionValueIndexOf(options,value) 
{
	for (var i=0;i<options.length;i++) 
	{
		if (options[i].value == value) 
		{
			return i;
		}
	}
	return -1;
}





function ClearOptions(OptionList) {

   // Always clear an option list from the last entry to the first
   for (x = OptionList.length; x >= 0; x = x - 1) {
      OptionList[x] = null;
   }
}


function AddToOptionList(OptionList, OptionValue, OptionText) {
   // Add option to the bottom of the list
   OptionList[OptionList.length] = new Option(OptionText, OptionValue);
}

function removeAllOptions(selectbox)
{
	var i;
	for(i=selectbox.options.length-1;i>=0;i--)
	{
		//selectbox.options.remove(i);
		selectbox.remove(i);
	}
}