// ==UserScript==
// @name           botFight
// @namespace      ΞViPΞ Team
// @include        http://r1.fr.bloodwars.net/?a=*
// @include        http://r2.fr.bloodwars.net/?a=*
// ==/UserScript==

// GM_setValue(location.hostname + getNameJoueur() + "adversaire", "");
var adversaire = new Array();

// ------------------------------------------------------------------------------------------------
// ======================================== LANG ==================================================
// ------------------------------------------------------------------------------------------------

function estPolonais(){
	return new RegExp("http\:\/\/r.\.bloodwars.interia.pl").test(location.href);
}

function estAnglais(){
	return new RegExp("http\:\/\/r.\.bloodwars.net").test(location.href);
}

var raceName = new Array();
raceName[1]=1;
raceName[2]=2;
raceName[3]=3;
raceName[4]=4;
raceName[5]=5;

var sUpdate	= 0;
var sCapteur	= 1;
var sCultiste	= 2;
var sSeigneur	= 3;
var sAbsorbeur	= 4;
var sDamne	= 5;
var sChooseRace	= 6;
var sParamStuff1= 7;
var sNbOfArk	= 8;
var sCharisme	= 9;
var sReputation = 10;
var sAgilite	= 11;
var sForce	= 12;
var sResistance	= 13;
var sChance	= 14;
var sPerception	= 15;
var sMajeste	= 16;
var sArdSang	= 17;
var sSangVie	= 18;
var sOmbreBete	= 19;
var sSilence	= 20;
var sAbsForce	= 21;
var sSouffle	= 22;
var sHorreur	= 23;
var splayerAdded= 24;
var sStartBot	= 25;
var sStopBot	= 26;
var sOptionsBot	= 27;
var sTaxi	= 28;
var sFightList	= 29;
var sFightTotal	= 30;
var sAdd2List	= 31;
var sDel2Total	= 32;

var frenchStrings = new Array(
	"Mettre à jour les options générale",
	"Capteur d'Esprit",
	"Cultiste",
	"Seigneur des Bêtes",
	"Absorbeur",
	"Damné",
	"Choisir la Race",
	"Choisir le stuff à replacer après les attaques.",
	"Nombre d'arcane",
	"Masque d'Adonis (Charisme)",
	"Masque de Caligula (Réputation)",
	"Voies Félines (Agilité)",
	"Frénésie Sauvage (Force)",
	"Peau de Bête (Rsistance)",
	"Pouvoir du Sang (Coups Critiques)",
	"Le Chasseur de la Nuit (Perception)",
	"La Majesté (Augmente la vie)",
	"L'Ardeur du Sang (Dommages a chaques manches)",
	"Le Sang de la Vie (Regénère les points de vie)",
	"Ombre de la Bête (Une attaque en plus CAC)",
	"Silence du Sang (Annule arcanes adverse)",
	"Absorption de la Force (Reprend arcanes adverse)",
	"Le Souffle Mortel",
	"L'Horreur (Bloque l'adversaire durant la première manche)",
	"à correctement été ajouté à la liste.",
	"Démarrer le FightBot",
	"Arrêter le FightBot",
	"Options du FightBot",
	"Vitesse du Taxi",
	"Liste des adversaires",
	"Liste complète des adversaires",
	"Ajouter à la liste d'adversaires",
	"Supprimer de la liste totale"
);

var englishStrings = new Array(
	"Mettre à jour les options générale",
	"Capteur d'Esprit",
	"Cultiste",
	"Seigneur des Bêtes",
	"Absorbeur",
	"Damné",
	"Choisir la Race",
	"Choisir le stuff à replacer après les attaques.",
	"Nombre d'arcane",
	"Masque d'Adonis (Charisme)",
	"Masque de Caligula (Réputation)",
	"Voies Félines (Agilité)",
	"Frénésie Sauvage (Force)",
	"Peau de Bête (Rsistance)",
	"Pouvoir du Sang (Coups Critiques)",
	"Le Chasseur de la Nuit (Perception)",
	"La Majesté (Augmente la vie)",
	"L'Ardeur du Sang (Dommages a chaques manches)",
	"Le Sang de la Vie (Regénère les points de vie)",
	"Ombre de la Bête (Une attaque en plus CAC)",
	"Silence du Sang (Annule arcanes adverse)",
	"Absorption de la Force (Reprend arcanes adverse)",
	"Le Souffle Mortel",
	"L'Horreur (Bloque l'adversaire durant la première manche)",
	"à correctement été ajouté à la liste.",
	"Démarrer le FightBot",
	"Arrêter le FightBot",
	"Options du FightBot",
	"Vitesse du Taxi",
	"Liste des adversaires",
	"Liste complète des adversaires",
	"Ajouter à la liste d'adversaires",
	"Supprimer de la liste totale"
);


var strings;
if (estPolonais()) {
	strings = polishStrings;
} else if (estAnglais()) {
	strings = englishStrings;
} else {
	strings = frenchStrings;
}

// ------------------------------------------------------------------------------------------------
// ======================================== Functions =============================================
// ------------------------------------------------------------------------------------------------

function getChildAttr(element, tagName, attributeNames, attributeValues){
	if (element == null || attributeNames.length != attributeValues.length) {
		return null;
	}
	//alert("Looking for " + tagName + " in " + element.childNodes.length + " elements.");
	for (var i = 0; i < element.childNodes.length; ++i) {
		if (tagName == null || element.childNodes[i].nodeName == tagName) {
			//alert("found " + tagName);
			var j;
			for (j = 0; j < attributeNames.length; ++j) {
				if (element.childNodes[i].getAttribute(attributeNames[j]) != attributeValues[j]) {
					break;
				}
			}
			if (j == attributeNames.length) {
				return element.childNodes[i];
			}
		}
	}
	return null;
}

