function getReport(reportURL, suggestionURL) {
	return getReportEx(reportURL, suggestionURL, "");
}

function getReportEx(reportURL, suggestionURL, channel) {
$('#token-panel').hide("slow");

function battleerror(XMLHttpRequest, textStatus, errorThrown)
{
  alert("congratulations. you have discovered a bug.  your battle report information has been logged and the developers will review the issue. Please the original Evony battle report for as long as possible to allow debugging of this issue");
}
$.ajaxSetup({
  error:battleerror
});

$.getJSON("/battle",{'url':reportURL, 'suggestion':suggestionURL, 'channel':channel}, function (data, textStatus) {
if (textStatus=="success") {
  if (data.error)         {  window.alert(data.error);  } 
  else if (data.redirect) {  window.location = data.redirect }
  else {
  $('#tokenurl').val('http://evonyurl.com/' + data.token);
  if (data.owner=="none")
  { $('#token-add').html("<div style='text-align:center;background:#DDDDDD;padding:5px;'><a href='" + data.addurl + "'>Sign in or register</a> to add a description to this battle report</div>"); }
  if (data.owner=="you")
  { $('#token-edit').html("<div style='background:#DDDDDD;padding:5px;'>add description:<form action='/ui' method='post'><input type='hidden' name='_a' value='describe' /><input type='hidden' name='_t' value='" + data.token + "' /><textarea rows='2' cols='80' name='_d'></textarea><br/>" + 
			"<input type='checkbox' name='_p' value='1'/> Share your report with the world.<br/>" +
			"<input type='checkbox' name='_f' value='1'/> Favorite<br/>" + 
		  	"<input type='checkbox' name='_o' value='1'/> Disguise city and hero names<br/><input type='submit' value='save' /></form></div>"); }
  $('#token-a').html('<a href="/' + data.token + '">view</a>')
  $('#token-panel').show("slow");
  }
  pageTracker._trackPageview("/battle");
} else {
  window.alert("congratulations. you have discovered a bug.  your battle report information has been logged and the developers will review the issue. Please the original Evony battle report for as long as possible to allow debugging of this issue");
}
});}
function showedit(token, obscure, fav, pub){
    ochecked = "";
    ofav = "";
    opub = "";
	if (obscure == "1")       { ochecked = " checked "; }
	if (fav == "True") { ofav =     " checked "; }
	if (pub == "True") { opub =     " checked "; }   
	$('#tr-' + token).html("<td colspan='8'><div style='background:#DDDDDD;padding:5px;'><b>" + token + "</b> add description:<form action='/ui' method='post'><input type='hidden' name='_a' value='describe' /><input type='hidden' name='_t' value='" + token + "' /><textarea rows='2' cols='80' name='_d'></textarea><br/>" +
		"<input type='checkbox' " + opub + " name='_p' value='1'/> Share your report with the world.<br/>" +
		"<input type='checkbox' " + ofav + " name='_f' value='1'/> Favourite<br/>" +
		"<input type='checkbox' " + ochecked + " name='_o' value='1'/> Disguise city and hero names<br/><input type='submit' value='save' /></form></div></td>") 
	$('#tr-' + token).show();
}
function fav(token,f){
	function faverror(XMLHttpRequest, textStatus, errorThrown)
	{
	  alert("unable to set favorite");
	}
	$.ajaxSetup({
	  error:faverror
	});
	$('#f-' + token).html( "<img src='/img/f2.png' width='16px' height='15px' />");
	$.getJSON("/ui",{'_a':'fav','_f': f,'_t':token}, function (data, textStatus) {
		if (textStatus=="success") {
		  if (data.f == 0){
			$('#f-' + data.t).html( "<img src='/img/f0.png' onclick='fav(\"" + data.t + "\",1);' width='16px' height='15px' />");
		  }else{
			$('#f-' + data.t).html( "<img src='/img/f1.png' onclick='fav(\"" + data.t + "\",0);' width='16px' height='15px' />");			  
		  }
		}
		else{
		  window.alert("error setting favorite. try again. if problem persists, please report a bug");
		}
			
	});
}
function pub(token,aval){
	
	function acterror(XMLHttpRequest, textStatus, errorThrown)
	{
	  alert("unable to set share setting");
	}
	$.ajaxSetup({
	  error:acterror
	});
	
	$('#p' + '-' + token).html( "<img src='/img/p2.png' width='16px' height='15px' />");
	$.getJSON("/ui",{'_a':'pub','_p':aval,'_t':token}, function (data, textStatus) {
		if (textStatus=="success") 
		{		
		  if (data.p == 0){
			$('#p-' + data.t).html( "<img src='/img/p0.png' onclick='pub(\"" + data.t + "\",1);' width='16px' height='15px' />");
		  }else{
			$('#p-' + data.t).html( "<img src='/img/p1.png' onclick='pub(\"" + data.t + "\",0);' width='16px' height='15px' />");			  
		  }

		
		}
		else{
		  window.alert("error setting share value. try again. if problem persists, please report a bug");
		}
			
	});
}


