



// ---------------- add_productstring ----------------------------------------------------------
function add_productstring(productstring,pagetodisplay) {
//$PCODE|$COST|$LBS|$DESCRIPTION|qtytoadd
var product_to_add_array=productstring.split("|");

var code = product_to_add_array[0];
var cost = product_to_add_array[1];
var lbs = product_to_add_array[2];
var desc = product_to_add_array[3];
var qtytoadd = product_to_add_array[4];


//alert ('code: ' + code + ' cost: ' + cost + ' lbs ' + lbs + ' desc:' + desc + ' qty:' + qtytoadd);

// -- get currentproducts ---
var productsthatexist = getCookie('products');
//alert ('productsthatexist:' + productsthatexist);
if (productsthatexist){
productsthatexist = productsthatexist + '~' + productstring;  // add this product to the end.
}
else{
productsthatexist = productstring;
}


deleteCookie('products'); // delete the cookie.  We're going to replace it.

DisableForm(document.productform);
set_products_cookie(productsthatexist);
alert ('Product Added to Cart');
location = pagetodisplay;
}  // ============================================= end of add_productstring ========================


// ------------------------- function: remove_productstring ------------------------
function remove_productstring(productstring) {
//$PCODE|$COST|$LBS|$DESCRIPTION|$QTY - this is what we need to delete from the products cookie
var productsthatexist = getCookie('products');

// split products
var different_products_array=productsthatexist.split("~");

// rebuild productsthatexist without this productstring
productsthatexist = '';
var removed = 'no';
for(var i=0;i < different_products_array.length;i++){
	if ((different_products_array[i] == productstring) && (removed == 'no')){
	removed = 'yes';
	}
	else{
		if (productsthatexist.length > 0){
			productsthatexist = productsthatexist + '~' + different_products_array[i] 
	
		}
		else{
		productsthatexist = different_products_array[i];
		}
	}
		
}
DisableForm(document.checkoutform);
set_products_cookie(productsthatexist);
location = "/cgi-bin/checkout.cgi"
} // ====================== end of function: remove_productstring =================






// ------------------------- function: replace_productstring ------------------------
function replace_productstring(oldproductstring,newproductstring) {
//$PCODE|$COST|$LBS|$DESCRIPTION|$QTY - this is what we need to delete from the products cookie
var productsthatexist = getCookie('products');


// split products
var different_products_array=productsthatexist.split("~");

// rebuild productsthatexist - replace oldproductstring with newproductstring
productsthatexist = '';
var replaced = 'no';
for(var i=0;i < different_products_array.length;i++){
	if ((different_products_array[i] == oldproductstring) && (replaced == 'no')){
		replaced = 'yes';
		if (productsthatexist.length > 0){
			productsthatexist = productsthatexist + '~' + newproductstring
	
		}
		else{
		productsthatexist = newproductstring;
		}
	}
	else{
		if (productsthatexist.length > 0){
			productsthatexist = productsthatexist + '~' + different_products_array[i] 
		}
		else{
		productsthatexist = different_products_array[i];
		}
	}
		
}
DisableForm(document.checkoutform);
set_products_cookie(productsthatexist);
location = "/cgi-bin/checkout.cgi"
} // ====================== end of function: replace_productstring =================










//function replace_cart_cookie(name, qty, value) {
//var exp = new Date();
//var days = 1;
//exp.setTime(exp.getTime() + days*24*60*60*1000);
//var expDate = exp.toGMTString();
//value = qty + '|' + value;
//path = "/";
//domain = '';
//secure = '';
//document.cookie= name + "=" + escape(value) +
//((expDate) ? "; expires=" + expDate : "") +
//((path) ? "; path=" + path : "") +
//((domain) ? "; domain=" + domain : "") +
//((secure) ? "; secure" : "");
//DisableForm(document.checkoutform);
//location = "/cgi-bin/checkout.cgi"
//}
















function DisableForm(xForm){
  objElems = xForm.elements;
  for(i=0;i<objElems.length;i++){
    objElems[i].disabled = true;
  }
}




function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}




function deleteCookie(name, path, domain) {
path = "/";    
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}




function run_script(scripttorun) {
location = scripttorun;
}




// -------------------------------------------------------------------------------
function empty_product_cookies(){

deleteCookie('products'); // delete the cookie.  

DisableForm(document.checkoutform);
location = "/cgi-bin/checkout.cgi"
}  // end of empty_product_cookies ===============================================



function empty_and_submit(thisform) {

empty_product_cookies();
DisableForm(document.checkoutform);
return true;
}  // ============ end of function: empty_and_submit ==================




function trimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}



// ----------------------- function: set_products_cookie ---------------------------
function set_products_cookie(productsthatexist){
var exp = new Date();
var days = 1;
exp.setTime(exp.getTime() + days*24*60*60*1000);
var expDate = exp.toGMTString();
setCookie('products',productsthatexist,expDate);
}




