var cartInitialized = 0;



if(document.cookie.length > 0 ){//this function tests for the existance of cookies.
	
	if(getValue("products")==""){// test for the existance of the products cookie, and create it if it doesn't exist.
		
		
		makeCookie("");
		
	}
	
}


function makeCookie(productId){
	
	
	//create  new date object
	//var expiry = new Date();
	
	//set the expiry for the end days time.
	//expiry.setTime(expiry.getTime() + (1*24*60*60*1000));
	
	if(productId ==""){
		
		//create the cookie
		document.cookie = "products=0; path=/; domain=www.hoofsolutions.com;";
		cartInitialized =0;
	}
	
	else	{
		
		cartInitialized = 1;  //this variable shows that the cart has had a value added to it.
		var existingValues = getValue("products");
		if(existingValues =="0"){existingValues="";}
		var quantity = document.getElementById("quantity_text").value;
		if(quantity == "")//if  a quantity is not set, ask to set one.
		{alert("Please enter a quantity for the product");
			
			
		}else{
			var size =getSize();
			var newValues = productId+","+size+","+quantity+"-";
			document.cookie = "products="+existingValues + newValues+"; path=/; domain=www.hoofsolutions.com;";
			showPreview(productId, quantity);
			
		}
		
	}
	
	
	
	
	
	
}


function getSize(){
	
	var sizesArray = document.getElementsByName("size");
	
	if(sizesArray.length == 0 )
		
		{checked = "none";}
	
	
	
	
	else{
		
		for(var i=0; i < sizesArray.length;i++){
			
			if(sizesArray[i].checked){
				var checked = sizesArray[i].value;
				
				
			}
		}
	}
	
	
	
	return checked;
}


function getValue(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{ 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
		
	}
	return "";
}


function add(productId){
	
	makeCookie(productId);
	
	
}
/* function changeButton(buttonName){
	var buttonToChange = document.getElementById(buttonName);
	buttonToChange.style.cursor="pointer";
	
} */

function showPreview(productId, quantity){
	
	
	preview = window.open('http://www.hoofsolutions.com/cartpreview.php?id='+productId+'&q='+quantity,'preview','height=200,width=350,directories=0,toolbar=0,satus=0,resizable=0,menubar=0,location=0,top=250,left=400');
	if (window.focus) {preview.focus()}
	
	
}


function getCartSize(){
	var values = getValue('products');
	var valuesArray = values.split('-');
	var cartSize = valuesArray.length -1;
	return cartSize;
	
}
function showCart(){
	var cartSize = getCartSize();
	window.open('cart.php','_blank');
	
}
function dropRow(rowToDrop){
var rows = getValue('products').split('-');

if(rows.length !=2){
newValuesArray = new Array();
for(var i = 0; i <rows.length;i++){
	if(i != rowToDrop)
		{
		newValuesArray[newValuesArray.length]= rows[i];
		
		}

}



var newValues = newValuesArray.join('-');

document.cookie = "products="+newValues+"; path=/; domain=www.hoofsolutions.com;";
}
else{makeCookie('');}
alert('This item has been deleted from your shopping cart');
window.location ='http://www.hoofsolutions.com/cart.php';



}


function emptyCart(){
	

var ask =confirm("Are you sure you want to empty your shopping cart?");// this function will reset the cart, but prompt the user for confirmation first
if (ask){

makeCookie(""); 
window.location ='http://www.hoofsolutions.com/cart.php';
}


}



