var xmlHttp;
var coulourid;
var quantity;
var weekstart;
var freetext;
var isbasket;
var ignore;
function ordercolour(mURL)
{
	window.location = mURL;
}
function creataccount()
{
	if (document.getElementById("email"))
	{
		if (document.getElementById("password").value != "")
		{
			xmlHttp=GetXmlHttpObject();
			var url = document.location.toString() ; //url
			var e_url = '' ; //edited url
			var p = 0 ; //position
			var p2 = 0 ;//position 2
			p = url.indexOf("//") ;
			e_url = url.substring(p+2) ;
			p2 = e_url.indexOf("/") ;
			var root_url = url.substring(0,p+p2+3);
			url=root_url+"includes/createaccount.php";
			url=url+"?password="+document.getElementById("password").value;
			url=url+"&sid="+Math.random();
			ignore = "ignoreit";
			xmlHttp.onreadystatechange=stateChanged;
			xmlHttp.open("GET",url,true);
			xmlHttp.send(null);
			document.getElementById("create_account").innerHTML = "<h2>Customer Account Created</h2><p>Your account has been created. You will recieve an email confirming your username and password. To pay for your goods, click on the continue button below.</p>";
		}
		
	}
	return;
}
function checkaddress()
{
	if (document.getElementById("country").value == "103")
    {
    	document.getElementById("postcode").value == "0";
		document.getElementById("bpostcode").value == "0";
    }
	if (document.getElementById("bsurname").value == "")
    {
    	alert("Please input a bliing surname.");
		return false;
    }
	if (document.getElementById("surname").value == "")
    {
    	alert("Please input a surname.");
		return false;
    }
	if (document.getElementById("bfirstname").value == "")
    {
    	alert("Please input a first name.");
		return false;
    }
	if (document.getElementById("firstname").value == "")
    {
    	alert("Please input a first name.");
		return false;
    }
	if (document.getElementById("bpostcode").value == "")
    {
    	alert("Please input a postcode/zip code.");
		return false;
    }
	if (document.getElementById("postcode").value == "")
    {
    	alert("Please input a postcode/zip code.");
		return false;
    }
	if (document.getElementById("email").value == "")
    {
    	alert("Please input an email address.");
		return false;
    }
	if (document.getElementById("bcountry").value == "")
    {
    	alert("Please input a billing country");
		return false;
    }
	if (document.getElementById("country").value == "")
    {
    	alert("Please input a country");
		return false;
    }
	if (document.getElementById("telephone").value == "")
    {
    	alert("Please input a telephone number");
		return false;
    }
	if (document.getElementById("telephone").value == "")
    {
    	alert("Please input a telephone number");
		return false;
    }
    return true;	
}
function copyaddress()
{
		document.getElementById("bfirstname").value=document.getElementById("firstname").value;
		document.getElementById("bsurname").value=document.getElementById("surname").value;
		document.getElementById("baddress").value=document.getElementById("address").value;
		document.getElementById("baddress2").value=document.getElementById("address2").value;
		document.getElementById("btown").value=document.getElementById("town").value;
		document.getElementById("bcounty").value=document.getElementById("county").value;
		document.getElementById("bpostcode").value=document.getElementById("postcode").value;
		document.getElementById("bcountry").selectedIndex =document.getElementById("country").selectedIndex ;
}
function updatebasket()
{
	xmlHttp=GetXmlHttpObject();
	var loopit = 1;
	var elementname = "";
	var inputname="";
	var loopcount = 0;
	var paramatres="";
	var totalprice = 0.00;
	var quantity=0;
	var totalpriceline=0;
	while (loopit == 1)
	{
		elementname="line"+loopcount;
		if (document.getElementById(elementname))
		{ 
			inputname="txt"+loopcount;
			if (paramatres == "")
			{
				
				quantity=document.getElementById(inputname).value;
				paramatres=paramatres+loopcount+","+quantity;
				inputname="prc"+loopcount;
				paramatres=paramatres+","+document.getElementById(inputname).value;
				totalprice=totalprice + (quantity*document.getElementById(inputname).value);
				totalpriceline=quantity*document.getElementById(inputname).value;
				document.getElementById("totalprice"+loopcount).innerHTML="&pound;"+totalpriceline.toFixed(2);
			}
			else
			{
				quantity=document.getElementById(inputname).value;
				paramatres=paramatres+","+loopcount+","+quantity;
				inputname="prc"+loopcount;
				paramatres=paramatres+","+document.getElementById(inputname).value;
				totalprice=totalprice + (quantity*document.getElementById(inputname).value);
				totalpriceline=quantity*document.getElementById(inputname).value;
				document.getElementById("totalprice"+loopcount).innerHTML="&pound;"+totalpriceline.toFixed(2);
			}
		}
		else
		{
			loopit = 0;
		}
		loopcount=loopcount+1;
	}
	var url = document.location.toString() ; //url
	var e_url = '' ; //edited url
	var p = 0 ; //position
	var p2 = 0 ;//position 2
	p = url.indexOf("//") ;
	e_url = url.substring(p+2) ;
	p2 = e_url.indexOf("/") ;
	var root_url = url.substring(0,p+p2+3);
	url=root_url+"includes/updatebasket.php";
	url=url+"?par="+paramatres;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	document.getElementById("grandtotal").innerHTML="&pound;"+totalprice.toFixed(2);



	
}

