// ==UserScript==
// @name				KB-Hosting
// @author				dob (http://userscripts.org/users/33073); Mikado (http://userscripts.org/users/31647); JoeSimmons (http://userscripts.org/users/JoeSimmons); Jarett (http://userscripts.org/users/38602); Tarja The Witch (http://userscripts.org/users/110724);
// @author				Un1matr1x 	http://kb.un1matr1x.de
// @date				2010-01-22
// @version				0.3.01
// @Development Stages	Release
// @description			KB-Hosting Script zum Speichern von Kampfberichten auf kb.un1mtar1x.de
// @namespace			KB-Hosting
// @include				http://*.ogame.*/game/index.php?page=bericht&*&bericht=*
// @include				http://*.ogame.*/game/index.php?page=combatreport*
// @include				http://*/br.php?rid=*
// @include				http://*/rw.php?raport=*

// ==/UserScript==

//======================Changelog======================\\
//--v0.3.01 by Un1matr1x--------------------[Release]--\\
// + spanish translation improved [thx to Mr. Méndez]  \\
//--v0.3.00 by Un1matr1x--------------------[Release]--\\
// + russian translation added [thx Tarja The Witch]   \\
// ! fix for open stored CombatReports @KB-Hosting 3.0 \\
//--v0.2.01 by Un1matr1x--------------------[Release]--\\
// + poor translations for Updatecheck added		   \\
// ~ include-change to work with new ogame-naming	   \\
//--v0.2.00 by Un1matr1x--------------------[Release]--\\
// + Script Update Checker from Jarett added		   \\
//--v0.1.00 by JoeSimmons-------------------[Release]--\\
// ~ code optimization (language switch)			   \\
// ? approval by oGameTech-Team	for oGame			   \\
//--v0.0.10 by Un1matr1x-----------------------[RC03]--\\
// ! fix of the language optimization				   \\
//--v0.0.09 by JoeSimmons----------------------[RC02]--\\
// ~ code optimization (language selection)			   \\
//--v0.0.08 by Un1matr1x-----------------------[RC01]--\\
// + Adding multiple languages						   \\
//--v0.0.07 by JoeSimmons----------------------[Beta]--\\
// + removing Button itself from transferred sc 	   \\
//--v0.0.06 by Un1matr1x-----------------------[Beta]--\\
// ! fix of the optimized "open in a new tab"		   \\
//--v0.0.05 by JoeSimmons----------------------[Beta]--\\
// ~ code optimization (open in a new Tab)			   \\
// + checkboxes for BR-Options (exclusiv skin)		   \\
//--v0.0.04 by Un1matr1x-----------------------[Beta]--\\
// + Name & Alliance selectable						   \\
//--v0.0.03 by Mikado--------------------------[Beta]--\\
// *http://userscripts.org/topics/28748#posts-132699*  \\
// + BR-Title selectable							   \\
// + open saved BR in a new Tab						   \\
//--v0.0.02 by Mikado-------------------------[Alpha]--\\
// + transmitting Username,... additional to the	   \\
//   sourcecode of the BattleRepport				   \\
//--v0.0.01 by dob-------------------------[preAlpha]--\\
// *http://userscripts.org/topics/28748#posts-132659*  \\
// + first version							  		   \\
//=====================================================\\

//====================Sprachauswahl====================\\
var GameURL=location.href;
//==============Standartsprache festlegen==============\\
var Sprache='de';
//====================Mögliche tld-s===================\\
const tld = new Array('de','org','us','se','com.es','ro','cz','fr','it','onet.pl','com.tr','info','at','ru');

for(var i=0 ; i<tld.length ; i++) {
	if ((GameURL.indexOf('ogame.'+tld[i],0))>=0) Sprache = tld[i];
	if ((GameURL.indexOf('durks.'+tld[i],0))>=0) Sprache = tld[i];
	if ((GameURL.indexOf('nonamegame.'+tld[i],0))>=0) Sprache = tld[i];
	if ((GameURL.indexOf('speed-game.'+tld[i],0))>=0) Sprache = tld[i];
}

//=================Spreache überprüfen=================\\
//alert(Sprache)
switch(Sprache) {
case 'org': Sprache='us'; break;
case 'info': Sprache='de'; break;
case 'at': Sprache='de'; break;
case 'com.es': Sprache='es'; break;
case 'onet.pl': Sprache='pl'; break;
case 'com.tr': Sprache='tr'; break;
}