function getArgent() {
	var divMain = getChildAttr(document.body, "DIV", Array("class"), Array("main"));
	var subDiv = getChild(divMain, "DIV");
	var divTop = getChildAttr(subDiv, "DIV", Array("class"), Array("top"));
	var divCash = getChildAttr(divTop, "DIV", Array("class"), Array("topstats stats-cash"));
	var table = getChild(divCash, "TABLE");
	var tbody = getChild(table, "TBODY");
	var tr = getChild(tbody, "TR");
	var td = getChildAttr(tr, "TD", Array("class"), Array("panel-cell"));
	var str = td.childNodes[4].nodeValue;
	var numbers = str.match(/(\d{1,3})/g);
	var strFormattee = "";
	for(var i = 0; i < numbers.length; ++i) {
		strFormattee = strFormattee.concat(numbers[i]);
	}
	return parseInt(strFormattee);
}

function getAssezPourFaireFerraille(){
		return getArgent()>26000;
}
function getChild(element, tagName){
	return getChildAttr(element, tagName, Array(), Array());
}

function incremIndiceCourant(){
	GM_setValue(location.hostname + getNameJoueur() + "indiceCourant",GM_getValue(location.hostname + getNameJoueur() + "indiceCourant", 0)+1);
}

function resetIndiceCourant(){
	GM_setValue(location.hostname + getNameJoueur() + "indiceCourant", 0);
}

function getIndiceCourant(){
	return GM_getValue(location.hostname + getNameJoueur() + "indiceCourant", 0);
}

function setChangementPageAutomatique(){
	GM_setValue(location.hostname + getNameJoueur() + "autoNavigate", "true");
}

function setChangementPageAutomatiqueEffectue(){
	GM_setValue(location.hostname + getNameJoueur() + "autoNavigate", "false");
}

function estChangementPageAutomatique(){
	return GM_getValue(location.hostname + getNameJoueur() + "autoNavigate", "false") == "true";
}
function isFightBOTRunning(){
	return GM_getValue(location.hostname + getNameJoueur() + "isBotFightRunning", "false") == "true";
}

function startFightBOT(){
	GM_setValue(location.hostname + getNameJoueur() + "isBotFightRunning", "true");
}

function stopFightBOT(){
	resetIndiceCourant();
	GM_setValue(location.hostname + getNameJoueur() + "isBotFightRunning", "false");
}

function estSurPageMagasin(){
	return document.getElementById("townshop-left") != null;
}
function estSurPageArmurerie(){
	return document.getElementById("formularz") != null;
}

function estSurPageAttaque(){
	return document.getElementsByClassName("search")!=null && document.getElementsByClassName("search").length!=0;
}

function estSurProfil(){
	return document.getElementsByClassName("profile") != null && document.getElementsByClassName("profile").length!=0;
}

function estSurArcaneAttaque(){
	var div=document.getElementsByClassName("top-options")[0];
	if(div!=null){
		a=div.getElementsByTagName("A");
		if(a!=null){
			return a.length==3 && a[1].className=="active"&&a[1].innerHTML=="ATTAQUER";
		}
	}
	
	return false;
}

function estSurPageProgressionQuete(){
	return estSurPageAttaque() && document.getElementById("atkTime") != null;
}

function estSurPageMessages(){
	var div=document.getElementsByClassName("top-options")[0];
	if(div!=null){
		a=div.getElementsByTagName("A");
		if(a!=null){
			return a.length==5 && a[0].className=="active"&&a[0].innerHTML=="LISTE DES MESSAGES";
		}
	}
	return false;
}

function estSurRapportAttaque(){
	if(estSurPageMessages()){
		var div=document.getElementsByClassName("msg-content ")[0];
		if(div!=null&&div.length!=0){
			a=div.getElementsByTagName("A");
			if(a!=null){
				
				if(a.length!=0&&a[0].className=="clanSecOwner"){
						return true;
				}
			}
		}
	}
	return false;
}

function entrerPseudo(ind){
	var Pseudo=adversaire[ind];
	var champ=document.getElementsByClassName("inputbox");
	var button=document.getElementsByClassName("button");
	if(champ.length!=0){
		champ[0].value=Pseudo;
		setChangementPageAutomatique();
		button[0].click();
	}		
}

function replaceAdversaire(ind){
	var i=adversaire.length;
	var tmp1,tmp2;
	while(i>-1&&timeAdversaire[ind]<timeAdversaire[i]){
		i--;
	}
	if(i!=-1){
		tmp1=timeAdversaire[ind];
		tmp2=adversaire[ind];
		timeAdversaire[ind]=timeAdversaire[i];
		adversaire[ind]=adversaire[i];
		timeAdversaire[i]=tmp1;
		adversaire[i]=tmp2;
	}
}

