// JavaScript Document

var nav = navigator.userAgent;

var FF = (nav.match('Firefox') || nav.match('Iceweasel')) ? 1 : 0;
var IE7 = (nav.match('MSIE 7.0')) ? 1 : 0;
var IE6 = (nav.match('MSIE 6.0') && !IE7) ? 1 : 0;

var siteUrl = document.domain;

function create_xhr_object ()
{
	var xhr;
	
	if (window.XMLHttpRequest) 
	{
		xhr = new XMLHttpRequest(); //Firefox ou IE >= 7.0
	}
	else if (window.ActiveXObject) 
	{
		try // essaie de charger l'objet pour IE
		{ 
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) // essaie de charger l'objet pour une autre version IE
		{
			try 
			{
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) 
			{
				xhr = 0;
			}
		} 
	}
	
	return xhr;
}

function position (objectID)
{
	var obj = document.getElementById(objectID);
	
	var curleft = obj.offsetLeft || 0;
	var curtop = obj.offsetTop || 0;
	
	while (obj = obj.offsetParent)
	{
		curleft += obj.offsetLeft
		curtop += obj.offsetTop
	}
	return {x:curleft, y:curtop};
}

/* Affiche la bulle pour les formulaires */
function show_info_bull (bullID, inputID, message)
{
	var bull, pos;
	
	if (!document.getElementById)
		return false;
	
	pos = position(inputID);
	bull = document.getElementById(bullID);
	
	bull.innerHTML = '<p class="bull_text">'+message+'</p>';
	
	bull.style.left = pos['x']+24+'px';
	bull.style.top = pos['y']+16+'px';
	bull.style.display = 'block';
}

/* Cache la bulle pour les formulaires */
function hide_info_bull (bullID)
{
	if (!document.getElementById)
		return false;
	
	document.getElementById(bullID).style.display = 'none';
}

/* Affiche une fenetre d'information */
function bull (objectID, listener, e)
{
	// Déclarations
	var object;
	var mouseX, mouseY;
	var x, y;
	var dx, dy;
	
	object = document.getElementById(objectID);
	if (!object)
		return false;
			
	/* Initial mouse position */
	if (IE6 || IE7)
	{
		object.style.left = (event.clientX + 8)+'px';
		object.style.top = (event.clientY + 8)+'px';
	}
	else
	{
		object.style.left = (e.clientX + 8)+'px';
		object.style.top = (e.clientY + 8)+'px';
	}
	
	/* Display object */
	object.style.display = 'block';
	listener.src = './css/prize_h.png';
	
	/* Dropping */
	listener.onmouseout = function ()
	{
		/* Hide */
		object.style.display = 'none';
		listener.src = './css/prize.png';
		
		/* clear method */
		document.onmouseout = null;
		document.onmousemove = null;
	};
	
	/* Moving */
	if (IE6 || IE7)
	{
		document.onmousemove = function ()
		{
			object.style.left = (event.clientX + 8)+'px';
			object.style.top = (event.clientY + 8)+'px';
		};
	}
	else
	{
		document.onmousemove = function (e)
		{
			object.style.left = (e.clientX + 8)+'px';
			object.style.top = (e.clientY + 8)+'px';
		};
	}
}

function drag (objectID, e)
{
	/* Déclarations */
	var object;
	var mouseX, mouseY;
	var x, y;
	var dx, dy;
	
	object = document.getElementById(objectID);
	if (!object)
		return false;
	
	/* Initial mouse position */
	if (IE6 || IE7)
	{
		mouseX = event.clientX;
		mouseY = event.clientY;
	}
	else
	{
		mouseX = e.clientX;
		mouseY = e.clientY;
	}
	
	/* Initial element position */
	if (object.style.left && object.style.top)
	{
		x = parseInt(object.style.left);
		y = parseInt(object.style.top);
	}
	else
	{
		x = 0;
		y = 0;
	}
	
	/* Delta element-mouse */
	dx = mouseX - x;
	dy = mouseY - y;
	
	/* Dropping */
	document.onmouseup = function ()
	{
		/* clear method */
		document.onmouseup = null;
		document.onmousemove = null;
	};
		
	/* Dragging */
	if (IE6 || IE7)
	{
		document.onmousemove = function ()
		{
			object.style.left = (event.clientX - dx)+'px';
			object.style.top = (event.clientY - dy)+'px';
		};
	}
	else
	{
		document.onmousemove = function (e)
		{
			object.style.left = (e.clientX - dx)+'px';
			object.style.top = (e.clientY - dy)+'px';
		};
	}
}

