
function move_to_cookie()
{
 var i = 0; 
 for (i=1;i<=items_ordered;i++){
	setCookie('bcol' + i, itemlist[i].artnum+'//'+itemlist[i].code+'//'+itemlist[i].price +'//'+ itemlist[i].quan +'//'+ itemlist[i].desc+'//'+ itemlist[i].ctva );
	
 }
 
 for (i=items_ordered + 1;i< maxarray;i++){
	deleteCookie('bcol' + i);
 }
}

function cookie_to_array()
{

 var i = 0
 var qte = 0; 
 var myString = '';
 for (i=1;i<maxarray;i++){
	myString = getCookie('bcol' + i);
	if ( myString != null) {
		tab = myString.split('//');
//		alert(tab);
		qte = parseInt(tab[3]);
		//qte = parseInt(tab[3]);
//		alert(qte);
		addnitem(qte,tab[0],tab[1],tab[2],tab[4],tab[5]);
		//addnitem(qte,tab[0],tab[1],tab[3],tab[4],tab[4]);
		//addnitem(qte,codes,prices,descrip,ctva)

	} else {
		i = maxarray;
	}	
 }
}

// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist

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));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

function open_basket() 
{
 this.folderFrame.location = 'show_basket.asp?langue='+current_language;
}

function item_tot_price(i)
{
 total_item_price = itemlist[i].price * itemlist[i].quan;
 return Math.round(total_item_price * 100)/100;
}

function all_order_totals()
{order_total = 0;
if (item_num > 0)
 {  
for (i =1;i < item_num;i++)
 { order_total = order_total + item_tot_price(i)}
 }
 return order_total;
}

function clear_all()
{
	basket_type=0;
	order_total =0;  
	item_num = 1;
	present_item = 1;
	items_ordered = 0;
	total_item_price = 0;
	initialize_arrays(itemlist);
//	move_to_cookie();	
	deleteCookie('comment');

}

function remove_nil_items()
{
 var i = 0; 
 var j = 1; 
 for (i=1;i<item_num;i++){
	if (itemlist[i].quan != 0){
		temp_array[j]=itemlist[i];
		items_ordered =j ;
		j=j+1;
	} else {
    } 
  } 
  itemlist = temp_array;
  item_num = items_ordered + 1;
}


function display_cookies()
{
 var i = 0; 
 var myString = '';
 for (i=1;i<maxarray;i++){
	myString = getCookie('bcol' + i);
	if ( myString != null) {
		alert(myString);
	} else {
		i = maxarray;
	}	
 }
}

function item_quan(artnum)
{
var loc = check_if_in(artnum)
if (loc > 0)
 var quantities = itemlist[loc].quan
else
 var quantities = "";
return quantities
}

function createArray(n)
//n		size of array
//init	what you want all values initialized to
{               this.length = n
		var i = 0
		for (i = 1 ; i < n ; i++) 
			this[i] = null;	
                return this
}

function format_dec(val, post)
{
    var decpoint;
    var begin;
    var end;
    var valstr;
    var temp_char;

    valstr = "" + val;
    decpoint = valstr.indexOf(".")
    if (decpoint != -1) {
        begin = valstr.substring(0,decpoint);
        end = valstr.substring(decpoint+1,valstr.length);
    }
    else {
        begin = valstr;
        end = "";
    } 
	if (end.length < post)
	 {while (end.length < post)
	    {
        end += "0";
        }
	 }
	end = end.substring(0,post);
    return (begin+"."+end);
}

function product(artnum,code,price,desc,quan,ctva)
{ this.price = 0;
  this.artnum = artnum;
  this.code = code;
  this.desc = desc;
  this.quan = quan;
  this.ctva = ctva;
  this.price = format_dec(price,2);
  return this;
}

function initialize_arrays(arraysa)
{
	for (i = 1;i < maxarray;i++)
	{
		arraysa[i] = new product('','',0,'',0,'')
	}
}

function check_if_in(code_check) // this works
{
var i = 1;
while (i < item_num)
{
  if (itemlist[i].artnum == code_check) return i;
  i = i + 1;
}
return -1;
}

function addnitem(qte,artnum,codes,prices,descrip,ctva)
{
loc = check_if_in(artnum);
if (loc != -1){
  // update existing item
	itemlist[loc] = new product(artnum,codes,prices,descrip,qte,ctva);
} else { // new item
	if (qte > 0) {
	    itemlist[item_num] = new product(artnum,codes,prices,descrip,qte,ctva);
		items_ordered = item_num;
		item_num = item_num + 1;
	}	
}
remove_nil_items()
}

function additem(artnum,codes,prices,descrip,ctva)
{
// adds another item to a variable length array
// remember to do it via new product()
loc = check_if_in(artnum)
// present_item = item_num
//last_item = item_num;
//alert('item_num = ' + item_num)

if (loc != -1){
  // update existing item
  olditem =  itemlist[loc].quan
//  alert(' localisation is before oldvalue ' + loc);
//  alert('olditem quantity is ' + olditem);
  itemlist[loc] = new product(artnum,codes,prices,descrip,olditem + 1,ctva)
} else {// new item
//  alert('nouveau produit')
  olditem =  itemlist[item_num].quan
  itemlist[item_num] = new product(artnum,codes,prices,descrip,olditem + 1,ctva);
  items_ordered = item_num
  item_num = item_num + 1
}
remove_nil_items()
}

function subitem(artnum,codes,prices,descrip,ctva)
{
loc = check_if_in(artnum)
if ((loc != -1) && (itemlist[loc].quan > 0)) {
  // update existing item
  olditem =  itemlist[loc].quan
  //alert(' loc is before oldvalue ' + loc);
  //alert('olditem is ' + olditem);
  itemlist[loc] = new product(artnum,codes,prices,descrip,olditem - 1,ctva)}
  remove_nil_items()
}