function attaqueProfil(){
	var i;
	var j=0;
	var legAction;
	var fieldSet;
	var trouve=0;
	var a;
	//recherche de action dans la legende du champs de saisie
	fieldSet=document.getElementsByTagName("FIELDSET");
	if(fieldSet!=null&&fieldSet.length!=0){
		lg2=fieldSet.length;
		while(j<lg2&&!trouve){
			i=0;
			legAction=fieldSet[j].getElementsByTagName("legend");
			lg=legAction.length;
			while(i<lg&&legAction[i].innerHTML!="ACTIONS"){
				i++;
			}
			trouve=(i!=lg);
			j++;
		}	
		if(trouve){//si "ACTIONS" est trouvé
			if(fieldSet[j-1].getElementsByTagName("SPAN").length==0){ //il n'y a pas de compt à rebourgs
				lg=fieldSet[j-1].getElementsByClassName("profileLinks").length;
				i=0;
				a=fieldSet[j-1].getElementsByClassName("profileLinks");
				while(i<lg&&a[i].innerHTML!="ATTAQUER"){//recherche du lien d'attaque
					i++;
				}
				if(i<lg){//on la trouver 
					setChangementPageAutomatique();
					window.document.location =	a[i].href;
				}
				else{//le joueur est bloqué ou en congé
					incremIndiceCourant();
					AllerPageAttaque();		// on passe au joueur suivant
				}					
			}
			else{// le joueur n'est pas attaquable
				incremIndiceCourant();
				AllerPageAttaque(); //on passe au joueur suivant 
			}
		}
	}
}

function lanceAttaque(){
//arcrane
		//3eme arcane seigneur des betes
		var arcaneBorder=document.getElementsByClassName("arcane-border")[0];
		var checkBox=document.getElementById("ark_15");
		checkBox.click();
//taxi
		var taxiDisp = document.getElementById('taxiDisp');
			if (taxiDisp) {
				var nivTaxi=4
				document.getElementById('taxi').value = nivTaxi;
			}
			setChangementPageAutomatique();
			document.getElementById("submit").click();
}

function coutArc(ark){
	var arcaneBorder=document.getElementsByClassName("arcane-border")[0];
	var code=arcaneBorder.innerHTML;
	var ind=code.indexOf("addArc(1, "+ark,0);
	var ind2=code.indexOf(")",ind);
	return parseInt(code.substring(ind+11+ark.length,ind2));
}

function getNbAttaquesRestantes(){
	var content = document.getElementById("content-mid");
	var div = getChildAttr(content, "DIV", Array("style"), Array("margin-top: 10px; margin-bottom: 20px;"));
	var span = getChild(div, "SPAN");
	var b = getChild(span, "B");
	return parseInt(b.childNodes[0].nodeValue);
}

function getSecondesRestantesA()
{
	var timeleft = document.getElementById("atkTime");
	var str = timeleft.childNodes[0].nodeValue;
	var myArray = /(\d+) s/.exec(str);
	secondsRemaining = -1;
	if (myArray != null) {
		secondsRemaining = parseInt(myArray[1]);
	} else {
		myArray = /\d:(\d{2}):(\d{2})/.exec(str);
		if (myArray != null) {
			secondsRemaining = (parseInt(myArray[1]) * 60) + parseInt(myArray[2]);
		}
	}
	return secondsRemaining;
}

function attendreFinDeLAttaque()
{
	var secondsRemaining = getSecondesRestantesA();
	if (secondsRemaining >= 0) {
		setTimeout(attendreFinDeLAttaque, (secondsRemaining + Math.ceil(10*Math.random()) + 1) * 1000);
	} else {
		var timeleft = document.getElementById("atkTime");
		if (timeleft.childNodes[0].nodeName == "A") {
			setChangementPageAutomatique();
			window.location.href = timeleft.childNodes[0].href;
		}
	}
}

function pageAttaque(){
	window.location.href = '/?a=ambush';
}
function pageArmurerie(){
	window.location.href = '/?a=equip';
}
function pageMagasin(){
	window.location.href = '/?a=townshop';
}

function AllerPageAttaque(){
	setChangementPageAutomatique();
	setTimeout(pageAttaque, Math.ceil(Math.random() * 1000)+1500);
}

function allerPageArmurerie(){
	setChangementPageAutomatique();
	setTimeout(pageArmurerie, Math.ceil(Math.random() * 1000)+1500);
}

function allerPageMagasin(){
	setChangementPageAutomatique();
	setTimeout(pageMagasin, Math.ceil(Math.random() * 1000)+1500);
}

function mettreStuffDef(){
		var ensAremettre = GM_getValue(location.hostname + getNameJoueur() + "ens");
		document.location = ('http://' + location.hostname + '?a=equip&eqset='+ensAremettre);
		setTimeout(mettreStuffDef, Math.ceil(Math.random() * 1000)+1000);
}
function acheterFerraille(){
		setChangementPageAutomatique();
		var fieldSet=document.getElementsByClassName("equip")[0];
		var a=fieldSet.getElementsByTagName("A")[0];
		window.document.location =	a.href;
		
	
}
 
function premiereUtilisation(){
	var pasPremier = GM_getValue(location.hostname + getNameJoueur() + "firstUsageF") == "true";
	GM_setValue(location.hostname + getNameJoueur() + "firstUsageF", "true");
	return !pasPremier;
}
  //************************************//
 //				runAttackBot		   //	