/* Convert special str encoding to normal */
function specialstr_to_js (string)
{
	var i;
	var cin = new Array();
	var cout = new Array();
	
	cin[0] = new RegExp('_01', 'g');
	cout[0] = '"';
	cin[1] = new RegExp('_02', 'g');
	cout[1] = '\'';
	cin[2] = new RegExp('_03', 'g');
	cout[2] = '+';
	cin[3] = new RegExp('_04', 'g');
	cout[3] = '-';
	cin[4] = new RegExp('_05', 'g');
	cout[4] = '/';
	cin[5] = new RegExp('_06', 'g');
	cout[5] = '*';
	cin[6] = new RegExp('_07', 'g');
	cout[6] = '=';
	cin[7] = new RegExp('_08', 'g');
	cout[7] = '(';
	cin[8] = new RegExp('_09', 'g');
	cout[8] = ')';
	cin[9] = new RegExp('_10', 'g');
	cout[9] = '<';
	cin[10] = new RegExp('_11', 'g');
	cout[10] = '>';
	cin[11] = new RegExp('_12', 'g');
	cout[11] = '\\\'';
	cin[12] = new RegExp('_13', 'g');
	cout[12] = ';';
	cin[13] = new RegExp('_14', 'g');
	cout[13] = '.';
	cin[14] = new RegExp('_15', 'g');
	cout[14] = ',';
	cin[15] = new RegExp('_16', 'g');
	cout[15] = '!';
	cin[16] = new RegExp('_17', 'g');
	cout[16] = '?';
	cin[16] = new RegExp('_18', 'g');
	cout[16] = ' ';
	cin[16] = new RegExp('_19', 'g');
	cout[16] = ':';
	cin[17] = new RegExp('_20', 'g');
	cout[17] = '&';
	
	for (i = 0; i < cin.length; i++)
	{
		string = string.replace(cin[i], cout[i]);
	}
	
	return string;
}

function fill_box (id, title, content, command)
{
	var object = document.getElementById(id);
	
	if (!object)
		return 0;
	
	/* Force opening */
	if (object.style.display == 'none')
		object.style.display = 'block';
	
	/* title */
	if (title)
		document.getElementById(id+'_title').innerHTML = specialstr_to_js(title);
	
	/* content */
	if (content)
		document.getElementById(id+'_info').innerHTML = specialstr_to_js(content);
	
	/* cmd */
	if (command)
		document.getElementById(id+'_cmd').innerHTML = specialstr_to_js(command);
}

function check_password (pwd, pwdCheck, pwd1stChk, pwd2ndChk)
{
	var password;
	var passwordCheck;
	var expReg = new RegExp("[\w]*");
	
	if (!document.getElementById)
		return false;

	password = document.getElementById(pwd).value;
	passwordCheck = document.getElementById(pwdCheck).value;
	
	/* Resultat */
	if (expReg.test(password) && password.length >= 6 && password.length <= 20)
	{
		document.getElementById(pwd1stChk).innerHTML = '<img src="http://'+siteUrl+'/css/done.png" width="16" height="16" alt="OK" />';
		
		if (expReg.test(passwordCheck) && passwordCheck.length >= 6 && passwordCheck.length <= 20 && password == passwordCheck)
		{
			document.getElementById(pwd2ndChk).innerHTML = '<img src="http://'+siteUrl+'/css/done.png" width="16" height="16" alt="OK" />';
			
			return true;
		}
		else
		{
			document.getElementById(pwd2ndChk).innerHTML = '<img src="http://'+siteUrl+'/css/cancel.png" width="16" height="16" alt="DIFFERENT" />';
			
			return false;
		}
	}
	else
	{
		document.getElementById(pwd1stChk).innerHTML = '<img src="http://'+siteUrl+'/css/cancel.png" width="16" height="16" alt="MAUVAIS" />';
		
		return false;
	}
}

