// (C)2008 addworks

var $ = function(id) {
	return document.getElementById(id);
};
var iif = function(bool, iftrue, iffalse) {
	if (bool)
		return iftrue;
	else
		return iffalse;
};
var blank = function(elt) {
	window.open(elt.href, "_blank");
	return false;
}

function arr_intersect(a, b)
{
	var inter = new Array();
	var i, j;
	for (i = a.length - 1; i >= 0; i--) {
		for (j = b.length - 1; j >= 0; j--) {
			if (a[i] == b[j]) {
				inter[inter.length] = b[j];
				break;
			}
		}
	}
	return inter;
}

function openHelpWindow( xURL )
{
    var win = null
    win = window.open( xURL , 'HELP', "width=395,height=345,resizable=yes,scrollbars=yes,status=yes");
    win.focus();
}

function setReadonly(obj, readonly) {
	var myClass;
	if (readonly) {
		if (arguments[3])
			myClass = arguments[3];
		else
			myClass = 'readonly';
		obj.setAttribute('readonly','readonly');
	}
	else {
		if (arguments[2])
			myClass = arguments[2];
		else
			myClass = 'select';
		obj.removeAttribute('readonly');
	}
	obj.className = myClass;
	//obj.disabled = readonly;
}

function my_updatecombo(obj, tab, newtab) {
	var val = false;
	var i;
	if (!tab || !tab["value"] || !tab["value"].length) { /*alert("Erreur dans "+obj.id+":2\nMerci de rafraichir/actualiser la page !");*/ return; }
	if (!newtab || !newtab.length) { alert("Erreur dans "+obj.id+":3\nMerci de rafraichir/actualiser la page !"); return; }
	if (obj.selectedIndex > -1)
		val = obj.options[obj.selectedIndex].value;
	for (i = obj.length - 1; i > 0; i--)
		obj.options[i] = null;
	for (i = 0; i < newtab.length; i++) {
		for (var j=0; j<tab["value"].length; j++) {
			if (tab["value"][j].toUpperCase() == newtab[i].toUpperCase()) {
				EW_newopt(obj, tab["value"][j], tab["text"][j], "");
				break;
			}
		}
		if (j == tab["value"].length)
			EW_newopt(obj, newtab[i], newtab[i], "");
	}
	if (newtab.length == 1) {
		val = obj.options[obj.length-1].value;
		setReadonly(obj, true);
	}
	else
		setReadonly(obj, false);

	EW_selectopt(obj, val);
}


function storeChange() {
	if(navigator.appName.indexOf("Microsoft Internet Explorer") != -1){
		for (i=0; i < document.fdevis_impadd.elements.length ; i++) {
			document.fdevis_impadd.elements[i].attachEvent("onchange",event_onchange);
		}
	}
	else if(navigator.appName.indexOf("Netscape") != -1 || navigator.appName.indexOf("Mozilla Firefox") != -1) {
		if (document.addEventListener)
			document.addEventListener('change',event_onchange,true);
		else {
			document.captureEvents(Event.CHANGE);
			document.onchange=event_onchange;
		}
	}
}

/*
TODO: Remplacer par : devisStep(field, display)
#function getdisplay(field) {
	if(document.forms['fdevis_impadd'].elements[field].value == "")
		return "none";
	return "block";
}
TODO: Remplacer par : devisStep(field, display, def)
#function getdisplay2(field,def) {
	if(document.forms['fdevis_impadd'].elements[field].value == ""
		|| document.forms['fdevis_impadd'].elements[field].value == def)
		return "none";
	return "block";
}
*/

function devisStep(field, display /*[ , def ]*/ )
{
	var def = '';
	if (arguments[2])
		def = arguments[2];
	if (display == 'clean' || display == 'clear')
	{
		document.forms['fdevis_impadd'].elements['x_' + field].value = def;
		display = false;
	}
	if (!display)
	{
		$('r_' + field).style.display = 'none';
		return false;
	}
	$('r_' + field).style.display = 'block';
	var val = document.forms['fdevis_impadd'].elements['x_' + field].value;
	if (val == '' || val == def)
		return false;
	return true;
}