function deletebasketgroup(mGroupID)
{
	xmlHttp=GetXmlHttpObject();
	var r=confirm("You are about to delete this item. Press cancel if you do not want to delete this item.");
	if (r==true)
	{
		var url = document.location.toString() ; //url
		var e_url = '' ; //edited url
		var p = 0 ; //position
		var p2 = 0 ;//position 2
		p = url.indexOf("//") ;
		e_url = url.substring(p+2) ;
		p2 = e_url.indexOf("/") ;
		var root_url = url.substring(0,p+p2+3);
		var elementname = "line"+mGroupID;
		//document.getElementById(elementname).innerHTML="<td></td><td></td><td></td><td></td>";
		url=root_url+"includes/deletebasketline.php";
		url=url+"?groupid="+mGroupID;
		url=url+"&sid="+Math.random();
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
		
		document.getElementById("del"+mGroupID).innerHTML="<span class = 'deleted'>Item Deleted</span>";
		document.getElementById("totalprice"+mGroupID).innerHTML="-";
		document.getElementById("txt"+mGroupID).value="0";
		document.getElementById("prc"+mGroupID).value="0";
		document.getElementById("txt"+mGroupID).disabled=true;
		
		var loopit = 1;
		var elementname = "";
		var inputname="";
		var loopcount = 0;
		var paramatres="";
		var totalprice = 0.00;
		var quantity=0;
		while (loopit == 1)
		{
			elementname="line"+loopcount;
			if (document.getElementById(elementname))
			{ 
				inputname="txt"+loopcount;
				if (paramatres == "")
				{
					
					quantity=document.getElementById(inputname).value;
					paramatres=paramatres+loopcount+","+quantity;
					inputname="prc"+loopcount;
					paramatres=paramatres+","+document.getElementById(inputname).value;
					totalprice=totalprice + (quantity*document.getElementById(inputname).value);
				}
				else
				{
					quantity=document.getElementById(inputname).value;
					paramatres=paramatres+","+loopcount+","+quantity;
					inputname="prc"+loopcount;
					paramatres=paramatres+","+document.getElementById(inputname).value;
					totalprice=totalprice + (quantity*document.getElementById(inputname).value);
				}
			}
			else
			{
				loopit = 0;
			}
			loopcount=loopcount+1;
			document.getElementById("grandtotal").innerHTML="&pound;"+totalprice.toFixed(2);
	}

		
	}
}

function addtobaset(mItemID,mItemPrice,mItemName,mMinOrder)
{ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
 	{
 		alert ("Browser does not support HTTP Request");
 		return;
 	}
	
	var mElement = "";
	mElement = "productquantity"+mItemID;
	quantity=document.getElementById(mElement).value;
	
	if(quantity == "")
	{
		alert("Please specify a quantity");
		return;
	}
	if(quantity < mMinOrder)
	{
		alert("You have not met the minimum order of "+mMinOrder);
		return;
	}
	var url = document.location.toString() ; //url
	var e_url = '' ; //edited url
	var p = 0 ; //position
	var p2 = 0 ;//position 2
	p = url.indexOf("//") ;
	e_url = url.substring(p+2) ;
	p2 = e_url.indexOf("/") ;
	var root_url = url.substring(0,p+p2+3);

	url=root_url+"includes/addtobasket.php";
	url=url+"?productid="+mItemID+"&quantity="+quantity+"&itemprice="+mItemPrice;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	var mMsg=mItemName+" added to basket";
	$.jGrowl(mMsg)
}
function updatebasketcontents()
{
	xmlHttp=GetXmlHttpObject();
		
	var url = document.location.toString() ; //url
	var e_url = '' ; //edited url
	var p = 0 ; //position
	var p2 = 0 ;//position 2
	p = url.indexOf("//") ;
	e_url = url.substring(p+2) ;
	p2 = e_url.indexOf("/") ;
	var root_url = url.substring(0,p+p2+3);

	url=root_url+"includes/showbasketoverview.php";
	url=url+"?sid="+Math.random();
	isbasket=1;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function stateChanged() 
{ 
	if(ignore == "ignoreit"){return;}
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 	{ 
 	
		document.getElementById("header_cart").innerHTML=xmlHttp.responseText;
 	} 
}

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;
}
function popup_message()
{

 };    
function searchdatasite()
{
	
	var http = new XMLHttpRequest();
	var url = getBaseURL();
	url = url+"includes/search.php";

	var params = "";
	
	params="searchterm="+document.getElementById("search").value;
		
	http.open("POST", url, true);

	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");

	http.onreadystatechange = function() 
	{//Call a function when the state changes.
		if(http.readyState == 4 && http.status == 200) 
		{	
			url=getBaseURL();
			window.location = url+"search.php";
		}
	}
	http.send(params);
	
}
function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));


    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }

}