//************************************//
function runAttackBot(){

	if (premiereUtilisation()) {
		//var answer = confirm(strings[sFirstUsage]);
		//if (!answer) {
			stopBOTFight();
			return;
		//}
	}
	var ind=getIndiceCourant();
	var changementPageAutomatique = estChangementPageAutomatique();
	setChangementPageAutomatiqueEffectue();
	if(ind<adversaire.length){
		if(changementPageAutomatique){
			
			if(estSurPageAttaque()){//1
				if(getAssezPourFaireFerraille()){
					allerPageMagasin();
				}
				else{ 
					if(estSurPageProgressionQuete()){//4
						
						attendreFinDeLAttaque();
					}
					else{
						if(document.getElementsByClassName("auBid")!=null&&document.getElementsByClassName("auBid").length!=0){//si le joueur n'existe pas on passe au suivant
							incremIndiceCourant();
							AllerPageAttaque();		
						}
						entrerPseudo(ind);
					}
				}
			}
			else{	
				if(estSurProfil()){//2
					attaqueProfil();
				}
				else{
					if(estSurArcaneAttaque()){//3
						if(getNbAttaquesRestantes()>0){//on peut attaquer
							if(document.getElementsByClassName("auBid")!=null&&document.getElementsByClassName("auBid").length!=0){//si pas attaquable
								incremIndiceCourant();
								AllerPageAttaque();
							}
							else{
								
								lanceAttaque();
							}
						}
						else{//plus d'attaque
							allerPageArmurerie();
						}
					}
					else{
						if(estSurRapportAttaque()){//5
								incremIndiceCourant();
								AllerPageAttaque();
						}
						else{
							if(estSurPageArmurerie()){
								stopFightBOT();
								setTimeout(mettreStuffDef, Math.ceil(Math.random() * 10)+1000);
							}
							else{
								if(estSurPageMagasin()){
									if(getAssezPourFaireFerraille()){
										acheterFerraille();
									}
									else{
										AllerPageAttaque();
									}
								}		
							}	
						}	
					}
				}
			}
		}
	}
	else{
		if(changementPageAutomatique){
			if(estSurPageMagasin()){
				if(getAssezPourFaireFerraille()){
					acheterFerraille();
				}
				else{
					allerPageArmurerie();
				}
			}
			else{
				if(estSurPageArmurerie()){
					stopFightBOT();
					setTimeout(mettreStuffDef, Math.ceil(Math.random() * 10)+1000);
				}
				else{
					if(estSurRapportAttaque()||estSurProfil()||estSurPageAttaque()){//pas vraiment utile le test
						if(getAssezPourFaireFerraille()){
							allerPageMagasin();
						}
						else{
							allerPageArmurerie();
						}
					}
				}
			}
		}	
	}
}

function Demarrer(){
	startFightBOT();
	AllerPageAttaque();
}
function Arreter(){
	stopFightBOT();
}


// ------------------------------------------------------------------------------------------------
// ==================================== Array Function ============================================
// ------------------------------------------------------------------------------------------------

function getNameJoueur(){
	var stat=document.getElementsByClassName("stats-player")[0];
	nom=stat.getElementsByClassName("me")[0];
	return nom.innerHTML;
}
function getTab(nomTab){
	chaineTemp=GM_getValue(location.hostname + getNameJoueur() +""+nomTab, "");
	var sep = '|*|';
	tabTemp=chaineTemp.split(sep);
	tabTemp.length--;
	return tabTemp;
}

function ajouterElementTab(tab,element){
	tab[tab.length]=element;
	return tab;
}

function estElementDuTab(tab,element){
	return getIndElementTab(tab,element)!=-1;
}

function getIndElementTab(tab,element){
	var i=0;
	while(i<tab.length&&!(element==tab[i])){
		i++;
	}
	return i!=tab.length ? i : -1 ;
}

function tabVersChaine(tab){
	var chaine="";
	var i=0;
	while(i<tab.length){
		chaine+=tab[i]+"|*|";
		i++;
	}
	return chaine;
}

function suppElementTab(tab,element){
	var i=getIndElementTab(tab,element);
	if(i!=-1){
		while(i<tab.length-1){
			tab[i]=tab[i+1];
			i++;
		}
		tab.length--;
	}
	return tab;
}


// ------------------------------------------------------------------------------------------------
// =================================== KOKO'S Function ============================================
// ------------------------------------------------------------------------------------------------
function arcanes(){
	var opt = '';
	var racetmp = GM_getValue(location.hostname + getNameJoueur() + "race");

	var ark1 = GM_getValue(location.hostname + getNameJoueur() + "ark_1");
	var ark2 = GM_getValue(location.hostname + getNameJoueur() + "ark_2");
	var ark3 = GM_getValue(location.hostname + getNameJoueur() + "ark_3");

	if(racetmp == 1 || racetmp==2 || racetmp==4){
		document.getElementById('ark3_tr').style.visibility = "visible";
	}else{
		document.getElementById('ark3_tr').style.visibility = "hidden";
	}

	if(racetmp == 3 || racetmp==5){
		document.getElementById('ark3a_tr').style.visibility = "visible";
	}else{
		document.getElementById('ark3a_tr').style.visibility = "hidden";
	}

	if(racetmp == 1){
		document.getElementById('ark1_txt').innerHTML	= '- '+strings[sCharisme];
		document.getElementById('ark2_txt').innerHTML	= '- '+strings[sReputation];
		document.getElementById('ark3_txt').innerHTML	= '- '+strings[sMajeste];
		document.getElementById('ark1').value		= ark1;
		document.getElementById('ark2').value		= ark2;
		document.getElementById('ark3').value		= ark3;
	}
	if(racetmp == 2){
		document.getElementById('ark1_txt').innerHTML	= '- '+strings[sSangVie];
		document.getElementById('ark2_txt').innerHTML	= '- '+strings[sAgilite];
		document.getElementById('ark3_txt').innerHTML	= '- '+strings[sArdSang];
		document.getElementById('ark1').value		= ark1;
		document.getElementById('ark2').value		= ark2;
		document.getElementById('ark3').value		= ark3;
	}
	if(racetmp == 3){
		document.getElementById('ark1_txt').innerHTML	= '- '+strings[sForce];
		document.getElementById('ark2_txt').innerHTML	= '- '+strings[sResistance];
		document.getElementById('ark3a_txt').innerHTML	= '- '+strings[sOmbreBete];
		document.getElementById('ark1').value		= ark1;
		document.getElementById('ark2').value		= ark2;
		if(ark3 == 1){ document.getElementById('ark3a').checked	= true; }

	}
	if(racetmp == 4){
		document.getElementById('ark1_txt').innerHTML	= '- '+strings[sSilence];
		document.getElementById('ark2_txt').innerHTML	= '- '+strings[sAbsForce];
		document.getElementById('ark3_txt').innerHTML	= '- '+strings[sChance];
		document.getElementById('ark1').value		= ark1;
		document.getElementById('ark2').value		= ark2;
		document.getElementById('ark3').value		= ark3;
	}
	if(racetmp == 5){
		document.getElementById('ark1_txt').innerHTML	= '- '+strings[sPerception];
		document.getElementById('ark2_txt').innerHTML	= '- '+strings[sSouffle];
		document.getElementById('ark3a_txt').innerHTML	= '- '+strings[sHorreur];
		document.getElementById('ark1').value		= ark1;
		document.getElementById('ark2').value		= ark2;
		if(ark3 == 1){ document.getElementById('ark3a').checked	= true; }
	}
}

