// JavaScript Document

function makeRequest(url,IDBalise)
{
	/*
	Note : if you are sending a request to a piece of code that will return XML, rather than to a static XML file, you must set some response 
	headers if your page is to work in Internet Explorer in addition to Mozilla. If you do not set header Content-Type: application/xml, IE 
	will throw a JavaScript error, 'Object Expected', after the line where you try to access an XML element. If you do not set header 
	Cache-Control: no-cache the browser will cache the response and never re-submit the request, making debugging "challenging."
	*/
	/*
	Response.ContentType = "application/xml"
	Response.CacheControl = "no-cache"
	*/

	var httpRequest ;

	
	if (window.XMLHttpRequest) // Mozilla, Safari, ...
	{
		httpRequest = new XMLHttpRequest() ;
		if (httpRequest.overrideMimeType)
		{
			//alert(httpRequest.overrideMimeType('text/xml'));
			httpRequest.overrideMimeType('text/xml') ;
			// Note: The line httpRequest.overrideMimeType('text/xml'); above will cause JavaScript Console errors in Firefox 1.5 or later, as documented in bug 311724 (https://bugzilla.mozilla.org/show_bug.cgi?id=311724) if the page retrieve by XMLHttpRequest is not valid XML (e.g., if it is plain text). This is actually correct behavior; this article will be revised soon to address this change.

		}
	} 
	else if (window.ActiveXObject) // IE
	{
		try
		{
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP") ;
		} 
		catch (e)
		{
			try
			{
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP") ;
			} 
			catch (e) {}
		}
	}

	if (!httpRequest)
	{
		alert('Giving up :( Cannot create an XMLHTTP instance') ;
		return false ;
	}
	httpRequest.onreadystatechange = function()
		{
			httpRequest_callback(httpRequest) ;
			DivID = GetVariablesURL(url,IDBalise);
		} ;
	httpRequest.open('GET', url, true) ;
	httpRequest.send('') ;
}

function httpRequest_callback(httpRequest)
{
        try
	{
		if (httpRequest.readyState == 4)
		{
			if (httpRequest.status == 200)
			{

				var el4 = document.getElementById(DivID);//alert(DivID);
				el4.innerHTML = httpRequest.responseText;
			
			
			}
			else
			{
				//alert('There was a problem with the request.\nhttpRequest.status: ' + httpRequest.status) ;
			}
		}
        }
        catch( e )
	{
		//alert('Caught Exception: ' + e.description) ;
        }
}

function GetVariablesURL(URL,NameVar)
{
var Req = "";
var nTemp = "";
//alert(location.search.substring(1,location.search.length));
if(URL == "")
{
Req = location.search.substring(1,location.search.length);	//suppression du ? en début de chaine
}
else
{
Req = URL.split("?");
//alert(Req[1]);
Req = Req[1];
//alert(Req[1]);
//Req = URL[1].substring(1,URL.length);	//suppression du ? en début de chaine
}

Req = Req.split("&"); //transformation en tableau

	for (var i=0;i<Req.length;i++)
	{
		if (Req[i].substring(0,Req[i].indexOf("=")) == NameVar)
		{
		nTemp = Req[i].substring(Req[i].indexOf("=")+1,Req[i].length);
		}
	}

//alert(nTemp);

	while (true)
	{
		var i = nTemp.indexOf('+');
		if (i < 0) break;
		nTemp = nTemp.substring(0,i) + '%20' + nTemp.substring(i + 1, nTemp.length);
	}

return (nTemp);
}
/*
function SetCheckBox(IdCheckBox)
{
	for(i=0;i<box_ids.length;i+=2) //on va de 2 en 2 car il ya les id de contenus aussi
	{	
		alert(IdCheckBox+box_ids[i]);
		formAdd.elements[IdCheckBox+box_ids[i]].checked = 'true';
	}
}
*/