function iif(condition, if_true, if_false)
{
	if (condition)
		return if_true;
	return if_false;
}

function displaychoice(choice1,choice2){
	if(choice2 == 'true')
		$(choice1).style.display = 'block';
	else if(choice2 == 'false')
		$(choice1).style.display = 'none';
	else if(document.forms['fdevis_impadd'].elements[choice2].value != "" && document.forms['fdevis_impadd'].elements[choice2].value != "0")
		$(choice1).style.display = 'block';
	else
		$(choice1).style.display = 'none';	
}

function setchoice(choice,val){
	document.forms['fdevis_impadd'].elements[choice].value = val;
}

function listvalue(choice){
	return(document.forms['fdevis_impadd'].elements[choice].value);
}

function okchoice(choice){
	if(document.forms['fdevis_impadd'].elements[choice].value != ""
				&& document.forms['fdevis_impadd'].elements[choice].value != "0")
	return true;
return false
}

function fdevis_finalise(displayNext)
{
	var simple = false;
	var encombrant = false;
	var offset = false;
	if (arguments[1]) {
		switch (arguments[1]) {
			case 'simple' :
				simple = true; // Pas de Delai & Livraison
			break;
			case 'encombrant' :
				encombrant = true;
			break;
			case 'J+1' : case 'J+2' : case 'J+3' : case 'J+4' : case 'J+5' :
			case 'J+6' : case 'J+7' : case 'J+8' : case 'J+9' : case 'J+10' :
				offset = arguments[1];
			break;
		}
	}

	// LIVRAISON
	//var ar_x_LivType = new Array('livagence', 'livcoursier', 'livxpress', 'livcoli', 'livcoliom', 'livcolinter');
	//var ar_x_LivTypeEncombrant = new Array('livagence', 'livcoursier');
	//var ar_x_LivTypeOffset = new Array('livagence', 'livoffset');
	var ar_x_DelaiRealisationStd = new Array("Standard","Preciser");
	var ar_x_DelaiRealisationOff = new Array(offset);

	if ($('x_Livraison').value == "livcolinter" && $('x_PaysInter').value == 'mc') {
		$('x_Livraison').value = "livxpress";
		$('x_PaysXpress').value = 'mc';
		alert('Monaco est desservi par Chronopost.');
	}

	// Initialisation des variables
	var delai = document.forms['fdevis_impadd'].elements['x_DelaiRealisation'].value;
	var livraison = document.forms['fdevis_impadd'].elements['x_Livraison'].value;
	var ville = $('x_Ville').value;
	var pays = $('x_Pays').value;
	var pointVente = '';

	//alert(delai);
	switch (livraison)
	{
		case 'livparis8' :
			pointVente = 'MALESHERBES';
			break;
		case 'livmval' :
			pointVente = 'LAGNY';
			break;
		default :
			pointVente = 'HORLOGE';
	}
	$('x_PointVente').value = pointVente;

	// Initialistion des listes de choix
	if (offset) {
		my_updatecombo(document.forms['fdevis_impadd'].elements['x_DelaiRealisation'], this.TabDelaiRealisation, ar_x_DelaiRealisationOff);
		//my_updatecombo(document.forms['fdevis_impadd'].elements['x_Livraison'], this.TabLivraison, ar_x_LivraisonOffset);
	} else {
		my_updatecombo(document.forms['fdevis_impadd'].elements['x_DelaiRealisation'], this.TabDelaiRealisation, ar_x_DelaiRealisationStd);
		//if (encombrant) { my_updatecombo(document.forms['fdevis_impadd'].elements['x_Livraison'], this.TabLivraison, ar_x_LivraisonEncombrant); } else { my_updatecombo(document.forms['fdevis_impadd'].elements['x_Livraison'], this.TabLivraison, ar_x_Livraison); }
	}
	$('h_livagence').style.display = 'block'; // Choix toujours disponible
	$('h_livcoursier').style.display = iif(offset, 'none', 'block'); // Accepte les encombrants
	$('h_livxpress').style.display = iif(offset || encombrant, 'none', 'block');
	//$('h_livcoli').style.display = iif(offset || encombrant, 'none', 'block');
	$('h_livcoli').style.display = 'none'; // Choix inactif pour l'instant
	$('h_livcoliom').style.display = iif(offset || encombrant, 'none', 'block');
	$('h_livcolinter').style.display = iif(offset || encombrant, 'none', 'block');
	$('h_livoffset').style.display = iif(offset, 'block', 'none'); // Choix Offset uniquement

	// Enchainement
	var livtype = '';
	if (simple) {
		devisStep('DelaiRealisation', 'clean');
		devisStep('DateRealisation', 'clean');
		devisStep('TrancheHoraire', 'clean');
		document.forms['fdevis_impadd'].elements['x_Livraison'].value = '';
		$('allLivraison').style.display = 'none';
		devisStep('Arrondissement', 'clean');
		devisStep('Zone', 'clean');
		livtype = 'simple';
	} else {
		displayNext = devisStep('DelaiRealisation', displayNext);
		if (offset || delai == "Standard") {
			devisStep('DateRealisation', 'clean');
			devisStep('TrancheHoraire', 'clean');
		} else {
			document.forms['fdevis_impadd'].elements['x_DateRealisation'].readOnly = true; // Pour interdire la saisie de la date
			if ($("x_DateRealisation").value == "")
				$("x_DateRealisationLabel").innerHTML = 'Cliquez sur le calendrier';
			displayNext = devisStep('DateRealisation', displayNext);
			displayNext = devisStep('TrancheHoraire', displayNext);
		}
		$('allLivraison').style.display = iif(displayNext, 'block', 'none');
		var picto = 'images/livraison/' + livraison + '.png';
		if (displayNext) {
			switch (livraison) {
				case 'livagence' :
				case 'livparis3' :
				case 'livparis8' :
				case 'livmval' :
					livtype = 'livagence';
					break;
				case 'livcoursier' :
				case 'livcoursieridf' :
					livtype = 'livcoursier';
					break;
				case 'livxpress' :
				case 'livxpresssamedi' :
					livtype = 'livxpress';
					break;
				case 'livcoli' :
				case 'livcoliom' :
				case 'livcolinter' :
				case 'livoffset' :
					livtype = livraison;
					break;
			}
			if (livtype == '' || $('h_' + livtype).style.display != 'block') {
				livraison = '';
				livtype = '';
				$('x_Livraison').value = livraison;
				displayNext = false;
			}
		}
		$('livagence').style.display = iif(livtype == 'livagence', 'block', 'none');
		$('livcoursier').style.display = iif(livtype == 'livcoursier', 'block', 'none');
		$('livxpress').style.display = iif(livtype == 'livxpress', 'block', 'none');
		$('livcoli').style.display = iif(livtype == 'livcoli', 'block', 'none');
		$('livcoliom').style.display = iif(livtype == 'livcoliom', 'block', 'none');
		$('livcolinter').style.display = iif(livtype == 'livcolinter', 'block', 'none');
		$('livoffset').style.display = iif(livtype == 'livoffset', 'block', 'none');
		// Agence
		if (livtype == "livagence") {
			$('x_Pays').value = '';
			if ($('livparis3').checked) $('x_Livraison').value = 'livparis3';
			else if ($('livparis8').checked) $('x_Livraison').value = 'livparis8';
			else if ($('livmval').checked) $('x_Livraison').value = 'livmval';
			else displayNext = false;
			if (displayNext) {
				//$('picto_livagence').src = picto;
			}
		}
		// Coursier
		if (livtype == "livcoursier") {
			if (livraison == 'livcoursier' && ville == '') {
				ville = "paris";
				$('x_Ville').value = ville;
			}
			$('x_Pays').value = 'fr';
			$('x_Livraison').value = iif(ville == "paris", 'livcoursier', 'livcoursieridf');
			//$('picto_livcoursier').src = picto;
			displayNext = devisStep('Ville', displayNext);
			if (ville == "paris")
				displayNext = devisStep('Arrondissement', displayNext);
			else
				devisStep('Arrondissement', 'clean');
		} else {
			devisStep('Ville', 'clean');
			devisStep('Arrondissement', 'clean');
		}
		// Chronopost
		if (livtype == "livxpress") {
			displayNext = devisStep('PaysXpress', displayNext);
			$('x_Pays').value = $('x_PaysXpress').value;
			$('x_Livraison').value = iif($("x_Designation1").checked, "livxpresssamedi", "livxpress");
		} else {
			devisStep('PaysXpress', 'clean');
			$("x_Designation1").checked = false;
		}
		/*if ($('x_PaysXpress').value == "fr")
			displayNext = devisStep('Zone', displayNext);
		else*/
			devisStep('Zone', 'clean');
		// Colissimo
		if (livtype == "livcoli")
			$('x_Pays').value = 'fr';
		if (livtype == "livcoliom") {
			displayNext = devisStep('PaysOM', displayNext);
			$('x_Pays').value = $('x_PaysOM').value;
		} else
			devisStep('PaysOM', 'clean');
		if (livtype == "livcolinter") {
			displayNext = devisStep('PaysInter', displayNext);
			$('x_Pays').value = $('x_PaysInter').value;
		} else
			devisStep('PaysInter', 'clean');
		// Offset
		if (livtype == "livoffset") {
			displayNext = devisStep('PaysOff', displayNext);
			$('x_Pays').value = $('x_PaysOff').value;
		} else
			devisStep('PaysOff', 'clean');
		/*displayNext = devisStep('Livraison', displayNext);
		if (livraison == "livcoursier") displayNext = devisStep('Arrondissement', displayNext); else devisStep('Arrondissement', 'clean');
		if (livraison == "livxpress") displayNext = devisStep('Zone', displayNext); else devisStep('Zone', 'clean');*/
	}
	if (livtype != '')
		$('complet_' + livtype).style.display = iif(displayNext, 'block', 'none');
	$('divstep').style.display = iif(displayNext, 'block', 'none');
	//if (displayNext) { $('divstep').style.display = 'block'; $('complet').style.display = 'block'; } else { $('complet').style.display = 'none'; $('divstep').style.display = 'none'; }
}

