var dossier="wow";
var now = new Date();
//fixDate(now);
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function setCookie(name, value, expires, path, domain, secure) 
{
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function deleteCookie(name, path, domain) 
{
  if (getCookie(name)) 
  {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function verif_reg_exp(A_chaine,A_Type)
{
	var L_Reg_Exp;
	var L_Pattern ="";
	var L_result=true;
	if (A_chaine =="")
	{
		L_result=false;
	}
	
	if(A_Type=="")
	{
		L_result=false;
	}
	if(L_result)
	{
		switch(A_Type)
		{
			case  "email":
				L_Pattern="^[a-z0-9_\-]+(\.[_a-z0-9\-]+)*@([_a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)$";
			break;
			default:
				L_result = false
			break;
		}
	}
	if(L_result)
	{
		L_Reg_Exp = new RegExp(L_Pattern)
		if( L_Reg_Exp.test(A_chaine))
		{
			L_result=true;
		}
		else
		{
			L_result=false;
		}
	}
	return L_result;
}

function window_reload()
{
	window.location.reload();
}
function reload_fenetre()
{
	window.location.reload();
}
function deconnexion()
{
	$("#dialog").html("Voulez-vous vraiment vous d&eacute;connecter?");
	$("#dialog").dialog(
	{
		modal:true,
		title:"Deconnexion",
		close:function()
		{
			$(this).dialog("destroy");
		},
		buttons:
		{
			"Ok":function()
			{
				$.getJSON("index.php?a=deco");
				$(this).dialog("destroy");
				//window.location.reload();
				$("#dialog").dialog('option', 'buttons',"");
				setTimeout("reload_fenetre()",500);
			},
			"Annuler":function()
			{
				$(this).dialog("destroy");
			}
		}
	});
}

function Close_dialog()
{
	$("#dialog").dialog("destroy");
}
function renvoi_mdp()
{
	var form="";
	form='<p>Saisissez votre email :</p><input type="text" name="zs_email_mdp" id="zs_email_mdp" size="52" maxlength="50"/><br/><span id="result_act" class="erreur"></span>'
	$("#dialog").html(form);
	$("#dialog").dialog(
	{
		modal:true,
		width:'auto',
		title:"Renvoyer le mot de passe",
		close:function()
		{
			$(this).dialog("destroy");
		},
		buttons:
		{
			"Ok":function()
			{
				if($("#zs_email_mdp").val()!="" || verif_reg_exp($("#zs_email_mdp").val()))
				{
					$.getJSON("http://"+window.location.hostname+"/"+dossier+"/json/compte.php?a=mdp&email="+$("#zs_email_mdp").val()+"",function(data)
					{
						if(data)
						{
							if(data.erreur!='Aucun')
							{
								$("#result_act").html(data.erreur);
							}
							else
							{
								$("#result_act").html("Un email avec votre mot de passe a &eacute;t&eacute; envoy&eacute;");
								$("#dialog").dialog('option', 'buttons',"");
								setTimeout("Close_dialog()",2000);
							}
						}
					});
				}
			},
			"Annuler":function()
			{
				$(this).dialog("destroy");
			}
		}
	});
}
function renvoi_activation()
{
	var form="";
	form='<p>Saisissez votre email :</p><input type="text" name="zs_email_activation" id="zs_email_activation" size="52" maxlength="50"/><br/><span id="result_act" class="erreur"></span>'
	$("#dialog").html(form);
	$("#dialog").dialog(
	{
		modal:true,
		width:'auto',
		title:"Renvoyer le mail d'activation",
		close:function()
		{
			$(this).dialog("destroy");
		},
		buttons:
		{
			"Ok":function()
			{
				if($("#zs_email_activation").val()!="" || verif_reg_exp($("#zs_email_activation").val()))
				{
					$.getJSON("http://"+window.location.hostname+"/"+dossier+"/json/activation.php?email="+$("#zs_email_activation").val()+"",function(data)
					{
						if(data)
						{
							if(data.erreur!='Aucun')
							{
								$("#result_act").html(data.erreur);
							}
							else
							{
								$("#result_act").html("Un email d'activation &agrave; &eacute;t&eacute; envoy&eacute;");
								$("#dialog").dialog('option', 'buttons',"");
								setTimeout("Close_dialog()",2000);
							}
						}
					});
				}
			},
			"Annuler":function()
			{
				$(this).dialog("destroy");
			}
		}
	});
}
function Aff_img($A_img)
{
	$("#aff_img_news").html('<img src="_img/news/'+$A_img+'"/>');
}

function aff_cat_enf(a_id)
{
    if($("#forum_cat_enf_"+a_id+"").css("display")=="none")
    {
        $("#forum_cat_enf_"+a_id+"").css("display","block");
        $("#forum_cat_em_"+a_id+"").attr("class","forum_cat_visible");
    }
    else
    {
        $("#forum_cat_enf_"+a_id+"").css("display","none");
        $("#forum_cat_em_"+a_id+"").attr("class","forum_cat_cache");
    }
}
function change_forum(A_id)
{
	if(!isNaN(A_id))
	{
		window.location="forum-"+A_id+".html";
	}
}
function post_remove(A_id,A_sujet)
{
	if(confirm("Voulez vous effacer ce sujet?"))
	{
		$.getJSON("http://"+window.location.hostname+"/"+dossier+"/json/forum.php?a=remove_post&id="+A_id+"&sujet="+A_sujet);
		setTimeout("window_reload()",500);
	}
}
function sujet_remove(A_id)
{
	if(confirm("Voulez vous effacer ce sujet?"))
	{
		$.getJSON("http://"+window.location.hostname+"/"+dossier+"/json/forum.php?a=remove_sujet&id="+A_id);
		
		setTimeout("window_reload()",500);
	}
		
}
function bbcode(bbdebut, bbfin)
{

	var input = window.document.form_post_forum.ta_msg_post;
	input.focus();
	if(typeof document.selection != 'undefined')
	{
		var range = document.selection.createRange();
		var insText = range.text;
		range.text = bbdebut + insText + bbfin;
		range = document.selection.createRange();
		if (insText.length == 0)
		{
			range.move('character', -bbfin.length);
		}
		else
		{
			range.moveStart('character', bbdebut.length + insText.length + bbfin.length);
		}
		range.select();
	}
	else if(typeof input.selectionStart != 'undefined')
	{
		var start = input.selectionStart;
		var end = input.selectionEnd;
		var insText = input.value.substring(start, end);
		input.value = input.value.substr(0, start) + bbdebut + insText + bbfin + input.value.substr(end);
		var pos;
		if (insText.length == 0)
		{
			pos = start + bbdebut.length;
		}
		else
		{
			pos = start + bbdebut.length + insText.length + bbfin.length;
		}
		input.selectionStart = pos;
		input.selectionEnd = pos;
	}
	else
	{
		var pos;
		var re = new RegExp('^[0-9]{0,3}$');
		while(!re.test(pos))
		{
			pos = prompt("insertion (0.." + input.value.length + "):", "0");
		}
		if(pos > input.value.length)
		{
			pos = input.value.length;
		}
		var insText = prompt("Veuillez taper le texte");
		input.value = input.value.substr(0, pos) + bbdebut + insText + bbfin + input.value.substr(pos);
	}
}
function Aff_img_avatar(A_value)
{
	if(isNaN(A_value))
	{
		if(A_value=="nochar")
		{
			$("#img_avatar_compte").attr("src","_img/forum/portrait/no-character-icon.gif");
		}
		else
		{
			$("#img_avatar_compte").attr("src","_img/forum/portrait/gm/"+A_value+"");
		}
	}
	else
	{
		$.getJSON("http://"+window.location.hostname+"/"+dossier+"/json/compte.php?a=avatar&id_perso="+A_value,function(data)
		{
			if(data)
			{
				$("#img_avatar_compte").attr("src","_img/forum/portrait/"+data.img+"");
			}
		});
	}
}
function smilies(img)
{
	window.document.form_post_forum.ta_msg_post.value += '' + img + '';
}
function aff_credit()
{
	$.getJSON("http://"+window.location.hostname+"/"+dossier+"/json/dons.php?a=credit",function(data)
	{
		if(data)
		{
			$("#cred_rest_aff").html(data.credit);
		}
	});
}
function envoi_item(A_itemID)
{
	L_aff_com='';
	
	$.getJSON("http://"+window.location.hostname+"/"+dossier+"/json/dons.php?a=perso",function(data)
	{
		if(data)
		{
			if(data.perso.length>0)
			{
				L_aff_com+='<div style="text-align:center;">';
				L_aff_com+='<p>S&eacute;lectionnez le personnage qui recevra l\'objet</p>';
				L_aff_com+='	<select id="se_perso">';
				$.each(data.perso,function(i,doc)
				{
					//alert(doc.nom);
					L_aff_com+='<option value="'+doc.id+'">'+doc.nom+'</option>';
				});
				L_aff_com+='	</select>';
				L_aff_com+='	<div class="erreur" id="erreur_objet"></div>';
				L_aff_com+='</div>';
				
				$("#dialog").dialog(
				{
					modal:true,
					width:'auto',
					title:"Commande d'un objet",
					autoOpen:false,
					close:function()
					{
						$(this).dialog("destroy");
					},
					buttons:
					{
						"Ok":function()
						{
							$.getJSON("http://"+window.location.hostname+"/"+dossier+"/json/dons.php?a=item&id_item="+A_itemID+"&perso="+$("#se_perso").val(),function(data)
							{
								if(data)
								{
									if(data.erreur=="")
									{
										$("#erreur_objet").html("L'objet a bien &eacute;t&eacute; ajout&eacute; vous le recevrez dans votre boite aux lettres.<br/>Vous avez &eacute;t&eacute; d&eacute;bit&eacute; de "+data.credit+" cr&eacute;dit(s).<br/>Merci et bon jeu");
										$("#dialog").dialog('option', 'buttons',"");
										aff_credit();
										setTimeout("Close_dialog()",5000);
									}
									else
									{
										$("#erreur_objet").html(data.erreur);
									}
								}
							});
						},
						"Annuler":function()
						{
							$(this).dialog("destroy");
						}
					}
				});
			}
			else
			{
				L_aff_com+='<div style="text-align:center;">';
				L_aff_com+='<p class="erreur">Vous n\'avez aucun personnage sur ce compte</p>';
				L_aff_com+='</div>';
				$("#dialog").html(L_aff_com);
				$("#dialog").dialog(
				{
					modal:true,
					width:'auto',
					title:"Commande d'un objet",
					autoOpen:false,
					close:function()
					{
						$(this).dialog("destroy");
					},
					buttons:
					{
						"Ok":function()
						{
							$(this).dialog("destroy");
						}
					}
				});
			}
			$("#dialog").html(L_aff_com);
			$("#dialog").dialog('open');
		}
	});
		
	
}
function move_topic(A_topic,A_forum)
{
	L_aff='';
	$("#dialog").dialog(
				{
					modal:true,
					width:'auto',
					title:"D&eacute;placer un sujet",
					autoOpen:false,
					close:function()
					{
						$(this).dialog("destroy");
					},
					buttons:
					{
						"Ok":function()
						{
							$.getJSON("http://"+window.location.hostname+"/"+dossier+"/json/forum.php?a=move_topic&id="+A_topic+"&forum="+$("#se_forum_dest").val()+"");
							$("#af_ok").html("Sujet d&eacute;plac&eacute;");
							$("#dialog").dialog('option', 'buttons',"");
							setTimeout("reload_fenetre()",2000);
						},
						"Annuler":function()
						{
							$(this).dialog("destroy");
						}
					}
				});
	$.getJSON("http://"+window.location.hostname+"/"+dossier+"/json/forum.php?a=aff_forum",function(data)
	{
		if(data)
		{
			L_aff+='<p>selectionnez le forum de destination</p>';
			L_aff+='<p id="af_ok"></p>';
			L_aff+='<select id="se_forum_dest">';
			$.each(data.forum,function(i,doc)
			{
				L_aff+='<option value="'+doc.id+'">'+doc.Libelle+'</option>';
			});
			L_aff+='</select>';
			$("#dialog").html(L_aff);
			$("#dialog").dialog('open');
		}
	});
}
function change_nom()
{
	L_aff_com='';
	
	$.getJSON("http://"+window.location.hostname+"/"+dossier+"/json/dons.php?a=perso",function(data)
	{
		if(data)
		{
			if(data.perso.length>0)
			{
				L_aff_com+='<div style="text-align:center;">';
				L_aff_com+='<p>S&eacute;lectionnez le personnage qui changera de nom</p>';
				L_aff_com+='	<select id="se_perso">';
				$.each(data.perso,function(i,doc)
				{
					//alert(doc.nom);
					L_aff_com+='<option value="'+doc.id+'">'+doc.nom+'</option>';
				});
				L_aff_com+='	</select>';
				L_aff_com+='	<div class="erreur" id="erreur_objet"></div>';
				L_aff_com+='</div>';
				
				$("#dialog").dialog(
				{
					modal:true,
					width:'auto',
					title:"Changer le nom de votre personnage",
					autoOpen:false,
					close:function()
					{
						$(this).dialog("destroy");
					},
					buttons:
					{
						"Ok":function()
						{
							$.getJSON("http://"+window.location.hostname+"/"+dossier+"/json/dons.php?a=change_nom&perso="+$("#se_perso").val(),function(data)
							{
								if(data)
								{
									if(data.erreur=="")
									{
										$("#erreur_objet").html("Votre demande a bien &eacute;t&eacute; prise en compte, vous pourrez renommer votre personnage &agrave; la prochaine connexion.<br/>Vous avez &eacute;t&eacute; d&eacute;bit&eacute; de "+data.credit+" cr&eacute;dit(s).<br/>Merci et bon jeu");
										$("#dialog").dialog('option', 'buttons',"");
										aff_credit();
										setTimeout("Close_dialog()",5000);
									}
									else
									{
										$("#erreur_objet").html(data.erreur);
									}
								}
							});
						},
						"Annuler":function()
						{
							$(this).dialog("destroy");
						}
					}
				});
			}
			else
			{
				L_aff_com+='<div style="text-align:center;">';
				L_aff_com+='<p class="erreur">Vous n\'avez aucun personnage sur ce compte</p>';
				L_aff_com+='</div>';
				$("#dialog").html(L_aff_com);
				$("#dialog").dialog(
				{
					modal:true,
					width:'auto',
					title:"Commande d'un objet",
					autoOpen:false,
					close:function()
					{
						$(this).dialog("destroy");
					},
					buttons:
					{
						"Ok":function()
						{
							$(this).dialog("destroy");
						}
					}
				});
			}
			$("#dialog").html(L_aff_com);
			$("#dialog").dialog('open');
		}
	});
}
function custome_perso()
{
	L_aff_com='';
	
	$.getJSON("http://"+window.location.hostname+"/"+dossier+"/json/dons.php?a=perso",function(data)
	{
		if(data)
		{
			if(data.perso.length>0)
			{
				L_aff_com+='<div style="text-align:center;">';
				L_aff_com+='<p>S&eacute;lectionnez le personnage qui changera de nom</p>';
				L_aff_com+='	<select id="se_perso">';
				$.each(data.perso,function(i,doc)
				{
					//alert(doc.nom);
					L_aff_com+='<option value="'+doc.id+'">'+doc.nom+'</option>';
				});
				L_aff_com+='	</select>';
				L_aff_com+='	<div class="erreur" id="erreur_objet"></div>';
				L_aff_com+='</div>';
				
				$("#dialog").dialog(
				{
					modal:true,
					width:'auto',
					title:"Customization de personnage",
					autoOpen:false,
					close:function()
					{
						$(this).dialog("destroy");
					},
					buttons:
					{
						"Ok":function()
						{
							$.getJSON("http://"+window.location.hostname+"/"+dossier+"/json/dons.php?a=custom&perso="+$("#se_perso").val(),function(data)
							{
								if(data)
								{
									if(data.erreur=="")
									{
										$("#erreur_objet").html("Votre demande a bien &eacute;t&eacute; prise en compte, vous pourrez modifier votre personnage &agrave; la prochaine connexion.<br/>Vous avez &eacute;t&eacute; d&eacute;bit&eacute; de "+data.credit+" cr&eacute;dit(s).<br/>Merci et bon jeu");
										$("#dialog").dialog('option', 'buttons',"");
										aff_credit();
										setTimeout("Close_dialog()",5000);
									}
									else
									{
										$("#erreur_objet").html(data.erreur);
									}
								}
							});
						},
						"Annuler":function()
						{
							$(this).dialog("destroy");
						}
					}
				});
			}
			else
			{
				L_aff_com+='<div style="text-align:center;">';
				L_aff_com+='<p class="erreur">Vous n\'avez aucun personnage sur ce compte</p>';
				L_aff_com+='</div>';
				$("#dialog").html(L_aff_com);
				$("#dialog").dialog(
				{
					modal:true,
					width:'auto',
					title:"Commande d'un objet",
					autoOpen:false,
					close:function()
					{
						$(this).dialog("destroy");
					},
					buttons:
					{
						"Ok":function()
						{
							$(this).dialog("destroy");
						}
					}
				});
			}
			$("#dialog").html(L_aff_com);
			$("#dialog").dialog('open');
		}
	});
}
function transfert_perso()
{
	L_aff_com='';
	
	$.getJSON("http://"+window.location.hostname+"/"+dossier+"/json/dons.php?a=perso",function(data)
	{
		if(data)
		{
			if(data.perso.length>0)
			{
				L_aff_com+='<div style="text-align:center;">';
				L_aff_com+='<p>S&eacute;lectionnez le personnage qui sera transf&eacute;r&eacute;</p>';
				L_aff_com+='	<select id="se_perso">';
				$.each(data.perso,function(i,doc)
				{
					//alert(doc.nom);
					L_aff_com+='<option value="'+doc.id+'">'+doc.nom+'</option>';
				});
				L_aff_com+='	</select>';
				L_aff_com+='	<div class="erreur" id="erreur_objet"></div>';
				L_aff_com+='	<p id="confirm_trasfet">Tapez le nom de compte o&ugrave; vous voulez transf&eacute;rer votre personnage</p>';
				L_aff_com+='	<input type="text" id="zs_nom_compte_perso"/>';
				L_aff_com+='</div>';
				
				$("#dialog").dialog(
				{
					modal:true,
					width:'auto',
					title:"Transfert de personnage",
					autoOpen:false,
					close:function()
					{
						$(this).dialog("destroy");
					},
					buttons:
					{
						"Ok":function()
						{
							if($("#zs_nom_compte_perso").val()!="")
							{
								$("#confirm_trasfet").html("Voulez-vous vraiment transf&eacute;rer ce personnage sur le compte "+$("#zs_nom_compte_perso").val()+"");
								$("#confirm_trasfet").css("color","#ff0000");
								$("#dialog").dialog('option', 'buttons',{"OK" :function()
								{
									$.getJSON("http://"+window.location.hostname+"/"+dossier+"/json/dons.php?a=transfert&compte="+$("#zs_nom_compte_perso").val()+"&perso="+$("#se_perso").val(),function(data)
									{
										if(data)
										{
											if(data.erreur=="")
											{
												$("#erreur_objet").html("Votre personnage a bien &eacute;t&eacute; transf&eacute;r&eacute;.<br/>Vous avez &eacute;t&eacute; d&eacute;bit&eacute; de "+data.credit+" cr&eacute;dit(s).<br/>Merci et bon jeu");
												$("#dialog").dialog('option', 'buttons',"");
												$("#confirm_trasfet").html("");
												$("#zs_nom_compte_perso").fadeOut("fast");
												aff_credit();
												setTimeout("Close_dialog()",5000);
											}
											else
											{
												$("#erreur_objet").html(data.erreur);
											}
										}
									});
								},"Annuler" :function()
								{
									$(this).dialog("destroy");
								}});
							}
							else
							{
								$("#erreur_objet").html("Veuillez saisir un nom de compte");
							}
						},
						"Annuler":function()
						{
							$(this).dialog("destroy");
						}
					}
				});
			}
			else
			{
				L_aff_com+='<div style="text-align:center;">';
				L_aff_com+='<p class="erreur">Vous n\'avez aucun personnage sur ce compte</p>';
				L_aff_com+='</div>';
				$("#dialog").html(L_aff_com);
				$("#dialog").dialog(
				{
					modal:true,
					width:'auto',
					title:"Commande d'un objet",
					autoOpen:false,
					close:function()
					{
						$(this).dialog("destroy");
					},
					buttons:
					{
						"Ok":function()
						{
							$(this).dialog("destroy");
						}
					}
				});
			}
			$("#dialog").html(L_aff_com);
			$("#dialog").dialog('open');
		}
	});
}
$(document).ready(function()
{
	$(".lien_ext").attr("target","_blank");
	function twitter_info()
	{
		$.getJSON("http://api.twitter.com/1/statuses/user_timeline/BlackdragonInfo.json?callback=?",function(data)
		{
			if (data)
			{
				$("#info_twitter span").html(data[0].text);
			}
		});
		
	}
	twitter_info();
});
