/* JS Document */

var g_comment_form = "<div style='display:none;'><div id='comment_form'>"
	+"<table cellspacing='5' width='100%' id='comment_table'>\
    <tbody>\
        <tr>\
            <td id='name_caption'><input name='commenter_nickname' class='commenter_nickname' /></td>\
            <td>Nom &nbsp;(Obligatoire)</td>\
        </tr>\
        <tr>\
            <td><input name='comment_email' class='comment_email' /></td>\
            <td id='email_caption'>Email &nbsp;(Obligatoire)(ne sera pas publi&eacute;)</td>\
        </tr>\
        <tr id='tr_password'>\
            <td><input type='password' class='comment_pwd' name='comment_pwd' /></td>\
            <td id='password_caption'>Mot de passe &nbsp;(Obligatoire)(Vous pouvez utiliser cela pour modifier votre commentaire)</td>\
        </tr>\
        <tr id='tr_confirm_password'>\
            <td><input type='password' class='comment_confirm_pwd' name='comment_confirm_pwd' /></td>\
            <td id='confirm_password_caption'>Confirmer mot de passe</td>\
        </tr>\
		<tr id='tr_veri_num'>\
		<td nowrap='nowrap' colspan=2 >\
			<img align=absmiddle width='206' height='20' id='img_verific_num' src='' /> \
			<img onclick='fn_refresh_image()' style='cursor:pointer;' align='absmiddle' id='b_refresh' src='/moteurajax/visitorC/images/refresh.gif' border=0 /> \
			<input id='verification_number' name='verification_number' />\
		</td>\
		<tr>\
            <td colspan='2'><textarea style='' class='comment_content' name='comment_content'></textarea></td>\
        </tr>\
        <tr>\
            <td colspan='2'><input type='button' class='comment_add' value='Submit Comment' onclick='fn_save_comment()' /></td>\
        </tr>\
    </tbody>\
	</table></div></div>\
	<form id='login_form' method='post' target='_self' >\
		<input type='hidden' id='ulog' name='ulog' />\
		<input type='hidden' id='uname' name='uname' />\
		<input type='hidden' id='upass' name='upass' />\
	</form>	\
	";
	
var g_confirm_comment_form = "<div style='display:none;'>\
	<div id='confirm_comment_form'>\
	<table>\
		<tr><td>Email</td><td><input name='confirm_email' class='confirm_email'></td>\
		<tr><td>Password</td><td><input type='password' name='confirm_password' class='confirm_password'></td>\
		<tr><td colspan='2'><input type='button' value='OK' onclick='fn_confirm_edit_comment();'>&nbsp;&nbsp;\
			<input type='button' value='Cancel' onclick='fn_cancel_edit_comment();'>\
		</td>\
	</table>\
	</div>\
	</div>";
	
var g_create_url = "/moteurajax/visitorC/create_visitor.asp";
var g_gen_cardnum_url = "/moteurajax/visitorC/gen_cardnum.asp";

var g_comment_langue,g_comment_num_page,g_comment_num_contenu,g_comment_nb_comments,g_anonym;

	var cook = document.cookie;
	cook = cook.substr(cook.indexOf("user%5Fid"));
	cook = cook.substr(0,cook.indexOf(";"));
	user_id = decodeURIComponent(cook)+"";
	user_id = user_id.substr(user_id.indexOf("=")+1);

var g_user_id = user_id;

function fn_refresh_image(){
	fn_load_verific_image();
}

var g_verific_count = 0;
function fn_load_verific_image(){
	jQuery('#b_refresh').css({'visibility':'hidden'});
	jQuery('#img_verific_num').css({'visibility':'hidden'});
	jQuery('#img_verific_num').unbind('load');
	jQuery('#img_verific_num').load(function(){
		jQuery('#b_refresh').css({'visibility':'visible'});
		jQuery('#img_verific_num').css({'visibility':'visible'});
		//fn_load_verific_number();
	});
	jQuery('#img_verific_num').attr('src',"/moteurajax/visitorC/gen_cardnum.asp?"+Math.random());
}
function fn_load_verific_number(){
	jQuery.ajax({
		url:"/moteurajax/visitorC/gen_cardnum.asp?val=check_gen_number&rand="+Math.random(),
		data:"",
		cache:false,
		success:function(ret){
			if(ret.substr(0,4)=="true"){
				jQuery('#b_refresh').css({'visibility':'visible'});
				jQuery('#img_verific_num').css({'visibility':'visible'});
				g_verific_count = 0;
			}else{
				if(g_verific_count++>5){
					alert("Failed to load the verification number image.\nPlease try again later.");
					g_verific_count = 0;
					jQuery('#b_refresh').css({'visibility':'visible'});
				}else{
					fn_load_verific_number();
				}
			}
		},
		error:function(ret){
			g_verific_count = 0;
			jQuery('#b_refresh').css({'visibility':'visible'});
			alert("Failed to load the verification number image.\nPlease try again later.");
		}
	});
}