function ensSave(){
	var data	= document.getElementsByTagName("SELECT")[0];
	var ens_1	= data.getElementsByTagName("OPTION")[1].innerHTML;
	for (i=1;i<=10;i++){
		GM_setValue(location.hostname + getNameJoueur() + "ENS_"+i, data.getElementsByTagName("OPTION")[i].innerHTML);
	}
}

function testEns(data){
	var out = data;
	if(GM_getValue(location.hostname + getNameJoueur() + "ENS_"+data) != null){
		out = GM_getValue(location.hostname + getNameJoueur() + "ENS_"+data);
	}
	return out;
}

function raceOptions(){
	var opt = '';
	var racetmp = GM_getValue(location.hostname + getNameJoueur() + "race");
	for(i=1; i<=5; i++){
		if(i==racetmp){
			opt += '<option value="'+i+'" selected>'+strings[i]+'</option>'
		}else{
			opt += '<option value="'+i+'">'+strings[i]+'</option>'
		}
	}
	return opt;
}

function taxiOptions(){
	var opt = '';
	var taxi = GM_getValue(location.hostname + getNameJoueur() + "taxi");
	for(i=1; i<=5; i++){
		if(i==taxi){
			opt += '<option value="'+i+'" selected>'+i+'</option>'
		}else{
			opt += '<option value="'+i+'">'+i+'</option>'
		}
	}
	return opt;
}

function stuffOptions(){
	var opt = '';
	var stufftmp = GM_getValue(location.hostname + getNameJoueur() + "ens");
	for(i=1; i<=10; i++){
		if(i==stufftmp){
			opt += '<option value="'+i+'" selected>'+testEns(i)+'</option>'
		}else{
			opt += '<option value="'+i+'">'+testEns(i)+'</option>'
		}
	}
	return opt;
}

function adversaireTotalOptionsPage(){
	var compteur = 0;
	var dsp = '<tr><td colspan="2"><table><tr>';
	var advOptions = getTab("adversaire_total");
	for(i=0;i<advOptions.length;i++){
		if (compteur==5){
			dsp += '</tr><tr>'
			compteur = 0;
		}

		dsp += '<td style="width: 100px; text-align: right;">'+advOptions[i]+'</td><td><input name="login" type="checkbox" id="'+i+'" value="'+advOptions[i]+'" /></td>';
		compteur++;
	}

	while(compteur<5){
		dsp += '<td></td>';
		compteur++;
	}

	dsp += '</tr>';

	dsp += '</table></td></tr>'
	return dsp;
}

function adversaireOptionsPage(){
	var compteur = 0;
	var dsp = '<tr><td colspan="2"><table><tr>';
	var advOptions = getTab("adversaire");
	for(i=0;i<advOptions.length;i++){
		if (compteur==6){
			dsp += '</tr><tr>'
			compteur = 0;
		}

		dsp += '<td style="width: 100px; text-align: center;">'+advOptions[i]+'</td>';
		compteur++;
	}

	while(compteur<6){
		dsp += '<td></td>';
		compteur++;
	}

	dsp += '</tr>';

	dsp += '</table></td></tr>'
	return dsp;
}

