var regPhone = new RegExp(/^[0-9]{10}$/);
var regPhone = new RegExp(/^[0-9]{10}$/);
var regNum = new RegExp(/^[0-9]$/);
var mailReg = new RegExp(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,8})+$/);
/****************************************************************************************************/
$(function(){
	$('#envoyer-contact').bind('click',validContact);	
	$('#trouver_entre').bind('click',trouver_entre);	
	$('#trouver_produit').bind('click',trouver_produit_service);	
	$('.pagination').bind('click',trouver_entre_pagination);	
	$('#partager_mail').bind('click',validatePopPartager);
	//zone de recherche des entreprises
		$('#entreprise').html('Tapez le nom d\'entreprise ou mots cles pour des produits ou services');
		$('#entreprise').bind('click',function(){
			if($(this).hasClass('txt-descript')){
				$('#entreprise').val('');
				$('#entreprise').removeClass('txt-descript');
			}
		});
		//zone de recherche des produits
		$('#produit_service').html('Tapez mots cles pour des produits ou services');
		$('#produit_service').bind('click',function(){
			if($(this).hasClass('txt-descript')){
				$('#produit_service').val('');
				$('#produit_service').removeClass('txt-descript');
			}
		});
		$("#photos1").change(function (){ 
				var photo = $("#photos1").val(); 
				$('.getPhoto1').html("le fichier telecharge est :"+photo);
		});
		$("#photos2").change(function (){ 
				var photo = $("#photos2").val(); 
				$('.getPhoto2').html("le fichier telecharge est :"+photo);
		});
		$("#photos3").change(function (){ 
				var photo = $("#photos3").val(); 
				$('.getPhoto3').html("le fichier telecharge est :"+photo);
		});
		$("#photos4").change(function (){ 
				var photo = $("#photos4").val(); 
				$('.getPhoto4').html("le fichier telecharge est :"+photo);
		});
		$("#photos5").change(function (){ 
				var photo = $("#photos5").val(); 
				$('.getPhoto5').html("le fichier telecharge est :"+photo);
		});
		$("#photos6").change(function (){ 
				var photo = $("#photos6").val(); 
				$('.getPhoto6').html("le fichier telecharge est :"+photo);
		});
		//popup overture
		if($('#popinOverlay').length){
			resizeBlocOverlay();
			resizeOverlayForPopin();	
		}
	
		/*$('#mycarousel').jcarousel({
			auto: 2,
			wrap: 'last',
			initCallback: mycarousel_initCallback
		});*/
		//getAllSociete();
		getSousCat();
		//lireUrl();
		
});	
function validatePopPartager(){
	formEmail = $('#formEmail').val();
	formName = $('#formName').val();
	formEmailAmi = $('#formEmailami').val();
	formNameAmi = $('#formNameAmi').val();
	
	var flag=true;
	var err = "Veuillez remplir les champs suivant : \n" ;
	
	if(!formName){
		 err += " - Votre nom \n";
		flag = false;
	}
	
	if(!mailReg.test(formEmail)){
		 err += " - Votre Adresse E-mail valide \n";
		flag = false;
	}
	if(!formNameAmi){
		 err += " - Nom de votre ami(e) \n";
		flag = false;
	}
	if(!mailReg.test(formEmailAmi)){
		 err += " - Adresse E-mail valide de votre ami(e) \n";
		flag = false;
	}
	if(!flag){	
		alert(err);
		return false;
	}else{
		var vars = 'action=sendMailAmi&formName='+formName+'&formEmail='+formEmail+'&formNameAmi='+formNameAmi+'&formEmailAmi='+formEmailAmi
		$.ajax({
			type: "POST",url: "../../includes/config.inc.php", data: vars,
			success: function(data){
				if (data == -1){
					alert('l\'envoi d\'email n\'a pas été effectué !');
				}else{
					//$('#libelle_sous_groupe').val('');
					closePopin('popinPartager');	
					
				}
			}
		}); 
	}
}
	
