﻿function OpenLocalite(param) { var lw=window.open("localite.php?"+param,"Aide","width=400,height=400,scrollbars"); lw.focus(); return true; }

function SetParamOpener(param, field) {
	if(field=='localite') { opener.document.Search.localite.value=param; }
}


function ShowContent(d) {
	if(d.length < 1) { return; }
	document.getElementById(d).style.display = "";
}

function HideContentAll(d,e,t) {
	if(d.length < 1) { return; }
	for(i=1;i<=t;i++) {
		if(document.getElementById(d+i)) {
			if(i!=e) { document.getElementById(d+i).style.display = "none"; }
		}
		else {/*alert('er: ' + d+i)*/}
	}
}

function HideContent(d) {
	if(d.length < 1) { return; }
	document.getElementById(d).style.display = "none";
}

function ReverseContentDisplay(d) {
	if(d.length < 1) { return; }
	if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = ""; }
	else { document.getElementById(d).style.display = "none"; }
}

function show_dialog(myurl, mytitle) {
	$(function() {
		var $dialog = $('<div></div>')
			.load(myurl)
			.dialog({autoOpen: false, title: unescape(mytitle), modal: true, width: 770, height: 400});
		$dialog.dialog('open');
	});
}

function get_photo(param, w, h) { var fw = window.open('view_photo.php?p='+param, 'photo', 'width=' + w + ',height=' + h + ',toolbar=no, scrollbars=no, status=no, menubar=no, resizable=no'); fw.focus(); }

function CheckIsEmpty(Num) {
	var vide=true;
	for (var i=0; i<Num.length; i++) {
		var ch=Num.substring(i,i+1);
		if (ch!='') { vide=false; }
	}
	return vide;
}

function CleanupFormInput(Str) {
	Str = Str.replace(/%/g, "");
	Str = Str.replace(/\?/g, "");
	Str = Str.replace(/\*/g, "");
	Str = Str.replace(/</g, "");
	Str = Str.replace(/>/g, "");
	Str = Str.replace(/\&/g, "");
	Str = Str.replace(/\.\.\./g, "");

	return Str;
}

function CheckFormSearch(lang) {
 	var errmsg;
	errmsg="";

	var pnom = document.Search.nom.value;
	var plocalite = document.Search.localite.value;

	pnom = CleanupFormInput(pnom);
	plocalite = CleanupFormInput(plocalite);

	if (CheckIsEmpty(pnom)) {
		if(lang=='fr') {
			errmsg=errmsg+"Le champ activit\351 et nom est obligatoire.\n";
		}
		else if(lang=='en') {
			errmsg=errmsg+"The field activity and name must be filled.\n";
		}
		else if(lang=='ar') {
			errmsg=errmsg+"يرجى تدوين حقل البحث النشاط أو الأسم\n";
		}
	}

	if (!CheckIsEmpty(pnom) && pnom.length<3) {
		if(lang=='fr') {
			errmsg=errmsg+"Veuillez saisir 3 lettres au minimum dans le premier champ .\n";
		}
		else if(lang=='en') {
			errmsg=errmsg+"You should type at least 3 characters in the first field.\n";
		}
		else if(lang=='ar') {
			errmsg=errmsg+"يجب عليك أن تدخل 3 أحرف على الأقل في الحقل الأول\n";
		}
	}
	if (!CheckIsEmpty(plocalite) && plocalite.length<2) {
		if(lang=='fr') {
			errmsg=errmsg+"Veuillez saisir 3 lettres au minimum dans le champ [Localit\351].\n";
		}
		else if(lang=='en') {
			errmsg=errmsg+"You should type at least 3 characters in the [City] field.\n";
		}
		else if(lang=='ar') {
			errmsg=errmsg+"يجب عليك أن تدخل 3 أحرف على الأقل في حقل البلدة\n";
		}
	}


	if (!CheckIsEmpty(errmsg)) { alert (errmsg); return false; }
	return true;
}