function Options(){
	var doc = document.getElementById("content-mid");
	var html = '';

	html	+='<br /><div id="opt_menu">'
		+ '<form id="botFight" name="botFight"><table style="width: 100%;">'
		+ 	'<tr>'
		+ 		'<td colspan="2" align="center" style="font-size: 16px; font-weight: bold;">Quest bot - Options</td>'
		+ 	'</tr>'

	html	+=	'<tr>'
		+ 		'<td colspan="2" align="center"><div class="hr620"></div></td>'
		+ 	'</tr>';

	html	+=	'<tr>'
		+ 		'<td width="300"><b>'+strings[sChooseRace]+'</b></td>'
		+ 		'<td>'
		+ 			'<select class="combobox i_realm" name="opt_race" id="opt_race">'
		+ 				raceOptions()
		+ 			'</select>'
		+ 		'</td>'
		+ 	'</tr>';

	html	+=	'<tr>'
		+ 		'<td id="ark1_txt"></td>'
		+ 		'<td><input id="ark1" class="inputbox" type="texte" value="" size="3" /></td>'
		+ 	'</tr>'

		+ 	'<tr id="ark2_tr">'
		+ 		'<td id="ark2_txt"></td>'
		+ 		'<td><input id="ark2" class="inputbox" type="texte" value="" size="3" /></td>'
		+ 	'</tr>'

		+ 	'<tr id="ark3_tr" style="visibility: none;">'
		+ 		'<td id="ark3_txt"></td>'
		+ 		'<td><input id="ark3" class="inputbox" type="texte" value="" size="3" /></td>'
		+ 	'</tr>'

		+ 	'<tr id="ark3a_tr" style="visibility: none;">'
		+ 		'<td id="ark3a_txt"></td>'
		+ 		'<td><input id="ark3a" class="inputbox" type="checkbox" value="" /></td>'
		+ 	'</tr>';

	html	+=	'<tr>'
		+ 		'<td colspan="2" align="center"><div class="hr620"></div></td>'
		+ 	'</tr>';

	html	+=	'<tr>'
		+ 		'<td width="250"><b>'+strings[sParamStuff1]+'</b></td>'
		+ 		'<td>'
		+ 			'<select class="combobox i_realm" name="opt_stuff" id="opt_stuff">'
		+ 				stuffOptions()
		+ 			'</select>'
		+ 		'</td>'
		+ 	'</tr>';

	html	+=	'<tr>'
		+ 		'<td width="250"><b>'+strings[sTaxi]+'</b></td>'
		+ 		'<td>'
		+ 			'<select class="combobox i_realm" name="opt_taxi" id="opt_taxi">'
		+ 				taxiOptions()
		+ 			'</select>'
		+ 		'</td>'
		+ 	'</tr>';

	html	+=	'<tr>'
		+ 		'<td colspan="2" align="center"><div class="hr620"></div></td>'
		+ 	'</tr>'
		+ 	'<tr>'
		+ 		'<td colspan="2" align="center" style=""><input class="button" id="opt_go" type="button" value="'+strings[sUpdate]+'" /></td>'
		+ 	'</tr>';

	html	+=	'<tr>'
		+ 		'<td colspan="2" align="center"><div class="hr620"></div></td>'
		+ 	'</tr>';

	html	+=	'<tr><td colspan="2" style="text-align: center; font-size: 14px; font-weight: bold;">'+strings[sFightTotal]+'<br /><br /></td></tr>'
		+ 	adversaireTotalOptionsPage()
		+	'<tr><td colspan="2" align="center"><br /><br /><input class="button" id="opt_add2list" type="button" value="'+strings[sAdd2List]+'" /> <input class="button" id="opt_del2total" type="button" value="'+strings[sDel2Total]+'" /></td></tr>';

	html	+=	'<tr>'
		+ 		'<td colspan="2" align="center"><div class="hr620"></div></td>'
		+ 	'</tr>';

	html	+=	'<tr><td colspan="2" style="text-align: center; font-size: 14px; font-weight: bold;">'+strings[sFightList]+'<br /><br /></td></tr>'
		+ 	adversaireOptionsPage();

	html	+=	'<tr>'
		+ 		'<td colspan="2" align="center"><div class="hr620"></div></td>'
		+ 	'</tr>'
		+ '</table></form>';

	doc.innerHTML = html;
	arcanes();

	//mettre à jour les valeurs
	document.getElementById('opt_go').addEventListener("click",function(event){
		// Set Race
		var opt_race = document.getElementById('opt_race').value;
		GM_setValue(location.hostname + getNameJoueur() + "race", opt_race);

		// vitesse du taxi
		GM_setValue(location.hostname + getNameJoueur() + "taxi", document.getElementById('opt_taxi').value);

		// Set Arcanes
		var ARK1 = document.getElementById('ark1').value;
		var ARK2 = document.getElementById('ark2').value;
		var ARK3 = 0;

		GM_setValue(location.hostname + getNameJoueur() + "ark2", 0);
		GM_setValue(location.hostname + getNameJoueur() + "ark_2", 0);
		GM_setValue(location.hostname + getNameJoueur() + "ark3", 0);
		GM_setValue(location.hostname + getNameJoueur() + "ark_3", 0);

		if (opt_race==3||opt_race==5){
			if(document.getElementById('ark3a').checked){
				ARK3 = 1;
			}
		}else{
			ARK3 = document.getElementById('ark3').value;
		}

		if(opt_race == 1){
			GM_setValue(location.hostname + getNameJoueur() + "ark_1", ARK1);
			GM_setValue(location.hostname + getNameJoueur() + "ark_2", ARK2);
			GM_setValue(location.hostname + getNameJoueur() + "ark_3", ARK3);

			GM_setValue(location.hostname + getNameJoueur() + "ark1", sCharisme);
			GM_setValue(location.hostname + getNameJoueur() + "ark2", sReputation);
			GM_setValue(location.hostname + getNameJoueur() + "ark3", sMajeste);
		}
		if(opt_race == 2){
			GM_setValue(location.hostname + getNameJoueur() + "ark_1", ARK1);
			GM_setValue(location.hostname + getNameJoueur() + "ark_2", ARK2);
			GM_setValue(location.hostname + getNameJoueur() + "ark_3", ARK3);

			GM_setValue(location.hostname + getNameJoueur() + "ark1", sSangVie);
			GM_setValue(location.hostname + getNameJoueur() + "ark2", sAgilite);
			GM_setValue(location.hostname + getNameJoueur() + "ark3", sArdSang);
		}
		if(opt_race == 3){
			GM_setValue(location.hostname + getNameJoueur() + "ark_1", ARK1);
			GM_setValue(location.hostname + getNameJoueur() + "ark_2", ARK2);
			GM_setValue(location.hostname + getNameJoueur() + "ark_3", ARK3);

			GM_setValue(location.hostname + getNameJoueur() + "ark1", sForce);
			GM_setValue(location.hostname + getNameJoueur() + "ark2", sResistance);
			GM_setValue(location.hostname + getNameJoueur() + "ark2", sOmbreBete);
		}
		if(opt_race == 4){
			GM_setValue(location.hostname + getNameJoueur() + "ark_1", ARK1);
			GM_setValue(location.hostname + getNameJoueur() + "ark_2", ARK2);
			GM_setValue(location.hostname + getNameJoueur() + "ark_3", ARK3);

			GM_setValue(location.hostname + getNameJoueur() + "ark1", sSilence);
			GM_setValue(location.hostname + getNameJoueur() + "ark2", sAbsForce);
			GM_setValue(location.hostname + getNameJoueur() + "ark3", sChance);
		}
		if(opt_race == 5){
			GM_setValue(location.hostname + getNameJoueur() + "ark_1", ARK1);
			GM_setValue(location.hostname + getNameJoueur() + "ark_2", ARK2);
			GM_setValue(location.hostname + getNameJoueur() + "ark_3", ARK3);

			GM_setValue(location.hostname + getNameJoueur() + "ark1", sPerception);
			GM_setValue(location.hostname + getNameJoueur() + "ark2", sSouffle);
			GM_setValue(location.hostname + getNameJoueur() + "ark3", sHorreur);
		}

		// Set stuff
		var opt_stuff = document.getElementById('opt_stuff').value;
		GM_setValue(location.hostname + getNameJoueur() + "ens", opt_stuff);

		// Affichage du message de mise à jour
		alert("Mise à jour des options avec succès");
		Options();
	},true);

	document.getElementById('opt_add2list').addEventListener("click",function(event){
		// login des attaquables
		var tailleLogin = document.getElementsByName('login').length;
		var tmp = '';
		var sep = '|*|';
		tmp = GM_getValue(location.hostname + getNameJoueur() + "adversaire");
		GM_setValue(location.hostname + getNameJoueur() + "adversaire", "");
		for (i=0;i<tailleLogin; i++){
			if (document.getElementById(i).checked){
				tmp = GM_getValue(location.hostname + getNameJoueur() + "adversaire");
				GM_setValue(location.hostname + getNameJoueur() + "adversaire", tmp + document.getElementById(i).value + sep);
			}
		}
		Options();
	},true);

	document.getElementById('opt_del2total').addEventListener("click",function(event){
		// login des attaquables
		var tailleLogin = document.getElementsByName('login').length;
		var tmp = '';
		var sep = '|*|';
		tmp = GM_getValue(location.hostname + getNameJoueur() + "adversaire_total");
		GM_setValue(location.hostname + getNameJoueur() + "adversaire_total", "");
		for (i=0;i<tailleLogin; i++){
			if (document.getElementById(i).checked == false){
				tmp = GM_getValue(location.hostname + getNameJoueur() + "adversaire_total");
				GM_setValue(location.hostname + getNameJoueur() + "adversaire_total", tmp + document.getElementById(i).value + sep);
			}
		}
		Options();
	},true);

	// import de la fonction arcanes dans la page
	document.getElementById('opt_race').addEventListener("change",function(arcanes){
		var opt = '';
		var racetmp = document.getElementById('opt_race').value;
		var ark1 = GM_getValue(location.hostname + getNameJoueur() + "ark_1");
		var ark2 = GM_getValue(location.hostname + getNameJoueur() + "ark_2");
		var ark3 = GM_getValue(location.hostname + getNameJoueur() + "ark_3");

		if(racetmp == 1 || racetmp==2 || racetmp==4){
			document.getElementById('ark3_tr').style.visibility = "visible";
		}else{
			document.getElementById('ark3_tr').style.visibility = "hidden";
		}

		if(racetmp == 3 || racetmp==5){
			document.getElementById('ark3a_tr').style.visibility = "visible";
		}else{
			document.getElementById('ark3a_tr').style.visibility = "hidden";
		}

		if(racetmp == 1){
			document.getElementById('ark1_txt').innerHTML	= '- '+strings[sCharisme];
			document.getElementById('ark2_txt').innerHTML	= '- '+strings[sReputation];
			document.getElementById('ark3_txt').innerHTML	= '- '+strings[sMajeste];
			document.getElementById('ark1').value		= ark1;
			document.getElementById('ark2').value		= ark2;
			document.getElementById('ark3').value		= ark3;
		}
		if(racetmp == 2){
			document.getElementById('ark1_txt').innerHTML	= '- '+strings[sSangVie];
			document.getElementById('ark2_txt').innerHTML	= '- '+strings[sAgilite];
			document.getElementById('ark3_txt').innerHTML	= '- '+strings[sArdSang];
			document.getElementById('ark1').value		= ark1;
			document.getElementById('ark2').value		= ark2;
			document.getElementById('ark3').value		= ark3;
		}
		if(racetmp == 3){
			document.getElementById('ark1_txt').innerHTML	= '- '+strings[sForce];
			document.getElementById('ark2_txt').innerHTML	= '- '+strings[sResistance];
			document.getElementById('ark3a_txt').innerHTML	= '- '+strings[sOmbreBete];
			document.getElementById('ark1').value		= ark1;
			document.getElementById('ark2').value		= ark2;
			if(ark3 == 1){ document.getElementById('ark3a').checked	= true; }
		}
		if(racetmp == 4){
			document.getElementById('ark1_txt').innerHTML	= '- '+strings[sSilence];
			document.getElementById('ark2_txt').innerHTML	= '- '+strings[sAbsForce];
			document.getElementById('ark3_txt').innerHTML	= '- '+strings[sChance];
			document.getElementById('ark1').value		= ark1;
			document.getElementById('ark2').value		= ark2;
			document.getElementById('ark3').value		= ark3;
		}
		if(racetmp == 5){
			document.getElementById('ark1_txt').innerHTML	= '- '+strings[sPerception];
			document.getElementById('ark2_txt').innerHTML	= '- '+strings[sSouffle];
			document.getElementById('ark3a_txt').innerHTML	= '- '+strings[sHorreur];
			document.getElementById('ark1').value		= ark1;
			document.getElementById('ark2').value		= ark2;
			if(ark3 == 1){ document.getElementById('ark3a').checked	= true; }
		}
	},true);
}

