$(document).ready(function() {
	$('#detail').focus(function() { $('#detail').val(''); });
	hideForm(1);
	$('input:text').keypress(function (e) { return valid(e, 'alphanum'); });
	$('textarea').keypress(function (e) { return valid(e, 'alphanum'); });
	$('input:text').blur(function () {
		var value = $(this).val();
		value = value.replace(/[^0-9A-Za-z.,@:"éàèùçê ]/gi,'') ;
		$(this).val(value);
	});
	$('textarea').blur(function () {
		var value = $(this).val();
		value = value.replace(/[^0-9A-Za-z.,@:"éàèùçê ]/gi,'') ;
		$(this).val(value);
	});
	$('#mail').focus(function () { $('#mail').val(''); });
});

function getvalue ()
{
	var i = $('#nbimages').val();
	var j = 1;
	$('#formannonceimg1').remove();
	$('#formannonceimg2').remove();
	$('#formannonceimg3').remove();
	$('#formannonceimg4').remove();
	$('#formannonceimg5').remove();
	$('#formannonceimg6').remove();
	$('#formannonceimg7').remove();
	$('#formannonceimg8').remove();
	var html = '';
	html += '<table cellspacing="0" cellpadding="0" border="0" style="margin-left:4px;">';
	
	while (i >= j)
	{
		html += '<tr id="formannonceimg'+j+'"><td width="131" style="text-align:right; padding-right:2px;">Image '+j+' :</td><td width="188"><input name="image'+j+'" type="file" /></td></tr>';
		j = j + 1;
	}
	html += '</table>';
	$('#formannonceimgtable').html(html);
}

function hideForm ()
{
	if (arguments[0] != 1)
		$('#detail').val('');
	var eTab = document.getElementById("newannonce")
	eTab.rows[3].style.display="none";
	eTab.rows[4].style.display="none";
	eTab.rows[5].style.display="none";
	eTab.rows[6].style.display="none";
	eTab.rows[7].style.display="none";
}

function showForm ()
{
	if (arguments[0] != 1)
		$('#detail').val('');
	var eTab = document.getElementById("newannonce")
	eTab.rows[3].style.display="";
	eTab.rows[4].style.display="";
	eTab.rows[5].style.display="";
	eTab.rows[6].style.display="";
	eTab.rows[7].style.display="";
}

//:: Controle les caractàres tappés par l'utilisateur dans un formulaire :://
function valid(evt,type) {
	if      (type=="price")  var interdit = '+azertyuiopqsdfghjklmnbvcxwAZERTYUIOPMLKJHGFDSQWXCVBNàâäãçéèêëìîïòôöõùûüñ&*?@_=!-:;\t#~"^$£?²¤§%*°¨µ()[]{}<>|\\/`\'€'; 
	else if (type=="alphanum")  var interdit = '&*?!_;\t#~^%$£?²¤§*°¨µ=()[]{}<>|\\/`';
	else if (type=="site")		var interdit = '+àâäãçéèêëìîïòôöõùûüñ*!;,\t#"=^$£²¤§%*@°¨µ()[]{}<>|\`\'€'; 
	else if (type=="num") 		var interdit = 'azertyuiopqsdfghjklmnbvcxwAZERTYUIOPMLKJHGFDSQWXCVBNàâäãçéèêëìîïòôöõùûüñ&*?@_=!:;,\t#~"^$£?²¤§%*°¨µ()[]{}<>|\\/`\'€'; 
	else if (type=="email") 	var interdit = '&*?!_;\t#~^%$£?²¤§*°¨µ=()[]{}<>|\\/`€';
	else 						var interdit = '\t#~^%$£?²¤§¨µ[]{}<>|\\`\'€'; 
	var keyCode = evt.which ? evt.which : evt.keyCode;
	if (keyCode==9 || keyCode==71) return true;
	if (interdit.indexOf(String.fromCharCode(keyCode)) >= 0) {
		return false;
	}
}

// Script Ajax perso ( POST/GET en asynchrone )
function ajax ( type, fichier, variables /* , fonction */ ) 
{
	if ( window.XMLHttpRequest ) var req = new XMLHttpRequest();
	else if ( window.ActiveXObject ) var req = new ActiveXObject("Microsoft.XMLHTTP");
	else alert("Votre navigateur n'est pas assez r&eacute;cent pour acc&eacute;der &agrave; cette fonction, ou les ActiveX ne sont pas autoris&eacute;s");
	if ( arguments.length==4 ) var fonction = arguments[3];

	if (type.toLowerCase()=="post") {
		req.open("POST", _URL+fichier, true);
		req.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=iso-8859-1');
		req.send(variables);
	} else if (type.toLowerCase()=="get") {
		req.open('get', _URL+fichier+"?"+variables, true);
		req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=iso-8859-1');
		req.send(null);
	} else { 
		alert("M&eacute;thode d'envoie des donn&eacute;es invalide"); 
	}

	req.onreadystatechange = function()  { 
		if (req.readyState == 4 && req.responseText != null )
		{				
			if (fonction) eval( fonction + "('"+escape(req.responseText)+"')");
			
		} 
	}
}

// Connexion automatique 
function loginAjax() 
{
	$('#status_log').html("<img src='images/indicator2.gif' style='vertical-align:middle'/> V&eacute;rification");
	$('#status_log').css('display',"inline");
	$('#status_log').css('color',"#0066FF");

	_login=escape($('#login').val());
	_pass=escape($('#pass').val());
	ajax ('get', 'include/ajax-login.php', 'login='+_login+'&pass='+_pass, 'loginAjaxAction');
}

function loginAjaxAction(r)
{
	var verif = unescape(r).split('|');
	if (verif[0] == "+") {
		$('#member_box').html("Connection r&eacute;ussie,<br />" + verif[1] + "<br /><a href='index.php?page=logout'>Se d&eacute;connecter</a>");
		redirect ("/index.php");
	}
	else
	{		
		$('#status_log').html("Identifiants incorrects");
		$('#status_log').css('display',"inline");
		$('#status_log').css('color',"#FF0066");
	}
}

function redirect (page)
{
	window.location = page;
}

function openpub (u, type)
{
	
	l = (document.all ? screen.width : screen.width) * 0.9;
	h = (document.all ? screen.height : screen.height) * 0.9;
	top = (screen.height - h) / 2;
	left = (screen.width - l) / 2;
	conf = 'scrollbars=yes,toolbar=yes,location=yes,directories=yes,menubar=yes,resizable=yes,status=yes,width=' + l + ',height=' + h + ',top=' + top + ',left=' + left;
	var F;
	linkpub = '/include/goto.php?type=' + type + '&link=' + u;
	F = open (linkpub, '', conf);
	F.focus();
}
