﻿function encode_utf8( s ){  return unescape( encodeURIComponent( s ) );}
function decode_utf8( s ){  return decodeURIComponent( escape( s ) );}


function encodeUTF8(string) {
	string = string.replace(/\r\n/g,"\n");
	var utftext = "";

	for (var n = 0; n < string.length; n++) 
	{
		var c = string.charCodeAt(n);

   	if (c < 128) {
   		utftext += String.fromCharCode(c);
   	}
   	else
   	{
   		if((c > 127) && (c < 2048)) 
   		{
   			utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}	
	}
	return utftext;
}

function decodeUTF8 (utftext) {
	var string = "";
	var i = 0;
	var c = c1 = c2 = 0;
	
	while ( i < utftext.length ) {
		c = utftext.charCodeAt(i);
		
		if (c < 128) {
			string += String.fromCharCode(c);
			i++;
		} else {
			if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			} else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
	}
	return string;
}




// 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 + "=" + encodeURIComponent(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
 // alert(encodeURIComponent (value));
}

// 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 decodeURIComponent(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 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);
	document.getElementById('title2').innerHTML = '<iframe frameborder="no" width="100%" height="100%" scrolling="no" allowtransparency="true" src="sumup.asp"></iframe>';
	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,data,etat,type,qtemini,qteprix)
{ this.price = 0;
  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 = '';};
  if(etat) { this.etat = etat;} else {this.etat = '0';};
  if(type) { this.type = type;} else {this.etat = '1';};
  if(qtemini) { this.qtemini = qtemini;} else {this.qtemini = 0;};
  if(qteprix) { this.qteprix = qteprix;} else {this.qteprix = '';};
  
  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,data,etat,type,qtemini,qteprix)
{

	loc = check_if_in(artnum);
	if (loc != -1){
	  // update existing item
	  	itemlist[loc] = new product(artnum,codes,prices,descrip,qte,info,data,etat,type,qtemini,qteprix);
	} else { // new item
		if (qte > 0) {
		    itemlist[item_num] = new product(artnum,codes,prices,descrip,qte,info,data,etat,type,qtemini,qteprix);
			items_ordered = item_num;
			item_num = item_num + 1;
		}	
	}
	remove_nil_items();
	document.getElementById('title2').innerHTML = '<iframe frameborder="no" width="100%" height="100%" scrolling="no" allowtransparency="true" src="sumup.asp"></iframe>';
	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].etat , itemlist[i].type , itemlist[i].qtemini , itemlist[i].qteprix );
		}
		items_ordered = j ;
		j = j + 1;
	} 
		
	itemlist = temp_array;
	item_num = items_ordered + 1;

	document.getElementById('title2').innerHTML = '<iframe frameborder="no" width="100%" height="100%" scrolling="no" allowtransparency="true" src="sumup.asp"></iframe>';
	if (db_frame) {
		db_frame.refresh_basket();
	}
}