function dspProfile(){
	var me = getNameJoueur();
	var tab = getTab("adversaire_total");

	NOM	= document.getElementsByClassName("profile-hdr")[0].innerHTML;
	nba	= NOM.indexOf("Profil du vampire ", 0);
	nbb	= NOM.length;
	NOM	= NOM.substring(nba+18, nbb-1);

	if (NOM!= me && estElementDuTab(tab, NOM)==false){
		var FIELDSET	= document.getElementsByTagName("FIELDSET");
		FIELDSET[1].innerHTML = FIELDSET[1].innerHTML + '<a href="#" id="addPlayer" style="color: RED; font-weight: bold;">AJOUTER À LA LISTE D\'ATTAQUE</a>';
		document.getElementById('addPlayer').addEventListener("click",function(event){
			savePlayer(NOM);
		}, true);
	}
}

function savePlayer(NOM){
	var sep = '|*|';
	var tmp = '';
	var tmp2 = '';
	tmp = tmp + GM_getValue(location.hostname + getNameJoueur() + "adversaire");
	tmp2 = tmp2 + GM_getValue(location.hostname + getNameJoueur() + "adversaire_total");
	GM_setValue(location.hostname + getNameJoueur() + "adversaire", tmp + NOM + sep);
	GM_setValue(location.hostname + getNameJoueur() + "adversaire_total", tmp2 + NOM + sep);
	alert(NOM + " " + strings[splayerAdded]);
}

