// Global variables
var submitcount=0;
var payForCats = "*****";
var prevCat = "";
var ie4 = (document.all) ? true : false;
var ns4 = (document.layers) ? true : false;
var ns6 = (document.getElementById && !document.all) ? true : false;

/////////////////////////// Trim functions
function ltrim ( s )
{
	return s.replace( /^\s*/, "" );
}
function rtrim ( s )
{
	return s.replace( /\s*$/, "" );
}
function trim ( s )
{
	return rtrim(ltrim(s));
} ////////////////////////// end Trim functions

// Valider email
function test(src) {
  var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
  return regex.test(src);
}

// Get Value of input radio by id
// param c = categorie
// returns number of months chosen for a categorie
function GetRadioValue(c) {
	var found = 0;
	var i = 0;
	while (found==0) {
		//alert("c"+c+"r"+i);
		if (ie4) {if (document.all["c"+c+"r"+i].checked) return document.all["c"+c+"r"+i].value;}
		if (ns4) {if (document.layers["c"+c+"r"+i].checked) return document.layers["c"+c+"r"+i].value;}
		if (ns6) {if (document.getElementById(["c"+c+"r"+i]).checked) return document.getElementById(["c"+c+"r"+i]).value;}
		
		i++;
	} //while
	
} // function GetRadioValue

function GetMois(dPrix) {
	blankPos=dPrix.indexOf(" ");
	return dPrix.substring(0,blankPos);
} // function GetMois

function GetPrix(dePrix) {
	bPos=dePrix.indexOf(" ");
	return dePrix.substring(bPos+1);
} // function GetPrix

// Valider les champs
function Valider() {
		
	// Trim les valeurs
	document.form_ins.choix.value = trim(document.form_ins.choix.value);
	document.form_ins.categorie.value = trim(document.form_ins.categorie.value);
	document.form_ins.nom.value = trim(document.form_ins.nom.value);
	document.form_ins.titre.value = trim(document.form_ins.titre.value);
	document.form_ins.commentaire.value = trim(document.form_ins.commentaire.value);
	document.form_ins.email.value = trim(document.form_ins.email.value);
	document.form_ins.url.value = trim(document.form_ins.url.value);
	
	// tester si vides
	if (document.form_ins.choix.value == "") 			{ alert("Vous n'avez pas choisi une région"); 			return false; }
	if (document.form_ins.categorie.value == "") 		{ alert("Vous n'avez pas choisi une rubrique"); 		return false; }
	if (document.form_ins.nom.value == "") 				{ alert("Vous n'avez pas saisi votre nom"); 			return false; }
	if (document.form_ins.titre.value == "") 			{ alert("Vous n'avez pas saisi le titre d'annonce"); 	return false; }
	if (document.form_ins.commentaire.value == "") 		{ alert("Vous n'avez pas saisi votre annonce"); 		return false; }
		
	// valider email
 	if (document.form_ins.email.value != "") { 
 		if (!test(document.form_ins.email.value)) 		{ alert("Votre e-mail est invalide"); 		return false; }	
	}
	
	// valider url
	site=document.form_ins.url.value;
 	if (site != "") {
 		if (site.indexOf("http://") != 0) 		{ alert("Votre site est invalide. \n\nSept premières lettres de votre site doivent être \n\nhttp://"); 		return false; }	
	}
	
	// all OK -- submit
	if (submitcount == 0)
	{
		document.form_ins.duree.value = GetRadioValue(document.form_ins.categorie.value);
		
		// submit
		submitcount++;
		
		// confirm boxes ask the user to confirm the details
		
		cmess="";
		cmess=cmess+"Région:\n";
		cmess=cmess+document.form_ins.choix.value+"\n\n";
		cmess=cmess+"Rubrique:\n";
		optionSelected=document.form_ins.categorie.selectedIndex;
		categ=document.form_ins.categorie.options[optionSelected].text;
		cmess=cmess+categ+"\n\n";
		cmess=cmess+"Nom:\n";
		cmess=cmess+document.form_ins.nom.value+"\n\n";
		cmess=cmess+"Titre de l'annonce:\n";
		cmess=cmess+document.form_ins.titre.value+"\n\n";
		if (document.form_ins.email.value != "") cmess=cmess+"E-mail:\n";
		if (document.form_ins.email.value != "") cmess=cmess+document.form_ins.email.value+"\n\n";
		if (document.form_ins.url.value != "") cmess=cmess+"Votre site:\n";
		if (document.form_ins.url.value != "") cmess=cmess+document.form_ins.url.value+"\n\n";
		if (document.form_ins.telephone.value != "") cmess=cmess+"Téléphone:\n";
		if (document.form_ins.telephone.value != "") cmess=cmess+document.form_ins.telephone.value+"\n\n";
		cmess=cmess+"Confirmez vos détails?";		
		if(!confirm(cmess)) { submitcount=0; return; }
		
		cmess="";
		cmess=cmess+"Votre annonce:\n\n";
		cmess=cmess+document.form_ins.commentaire.value+"\n\n";
		cmess=cmess+"Confirmez votre annonce?";
		if(!confirm(cmess)) { submitcount=0; return; }
		
		dureePrix=document.form_ins.duree.value;
				
		// disable buttons and submit
		//document.form_ins.enr.disabled=true; 
		//document.form_ins.enr2.disabled=true;
		//document.form_ins.eff.disabled=true; 
		//document.form_ins.eff2.disabled=true;
		document.form_ins.submit();
	}
	else 
	{	
		// block submit
	}

} // function Valider