function setCookie(name, value, expDate, path, domain, secure) {

path = "/";
document.cookie= name + "=" + escape(value) +
((expDate) ? "; expires=" + expDate : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}


function openpopup(popurl){
var winpops=window.open(popurl,"","width=480,height=400,status,resizable,scrollbars")
}


// all functions below may not bee needed....







































// travis function:
function get_products() {
var nameEQ = name + "=";
var ca = document.cookie.split(';');

for(var i=0;i < ca.length;i++)
{
	var c = ca[i];
	c = trimString(c);
	if (c == 'products')
		{
		 //alert (c);
		}
	while (c.charAt(0)==' ') c = c.substring(1,c.length);
	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;

}


























// travis function: set_candle_cookie ----------------------------------------------------------
function set_candle_cookie(recordcount,productstring) {
//$PCODE|$COST|$LBS|$DESCRIPTION
var code = productstring.substring(0,4);
var productname = 'productstring_' + code;
productstring = productstring.substring(5);

// get the quantity from the correct productqtyX field -------
var qty;
var fieldname = 'productqty' + recordcount;
if (document.images)
{
	for (i=0;i<document.productform.length;i++)
	{
		var tempobj=document.productform.elements[i]
		if (tempobj.name == fieldname)
		{
			qty = tempobj.value
		} // end of if tempobj.name is pcode
	}  // end of for through all elements
}  // end of if document.images
// =======================================================

productstring = qty + '|' + productstring;

var exp = new Date();
var days = 1;
exp.setTime(exp.getTime() + days*24*60*60*1000);
var expDate = exp.toGMTString();
//alert('productname:' + productname + 'productstring: ' + productstring);
DisableForm(document.productform);
setCookie(productname,productstring,expDate);
location = "/cgi-bin/candles.cgi"
}  // ============================================= end of set_candle_cookie ========================












// travis function: set_jelly_cookie ----------------------------------------------------------
function set_jelly_cookie(recordcount,productstring) {
//$PCODE|$COST|$LBS|$DESCRIPTION
var code = productstring.substring(0,4);
var productname = 'productstring_' + code;
productstring = productstring.substring(5);

// get the quantity from the correct productqtyX field -------
var qty;
var fieldname = 'productqty' + recordcount;
if (document.images)
{
	for (i=0;i<document.productform.length;i++)
	{
		var tempobj=document.productform.elements[i]
		if (tempobj.name == fieldname)
		{
			qty = tempobj.value
		} // end of if tempobj.name is pcode
	}  // end of for through all elements
}  // end of if document.images
// =======================================================

productstring = qty + '|' + productstring;

var exp = new Date();
var days = 1;
exp.setTime(exp.getTime() + days*24*60*60*1000);
var expDate = exp.toGMTString();
//alert('productname:' + productname + 'productstring: ' + productstring);
DisableForm(document.productform);
setCookie(productname,productstring,expDate);
location = "/cgi-bin/jellies.cgi"
}  // ============================================= end of set_jelly_cookie ========================









// travis function:
function set_prod_cookie(name, qtyvalue, weightvalue, costvalue, descvalue) {
var exp = new Date();
var days = 1;
exp.setTime(exp.getTime() + days*24*60*60*1000);
var expDate = exp.toGMTString();
setCookie(name,qtyvalue + '|' + costvalue + '|' + weightvalue + '|' + descvalue,expDate);
//setCookie(weightname,weightvalue,expDate);
}



// travis function:
function get_all_prod_cookies() {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++)
{
	var c = ca[i];
	c = trimString(c);
	if (c.substring(0,13) == 'productstring')
	
		{
		 alert (c);
		}
	while (c.charAt(0)==' ') c = c.substring(1,c.length);
	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;

}






// -------------------------------------------------------------------------------
function update_zip_cookie(value) {
if (isValidZipCode(value)){

var name='zip';
var exp = new Date();
var days = 1000;
exp.setTime(exp.getTime() + days*24*60*60*1000);
var expDate = exp.toGMTString();

path = "/";
domain = '';
secure = '';

document.cookie= name + "=" + escape(value) +
((expDate) ? "; expires=" + expDate : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
DisableForm(document.checkoutform);
location = "/cgi-bin/checkout.cgi"
} // end of if isValidZipCode(value)
else{
alert(value + ' is not a valid zip code.');
}
}
// ======================= end of update_zip_cookie ============================

function isValidZipCode(value) {
   var re = /^\d{5}([\-]\d{4})?$/;
   return (re.test(value));
}







function delete_cart_cookie(name){
deleteCookie(name);
DisableForm(document.checkoutform);
location = "/cgi-bin/checkout.cgi"
}



