function verificationQAS(divid, force){	
	 	if(divid == ''){
	 		divid = 'div_wait_PP';
	 	} 
	 	//alert(mode);
		function handleSuccess(html, textStatus)
		{
			eval(html);
			if(verifQAS=='ok' && force!=true)
			{
				displayQAS(hasMonikers, hasAdressFound, sMoniker);
			}
			else{
				document.frmCompte.adresse.value = adresse;
				document.frmCompte.lieu.value = lieu;
				document.frmCompte.code_postal.value = code_postal;
				document.frmCompte.code_insee.value = code_insee;
				document.frmCompte.ville.value = ville;
				if(adresseForcee){
					 document.frmCompte.topAdr.value = 1;
				}
				else{
					 document.frmCompte.topAdr.value = 0;
				}
				if(modetmp){
					 document.frmCompte.mode.value = modetmp;
				}
				else{
					 document.frmCompte.mode.value = document.frmCompte.modetmp.value;
				}
				switch(document.frmCompte.mode.value){
					case 'nouveauClient':
						setPanierSwitcher(document.frmCompte, "nouveauClient");
						break;
					case 'modifCompte':
					case 'popupNewAdr':						
					case 'popupNewAmie':
						setPanierSwitcher(document.frmCompte, "/livraison.html");
						break;
					default:
						document.frmCompte.submit();
						break;
				}
				
			}
			//displaydiv(divid, 0);
		}
		
		function handleFailure(html, textStatus)
		{
			divWait(0);
			if(textStatus=='timeout')
			{
				panierLMDV.getTimeout();
			}
			failureFunction('ERR028', 'verificationQAS', (html!=undefined)? textStatus:'', (html!=undefined) ? textStatus:'');
		}
					
		var sUrl = '/inc/pages/async/setPanier.php';
		//var temp = (document.frmCompte.modetmp.value=='') ? document.frmCompte.mode.value : document.frmCompte.modetmp.value;
		document.frmCompte.modetmp.value = (document.frmCompte.modetmp.value=='') ? document.frmCompte.mode.value : document.frmCompte.modetmp.value;
		var pars = "action=action&adresse="+document.frmCompte.adresse.value+"&lieu="+document.frmCompte.lieu.value+"&code_postal="+document.frmCompte.code_postal.value+"&ville="+document.frmCompte.ville.value+"&mode=verificationQAS&modetmp="+ document.frmCompte.modetmp.value;
		//displaydiv(divid, 1);
		divWait(1);
		//var request = YAHOO.util.Connect.asyncRequest('POST', url, callback, pars);
		$.ajax({
		  	url: sUrl,
		  	success: handleSuccess,
		  	error: handleFailure,
		  	data: pars
	 	});
		
		
	}

function setPanierSwitcher(frm, location){
	function handleSuccess(html, textStatus)
	{
		//displaydiv("div_wait_PP", 0);
		eval(html);
		if (location == 'nouveauClient' && compte =='nok'){
			/*activeHref("btn_valider");
			if (document.getElementById("btn_ok")!= null){
				activeHref("btn_ok");
			}*/
			alertMsg(error);
		}else{
			var news_lmdv='';
			var news_part='';
			if (frm.news_radio!= undefined){
				var news_lmdv = (frm.news_radio[1].checked) ? 0 : 1;
			}else if (frm.news!= undefined){
				var news_lmdv = frm.news;
			}
		
			if (frm.offres_radio!= undefined){
				var news_part = (frm.offres_radio[1].checked) ? 0 : 1;
			}else if (frm.offres!= undefined){
				var news_part = frm.offres.value;
			}
			
			if (location == 'nouveauClient'){
				var msg = '';
				panierLMDV.finIdentification(frm, msg, (eval(frm.tunnel.value))?statut:'', (eval(frm.tunnel.value))?TopPromoCL:'');
			}else{
				document.location.href= panierLMDV.getUrlNoCache()+'/'+location;
			}
		}
		divWait(0);
	}
	
	function handleFailure(html, textStatus)
	{
		divWait(0);
		if(textStatus=='timeout')
		{
			panierLMDV.getTimeout();
		}
		failureFunction('ERR029', 'setPanierSwitcher', (html!=undefined)? textStatus:'', (html!=undefined) ? textStatus:'');
	}
						
	var sUrl = './inc/pages/async/setPanier.php';
	var pars ='';
	elements = frm.elements;
    for(var i = 0; i < elements.length; i++) {
    	if (elements[i].name=='news_radio' && elements[i].value==1){
    		pars += 'news=' + elements[i].checked + '&';
    	}else if(elements[i].name=='offres_radio' && elements[i].value==1){
    		pars += 'offres=' + elements[i].checked + '&';
    	/*}else if (elements[i].name=='news' || elements[i].name=='offres'){
    			pars += elements[i].name + '=' + elements[i].checked + '&';
    	}*/
    	}else{
     		pars += elements[i].name + '=' + elements[i].value + '&';
     	}
     	
    }
    
    //displaydiv("div_wait_PP", 1);
    divWait(1);
  	$.ajax({
	  	url: sUrl,
	  	success: handleSuccess,
	  	error: handleFailure,
	  	data: pars
 	});
}

