var chartOn=0;
var mapOn = 0;
napkinChartOn = false;

function submitsearch(){
	if(document.forms.searchform.terms){
		document.forms.searchform.submit();
	}
}

function clearsearch(){
	document.forms['searchform'].elements['terms'].value = '';	
}





function no(){}

function showusage(){
	$("#olivineusage").fadeIn("fast");	
}
function hideusage(){
	$("#olivineusage").fadeOut("fast");	
}

function linkto(url){
	document.location.href = url;
}

function tellFriendOpen(){
	try{document.getElementById("errorcode").innerHTML = '';}catch(err){}  
	$("#tellFriend").fadeIn("fast");
}

function tellFriendClose(){
	try{document.getElementById("errorcode").innerHTML = '';}catch(err){}  
	$("#tellFriend").fadeOut("fast");
}

function setDelivery(pagename){
	del_id = document.forms.basketform.del_id.value;
	document.location.href=pagename+'?action=setDelivery&del_id='+del_id;
}

function calctotal(){
	
	var price = 0;
	var q = 0;
	var total = 0;
	var str = '';
	
	for(i=0; i<document.orderform.elements.length; i++){
		
		var name = document.orderform.elements[i].name;
		var val = document.orderform.elements[i].value;
		
		if(parseFloat(val) || parseInt(val)){
		
			if(name.substr(0, 2) == 'p_'){
				price = val;
			}
			if(name.substr(0, 2) == 'it'){
				q = val;
			}
			
			if(q>0 && price>0){
				str +=  "The quantity: " + q + " and it’s price is: " +  price + "<br />";
				total = total + (q*price);
				price = q = 0;
			}
			
		}else if(name.substr(0, 2) == 'p_' || name.substr(0, 2) == 'it'){
			document.orderform.elements[i].value = 0;
		}
		
		
	}
	total = (total).formatMoney(2, '.', ',');
	document.getElementById('runningtotal').innerHTML = '&pound;'+total;
}

Number.prototype.formatMoney = function(c, d, t){
var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
   return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
 };

function ourmap(){
	
	if(mapOn){
		$("#ourmap").fadeOut("fast");
		mapOn = false;
	}else{
		$("#ourmap").fadeIn("fast");
		mapOn = true;
	}
}

function napkins(){
	
	if(napkinChartOn){
		$("#napkinChart").fadeOut("fast");
		napkinChartOn = false;
	}else{
		$("#napkinChart").fadeIn("fast");
		napkinChartOn = true;
	}
}

function ourmapclose(){
	$("#ourmap").fadeOut("fast");
	mapOn = false;
}

function useBillingAddress(){
	document.forms.signupform.delivery_address1.value	= document.forms.signupform.address1.value;
	document.forms.signupform.delivery_address2.value	= document.forms.signupform.address2.value;
	document.forms.signupform.delivery_town.value		= document.forms.signupform.town.value;
	document.forms.signupform.delivery_county.value		= document.forms.signupform.county.value;
	document.forms.signupform.delivery_postcode.value	= document.forms.signupform.postcode.value;
	document.forms.signupform.delivery_country.value	= document.forms.signupform.country.value;
}


function deleteconfirm(del){ 
	if (
		confirm("Are you sure you wish to permanently delete this item?\n\nOK = Yes - Cancel = No")){ 
		window.location=del;
	}
} 

var xmlHttp = createXmlHttpRequestObject(); 

function createXmlHttpRequestObject(){	
  var xmlHttp;
  // if running Internet Explorer
  if(window.ActiveXObject)
  {
    try{
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // if running Mozilla or other browsers
  else
  {
    try 
    {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

function processTellFriend(){
	
	var from = document.forms['tellform'].from.value;
	var to = document.forms['tellform'].to.value;
	var message = document.forms['tellform'].message.value;
	
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){

	var url = "ajax/processor.php?action=friend&from=" + from + '&to=' + to + '&msg=' + message;
	//alert(url);
	xmlHttp.open("GET", url, true);  
	xmlHttp.onreadystatechange = handleServerResponseTellFriend;
    xmlHttp.send(null);
	
	//try{document.getElementById("tellFriend").innerHTML = url;}catch(err){}
	try{document.getElementById("errorcode").innerHTML = 'Contacting server...';}catch(err){}
	
  }else{ 
    setTimeout('processTellFriend()', 1000);
  }
}



// executed automatically when a message is received from the server
function handleServerResponseTellFriend() {
  if (xmlHttp.readyState == 4) {
    if (xmlHttp.status == 200) {
      
	  xmlResponse = xmlHttp.responseXML;
	  xmlDocumentElement = xmlResponse.documentElement;
	
	var errorcode = xmlDocumentElement.getElementsByTagName("errorcode").item(0).firstChild.data;
	var htmlcode = xmlDocumentElement.getElementsByTagName("htmlcode").item(0).firstChild.data;
	
	 if(errorcode == 1){
		try{document.getElementById("errorcode").innerHTML = htmlcode;}catch(err){}  
	 }else{
		 try{document.getElementById("errorcode").innerHTML = '';}catch(err){}  
		 try{document.getElementById("friendsend").innerHTML = '';}catch(err){}  
		 
		try{document.getElementById("tellFriendContent").innerHTML = htmlcode;}catch(err){}
	 }
	
	} else {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}


function team(team_id, dir){
	if(dir){
		loadTeam(team_id)
		$("#teampopup").fadeIn(100);
	}else{
		$("#teampopup").fadeOut(20);
		try{document.getElementById("teampopup").innerHTML = '';}catch(err){}
	}
}

function loadTeam(team_id){
	
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){

	var url = "ajax/processor.php?action=team&team_id=" + team_id;

	xmlHttp.open("GET", url, true);  
	xmlHttp.onreadystatechange = handleServerResponseTeam;
    xmlHttp.send(null);
	
  }else{ 
    setTimeout('loadTeam()', 1000);
  }
}

function handleServerResponseTeam() {
  if (xmlHttp.readyState == 4) {
    if (xmlHttp.status == 200) {
      
	  xmlResponse = xmlHttp.responseXML;
	  xmlDocumentElement = xmlResponse.documentElement;
	
	var htmlcode = xmlDocumentElement.getElementsByTagName("htmlcode").item(0).firstChild.data;
	
	try{document.getElementById("teampopup").innerHTML = htmlcode;}catch(err){}  

	} else {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}


function chart(){
	
	//convChart
	if(chartOn == 1){
		$("#convChart").fadeOut("fast");
		chartOn = 0;
	}else{
		$("#convChart").fadeIn("fast");
		chartOn = 1;
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
