var re_mail = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/

function addToFavorites()
{
	//alert(window.location.href);
	//if($.browser.msie)
	//else if($.browser.mozilla)
	if(window.sidebar || $.browser.mozilla)
		window.sidebar.addPanel($('title').text(),window.location.href,false);
	else if(window.external || $.browser.msie)
		window.external.AddFavorite(window.location.href, $('title').text());
	else
		alert("Cette fonctionnalité n'est disponible que pour Internet Explorer ou Firefox.\nVous devez utiliser le menu \"Favoris\" ou \"Marques-pages\" de votre navigateur ou utiliser le raccourcis Ctrl+D sur Windows ou Pomme+D sur MacOS.");
}
function sendToAFriend()
{
	//alert(window.location.href);
	
	$('body').append(
		'<div id="sendtofriend">'+
			'<h1><a href="javascript:void(0);" onclick="closeDOMWindow(\'sendtofriend\');">X</a>'+label('send_to_a_friend_title')+'</h1>'+
			'<div class="formcontainer">'+
				'<form name="sendtofriend_form" id="sendtofriend_form" method="post" action="javascript:void(0);">'+
					'<input type="hidden" name="page_url" id="page_url" value="'+window.location.href+'" />'+
					'<label for="your_name">'+label('your_name')+'</label><input type="text" name="your_name" id="your_name" /><br />'+
					'<label for="your_mail">'+label('your_email')+'</label><input type="text" name="your_mail" id="your_mail" /><br />'+
					'<label for="your_friendsmail">'+label('your_friends_email')+' (max. 5 email)</label>'+
					'<textarea name="your_friendsmail" id="your_friendsmail"></textarea><br />'+
					'<div class="legend">'+label('sendtoafriend_legend')+'</div>'+
					'<label for="your_text">'+label('your_text')+'</label><textarea name="your_text" id="your_text"></textarea><br />'+
					'<div class="forms_button">'+
						'<input type="image" src="/img/fiche/envoyer.gif" id="send_it" name="send_it"/>'+
					'</div>'+
				'</form>'+
			'</div>'+
		'</div>'
	);
	iframe4IE($('#sendtofriend'));
	$('#send_it').click(function() {
		if($('#your_name').val()!='')
		{
			if($('#your_mail').val()!='')
			{
				ym = $('#your_mail').val();
				if(re_mail.test(ym))
				{
					if($('#your_friendsmail').val()!='')
					{
						yfm = $('#your_friendsmail').val();
						yfm = yfm.replace(/^;/,'');
						yfm = yfm.replace(/;$/,'');
						var armail = yfm.split(';');
						bValid = true;
						for(a=0;a<armail.length;a++)
						{
							if(!re_mail.test(armail[a]))
							{
								bValid = false;
								break;
							}
						}
						if(bValid)
						{
							var varpost = $('#sendtofriend_form').serialize();
							$.ajax({
								type: "POST",
								url: '/lb/get_script.php?obj=utilities/userfriendly/sendtoafriend',
								dataType: 'xml',
								data: varpost,
								success: function(xml,str) {
									resp = $('response',xml);
									ok = resp.attr('api_return');
									if(ok=='0')
									{
										error = $('error',xml).text();
										if(error!='')
											alert(error);
										else
										{
											
											$('#sendtofriend div.formcontainer').empty();
											$('#sendtofriend div.formcontainer').append(
												'<p>Un problème est survenu et votre message n\'a pu être envoyé aux adresses suivantes:'+
												'<br /><ul></ul></p>'
											);
											$('mail',resp).each(function() {
												$('#sendtofriend div.formcontainer p ul').append(
													'<li>'+$(this).text()+'</li>'
												);
											});
										}
									}
									else
									{
										$('#sendtofriend div.formcontainer').empty();
										$('#sendtofriend div.formcontainer').append(
											'<p>'+$('message',resp).text()+
											'<br />'+
											'<ul></ul>'+
											'</p>'
										);
										//alert(resp.text());
										$('mail',resp).each(function() {
											var txtm = $(this).text();
											$('<li>'+txtm+'</li>').appendTo('#sendtofriend div.formcontainer ul');
										});
									}
								},
								error: function(oxml,str) {
									alert(str);
								}
							});
						}
						else
							throwError('your_friendsmail_notgood');
							//throwError(label('your_friendsmail_notgood'));
					}
					else
						throwError('your_friendsmail_empty');
						//throwError(label('your_friendsmail_empty'));
				}
				else
					throwError('your_mail_notgood');
					//throwError(label('your_mail_notgood'));
			}
			else
				throwError('your_mail_empty');
				//throwError(label('your_mail_empty'));
		}
		else
			throwError('your_name_empty');
			//throwError(label('your_name_empty'));
	});
}
function throwError(str)
{
	alert(str);
}
function iframe4IE(jqobj)
{
	if($.browser.msie)
	{
		$('<iframe name="cache_ie_'+jqobj.attr('id')+'" id="cache_ie_'+jqobj.attr('id')+'" src="about:blank"></iframe>').appendTo('body');
		o_offset = jqobj.offset();
		$('#cache_ie').css({
			position: 'absolute',
			top: o_offset.top+'px',
			left: o_offset.left+'px',
			width: jqobj.width(),
			height: jqobj.height(),
			border: '0px',
			marginTop: jqobj.css('margin-top'),
			marginLeft: jqobj.css('margin-left'),
			zIndex: (parseInt(jqobj.css('z-index'))-1)
		});
	}
}
function closeDOMWindow(o_id)
{
	$('#'+o_id).empty().remove();
	$('#cache_ie_'+o_id).empty().remove();
}
function printerFriendly()
{
	window.open('/imprimer.php?purl='+document.location.href,'printer_friendly','width=660,height=450,scrollbars=yes');
}
function getLoc(href)
{
	alert(href+' #column1');
	$('body div#txt_to_print').load(href+' #column1');
}
t = 0;
function checkComment()
{
	t++;
	if(t<2)
	{
		var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/
		var f = $('div.f_laisser form');
		var return_str = '';
		var m = $('input#usremail',f).val();
		if($('input#usrname',f).val()=='') return_str += 'Votre nom\n';
		if(m=='') return_str += 'Votre adresse email\n';
		else if(!m.match(reg)) return_str += 'Adresse email invalide\n';
		if($('textarea#usrcomment',f).val()=='') return_str += 'Votre commentaire\n';
		var capt = $('input#txt_i',f).val();
		if(capt.length<6||capt.length>6) return_str += 'Le champ de contrÃ´le avec l\'image\n';
		if(return_str.length>0) {
			alert('Certains champs du formulaire ne sont pas correctement remplis:\n'+return_str);
		}
		else
		{
			f.attr('action',$('input#action',f).val());
			f.submit();
		}
	} else t = 0;
}
function showHints()
{
	
}
function getSearchKeywords()
{
/*
	var kw = document.location.search.split('&');
	var k = '';
	var nb = kw.length;
	var a = 0;
	while(nb>a)
	{
		if(kw[a].match(/recherche/))
		{
			k = kw[a];
			break;
		}
		a++;
	}
	*/
}
$(document).ready(function(){
	$.getScript('/js/shared/getLabels2.js.php?fm=userfriendly');
	$('input#btn_com').click(function(){ checkComment();});
	getSearchKeywords();
//	$('div.f_laisser form input[@type=submit]').click(function(){ checkComment();});
});