function getSelectedPickList(sMoniker){	
	
			function handleSuccess(html, textStatus)
			{
				//displaydiv("div_wait_PP", 0);
				//YAHOO.popup.container.popupQAS.hide();
				$('#popup_QAS').dialog('close');
				eval(html);
				document.frmCompte.adresse.value = adresse;
				document.frmCompte.lieu.value = lieu;
				document.frmCompte.code_postal.value = code_postal;
				document.frmCompte.ville.value = ville;
				document.frmCompte.mode.value = mode;
				document.frmCompte.modetmp.value = document.getElementById('modetmp').value;
				if(sMoniker){	
					verificationQAS();
				}else{
					verificationQAS('', true);
				}
				//divWait(0);
			}
			
			function handleFailure(html, textStatus)
			{
				divWait(0);
				if(textStatus=='timeout')
				{
					panierLMDV.getTimeout();
				}
				failureFunction('ERR030', 'getSelectedPickList', (html!=undefined)? textStatus:'', (html!=undefined) ? textStatus:'');
			}
			
			if(sMoniker){
				var pars = "action=action&Moniker="+sMoniker+"&mode=verificationQAS&modetmp="+document.getElementById('modetmp').value;
			}else{
				document.frmCompte.adresse.value = document.formAddress.adresse.value;
				document.frmCompte.lieu.value = document.formAddress.lieu.value;
				document.frmCompte.code_postal.value = document.formAddress.code_postal.value;
				document.frmCompte.ville.value = document.formAddress.ville.value;
				var pars = "action=action&adresse="+document.frmCompte.adresse.value+"&lieu="+document.formAddress.lieu.value+"&code_postal="+document.formAddress.code_postal.value+"&ville="+document.formAddress.ville.value+"&mode=verificationQAS&modetmp="+document.frmCompte.mode.value;
			}
			//alert(pars);
			var sUrl = '/inc/pages/async/setPanier.php';
			//displaydiv("div_wait_PP", 1);
			divWait(1);
			//var request = YAHOO.util.Connect.asyncRequest('POST', url, callback, pars);
			$.ajax({
			  	url: sUrl,
			  	success: handleSuccess,
			  	error: handleFailure,
			  	data: pars
		 	});
		
	}
	
				
function displayQAS(hasMonikers, hasAdressFound, sMoniker){
						
	body = '';
	body += '<div id="div_wait_PP" name="div_wait_PP" style="display:none">';
	body += '	<div id="div_wait_texte_PP">Traitement en cours, merci de bien vouloir patienter.';
	body += '	<br><img src="/img/commun/loading.gif" width="220" height="19" alt="Work in progress" title="Work in progress" /></div>';
	body += '</div>';

	body += '<div style="float:left">';
	body += '	<div class="rl bd_o" style="width:585px; height:380px">';
	body += '		<div class="corner_tl"></div><div class="corner_tr"></div>';
	body += '		<div class="titre">Mon adresse de facturation</div><br clear="both">';
	body += '		<input type="hidden" name="mode"/>';
	body += '		<div class="adrliv rl1">';				
	body += hasMonikers;
	body += hasAdressFound;
	body += '		</div>';
	body += '	</div>';
	body += '	<div class="clear"></div>';
	body += '	<div class="bouton_orange">';
	body += '		<div class="corner_bl"></div><div class="corner_br"></div>';
	if(sMoniker){
		body += '	<div class="fleche_droite"><a href="#" onclick="getSelectedPickList(sMoniker);">Valider</a></div>';
	}
	else{
		body += '	<div class="fleche_droite"><a href="#" onclick="getSelectedPickList();">Valider</a></div>';
	}	
	body += '	</div>';
	body += '</div>';
		
	printdiv('popup_QAS',  body );
	//constructPopup('popup_QAS', 665, 510, true);
	$("#popup_QAS").dialog({width: 665,
		  height:510,
		  closeOnEscape:false,
		  zIndex:9999,
		  draggable:false,
		  modal:true,
		  resizable: false,
		  title: "",
		  close: function(){divWait(0);}});
	$(".ui-dialog-titlebar").show();
}


function verifChoixCommune(frm){	
		function handleSuccess(html, textStatus)
		{
			divWait(0);
			var dataCommunes = '';
					
			eval(html);
	
			if(dataCommunes != ''){
				displayChoixCommune(dataCommunes);
			}
			else{

				//panierLMDV.checkPaiement(document.frm_paiement_cmn,'cmn');
				//document.frm_paiement_cmn.submit();
				frm.submit();
			}
		}
		
		function handleFailure(html, textStatus)
		{
			divWait(0);
			alert("Adéquation commune de naissance/département impossible.");
			if(textStatus=='timeout')
			{
				panierLMDV.getTimeout();
			}
		}
				
		var sUrl = '/inc/pages/async/setPanier.php';
		var pars = "ville="+frm.ville_naissance.value+"&departement="+frm.dep_naissance.value+"&mode=popupChoixCommune&frm_name="+frm.name;
		divWait(1);
		//var request = YAHOO.util.Connect.asyncRequest('POST', url, callback, pars);
		$.ajax({
		  	url: sUrl,
		  	success: handleSuccess,
		  	error: handleFailure,
		  	data: pars
	 	});
	}
		
