// JavaScript Document

function updatePrice(element, prodID)
{
		fieldData = element.value;
		findNewPrice(fieldData, prodID);
}
function toggleField(val, id) {
	var box = document.getElementById(id);
	(val == '11')? box.style.display = 'block' : box.style.display = 'none';
	parent.resizeCaller();
}
function toggleField2(val, boxName, id) {
	var box = document.getElementById(boxName);
	var logoVal = document.getElementById("logo"+id).value;
	var curPrice = document.getElementById("prodPrice"+id).value;

	if (val == '1') {
		box.style.display = 'block';
		if (logoVal == '1') {
			curPrice = parseFloat(curPrice);
		}else{
			curPrice = parseFloat(curPrice)+0.25;
		}
		document.getElementById("priceTag"+id).innerHTML="&pound;"+CurrencyFormatted(curPrice);
		document.getElementById("prodPrice"+id).value=curPrice;
	}else{
		box.style.display = 'none';
		if (logoVal == '1') {
			curPrice = parseFloat(curPrice);
		}else{
			curPrice = parseFloat(curPrice)-0.25;
		}
		document.getElementById("priceTag"+id).innerHTML="&pound;"+CurrencyFormatted(curPrice);
		document.getElementById("prodPrice"+id).value=curPrice;
	};
	parent.resizeCaller();
}
function toggleField3(val, id) {
	var curPrice = document.getElementById("prodPrice"+id).value;
	var schVal = document.getElementById("school"+id).value;
	
	if (val == '1') {
		if (schVal == '1') {
			curPrice = parseFloat(curPrice)+0.20;
		}else{
			curPrice = parseFloat(curPrice)+0.45;
		}
		document.getElementById("priceTag"+id).innerHTML="&pound;"+CurrencyFormatted(curPrice);
		document.getElementById("prodPrice"+id).value=curPrice;
		document.getElementById("logoTip"+id).style.visibility='visible';
	}else{
		if (schVal == '1') {
			curPrice = parseFloat(curPrice)-0.20;
		}else{
			curPrice = parseFloat(curPrice)-0.45;
		}
		document.getElementById("priceTag"+id).innerHTML="&pound;"+CurrencyFormatted(curPrice);
		document.getElementById("prodPrice"+id).value=curPrice;
		document.getElementById("logoTip"+id).style.visibility='hidden';
	};
}
function CurrencyFormatted(amount) {
        var i = parseFloat(amount);
        if(isNaN(i)) { i = 0.00; }
        var minus = '';
        if(i < 0) { minus = '-'; }
        i = Math.abs(i);
        i = parseInt((i + .005) * 100);
        i = i / 100;
        s = new String(i);
        if(s.indexOf('.') < 0) { s += '.00'; }
        if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
        s = minus + s;
        return s;
}
function checkOptions(newPrice, id) {
	var schOp = document.getElementById("school"+id).value;
	var logOp = document.getElementById("logo"+id).value;

	if ((schOp == '1') && (logOp == '1')) {
		newPrice = parseFloat(newPrice)+0.45;
	}else if ((schOp == '0') && (logOp == '1')) {
		newPrice = parseFloat(newPrice)+0.45;
	}else if ((schOp == '1') && (logOp == '0')) {
		newPrice = parseFloat(newPrice)+0.25;
	}else{
		newPrice = parseFloat(newPrice);
	}
	document.getElementById("priceTag"+id).innerHTML="&pound;"+CurrencyFormatted(newPrice);
	document.getElementById("prodPrice"+id).value=newPrice;		
}

var xmlHttp
var alertWin

function findNewPrice(fieldData, prodID)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="scripts/getNewPrice.asp";
url=url+"?prodID="+prodID;
url=url+"&quantity="+fieldData;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4)
	{ 
	//alert(prodID);
	//alert(xmlHttp.responseText);
	if (xmlHttp.responseText != 'None')
		{
		var stuff = xmlHttp.responseText;
		var strResponse = stuff.split(',');
		document.getElementById("priceTag"+strResponse[2]).innerHTML=strResponse[0];
		document.getElementById("prodPrice"+strResponse[2]).value=strResponse[1];
		checkOptions(strResponse[1], strResponse[2]);
		}
	}
}


function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
