//
// TODO
// This needs a elements cookie to record the number of products in the list.
// This wil make the getCookieArray loop until it has found all the cookies
//

GBBasket=function() {

	this.today=new Date();
	this.today.setTime(this.today.getTime() + (3600000));
	this.today=this.today.toGMTString();
	this.currentdate=new Date();
	this.currentdate=this.currentdate.toLocaleString();
	this.expdate=new Date()
	this.expdate.setDate(this.expdate.getDate()+5)
	this.productlist = new Array();
	
	// Add a value to the session
	this.init=function() {
//		return;
		var total = parseFloat(this.getCookie( "gbtotal"));
		this.productlist = this.getCookieArray("gbproduct");
		
		if($('#basket_total').length>0) {
			$('#basket_total')[0].innerHTML= parseFloat(total)
			$('#basket_total').formatCurrency({useHtml:true});
			$('#basket_total')[0].innerHTML= " Total " + $('#basket_total')[0].innerHTML
		}
	}
	
	// Add a value to the session
	this.addToBasket=function (pid, cost) {

	    this.productlist = this.getCookieArray("gbproduct");
	    if (this.productlist.length >= 10) {
	        alert("You can only buy 10 products");
	        return;
	    }
	    if (this.checkExists(pid)) {
	        alert("Product already in basket");
	        return;
	    }
	    this.setCookieArray("gbproduct", pid);
	    var total = parseFloat(this.getCookie("gbtotal"));
	    if (total > 0) {
	        total = total + parseFloat(cost);
	        this.setCookie("gbtotal", total, this.expdate);
	    } else {
	        total = parseFloat(cost);
	        this.setCookie("gbtotal", total, this.expdate);
	    }
	    
	    $('#BasketProductsCount').html(myFramework.basket.productlist.length+1);
	    this.init();
	}
	
	// Clears all values from the session
	this.emptyBasket=function( ) {
		// Confirm the deletion of the basket
		if ( confirm( "Are you sure you wish to empty your basket?" ) ) {
				this.delCookie( "gbproduct");
		}
		window.location="/ecom/basket";
		return;
	}
	
	this.getCookie=function(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 "";
	}
	
	// Setup the default values for the cookie
	this.setCookie=function(c_name,value,expiredays) {
		if(expiredays==undefined) {
		    expiredays=2;
		}
		
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+"; path=/";
	}
	
	this.delAllCookies=function() {
	    var new_date = new Date()
	    new_date = new_date.toGMTString()
	    var thecookie = document.cookie.split(";")
	    for (var i = 0;i < thecookie.length;i++) {
	        document.cookie = thecookie[i] + "; expires ="+ new_date
	    }
	}
	
	// Removes the cookies from the system therefore emptying the basket
	this.delCookie=function(name) {
		var expireNow = new Date();
		this.productlist = this.getCookieArray("gbproduct");
		document.cookie = "gbtotal=; expires="+expireNow+"; path=/";
		for( var x=0;x<this.productlist.length; x++) {
			document.cookie = name + x + "=; expires="+expireNow+"; path=/";
		}
	}
	
	// Remove a product from the shopping basket
	this.delProduct=function( value ) {
		var expireNow = new Date();
		this.productlist = this.getCookieArray("gbproduct");
		var items = this.productlist.length; // Need to know how many items where in the basket
			
		for( var x=0;x<=items; x++) {
		// Cookie value is x-1 as it starts at 0
		var cookie_x = x+1;

			if (this.getCookie("gbproduct" + x) == value ) {
				this.setCookie("gbtotal", 0.0, this.expdate  );
				document.cookie = "gbproduct" + cookie_x + "=; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/";
				this.resetCookies( x );
			}
		}
		this.init();
		window.location="/ecom/basket";
	}

	// Reset total
	this.recalcTotal=function( price ) {
		var total = parseFloat(this.getCookie( "gbtotal"));
		
		if ( total>=price ) {
			total = ( total - parseFloat(price) );
			this.setCookie("gbtotal", total, this.expdate  );
		} 
		
		this.init();
	}
	
	// Reorder the ADT
	// If you delete a cookie that is in the middle of the array you need to bring the other
	// cookies values in syc. This is a linklist ADT at its dirtiest. :)
	// x if the cookie value that was deleted, note that cookie values start at 0
	this.resetCookies=function( node ) {
		var expireNow = new Date();
		this.productlist = this.getCookieArray("gbproduct");
		for(x=node;x<this.productlist.length;x++) {
			document.cookie = "gbproduct" + x + "=" + this.productlist[x+1] + "; expires=" + this.expdate +  "; path=/";
		}

		document.cookie = "gbproduct" + x + "=; expires=" + expireNow +  "; path=/";	
	}
	
	this.setCookieArray=function(c_name,value,expiredays) {
		var length = this.getCookieArray(c_name).length+1;
		this.setCookie(c_name + length,value,expiredays);
	}
	
	this.getCookieArray=function(name) {
		var i = 1;
		var next = i+1;
		var cookie = this.getCookie(name + i);
		
		while (cookie != null && cookie!="" ) {
			// See if we can update any items on the page
			if( $('#gbproduct' + cookie).length>0) {
				$('#gbproduct' + cookie)[0].value="Item in basket";
				$('#gbproduct' + cookie)[0].onclick=function(){alert("Go to your basket to add mutiples or this product");}		
			}
	    	this.productlist[i-1] = this.getCookie(name + i);
	    	i++;
	    	cookie = this.getCookie(name + i);	    		
		}
		
		for(x=0;x<this.productlist.length;x++) {
		// Loop over all items
			if(this.getCookie(name + i) != null && this.getCookie(name + i)!="") {
				// Check if this is a gbproduct cookie and add to list, also update any items on the page
				if( $('#gbproduct' + i).length >0) {
					$('#gbproduct' + i)[0].value="Item in basket";
					$('#gbproduct' + i)[0].onclick=function(){alert("Go to your basket to add mutiples or this product.");}		
				}
			}
			
	    	i++;		
		}		
		
		return this.productlist;
	}
	
	// This method check to see if the product is already in the basket
	// If it is we do not what them to add it again
	this.checkExists=function( name ) {
		var expireNow = new Date();
		this.productlist = this.getCookieArray("gbproduct");
		var items = this.productlist.length; // Need to know how many items where in the basket
			
		for( var x=0;x<=items; x++) {
		// Cookie value is x-1 as it starts at 0
			if (this.productlist[x] == name ) {
				return true;
			}
		}
		return false;
	}
	
	// Test if the cookie is empty
	this.isSet=function( value ) {
		//alert ("Call to isSet");
		if ( value == "undefined" || value ==null )
			return false;
		else
			return true;
	}
	
	// Test if the basket is empty
	this.isEmpty=function( ) {
		this.productlist = this.getCookieArray("gbproduct");	
		if ( this.isSet(this.productlist.length)){
			return false;
		} else {
			return true;
		}
	}
	
	// GoTO URL
	this.gotoURL=function( arg ) {
		window.location=arg;
	}
		
	
	// A general testing function
	this.test=function( arg ) {
		alert( "Test function call : " + arg );
	}
	
}