function getSousCat(){
	
	$('#categorie_parent').bind('click',function(){
		categorie_parent = $('#categorie_parent').val();
		if(categorie_parent!="0"){ 
			var vars = 'action=getSousCatByCat&element='+categorie_parent;
			$.ajax({
				type: "POST",url: "../../includes/config.inc.php", data: vars,
				success: function(data){
					if (data){	
						$('.blok-sousElement').show();
						$('.blok-sousElement').html(data);
						//getS_sousCat(categorie_parent);
						return true;
					}else{
						$('.blok-sousElement').html('');
					}
				}
			}); 	
		}
	});	
}
function getS_sousCat(val){
	$('#categorie_parent_'+val).bind('click',function(){
		categorie_parent = $('#categorie_parent_'+val).val();
		if(categorie_parent!="0"){ 
			var vars = 'action=getSousCatByCat&element='+categorie_parent;
			$.ajax({
				type: "POST",url: "../../includes/config.inc.php", data: vars,
				success: function(data){
					if (data){	
						//$('.blok-sousElement').show();
						$('select#categorie_parent_'+val).after(data);
						getS_sousCat(categorie_parent);
						return false;
					}
				}
			}); 	
		}
	});	
}
function trouver_entre(){
	idEntre = $('#entreprise').val();
	var flag=true;
	
	if(!idEntre || ( idEntre =='Tapez le nom d\'entreprise')){
		var err = " Veuillez taper un element ";
		flag = false;
	}
	
	if(!flag){	
		alert(err);
		return false;
	}else{
		
		//ajax_page('.offres-pager','<div style="margin-left:150px;"><img src="../images/ajax-loader.gif" style="text-align: center"/></div>',this.href);
		//ajax_page('.block-php','<div style="margin-left:150px;"></div>',this.href);
		//return false;
		$("#send2").ajaxStart(function(){
		   $(this).show();
		 });		
		var vars = 'action=getElement&element='+idEntre;
		$.ajax({
			type: "POST",url: "../../includes/config.inc.php", data: vars,
			success: function(data){
				if (data){	
					//alert(data);
					//window.location="http://127.0.0.1:8888/quebec-i/resultat.php";
					//$('.block-php').html('');
					$("#send2").hide();
					$(".chemins-link").hide();
					$("#deffil-images").hide();
					$(".select-ville").hide();
					$('.block-php').html(data);
					$('.block-php').show();
					$('.infor-suppl').hide(data);
					$('.cordonne-entreprise').hide(data);
					$('.block-description').hide(data);
					$('#bloc-categories').hide(data);
					$('.infor-suppl').hide(data);
					$('#entreprise').addClass('txt-descript');
				}else{
					$('.block-php').html('');
				}
			}
		}); 	
	}
}
function trouver_produit_service(){
	idEntre = $('#produit_service').val();
	var flag=true;
	
	if(!idEntre || ( idEntre =='Tapez mots cles pour des produits ou services')){
		var err = " Veuillez taper un element ";
		flag = false;
	}
	
	if(!flag){	
		alert(err);
		return false;
	}else{
		
		//ajax_page('.offres-pager','<div style="margin-left:150px;"><img src="../images/ajax-loader.gif" style="text-align: center"/></div>',this.href);
		//ajax_page('.block-php','<div style="margin-left:150px;"></div>',this.href);
		//return false;
		$("#send2").ajaxStart(function(){
		   $(this).show();
		 });		
		var vars = 'action=getElement&element='+idEntre;
		$.ajax({
			type: "POST",url: "../../includes/config.inc.php", data: vars,
			success: function(data){
				if (data){	
					//alert(data);
					//window.location="http://127.0.0.1:8888/quebec-i/resultat.php";
					//$('.block-php').html('');
					$("#send2").hide();
					$(".chemins-link").hide();
					$("#deffil-images").hide();
					$(".select-ville").hide();
					$('.block-php').html(data);
					$('.block-php').show();
					$('.infor-suppl').hide(data);
					$('.cordonne-entreprise').hide(data);
					$('.block-description').hide(data);
					$('#bloc-categories').hide(data);
					$('.infor-suppl').hide(data);
					$('#entreprise').addClass('txt-descript');
				}else{
					$('.block-php').html('');
				}
			}
		}); 	
	}
}
function trouver_entre_pagination(){
	element = $('#element').val();
	page = $('#page').val();

	$("#send2").ajaxStart(function(){
	   $(this).show();
	 });		
	var vars = 'action=getElementByPage&element='+element+'&page='+page;
	$.ajax({
		type: "POST",url: "../../includes/config.inc.php", data: vars,
		success: function(data){
			if (data){	
				//alert(data);
				$("#send2").hide();
				$(".chemins-link").hide();
				$("#deffil-images").hide();
				$(".select-ville").hide();
				$('.block-php').html(data);
				$('.block-php').show();
				$('.infor-suppl').hide(data);
				$('.cordonne-entreprise').hide(data);
				$('.block-description').hide(data);
				$('#bloc-categories').hide(data);
				$('.infor-suppl').hide(data);
				$('#entreprise').addClass('txt-descript');
			}else{
				$('.block-php').html('');
			}
		}
	}); 	

}
function validContact(){
	nom = $('#nom').val();
	tel = $('#tel').val();
	email = $('#email').val();
	commentaire = $('#commentaires').val();
	code_img = $('#code_img').val();
		
		
	var flag=true;
	var err = "Veuillez remplir les champs suivant : \n" ;
	
	if(!nom){
		 err += " - Nom et prenom \n";
		flag = false;
	}
	if(!regPhone.test(tel)){
		 err += " - Numero de telephone valide \n";
		flag = false;
	}
	if(!mailReg.test(email)){
		 err += " - Adresse E-mail valide \n";
		flag = false;
	}
	if(!code_img){
		 err += " - Le code affich顜n";
		flag = false;
	}
	if(!flag){	
		alert(err);
		return false;
	}/*else{
		var vars = 'action=addContact&nom='+nom+'&tel='+tel+'&mail='+email+
		'&commentaire='+commentaire
		$.ajax({
			type: "POST",url: "http://127.0.0.1:8888/quebec-i/includes/config.inc.php", data: vars,
			success: function(data){
				if (data == -1 ){
					alert('Erreur d\'envoi !');
				}else{
					$('#envoi-contact').html('');
					$('.message_envoye').show();
					//$('.tt_rouge').hide();
					
				}
			}
		}); 	
		return false;
	
	}*/

}
function valid_entre(){
	raison_social = $('#raison_social').val();
	adresse1 = $('#adresse1').val();
	adresse2 = $('#adresse2').val();
	email = $('#email').val();
	password = $('#password').val();
	tel = $('#tel').val();
	fax = $('#fax').val();
	cp = $('#cp').val();
	ville = $('#ville').val();
	site = $('#site').val();
	nom_resp = $('#nom_resp').val();
	email_resp = $('#email_resp').val();
	tel_resp = $('#tel_resp').val();
	categorie_parent = $('#categorie_parent').val();
	categorie_fils = $('#categorie_parent_'+categorie_parent).val();
	produit_service = $('#produit_service').val();
	description = $('#description').val();
	key_word = $('#key_word').val();
	logo = $('#logo').val();
	photos1 = $('#photos1').val();
	photos2 = $('#photos2').val();
	photos3 = $('#photos3').val();
	photos4 = $('#photos4').val();
	photos5 = $('#photos5').val();
	photos6 = $('#photos6').val();
			
	
	var vars = 'action=addEntre&raison_social='+raison_social+'&adresse1='+adresse1+'&adresse2='+adresse2+
				'&email='+email+'&password='+password+'&tel='+tel+
				'&fax='+fax+'&cp='+cp+'&ville='+ville+'&site='+site+'&nom_resp='+nom_resp+'&email_resp='+email_resp+
				'&tel_resp='+tel_resp+'&categorie_parent='+categorie_parent+'&categorie_fils='+categorie_fils+'&produit_service='+produit_service+'&description='+description+'&key_word='+key_word+
				'&logo='+logo+'&photos1='+photos1+'&photos2='+photos2+'&photos3='+photos3
				'&photos4='+photos4+'&photos5='+photos5+'&photos6='+photos6
	$.ajax({
		type: "POST",url: "../../includes/config.inc.php", data: vars,
		success: function(data){
			if (data == -1 ){ alert(data);
				alert('Erreur d\'envoi !');
			}else{
				$('#envoi-contact').html('');
				$('.message_envoye').show();
				//$('.tt_rouge').hide();
				
			}
		}
	}); 	
	return false;
	
	

}
function getEntrepriseByVille(){
	var categorie = $('#id_cat_ville').val();
	var url = $('#url').val();
	
	var ville_1 = $('#1').val();
	var ville_2 = $('#2').val();
	var ville_3 = $('#3').val();
	var ville_4 = $('#4').val();
	var ville_8 = $('#8').val();
	var ville_7 = $('#7').val();
	var ville_6 = $('#6').val();
	var ville_5 = $('#5').val();
	var ville_9 = $('#9').val();
	var ville_10 = $('#10').val();
	var ville_11 = $('#11').val();
	
	
	$(".checkbox-ville").unbind();
	$(".checkbox-ville").bind('click',function(){
		
		if($(this).attr('checked')=='checked'){	
			var	val = $(this).val();
			var vars = 'action=getEntrByVille&id_ville='+val+'&ville_1='+ville_1+'&ville_2='+ville_2+'&ville_3='+ville_3+
			'&ville_4='+ville_4+'&ville_5='+ville_5+'&ville_6='+ville_6+'&ville_7='+ville_7+'&ville_8='+ville_8+
			'&ville_9='+ville_9+'&ville_10='+ville_10+'&ville_11='+ville_11+'&id_cat='+categorie;
			$.ajax({
				type: "POST",url: "../../includes/config.inc.php", data: vars,
				success: function(data){ 
					//$('.block-php').html('');
					$('.block-php').html(data);
					//window.location="../../"+url+"-ville-"+val+".html";
				}
			}); 
		}else{//
		//alert($(this).attr('checked'));
			window.location="../../"+url+".html";
			
		}
	});

}
/*********************keyup name societe****************/
function getNameSociete(){
	$('input#idRaisonsocial').val('');
	if($(this).val()){
		var vars = 'action=getNameSociete&nameSociete='+$(this).val();
		$.ajax({
			type: "POST",url: "../../includes/config.inc.php", data: vars,
			success: function(data){
				if (data){	
					$('ul.listName').html(data);
					$('ul.listName').show();
					eventListSociete();
				}else{
					$('ul.listName').html('');
					$('ul.listName').hide();
				}
			}
		}); 	
	}
}
function getAllSociete(){
		$("#send3").ajaxStart(function(){
		   $(this).show();
		 });
		var vars = 'action=getAllSociete';
		$.ajax({
			type: "POST",url: "../../includes/config.inc.php", data: vars,
			success: function(data){
				if (data){	
				console.log(data);
					data = data.split(",");
					$("#send3").hide();
					$('#entreprise').autocomplete(data);
				}else{
					$('#entreprise').html('');

				}
			}
		}); 	
	
}
function getAllProduits(){
		$("#send3").ajaxStart(function(){
		   $(this).show();
		 });
		var vars = 'action=getAllProduits';
		$.ajax({
			type: "POST",url: "../../includes/config.inc.php", data: vars,
			success: function(data){
				if (data){	
				//alert(data);
				console.log(data);
					data = data.split(",");
					$("#send3").hide();
					$('#produit_service').autocomplete(data);
				}else{
					$('#produit_service').html('');

				}
			}
		}); 	
	
}
function lireUrl(){
	var uriCourant = document.location;
	alert(uriCourant);

	var indiceDepart = uriCourant.lastIndexOf('/')+1; // Plus 1 car on ne veut pas le '/'.
	var indiceArrivee = uriCourant.length-4; // Moins les 4 caract鳥s de l'extension.

	var nomPage = uriCourant.substring(indiceDepart, indiceArrivee);
	alert(nomPage); 
}
function addChemins(){
		val=$('#id_cat').val();
		var vars = 'action=addChemins&val='+val
		$.ajax({
			type: "POST",url: "../../includes/config.inc.php", data: vars,
			success: function(data){
				if (data){	
					$('div.chemins-link').next().html(data);
				}else{
					$('div.chemins-link').next().html('');
				}
			}
		}); 	
}
function eventListSociete(){	
		
	/* evenemet sur les liens de la liste de noms des commercials */
	
	$('ul.listName a').bind('click',function(){
		var idSociete = $(this).attr('rel');
		$('#entreprise').val($(this).text());
		$('ul.listName').hide();
		//$('#field_client_societe').show();
		$('input#idRaisonsocial').val(idSociete);
		//$('input#field_client_particulier').val(idClient);
		return false;
	})
}
function showPopin(id){	
	var overlay = $('#popinOverlay');	
	overlay.css({
		opacity: 0.7,
		display: 'block'
	}).fadeIn();
	
	$('#'+id).show();
	
	setPopinPosition(id);
	$('.btClose,.btReset').bind('click', function(){
		closePopin(id);
		return false;
	})
}

function closePopin(id){
	var overlay = $('#popinOverlay');
	overlay.hide();
	$('#'+id).hide();
}

function setPopinPosition(id){
	var dim = getPageSize();
	var scroll = getPageScroll();
	
	var popin = $('#'+id);

	popin.css({
		top: scroll[1] + (dim[3] / 10),
		left: (dim[2]-$(popin).width())/2,
		opacity: 1,
		display: 'block'
	}).fadeIn();
	
}

function resizeOverlayForPopin(){
	$(window).bind('resize',function(){
		resizeBlocOverlay();
	});
	
	$(window).bind('scroll',function(){
		resizeBlocOverlay();
	});	
}	

function resizeBlocOverlay(){
	var scroll = getPageScroll();
	var overlay = $('#popinOverlay');
	var hi = getHeight() + scroll[1];
	overlay.height(hi);
}

function getHeight(){
	if(window.innerHeight || window.innerWidth)
		return window.innerHeight ;
	return document.documentElement.clientHeight ;
}

// Renvoie les dimensions de la page
function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};

function getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
}
