var configuratorData = {

	initial: {
		imgSrc: F6.CONTEXT + "/img/demo/packInitial_sm.png",
		imgAlt: "pack Initial",
		pxPoste: 29,
		forfaitTxt: "OPTION FORFAIT __duree__H VERS MOBILES POUR TOUTES VOS LIGNES FIXES",
		"0": 0,
		"2h": 14.4,
		"4h": 28.8,
		"6h": 43.2,
		"8h": 57.6,
		"10h": 72,
		"12h": 86.4,
		"16h": 124.8,
		"20h": 156,
		"30h": 234,
		ligneDefaultText: "<span class='txtGrey2'>LIGNE SUPPL&Eacute;MENTAIRE</span>",
		fax: "LIGNE FAX",
		tpe: "LIGNE TPE",
		alarm: "LIGNE ALARME",
		autre: "AUTRE",
		autreLigneTxt: "",
		pxLigne: 17.30
		
	},
	
	intensif: {
		imgSrc: F6.CONTEXT + "/img/demo/packIntensif_sm.png",
		imgAlt: "pack Intensif",
		pxPoste: 49,
		forfaitTxt: "APPELS INCLUS VERS MOBILES TOUS OP&Eacute;RATEURS",
		ligneDefaultText: "<span class='txtGrey2'>LIGNE SUPPL&Eacute;MENTAIRE</span>",
		fax: "LIGNE FAX",
		tpe: "LIGNE TPE",
		alarm: "LIGNE ALARME",
		autre: "AUTRE",
		pxLigne: 17.30
	}
}