function afficher_menu(menu, idarticle)
{
	document.getElementById(menu).className = menu+'_on';
	
	switch(menu)
	{
		case 'rouge' :
			document.getElementById('orange').className = 'orange_off';
			document.getElementById('bleu').className 	= 'bleu_off';
			document.getElementById('vert').className 	= 'vert_off';
			break;
		case 'orange' :
			document.getElementById('rouge').className 	= 'rouge_off';
			document.getElementById('bleu').className 	= 'bleu_off';
			document.getElementById('vert').className 	= 'vert_off';
			break;
		
		case 'bleu' :
			document.getElementById('rouge').className 	= 'rouge_off';
			document.getElementById('orange').className = 'orange_off';
			document.getElementById('vert').className 	= 'vert_off';
			break;
		
		case 'vert' :
			document.getElementById('rouge').className 	= 'rouge_off';
			document.getElementById('bleu').className 	= 'bleu_off';
			document.getElementById('orange').className	= 'orange_off';
			break;
		
		default :
			document.getElementById('rouge').className	= 'rouge_on';
			document.getElementById('orange').className = 'orange_off';
			document.getElementById('bleu').className 	= 'bleu_off';
			document.getElementById('vert').className 	= 'vert_off';
			break;
	}
	makeRequest('?page=contenu_article_home&DivID=texte_article_home&id_article='+idarticle+'&couleur='+menu,'DivID');
	makeRequest('?page=contenu_visuel_home&DivID=visuel_article_home&id_article='+idarticle,'DivID');
}
function SetCookie (name, value)
{
	//nouvel objet date
	var aujourdhui = new Date() ;
	//nouvel objet date
	var expdate = new Date() ;
	//plus an an à partir d'aujourdh'ui le cookie ne s'efface jamais
	expdate.setTime( aujourdhui.getTime() + ( 365*24*60*60*1000 ) )
	//creation du cookie
	document.cookie = name + "=" + value + ";expires=" + expdate.toGMTString() ;
	return true;
}

function getCookieVal(offset)
{
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr)); 
}

function LireCookie(nom)
{
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen)
	{
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	}
	return false; 
}

function affichePopup()
{
	if(LireCookie("popuphome") == false)
	{
		document.getElementById("popuphome").style.display = "block";
		SetCookie("popuphome", "1");
	}
}

function check_mail(mail){

    var re    = new RegExp("^([a-zA-Z0-9_-])+([.]?[a-zA-Z0-9_-]{1,})*@([a-zA-Z0-9-_]{2,}[.])+[a-zA-Z]{2,3}$", "gi");

    if(!re.test(mail)){
	   return false;
	}else{
	   return true;
	}

}

function validForm(){
	var company		= document.getElementById('name').value;
	var address		= document.getElementById('address').value;
	var city		= document.getElementById('city').value;
	var zip			= document.getElementById('zip').value;
	var continent	= document.getElementById('continent').value;
	var country		= document.getElementById('country').value;
	var firstname	= document.getElementById('firstname').value;
	var lastname	= document.getElementById('lastname').value;
	var phone		= document.getElementById('phone').value;
	var mail		= document.getElementById('mail').value;
	var mail2		= document.getElementById('mail2').value;
	
	if(company == ""){
		alert("Please enter your company");
		return false;
	}

	if(address == ""){
		alert("Please enter your address");
		return false;
	}

	if(city == ""){
		alert("Please enter your city");
		return false;
	}	
	
	if(zip == ""){
		alert("Please enter your zip code");
		return false;
	}	
	
	if(continent == ""){
		alert("Please select your continent");
		return false;
	}
	
	if(country == ""){
		alert("Please select your country");
		return false;
	}
	
	if(firstname == ""){
		alert("Please enter your firstname");
		return false;
	}
	
	if(lastname == ""){
		alert("Please enter your firstname");
		return false;
	}

	if(phone == ""){
		alert("Please enter your phone number");
		return false;
	}
	
	if(!check_mail(mail)){
		alert("Wrong mail adress");
		return false;
	}
	
	if(!check_mail(mail2)){
		alert("Wrong mail adress");
		return false;
	}
	
	if(mail != mail2){
		alert("Mail adresses doesn't match");
		return false;
	}
	
	return true;	
}

