// JavaScript Document

function cookieExists(){
	if (document.cookie.length > 0 && getCookie('cart') != null) { 
		return true;}
	else{ 
		return false;}
}


function setCookie(name, value) {
	var expires = new Date ();
    expires.setTime (expires.getTime() + (1000 * 60 * 60 * 24 * 2));  //sets the date for 2 days
	if (!expires) expires = new Date();
    document.cookie = name + "=" + escape (value) + 
	"; expires=" + expires.toGMTString() +  "; path=/";
    } 

function getCookie(name) {
    var dcookie = document.cookie; 
    var cname = name + "=";
    var clen = dcookie.length;
    var cbegin = 0;
    while (cbegin < clen) {
        var vbegin = cbegin + cname.length;
        if (dcookie.substring(cbegin, vbegin) == cname) { 
            var vend = dcookie.indexOf (";", vbegin);
            if (vend == -1) vend = clen;
            return unescape(dcookie.substring(vbegin, vend));
            }
       cbegin = dcookie.indexOf(" ", cbegin) + 1;
       if (cbegin == 0) break;
       }
    return null;
    }
	
function delCookie (cookie_name){
    var cookie_date = new Date ( );  // current date & time
    cookie_date.setTime ( cookie_date.getTime() - 1 );
    document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
	}

function removeItem(cartItem){//function
	if(cookieExists("cart")){//if
		var cart = new Array();
		var value ="";
		cart=getCookie('cart').split(",");
		for(var i = 0; i < cart.length; i++){//for
			if(getItem(cart[i]) != getItem(cartItem)){
				value=value+cart[i]+",";}
			}//for
		//remove the last comma
		if(value.length > 0){
			value = value.substring(0,value.length - 1);
			setCookie('cart', value);}
		else{setCookie('cart', "");}
		}	//if
		
	}//function

function addItem(cartItem){//function
	var cart = new Array();
	var value ="";
	var quantity = 0;
	var exists ="";
	var item1 = "";
	var item2 = "";
	if(cookieExists()){//if
		cart=getCookie('cart').split(",");
		for(var i = 0; i < cart.length; i++){
			if(getItem(cart[i]) == getItem(cartItem)){exists="true";}
			}
			if(exists!="true"){
				value = getCookie('cart')+","+cartItem;
				setCookie('cart', value);
				}
	} //if
	else{
		setCookie('cart', cartItem);
		}			
		
	window.open('cart/cart.php','','scrollbars=yes,menubar=no,height=500,width=700,resizable=yes,toolbar=yes,location=no,status=no');		
}//function

// when you apply this function to a item|quantity it will
// return the item without the quantity	
function getItem(itemAndQuantity){
	var itemQuantity = new Array();
	itemQuantity = itemAndQuantity.split("|")
	return itemQuantity[0];
}	

// when you apply this function to a item|quantity it will
// return the quantity without the item
function getQuantity(itemAndQuantity){
	var itemQuantity = new Array();
	itemQuantity = itemAndQuantity.split("|")
	return itemQuantity[1];
}	

function removeAndRefresh(cartItem){
	removeItem(cartItem);
	window.location= "cart2.php";
}

function updateCart(){
	if(cookieExists()){
		var cart = new Array();
		var value = "";
		var cartItem = "";
		var quantity = 1;
		cart=getCookie('cart').split(",");
		for(var i = 0; i < cart.length; i++){
			cartItem = getItem(cart[i]);
			quantity = Number(document.getElementById("txtQuantity"+cartItem).value);
			if(value==""){value = cartItem+"|"+quantity;}
			else{value += ","+cartItem+"|"+quantity;}
		}
		setCookie("cart", value);
		window.location= "cart2.php";
	}
}

function toCheckout(){
	window.location="checkout.html";	
}

function printRows(){
	var cart = new Array();
	var cartItem = "";
	var quantity = 1;
	cart=getCookie('cart').split(",");
	for(var i = 0; i < cart.length; i++){
		cartItem = getItem(cart[i]);
		quantity = getQuantity(cart[i]);
		document.write("<tr><td>");
		document.write(cartItem);
		document.write("<");

	}
}

function toLocation(location){
	window.location = location;
}

function toNewLocation(location){
	window.open(location, "mywindow", "")
}

function viewCart(){
	window.open('cart/cart.php','','scrollbars=yes,menubar=yes,height=500,width=700,resizable=yes,toolbar=yes,location=no,status=no');
}
/*
function popUp(location, wid, height){
	var width = parseInt(wid)+16; 
	window.open("externalFiles/popup.php?location="+location, "mywindow", "height="+height+",width="+width+"resizable=1,scrollbars=1");
}
*/

function popUp(title, url, width, height)  {
	newwindow2=window.open('',"windowname","toolbar=no,resizable=yes,scrollbars=no,width="+width+",height="+height+",left=50,top=50");
	var tmp = newwindow2.document;
	tmp.write('<html><head><title>');
	tmp.write(title);
	tmp.write('</title></head><body style="margin:0px; padding:0px;"><img src="');
	tmp.write(url);
	tmp.write('" />');
	tmp.write('</body></html>');
	tmp.close();
}
