var xmlHttp;

function GetXmlHttpObject()
{
  var xmlHttp=null;
  try
    {
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
  return xmlHttp;
}

function emailReminder(email,stockid){
	xmlHttp=GetXmlHttpObject();

	xmlHttp.onreadystatechange=function()
  {
	   if(xmlHttp.readyState==4)
	   {
		   document.getElementById("reminderspan").innerHTML = xmlHttp.responseText;
	   }
  }

	xmlHttp.open("GET","emailreminder.php?email="+email+"&prodid="+stockid,true);
    xmlHttp.send(null);
	document.getElementById('regform').style.display='none';
}

function cancelReminder(reminderid){
	xmlHttp=GetXmlHttpObject();

	xmlHttp.onreadystatechange=function()
  {
	   if(xmlHttp.readyState==4)
	   {
		   document.getElementById("reminderspan").innerHTML = "";
	   }
  }

	xmlHttp.open("GET","emailreminder.php?reminderid="+reminderid,true);
    xmlHttp.send(null);
}

function selectCase(id, showvat, vatrate)
{

	var stockidprice = (document.getElementById(id).options[document.getElementById(id).options.selectedIndex].title);
	var stockid = stockidprice.slice(0,9);
	var price = Number(stockidprice.slice(10));
	var photo = (document.getElementById(id).options[document.getElementById(id).options.selectedIndex].id);

	//var base = Number(document.getElementById("baseprice").value);

	//newprice = price + base;
	//newprice = newprice.toFixed(2)
	//document.getElementById("prodprice").innerHTML="&pound;"+newprice;

	document.getElementById("prodimage").src="pimg/"+stockid+"_200.jpg";
	document.getElementById("showphoto").href="showphoto.php?src=pimg/"+stockid+"_400.jpg&amp;name="+photo;
	//alert(document.getElementById(id).options.selectedIndex);
	//alert(id);
	prodPrice(showvat, vatrate);
}

function selectOther(id, showvat, vatrate)
{
	prodPrice(showvat, vatrate);
}


function prodPrice(showvat, vatrate){
	var newprice = 0;
	var newpriceinc = 0;
	var smallprice = 0;
	var largeprice = 0;
	var base = Number(document.getElementById("baseprice").value);
	var linkList = document.getElementById('productInfo_tbl_right').getElementsByTagName('select');
	for (i = 0; i < linkList.length; i++)
    {
	    var idstring = linkList[i].id;
		if(idstring.slice(0,3) == "sel")
        {
	        var stockidprice = (linkList[i].options[linkList[i].options.selectedIndex].title);
			var stockid = stockidprice.slice(0,9);
			var price = Number(stockidprice.slice(10));
			newprice = newprice + price;

			var selLength = linkList[i].length;
			for(var j = 0; j < selLength; j++) {
				var sText = linkList[i].options[j].text;
				iSplit = 0;
				iSplit = sText.indexOf("(£");
				if(iSplit == -1) {
					iSplit = sText.indexOf("(-");
				}
				if(iSplit == -1){
					iSplit = sText.indexOf("(+");
				}
				iSplit = iSplit - 1;
				sDesc = sText.slice(0,iSplit);
				var stockidprice = (linkList[i].options[j].title);
				var optprice = Number(stockidprice.slice(10));
				if (linkList[i].options.selectedIndex == j) {
					linkList[i].options[j].text = sDesc + " (£0.00)";
				}
				else
				{
					if (optprice > price) {
						ioptprice = optprice - price;
						ioptprice = ioptprice.toFixed(2);
						linkList[i].options[j].text = sDesc + " (+ £"+ioptprice+")";
					}
					else
					{
						ioptprice = price - optprice;
						ioptprice = ioptprice.toFixed(2);
						linkList[i].options[j].text = sDesc + " (- £"+ioptprice+")";
					}
				}
			}
        }
    }

	newprice = newprice + base;
    newprice = newprice.toFixed(2);
	if(showvat == 1)
	{
    	largeprice = newprice * (1+(vatrate / 100));
		smallprice = newprice;
	}
	else
	{
		largeprice = newprice;
		smallprice = newprice * (1+(vatrate / 100));
	}
	largeprice = Number(largeprice);
	smallprice = Number(smallprice);
    largeprice = largeprice.toFixed(2);
	smallprice = smallprice.toFixed(2);

	document.getElementById("prodprice").innerHTML="&pound;"+largeprice;
	document.getElementById("prodpriceinc").innerHTML="&pound;"+smallprice;
}