function select_continent(select){
	
	// initialisation des variables
	var continent	= select.options[select.selectedIndex].value;
	var country		= document.getElementById('country');
	var i 			= 0;
	var taille_select = country.options.length;
	
	// je reduis la taille de 1 pour être conforme à la position des options
	taille_select--;
	
	// je vide la liste des pays (a vider à l'envers sinon les indexs des options changent) 
	for(i=taille_select; i > 0; i--){
		country.options[i] = null;
	}	
	
	// je rempli en fonction du continent choisi
	switch(select.value){
		case "North America" :
		country.options[1] = new Option("Canada","Canada");	
		country.options[2] = new Option("Mexico","Mexico");	
		country.options[3] = new Option("USA","USA");
		country.options[0].selected = true;
		break;
		
		case "Central, South America" :
		country.options[1] = new Option("Argentina","Argentina");	
		country.options[2] = new Option("Bolivia","Bolivia");	
		country.options[3] = new Option("Brazil","Brazil");
		country.options[4] = new Option("Chile","Chile");
		country.options[5] = new Option("Ecuador","Ecuador");
		country.options[6] = new Option("Paraguay","Paraguay");
		country.options[7] = new Option("Peru","Peru");
		country.options[8] = new Option("Uruguay","Uruguay");
		country.options[9] = new Option("other countries","other countries");
		country.options[0].selected = true;
		break;
		
		case "Europe, Middle East, Africa" :
		country.options[1] = new Option("Austria","Austria");	
		country.options[2] = new Option("Belgium","Belgium");	
		country.options[3] = new Option("Bulgaria","Bulgaria");
		country.options[4] = new Option("Croatia","Croatia");
		country.options[5] = new Option("Czech Republic","Czech Republic");
		country.options[6] = new Option("Denmark","Denmark");
		country.options[7] = new Option("Estonia","Estonia");
		country.options[8] = new Option("Finland","Finland");
		country.options[9] = new Option("France","France");
		country.options[10] = new Option("Germany","Germany");
		country.options[11] = new Option("Greece","Greece");
		country.options[12] = new Option("Hungary","Hungary");
		country.options[13] = new Option("Ireland","Ireland");
		country.options[14] = new Option("Israel","Israel");
		country.options[15] = new Option("Italy","Italy");
		country.options[16] = new Option("Latvia","Latvia");
		country.options[17] = new Option("Lithuania","Lithuania");
		country.options[18] = new Option("Middle East","Middle East");
		country.options[19] = new Option("Netherlands","Netherlands");
		country.options[20] = new Option("Norway","Norway");
		country.options[21] = new Option("Other countries","Other countries");
		country.options[22] = new Option("Poland","Poland");
		country.options[23] = new Option("Portugal","Portugal");
		country.options[24] = new Option("Romania","Romania");
		country.options[25] = new Option("Russia","Russia");
		country.options[26] = new Option("Serbia and Montenegro","Serbia and Montenegro");
		country.options[27] = new Option("Slovakia","Slovakia");
		country.options[28] = new Option("Slovenia","Slovenia");
		country.options[29] = new Option("South Africa","South Africa");
		country.options[30] = new Option("Spain","Spain");
		country.options[31] = new Option("Sweden","Sweden");
		country.options[32] = new Option("Switzerland","Switzerland");
		country.options[33] = new Option("Turkey","Turkey");
		country.options[34] = new Option("Ukraine","Ukraine");
		country.options[35] = new Option("United Kingdom","United Kingdom");
		country.options[0].selected = true;
		break;
		
		case "Asia Pacific" :
		country.options[1] = new Option("Australia","Australia");	
		country.options[2] = new Option("India","India");	
		country.options[3] = new Option("Indonesia","Indonesia");	
		country.options[4] = new Option("Japan","Japan");	
		country.options[5] = new Option("Korea","Korea");	
		country.options[6] = new Option("Malaysia","Malaysia");	
		country.options[7] = new Option("New Zealand","New Zealand");	
		country.options[8] = new Option("Other countries","Other countries - Asia Pacific");	
		country.options[9] = new Option("Singapore","Singapore");	
		country.options[10] = new Option("Taiwan","Taiwan");	
		country.options[11] = new Option("Thailand","Thailand");	
		country.options[12] = new Option("Vietnam","Vietnam");
		country.options[0].selected = true;		
		break;
		
		default :
		country.options[0].selected = true;
		break;
	}
	
	return false;
}