// JavaScript Document

var xhr=null;

//Communication avec le serveur
function getXhr(){
	if(window.XMLHttpRequest){ //Firefox et autres
		xhr = new XMLHttpRequest();
	} else if(window.ActiveXObject){//IE
		try{
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e){
			try{
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e1){
				xhr = null;
			}
		}
	}
	else {//XMLHttpRequest non supporté par le navigateur
		alert('Utilisation Ajax impossible');
	}
	//alert (xhr);
	return xhr;

}
function ajoutPanier(img,taille,quantite,couleur){

getXhr();
	//On défini ce qu'on va faire quand on aura la réponse
	xhr.onreadystatechange = function(){
		//on ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200){
			alert("MISE A JOUR DU PANIER");
			//letexte = xhr.responseText;
			totPrice();

		}
	}

	//on envoie à l'url avec les paramètres et la valeur
	xhr.open("GET","include/ajax.php?ligne="+img+"&taille="+taille+"&quantite="+quantite+"&couleur="+couleur,true);
	//urltruc = "include/ajax.php?ligne="+img+"&taille="+taille+"&quantite="+quantite+"&couleur="+couleur ;
	//alert (urltruc);
	//prompt('url',urltruc);
	xhr.send(null);
}

function delit(){
	if (confirm("Etes-vous sur de vouloir supprimer ce membre ?")){
		return true;
	}else{
		return false;
	}
}

function maj_champs(ligne,a,img){

	champsTaille = "taille_" + ligne + "";
	champsQuantite = "quantiteprod_" + ligne + "";
	champsCouleur = "couleur_" + ligne + "";
	
	var quantite = document.getElementById(champsQuantite).value;

	//champsCouleur = "couleur_"+ligne;
	if (a){
		var taille = 99;
		var couleur = 1;
		prix = 19*quantite;
	}else{
		var taille = document.getElementById(champsTaille).value;
		var couleur = document.getElementById(champsCouleur).value;
	
		switch(taille){
			case '1':
			if (couleur == 1)
				prix = 5.5*quantite;
				else
				prix = 6.5*quantite;
			break;
			
			case '2':
			if (couleur == 1)
				prix = 15*quantite;
			else
				prix = 16*quantite;
			break;
			
			case '3':
			if (couleur == 1)
				prix = 30.5*quantite;
			else
				prix = 31.5*quantite;
			break;
			
			case '4':
			if (couleur == 1)
				prix = 65*quantite;
			else
				prix = 66*quantite;
			
			break;
			
			case '5':
			if (couleur == 1)
				prix = 88*quantite;
			else
				prix = 89*quantite;
			
			break;
			
			case '6':
			if (couleur == 1)
				prix = 100*quantite;
			else
				prix = 101*quantite;
				
			break;
			
			case '7':
			if (couleur == 1)
				prix = 150*quantite;
			else
				prix = 151*quantite;
			
			break;
		}
	}
	
	champsPrix = "prixprod_"+ligne;
	document.getElementById(champsPrix).value = prix;
	//alert(prix);
	
	//alert (''+img+'');
	//alert ("ajoutPanier("+img+","+taille+","+quantite+","+couleur+")");
	ajoutPanier(img,taille,quantite,couleur)
	
	}
	
function totPrice(){
	nbrRow = document.getElementById('totprod').value;
	//alert();
	total = 0;
		for(i=0; i<nbrRow; i++){
			champsPrix = "prixprod_"+i;
			total += Number(document.getElementById(champsPrix).value);
		}
		document.getElementById('prixtotal').value = total+5;
	}

function quantitePlus(mychamps,a,img){ //v3.0
	
	var num = mychamps.split("_");
	//alert (num[1]);
	myStock = num[1];
	//alert('TAB :: '+myStock)
	val = document.getElementById(mychamps).value;
	//alert(TabStok[myStock]+' :: '+(val+1));
	val++;
	
	//alert (val);
	document.getElementById(mychamps).value = val;
	
	if (a){
		maj_champs(num[1], 1, ''+img+'');
	}else{
		maj_champs(num[1], 0, ''+img+'');
		}
	
	
}
function quantiteMoins(mychamps,a,img){ //v3.0
  var num = mychamps.split("_");
  val = document.getElementById(mychamps).value;
  val--;
  if (val<1){
  document.getElementById(mychamps).value = 1;
  }else{
  document.getElementById(mychamps).value = val;
  }
  if (a){
		maj_champs(num[1], 1, ''+img+'');
	}else{
		maj_champs(num[1], 0, ''+img+'');
		}
  
}
function changeIMG(img){

	myDiv = document.getElementById('agrandissement');

	myDiv.innerHTML = "<img src='"+ img +"' alt='Photo mariage agrandissement' />";

}

function checkEmail(email)

{

	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) return true;

	return false;

}



// Function to check field value

function ValidateField(strFieldName)

