function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function image(img)
{
	document.main_image.src="/corporategifts/thumbnails/phpThumb.php?src=../images/products/" + img + "&w=300&h=300";
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function getSample()
{

var sapref = document.forms.sample.sapref.value;
document.forms.sample.colour.value = document.forms.quoteform.productcolour.value;
document.forms.sample.colourqty.value = document.forms.quoteform.colourqty.value;
document.forms.sample.imprintmethod.value = document.forms.quoteform.imprintmethod.value;
document.forms.sample.submit();

}

function basketUpdate(qty, bid, pid, minqty, colourqty, imprint, layer)
{

var qty = qty.value;

if(parseInt(qty) < parseInt(minqty)) { 
alert("Quote Problem: You must order a minimum of "+minqty+""); 
} else {

document.getElementById(layer).innerHTML = '<img src="/js/line.gif">';
	
	var url = "/corporategifts/_ajax.php";
	var data = "call=updateQuote&qty=" + qty + "&pid=" + pid + "&bid=" + bid + "&colourqty=" + colourqty + "&imprint=" + imprint;
	
	http.open("POST", url, true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http.send(data);
	http.onreadystatechange = updateUnitPrice;

}

}

function setImprint()
{

if(document.forms.quoteform.imprintmethod)
{
var imprint = document.forms.quoteform.imprintmethod.value;
} else {
var imprint = '';
}

var pid = document.forms.quoteform.id.value;

	var url = "/corporategifts/_ajax.php";
	var data = "call=getImprint&pid=" + pid + "&imprint=" + imprint;

	http.open("POST", url, true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http.send(data);
	http.onreadystatechange = updateImprint;

}

function checkproduct()
{

var sapref = document.forms.addoffer.sapref.value;

	var url = "/corporategifts/admin/index.php";
	var data = "action=checkproduct&sapref=" + sapref;

	http.open("POST", url, true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http.send(data);
	http.onreadystatechange = updateCheck;

}

function setImprintChange(obj)
{

if(document.forms.quoteform.imprintmethod)
{
var imprint = obj.value;
var pid = document.forms.quoteform.id.value;

	var url = "/corporategifts/_ajax.php";
	var data = "call=getImprint&pid=" + pid + "&imprint=" + imprint;

	http.open("POST", url, true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http.send(data);
	http.onreadystatechange = updateImprint;
	
}

}

function advancedCategory(obj)
{

	var url = "/corporategifts/_ajax.php";
	var data = "call=getCategories&catalogue=" + obj.value;

	http.open("POST", url, true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http.send(data);
	http.onreadystatechange = updateAdvancedCategories;

}

function getQuoteColour(obj)
{

var origination = document.forms.quoteform.origination_single.value;

var new_origination = parseInt(obj.value) * parseFloat(origination);
var new_origination = new_origination.toFixed(2);

	document.getElementById('origination_span').innerHTML = new_origination; 
	document.forms.quoteform.origination.value = new_origination;

	document.forms.quoteform.colourqty.value = obj.value;
	getQuote();

}

function getQuote()
{

var min = document.forms.quoteform.lowest_qty_available.value;
var qty = document.forms.quoteform.productqty.value;

	if(parseInt(qty) < parseInt(min)) { 

	document.forms.quoteform.productqty.value = min;
	alert("Quote Problem: You must order a minimum of "+min+""); 

	} else  { 
	
	if(document.forms.quoteform.imprintmethod) { var imprint = document.forms.quoteform.imprintmethod.value; }
	if(document.forms.quoteform.colourqty) { var colourqty = document.forms.quoteform.colourqty.value; }
	var pid = document.forms.quoteform.id.value;	

	var url = "/corporategifts/_ajax.php";
	var data = "call=getQuote&qty=" + qty + "&pid=" + pid + "&colourqty=" + colourqty + "&imprint=" + imprint;
	
	http.open("POST", url, true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http.send(data);
	http.onreadystatechange = setUnitPrice;

	}
	
if(document.forms.quoteform.colourqty) { var colourqty = document.forms.quoteform.colourqty.value; }
var origination = document.forms.quoteform.origination.value;

}

function setDelivery(obj)
{

	var url = "/corporategifts/_ajax.php";
	var data = "call=getAddress&aid=" + obj.value;

	http.open("POST", url, true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http.send(data);
	http.onreadystatechange = updateAddress;

}

function remove(bid)
{

document.getElementById('remove'+bid).innerHTML = '<img src="/js/spin.gif">';

	var url = "/corporategifts/_ajax.php";
	var data = "call=updateBasket&sub=delete&bid=" + bid;

	http.open("POST", url, true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http.send(data);
	http.onreadystatechange = showBasket;

}

function showBasket() {
  if (http.readyState == 4) {
    if (http.responseText.indexOf('invalid') == -1) {
      
	  items = http.responseText.split("@@@");
	  
		for (var i=0;i<items.length;i++) {	  
			results = items[i].split("---!---");
			if(results[0] == 'dynamic_qty' && results[1] == '0 Items') { window.location='/corporategifts/basket'; }
			else { 
				if(document.getElementById(results[0])) {
					document.getElementById(results[0]).innerHTML = results[1]; 
				}
			}
		}
	}
  }
}

function setUnitPrice() {
  if (http.readyState == 4) {
    if (http.responseText.indexOf('invalid') == -1) {

	  var unit_price = http.responseText;
	  
	  if(parseFloat(unit_price) > 0) {
	  
			  document.getElementById('unit_price_span').innerHTML = '&pound;' + unit_price;
			  document.forms.quoteform.unit_price.value = unit_price;
			  
			var qty = document.forms.quoteform.productqty.value;
			var origination = document.forms.quoteform.origination.value;
			
			var total_price = parseFloat((parseFloat(qty) * parseFloat(unit_price)) + parseFloat(origination));
			var total_price_rounded = total_price.toFixed(2);
			
			document.getElementById('total_price_span').innerHTML = '&pound;' + total_price_rounded;
			document.forms.quoteform.total_price.value = total_price_rounded;
	
		} else {
		
			  document.getElementById('unit_price_span').innerHTML = 'Please Call';
			  document.forms.quoteform.unit_price.value = '0.00';
			  
			var qty = document.forms.quoteform.productqty.value;
			var origination = document.forms.quoteform.origination.value;
			
			var total_price = parseFloat((parseFloat(qty) * parseFloat(unit_price)) + parseFloat(origination));
			var total_price_rounded = total_price.toFixed(2);
			
			document.getElementById('total_price_span').innerHTML = 'Please Call';
			document.forms.quoteform.total_price.value = '0.00';		
		
		}

	}
  }
}

function updateUnitPrice() {
  if (http.readyState == 4) {
    if (http.responseText.indexOf('invalid') == -1) {

	  window.location='/corporategifts/basket';

	}
  }
}

	function RoundMe(x,places) {
		return (Math.round(x*Math.pow(10,places)))/Math.pow(10,places)
	}

function updateImprint() {
  if (http.readyState == 4) {
    if (http.responseText.indexOf('invalid') == -1) {
	     
	  response = http.responseText.split("@@@");
	  
		for (var i=0;i<response.length;i++) {	  
			results = response[i].split("---!---");	 
			
			if(results[0] == 'origination')
			{
				document.getElementById('origination_span').innerHTML = results[1]; 
				document.getElementById('origination_footer').innerHTML = results[1]; 
				document.forms.quoteform.origination.value = results[1];
				document.forms.quoteform.origination_single.value = results[1];
			}
			
			if(results[0] == 'colours')
			{
		
			if(document.forms.quoteform.colourqty) { 
				var selbox = document.forms.quoteform.colourqty;
				selbox.options.length = 0;		

				options = results[1].split(",");	 				

				for(i=0;i<options.length;i++) {
					selbox.options[selbox.options.length] = new Option(options[i],options[i]);
				}	
			}
			}
			
			getQuote();
			
		}
	  

	}
  }
}

function updateAdvancedCategories() {
  if (http.readyState == 4) {
    if (http.responseText.indexOf('invalid') == -1) {
	     
				var selbox = document.forms.advanced.category;
				selbox.options.length = 0;		

				options = http.responseText.split("^");	 				

				for(i=0;i<options.length;i++) {
				thisitem = options[i].split("---");	
					selbox.options[selbox.options.length] = new Option(thisitem[0],thisitem[1]);
				}	
		}
  }
}

function updateCheck() {
  if (http.readyState == 4) {
    if (http.responseText.indexOf('invalid') == -1) {
	     
				options = http.responseText.split("---");	
				document.getElementById(options[0]).innerHTML = options[1];  				

	  }
}
}

function updateAddress() {
  if (http.readyState == 4) {
    if (http.responseText.indexOf('invalid') == -1) {
      
	  items = http.responseText.split("@@@");
	  
		document.forms.finalise.firstname.value = items[0];
		document.forms.finalise.lastname.value = items[1];
		document.forms.finalise.companyname.value = items[2];
		document.forms.finalise.address1.value = items[3];
		document.forms.finalise.address2.value = items[4];
		document.forms.finalise.city.value = items[5];
		document.forms.finalise.state.value = items[6];
		document.forms.finalise.postcode.value = items[7];
		document.forms.finalise.country.value = items[8];
		document.forms.finalise.phonenumber.value = items[9];
		
	}
  }
}