function fn_add_comment_prev(){
	jQuery("#hiddenclicker2").fancybox({'overlayColor':'#000','overlayOpacity': 0.5,'width':'200px','height':'150px','autoScale':false,'transitionIn':
		'elastic','transitionOut':'elastic','type':'ajax'});
	jQuery("#hiddenclicker2").attr('href','/moteurajax/visitorC/confirm_dialog.asp');
	jQuery("#hiddenclicker2").trigger('click');
	
}

function fn_add_comment(b_anonym,nb_comments,langue,num_page,num_contenu){
	g_anonym = b_anonym;
	g_comment_nb_comments = nb_comments;
	g_comment_langue = langue;
	g_comment_num_page = num_page;
	g_comment_num_contenu = num_contenu;
	if(g_user_id==""){
		fn_add_comment_prev();
	}else{
		fn_add_comment_next(b_anonym,nb_comments,langue,num_page,num_contenu);
	}
}

function fn_add_comment_next(b_anonym,nb_comments,langue,num_page,num_contenu){
	fn_open_dialog('comment_form',"Ajoutez un nouveau commentaire",550,440,g_comment_form);
	jQuery("input[type!=button],textarea","#comment_form").val('');
	if(g_user_id!=""){
		jQuery('#comment_table tr#tr_password').hide();
		jQuery('#comment_table tr#tr_confirm_password').hide();
		jQuery('#comment_table tr#tr_veri_num').hide();
		jQuery('input.comment_email','#comment_form').attr('readonly',true);
		jQuery('input.comment_email','#comment_form').val(g_user_id);
		jQuery('input.commenter_nickname','#comment_form').val(g_user_id.substr(0,g_user_id.indexOf("@")) );
		jQuery('textarea.comment_content','#comment_form').css('height','265px');
	}
	else{
		jQuery('#comment_table tr#tr_password').show();
		jQuery('#comment_table tr#tr_confirm_password').show();
		jQuery('#comment_table tr#tr_veri_num').show();
		jQuery('input.comment_email','#comment_form').attr('readonly',false);
		jQuery('textarea.comment_content','#comment_form').css('height','160px');
	}
	
	if(g_user_id==""){
		fn_load_verific_image();
	}
	
	if(b_anonym=="true")
	{
		jQuery('#email_caption','#comment_form').html('Email &nbsp;(obligatoire)(ne sera pas publi&eacute;)');
		jQuery('#password_caption','#comment_form').html('Mot de passe &nbsp;(Vous pouvez l"utiliser pour supprimer ou modifier votre commentaire )');
		jQuery('#confirm_password_caption','#comment_form').html('Confirmer mot de passe');
	}
	else
	{
		jQuery('#email_caption','#comment_form').html('Email &nbsp;(obligatoire)(ne sera pas publi&eacute;)');
		jQuery('#password_caption','#comment_form').html('Mot de passe &nbsp;(Vous pouvez l"utiliser pour supprimer ou modifier votre commentaire )');
		jQuery('#confirm_password_caption','#comment_form').html('Confirmer mot de passe');
	}

}

function fn_view_more_comment(nb_comments,langue,num_page,num_contenu){

	g_comment_nb_comments = nb_comments;
	g_comment_langue = langue;
	g_comment_num_page = num_page;
	g_comment_num_contenu = num_contenu;

	var request_parameter = "meth=get_comments&langue="+langue
		+"&num_page="+num_page+"&num_contenu="+num_contenu+"&nb_comments=0";
	if(typeof(fn_init_waiter)=="function")fn_init_waiter();

	jQuery.get(
		g_comment_url+"?"+request_parameter,
		"",
		function(ret){
			
			if(typeof(fn_timeout_waiter)=="function")fn_timeout_waiter();

			var pos = ret.indexOf(":");
			var comment_count = ret.substr(0,pos);
			ret = ret.substr(pos+1);
			jQuery("#comments_"+g_comment_num_contenu).empty().html(ret);
			jQuery("#comment_count_"+g_comment_num_contenu).empty().html(comment_count);
			fn_close_dialog('comment_form');
			jQuery('#img_verific_num').attr('src',"");
		},
		function(ret){
			alert(ret);
		}
	);
}

var g_comment_url = "/moteurajax/visitorC/modules/comments.asp";

var g_verification_number = "";

