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);
	title_set_basket();

	if (db_frame) {
		db_frame.refresh_basket();
	}
}

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,info,node,url,data)
{ this.price = 0;
  this.node = node;
  this.url = url;
  this.artnum = artnum;
  this.code = code;
  this.desc = desc;
  this.quan = quan;
  this.info = info;
  this.price = format_dec(price,2);
  if(data) { this.data = data;} else {this.data = '';};
  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,info,node,url,data)
{
	loc = check_if_in(artnum);
	if (loc != -1){
	  // update existing item
	  	itemlist[loc] = new product(artnum,codes,prices,descrip,qte,info,node,url,data);
	} else { // new item
		if (qte > 0) {
		    itemlist[item_num] = new product(artnum,codes,prices,descrip,qte,info,node,url,data);
			items_ordered = item_num;
			item_num = item_num + 1;
		}	
	}
	remove_nil_items();
	title_set_basket();

	if (db_frame) {
		db_frame.refresh_basket();
	}
}

function modifynitem(artnum,qte)
{
	loc = check_if_in(artnum);
	if (loc != -1){
	  	itemlist[loc].quan = qte * 1;
		remove_nil_items();
		title_set_basket();
		if (db_frame) {
			db_frame.refresh_basket();
		}
	}
}

function duplicateitem(artnum)
{
	var i = 0; 
	var j = 1; 
	var curdate;
	var myCode = "";
	var myTab;
	var myString = "";

	
	remove_nil_items();
	
	for (i=1;i<item_num;i++){
		temp_array[j] = itemlist[i];
		items_ordered = j ;
		
		if (itemlist[i].artnum  == artnum){
			myCode = itemlist[i].artnum;
			myTab = myCode.split('-');
			myCode = myTab[0];
			curdate = new Date();
			myString = curdate.getHours().toString() + curdate.getMinutes().toString() + curdate.getSeconds().toString();
			j = j + 1;
			temp_array[j] = new product( myCode + '-' + myString , itemlist[i].code , itemlist[i].price , itemlist[i].desc,itemlist[i].quan , itemlist[i].info ,'',itemlist[i].node, itemlist[i].url);
		}
		items_ordered = j ;
		j = j + 1;
	} 
		
	itemlist = temp_array;
	item_num = items_ordered + 1;
	title_set_basket();
	if (db_frame) {
		db_frame.refresh_basket();
	}

}