// Sauvegarde des ListBox

var TabImpType = new Array("value", "text");
var TabOriginaux = new Array("value", "text");
var TabMode = new Array("value", "text");
var TabPapier = new Array("value","text");
var TabImpression = new Array("value","text");
var TabCouverture = new Array("value","text");
var TabFormat = new Array("value","text");
var TabFormat2 = new Array("value","text");
var TabFinition = new Array("value","text");
var TabDelaiRealisation = new Array("value","text");
//var TabLivraison = new Array("value","text");
var TabNbPagesFixe = new Array("value","text");
var TabNbExemplairesFixe = new Array("value","text");

function SaveOptions(Tab,obj)
{
	Tab["value"] = new Array();
	Tab["text"] = new Array();

	for (var i = 0; i < obj.length; i++) {
		Tab["value"][i] = obj.options[i].value;
		Tab["text"][i] = obj.options[i].text;
	}
}


function event_onload()
{

	if(document.forms['fdevis_impadd'])
	{

		// On sauvegarde les differentes listes de choix;
		typedevis=document.forms['fdevis_impadd'].elements['x_TypeDevis'].value;
		if(typedevis == "multi"
			|| typedevis == "depliant"
			|| typedevis == "flyer"
			|| typedevis == "brochure"
			|| typedevis == "carterie"
			|| typedevis == "these"
			|| typedevis == "affiche"
			|| typedevis == "plan"
			|| typedevis == "lettre"
			|| typedevis == "enveloppe"
			|| typedevis == "chemise"
			|| typedevis == "sticker"
			|| typedevis == "autocollant")
		{
			SaveOptions(this.TabImpType,document.forms['fdevis_impadd'].elements['x_ImpType']);
			SaveOptions(this.TabOriginaux,document.forms['fdevis_impadd'].elements['x_Originaux']);
			SaveOptions(this.TabMode,document.forms['fdevis_impadd'].elements['x_Mode']);
			SaveOptions(this.TabPapier,document.forms['fdevis_impadd'].elements['x_Papier']);
			SaveOptions(this.TabImpression,document.forms['fdevis_impadd'].elements['x_Impression']);
			SaveOptions(this.TabCouverture,document.forms['fdevis_impadd'].elements['x_CouvertureDevant']);
			SaveOptions(this.TabFormat,document.forms['fdevis_impadd'].elements['x_Format']);
			SaveOptions(this.TabFormat2,document.forms['fdevis_impadd'].elements['x_Format2']);
			SaveOptions(this.TabFinition,document.forms['fdevis_impadd'].elements['x_Finition']);
			SaveOptions(this.TabNbPagesFixe,document.forms['fdevis_impadd'].elements['x_NbPagesFixe']);
			SaveOptions(this.TabNbExemplairesFixe,document.forms['fdevis_impadd'].elements['x_NbExemplairesFixe']);
		}
		else if(typedevis == "fiche")
		{
			SaveOptions(this.TabPapier,document.forms['fdevis_impadd'].elements['x_Papier']);
			SaveOptions(this.TabImpression,document.forms['fdevis_impadd'].elements['x_Impression']);
			SaveOptions(this.TabFormat,document.forms['fdevis_impadd'].elements['x_Format']);

		}

		SaveOptions(this.TabDelaiRealisation,document.forms['fdevis_impadd'].elements['x_DelaiRealisation']);
		//SaveOptions(this.TabLivraison,document.forms['fdevis_impadd'].elements['x_Livraison']);

		// On demarre avec la demande des Originaux
		$('r_Originaux').style.display = 'block';
		$('divstep').style.display = 'none';
		$('button').style.display = 'none';

		storeChange();
		event_onchange();
	}

	// Pour afficher les menus
	//event_onload2();
}

