var detect = navigator.userAgent.toLowerCase();
var browser;

function checkIt(string)
{
	place = detect.indexOf(string)+1; //If the string doesn't exist, place will return 0
	
	return place;
}

if (checkIt('safari'))
	browser = 'safari';
else if (checkIt('firefox'))
	browser = 'firefox';
else if (checkIt('msie'))
	browser = 'ie';
else
	browser = 'other';
	
function deleteAlert() {
	id = document.getElementById("delete").value;
	deleteID = "delete" + id;
	issue = document.getElementById(deleteID).innerHTML;
	return confirm("Are you sure you want to delete " + issue + "?");
}

//AJAX Scripts

function getXMLHttpObject()
{
	var xmlHttp;
  	try
    {
    		// Firefox, Opera 8.0+, Safari
    		xmlHttp=new XMLHttpRequest();
    }
  	catch (e)
    {
    		// Internet Explorer
    		try
      	{
      		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      	}
    		catch (e)
      	{
      		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      	}
    	}
	
	return xmlHttp;
}

function stateChanged() 
{ 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
 	{ 
 		content = xmlHttp.responseText.split("%%%");
		document.getElementById("issue").value = content[0];
		document.getElementById("stance").defaultValue = content[1];
 	} 
}

function changeIssue(val) {
	xmlHttp=getXMLHttpObject();
	if (xmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	}
	
	var url="ajaxIssue.php?q=" + val + "&sid=" + Math.random();
		
	xmlHttp.onreadystatechange = stateChanged;
	
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function changeUpload(val)
{
	xmlHttp=getXMLHttpObject();
	if (xmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	}
	
	var url="ajaxUpload.php?q=" + val + "&sid=" + Math.random();
		
	xmlHttp.onreadystatechange = processUpload;
	
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
		
}

function processUpload()
{ 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
 	{ 
 		content = xmlHttp.responseText.split("%%%");
		if (content[1] == "exists")
			alert("There is currently a file on the server with the same name as the file you are seeking to upload.  Please be aware that be submitting this form with the file you selected, you will replace the file currently on the server with the file you have selected.");
		document.getElementById("url").value = "http://staterep.stancox.com/uploads/" + content[0];
		
 	}
}

function editReport() {
	xmlHttp=getXMLHttpObject();
	if (xmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	}
	
	var url="ajaxEditReport.php?sid=" + Math.random();
		
	xmlHttp.onreadystatechange = editReportStateChanged;
	
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);

}

function editReportStateChanged() 
{ 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
 	{ 
 		response = xmlHttp.responseText.split("%%%");
		document.getElementById("title").value = response[0];
		document.getElementById("content").defaultValue = response[1];
 	} 
}

function editPageURL() {
	xmlHttp=getXMLHttpObject();
	if (xmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	}
	
	var url="ajaxEditPageURL.php?sid=" + Math.random();
		
	xmlHttp.onreadystatechange = editPageURLStateChanged;
	
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);

}

function editPageURLStateChanged() 
{ 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
 	{ 
 		response = xmlHttp.responseText.split("%%%");
		document.getElementById("homeURL").value = response[0];
		document.getElementById("billsURL").value = response[1];
 	} 
}

function narrowArchives(monthVal, yearVal)
{
	xmlHttp=getXMLHttpObject();
	if (xmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	}
	
	var url="ajaxNarrowArchives.php?month=" + monthVal + "&year=" + yearVal + "&sid=" + Math.random();
		
	xmlHttp.onreadystatechange = processNarrowArchives;
	
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function processNarrowArchives()
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
 	{ 
 		responseLength = xmlHttp.responseText.length;
 		response = xmlHttp.responseText.substr(0, responseLength-3);
 		reportsArray = response.split("///");
 		
 		selectionString = "";
 		
 		if(responseLength != 0) {
	 		for (x in reportsArray) {
		 		partsArray = reportsArray[x].split("%%%");
		 		id = partsArray[0];
		 		title = partsArray[1];
		 		date = partsArray[2];
		 		
		 		selectionString += "<p><a href=\"viewArchive.php?q=" + id + "\"><span style=\"font-size: 1.5em\">" + title + "</span></a><br />\n";
				selectionString += "<span class=\"capitolReport\">" + date + "</span></p>\n\n";
	 		}
		} else
			selectionString = "There are not any Capitol Reports for this month.";
 		
		document.getElementById("reports").innerHTML = selectionString;
 	} 
}