function Trim(TRIM_VALUE)
{
	if(TRIM_VALUE.length < 1)
	{
		return"";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if(TRIM_VALUE=="")
	{
		return "";
	}
	else
	{
		return TRIM_VALUE;
	}
}

function RTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0)
	{
		return"";
	}
	var iTemp = v_length -1;
	
	while(iTemp > -1)
	{
		if(VALUE.charAt(iTemp) == w_space)
		{
		}
		else
		{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;
	}
	return strTemp;
}

function LTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	if(v_length < 1)
	{
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";
	
	var iTemp = 0;
	
	while(iTemp < v_length)
	{
		if(VALUE.charAt(iTemp) == w_space)
		{
		}
		else
		{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	}
	return strTemp;
}

function addUnit(catID)
{
	qty=eval("document.formulaire.qty_"+catID+".value")-0+1;
	if (qty>99) {qty=99;}
	eval("document.formulaire.qty_"+catID+".value="+qty);
	addMat(catID);
}
function removeUnit(catID)
{
	qty=eval("document.formulaire.qty_"+catID+".value")-1;
	if (qty<0) {qty=0;}
	eval("document.formulaire.qty_"+catID+".value="+qty);
	addMat(catID);
}
function checkQty(catID)
{
	qty=eval("document.formulaire.qty_"+catID+".value")-0;
	if (qty>99) {qty=99;}
	if (qty<0) {qty=0;}
	eval("document.formulaire.qty_"+catID+".value="+qty);
	addMat(catID);
}

matQty=Array();	//used to store quantities associated with each hardware element

function addMat(catID)
{
	explication=true;

	if (catID=='autre')
	{//if the user wants to add a materiel that doesn't exist in the lists
		autre_item=Trim(document.formulaire.mat_autre.value);
		matID=autre_item.replace('"',"''");
		matID=autre_item.replace('&','-');  //we need to replace this character because we use it to seperate the items in the quote list
		matName=matID+" (autre)";
	}
	else
	{//if the user is adding a materiel that exists in the lists
		cmbMat=eval("document.formulaire.cmb_"+catID);
		//we assign variables to the selected item
		matID=cmbMat.value;
		matName=cmbMat[cmbMat.selectedIndex].text;
	}
	
	//we retrieve the qty desired for the selected materiel
	qty=eval("document.formulaire.qty_"+catID+".value");
	if (catID!='autre') {matQty[matID]=Array(qty,catID);}	//	we store the qty and the catID of the selected materiel
	
	//this condition has been inserted to avoid adding empty lines and negative numbers in the quote list
	if (matName!=" (autre)" && qty>=0)
	{
		//we check if the item is already present in the list
		cmbQuote=document.formulaire.quote_list;
		if (cmbQuote.length>0)
		{//there is at least on item in the quote list
			for (x=0;x<=cmbQuote.length-1;x++)
			{
				if (cmbQuote.options[x].value==matID) 
				{//item is present
					itemPresent=true;
					itemIndex=x;
					break;
				}
				else
				{//item is not present
					itemPresent=false;
				}
			}
		}
		else
		{//the quote list is empty
			itemPresent=false;
		}
		
		if (itemPresent)
		{//item is present : so we have to update the line corresponding to that item
			if (qty==0)
			{
				cmbQuote.options[itemIndex]=null;
			}
			else
			{
				cmbQuote.options[itemIndex]=new Option(qty+" "+matName,matID);
			}
		}
		else
		{//item is not present : we need to add it
			if (qty!=0) { cmbQuote.options[cmbQuote.length]= new Option(qty+" "+matName,matID); }
		}
	}
}
function remMat()
{
	cmbQuote=document.formulaire.quote_list;
	for (x=cmbQuote.length-1;x>=0;x--)
	{
		if (cmbQuote.options[x].selected==true) 
		{
			matID=cmbQuote.options[x].value;
			if (matID>=0) // we make sur we're not dealing with "autre" as the matID of "autre" is non numeric
			{
				catID=matQty[matID][1];
				qty=matQty[matID][0];
				matQty[matID][0]=0;
				
				eval("curCmb=document.formulaire.cmb_"+catID);
				if (curCmb.value==matID)	//	if the visible element of the current combo box is the on we're removing, we change the value of the qty box
				{
					eval("document.formulaire.qty_"+catID+".value=0");	// set the qty associated with the selected hardware element to 0
				}
			}
			else // we are here dealing with a "autre" type
			{
				document.formulaire.qty_autre.value=0;
			}
			cmbQuote.options[x]=null;	//	remove it from the list
		}
	}
}
function resetForm()
{
	//we reinitialise all the form fields
	myForm=document.formulaire;
	myForm.reset();
	
	//we remove all the added items in the quote_list
	cmbQuote=myForm.quote_list;
	for (x=cmbQuote.length-1;x>=0;x--)
	{
		cmbQuote.options[x]=null;
	}
}

function checkSubmit()
{
	myForm=document.formulaire;

	myForm.mat_list.value='';
	for (x=0;x<myForm.quote_list.length;x++)
	{
		myForm.mat_list.value=myForm.mat_list.value+myForm.quote_list.options[x].text+'&';
	}

	myForm.human_user.value=humanUser;
	clearTimeout(TimeSpentOnPage);
	myForm.time_spent.value=timeSpent;
	myForm.submit();
}

function checkSubmit_back()
{
	myForm=document.formulaire;
	
	//EITHER the nom and prenom OR the raison sociale must be filled
	nameInfo=false;
	if ((Trim(myForm.nom.value)!="" && Trim(myForm.prenom.value)!="") || Trim(myForm.raisoc.value)!="")
	{
		nameInfo=true;
	}
	
	//EITHER the tel OR the email must be filled
	contactInfo=false;
	wrongTelNumber=false;
	if (Trim(myForm.email.value)!="" || Trim(myForm.tel.value)!="")
	{
		contactInfo=true;
		//now, we check if the tel number is valid
		if (Trim(myForm.tel.value)!="")
		{
			//if tel starts with 1234, it's host certainly fake
			if ((Trim(myForm.tel.value)).substr(0,4)=='1234')
			{
				wrongTelNumber=true;
			}
		}
	}
	
	//EITHER the quote list has to contain at least on item OR the comments field has to be filled
	needsInfo=false;
	if (myForm.quote_list.length>0 || Trim(myForm.comments.value)!="")
	{
		needsInfo=true;
	}
	
	//EITHER the date fields OR the comments field has to be filled
	dateInfo=false;
	if ( ( (Trim(myForm.start.value)!="") && (Trim(myForm.start.value)!="jj/mm/aaaa") && (Trim(myForm.end.value)!="") && (Trim(myForm.end.value)!="jj/mm/aaaa")  ) || Trim(myForm.comments.value)!="" )
	{
		dateInfo=true;
	}
	
	//we check validity of data and return error message or proceed to the next page
	errorMsg="";
	if (!nameInfo){errorMsg+="\n # Veuillez renseigner les champs 'nom' et 'prenom' ou bien le champs 'raison sociale'";}
	if (!contactInfo){errorMsg+="\n # Veuillez renseigner le champs 'email' ou le champs 'Tel'";}  //commented because we want to force the email
	if (wrongTelNumber){errorMsg+="\n # Veuillez entrer un numero de telephone valide";}
	if (!needsInfo){errorMsg+="\n # Veuillez ajouter au moins un materiel a votre demande de devis ou laissez nous un commentaire";}
	if (!dateInfo){errorMsg+="\n # Si vous ne disposez pas des dates de location, entrez la duree dans la zone 'commentaires'";}
	//if livraison is checked, the lieu de prestation has to be filled
	if (myForm.livraison.checked && (Trim(myForm.lieu.value)=="") ){errorMsg+="\n # Pour le service 'livraison et installation', veuillez indiquer le lieu de la prestation";}
	//we check if either news or destockage is checked : if this is the case, we need an email address
	if (myForm.news.checked && (Trim(myForm.email.value)=="")){errorMsg+="\n # Vous avez choisi de recevoir notre newsletter : entrez un email";}
	if (myForm.destockage.checked && (Trim(myForm.email.value)=="")){errorMsg+="\n # Vous avez choisi de recevoir nos offres de destockage : entrez un email";}
	//we check if the email has a valid format
	if (Trim(myForm.email.value)!="" && !isMailValid(Trim(myForm.email.value))){errorMsg+="\n # L'email que vous avez entre ne semble pas etre correcte";}
	
	if (errorMsg!="")
	{//au moins une erreur
		alert(errorMsg);
	}
	else
	{//envoi des informations au php qui genere et envoie les mails
		//creation d'un champs texte pour contenir le materiel choisi
		myForm.matList.value='';
		for (x=0;x<myForm.quote_list.length;x++)
		{
			myForm.matList.value=myForm.matList.value+myForm.quote_list.options[x].text+'&';
		}

		myForm.humanUserValue.value=humanUser;
		clearTimeout(TimeSpentOnPage);
		myForm.timeSpentValue.value=timeSpent;
		myForm.submit();
		//alert (myForm.matList.value);
	}
}
//check the validity of an email address
function isMailValid(e) 
{
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
	
	for(i=0; i < e.length ;i++)
	{
		if(ok.indexOf(e.charAt(i))<0)
		{ 
			return (false);
		}	
	} 
	
	if (document.images)
	{
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!e.match(re) && e.match(re_two)) 
		{
			return (-1);
		}
	} 
}