//////////////////////////////////////// LAYERS 
function roundDec(number,X) {
	// rounds number to X decimal places, defaults to 2
	x = (!X ? 2 : X);
	return Math.round(number*Math.pow(10,x))/Math.pow(10,x);
}

function addUp(a, how) {
	a=a+"";
	how=how+"";
	if (how=="oncat") a=GetRadioValue(a);
		
	if (how != "0") {
		p=GetPrix(a);
		if (p=="gratuit") p="0.00";
		document.form_ins.prix_annonce.value=p;
	} 
	else {
		p=document.form_ins.prix_annonce.value;
	}
	
	pURL=document.form_ins.prix_url.value;
	
	textURL=document.form_ins.url.value;
	if (pURL=="gratuit" || trim(textURL) == "") pURL="0.00";	
	document.form_ins.prix_lien.value=pURL;
	prixTot=parseFloat(p)+parseFloat(pURL);
	ptRound=roundDec(prixTot);
	ptString="a"+ptRound;
	pointPos=ptString.indexOf(".");
	decPlaces=ptString.substring(pointPos+1)
	if (pointPos == -1) ptRound=ptRound+".00";
	if (pointPos != -1 && decPlaces.length < 2) ptRound=ptRound+"0";
	
	document.form_ins.prix_total.value=ptRound;
} // function addUp

function TotalOnRadio(prixDuree) {
	addUp(prixDuree, "onradio");
} // function TotalOnRadio

function categorieChange(cat) {
	
	// populate prix_url	
	if (cat=="") { 
		document.form_ins.votre_site.value="";
		document.form_ins.prix_url.value="";
		prixPourURL="0";
		document.form_ins.prix_annonce.value="0.00";
		document.form_ins.prix_total.value="0.00";
	} 
	else {
		optionSelect=document.form_ins.categorie.selectedIndex;
		//prixPourURL=document.form_ins.categorie.options[optionSelect].label;
		prixPourURL=0;		
		document.form_ins.prix_url.value=prixPourURL;
		document.form_ins.votre_site.value="Votre site "+prixPourURL+(prixPourURL=="gratuit"?"":" Euro");
		
		// calculate prix total
		addUp(cat, "oncat");
	} // else
	
	if (cat=="") {
		if (ie4) {
			if (prevCat != "") {
				document.all["rlayer"+prevCat].style.left = -5000;					
				document.all["player"].style.left = -5000;
			}
			document.all["blayer2"].style.left = 0;
		}
		
		if (ns4) {
			if (prevCat != "") {
				document.layers["rlayer"+prevCat].style.left = -5000;					
				document.layers["player"].style.left = -5000;
			}
			document.layers["blayer2"].style.left = 0;
		}
		
		if (ns6) {
			if (prevCat != "") {
				document.getElementById(["rlayer"+prevCat]).style.left = -5000;					
				document.getElementById(["player"]).style.left = -5000;
			}
			document.getElementById(["blayer2"]).style.left = 0;
		}
		
		prevCat = ""; // prev paying categ does not exist
	}
	else {
		//alert(payForCats+"--"+cat);
		
		foundCat=payForCats.indexOf("*"+cat+"*")
		// if pUrl is not free - we still show payment section
		pUrl=document.form_ins.prix_url.value;
		
		// if categ is paying categ then show duration radio buttons
		if (foundCat != -1 || pUrl != "gratuit" ) {
			
			if (ie4) {
				if (prevCat != "") {
					document.all["rlayer"+prevCat].style.left = -5000;					
				}
				else {
					document.all["player"].style.left = 200;
					document.all["blayer2"].style.left = -5000;
				}
				if (foundCat != -1) document.all["rlayer"+cat].style.left = 0;					
			}
			
			if (ns4) {
				if (prevCat != "") {
					document.layers["rlayer"+prevCat].style.left = -5000;					
				}
				else {
					document.layers["player"].style.left = 200;
					document.layers["blayer2"].style.left = -5000;
				}
				if (foundCat != -1) document.layers["rlayer"+cat].style.left = 0;					
			}
			
			if (ns6) {
				if (prevCat != "") {
					document.getElementById(["rlayer"+prevCat]).style.left = -5000;					
				}
				else {
					document.getElementById(["player"]).style.left = 200;
					document.getElementById(["blayer2"]).style.left = -5000;
				}
				if (foundCat != -1) document.getElementById(["rlayer"+cat]).style.left = 0;					
			}
			
			prevCat = cat;
			
		} // if
		else { // there might be a categ to be hidden

			if (ie4) {
				if (prevCat != "") {
						document.all["rlayer"+prevCat].style.left = -5000;					
						document.all["player"].style.left = -5000;
					}
					document.all["blayer2"].style.left = 0;
			}
			
			if (ns4) {
				if (prevCat != "") {
					document.layers["rlayer"+prevCat].style.left = -5000;					
					document.layers["player"].style.left = -5000;
				}
				document.layers["blayer2"].style.left = 0;
			}
			
			if (ns6) {
				if (prevCat != "") {
					document.getElementById(["rlayer"+prevCat]).style.left = -5000;					
					document.getElementById(["player"]).style.left = -5000;
				}
				document.getElementById(["blayer2"]).style.left = 0;
			}
			
			prevCat = ""; // this categ was not paying so reset prev paying categ														
		}
	} // else
	
} // function categorieChange

function resetForm() {
	categorieChange("");
	document.form_ins.reset(); 
} // function resetForm

function ouvreSite() {
	if (trim(document.form_ins.url.value) != "") window.open(document.form_ins.url.value);
} // function resetForm

//////////////////////////////////////// END LAYERS 



   
   