var bez = {
//=======================Deutsch=======================\\
de: {
button : 'KB Speichern',
name : 'Name:',
allianz : 'Allianz:',
titel : 'Titel:',
privat : 'Privater KB\n\n[OK] für Ja\n[Abbrechen] für Nein',
koord : 'Koordinaten\n\n[OK] = Anzeigen\n[Abbrechen]= Verstecken',
tech : 'Techniklevel\n\n[OK] = Anzeigen\n[Abbrechen]= Verstecken',
time : 'Kampfberichtszeit\n\n[OK] = Anzeigen\n[Abbrechen]= Verstecken',
update_avalible : 'Es ist ein Update für das Greasemonkey-Skript zum Speichern von KBs vorhanden.\nMöchtest du auf die Installationsseite?',
update_unavalible : 'Das Skript ist auf dem neuesten Stand.',
update_error : 'in Fehler ist aufgetreten, während der Suche nach Updates:\n'
},
//=======================Englisch======================\\
us: {
button : 'Store CR',
name : 'Name:',
allianz : 'Alliance:',
titel : 'Title:',
privat : 'privat CR\n\n[OK] for Yes\n[Cancel] for No',
koord : 'Coordinates\n\n[OK] = Show\n[Cancel]= Hide',
tech : 'Technologylevel\n\n[OK] = Show\n[Cancel]= Hide',
time : 'CombatReportTime\n\n[OK] = Show\n[Cancel]= Hide',
update_avalible : 'There is an update available for the Greasemonkey script to store CombatReports\nWould you like to go to the install page now?',
update_unavalible : 'Your script is up to date.',
update_error : 'An error occurred while checking for updates:\n'
},
//=======================Spanisch======================\\
es: {
button : 'Guardar RdB',
name : 'Nombre:',
allianz : 'Alianza:',
titel : 'Título:',
privat : 'privado RdB\n\n[Aceptar] para Si\n[Cancelar] para No',
koord : 'Coordenadas\n\n[Aceptar] = Visualizar\n[cancelar] = ocultar',
tech : 'nivel tecnico\n\n[Aceptar] = Visualizar\n[cancelar] = ocultar',
time : 'tiempo del Reporte de Batalla\n\n[Aceptar] = Visualizar\n[cancelar] = ocultar',
update_avalible : 'Hay una actualización para guardar el script de Greasemonkey BdRs disponible.  Te gustaría ir ahora a la página de instalar?',
update_unavalible : 'Tu script esta a nivel actual.',
update_error : 'Se produjo un error al comprobar las actualizaciones:\n'
},
//======================Französisch====================\\
fr: {
button : 'Sauvegarder RdC',
name : 'Nom:',
allianz : 'Alliance:',
titel : 'Titre:',
privat : 'RdC privé\n\n[OK] pour Oui\n[Annuler] pour Non',
koord : 'Coordonnées\n\n[OK] = Puesto\n[Annuler]= Cacher',
tech : 'Level de technologie\n\n[OK] = Puesto\n[Annuler]= Cacher',
time : 'Heure du Rapport de Combat\n\n[OK] = Puesto\n[Annuler]= Cacher',
update_avalible : 'Il est une mise à jour disponible pour le script Greasemonkey pour sauvegarder RdC\nVoulez-vous aller à la page installer maintenant?',
update_unavalible : 'Votre script est à jour.',
update_error : 'Une erreur s\'est produite lors de la vérification de mises à jour:\n'
},
//======================Italienisch====================\\
it: {
button : 'Salva CR',
name : 'Nome:',
allianz : 'Alleanza:',
titel : 'Titolo:',
privat : 'CR privato\n\n[OK] per Si\n[Annulla] per No',
koord : 'Coordinate\n\n[OK] = Visualizza\n[Annulla]= Nascondi',
tech : 'Tecnologie\n\n[OK] = Visualizza\n[Annulla]= Nascondi',
time : 'Orario del Combat Report\n\n[OK] = Visualizza\n[Annulla]= Nascondi',
update_avalible : 'Non è disponibile un aggiornamento per il Greasemonkey script per salva CR\nVuoi andare alla pagina di installazione adesso?',
update_unavalible : 'Il tuo script è fino ad oggi.',
update_error : 'Si è verificato un errore durante il controllo per gli aggiornamenti:\n'
},
//=======================Polnisch======================\\
pl: {
button : 'Zapisz RW',
name : 'Nazwa:',
allianz : 'Sojusz:',
titel : 'Tytuł:',
privat : 'prywatny RW\n\n[OK] dla Tak\n[Zrezygnuj] dla Nie',
koord : 'Koordynaty\n\n[OK] = Pokaż\n[Zrezygnuj]= Ukryj',
tech : 'Poziom technologii\n\n[OK] = Pokaż\n[Zrezygnuj]= Ukryj',
time : 'CzasRaportuWojennego\n\n[OK] = Pokaż\n[Zrezygnuj]= Ukryj',
update_avalible : 'Istnieje aktualizacji dostępnych dla Greasemonkey skrypt do zapisz RW\nCzy chcesz przejść do strony zainstalować teraz?',
update_unavalible : 'Twój skrypt jest aktualne.',
update_error : 'Wystąpił błąd podczas sprawdzania dostępności aktualizacji:\n'
},
//=======================Türkisch======================\\
tr: {
button : 'Kaydet SR',
name : 'Isim:',
allianz : 'Ittifak:',
titel : 'Baslik:',
privat : 'Ozel SR\n\n[Tamam] için Evet\n[İptal] için Hayır',
koord : 'Koordinatlar\n\n[Tamam] = Goster\n[İptal]= Gizle',
tech : 'Teknoloji Seviyeleri\n\n[Tamam] = Goster\n[İptal]= Gizle',
time : 'Savas Raporu Zaman\n\n[Tamam] = Goster\n[İptal]= Gizle',
update_avalible : 'Bir güncelleştirme Greasemonkey betiği için kullanılabilir kaydet SR\nSize şimdi sayfası gitmek ister misiniz?',
update_unavalible : 'Script güncel olduğunu.',
update_error : 'Bir hata oluştu güncellemeleri kontrol:\n'
},
//======================Schwedisch=====================\\
se: {
button : 'Spara KR',
name : 'Namn:',
allianz : 'Allians:',
titel : 'Titel:',
privat : 'privat KR\n\n[OK] för Ja\n[Avbryt] för Nej',
koord : 'Koordinater\n\n[OK] = Visa\n[Avbryt]= Gömma',
tech : 'Technologynivå\n\n[OK] = Visa\n[Avbryt]= Gömma',
time : 'KampRapportsTid\n\n[OK] = Visa\n[Avbryt]= Gömma',
update_avalible : 'Det finns en uppdatering tillgänglig för Greasemonkey script för att spara KR\nVill du gå till installera sidan nu?',
update_unavalible : 'Ditt script är aktuell.',
update_error : 'Ett fel uppstod vid att söka efter uppdateringar:\n'
},
//======================Teschisch======================\\
cz: {
button : 'Uložit BZ',
name : 'Jméno:',
allianz : 'Aliance:',
titel : 'Titulek:',
privat : 'soukromé BZ\n\n[OK] pro Ano\n[Zrušit] pro Ne',
koord : 'Souøadnice\n\n[OK] = Ukázat\n[Zrušit]= Skrýt',
tech : 'Technologický level\n\n[OK] = Ukázat\n[Zrušit]= Skrýt',
time : 'Èas bitevní zprávy\n\n[OK] = Ukázat\n[Zrušit]= Skrýt',
update_avalible : 'K dispozici je aktualizace k dispozici pro Greasemonkey skript pro uložit BZ\nChtěli byste jít na stránku nainstalovat nyní?',
update_unavalible : 'Váš skript je aktuální.',
update_error : 'Došlo k chybě při kontrole aktualizace:\n'
},
//======================Rumänisch======================\\
ro: {
button : 'Salvează RdL',
name : 'Nume:',
allianz : 'Alianta:',
titel : 'Titlu:',
privat : 'RdL privat\n\n[OK] pentru Da\n[Anulare] pentru Nu',
koord : 'Coordinate\n\n[OK] = Arata\n[Anulare]= Ascunde',
tech : 'Nivelul de tehnologie\n\n[OK] = Arata\n[Anulare]= Ascunde',
time : 'Data raportului de lupta\n\n[OK] = Arata\n[Anulare]= Ascunde',
update_avalible : 'Nu există o actualizare disponibilă pentru Greasemonkey script-ul pentru a salvează RdL\nDoriţi să mergeţi la pagina de instala acum?',
update_unavalible : 'Script-ul dvs. este până la data de.',
update_error : 'A apărut o eroare la verificarea de actualizări:\n'
},
//=======================Russian======================\
ru: {
button : 'Сохранить доклад',
name : 'Имя:',
allianz : 'Альянс:',
titel : 'Название:',
privat : 'Приватный доклад\n\n[ОК] = Да\n[Отмена] = Нет',
koord : 'Координаты\n\n[ОК] = Показать\n[Отмена]= Скрыть',
tech : 'Технологии\n\n[ОК] = Показать\n[Отмена]= Скрыть',
time : 'Дата/Время\n\n[ОК] = Показать\n[Отмена]= Скрыть',
update_avalible : 'Доступно обновление для скрипта, сохраняющего боевые доклады.\nПерейти на страницу установки?',
update_unavalible : 'Новых версий не найдено.',
update_error : 'Ошибка во время проверки обновлений:\n'
}
};