function event_onchange(e)
{
	if(document.forms['fdevis_impadd'])
	{
		devis_display();

		save_target=document.forms['fdevis_impadd'].target;
		save_action=document.forms['fdevis_impadd'].action;

		document.forms['fdevis_impadd'].target = "ifrmstep";
		document.forms['fdevis_impadd'].action = "../sldevis/step.php";
		document.forms['fdevis_impadd'].submit();

		document.forms['fdevis_impadd'].target = "ifrmpicto";
		document.forms['fdevis_impadd'].action = "http://kiev.addworks.fr/scriptlaser/slcalc"+modeSfx+"/picto.php";
		document.forms['fdevis_impadd'].submit();

		document.forms['fdevis_impadd'].target = "ifrmdevis";
		document.forms['fdevis_impadd'].action = "../sldevis/devis"+modeSfx+".php";
		document.forms['fdevis_impadd'].submit();

		document.forms['fdevis_impadd'].target = save_target;
		document.forms['fdevis_impadd'].action = save_action;
	}

}

function event_onclick(e)
{
	// Calcul du montant
	if (document.forms['fdevis_impadd'])
		if (navigator.appName.indexOf("Netscape") != -1 || navigator.appName.indexOf("Mozilla Firefox") != -1)
			devis_display();
}

function fdevis_impaddSubmit(action) {

	if(document.forms['fdevis_impadd'])
	{
		if( action == "submit")
			document.forms['fdevis_impadd'].submit();
		else if( action == "cancel")
			document.location.href = "../sl/devis_implist.php";
		else if( action == "register")
			document.location.href = "../sl/register.php";
	}
}

function fdevis_impaddRefresh(type) {

	if(document.forms['fdevis_impadd'])
	{
		window.location.href = "../sl/devis_impadd.php?type=" + type + "#step3";
	}
}

function fdevis_impaddCancel() {
	if(document.forms['fdevis_impadd'])
	{
		document.forms['fdevis_impadd'].submit();
	}
}


function pre_EW_checkMyForm(form) {
	return true;
}