function dspMenuFight(){
	html	= document.getElementsByClassName("logo")[0].innerHTML;
	html	+= '</div><div>';

	html	+='<div id="button_menu_FightBot">'
		+ '<table style="width: 100%;">'
		+ 	'<tr>'
		+ 		'<td align="center">'
		+ 			'<input class="button" type="button" value="'+strings[sStartBot]+'" id="StartFightBot" />'
		+ 			'<input class="button" type="button" value="'+strings[sStopBot]+'" id="StopFightBot" style="margin-left: 7px;" />'
		+ 			'<input class="button" type="button" value="'+strings[sOptionsBot]+'" id="OptionsFightBot" style="margin-left: 7px;" />'
		+ 		'</td>'
		+ 	'</tr>'
		+ '</table>'
		+ '</div>';

	var menuFight = '<li class="menu"><a href="#" class="menulink" id="StartFightBot">'+strings[sStartBot]+'</a></li>'
		      + '<li class="menu"><a href="#" class="menulink" id="StopFightBot">'+strings[sStopBot]+'</a></li>'
		      + '<li class="menu"><a href="#" class="menulink" id="OptionsFightBot">'+strings[sOptionsBot]+'</a></li>'
		      + '<li class="menu"><img src="http://r1.fr.bloodwars.net/gfx/liniamenu.gif" alt="---" /></li>';

	var MENU	= document.getElementsByClassName("menu")[1];
	MENU.innerHTML	= menuFight + MENU.innerHTML;

// 	document.getElementsByClassName("logo")[0].innerHTML = html;

	// Actions
	document.getElementById('StartFightBot').addEventListener("click",function(event){
		Demarrer();
	}, true);

	document.getElementById('StopFightBot').addEventListener("click",function(event){
		Arreter();
	}, true);

	document.getElementById('OptionsFightBot').addEventListener("click",function(event){
		Options();
	}, true);
}

function whereIam(){
	if (document.getElementsByClassName("profile-hdr")[0] != undefined) { dspProfile(); }

	if(document.getElementsByClassName("eqhdr")[0]!=undefined){
		// enregistrement des pré-équipements
		ensSave();
	}

	if (document.getElementsByClassName("remark")[0] != undefined){
		dspMenuFight();
	}
}

// ------------------------------------------------------------------------------------------------
// ======================================== Commandes =============================================
// ------------------------------------------------------------------------------------------------

GM_registerMenuCommand("StartFightBot", Demarrer);
GM_registerMenuCommand("StopFightBot", Arreter);
GM_registerMenuCommand("FightBot Options", Options);

// ------------------------------------------------------------------------------------------------
// ======================================== MAIN ==================================================
// ------------------------------------------------------------------------------------------------

if(GM_getValue(location.hostname + getNameJoueur() + "adversaire")!=""){
	adversaire = getTab("adversaire");
}else{
	stopFightBOT()
	adversaire = '';
}

if(isFightBOTRunning()){
	runAttackBot();
}

whereIam();