//===================AutoUpdate-Check==================\\

var SUC_script_num = 51558; // Change this to the number given to the script by userscripts.org (check the address bar)
try
{
	function updateCheck(forced)
	{
		if ((forced) || (parseInt(GM_getValue('SUC_last_update', '0')) + 86400000 <= (new Date().getTime()))) // Checks once a day (24 h * 60 m * 60 s * 1000 ms)
		{
			try
			{
				GM_xmlhttpRequest(
				{
					method: 'GET',
					url: 'http://userscripts.org/scripts/source/'+SUC_script_num+'.meta.js?'+new Date().getTime(),
					headers: {'Cache-Control': 'no-cache'},
					onload: function(resp)
					{
						var local_version, remote_version, rt, script_name;
						
						rt=resp.responseText;
						GM_setValue('SUC_last_update', new Date().getTime()+'');
						remote_version=parseInt(/@uso:version\s*(.*?)\s*$/m.exec(rt)[1]);
						local_version=parseInt(GM_getValue('SUC_current_version', '-1'));
						if(local_version!=-1)
						{
							script_name = (/@name\s*(.*?)\s*$/m.exec(rt))[1];
							GM_setValue('SUC_target_script_name', script_name);
							if (remote_version > local_version)
							{
								if(confirm(bez[Sprache].update_avalible))
								{
									GM_openInTab('http://kb.un1matr1x.de/viewforum.php?f=3');
									GM_setValue('SUC_current_version', remote_version);
								}
							}
							else if (forced)
								alert(bez[Sprache].update_unavalible);
						}
						else
							GM_setValue('SUC_current_version', remote_version+'');
					}
				});
			}
			catch (err)
			{
				if (forced)
					alert(bez[Sprache].update_error+err);
			}
		}
	}
	GM_registerMenuCommand('Manual Update Check 4 KB-Hosting', function()
	{
		updateCheck(true);
	});
	updateCheck(false);
}
catch(err)
{}


