var xmlHttp
//////////////////////////////functions and variables for adding to basket
var productId
var OKtoAdd = true;

function addToBasket(prodId,catId,cartId,transid,prodName,qty)
{
	if (OKtoAdd)
	{
		OKtoAdd = false;
		if (qty == null) qty = 1;
		document.getElementById(prodId).innerHTML="<img src='/images/icons/ab_button/adding.gif' alt='adding to basket' />";
		productId = prodId
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
		  alert ("Browser does not support HTTP Request")
		  return
		}
		var noncache = Math.random()
		var aurl="/addToBasket.php?noncache="+noncache+"&prodId="+prodId+"&catId="+catId+"&cartId="+cartId+"&ab_addBask=y"+"&ab_transid="+transid+"&prodName="+prodName+"&qty="+qty
		xmlHttp.onreadystatechange=baskStateChanged
		xmlHttp.open("GET",aurl,true)
		xmlHttp.send(null)
	}
}



function baskStateChanged()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		
		var resText = xmlHttp.responseText
		var str
		var amount
		//str is used to store "X in basket", amount is used to store "basket contains X items"
		str = resText
		
		if(str >= 0)
		{
			//document.getElementById(productId).className='baskLink';
			document.getElementById(productId).innerHTML="<a href='/cart_view.php'><img src='/images/icons/ab_button/added.gif' alt='" + str + " in basket' /></a>";
		}
		else if(str == -1)
		{
			//document.getElementById(productId).className='noStock';
			document.getElementById(productId).innerHTML="<img src='/images/icons/ab_button/nostock.gif' alt='There is no more stock available of this product' />";
		}
		/*else
		{
			document.getElementById(productId).innerHTML=str
		}*/
		updateBaskSummary();
		OKtoAdd = true;
	}
}
////////////////////////////////////

function updateBaskSummary()
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
	  alert ("Browser does not support HTTP Request")
	  return
	}
	var aurl="/updateBaskSummary.php?echo=1"
	xmlHttp.onreadystatechange=baskSummaryUpdated
	xmlHttp.open("GET",aurl,true)
	xmlHttp.send(null)
	
}
function baskSummaryUpdated()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		var resText = xmlHttp.responseText
		document.getElementById("basket_summary").innerHTML = resText;
	}
}

function addToSample(prodId)
{
	productId = prodId;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
	  alert ("Browser does not support HTTP Request");
	  return;
	}
	var noncache = Math.random();
	var aurl="/addToSample.php?noncache="+noncache+"&prodId="+prodId;
	xmlHttp.onreadystatechange=sampleStateChanged
	xmlHttp.open("GET",aurl,true)
	xmlHttp.send(null)
}

function deleteSample(prodId)
{
	productId = prodId;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
	  alert ("Browser does not support HTTP Request");
	  return;
	}
	var noncache = Math.random();
	var aurl="/addToSample.php?noncache="+noncache+"&prodId="+prodId+"&delete=true";
	xmlHttp.onreadystatechange=sampleStateChanged
	xmlHttp.open("GET",aurl,true)
	xmlHttp.send(null)
}

function sampleStateChanged()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		var str = xmlHttp.responseText;

		if(str == "view") //OK
		{
			document.getElementById(productId + "_sample").innerHTML="<span id='" + productId + "_sample'><a href='/sample.php'>view sample list</a>&nbsp;<a href=\"javascript:deleteSample('"+productId+"');\" ><img src='/images/icons/green_cross.jpg' width='10' height='10'/></a></span>";
		}
		else if(str == "loseOne") //basket full
		{
			//if(confirm("You already have 5 cards in your sample list\n\n Click OK to substitute a card in your list"))
			{
				window.location = "/sample.php?prodId=" + productId + "&mode=add";
			}
		}
		else if(str == "duplicate")
		{
			alert("You already have that card type in your sample list");
		}
		else if(str == "delete")
		{
			document.getElementById(productId + "_sample").innerHTML="<span id='" + productId + "_sample'><a href=\"javascript:addToSample('" + productId + "')\">order sample<\/a><\/span>";
		}
	}
}
////////////////////////////////////
var id;
var n;
function autofill(n)
{
	this.n = n;
	var contactId = document.getElementById('addrList' + n).value;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
	  alert ("Browser does not support HTTP Request");
	  return;
	}
	var noncache = Math.random();
	var aurl="autofill.php?noncache="+noncache+"&id=" + contactId;
	xmlHttp.onreadystatechange=detailChange;
	xmlHttp.open("GET",aurl,true);
	xmlHttp.send(null);
}

