var _XMLconnOK = 0;

var global_prg_id  ; 
var global_lot_id  ; 

var cmd_time ; 
window.onload = function() {
var DSelect_el = new DSelect('select_list', 'invisible', 'select_list3_sim', 'select_content3_sim', 'select_txt3_sim', 'select_input3_sim');
DSelect_el.init();

var DSelect_el = new DSelect('select_list', 'invisible', 'select_list4', 'select_content4', 'select_txt4', 'select_input4');
DSelect_el.init();

var DSelect_el = new DSelect('select_list', 'invisible', 'select_list5', 'select_content5', 'select_txt5', 'select_input5');
DSelect_el.init();
}

function getXtmultc_ComNum(){
var d = new Date();
var c = String(d.getTime());
return(c.substr(3,8));
} 

function show_form_simulation(element, type, top){
	hide_form_simulation();
	document.getElementById("div_contenu_speed_contact").className = "contenu_speed_"+type;
        var ofsset = jQuery('#tableau_identifier').offset() ; 
        
        var topHeight = -(ofsset.top) ;
        var elementHeight = jQuery('#'+element).offset() ; 
        
        topHeight = topHeight + elementHeight.top - 400;
	document.getElementById("div_contenu_speed_contact").style.top = topHeight+"px";
	document.getElementById("speed_"+type+"_intro").className = "texte";
	if(type == "simu"){
		document.getElementById("bloc_simu").className = "bloc_rdv";
		document.getElementById("select_simu_csf").className = "select_txt";
		document.getElementById("select_content4").className = "select_txt_content";
		document.getElementById("select_simu_mnc").className = "select_txt";
		document.getElementById("select_content5").className = "select_txt_content";
		document.getElementById("valider_rdv_ajax_plan").innerHTML = "<input type='submit' class='bt_valider' name='valider_rdv' id='valider_rdv' value=''/>";
		xt_med('F','18','FORMULAIRE::DEMANDE_SPEED_SIMULATEUR');
	} else if(type == "plan") {
		if(_XMLconnOK == 0){
			xt_med('F','11','FORMULAIRE::DEMANDE_SPEED_PLAN');
		}
	}
	document.getElementById("speed_contact").className = "speed_div";
}

function hide_form_simulation(){
	document.getElementById("speed_contact").className = "invisible";
	document.getElementById("speed_plan_intro").className = "invisible";
	document.getElementById("speed_simu_intro").className = "invisible";
	document.getElementById("select_simu_csf").className = "invisible";
	document.getElementById("select_content4").className = "invisible";
	document.getElementById("select_simu_mnc").className = "invisible";
	document.getElementById("select_content5").className = "invisible";
	document.getElementById("bloc_simu").className = "invisible";
}

function call_DLService(){
        // xt_med('F','11','CONFIRMATION::DEMANDE_SPEED_PLAN_CONF');
        
        xt_med('F','11','DOWNLOAD_PLAN_'+global_prg_id+'_'+global_lot_id);
	window.location.href = "download_service/plan_lot.php?"+document.getElementById("speed_tgt").data;
}

function test_confirmation(XMLconn){
	if(typeof XMLconn == "object"){
            
		form_retour = eval('(' + XMLconn.responseText + ')');
                jQuery('#loader-download').hide() ; 
                jQuery('#form-dwonload').css('opacity' , '') ; 
		if( form_retour.status == 0 ){
                    
			_XMLconnOK = 1;
                        
                        var xt_multc = "";
                        var Xtmultc_ComNum = cmd_time ; 
                        xt_multc += "&roimt=1&cmd=" + Xtmultc_ComNum;
                        xt_multc += "&x4="+document.getElementById("formcontact_utilisateur_telephone_sim").value+"&x6="+document.getElementById("formcontact_utilisateur_codepostal_sim").value;
                        xt_multc += "&x7="+global_prg_id+"&x11="+form_retour.mailcc;
                        xt_multc += "&x12="+document.getElementById("formcontact_utilisateur_email_sim").value+"&x13="+Xtmultc_ComNum; 
                         xt_multc += "&x14="+form_retour.form_id ; 
       
                        
			xt_med('F','','CONFIRMATION::DEMANDE_SPEED_PLAN_CONF'+xt_multc);
                        
                        hide_form_simulation();
                        call_DLService(); 
                        //jQuery('#valider_rdv_ajax_plan').html('<span onclick=\"call_DLService();hide_form_simulation();\" style=\"cursor:pointer;\"><img src=\"/skins/fr/img/programme/bt_telecharger_plan.gif\" alt=\"Télécharger le plan\"/></span>') ; 
			
		}
	}else{
		if(_XMLconnOK == 1){
			hide_form_simulation();
			call_DLService();
		}
	}
	
}