function obs(token,o){
	function obserror(XMLHttpRequest, textStatus, errorThrown)
	{
	  alert("unable to set obscure");
	}
	$.ajaxSetup({
	  error:obserror
	});
	$('#o-' + token).html( "<img src='/img/o2.png' width='16px' height='15px' />");
	$.getJSON("/ui",{'_a':'obs','_o': o,'_t':token}, function (data, textStatus) {
		if (textStatus=="success") {
		  if (data.o == 0){
			$('#o-' + data.t).html( "<img src='/img/o0.png' onclick='obs(\"" + data.t + "\",1);' width='16px' height='15px' />");
		  }else{
			$('#o-' + data.t).html( "<img src='/img/o1.png' onclick='obs(\"" + data.t + "\",0);' width='16px' height='15px' />");			  
		  }
		}
		else{
		  window.alert("error setting obscure. try again. if problem persists, please report a bug");
		}
			
	});
	
}
function del(token){
	if (window.confirm("Are you sure you wish to delete report[" + token + "]"))
	{
	
	function deleteerror(XMLHttpRequest, textStatus, errorThrown)
	{
	  alert("unable to delete");
	}
	$.ajaxSetup({
	  error:deleteerror
	});
	$.getJSON("/ui",{'_a':'del','_t':token}, function (data, textStatus) {
	if (textStatus=="success") 
	{		
		$('#r-' + token).hide("slow");
	}
	else
	{
		window.alert("error deleting report. try again. if problem persists, please report a bug");
	}
			
	});
	}
}

//--- url helper functions
function trim11 (str) {
    str = str.replace(/^\s+/, '');
    for (var i = str.length - 1; i >= 0; i--) {
        if (/\S/.test(str.charAt(i))) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    return str;
}

function setsuggestion(value)
{
  inputsuggestion = document.getElementById("urlsuggestion");
  inputsuggestion.value = value;
}
function sethelper(value)
{
  helperelement = document.getElementById("url-helper");
  helperelement.innerHTML = value;
}

function checkurl(url)
{
  if ( trim11(url) )
  {
    reason = urlhelper(url,1);
    if (reason)
    {
      helperelement = document.getElementById("url-helper");
      helperelement.innerHTML = reason;
      setsuggestion("");
      $("#suggestion").show();
    }
    else
    {
      suggestion = urlhelper(url);
      setsuggestion(suggestion);
      sethelper("");
      $("#suggestion").hide();
    }
  }
  else
  {
      helperelement = document.getElementById("url-helper");
      helperelement.innerHTML = "";
      setsuggestion("");
      $("#suggestion").hide();
  }
}

function urlhelper(url, reason)
{
  url = trim11(url);
  // evony.***
  url = url.replace(/evony\.\*\*\*/,"evony.com");
  // battle12*evony*com
  url = url.replace(/\*/g,".");
  // battle12,evony,com
  url = url.replace(/,/g,".");

  
  osbattle = url.indexOf("battle");
  osbattle2 = url.indexOf("battle", osbattle + 1);
  osbattlefragment = url.indexOf("tle");
  osevonycom = url.indexOf(".evony.com");
  osxml = url.indexOf(".x", osevonycom);

  if (osbattle >= 0 && osbattle2 == -1 && (osxml-osbattle) > 70 && (osxml-osbattle) < 100)
  {
    if (reason) { return }
    cleanurl = url.substring(osbattle,osxml) + ".xml";
    return cleanurl;
  }

  if (osbattle2 && osbattle2 > osbattle && osbattle2 < osxml && (osxml-osbattle2) > 70 && (osxml-osbattle2) < 100)
  {
    if (reason) { return }
    cleanurl = url.substring(osbattle2,osxml) + ".xml";
    return cleanurl;
  }

  if (osbattlefragment>=0 && osbattlefragment < osevonycom && osevonycom - osbattlefragment < 9 && (osxml-osbattlefragment) > 70 && (osxml-osbattlefragment) < 100 )
  {
    if (reason) { return }
    cleanurl = "battle" + url.substring(osbattlefragment +3,osxml) + ".xml";
    return cleanurl;
  }

  if (reason)
  {
    var strout = "<p>Your battle report url doesn't appear to be valid. In case you need some help:</p>"
    strout = strout + "<ul>";
    var strexample = "http://";
    if (osbattlefragment == -1)
    {
      strout = strout + "<li>Needs to start with <strong>battle</strong></li>";
      strexample = strexample + "<strong>battle</strong>";
    }
    else
    {
      strexample = strexample + "battle";
    }
    strexample = strexample + "00";
    if (osevonycom == -1)
    {
      strout = strout + "<li>Needs to be an <strong>evony.com</strong> url</li>";
      strexample = strexample + "<strong>.evony.com</strong>";
    }
    else
    {
      strexample = strexample + ".evony.com";
    }

    strexample = strexample +"/default.html?logfile/20100101/aa/bb/aabbccddeeff11223344556677889900";

    if (osxml == -1)
    {
      strout = strout + "<li>Needs to end with <strong>.xml</strong></li>";
      strexample = strexample + "<strong>.xml</strong>";
    }
    else
    {
      strexample = strexample + ".xml";
      if( (osxml-osbattlefragment) <= 70)
      {
        strout = strout + "<li>Your url appears to be too short. Some information may be missing</li>";
      }

      if( (osxml-osbattlefragment) >= 100)
      {
        strout = strout + "<li>Your url appears to be too long. Try clearing the textbox first</li>";
      }

    }

    strout = strout + "<li>Try copying the url from evony again</li>";

    strout = strout + "</ul>";
    return strout + "<span class='example'>example: " + strexample + "</span>";
  }
  return;
}