//=================KB Speicher Button==================\\
var link = document.createElement("a");
    link.setAttribute('style', 'position: fixed; top: 2px; right: 2px; padding: 4px; text-decoration:none; color:#f1f1f1;font: 12px Verdana,Arial,SunSans-Regular,Sans-Serif;text-align:center; background:url("http://kb.un1matr1x.de/images/icons/hauptnavi_a.gif") no-repeat;height:29px;width:135px;');
    link.setAttribute('href', 'javascript:void(0);');
    link.textContent = bez[Sprache].button;
    link.addEventListener("click", function(e) {
//===========Button beim Übertragen entfernen==========\\
			e.currentTarget.parentNode.removeChild(e.currentTarget);
        GM_xmlhttpRequest({
            url: "http://kb.un1matr1x.de/save.php?lang=de",
            method: "post",
            headers: {
                 "Content-Type": "application/x-www-form-urlencoded"
            },
//=======Daten für die Übertragung, incl. Auswahl======\\
			data: "username=" + encodeURIComponent(prompt(bez[Sprache].name) || 'ScriptUser') + "&userally=" + encodeURIComponent(prompt(bez[Sprache].allianz) || '') + "&titel=" + encodeURIComponent(prompt(bez[Sprache].titel) || '') + "&privat_kb=" + (confirm(bez[Sprache].privat)?'true':'false') + "&time=" + (confirm(bez[Sprache].time)?'true':'false') + "&koord=" + (confirm(bez[Sprache].koord)?'true':'false') + "&tech=" + (confirm(bez[Sprache].tech)?'true':'false') + "&style=0&kb=" + encodeURIComponent(document.body.innerHTML),
onload: function(e) {
//============KB nach dem Speichern öffnen=============\\
			GM_openInTab(e.responseText.match(<span class="syntaxdefault">(http:\/\/kb.un1matr1x.de/\kb.php\?show=[/\S/]+)</span>)[1]);
			}
        });
      }, false);
document.body.appendChild(link);