function fn_save_comment(){
	
	var commenter_nickname = jQuery("#comment_form input.commenter_nickname").val();
	var comment_email = jQuery("#comment_form input.comment_email").val();
	var comment_pwd = jQuery("#comment_form input.comment_pwd").val();
	var comment_confirm_pwd = jQuery("#comment_form input.comment_confirm_pwd").val();
	var comment_content = jQuery("#comment_form textarea.comment_content").val();
	var verification_number = jQuery('#comment_form input#verification_number').val();
	
	if( !fn_valid_check_form(commenter_nickname,comment_email,comment_pwd,
						   comment_confirm_pwd,comment_content,verification_number)
	){
		return;
	}
	
	if(typeof(fn_init_waiter)=="function")fn_init_waiter();
	g_verification_number = jQuery('#comment_form input#verification_number').val();
	if(g_user_id==""){
		fn_precheck_cardnum();
	}else{
		fn_save_comment_to_server();
	}
}

var g_gen_check_count = 0;

function fn_precheck_cardnum(){
	var request_parameter = "val=check_gen_number&cardnum="+g_verification_number;
	jQuery.ajax({
		type:"GET",
		url:g_gen_cardnum_url+"?"+request_parameter,
		cache:false,
		success:function(ret){
			if(ret.substr(0,4) == "true"){
				g_gen_check_count = 0;
				fn_save_comment_to_server();
			}else{
				if(g_gen_check_count++<10){
					fn_precheck_cardnum();
				}else{
					alert("Echec de verification du code.\n Essayer encore.");
					g_gen_check_count = 0;
					if(typeof(fn_timeout_waiter)=="function")fn_timeout_waiter();
				}
			}
			
		},
		error:function(ret){
			alert("Echec de verification du code.\n Essayer encore.");
			g_gen_check_count = 0;
			if(typeof(fn_timeout_waiter)=="function")fn_timeout_waiter();
		}
	});
}

function fn_valid_check_form(commenter_nickname,comment_email,comment_pwd,
						   comment_confirm_pwd,comment_content,verification_number)
{ // validate part
	if(commenter_nickname=="")	{alert("Entrez votre surnom.");return false;}
	
	if(g_anonym=="false" && comment_email==""){
		alert("Entrez votre adresse email.");
		return false;
	}
	
	if(comment_email!="" && !email_validate(comment_email)){
		alert("Entrez une adresse email valide.");
		return false;
	}
	
	if(g_user_id==""){
		if(g_anonym=="false"){
			if(comment_pwd=="")			{alert("Entrez votre mot de passe.");return false;}
		}

		if(comment_pwd!="" && comment_pwd!=comment_confirm_pwd)
		{
			alert("Controler votre mot de passe et confirmez le.");return false;
		}
	}
	if(comment_content=="")		{alert("Entrez votre commentaire");return false;}
	return true;
}