function visi(nr,status)
{
	if (document.layers)
	{
		vista = (status == 'show') ? 'show' : 'hide'
		document.layers[nr].visibility = vista;
	}
	else if (document.all)
	{
		vista = (status == 'show') ? 'visible'	: 'hidden';
		document.all[nr].style.visibility = vista;
	}
	else if (document.getElementById)
	{
		vista = (status == 'show') ? 'visible' : 'hidden';
		document.getElementById(nr).style.visibility = vista;

	}
}

function updateProDetails(selValue)
{//function that will hide or show the pro details section according to the "represente" values chosen
	if (selValue != 7)  //7=Particulier => we display the pro details
	{
		visi('pro_details','show');
	}
	else
	{
		visi('pro_details','hide');
	}
}

humanUser=0;

function validateMouseAction()
{
	humanUser=1;
	//alert (humanUser + " - " + timeSpent);
}

timeSpent=0;
function countTimeSpentOnPage()
{
	timeSpent+=500;
	TimeSpentOnPage=setTimeout("countTimeSpentOnPage()",500);
}
TimeSpentOnPage=setTimeout("countTimeSpentOnPage()",500);

explication=false;	//	variable used to determine if a message has already been sent to the user to tell them to click on "add to quote" to add a hardware to the quote

function updateQty(obj)
{
	//	This function will display a message in a messagebox telling the user to click on " ajouter au devis " when they've selected a hardware
	if (!explication)
	{// if the warning has already been displayed, we don't display if again.
		alert ("Veuillez cliquer sur '+' pour ajouter du materiel a votre demande de devis ou '-' pour en enlever");
		explication=true;
	}
	//we're gonna use this function to update the matQty array too ! :o) Lazy bastard !!!
	parts=obj.id.split("_");
	eval("document.formulaire.qty_"+parts[1]+".value="+(matQty[(obj.value)]==null?1:matQty[(obj.value)][0]));
	checkQty(parts[1]);
}

