// ==UserScript==
// @name           CS2 Nicer Ship Pages 2
// @namespace   	http://black-beach.net
// @version     	0.1j 
// @description    nicens up the scan & review pages for ships & outposts reviews & submits to Black-Beach
// @include        http://*.chosenspace.com/index.php?go=design_info*
// @include        http://*.chosenspace.com/index.php?go=scan*
// @include		   http://*.chosenspace.com/index.php?go=ship_info&ship_id=*
// @exclude        http://*.chosenspace.com/index.php?go=scan_*
// @exclude        http://*.chosenspace.com/*/*
// ==/UserScript==

// version 0.1i - Uploads data without iframes
// version 0.1j - Uses local storage

var all2tags,this2tag,newContent;
all2tags=document.evaluate("//div[@style='position:relative; width:423px; height:15px;']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
this2tag=all2tags.snapshotItem(0);
if(this2tag){
	var id=document.location.href.split('&ship_id=')[1];
	id=id*1;
	newbutton=document.createElement("input");
	newbutton.setAttribute('type','button');
	newbutton.setAttribute('class','forms_btn');
	newbutton.setAttribute('value',"Previous");
	newbutton.setAttribute('style','float:left;');
	newbutton.setAttribute('onclick',"location.href='http://g1.chosenspace.com/index.php?go=ship_info&ship_id="+(id-1)+"'");
	this2tag.appendChild(newbutton);
	newbutton=document.createElement("input");
	newbutton.setAttribute('type','button');
	newbutton.setAttribute('class','forms_btn');
	newbutton.setAttribute('value',"Next");
	newbutton.setAttribute('style','float:right;');
	newbutton.setAttribute('onclick',"location.href='http://g1.chosenspace.com/index.php?go=ship_info&ship_id="+(id+1)+"'");
	this2tag.appendChild(newbutton);
} else {
all2tags=document.evaluate("//div[@style='position: relative; width: 423px; height: 15px;']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
this2tag=all2tags.snapshotItem(0);
if(this2tag){
	var id=document.location.href.split('&ship_id=')[1];
	id=id*1;
	newbutton=document.createElement("input");
	newbutton.setAttribute('type','button');
	newbutton.setAttribute('class','forms_btn');
	newbutton.setAttribute('value',"Previous");
	newbutton.setAttribute('style','float:left;');
	newbutton.setAttribute('onclick',"location.href='http://g1.chosenspace.com/index.php?go=ship_info&ship_id="+(id-1)+"'");
	this2tag.appendChild(newbutton);
	newbutton=document.createElement("input");
	newbutton.setAttribute('type','button');
	newbutton.setAttribute('class','forms_btn');
	newbutton.setAttribute('value',"Next");
	newbutton.setAttribute('style','float:right;');
	newbutton.setAttribute('onclick',"location.href='http://g1.chosenspace.com/index.php?go=ship_info&ship_id="+(id+1)+"'");
	this2tag.appendChild(newbutton);
}
}

function checkTime(i) {if(i<10){i="0"+i;};return i;}
var nd = new Date();
						d=checkTime(nd.getUTCDate());
						m=checkTime(nd.getUTCMonth()+1);
						y=nd.getUTCFullYear();
						h=checkTime(nd.getUTCHours());
						n=checkTime(nd.getUTCMinutes());
						s=checkTime(nd.getUTCSeconds());
						ms=checkTime(nd.getUTCMilliseconds());
						rtime=y+'/'+m+'/'+d+' '+h+':'+n+':'+s+':'+ms;
						uid=y+m+d+h+n+s+ms;

cetags = document.evaluate("//td [contains (., 'Year')]  ", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
cetime=cetags.snapshotLength;
for(var i=0;i<cetime;i++){
	cetime=cetags.snapshotItem(i);
	var nocetime= cetime.textContent.split(' CE')[0].split('Year ')[1];
	cetime = cetime.textContent
	parseFloat(nocetime);
}

uploader = localStorage.getItem("cpt");

var alltags,thistag,newContent;
alltags=document.evaluate("//input[@value='Sector']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
thistag=alltags.snapshotItem(0);
if(thistag){
	var getsys=thistag.getAttribute('onclick');
	var systemno=getsys.split("system_id=")[1].split("&")[0];
	var sector=getsys.split("sector_id=")[1].split("&")[0];
	var cgrid=getsys.split("grid_id=")[1].split("'")[0];
}
var systemNumber=["133",   "147",   "148",    "149",    "150",   "151",    "152",    "153",  "168",  "169",    "170",    "171",    "172",  "173",    "188",    "189",  "190",  "191",     "192",  "193",     "208",  "209",     "210",     "211",  "212",     "213",  "228",  "229",    "230",  "231",   "232",   "233",   "248",   "249",   "250",   "251",   "252",   "253",   "254",   "268"];
var systemName=  ["Arcas Expanse","Asterion Expanse","Regulus Expanse","Hathor Expanse","Nespian System","Newhope Expanse","Polaris Expanse","Basian System","Tyrian System","Memorial Expanse","Midway Expanse","Perennis Expanse","Raxian System","Ursa Expanse","Iota Expanse","Altian System","Solian System","Atra Expanse","Jexian System","Antares Expanse","Zarian System","Allansia Expanse","Veranza Expanse","Casian System","Wartorn Expanse","Farian System","Lyrian System","Pulsar Expanse","Genian System","Exile Expanse","Volian System","Imperial Expanse","Meridian Expanse","Omnian System","Darkened Expanse","Adarian System","Cephalus Expanse","Desian System","Heavenly Expanse","Utopian Expanse"];
for(i=0;i < systemNumber.length;i++){
	if(systemNumber[i]==systemno)
		{
		system=systemName[i];
		break;
		}}
		var loc = system +"-"+ sector +"-"+ cgrid;

/* Start Minefield numbers grabbing */
var vht=0,vat=0,vst=0,vpt=0;
minetags=document.evaluate("//td[substring(., string-length() -8) = 'Deployed:']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
minelen=minetags.snapshotLength;
if (minelen>0){
for(var i=0;i<minelen;i++){
var vh = minetags.snapshotItem(i).nextSibling.textContent;
var vh = vh.replace(/[^\d\.\-\ ]/g, '');
vht = vht + vh*1;
var va = minetags.snapshotItem(i).nextSibling.nextSibling.textContent;
var va = va.replace(/[^\d\.\-\ ]/g, '');
vat = vat + va*1;
var vs = minetags.snapshotItem(i).nextSibling.nextSibling.nextSibling.textContent;
var vs = vs.replace(/[^\d\.\-\ ]/g, '');
vst = vst + vs*1;
var vp = minetags.snapshotItem(i).nextSibling.nextSibling.nextSibling.nextSibling.textContent;
var vp = vp.replace(/[^\d\.\-\ ]/g, '');
vpt = vpt + vp*1;
}
density=vht+vat+vst+vpt;
	var mLink=uid + "&cetime=" + nocetime + " CE&density=" + density + "&vht=" + vht + 
	"&vat=" + vat + "&vst=" + vst + "&vpt=" + vpt +	"&loc=" + loc + "&uploader=" + uploader;
//alert(mLink);

       GM_xmlhttpRequest({
                method: "GET",
                url: "http://www.black-beach.net/db/mfimport.php?uid=" + mLink,
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded"
          },
});

}
totalominetags=document.evaluate("//a[@href[starts-with(.,'index.php?go=item_info&item_id=16')]]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
totalominelen=totalominetags.snapshotLength;
if (totalominelen>0){
ominetags=document.evaluate("//a[@href[starts-with(.,'index.php?go=item_info&item_id=163')]]/following::td", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
ominelen = ominetags.snapshotLength;
if(ominelen>0){
ovht = ominetags.snapshotItem(1).textContent.split('X ')[1];
var ovht = (ovht.replace(/[^\d\.\-\ ]/g, ''))*1;
}else{ovht=0};
ominetags=document.evaluate("//a[@href[starts-with(.,'index.php?go=item_info&item_id=164')]]/following::td", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
ominelen = ominetags.snapshotLength;
if(ominelen>0){
ovat = ominetags.snapshotItem(1).textContent.split('X ')[1];
var ovat = (ovat.replace(/[^\d\.\-\ ]/g, ''))*1;
}else{ovat=0};
ominetags=document.evaluate("//a[@href[starts-with(.,'index.php?go=item_info&item_id=165')]]/following::td", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
ominelen = ominetags.snapshotLength;
if(ominelen>0){
ovst = ominetags.snapshotItem(1).textContent.split('X ')[1];
var ovst = (ovst.replace(/[^\d\.\-\ ]/g, ''))*1;
}else{ovst=0};
ominetags=document.evaluate("//a[@href[starts-with(.,'index.php?go=item_info&item_id=166')]]/following::td", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
ominelen = ominetags.snapshotLength;
if(ominelen>0){
ovpt = ominetags.snapshotItem(1).textContent.split('X ')[1];
var ovpt = (ovpt.replace(/[^\d\.\-\ ]/g, ''))*1;
}else{ovpt=0};
density=ovht+ovat+ovst+ovpt;
	if (mLink==undefined){
	var mLink= uid + "&cetime=" + nocetime + " CE&density=" + density + "&vht=" + ovht + 
	"&vat=" + ovat + "&vst=" + ovst + "&vpt=" + ovpt +	"&loc=" + loc + "&uploader=" + uploader;
//alert(mLink);
       GM_xmlhttpRequest({
                method: "GET",
                url: "http://www.black-beach.net/db/mfimport.php?uid=" + mLink,
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded"
          },
});
		}
}
/* End Minefield numbers grabbing */

function checkTime(i) {if(i<10){i="0"+i;};return i;}
var nd = new Date();
						d=checkTime(nd.getDate());
						m=checkTime(nd.getMonth()+1);
						y=nd.getFullYear();
						h=checkTime(nd.getHours());
						n=checkTime(nd.getMinutes());
						GMTd=d+"/"+m+"/"+y;
						
var shipid = location.href.split('ship_id=')[1];

if(location.href.split('design')[1]==null){
	var Design=document.evaluate("//input[@value='E']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
	var MyDesigns=Design.snapshotItem(0);
	if(MyDesigns == null) {
		var ter={"ExtraOfficer": "", "ExtraCrew": "", "Points": "", "DockingBay": "", "Created": "",
			"Design": "", "Hull": "", "Isaton": "", "Size": "", "Class": "", "Reactor": "",
			"Engine": "", "JumpDrive": "", "Navigation": "", "Scanner": "", "Targeting": "",
			"ShieldGen": "", "Weapon1": "", "Wamt1": "", "Weapon2": "", "Wamt2": "", "Armor": "",
			"CargoHold": "", "MiningBay": "", "ConstructionBay": "", "RepairBay": "", "PassengerQuarters": "",
			"Output": "", "LightSpeed": "", "HyperJump": "", "Damage": "", "Captain": "",
			"Officer": "", "Crew": "", "OfficerT": "", "CrewT": "", "OfficerG": "", "CrewG": "", "FreeSpace": "",
			"Status": ""
		}
		var terext = new Array();
		var terextamt = new Array();
		var reduce;
		var neutral=0;
		externals=document.evaluate("//td[contains(.,'Installed Externals')] ", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
		extgrab=externals.snapshotItem(0);
		if(extgrab){
		ext=extgrab.textContent.split('Installed Externals' + '\n')[1].split('\n' + '\n' + 'Attacking')[0].split('\n');
		for(i = 0; i < ext.length; i++){
		terext[i]=ext[i].split(' x')[0];
		terextamt[i]=ext[i].split('x ')[1];
		if (terext[i] == 'Ion Neutralizer') {var neutral = terextamt[i]*1}
		switch(neutral){
		case 0:break;
		case 1:reduce=0.498;break;
		case 2:reduce=0.3293;break;
		case 3:reduce=0.244;break;
		case 4:reduce=0.192;break;
		case 5:reduce=0.1567;break;
		case 6:reduce=0.1309;break;
		case 7:reduce=0.111;break;
		case 8:reduce=0.0951;break;
		case 9:reduce=0.082;break;
		case 10:reduce=0.0709;break;
		case 11:reduce=0.0613;break;
		case 12:reduce=0.0529;break;
		case 13:reduce=0.0454;break;
		case 14:reduce=0.0387;break;
		case 15:reduce=0.0325;break;
		case 16:reduce=0.0268;break;
		case 17:reduce=0.0216;break;
		case 18:reduce=0.0166;break;}
		}};
		
		if (terext[0]==undefined) {terext[0]="";}
		if (terext[1]==undefined) {terext[1]="";}
		if (terext[2]==undefined) {terext[2]="";}
		if (terext[3]==undefined) {terext[3]="";}
		if (terext[4]==undefined) {terext[4]="";}
		if (terextamt[0]==undefined) {terextamt[0]="";}
		if (terextamt[1]==undefined) {terextamt[1]="";}
		if (terextamt[2]==undefined) {terextamt[2]="";}
		if (terextamt[3]==undefined) {terextamt[3]="";}
		if (terextamt[4]==undefined) {terextamt[4]="";}
		
		var statuseval=document.evaluate("//body/table/tbody/tr/td/table/tbody/tr/td/div[@class='grey']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
		var statusgrab=statuseval.snapshotItem(0);
		if(statusgrab){
			ter.Status = statusgrab.textContent.split(" and protected")[0].split("Ship is ")[1];}
			else {ter.Status="In Space";}
		
		allEdits=document.evaluate("//input[@onfocus='blur();']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
		for(var i=0;i < allEdits.snapshotLength;i++){thisEdit=allEdits.snapshotItem(i);
			if(thisEdit.previousSibling.textContent==' : '){desc=thisEdit.previousSibling.previousSibling.previousSibling.textContent+':';}
			else{desc=thisEdit.previousSibling.textContent;}
			desc=desc.split('\n').join('').split(' ').join('');
			MWSW: switch(desc){
				case'ShipName:':ter.ShipName=thisEdit.getAttribute('value');break MWSW;
				case'Class:':ter.Class=thisEdit.getAttribute('value');break MWSW;
				case'Design:':case'DesignName:':ter.Design=thisEdit.getAttribute('value');break MWSW;
				case'Hull:':ter.Hull=thisEdit.getAttribute('value');break MWSW;
				case'Size:':case'Isatons:':ter.Isaton=thisEdit.getAttribute('value').split(',').join('').split(' Isatons').join('');break MWSW;
				case'Output:':ter.Output=thisEdit.getAttribute('value').split(',').join('');if (neutral>0) {thisEdit.setAttribute('value',ter.Output +" or "+ Math.ceil(ter.Output*reduce))}; break MWSW;
				case'Condition:':ter.Condition=thisEdit.getAttribute('value');break MWSW;
				case'Reactor:':ter.Reactor=thisEdit.getAttribute('value');break MWSW;
				case'Engine:':ter.Engine=thisEdit.getAttribute('value');break MWSW;
				case'JumpDrive:':ter.JumpDrive=thisEdit.getAttribute('value');break MWSW;
				case'Navigation:':ter.Navigation=thisEdit.getAttribute('value');break MWSW;
				case'Scanner:':ter.Scanner=thisEdit.getAttribute('value');break MWSW;
				case'Targeting:':ter.Targeting=thisEdit.getAttribute('value');if(ter.Targeting=='Targeting') ter.Targeting='No Targeting';break MWSW;
				case'ShieldGen:':ter.ShieldGen=thisEdit.getAttribute('value');break MWSW;
				case'CargoHoldX':case'CargoStoreX':ter.CargoHold=thisEdit.getAttribute('value');break MWSW;
				case'MiningBay:':ter.MiningBay=thisEdit.getAttribute('value');break MWSW;
				case'RepairBay:':ter.RepairBay=thisEdit.getAttribute('value');break MWSW;
				case'ConstructionBay:':ter.ConstructionBay=thisEdit.getAttribute('value');break MWSW;
				case'PrimaryWeapon:':ter.Weapon1=thisEdit.getAttribute('value');
					if(ter.Weapon1.substring(0,2)!='No'){i++;thisEdit=allEdits.snapshotItem(i);ter.Wamt1=thisEdit.getAttribute('value');}break MWSW;
				case'SecondaryWeapon:':ter.Weapon2=thisEdit.getAttribute('value');
					if(ter.Weapon2.substring(0,2)!='No'){i++;thisEdit=allEdits.snapshotItem(i);ter.Wamt2=thisEdit.getAttribute('value');}break MWSW;
				case'Armor:':if(thisEdit.getAttribute('value').substring(0,2)=='Q.'||thisEdit.getAttribute('value').substring(0,2)=='No') ter.Armor=thisEdit.getAttribute('value');break MWSW;
				case'Created:':ter.Created=thisEdit.getAttribute('value');break MWSW;
				case'PassengerQuartersX':ter.PassengerQuarters=thisEdit.getAttribute('value');break MWSW;
				case'ExtraOfficersQuartersX':ter.ExtraOfficer=thisEdit.getAttribute('value');break MWSW;
				case'ExtraCrewQuartersX':ter.ExtraCrew=thisEdit.getAttribute('value');break MWSW;
				case'Points:':ter.Points=thisEdit.getAttribute('value');break MWSW;
				case'LightSpeed:':ter.LightSpeed=thisEdit.getAttribute('value');break MWSW;
				case'HyperJump:':ter.HyperJump=thisEdit.getAttribute('value');break MWSW;
				case'Damage:':ter.Damage=thisEdit.getAttribute('value');break MWSW;
				case'Captain:':ter.Captain=thisEdit.getAttribute('value');break MWSW;
				case'Officers:':ter.Officer=thisEdit.getAttribute('value');break MWSW;
				case'Crew:':ter.Crew=thisEdit.getAttribute('value');break MWSW;
			}
		}

		if (neutral>0) {var lsoutput = Math.ceil(ter.Output*reduce)}
		else {var lsoutput = ter.Output};
		
		if(ter.Condition==undefined && location.href.split('ship_id=')[1]!=null) {
		nameeval=document.evaluate("//td[contains(.,'Scan')] ", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
		namegrab=nameeval.snapshotItem(0);

		if (namegrab==undefined){
			ter.Status = "Deceased";
			var nLink="http://black-beach.net/db/deadspecimport.php?csscandate=" + rtime + "&" +
			"csshipid=" + shipid + "&" +
			"csstatus=" + ter.Status;}else if(namegrab){
			ter.Status = "OG Scan";
			var nLink="http://black-beach.net/db/deadspecimport.php?csscandate=" + rtime + "&" +
			"csshipid=" + shipid + "&" +
			"csstatus=" + ter.Status;}

		nameeval=document.evaluate("//td[contains(.,'Design:')] ", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
		namegrab=nameeval.snapshotItem(0);			

			if (namegrab) {			
	newbox=document.createElement("input");
	newbox.setAttribute('type','text');
	newbox.setAttribute('class','forms_txt_fade');
	newbox.setAttribute('value','');
	newbox.setAttribute('style','float:center;');
	newbox.style.margin='10px 2px 0px 0px';
	newbox.style.width='55px';
	newbox.id='trailID';
	newbox.maxLength=9;
	namegrab.appendChild(newbox);

	GM_xmlhttpRequest({
		method: "GET",
		url: "http://black-beach.net/db/inject/scanioninject.php?csshipid=" + shipid,
		headers: {
			"Content-Type": "application/x-www-form-urlencoded"
		},
		onload: function(response) {
		newbox.setAttribute('value',response.responseText);
			if (response.responseText>0) {
				newbutton.setAttribute('disabled',"disabled");
				newbutton.setAttribute('class','forms_btn_off');
			}
		}
	});
	
	newbutton=document.createElement("input");
	newbutton.setAttribute('type','button');
	newbutton.setAttribute('class','forms_btn');
	newbutton.setAttribute('value',"Submit Ion");
	newbutton.setAttribute('style','float:center;');
	newbutton.style.margin='10px 2px 0px 0px';
	newbutton.onclick = function() {
		var csion = document.getElementById('trailID').value;
		if (csion.search("\\.")!=-1)
		{alert('Just the bit after the . in the trail please');}
		else
		{var shipid = location.href.split('ship_id=')[1];
		var ionLink=shipid + '&csion=' + csion;
		GM_xmlhttpRequest({
			method: "GET",
			url: "http://black-beach.net/db/ionimport.php?csshipid=" + ionLink,
			headers: {"Content-Type": "application/x-www-form-urlencoded"
			},
		});
		if (csion.length==9) {alert('Ion submitted');} else {alert('Ion length too short - should be 9 digits');}
		}
	};
	namegrab.appendChild(newbutton);}
			
			if (namegrab) {
			ter.ShipName = namegrab.textContent.split("'")[1];
			ter.Design = namegrab.textContent.split("Design: ")[1].split(" (")[0];
			ter.Class = namegrab.textContent.split("(")[1].split(")")[0];
			ter.Status = namegrab.textContent.split("Currently ")[1];
				if (ter.Status==undefined) {ter.Status="In Space";}
			var nLink="http://black-beach.net/db/minispecimport.php?csscandate=" + rtime + "&" +
			"csshipid=" + shipid + "&" +
			"csshipname=" + ter.ShipName + "&" +
			"csshiptype=" + ter.Class + "&" +
			"csshipdesign=" + ter.Design + "&" +
			"csstatus=" + ter.Status;
			}
		}
		else if (ter.Condition!=undefined) {
			var nLink= rtime + "&" +
			"csshipid=" + shipid + "&" +
			"csshipname=" + ter.ShipName + "&" +
			"csshiptype=" + ter.Class + "&" +
			"csshipdesign=" + ter.Design + "&" +
			"cshull=" + ter.Hull + "&" +
			"cssize=" + ter.Isaton + "&" +
			"csoutput=" + ter.Output + "&" +
			"lsoutput=" + lsoutput + "&" +
			"cscond=" + ter.Condition + "&" +
			"csreactor=" + ter.Reactor + "&" +
			"csengine=" + ter.Engine + "&" +
			"csjump=" + ter.JumpDrive + "&" +
			"csnav=" + ter.Navigation + "&" +
			"csscanner=" + ter.Scanner + "&" +
			"cstarget=" + ter.Targeting + "&" +
			"csshield=" + ter.ShieldGen + "&" +
			"cspriwep=" + ter.Weapon1 + "&" +
			"cspriwepx=" + ter.Wamt1 + "&" +
			"cssecwep=" + ter.Weapon2 + "&" +
			"cssecwepx=" + ter.Wamt2 + "&" +
			"csminingbay=" + ter.MiningBay + "&" +
			"csrepairbay=" + ter.RepairBay + "&" +
			"csconsbay=" + ter.ConstructionBay + "&" +
			"csarmor=" + ter.Armor + "&" +
			"cshold=" + ter.CargoHold + "&" +
			"csext0=" + terext[0] + "&" +
			"csext0x=" + terextamt[0] + "&" +
			"csext1=" + terext[1] + "&" +
			"csext1x=" + terextamt[1] + "&" +
			"csext2=" + terext[2] + "&" +
			"csext2x=" + terextamt[2] + "&" +
			"csext3=" + terext[3] + "&" +
			"csext3x=" + terextamt[3] + "&" +
			"csext4=" + terext[4] + "&" +
			"csext4x=" + terextamt[4] + "&" +
			"cssystem=" + system + "&" +
			"cssector=" + sector + "&" +
			"csgrid=" + cgrid + "&" +
			"csstatus=" + ter.Status;
		}
	}
}
	if (nLink!=undefined) {
       GM_xmlhttpRequest({
                method: "GET",
                url: "http://black-beach.net/db/shipspecimport.php?csscandate=" + nLink,
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded"
          },
});
//		alert(nLink);
		}
		
		
var newTable=document.createElement('table');
newTable.align="center";
var newTR,newTD;
var mainPage=document.evaluate("//a[text()='Starships in Grid']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
var thisPage=mainPage.snapshotItem(0);
if(thisPage){
	newTR=document.createElement('tr');
		newTD=document.createElement('td');
			newTD.align='center';
			newTD.setAttribute('style','padding:4px 4px 4px 4px;');
			newTD.appendChild(thisPage.cloneNode(true));
			newTD.appendChild(thisPage.nextSibling.cloneNode(true));
			newTD.setAttribute('colspan','4');
			newTR.appendChild(newTD);
	newTable.appendChild(newTR);
}
var editPage=document.evaluate("//form[contains(@action,'design_name')]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
var editsPage=editPage.snapshotItem(0);
if(editsPage){
	newTR=document.createElement('tr');
		newTD=document.createElement('td');
			newTD.align='center';
			newTD.setAttribute('style','padding:4px 4px 4px 4px;');
				newTD.setAttribute('colspan','4');
				newForm=document.createElement('form');
					newForm.setAttribute('action',editsPage.getAttribute('action'));
					newForm.setAttribute('onsubmit',editsPage.getAttribute('onsubmit'));
					newForm.setAttribute('method',editsPage.getAttribute('method'));
					newForm.setAttribute('style','margin:0px');
					newTable2=document.createElement('table');
						newTable2.setAttribute('cellspacing','2');
						newTable2.setAttribute('cellpadding','0');
						newTable2.setAttribute('border','0');
						newTR2=document.createElement('tr');
							newTD2=document.createElement('td');
								newTD2.appendChild(document.createTextNode('Design Name: '));
								var dnames=document.evaluate("//input[@name='design_name']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
								var dname=dnames.snapshotItem(0);
								newTD2.appendChild(dname.cloneNode(true));
							newTR2.appendChild(newTD2);
							newTD2=document.createElement('td');
								var dbuttons=document.evaluate("//input[@value='E']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
								var dbutton=dbuttons.snapshotItem(0);
								newTD2.appendChild(dbutton.cloneNode(true));
							newTR2.appendChild(newTD2);
					newTable2.appendChild(newTR2);
				newForm.appendChild(newTable2);
				newTD.appendChild(newForm);
			newTR.appendChild(newTD);
	newTable.appendChild(newTR);
}
var ter=new Array();
allEdits=document.evaluate("//input[@onfocus='blur();']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
var iLength=allEdits.snapshotLength;
var thisEdit;
for(var i=0;i < iLength;i++){
	thisEdit=allEdits.snapshotItem(i);
	if(thisEdit.previousSibling.textContent==' : '){
		desc=thisEdit.previousSibling.previousSibling.previousSibling.textContent+': ';
	}else{
		desc=thisEdit.previousSibling.textContent;
	}
	desc=desc.split('\n').join('').split(' :').join(':');
	ter[i]=new Object();
	if(desc!=' X '){
		ter[i]["desc"]=desc;
		ter[i]["value"]=thisEdit.getAttribute('value');
		ter[i]["amount"]="no";
	}else{
		ter[i-1]["amount"]=thisEdit.getAttribute('value');
	}
}
mainloop:for(var j=0;j < ter.length;j++){
	if(ter[j]["desc"]!=null){
		if(ter[j]["desc"]!="Points: "){
			newTR=document.createElement('tr');
				newTD=document.createElement('td');
					newTD.align='right';
					newTD.setAttribute('style','padding:0px 4px 0px 0px;');
					newTD.appendChild(document.createTextNode(ter[j]["desc"]));
					newTR.appendChild(newTD);
				newTD=document.createElement('td');
					newTD.align='left';
					newTD.setAttribute('style','padding:0px 4px 0px 4px;');
					newTD.appendChild(document.createTextNode(ter[j]["value"]));
								if (ter[j]["desc"]=="Output: ") {
								newTD.className='forms_txt_fade';
								newTR.appendChild(newTD);
	//							newTD.align='center';
										child=document.createElement("input");
										child.type='text';
										child.id='trailID';
								
									GM_xmlhttpRequest({
										method: "GET",
										url: "http://black-beach.net/db/inject/scanioninject.php?csshipid=" + shipid,
										headers: {
											"Content-Type": "application/x-www-form-urlencoded"
										},
										onload: function(response) {
										child.value=response.responseText;
											if (response.responseText>0) {
											newbutton.setAttribute('disabled',"disabled");
											newbutton.setAttribute('class','forms_btn_off');
											}
										}
									});
									
										child.className='forms_txt_fade';
										child.style.width='55px';
										child.style.margin='0px 3px 0px 5px';
										child.maxLength=9;
										child.setAttribute("onKeyPress","var key,keychar;if(window.event) key=window.event.keyCode;else if(event) key=event.which;else return true;keychar=String.fromCharCode(key);if(key==0||key==8||key==9||key==13||key==27||key==86) return true;else if(key<=47||key>=58) return true;else if((('0123456789').indexOf(keychar) > -1)) return true;else return false;");
									newbutton=document.createElement("input");
									newbutton.className='forms_txt_fade';
									newbutton.style.margin='0px -3px 0px 0px';
									newbutton.setAttribute('type','button');
									newbutton.setAttribute('class','forms_btn');
									newbutton.setAttribute('value',"Submit Ion");
									newbutton.onclick = function() {
                                        var csion = document.getElementById('trailID').value;
										if (csion.search("\\.")!=-1)
										{alert('Just the bit after the . in the trail please');}
										else
										{var shipid = location.href.split('ship_id=')[1];
										var ionLink=shipid + '&csion=' + csion;
										GM_xmlhttpRequest({
											method: "GET",
											url: "http://black-beach.net/db/ionimport.php?csshipid=" + ionLink,
											headers: {"Content-Type": "application/x-www-form-urlencoded"
											},
										});
                                        if (csion.length==9) {alert('Ion submitted');} else {alert('Ion length too short - should be 9 digits');}
										}
                                    };
//								newTD=document.createElement('td');
								newTD.appendChild(child);
								newTD.appendChild(newbutton);			
								}
					newTD.className='forms_txt_fade';
					newTR.appendChild(newTD);
			if(ter[j]["amount"]=="no"){
				newTD=document.createElement('td');
					newTR.appendChild(newTD);
				newTD=document.createElement('td');
					newTR.appendChild(newTD);
			}else{

				newTD=document.createElement('td');
					newTD.align='center';
					newTD.setAttribute('style','padding:0px 4px 0px 4px;');
					newTD.appendChild(document.createTextNode(' X '));
					newTR.appendChild(newTD);
				newTD=document.createElement('td');
					newTD.align='left';
					newTD.setAttribute('style','padding:0px 4px 0px 4px;');
					newTD.appendChild(document.createTextNode(ter[j]["amount"]));
					newTD.className='forms_txt_fade';
					newTR.appendChild(newTD);
			}
			newTable.appendChild(newTR);
		}else{
			function addinner(){
				var loop=2;
				var num=0;
				while(num <= loop) {
					newTR3=document.createElement('tr');
						newTD3=document.createElement('td');
							newTD3.align='right';
							newTD3.setAttribute('style','padding:0px 4px 0px 0px;');
							newTD3.appendChild(document.createTextNode(ter[j]["desc"]));
							newTR3.appendChild(newTD3);
						newTD3=document.createElement('td');
							newTD3.align='left';
							newTD3.setAttribute('style','padding:0px 4px 0px 4px;');
							newTD3.appendChild(document.createTextNode(ter[j]["value"]));
							newTD3.className='forms_txt_fade';
							newTR3.appendChild(newTD3);
					newTable3.appendChild(newTR3);
					num++;
					j++;
				}
			}
			newTR=document.createElement('tr');
			newTable.appendChild(newTR);
				newTD=document.createElement('td');
					newTD.align='center';
					newTD.setAttribute('style','padding:0px;');
					newTD.setAttribute('colspan','4');
					newTR.appendChild(newTD);
					var newTR2,newTD2,newTable2,newTR3,newTD3,newTable3;
					newTable2=document.createElement('table');
					newTable2.align="center";
					newTD.appendChild(newTable2);
					newTR2=document.createElement('tr');
						newTable2.appendChild(newTR2);
						newTD2=document.createElement('td');
							newTD2.align='center';
							newTD2.setAttribute('style','padding:0px;');
							newTR2.appendChild(newTD2);
							newTable3=document.createElement('table');
							newTable3.align="center";
							newTD2.appendChild(newTable3);
							addinner();
						newTD2=document.createElement('td');
							newTD2.align='center';
							newTD2.setAttribute('style','padding:0px;');
							newTR2.appendChild(newTD2);
							newTable3=document.createElement('table');
							newTable3.align="center";
							newTD2.appendChild(newTable3);
							addinner();
						newTD2=document.createElement('td');
							newTD2.align='center';
							newTD2.setAttribute('style','padding:0px;');
							newTR2.appendChild(newTD2);
							newTable3=document.createElement('table');
							newTable3.align="center";
							newTD2.appendChild(newTable3);
							addinner();
		}
	}
}
var external=document.evaluate("//text()[contains(.,'Installed Externals')]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
var externals=external.snapshotItem(0);
if(externals){
	var extindex=0;
	var extparnum=externals.parentNode.childNodes.length;
	extloop:while (extindex<extparnum){
		if(externals==externals.parentNode.childNodes[extindex])break extloop;
		extindex++;
	}
	var extclone=externals.parentNode.childNodes[extindex].cloneNode(true);
	while(extclone.nodeName!="IMG"&&extclone.nodeName!="HR"){
		if(extclone.nodeName!="BR"&&extclone.nodeValue!="\n"){
			var exttoken=extclone.nodeValue.split(' x ');
			if(exttoken[1]!=null){
				newTR=document.createElement('tr');
					newTD=document.createElement('td');
						newTD.className='forms_txt_fade';
						newTD.align='center';
						newTD.setAttribute('style','padding:0px;');
						newTD.appendChild(extclone);
						newTD.setAttribute('colspan','4');
						newTR.appendChild(newTD);
				newTable.appendChild(newTR);
			}else{
				newTR=document.createElement('tr');
					newTD=document.createElement('td');
						newTD.align='center';
						newTD.setAttribute('style','padding:0px;');
						newTD.appendChild(extclone);
						newTD.setAttribute('colspan','4');
						newTR.appendChild(newTD);
				newTable.appendChild(newTR);
			}
		}
		extindex++;
		extclone=externals.parentNode.childNodes[extindex].cloneNode(true);
	}
}
if(thisPage){
	var bottomStuff=document.evaluate("//hr", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
	var bottomStuffs=bottomStuff.snapshotItem(0);
	newTR=document.createElement('tr');
		newTD=document.createElement('td');
		newTD.align='center';
		newTD.setAttribute('style','padding:4px 4px 4px 4px;');
		newTD.setAttribute('colspan','4');
		newTR.appendChild(newTD);
	newTable.appendChild(newTR);
	while(bottomStuffs){
		if(bottomStuffs.nodeName=='HR'){
			newTD.appendChild(document.createElement('hr'));
		}else{
			var bottomStuffClone=bottomStuffs.cloneNode(true);
			newTD.appendChild(bottomStuffClone);
		}
		bottomStuffs=bottomStuffs.nextSibling;
	}
	thisPage.parentNode.parentNode.appendChild(newTable);
	thisPage.parentNode.setAttribute('style','display:none!important;');
	
}else{
	thisEdit.offsetParent.offsetParent.offsetParent.offsetParent.parentNode.appendChild(newTable);
	thisEdit.offsetParent.offsetParent.offsetParent.offsetParent.setAttribute('style','display:none!important;');
}