function detailChange()
{
	if(xmlHttp.readyState!=4)
	{
		document.getElementById('contactDetailSalutation' + n).value = "**";
		document.getElementById('contactDetailName1' + n).value = "**";
	 	document.getElementById('contactDetailName2' + n).value = "**";
	}
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
	 	var resText = xmlHttp.responseText;
	 	var details = resText.split('*');
	 	document.getElementById('contactDetailSalutation' + n).value = details[0];
	 	document.getElementById('contactDetailName1' + n).value = details[1];
	 	document.getElementById('contactDetailName2' + n).value = details[2];
	 	document.getElementById('contactDetailCompany' + n).value = details[3];
	 	document.getElementById('contactDetailStreet1' + n).value = details[4];
	 	document.getElementById('contactDetailStreet2' + n).value = details[5];
	 	document.getElementById('contactDetailTown' + n).value = details[6];
	 	document.getElementById('contactDetailPostcode' + n).value = details[7];
	 	document.getElementById('locationId' + n).value = details[8];
	 }
}

////////////////////////////////////
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;
}


/* GENERIC AJAX SCRIPT */
var http_request = false;
function makePOSTRequest(url, parameters) 
{
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
     http_request = new XMLHttpRequest();
     if (http_request.overrideMimeType) {
     	// set type accordingly to anticipated content type
        //http_request.overrideMimeType('text/xml');
        http_request.overrideMimeType('text/html');
     }
  } else if (window.ActiveXObject) { // IE
     try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
        try {
           http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
     }
  }
  if (!http_request) {
     alert('Cannot create XMLHTTP instance');
     return false;
  }
  
  http_request.onreadystatechange = alertContents;
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}

function alertContents() 
{
  if (http_request.readyState == 4) {
     if (http_request.status == 200) {
        //alert(http_request.responseText);
        result = http_request.responseText;
        document.getElementById('reminders_special').innerHTML = result;
        //alert(result); //document.getElementById('myspan').innerHTML = result;            
     } else {
        alert('There was a problem with the request.');
     }
  }
}

function savecheckreminder(obj, user, check, remname) {

  var poststr = obj.name +"=" + encodeURI( obj.checked );
  poststr += "&user="+user+"&check="+check+"&remname="+remname;
  //alert(poststr);
  makePOSTRequest('/templates/users/savecheckreminder.php', poststr);
}



/* ADD TO FAVOURITES AJAX REQUEST */
function addToFav(prodId, isBulk)
{
	productId = prodId;
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
	  alert ("Browser does not support HTTP Request")
	  return
	}
	var noncache = Math.random()
	var aurl="/addToFav.php?prodId="+prodId+"&isBulk="+isBulk
	xmlHttp.onreadystatechange=favStateChanged
	xmlHttp.open("GET",aurl,true)
	xmlHttp.send(null)
}
function favStateChanged()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		var resText = xmlHttp.responseText
		var str
		var amount
		//str is used to store "X in basket", amount is used to store "basket contains X items"
		str = resText
		if(str == 1)
		{
			document.getElementById("add-to-fav-"+productId).innerHTML="Remove from Favourites";
			document.getElementById("add-to-fav-icon-"+productId).innerHTML="<img src='/images/icons/favourites/smile.gif' />";
			
		}
		else if(str == 0)
		{
			document.getElementById("add-to-fav-"+productId).innerHTML="Add to Favourites";
			document.getElementById("add-to-fav-icon-"+productId).innerHTML="<img src='/images/icons/favourites/blank.gif' />";
		}
		else
		{
			document.getElementById("add-to-fav-"+productId).innerHTML="Failed to add to favourites " + str;
			document.getElementById("add-to-fav-icon-"+productId).innerHTML="<img src='/images/icons/favourites/unsure.gif' />";
		}
	}
}