{

	if (strFieldName.value.length>0) return true;

	return false;

}

function is_numeric(num)

	{

		var exp = new RegExp("^[0-9-.]*$","g");

		return exp.test(num);

	}



function checkSubForm()

{

	   

	msg = 'Veuillez vérifier :\n';

	bool = true;

	if (ValidateField(document.subForm.nom) == false)

	{

	msg += '- Votre nom\n';

	bool = false;

	}

	if (ValidateField(document.subForm.prenom) == false)

	{

	msg += '- Votre prénom\n';

	bool = false;

	}

	if(!is_numeric(document.subForm.tel.value))

	{

	msg += '- Le format de votre numéro de téléphone\n';

	bool = false;

	}

	if(document.subForm.tel.value.length < 10)

	{

	msg += '- La longueur de votre numéro de téléphone\n';

	bool = false;  

	}

	if(document.subForm.tel.value.substring(0, 1) != 0)

	{

	msg += '- Votre numéro de téléphone doit commencer par 0\n';

	bool = false;  

	}	

	if (checkEmail(document.subForm.email.value) == false)

	{

	msg += '- Le format de votre e-mail\n';

	bool = false;

	}



	if (bool == false)

	{

	alert(msg);

	}

	return bool;

	msg = 'Veuillez vérifier :\n';

	bool = true;

}



function adFormSub()

 {

	   

	msg = 'Veuillez vérifier :\n';

	bool = true;

	if (ValidateField(document.adForm.nom_client) == false)

	{

	msg += '- Le nom ou la réf. du client\n';

	bool = false;

	}

	if(!is_numeric(document.adForm.jour_ev.value))

	{

	msg += '- Le jour de l\'évènement\n';

	bool = false;

	}

	if(!is_numeric(document.adForm.mois_ev.value))

	{

	msg += '- Le mois de l\'évènement\n';

	bool = false;

	}

	if(!is_numeric(document.adForm.annee_ev.value))

	{

	msg += '- L\'année de l\'évènement\n';

	bool = false;

	}

	if (ValidateField(document.adForm.log_client) == false)

	{

	msg += '- L\'identifiant du client\n';

	bool = false;

	}

	if (ValidateField(document.adForm.pass_client) == false)

	{

	msg += '- Le mot de passe du client\n';

	bool = false;

	}



	if (bool == false)

	{

	alert(msg);

	}

	return bool;

	msg = 'Veuillez vérifier :\n';

	bool = true;

}

function effaceChamps(champ){

	if (champ.value == "JJ" || champ.value == "AAAA" || champ.value == "MM"){

		champ.value = "";

	}

}



function retabChamps(champ, valeur){

	if (champ.value == ""){

		champ.value = valeur;

	}

}

function gotoClient(){

	bool = true;

	if (document.getElementById('clientToShow').selectedIndex == 0){

		alert ('Veuillez choisir un client');

		bool = false;

	}

	return bool;

	bool = true;

}

/*
Simple Image Trail script- By JavaScriptKit.com
Visit http://www.javascriptkit.com for this script and more
This notice must stay intact
*/

var offsetfrommouse=[15,15]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var displayduration=0; //duration in seconds image should remain visible. 0 for always.
var currentimageheight = 270;	// maximum image size.

if (document.getElementById || document.all){
	document.write('<div id="trailimageid">');
	document.write('</div>');
}

function gettrailobj(){
if (document.getElementById)
return document.getElementById("trailimageid").style
else if (document.all)
return document.all.trailimagid.style
}

function gettrailobjnostyle(){
if (document.getElementById)
return document.getElementById("trailimageid")
else if (document.all)
return document.all.trailimagid
}