function check_name (nameID, textID)
{
	if (!document.getElementById)
		return false;
	
	var name = document.getElementById(nameID).value; /* Nom a tester */
	var done = true; /* Bool de résultat */
	var text; /* retour spécial */
	
	if (name.length == 0)
		return done;
	
	/* test la longueur */
	if (done)
	{
		if (name.length < 4 || name.length > 16)
		{
			done = false;
			text = '<span style="color:#900"><img src="http://'+siteUrl+'/css/cancel.png" width="16" height="16" alt="/!\\" /> Entre 4 et 16 caractères!</span>';
		}
	}
	
	/* test les caractères */
	if (done)
	{
		var expReg = new RegExp("^[ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿa-zA-Z0-9'_]{1,}$");
		
		if (!name.match(expReg))
		{
			done = false;
			text = '<span style="color:#900"><img src="http://'+siteUrl+'/css/cancel.png" width="16" height="16" alt="/!\\" /> Caractères interdits!</span>';
		}
	}
	
	/* show result */
	document.getElementById(textID).innerHTML = (done == true) ? '<img src="http://'+siteUrl+'/css/done.png" width="16" height="16" alt="OK" />' : text;
	
	return done;
}

function check_name_uniqueness (nameID, textID)
{
	if (!document.getElementById)
		return false;
	
	if (!check_name(nameID, textID, true))
		return false;
	
	/* nom a tester */
	var name = document.getElementById(nameID).value;
	var xhr = create_xhr_object();
	var result;
	
	if (name.length == 0)
		return true;
	
	if (xhr)
	{
		var timer = setTimeout(function () {
			xhr.onreadystatechange = null;
			document.getElementById(textID).innerHTML = 'Le délais est dépassé...';
			return 1;
		}, 5000);
		
		/* Affiche un logo de recherche en attendant AJAX */
		document.getElementById(textID).innerHTML = '<img src="http://'+siteUrl+'/css/search.gif" width="24" height="24" alt="Vérifie le nom ..." />';
		
		xhr.onreadystatechange = function ()
		{
			if (xhr.readyState == 4 && xhr.status == 200)
			{
				clearTimeout(timer);
				
				switch (xhr.responseText)
				{
					case '_OK' :
						document.getElementById(textID).innerHTML = '<span style="color:#900"><img src="http://'+siteUrl+'/css/done.png" width="16" height="16" alt="*" /></span>';
						break;
						
					case '_EXISTS' :
						document.getElementById(textID).innerHTML = '<span style="color:#900"><img src="http://'+siteUrl+'/css/cancel.png" width="16" height="16" alt="*" /> Le nom est déjà employé!</span>';
						break;
					
					default :
						document.getElementById(textID).innerHTML = '<span style="color:#900"><img src="http://'+siteUrl+'/css/cancel.png" width="16" height="16" alt="*" /> La vérification du nom n\'a pas pu être effectuée! (2)</span>';
						break;
				}
			}
		}
		
		xhr.open('GET', 'http://'+siteUrl+'/js/check_name.php?name='+name, true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send(null);
	}
	else
	{
		document.getElementById(textID).innerHTML = '<span style="color:#900"><img src="http://'+siteUrl+'/css/cancel.png" width="16" height="16" alt="*" /> La vérification du nom n\'a pas pu être effectuée!</span>';
	}
}

function check_input (textField, textResult, type, safeEmpty)
{
	var string;
	var expReg;
	
	if (!document.getElementById)
		return false;
		
	switch (type)
	{
		case 'number' :
			expReg = new RegExp("^[0-9]{1,}$");
			break;
			
		case 'alphanumeric' :
			expReg = new RegExp("^[a-zA-Z0-9]{1,}$");
			break;
			
		case 'text' :
			expReg = new RegExp("^[ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ a-zA-Z0-9'_\-]{1,}$");
			break;
	}
	
	string = document.getElementById(textField).value;
	
	if (safeEmpty && string == '')
	{
		document.getElementById(textResult).innerHTML = '';
		
		return true;
	}
	else
	{
		if (string.match(expReg))
		{
			document.getElementById(textResult).innerHTML = '<img src="http://'+siteUrl+'/css/done.png" width="16" height="16" alt="OK" />';
			
			return true;
		}
		else
		{
			document.getElementById(textResult).innerHTML = '<img src="http://'+siteUrl+'/css/cancel.png" width="16" height="16" alt="Mauvais" />';
			
			return false;
		}
	}
}

function check_country (countryID, checkID)
{
	var check, country;
	country = document.getElementById(countryID);
	check = document.getElementById(checkID);
	if (country.value == 0)
	{
		check.innerHTML = '<img src="http://'+siteUrl+'/css/alert.gif" width="14" height="14" alt="*" />';
	}
	else
	{
		check.innerHTML = '<img src="http://'+siteUrl+'/css/done.png" width="16" height="16" alt="V" />';
	}
}

function check_date (dayID, monthID, yearID, checkID)
{
	var check, day, month, year;
	day = document.getElementById(dayID);
	month = document.getElementById(monthID);
	year = document.getElementById(yearID);
	check = document.getElementById(checkID);
	if (day.value == 0 || month.value == 0 || year.value == 0)
	{
		check.innerHTML = '<img src="http://'+siteUrl+'/css/alert.gif" width="14" height="14" alt="*" />';
	}
	else
	{
		check.innerHTML = '<img src="http://'+siteUrl+'/css/done.png" width="16" height="16" alt="V" />';
	}
}

function check_char_count (field, counter, maxChar)
{
	var length = field.value.length;
	
	if ((maxChar - length) < 0)
	{
		alert('Vous ne pouvez pas écrire plus de ' + maxChar + ' caractères !');
		counter.innerHTML = 0;
		field.value = field.value.substring(0, maxChar);
	}
	else
	{
		counter.innerHTML = maxChar - length;
	}
}

function check_email (string)
{
	var testAt, testDot;
	var valid = new RegExp('^[a-zA-Z0-9@._\-]+$', 'g');
	
	if (!string)
		return false;
	
	if (!string.match(valid))
		return false;
	
	testAt = string.split('@');
	if (testAt.length != 2 || !testAt[1])
		return false;
	
	testDot = testAt[1].split('.');
	if (testDot.length == 1 || !testDot[1])
		return false;
	
	return true;
}

function form_email_check (emailID, outputID)
{
	var email;
	var output;
	
	if (!document.getElementById)
		return false;
	
	email = document.getElementById(emailID);
	output = document.getElementById(outputID);
	
	if ( check_email(email.value) )
	{
		output.innerHTML = '<img src="http://'+siteUrl+'/css/done.png" width="16" height="16" alt="Valide" />';
		
		return true;
	}
	else
	{
		output.innerHTML = '<img src="http://'+siteUrl+'/css/cancel.png" width="16" height="16" alt="Invalide" />';
		
		return false;
	}
}

function set_mdp (managerID, keyID, loginID, passwordID, objectID)
{
	var login = document.getElementById(loginID).value;
	var password = document.getElementById(passwordID).value;
	
	var vars = 'id='+keyID+'&mid='+managerID+'&login='+login+'&password='+password+'&submitted=true&no_html=1';
	
	// Va chercher chien-chien!
	xhr = create_xhr_object();
	if (xhr)
	{
		xhr.onreadystatechange = function ()
		{
			if (xhr.readyState == 4 && xhr.status == 200)
			{
				result = xhr.responseText;

				document.getElementById(objectID).innerHTML = result;
			}
		}
		
		xhr.open('POST', 'http://'+siteUrl+'/page_keyring_tools.php', true);
		xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xhr.send(vars);
	}
	
	return false;
}
