//--------------------------------------------------------------------------
//  JavaScript Document
//--------------------------------------------------------------------------




//--------------------------------------------------------------------------
// 
// Fonction de roll-over
function Roll(id, chemin, arg)
{
	var adresse = "img/" + chemin + "" + id;
	if(arg == 1)
	{
		document.images[id].src = adresse + "_over.gif";
	}
	else
	{
		document.images[id].src = adresse + "_out.gif";
	}
}
//--------------------------------------------------------------------------




//--------------------------------------------------------------------------
// Fonction de changement de style
function chBgTR(tr, style)
{
	tr.className = style;
}
//--------------------------------------------------------------------------




//--------------------------------------------------------------------------
// Fonction de changement de style
function chStyle(id, style)
{
	//document.getElementById(id).className = style;
	id.className = style;
}
//--------------------------------------------------------------------------




//--------------------------------------------------------------------------
// 
function Switch(arg1, arg2)
{
	var elements1 = arg1.split(",");
	for(i=0; i<elements1.length; i++)
	{
		document.getElementById(elements1[i]).style.display = '' ;
	}
	
	var elements2 = arg2.split(",");
	for(i=0; i<elements2.length; i++)
	{
		document.getElementById(elements2[i]).style.display = 'none' ;
	}
}
//--------------------------------------------------------------------------




//--------------------------------------------------------------------------
// 
function openPopUp( url, titre, largeur, hauteur) 
{ 
   window.open(url, titre, 'width=' + largeur + ', height=' + hauteur + ', toolbar=0, location=0, directories=0, menuBar=0, scrollbars=1');
} 
//--------------------------------------------------------------------------




//--------------------------------------------------------------------------
// 
function redirect(arg)
{
	location.href = arg;
}
//--------------------------------------------------------------------------




//--------------------------------------------------------------------------
// 
var fieldAll = false;

function checkflag(field)
{
	if(fieldAll == false)
	{
		for (i = 0; i < field.length; i++)
		{
			field[i].checked = true;
		}
		fieldAll = true;
	}
	else
	{
		for (i = 0; i < field.length; i++)
		{
			field[i].checked = false;
		}
		fieldAll = false;
	}
}
//--------------------------------------------------------------------------




//--------------------------------------------------------------------------
// 
var f = false;

function checkAll()
{
	if(f == false)
	{
		for (var j = 1; j <= 3; j++)
		{
			box = eval("document.checkboxform.c" + j);  
			if (box.checked == false) box.checked = true;
		}

		f = true;
	}
	else
	{
		for (var j = 1; j <= 5; j++)
		{
			box = eval("document.checkboxform.c" + j);  
			if (box.checked == true) box.checked = false;
		}
		f = false;
	}
}
//--------------------------------------------------------------------------




_arrMsgError = new Array();
_arrMsgError['champVide'] = "Vous devez remplir tous les champs.";
_arrMsgError['invalideNumeric'] = "le format est incorrect.\nVous devez rentrer obligatoirement que des chiffres.";
_arrMsgError['invalideEmail'] = "L'e-mail n'est pas un format strandard d'un e-mail.";