function truebody(){
return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function showtrail(imagename,title,showthumb,height,filetype){
	if (height > 0){
		currentimageheight = height;
	}

	document.onmousemove=followmouse;

	newHTML = '<div align="center" style="padding: 8px 2px 2px 2px;" 5px; background-color: #FFF; border: 1px solid #888;">';
	newHTML = newHTML + '<img src="' + imagename + '" border="0"></div>';
	newHTML = newHTML + '</div>';
	
	gettrailobjnostyle().innerHTML = newHTML;
	gettrailobj().display="inline";
	gettrailobj().position="absolute";
	//currentimageheight = $('trailInnerDiv').offsetHeight;
}

function showtrailBatch(imagename,title,filetype){
	
	document.onmousemove=followmouseBatch;
	
	newHTML = newHTML + '<div align="center" style="padding: 8px 2px 2px 2px;">';
	newHTML = newHTML + '<img src="' + imagename + '" border="0"></div>';
	newHTML = newHTML + '</div>';
	gettrailobjnostyle().innerHTML = newHTML;
	//gettrailobj().display="inline";
	gettrailobj().display="inline";
	gettrailobj().position="absolute";
	currentimageheight = $('trailInnerDiv').offsetHeight;
	
}

function hidetrail(){
	gettrailobj().innerHTML = " ";
	gettrailobj().display="none"
	document.onmousemove=""
	gettrailobj().left="-500px"

}

function followmouse(e){

	var xcoord=offsetfrommouse[0]
	var ycoord=offsetfrommouse[1]

	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)

	//if (document.all){
	//	gettrailobjnostyle().innerHTML = 'A = ' + truebody().scrollHeight + '<br>B = ' + truebody().clientHeight;
	//} else {
	//	gettrailobjnostyle().innerHTML = 'C = ' + document.body.offsetHeight + '<br>D = ' + window.innerHeight;
	//}

	if (typeof e != "undefined"){
		if (docwidth - e.pageX < 380){
			xcoord = e.pageX - xcoord - 400; // Move to the left side of the cursor
		} else {
			xcoord += e.pageX;
		}
		if (docheight - e.pageY < (currentimageheight + 110)){
			ycoord += e.pageY - Math.max(0,(110 + currentimageheight + e.pageY - docheight - truebody().scrollTop));
		} else {
			ycoord += e.pageY;
		}

	} else if (typeof window.event != "undefined"){
		if (docwidth - event.clientX < 380){
			xcoord = event.clientX + truebody().scrollLeft - xcoord - 400; // Move to the left side of the cursor
		} else {
			xcoord += truebody().scrollLeft+event.clientX
		}
		if (docheight - event.clientY < (currentimageheight + 110)){
			ycoord += event.clientY + truebody().scrollTop - Math.max(0,(110 + currentimageheight + event.clientY - docheight));
		} else {
			ycoord += truebody().scrollTop + event.clientY;
		}
	}

	if(ycoord < 0) { ycoord = ycoord*-1; }
	gettrailobj().left=xcoord+"px"
	gettrailobj().top=ycoord+"px"

}

function followmouseBatch(e){
	var xcoord=offsetfrommouse[0]
	var ycoord=offsetfrommouse[1]

	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)

	var trailInnerDiv = $('trailInnerDiv');
	var currentimageheight = trailInnerDiv.offsetHeight;
	var currentimagewidth = trailInnerDiv.offsetWidth;

	scrollPos = Position.realOffset(truebody());
	
	if (typeof e != "undefined"){
		if (docwidth - e.pageX < 380){
			xcoord = e.pageX - xcoord - 400; // Move to the left side of the cursor
		} else {
			xcoord += e.pageX;
		}
		if ((e.pageY - scrollPos[1]) + currentimageheight > docheight){
			ycoord = -ycoord + (e.pageY - currentimageheight);
		} else {
			ycoord += e.pageY;
		}
	} else if (typeof window.event != "undefined"){
		if (event.clientX + currentimagewidth > docwidth){
			xcoord = -xcoord + ((event.clientX + scrollPos[0]) - currentimagewidth); // Move to the left side of the cursor
		} else {
			xcoord += (event.clientX + scrollPos[0]);
		}
		if (event.clientY + currentimageheight > docheight){
			ycoord = -ycoord + ((event.clientY + scrollPos[1]) - currentimageheight);
		} else {
			ycoord += (event.clientY + scrollPos[1]);
		}
	}

	if(ycoord < 0) { ycoord = ycoord*-1; }

	gettrailobj().left=xcoord+"px"
	gettrailobj().top=ycoord+"px"

}
function checkSubFormCommande()

{

	   

	msg = 'Veuillez vérifier :\n';

	bool = true;

	if (ValidateField(document.subForm.nom) == false)

	{

	msg += '- Votre nom\n';

	bool = false;

	}

	if (ValidateField(document.subForm.prenom) == false)

	{

	msg += '- Votre prénom\n';

	bool = false;

	}
	if (ValidateField(document.subForm.adresse) == false)

	{

	msg += '- Votre adresse\n';

	bool = false;

	}
	if (ValidateField(document.subForm.ville) == false)

	{

	msg += '- Votre ville\n';

	bool = false;

	}

	if(document.subForm.cp.value.length < 5)

	{

	msg += '- La longueur de votre code postal\n';

	bool = false;  

	}
	if(!is_numeric(document.subForm.tel.value))

	{

	msg += '- Le format de votre numéro de téléphone\n';

	bool = false;

	}

	if(document.subForm.tel.value.length < 10)

	{

	msg += '- La longueur de votre numéro de téléphone\n';

	bool = false;  

	}

	if(document.subForm.tel.value.substring(0, 1) != 0)

	{

	msg += '- Votre numéro de téléphone doit commencer par 0\n';

	bool = false;  

	}	

	if (checkEmail(document.subForm.email.value) == false)

	{

	msg += '- Le format de votre e-mail\n';

	bool = false;

	}



	if (bool == false)

	{

	alert(msg);

	}

	return bool;

	msg = 'Veuillez vérifier :\n';

	bool = true;

}
