function isNumeric(sText,decimals,negatives) {
	var isNumber=true;
	var numDecimals = 0;
	var validChars = "0123456789";
	if (decimals)  validChars += ".";
	if (negatives) validChars += "-";
	var thisChar;
	for (i = 0; i < sText.length && isNumber == true; i++) {  
		thisChar = sText.charAt(i); 
		if (negatives && thisChar == "-" && i > 0) isNumber = false;
		if (decimals && thisChar == "."){
			numDecimals = numDecimals + 1;
			if (i==0 || i == sText.length-1) isNumber = false;
			if (numDecimals > 1) isNumber = false;
		}
		if (validChars.indexOf(thisChar) == -1) isNumber = false;
	}
	return isNumber;
}

function get_cookies_array() {

    var cookies = { };

    if (document.cookie && document.cookie != '') {
        var split = document.cookie.split(';');
        for (var i = 0; i < split.length; i++) {
            var name_value = split[i].split("=");
            name_value[0] = name_value[0].replace(/^ /, '');
            cookies[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);
        }
    }

    return cookies;
   
}


jQuery.noConflict();
jQuery(document).ready(function(){
	jQuery("a.parts_entry_add").click(function(){
	
		newval = "1";
		
		var cookie_name = jQuery(this).attr('rel');
		var cookie_name_name = jQuery(this).attr('rel')+"_product";
		var cookie_product = jQuery(this).attr('title'); 
		
		if ((jQuery.cookie(cookie_name)==null) || (jQuery.cookie(cookie_name)==""))
		{
		//Value is Null, so we're setting it for the first time
		
		jQuery.cookie(cookie_name,"1",{path: '/'})
		jQuery.cookie(cookie_name_name,cookie_product,{path: '/'})
		
		}
		else
		{
		//Value is not null, so we're adding one to the existing value
		var newval = parseInt(jQuery.cookie(cookie_name))+1;
				
		jQuery.cookie(cookie_name,newval,{path: '/'})
		}
		
		jQuery("span[rel="+cookie_name+"]").html(newval+" in basket");
		
		//alert ( jQuery.cookie(cookie_name) );
		return false;
	})
	
	jQuery("a.parts_entry_subtract").click(function(){
	
		newval = "1";
		
		var cookie_name = jQuery(this).attr('rel');
		
		if ((jQuery.cookie(cookie_name)==null) || (jQuery.cookie(cookie_name)=="1"))
		{
		jQuery.cookie(cookie_name,null,{path: '/'})
		jQuery("span[rel="+cookie_name+"]").html("Order Item");
		}
		else
		{
		//Value is not null, so we're adding one to the existing value
		var newval = parseInt(jQuery.cookie(cookie_name))-1;
				
		jQuery.cookie(cookie_name,newval,{path: '/'});
		
		jQuery("span[rel="+cookie_name+"]").html(newval+" in basket");
		}
		
		//alert ( jQuery.cookie(cookie_name) );
		return false;
	})
	
	var cookies = get_cookies_array();
	for(var name in cookies){
		jQuery("span[rel="+name+"]").html(cookies[name]+" in basket");
		//alert(name);
	}
	
	jQuery("input#complete_checkout").click(function(){ //Destroys the Cookies when you complete checkout
		var cookies = get_cookies_array();
		
		for(var name in cookies){
			
			if (isNumeric(name)){
				jQuery.cookie(name,null,{path: '/'});
				jQuery.cookie(name+'_product',null,{path: '/'});
				//alert('Cookie '+name+' Setting to null');
			}

		}
	})
	
	jQuery("a#logmeout").click(function(){ //Destroys the Cookies when you log out
		var cookies = get_cookies_array();
		
		for(var name in cookies){
			
			if (isNumeric(name)){
				jQuery.cookie(name,null,{path: '/'});
				jQuery.cookie(name+'_product',null,{path: '/'});
				//alert('Cookie '+name+' Setting to null');
			}

		}
	})
});

function loadcheckout()
{
	var cookies = get_cookies_array();
	var prod_data  = "";
	
	for(var name in cookies){
		
		if (isNumeric(name)){
			jQuery('#checkout').append("<tr><td>["+name+"]</td><td>"+jQuery.cookie(name+'_product')+"</td><td>"+cookies[name]+"</td></tr>");
			var prod_data = prod_data + '[' + jQuery.cookie(name+'_product') + '] x ' + cookies[name] + '<br />';
		}
		
		//jQuery("span[rel="+name+"]").html(cookies[name]+" in basket");
		//alert(name);
	}
	
	jQuery("input#productdata").val(prod_data);
	
	//alert ( jQuery('input#productdata').val() );
}


