// Lalala Oooh Hoo.

var callingElement;
var callingPosition;
var involvedCount = 1;
var toggleNonISKLock = 0;

function isset( variable ){ return( typeof( variable ) != 'undefined' ); }

function stripslashes(str) {
   str=str.replace(/\\'/g,'\'');
   str=str.replace(/\\"/g,'"');
   str=str.replace(/\\\\/g,'\\');
   str=str.replace(/\\0/g,'\0');
   return str;
}
function nl2br(str) {
	return str.replace(/([^>]?)\n/g, "$1<br />\n");
}

function findPosition(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function chooseSuggest(e) {
	callingElement.value = e.innerHTML;
	$("#suggestbox").css("display","none");
	callingElement.blur();
	validateNewForm();
}

function suggest(element, type){
	var str = element.value;
	if(str.length==0){ $("#suggestbox").css("display","none"); return; }
	if(callingElement!=element){
		callingElement = element;
		callingPosition = findPosition(element);
		$("#suggestbox").css("left", (callingPosition[0]+210).toString()+"px");
		$("#suggestbox").css("top", (callingPosition[1]).toString()+"px");
	}
	$.ajax({
		mode: "GET",
		url: "ajax/suggest.php",
		data: "q="+str+"&type="+type,
		success: function(htmlDoc){
			if(htmlDoc!=""){
				$('#suggestbox').html(htmlDoc).css("display", "block");
			}else{
				$('#suggestbox').css("display", "none");
			}
		}
	});
}

function makeReadable(e) {
	var amount = new Number(e.value.replace(",", ""));
	if(amount>=1000000000){
		amount = "~" + Math.round(amount/1000000000).toString() + " billion ISK";
		$('#amountreadable').html(amount);
		return;
	}
	if(amount>=1000000){
		amount = "~" + Math.round(amount/1000000).toString() + " million ISK";
		$('#amountreadable').html(amount);
		return;
	}
	if(amount>=1000){
		amount = "~" + Math.round(amount/1000).toString() + "k ISK";
		$('#amountreadable').html(amount);
		return;
	}
	$('#amountreadable').html("");
}

function addInvolvedOld(startCount) {
	var ilist = document.getElementById('involvedlist');
	var newinput = document.createElement('input');
	if(startCount!=0 && involvedCount==1){ involvedCount = startCount; }
	involvedCount++;
	newinput.setAttribute("id", "involved"+involvedCount);
	newinput.setAttribute("name", "involved"+involvedCount);
	newinput.setAttribute("onkeyup", "suggest(this, 'pilot'); validateNewRansom();");
	newinput.setAttribute("onblur", "document.getElementById('suggestbox').style.display='none';");
	ilist.appendChild(document.createElement('label'));
	ilist.appendChild(newinput);
	ilist.appendChild(document.createElement('br'));
  
	//ilist.innerHTML = ilist.innerHTML + "\n<label for=\"involved" + involvedCount + "\"></label><input name=\"involved" + involvedCount + "\" type=\"text\" onkeyup=\"suggest(this, 'pilot');\" onblur=\"document.getElementById('suggestbox').style.display='none';\"><br />";
	return;
}
function addInvolved(startCount) {
	if(startCount!=0 && involvedCount==1){ involvedCount = startCount; }
	involvedCount++;
	var newinv = $('<div/>').appendTo('#involvedlist').hide();
	$('<label/>').appendTo(newinv);
	$('<input/>').attr({'id':'involved'+involvedCount, 'name':'involved'+involvedCount}).bind('keyup', function(){ suggest(this, 'pilot'); validateNewRansom();} ).bind('blur', function(){	$('#suggestbox').hide();} ).appendTo(newinv);
	$(newinv).slideDown();
	return;
}

function toggleNonISK() {
	if(toggleNonISKLock==0){
		toggleNonISKLock=1;
		if($('#nonisk').css('display')=='none'){
			$('#newinput_usenonisk').val('yes');
			$('#amountlabel').html('ISK value:');
		}else{
			$('#newinput_usenonisk').val('no');
			$('#amountlabel').html('Amount:');
		}
		$('#nonisk').slideToggle(function(){ toggleNonISKLock=0; });
		validateNewRansom();
	}
}
function validateNewRansom() {
	var ni_ship = $('#newinput_victimship').val();
	var ni_amount = $('#newinput_amount').val();
	var ni_nonisk = $('#newinput_nonisk').val();
	var ni_usenonisk = $('#newinput_usenonisk').val();
	var ni_involved = $('#newinput_involved').val();
	var errors = ""; var valid = true;
	if(ni_ship.length<1){ valid = false; errors = errors + "<span class=\"redrow\">You must enter the ship type.</span>\n"; }
	if(ni_amount.length<1){ valid = false; errors = errors + "<span class=\"redrow\">You must enter the ransom amount.</span>\n"; }
	if(ni_usenonisk=="yes"){
		if(ni_nonisk.length<1){ valid = false; errors = errors + "<span class=\"redrow\">You must enter a non-isk value.</span>\n"; }
		if(ni_amount<0){ valid = false; errors = errors + "<span class=\"redrow\">Ransom amount must be greater than or equal to 0.</span>\n"; }
	}else{
		if(ni_amount<=0){ valid = false; errors = errors + "<span class=\"redrow\">Ransom amount must be greater than 0.</span>\n"; }
	}
	if(ni_involved.length<1){ valid = false; errors = errors + "<span class=\"redrow\">You must enter at least one involved pilot.</span>\n"; }
	if(valid==true){
		$('#newinput_submit').css({"display": "block", "opacity": "0"}).animate({opacity:1}, "slow"); $('#inputerrors').html("").hide("slow");
	}else{
		$('#newinput_submit').css("display", "none"); $('#inputerrors').html(errors).show("fast");
	
	}
}

function adminRansomSearchID(e){
	$.ajax({
		mode: "GET",
		url: "ajax/searchid.php",
		data: "id=" + e.value,
		success: function(xmlDoc){
			var id = $(xmlDoc).find('id').text();
			var pilotid = $(xmlDoc).find('pilot').attr('id');
			var pilotname = $(xmlDoc).find('pilot').text();
			var shipid = $(xmlDoc).find('ship').attr('id');
			var shipname = $(xmlDoc).find('ship').text();
			var systemid = $(xmlDoc).find('system').attr('id');
			var systemname = $(xmlDoc).find('system').text();
			var amount = $(xmlDoc).find('amount').text();
			var inv = $(xmlDoc).find('involved').text();
			var rhtml = "<div class=\"id\">Ransom ID: <a href=\"index.php?p=ransom&id=" + id + "\">" + id + "</a></div>\n";
			rhtml = rhtml + "<div class=\"pilot\">Pilot: <a href=\"index.php?p=pilot&id=" + pilotid + "\">" + pilotname + "</a></div>\n";
			rhtml = rhtml + "<div class=\"ship\">Ship: <a href=\"index.php?p=ship&id=" + shipid + "\">" + shipname + "</a></div>\n";
			rhtml = rhtml + "<div class=\"system\">System: <a href=\"index.php?p=system&id=" + systemid + "\">" + systemname + "</a></div>\n";
			rhtml = rhtml + "<div class=\"amount\">Amount: " + amount + "</div>\n";
			rhtml = rhtml + "<div class=\"involved\">Involved: " + inv + "</div>\n";
			$('#adminransominputactions').html("<a href=\"index.php?p=editr&id=" + id + "\"><img src='img/admin_edit.gif' alt='Edit'> Edit</a> - <a href=\"index.php?p=deleter&id=" + id + "\"><img src='img/admin_delete.gif' alt='Delete'> Delete</a>");
			$('#adminransominputresult').html(rhtml);
		}
	});
}

function toggleComments(){
	if($('#ransomcomments').css("display")=="block"){
		$('#ransomcomments').animate({opacity:0}, "fast", function(){
			$('#ransomcomments').css("display", "none");
			$('#ransomstats').css({"display": "block", "opacity": "0"});
			$('#ransomstats').animate({opacity:1}, "slow");
		});
	}else{
		$('#ransomstats').animate({opacity:0}, "fast", function(){
			$('#ransomstats').css("display", "none");
			$('#ransomcomments').css({"display": "block", "opacity": "0"});
			$('#ransomcomments').animate({opacity:1}, "slow");
		});
	}
}