var req = null;
var filelines;
var cellule;
var ratio_unites = 12;

function affiche(x) {
	if (document.getElementById(x)) {
		document.getElementById(x).style.display="block";
	}
}
function efface(x) {
	if (document.getElementById(x)) {
		document.getElementById(x).style.display="none";
	}
}
function addType() {
	affiche('types');
	affiche('reportsexpliq');
	efface('reportsmodif');
		if (document.getElementById('identifie')) {
	efface('sescoords');
	efface('banque');
	efface('motif');
	efface('comp');
	}
	efface('valid');
	if (req && req.readyState != 0){
		req.abort();
	}
	req = null;
	if (window.XMLHttpRequest) { // Firefox, Safari, IE7
		req = new XMLHttpRequest();
		if (req.overrideMimeType) {// Safari
			req.overrideMimeType("text/xml");
		}
	}
	else {
		if (window.ActiveXObject) {
			try { // IE6
				req = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				try { // IE5
					req = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e) {
					window.alert("Votre browser ne supporte pas AJAX");
				}
			}
		}
	}
	req.onreadystatechange = getresponse;
	var str = document.form000.pays.value;
	req.open("GET", "/cgi-bin/beicint?idpays="+str, true);
	req.setRequestHeader("Cache-Control","no-cache");
	req.send(null);
	document.form000.pays.blur();
}

