var comArray = Array();
function delItem(no){
	if(comArray[no][1] > 1){
		comArray[no][1] -= 1;
	}else{
		comArray.splice(no,1);
	}
	showCommande();
}

function addItem(id,nb,name,prix,allname){
	var ifExist = false;
	var noItem = null;
	for(x=0;x<comArray.length;x++){
		if(comArray[x][0] == id){
			ifExist = true;
			noItem = x;
		}
	}
	if(ifExist === true && noItem != null){
		comArray[noItem][1] += 1;
	}else{
		var thisItem = Array(id,nb,name,prix,allname);
		comArray.push(thisItem);
	}
	showCommande();
}

function showCommande(){
	var comHTML = '<table cellpadding="0" cellspacing="0" border="0" style="width:220px;">';
	var className = 'dark';
	for(x=0;x<comArray.length;x++){
		comHTML += '<tr class="'+className+'">';
			comHTML += '<td width="20">'+comArray[x][1]+'</td>';
			comHTML += '<td>'+comArray[x][2]+'</td>';
			comHTML += '<td width="50" align="right">'+comArray[x][3]+'$</td>';
			comHTML += '<td width="20" align="right"><img src="img/commande/iconMoin.gif" border="0" onClick="delItem('+x+')"></td>';
		comHTML += '</tr>';
		if(className == 'dark'){
			className = 'light';
		}else{
			className = 'dark';
		}
	}
	comHTML += '</table>';
	document.getElementById('comList').innerHTML = comHTML;
	calculatePrice();
}

function calculatePrice(){
	var totPrice = 0;
	for(x=0;x<comArray.length;x++){
		var thisPrice = 0;
		thisPrice = comArray[x][1]*comArray[x][3];
		totPrice += thisPrice;
	}
	document.getElementById("boxPrixTot").innerHTML = arrondir(totPrice)+' $';
	var tps = totPrice/100*6;
	var totPriceTps = totPrice + tps;
	var tvq = totPriceTps/100*7.5;
	var totPriceTaxe = totPriceTps+tvq;
	//document.getElementById("boxPrixTaxe").innerHTML = arrondir(totPriceTaxe)+' $';
}
function arrondir(resultat) {
      resultat = Math.round(resultat*100)/100;
      return resultat.toFixed(2);
}
function printCommande(){
	var HTMLpage = document.getElementById('comForPrint').innerHTML;
	var HTMLpage2 = document.getElementById('comForPrint2').innerHTML;
	printPopUp = window.open('','_blank',"fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=400,height=600,left=0,top=0");
	printPopUp.document.open();
	printPopUp.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link rel="stylesheet" type="text/css" href="../../imports/css/se_styles_menu.css"><link rel="stylesheet" type="text/css" href="../../imports/css/se_styles.css"><title>Commande</title></head><body><div class="divPrint"><br><br><img src="../../imports/images/logo_sushiexpress.gif">');
	printPopUp.document.write('<div style="padding:15px;">'+HTMLpage);
	printPopUp.document.write(HTMLpage2+'</div>');
	printPopUp.document.write('</div></body></html>');
	printPopUp.document.close();
	printPopUp.print();
}
function showCommandeTClean(){
	var comHTML = '<table cellpadding="0" cellspacing="0" border="0" style="width:220px;">';
	var className = 'dark';
	for(x=0;x<comArray.length;x++){
		comHTML += '<tr class="'+className+'">';
			comHTML += '<td width="20">'+comArray[x][1]+'</td>';
			comHTML += '<td>'+comArray[x][4]+'</td>';
			comHTML += '<td width="50" align="right">'+comArray[x][3]+'$</td>';
		comHTML += '</tr>';
		if(className == 'dark'){
			className = 'light';
		}else{
			className = 'dark';
		}
	}
	comHTML += '</table>';
	document.getElementById('comList').innerHTML = comHTML;
	calculatePrice();
}
function getCmdAndSend(){
	/*VALIDATION DES CHAMP*/
	var send = true;
	var msg = '';
	if(document.getElementById('name').value==""){
		send = false;
		msg += 'Champ nom vide'+"\n";
	}
	if(document.getElementById('prenom').value==""){
		send = false;
		msg += 'Champ prenom vide'+"\n";
	}
	if(document.getElementById('email').value==""){
		send = false;
		msg += 'Champ courriel vide'+"\n";
	}
	if(document.getElementById('adresse').value==""){
		send = false;
		msg += 'Champ adresse vide'+"\n";
	}
	
	if(send == true){
		showCommandeTClean();
		var HTMLpage = document.getElementById('comForPrint').innerHTML;
		document.getElementById('cmd').value = HTMLpage;
		document.formCmd.submit();
	}else{
		alert(msg);
	}
}

function centerDesign() {
  var eleWidth = 763, eleHeight = 608;
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  if(myWidth > eleWidth){
  	var thisLeft = (myWidth-eleWidth)/2;
	document.getElementById('all').style.left = thisLeft+'px';
  }
  if(myHeight > eleHeight){
  	var thisTop = (myHeight-eleHeight)/2;
	document.getElementById('all').style.top = thisTop+'px';
  }
}