//--------------------------------------------------------------------------
// 
function fct_verifForm(argFormName, argActionType, argWidth, argHeight)
{
	var objForm = document.forms[argFormName];
	var blnErreur = false;
	var strMsg = _arrMsgError['champVide'];
	var strName = '';
	var strValue = '';

	for(i = 0; i < objForm.length; i++)
	{
		strName = strName + objForm.elements[i].name + ' - ' + objForm.elements[i].type + '\n';
	}

	//alert(strName);

	for(i = 0; i < objForm.length; i++)
	{
		strName = objForm.elements[i].name;
		_elementType = '';

		if(strName != null)
		{
			//alert(strName);
			_arrSplit = strName.split("_");

			if(_arrSplit[0] == 'chk')
			{
				strTest = objForm.elements[i].value;
				_arrSplit.shift();
				strName = _arrSplit.join("_");
				//alert(strName);

				objInput = objForm.elements[strName];
				
				if(objInput != undefined && objForm.elements[i].disabled == false)
				{
					strValue = objInput.value;
					//alert('strValue : ' + strValue);

					_elementType = objInput.type;
					//alert('_elementType : ' + _elementType);

					if(_elementType == 'radio')
					{
						strValue = '';
						for(j = 0; j < objInput.length; j++)
						{
							if(objInput[j].checked == true)
							{
								//alert(strName + ' - ' + _elementType);
								strValue = objInput[j].value;
							}
						}
					}

					if(_elementType == 'checkbox')
					{
						strValue = '';
						if(objInput.checked == true)
						{
							strValue = objInput.value;
						}
					}

					strMsgTest = isTest(strValue, strTest, argFormName);

					//alert(strMsgTest);

					if(strMsgTest != '')
					{
						strMsg = strMsgTest;

						if(_elementType == 'hidden' || _elementType == undefined || _elementType == 'radio' || _elementType == 'checkbox')
						{
							//objForm.elements[strName][0].focus();
						}
						else
						{
							objInput.focus();
						}

						blnErreur = true;
						break;
					}
				}
			}
		}
	}

	if(blnErreur)
	{
		if(strMsg != '')
		{
			alert(strMsg);
		}

		switch(argActionType)
		{
			case 'post' :

				return false;

				break;

			case 'popupclick' :

				return false;

				break;

			default :

				break;
		}
	}
	else
	{
		for(i = 0; i < objForm.length; i++)
		{
			strName = objForm.elements[i].name;
			arrSplit = strName.split("_");

			//objForm.elements[i].disabled = false;
			if(strName.substr(0, 4) == 'chk_')
			{
				objForm.elements[i].disabled = true;
				//document.getElementByName(strName).style.display = 'none';
			}
		}


		switch(argActionType)
		{
			case 'post' :

				return true;

				break;

			case 'click' :

				objForm.submit();

				break;

			case 'popupclick' :

				fctOpenWindow('../_templates/blanc.html', argWidth, argHeight, false, 'win');

				return true;

				break;

			default :

				//return true;

				break;
		}
	}
}
//--------------------------------------------------------------------------