function CheckFormInscription() {
 	var errmsg;
	errmsg="";
	if (CheckIsEmpty(document.inscrip.RSocial.value)) { errmsg=errmsg+"Le champ [Raison Sociale ou Nom] est obligatoire.\n"; }
	if (CheckIsEmpty(document.inscrip.Activite.value)) { errmsg=errmsg+"Le champ [Activit\351] est obligatoire.\n"; }
	if (CheckIsEmpty(document.inscrip.Tel.value)) { errmsg=errmsg+"Le champ [T\351l\351phone] est obligatoire.\n"; }
	
	if (!CheckIsEmpty(document.inscrip.Tel.value) && !CheckTelFax(document.inscrip.Tel.value)) {
		errmsg=errmsg+"Le champ [T\351l\351phone] est incorrect.\n"; 
	}
	
	if (!CheckIsEmpty(document.inscrip.Fax.value) && !CheckTelFax(document.inscrip.Fax.value)) {
		errmsg=errmsg+"Le champ [Fax] est incorrect.\n"; 
	}
	
	if (!CheckIsEmpty(document.inscrip.GSM.value) && !CheckGSM(document.inscrip.GSM.value)) {
		errmsg=errmsg+"Le champ [GSM] est incorrect.\n"; 
	}
	
	if (!CheckIsEmpty(errmsg)) { alert (errmsg); return false; }
	return true;
}

function CheckTelFax(Num) {
	var d = Num.substring(0,1)
	if (!(d == 7 || d == 9 || d == 2 || d == 5 || d == 3)) {
		return false;
	}
	else {
		if (Num.length != 8) {
			return false;
		}
		else {
			for (var i=0; i<Num.length; i++) {
				ch = Num.substring(i,i+1)
				if (!(ch>="0" && ch<="9"))
					return false;
			}
			return true;
		}
	}
}

function CheckGSM(Num) {
	var d = Num.substring(0,1)
	if (!(d == 9 || d == 2 || d == 5 || d == 3)) {
		return false;
	}
	else {
		if (Num.length != 8) {
			return false;
		}
		else {
			for (var i=0; i<Num.length; i++) {
				ch = Num.substring(i,i+1)
				if (!(ch>="0" && ch<="9"))
					return false;
			}
			return true;
		}
	}
}



/*****************************************************************************/

function get_rub(param,iddiv) {
	var divname="#activite" + iddiv;
	$(divname).empty();
	$(function() {
		$.post("societe_rubriques.php?p=" + param, {iddiv: iddiv}, function(data) {
			$(divname).html(data);
		});
	});
	HideContentAll('activite', iddiv, 100);
	//HideContentAll('numero', iddiv, 100);
	//HideContentAll('email', iddiv, 100);
	ShowContent('activite' + iddiv);
}

function get_num(param,iddiv) {
	var divname="#numero" + iddiv;
	$(divname).empty();
	$(function() {
		$.post("adresse_numeros.php?p=" + param, {iddiv: iddiv}, function(data) {
			$(divname).html(data);
		});
	});
	HideContentAll('numero', iddiv, 100);
	HideContentAll('email', iddiv, 100);
	ShowContent('numero' + iddiv);
}

function get_email(param,iddiv) {
	var divname="#email" + iddiv;
	$(divname).empty();
	$(function() {
		$.post("adresse_emails.php?p=" + param, {iddiv: iddiv}, function(data) {
			$(divname).html(data);
		});
	});
	HideContentAll('numero', iddiv, 100);
	HideContentAll('email', iddiv, 100);
	ShowContent('email' + iddiv);
}


// Disable right-click
var message="Sorry, right-click has been disabled.";

function clickIE() {if(document.all) {/*alert(message);*/ return false; }}

function clickNS(e) {if(document.layers||(document.getElementById&&!document.all)) { if(e.which==2||e.which==3) {/*alert(message);*/return false; } }}

if(document.layers) {document.captureEvents(Event.MOUSEDOWN); document.onmousedown=clickNS;}
else { document.onmouseup=clickNS; document.oncontextmenu=clickIE;}

document.oncontextmenu=new Function("return false");

// Disable content selection
function disableSelection(target) {
	//IE route
	if (typeof target.onselectstart!="undefined") { target.onselectstart=function(){return false;} }
	//Firefox route
	else if (typeof target.style.MozUserSelect!="undefined") { target.style.MozUserSelect="none"; }
	//All other route (ie: Opera)
	else { target.onmousedown=function(){return false;}}
	target.style.cursor = "default";
}