function refreshConfigurator(){
    // free month parameter
    var freeMonthPack = 2;
    var freeMonthMobile = 1;
    
	var context = QS2OBJ($('configurator').toQueryString());

    var oSrc = configuratorData[context['packTelephonie']];
	var total = 79;
	$('total_systemeCommunication').value = 79;
	
	
	if(!oSrc) {
		$("initial").checked = true;
		$("initial").getParent("label").addClass("check");
		$('forfait').selectedIndex = 0;
		oSrc = configuratorData.initial;
	}
	if(!context.nbPostes) {
		$("poste2").checked = true;
		$("poste2").getParent("label").addClass("check");
		context = QS2OBJ($('configurator').toQueryString());
	}
	if(!context.fax1) {
		$("fax1").checked = true;
		$("fax1").getParent("label").addClass("check");
		context = QS2OBJ($('configurator').toQueryString());
	}
	
	
	//
	
	$('packImg').src = oSrc.imgSrc;
	
    $('posteSupp').set("html", "1");
    $('nbPostesFixes').set("html", context.nbPostes.toInt());
    $('nbPosteSupp').set("html", context.nbPostes.toInt() - 1);
    $('nbPosteSupp2').set("html", context.nbPostes.toInt());
	
	$('pxUnit').set("html", oSrc.pxPoste.toInt());
    var prixPack = oSrc.pxPoste.toInt() * context.nbPostes.toInt();
	$('prixOptionsTelFixe').set("html", prixPack + "<span>&euro; HT</span>") ;

	total += oSrc.pxPoste.toInt() * (context.nbPostes.toInt());
	$('total_pack').value = oSrc.pxPoste.toInt() * (context.nbPostes.toInt() + 1);
	
	
	if(!context["ligne2"]){
		$('ligne2Txt').set("html", oSrc.ligneDefaultText);
		$('prixLigne2').set("html", "<div class='txtGrey2'>{euros},<span>{cents}&euro; HT</span></div>".substitute(BOL_BT.Basket.splitPrice(oSrc.pxLigne)));
		$('total_ligne2').value = 0;
	}
	else {
		$('ligne2Txt').set("html", oSrc[context.ligne2])
		$('prixLigne2').set("html", "{euros},<span>{cents}&euro; HT</span>".substitute(BOL_BT.Basket.splitPrice(oSrc.pxLigne)));
		total += oSrc.pxLigne;
		$('total_ligne2').value = oSrc.pxLigne;
	};
	
	/*if(!context["ligne3"]){
		$('ligne3Txt').set("html", oSrc.ligneDefaultText);
		$('prixLigne3').set("html", "<div class='txtGrey2'>{euros},<span>{cents}&euro; HT</span></div>".substitute(BOL_BT.Basket.splitPrice(oSrc.pxLigne)));
		$('total_ligne3').value = 0;
	}
	else {
		$('ligne3Txt').set("html", oSrc[context.ligne3])
		$('prixLigne3').set("html", "{euros},<span>{cents}&euro; HT</span>".substitute(BOL_BT.Basket.splitPrice(oSrc.pxLigne)));
		total += oSrc.pxLigne;
		$('total_ligne3').value = oSrc.pxLigne;
	};
	
	if(!context["ligne4"]){
		$('ligne4Txt').set("html", oSrc.ligneDefaultText);
		$('prixLigne4').set("html", "<div class='txtGrey2'>{euros},<span>{cents}&euro; HT</span></div>".substitute(BOL_BT.Basket.splitPrice(oSrc.pxLigne)));
		$('total_ligne4').value = 0;
	}
	else {
		$('ligne4Txt').set("html", oSrc[context.ligne4])
		$('prixLigne4').set("html", "{euros},<span>{cents}&euro; HT</span>".substitute(BOL_BT.Basket.splitPrice(oSrc.pxLigne)));
		total += oSrc.pxLigne;
		$('total_ligne4').value = oSrc.pxLigne;
		
	};*/
	
	switch(true){
		case context['packTelephonie'] == "initial":
			if(context.forfait == 0) {
				$("optionsTelFixeForfaitTxt").set('html', "<span class='txtGrey2'>OPTION FORFAIT <br />VERS MOBILE NON-SELECTIONNE</span>")
				$('prixOptionsTelFixeForfait').set("html", "");
				$('total_forfaitOption').value = 0;
			}
			else {
				$("optionsTelFixeForfaitTxt").set('html', oSrc.forfaitTxt.replace(/__duree__/, context.forfait.toInt()))
				$('prixOptionsTelFixeForfait').set("html", "{euros},<span>{cents}&euro; HT</span>".substitute(BOL_BT.Basket.splitPrice(oSrc[context.forfait])));
				$('total_forfaitOption').value = oSrc[context.forfait];
			}
			total += oSrc[context.forfait];
			break;
		case context['packTelephonie'] == "intensif":
			if(!$('prixOptionsTelFixeForfait').getElement('img')) $('prixOptionsTelFixeForfait').set("html", '<img height="18" alt="" src="'+F6.CONTEXT+'/img/demo/offert_sm.png" />');
			$("optionsTelFixeForfaitTxt").set('html', oSrc.forfaitTxt)
			break;
	}
	
	total = (total*100).toInt()/100;
	$('prixTotal').set("html", "{euros},<span>{cents}&euro;<br/>HT/mois</span>".substitute(BOL_BT.Basket.splitPrice(total)));
	$('total_montant').value = total;

    // partie forfait

    // reninit
    var bigTotal = total;
    total = 0;

    if(context.ligneForfaitEssentiel){
        var t = context.ligneForfaitEssentiel * context.forfait_essentiel_base;
        total += t;
        $('3MoisOffertsMobileE').removeClass("hidden");
        $('reduction3MoisOffertsMobileE').set("html", t * freeMonthMobile);
        
        $("forfait_essentiel_px").set("html", "{euros}<span>,{cents}&euro; HT</span>".substitute(BOL_BT.Basket.splitPrice(t)));
        $("total_forfait_essentiel").value = t;
        $("total_forfait_essentiel").getParent("tr").getElements(".txtGrey2").removeClass("txtGrey2");
    }
    else {
        $('3MoisOffertsMobileE').addClass("hidden");
        $("forfait_essentiel_px").set("html", "<span class='txtGrey2 txtC em1'>{euros}<span>,{cents}&euro; HT</span></span>".substitute(BOL_BT.Basket.splitPrice(0)));
        $("total_forfait_essentiel").value = 0;
        $("total_forfait_essentiel").getParent("tr").getElement("span").addClass("txtGrey2");
    }

     if(context.ligneForfaitIllimythics){
        var t = context.ligneForfaitIllimythics * context.forfait_illimythics_base;
        total += t;
        $('3MoisOffertsMobileI').removeClass("hidden");
        $('reduction3MoisOffertsMobileI').set("html", t * freeMonthMobile);
        
        $("forfait_illimythics_px").set("html", "{euros}<span>,{cents}&euro; HT</span>".substitute(BOL_BT.Basket.splitPrice(t)));
        $("total_forfait_illimythics").value = t;
        $("total_forfait_illimythics").getParent("tr").getElements(".txtGrey2").removeClass("txtGrey2");
    }
    else {
        $('3MoisOffertsMobileI').addClass("hidden");
        
        $("forfait_illimythics_px").set("html", "<span class='txtGrey2 txtC em1'>{euros}<span>,{cents}&euro; HT</span></span>".substitute(BOL_BT.Basket.splitPrice(0)));
        $("total_forfait_illimythics").value = 0;
        $("total_forfait_illimythics").getParent("tr").getElement("span").addClass("txtGrey2");
    }

     if(context.ligneForfaitAbsolu){
        var t = context.ligneForfaitAbsolu * context.forfait_absolu_base;
        total += t;
        $('3MoisOffertsMobileA').removeClass("hidden");
        $('reduction3MoisOffertsMobileA').set("html", t * freeMonthMobile);
        
        $("forfait_absolu_px").set("html", "{euros}<span>,{cents}&euro; HT</span>".substitute(BOL_BT.Basket.splitPrice(t)));
        $("total_forfait_absolu").value = t;
        $("total_forfait_absolu").getParent("tr").getElements(".txtGrey2").removeClass("txtGrey2");
    }
    else {
        $('3MoisOffertsMobileA').addClass("hidden");
        
        $("forfait_absolu_px").set("html", "<span class='txtGrey2 txtC em1'>{euros}<span>,{cents}&euro; HT</span></span>".substitute(BOL_BT.Basket.splitPrice(0)));
        $("total_forfait_absolu").value = 0;
        $("total_forfait_absolu").getParent("tr").getElement("span").addClass("txtGrey2");
    }

	total = (total*100).toInt()/100;
	$('prixTotalForfait').set("html", "{euros},<span>{cents}&euro;<br/>HT/mois</span>".substitute(BOL_BT.Basket.splitPrice(total)));
	$('total_montant_forfait').value = total;

    bigTotal+= total;
    bigTotal = (bigTotal*100).toInt()/100;


    /*Dummy test: never used because linked html structure is disabled ...*/
    if($("total_total_total_horsremise")){
        $("total_total_total_horsremise").set("html", "{euros},<span>{cents}&euro;<br/>HT/mois</span>".substitute(BOL_BT.Basket.splitPrice(bigTotal)));
    }



    bigTotal *= (1 - $("reduction_taux").value);
    bigTotal = (bigTotal*100).toInt()/100;
    $("total_total_total").set("html", "{euros},<span>{cents}&euro;<br/>HT/mois</span>".substitute(BOL_BT.Basket.splitPrice(bigTotal)));
    $('total_total_total_total').value = bigTotal;

	
}




function QS2OBJ(str){
	if(!str) str = document.location.search
	if(str[0] == "?") str = str.substring(1);
	var vars = str.split("&");
	var o = {};
	vars.each(function (el, i){
		var sp = el.split("=");
		o[sp[0]] = sp[1];
	})
	return o;
}

addEvent("load", function (){
	$('configurator').getElements("input[type=radio]").addEvent("click", function (){
		refreshConfigurator();
	})
	$('configurator').getElements("select").addEvent("change", function (){
		refreshConfigurator();
	})
	refreshConfigurator();

    $("nbMail").addEvent("blur", function(){
        if(this.value > 10) this.value = 10;
        else if(this.value < 0) this.value = 0;
    })

    $("ndd").addEvent('blur', function (){
        var nb = this.value.split(".");
        if(nb.length == 1){
            this.value = "www."+this.value;
            return leftExt(this);
        }
        else if (nb.length == 2 && /www/.test(this.value)) {
            this.value += ".com";
        }
        else if (nb.length == 2 && !/www/.test(this.value)) {
            this.value = "www."+this.value;
        }
        
    })


})