function valid_form_plan(p_prg_id, p_fil_id){
	var _form_err;
	
	_form_err = "";
	if(document.getElementById("img_utilisateur_nom_sim").src.indexOf("form_valide") < 0 && document.getElementById("formcontact_utilisateur_nom_sim").value.length > 1){
		check_form_alpha('utilisateur_nom_sim');check_form_alpha('utilisateur_prenom_sim');check_form_telephone('utilisateur_telephone_sim');check_form_alpha_num('utilisateur_adresse_sim');
		check_form_email('utilisateur_email_sim');check_form_alpha_num('utilisateur_codepostal_sim');check_form_alpha('utilisateur_ville_sim');
	}
	
	if(document.getElementById("img_utilisateur_nom_sim").src.indexOf("form_valide") < 0){
		_form_err += "- Nom invalide\n";
	}
	if(document.getElementById("img_utilisateur_prenom_sim").src.indexOf("form_valide") < 0){
		_form_err += "- Prénom invalide\n";
	}
	if(document.getElementById("img_utilisateur_email_sim").src.indexOf("form_valide") < 0){
		_form_err += "- Email invalide\n";
	}
	if(document.getElementById("img_utilisateur_telephone_sim").src.indexOf("form_valide") < 0){
		_form_err += "- Téléphone invalide\n";
	}
	if(document.getElementById("img_utilisateur_adresse_sim").src.indexOf("form_valide") < 0){
		_form_err += "- Adresse invalide\n";
	}
	if(document.getElementById("img_utilisateur_codepostal_sim").src.indexOf("form_valide") < 0){
		_form_err += "- Code postal invalide\n";
	}
	if(document.getElementById("img_utilisateur_ville_sim").src.indexOf("form_valide") < 0){
		_form_err += "- Ville invalide\n";
	}
	
	if(_form_err != ""){
		alert("Un ou plusieurs champs génèrent une erreurs :\n\n" + _form_err);
	} else {
		if(document.getElementById("bloc_simu").className == "invisible"){
			document.getElementById("valider_rdv_ajax").innerHTML = "<img src='/skins/fr/img/commun/form-ajax-loader.gif' alt='Vérification des données' />";
			comXhrObj1 = new XHRConnection();
			// Données à transmettre
			comXhrObj1.appendData('formcontact_utilisateur_civilite', document.getElementById("select_input2_sim").value);
			comXhrObj1.appendData('formcontact_utilisateur_nom', document.getElementById("formcontact_utilisateur_nom_sim").value);
			comXhrObj1.appendData('formcontact_utilisateur_prenom', document.getElementById("formcontact_utilisateur_prenom_sim").value);
			comXhrObj1.appendData('formcontact_utilisateur_email', document.getElementById("formcontact_utilisateur_email_sim").value);
			comXhrObj1.appendData('formcontact_pay_id', document.getElementById("select_input3_sim").value);
			comXhrObj1.appendData('formcontact_utilisateur_telephone', document.getElementById("formcontact_utilisateur_telephone_sim").value);
			comXhrObj1.appendData('formcontact_utilisateur_adresse', document.getElementById("formcontact_utilisateur_adresse_sim").value);
			comXhrObj1.appendData('formcontact_utilisateur_codepostal', document.getElementById("formcontact_utilisateur_codepostal_sim").value);
			comXhrObj1.appendData('formcontact_utilisateur_ville', document.getElementById("formcontact_utilisateur_ville_sim").value);
			comXhrObj1.appendData('formcontact_priorite', 1);
			comXhrObj1.appendData('form_return_type', 'speed');
                        
                        var opt_partner = "non" ; 
                        jQuery('.optin_partner').each(function(){
                            
                            if(jQuery(this).is(':checked'))
                              opt_partner = jQuery(this).val() ; 
      
                        }) ; 
                        comXhrObj1.appendData('formcontact_optin_partenaire', opt_partner);
                        
			//Lancement
                        jQuery('#loader-download').show() ; 
                        jQuery('#form-dwonload').css('opacity' , '0.4') ; 
			comXhrObj1.sendAndLoad("/contact/demande_infos/"+p_prg_id+"_"+p_fil_id+".php", "POST", test_confirmation);
		} else {
			return true;
		}
	}
	
	return false;
}

function calc_loyer(prix_lot, lot_surface, type){
	var tx_rentabilite = 4;
	var plafond_loyer = 21.65;
	if(type == 'borloo'){
		tx_rentabilite = 4;
		plafond_loyer = 17.32;
	}
	return (Math.floor( ( Math.min((prix_lot * tx_rentabilite/100 /12), (lot_surface * plafond_loyer)) )/50) * 50);
}

function go_Simulation_form(element,prix_logement, lot_surface, top){
	
        document.getElementById("formcontact_PrixLogement_input").value = prix_logement;
	
	show_form_simulation(element,"simu", top);
}