function fn_save_comment_to_server(){
	
	var commenter_nickname = jQuery("#comment_form input.commenter_nickname").val();
	var comment_email = jQuery("#comment_form input.comment_email").val();
	var comment_pwd = jQuery("#comment_form input.comment_pwd").val();
	var comment_confirm_pwd = jQuery("#comment_form input.comment_confirm_pwd").val();
	var comment_content = jQuery("#comment_form textarea.comment_content").val();
	var verification_number = jQuery('#comment_form input#verification_number').val();
	
	if( !fn_valid_check_form(commenter_nickname,comment_email,comment_pwd,
						   comment_confirm_pwd,comment_content,verification_number)
	){
		return;
	}


	if(g_user_id==""){
		if(verification_number==""){
			alert("Entrez le code de vérification.");
			return;
		}
	}
	
	if(comment_email!="" && comment_pwd!=""){
		var s_rand = Math.random()+""; s_rand = s_rand.substr(2);
		var request_parameter = "meth=create_visitor&email="+escape(comment_email)+"&pwd="+escape(comment_pwd)+"&verification_number="+verification_number+"&rand="+s_rand;
		jQuery.ajax({
			type:"GET",
			url:g_create_url+"?"+request_parameter,
			data:"",
			cache:false,
			success:function(ret){
				if(ret=="true"){
					var commenter_nickname = jQuery("#comment_form input.commenter_nickname").val();
					var comment_email = jQuery("#comment_form input.comment_email").val();
					var comment_pwd = jQuery("#comment_form input.comment_pwd").val();
					var comment_confirm_pwd = jQuery("#comment_form input.comment_confirm_pwd").val();
					var comment_content = jQuery("#comment_form textarea.comment_content").val();
					var verification_number = jQuery('#comment_form input#verification_number').val();
	
					comment_content = comment_content.replace(/\n/g,"<br>");
					comment_content = escape(comment_content);
					

					var variables = "meth=set_comment&commenter_nickname="+commenter_nickname
						+"&comment_email="+comment_email+"&comment_pwd="+comment_pwd
						+"&comment_content="+comment_content+"&langue="+g_comment_langue+"&anonym="+g_anonym
						+"&num_page="+g_comment_num_page+"&num_contenu="+g_comment_num_contenu;
	
					
					jQuery.post(
						g_comment_url,
						variables,
						function(ret){
							if(typeof(fn_timeout_waiter)=="function")fn_timeout_waiter();
							if(ret.substr(0,4)=="true"){
								alert("Saved successfully");
								//fn_get_comments();
								//alert("Création réussi!");
								jQuery('input#ulog','#login_form').val("connecte");
								jQuery('input#uname','#login_form').val(jQuery("#comment_form input.comment_email").val());
								jQuery('input#upass','#login_form').val(jQuery("#comment_form input.comment_pwd").val());
	
								jQuery('#login_form').attr('action',g_url_now);
								jQuery('#login_form').submit();
							}else{
								alert(ret);
								if(typeof(fn_timeout_waiter)=="function")fn_timeout_waiter();
							}
						},
						function(ret){
							alert(ret);
							if(typeof(fn_timeout_waiter)=="function")fn_timeout_waiter();
						}
					);
					
				}else{
					alert(ret);
					if(typeof(fn_timeout_waiter)=="function")fn_timeout_waiter();
				}
			},
			error:function(ret){
				alert(ret);
				if(typeof(fn_timeout_waiter)=="function")fn_timeout_waiter();
			}
		});
	}else{
		var commenter_nickname = jQuery("#comment_form input.commenter_nickname").val();
		var comment_email = jQuery("#comment_form input.comment_email").val();
		var comment_pwd = jQuery("#comment_form input.comment_pwd").val();
		var comment_confirm_pwd = jQuery("#comment_form input.comment_confirm_pwd").val();
		var comment_content = jQuery("#comment_form textarea.comment_content").val();

		var variables = "meth=set_comment&commenter_nickname="+commenter_nickname
			+"&comment_email="+comment_email+"&comment_pwd="+comment_pwd
			+"&comment_content="+escape(comment_content)+"&langue="+g_comment_langue+"&anonym="+g_anonym
			+"&num_page="+g_comment_num_page+"&num_contenu="+g_comment_num_contenu;
	
		
		jQuery.post(
			g_comment_url,
			variables,
			function(ret){
				if(typeof(fn_timeout_waiter)=="function")fn_timeout_waiter();
				if(ret.substr(0,4)=="true"){
					alert("Saved successfully");
					fn_get_comments();
				}else{
					alert(ret);
				}
			},
			function(ret){
				alert(ret);
				if(typeof(fn_timeout_waiter)=="function")fn_timeout_waiter();
			}
		);
		
	}
	
}

function fn_get_comments(){
	var nb_comments = jQuery("#comments_"+g_comment_num_contenu).attr('nb_comments');
	var request_parameter = "meth=get_comments&langue="+g_comment_langue
		+"&num_page="+g_comment_num_page+"&num_contenu="+g_comment_num_contenu+"&nb_comments="+nb_comments;
	jQuery.get(
		g_comment_url+"?"+request_parameter,
		"",
		function(ret){
			if(typeof(fn_timeout_waiter)=="function")fn_timeout_waiter();
			
			var pos = ret.indexOf(":");
			var comment_count = ret.substr(0,pos);
			ret = ret.substr(pos+1);
			jQuery("#comments_"+g_comment_num_contenu).empty().html(ret);
			jQuery("#comment_count_"+g_comment_num_contenu).empty().html(comment_count);
			jQuery("input[type!=button],textarea","#comment_form").val('');
			fn_close_dialog('comment_form');
		},
		function(ret){
			alert(ret.responseText);
		}
	);
}

var g_edit_comment_id = "";
function fn_edit_comment(comment_id){
	
	g_edit_comment_id = comment_id;
	if(jQuery('#confirm_comment_form').size()==0){
		jQuery(g_confirm_comment_form).appendTo('body');
	}

	fn_open_dialog('confirm_comment_form',"Confirm your infomation",350,200,g_confirm_comment_form);
}

function fn_confirm_edit_comment(){
	var s_email = jQuery('#confirm_comment_form input.confirm_email');
	var s_password = jQuery('#confirm_comment_form input.confirm_password');
	if(s_email==""){
		alert("Please enter your email address.");
		return;
	}else{
		if(!email_validate(s_email)){
			alert("Please enter a valid email address");
			return;
		}
	}
	
	if(s_password==""){
		alert("You need to enter your password to edit this comment.");
		return;
	}
	
	var request_parameter = "meth=edit_comment&email="+s_email+"&password="+s_password;
	jQuery.get(
		g_comment_url+"?"+request_parameter,
		"",
		function(ret){
			if(ret.substr(0,4)=="true"){
				
			}
		},
		function(ret){
			
		}
	);
}

function fn_cancel_edit_comment(){
	
}


function fn_delete_comment(comment_id){
	
}


function email_validate(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return false;
   }
   return true;
}