function displayChoixCommune(communes){
			/*YAHOO.namespace("popup.container");
			YAHOO.popup.container.popupQAS = new YAHOO.widget.Dialog("popup_QAS", 
					{ width:"400px",
					  height:"300px",
					  zIndex:9999,
					  fixedcenter:true,
					  visible:false, 
					  constraintoviewport:true,
					  draggable:false,
					  modal:true,
					  effect:[{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.5}]
					});*/
					
		body = '';

body += '<div id="div_wait_PP" name="div_wait_PP" style="display:none">';
body += '<div id="div_wait_texte_PP">Traitement en cours, merci de bien vouloir patienter.';
body += '<br><img src="/img/commun/loading.gif" width="220" height="19" alt="Work in progress" title="Work in progress" /></div>';
body += '</div>';

		body += '<div style="float:left">';
		body += '<div class="rl bd_o" style="width:400px; height:280px">';
			body += '<div class="corner_tl"></div><div class="corner_tr"></div>';
			body += '<input type="hidden" name="mode"/>';
			body += '<div class="adrliv rl1">';
			body += '<div class="orange">Nous vous proposons les villes suivantes </div>';
			body += '<div class="clear"></div>';
			body += '<dl class="select w270px" style="left:50px;z-index:10;">';			
				body += '<dt onclick="show(\'smenu10\');" onmouseout="show();" id="lieuDisplay">Selectionner';
					body += '<dd id="smenu10" onmouseover="show(\'smenu10\');" onmouseout="show();" style="height: 200px; overflow-x: auto; overflow-y: scroll; display: none;">';
						body += '<ul>';
						body += communes;
						body += '</ul>';
					body += '</dd>';
				body += '</dt>';
			body +=  '</dl>';
		body += '</div>';
			
			
				
		body += '</div>';
		body += '</div>';
		body += '<div class="clear"></div>';
		body += '<div class="bouton_orange"><div class="corner_bl"></div><div class="corner_br"></div><a href="#">Valider</a></div>';
		body += '</div>';
		body += '</div>';
			
		printdiv('popup_QAS',  body );
		constructPopup('popup_QAS', 450, 420, true);
		/*YAHOO.popup.container.popupQAS.render(document.body);
		YAHOO.popup.container.popupQAS.show();*/	
}



function valideAdresse(frm){
		var error = "";
		error += checkAdresseLivraison2(frm);
		if (error==""){
			verificationQAS('');
		}else{
			alertMsg("<h2>Veuillez saisir:</h2>" + error);
		}
}

function checkAdresseLivraison2(frm){
	var civilite = frm.civilite.value;
	var nom = frm.nom.value.toUpperCase();
	var prenom = frm.prenom.value;
	var adresse = frm.adresse.value;
	var etage = frm.etage.value;
	var batiment = frm.escalier.value;
	var lieu = frm.lieu.value;
	var code_postal = frm.code_postal.value;
	var ville = frm.ville.value;
	var pays = frm.pays.value;
	var error = "";
	if (civilite==""){
		error += "votre civilite<br>";
	}
	if (nom==""){
		error += "votre nom<br>";
	}
	if (prenom==""){
		error += "votre prenom<br>";
	}
	if (adresse==""){
		error += "votre adresse<br>";
	}
	if (code_postal=="" && ville==""){
		error += "votre code postal ou votre ville<br>";
	}
	if (code_postal.substr(0,2)=='97'){
		error += "votre code postal<br>Désolé, nous ne livrons que la France Métropolitaine.<br>Nous vous invitons à modifier votre adresse de livraison.";
	}
	if (pays==""){
		error += "votre pays<br>";
	}
	return error;
}

function verifModLiv(theForm, location)
{
	divWait(1);
	var nameTab = new Array();
	var diff = 0;
	var error = "";
	elements = theForm.elements;
	for (i=0; i<elements.length; i++){
		if(!in_array(elements[i].name, nameTab) && elements[i].type=='radio'){
			array_push(nameTab, elements[i].name);
		}
		if(elements[i].checked){
			diff++;
		}
	}
	if(diff != nameTab.length){
		error += "Veuillez saisir les modes de livraison pour tous les produits SVP ! ";
	}
	if (error==""){
		theForm.submit();
	}else{
		divWait(0);
		alertMsg(error);
	}
}

function in_array(needle, haystack, argStrict) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true
    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
 
    var key = '', strict = !!argStrict;
 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
    return false;
}

function array_push ( array ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: array_push(['kevin','van'], 'zonneveld');
    // *     returns 1: 3
 
    var i, argv = arguments, argc = argv.length;
 
    for (i=1; i < argc; i++){
        array[array.length++] = argv[i];
    }
 
    return array.length;
}