//--------------------------------------------------------------------------
// 
function isTest(argValue, argTest, argFormName)
{
	var i;
	var _objForm = document.forms[argFormName];
	var _blnTest = false;
	var _msg = '';
	var _nonObligatoire = false;
	var _type = null;
	var _arrSplit1 = null;
	var _arrSplit2 = null;
	var _arrData = new Array();

	if(argTest != '')
	{
		_arrSplit1 = argTest.split("&");
		//alert(_arrSplit1.length);

		for(i = 0; i < _arrSplit1.length; i++)
		{
			_arrSplit2 = _arrSplit1[i].split("=");

			if(_arrSplit2.length == 2)
			{
				_arrData[_arrSplit2[0]] = _arrSplit2[1];
			}
		}


		if(argValue != '')
		{
			//alert(_arrData['type'] + ' ' + _arrData['message'] + ' ' + _arrData['messagetest']);

			switch(_arrData['type'])
			{
				case 'formatage' :
				
					if(_arrData['expression'] != undefined)
					{
						var reg = new RegExp('^(' + _arrData['expression'] + ')$', 'gi');
						if((reg.exec(argValue) != null) == false)
						{
							if(_arrData['messageCheck'] != undefined)
							{
								_msg = _arrData['messageCheck'];
							}
						}
					}

					break;

				case 'email' :

					if(!isEmail(argValue))
					{
						_msg = _arrMsgError['invalideEmail'];

						if(_arrData['messageCheck'] != undefined)
						{
							_msg = _arrData['messageCheck'];
						}
					}

					break;

				case 'numeric' :

					if(!isNumeric(argValue))
					{
						_msg = _arrMsgError['invalideNumeric'];

						if(_arrData['messageCheck'] != undefined)
						{
							_msg = _arrData['messageCheck'];
						}
					}

					break;

				case 'chiffresAndLettres' :

					var reg = new RegExp('^([a-z0-9]+)$', 'gi');
					if((reg.exec(argValue) != null) == false)
					{
						strMsg = "le format est incorrect.\nVous devez rentrer obligatoirement que des chiffres ou des lettres.";	
					}
					break;

				case 'limitechiffre' :

					intLimite = 1;
					var resultat = _test.match(/(limite#)([^§]*)(\s)/);
					intLimite = resultat[2];

					var reg = new RegExp('^[0-9]{' + intLimite + '}$', 'gi');
					if((reg.exec(argValue) != null) == false)
					{
						strMsg = "le format de ce numéro est incorrect.\nVous devez rentrer obligatoirement " + intLimite + " chiffres.";
					}
					break;

				case 'password' :

					var resultat = _test.match(/(comparaison#)([^§]*)(\s)/);
					_input = resultat[2];

					if(document.forms[argFormName].elements[_input].value != argValue)
					{
						strMsg = "la confirmation de mot de passe est incorrect.";
					}

					break;

				case 'chaineVideInterdite' :

					break;

				default :

					break;

			}
		}
		else
		{
			_msg = _arrMsgError['invalideNumeric'];

			if(_arrData['message'] != undefined)
			{
				_msg = _arrData['message'];
			}
		}
	}

	return _msg;
}
//--------------------------------------------------------------------------




//--------------------------------------------------------------------------
// 
 function isNull(argValue) 
 {
      return typeof argValue == 'object' && !argValue;
 }
//--------------------------------------------------------------------------




//--------------------------------------------------------------------------
// 
function isEmail(argValue)
{
	var _reg = new RegExp('^[a-z0-9._-]+@[a-z0-9\-\.]+[.][a-z]{2,4}$', 'gi');
	var _blnCheck = true;

	if((_reg.exec(argValue) != null) == false)
	{
		_blnCheck = false;
	}

	return _blnCheck;
}
//--------------------------------------------------------------------------




//--------------------------------------------------------------------------
// 
function isNumeric(argValue)
{
	var i;
	var _validChars = "0123456789.-";
	var _char;
	var _blnCheck = true;

	if(argValue.length == 0) return false;

	//  test strString consists of valid characters listed above
	for (i = 0; i < argValue.length && _blnCheck == true; i++)
	{
		_char = argValue.charAt(i);

		if (_validChars.indexOf(_char) == -1)
		{
			_blnCheck = false;
		}
	}

	return _blnCheck;
}
//--------------------------------------------------------------------------




//--------------------------------------------------------------------------
// 
function clearDisabled(argFormName)
{
	var i;
	var _objForm = document.forms[argFormName];

	for(i = 0; i < _objForm.elements.length; i++)
	{
		if(_objForm.elements[i].disabled) _objForm.elements[i].disabled = false;
	}
}
//--------------------------------------------------------------------------




// ========================================================================== //
// ================================== AJAX ================================== //
// ========================================================================== //


var _objHttp;
var _objHttpUrl ='/_asp/xmlProxy.php';
var _objHttpAjaxDefault = '/_asp/ajax.asp';
var _objHttpAjax = _objHttpAjaxDefault;
var _objHttpParams = Array();

var localData = new Object();
var localRoot = new Object();


//--------------------------------------------------------------------------
// REQUEST
var Request = new Object();

Request.send = function(url, method, callback, data, urlencoded, name) {
	var req;

	if(window.XMLHttpRequest)
	{
		req = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}

	if(typeof name == "undefined")
	{
		name = "";
	}
	else
	{
		name += "\n";
	}

	var readychange = function() {
		if (req.readyState == 4) {// only if req shows "loaded"

			// only if "OK"
			if (req.status < 400)
			{
				if (method == "POST")
				{
					callback(req);
					delete callback;
				}
				else
				{
					callback(req,data);
					delete callback;
				}

			}
			// don't do anything. user has navigated away
			else if (typeof req == "undefined" || typeof req.status == "undefined")
			{
				delete callback;
			} 
			// unauthorized
			else if (req.status == 401)
			{
				callback(req);
				delete callback;
			}
			// should not happen, but ignore it for now
			else if (req.status == 404)
			{

			}
			else
			{
				//if (App.errorShowing) return false;
				//App.errorShowing = true;
				switch(req.status)
				{
					// windows error codes
					case 12002: // server timeout
					case 12029: case 12030: case 12031: // dropped connection
					case 12152: // connection closed by server
					case 13030:
						alert(name + _("There was a network problem. Please reload the page."));
						break;
					case 500: case 503:
						alert(name + _("There was an internal server error. Please try later."));
						break;
					default:
						alert(_("There was a problem loading data:") + "\nstatus: " + req.status+ "/" + req.statusText + "\n" + url);
					//App.errorShowing = false;
				}
				delete callback;
			}
		}
	};

	function do_request(){

		if(method == "POST")
		{
			req.open("POST", url, true);
			if (urlencoded) req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			req.onreadystatechange = readychange;
			req.send(data);
		}
		else
		{
			req.open("GET", url, true);
			req.onreadystatechange = readychange;
			req.send(null);
		}
	};

	do_request();

	return req;
}

Request.sendRawPOST = function(url, data, callback, name) {
	Request.send(url, "POST", callback, data, false, name);
}
Request.sendPOST = function(url, data, callback, name) {
	Request.send(url, "POST", callback, data, true, name);
}
Request.sendGET = function(url, callback, args, name) {
	return Request.send(url, "GET", callback, args, name);
}
//--------------------------------------------------------------------------




//--------------------------------------------------------------------------
// 
function ajaxSave(argAction, argParams, argForm, argMethode, argFunction, argHttpAjax)
{
	_url = '';

	_cache = Math.round(Math.random()*1000000000);
	_function = argFunction;
	_httpAjax = _objHttpAjax;

	if(argMethode == 'post')
	{
		_datas = '_action=' + argAction;

		if(argForm != '')
		{
			for(i = 0; i < argForm.length; i++)
			{
				_name = argForm.elements[i].name;

				if(_name != '' && _name.substr(0, 4) != 'chk_')
				{
					 _datas += '&' + argForm.elements[i].name + '=' + escape(argForm.elements[i].value);
				}
			}
		}

		if(argParams != '') _datas += '&' + argParams;
		//alert(_datas);

		if(argFunction == 'undefined' || argFunction == null) _function = 'displaySave';

		if(argHttpAjax != 'undefined' && argHttpAjax != null) _httpAjax = argHttpAjax;

		Request.sendPOST(_httpAjax, _datas, eval(_function));
	}
	else
	{
		_url = _objHttpAjax + '?rq_act=' + argAction;
		if(argParams != '') _url += '&' + argParams;
		_url += '&cache=' + _cache;

		Request.sendGET(_url, displaySave);
	}



	return false;
}
//--------------------------------------------------------------------------




//--------------------------------------------------------------------------
// 
function displaySave(response)
{
	localData.rootNode = response.responseXML.documentElement;
	
	var _flag = true;
	var _root = localData.rootNode;

	_sql = _root.getElementsByTagName("sql");
	if(_sql.length > 0)
	{
		_sql = _sql[0].firstChild.nodeValue;
		alert(_sql);
	}


	var _msg = _root.getElementsByTagName("message");
	if(_msg.length > 0)
	{
		_msg = _msg[0].firstChild.nodeValue;
		_msg = _msg.replace(/<br \/>/gi,"\n");
		alert(_msg);
	}


	var _path = _root.getElementsByTagName("path");
	if(_path.length > 0)
	{
		_path = _path[0].firstChild.nodeValue;
		//alert(_path);
	}

	if(_flag)
	{
		var _reload = _root.getElementsByTagName("reload");
		if(_reload.length > 0)
		{
			_reload = _reload[0].firstChild.nodeValue;
			//alert(_path);

			window.location = _reload;
		}

		var _div = _root.getElementsByTagName("div");
		if(_div.length > 0)
		{
			_name = _div[0].getElementsByTagName("name")[0].firstChild.nodeValue;
			_text = _div[0].getElementsByTagName("text")[0].firstChild.nodeValue;

			document.getElementById(_name).innerHTML = _text;
		}
	}
}
//--------------------------------------------------------------------------




//--------------------------------------------------------------------------
// 
function checkNewsletter(argFormName)
{
	if(fct_verifForm(argFormName, 'post'))
	{
		ajaxSave('inscriptionNewsletter', '', document.forms[argFormName], 'post', 'displaySave');
	}

	return false;
}
//--------------------------------------------------------------------------




//--------------------------------------------------------------------------
// 
function checkNousContacter(argFormName)
{
	if(fct_verifForm(argFormName, 'post'))
	{
		ajaxSave('nousContacter', '', document.forms[argFormName], 'post', 'displaySave');
	}

	return false;
}
//--------------------------------------------------------------------------