function selectreport(x) {
	var theone;
	var option;
	if (x==0) {// from ajax
		option = document.form000.prestation;
		for(var i = 0; i < option.length; i++) {
			if(option[i].checked) {
				//var report = option[i].value;
				theone = i;
			}
		}
		if (document.getElementById('identifie')) {
			affiche('sescoords');
			affiche('banque');
			affiche('motif');
			affiche('comp');
		}
		affiche('valid');
		for (i=0;i<option.length;i++) {
			if ((i)!=theone) {
				efface('a1-'+i);
				efface('a2-'+i);
				document.getElementById('p-content-'+i).innerHTML = "";
			}
		}
	}
	else {// from back errorpage
		var prestation0 = document.form000.prestation0;
		var delai0 = document.form000.delai0;
		var p0 = document.form000.p0;
		var md50 = document.form000.md50;
		document.getElementById('reports').innerHTML = "";
		var contenu = '<br><table align="center" border="0" cellpadding="1" cellspacing="0" width="350">\r\n';
		contenu += '<tr><td colspan="4" class="pointBleu"></td></tr>\r\n';
		contenu += '<tr id="a1-'+i+'">\r\n';
		contenu += '<td width="15"><input type="radio" checked="checked" name="prestation" value="'+prestation0.value+'" class="normalSize" /></td>\r\n';
		contenu += '<td align="left">'+prestation0.value+'</td>\r\n';
		contenu += '<td align="left">'+delai0.value+' ouvrés</td>\r\n';
		contenu += '<td align="right">'+(p0.value*ratio_unites).toFixed(2)+' &euro;<span id="p-content-'+i+'"><input type="hidden" name="delai" value="'+delai0.value+'"><input type="hidden" name="p" value="'+p0.value+'"><input type="hidden" name="md5" id="md50'+i+'" value="'+md50.value+'"></span></td>\r\n';
		contenu += '</tr>\r\n';
		contenu += '<tr id="a2-'+i+'"><td colspan="4" class="pointBleu"></td>\r\n';
		contenu +='</table><br>\r\n';
		document.getElementById('reports').innerHTML=contenu;
		if (document.getElementById('identifie')) {
			affiche('sescoords');
			affiche('banque');
			affiche('motif');
			affiche('comp');
		}
		affiche('valid');
	}
	efface('reportsexpliq');
	document.getElementById('reportsmodif').style.display="inline";
}
function verifAll() {
	var erreur = false;
	var phrase_erreur = "Certains champs sont obligatoires :";
	if (document.form000.rs.value=="" ) {
		phrase_erreur +="\r\n- Raison sociale de l'entreprise analysée";
		erreur = true;
	}
	if (document.form000.adresse.value=="" ) {
		phrase_erreur +="\r\n- Adresse de l'entreprise analysée";
		erreur = true;
	}
	if (erreur) {
		alert(phrase_erreur);
	}
	else {
		document.form000.submit();
	}
}
function getresponse() { 
  if (req.readyState == 4) {
		filelines = req.responseText.split("\n");
		document.getElementById('reports').innerHTML = "";
		
		var contenu = '<br><table align="center" border=0 cellpadding=1 cellspacing=0 width=340>\r\n';
		contenu += '<tr><td colspan="4" class="pointBleu"></td></tr>\r\n';
		for (i=0; i<filelines.length-1; i++) {
			cellule = filelines[i].split(" ");
			if (cellule[6]) {
				contenu += '<tr id="a1-'+i+'">\r\n';
				contenu += '<td width="15"><input onClick="selectreport(0);" type="radio" name="prestation" value="'+cellule[0]+' '+cellule[1]+'" class="normalSize" /></td>\r\n';
				contenu += '<td align="left">'+cellule[0]+' '+cellule[1]+'</td>\r\n';
				contenu += '<td align="left">'+cellule[2]+' '+cellule[3]+' '+cellule[4]+' ouvrés</td>\r\n';
				contenu += '<td align="right">'+(cellule[5]*ratio_unites).toFixed(2)+' &euro;<span id="p-content-'+i+'"><input type="hidden" name="delai" value="'+cellule[2]+' '+cellule[3]+' '+cellule[4]+'"><input type="hidden" name="p" value="'+cellule[5]+'"><input type="hidden" name="md5" id="md50'+i+'" value="'+cellule[6]+'"></span></td>\r\n';
				contenu += '</tr>\r\n';
				contenu += '<tr id="a2-'+i+'"><td colspan="4" class="pointBleu"></td>\r\n';
			}
			else {
				contenu += '<tr id="a1-'+i+'">\r\n';
				contenu += '<td width="15"><input onClick="selectreport(0);" type="radio" name="prestation" value="'+cellule[0]+' '+cellule[1]+'" class="normalSize" / ></td>\r\n';
				contenu += '<td align="left">'+cellule[0]+' '+cellule[1]+'</td>\r\n';
				contenu += '<td align="left">'+cellule[2]+' '+cellule[3]+'</td>\r\n';
				contenu += '<td align="right">'+(cellule[4]*ratio_unites).toFixed(2)+' &euro;<span id="p-content-'+i+'"><input type="hidden" name="delai" value="'+cellule[2]+' '+cellule[3]+'"><input type="hidden" name="p" value="'+cellule[4]+'"><input type="hidden" name="md5" value="'+cellule[5]+'"></span></td>\r\n';
				contenu += '</tr>\r\n';
				contenu += '<tr id="a2-'+i+'"><td colspan="4" class="pointBleu"></td>\r\n';
			}
		}
		contenu +='</table><br>\r\n';
		document.getElementById('reports').innerHTML=contenu;
	}
}
function checkgetvars() {
	var pays=getVar('pays');
	if(pays!="") {
		addType();
		var option = document.form000.prestation;
		var selectedreport=getVar('selectedreport');
		option[selectedreport].checked=true;
		selectreport(0);
	}
}
function getVar (nomVariable)
{
	var infos = location.href.substring(location.href.indexOf("?")+1, location.href.length)+"&"
	if (infos.indexOf("#")!=-1)
		infos = infos.substring(0,infos.indexOf("#"))+"&"
	var variable=0
	{
		nomVariable = nomVariable + "="
		var taille = nomVariable.length
		if (infos.indexOf(nomVariable)!=-1)
			variable = infos.substring(infos.indexOf(nomVariable)+taille,infos.length).substring(0,infos.substring(infos.indexOf(nomVariable)+taille,infos.length).indexOf("&"))
	}
	return variable
}

