// Browser detection
var ie4 = document.all;
var ns6 = document.getElementById && !document.all;

// Initial quote URL
var url = "/home/getQuote.php?param="; 

// Other variables
var reqnum = 0;	
var param = 0;

// Run the trip function on load
window.onload = trip2;

// Move the corner, initialize the quote, setup the post parameters,
// add the quote reload methods and load the post variables
function trip2() {
	updateQuote(0);
	window.status = '';
	document.getElementById('reload').onmousedown=function(){quoteDown();};
	document.getElementById('reload').onmouseup=function(){quoteUp();};
	document.getElementById('reload').onclick=function(){updateQuote(0);};
}	

// Shortcut keys - 'q' only when on misc. pages.
function keyCall(e) {
	var keyCode = (window.event) ? event.keyCode : e.keyCode;
	var keyChar = (String.fromCharCode(keyCode).toLowerCase());
		if (keyChar == 'q')
			updateQuote();
}

// Checks keypresses on keyup
document.onkeyup = keyCall


//
// COMMON JAVASCRIPT FUNCTIONS
//

// XMLHttpRequest object (AJAX)
var http = getHTTPObj();

// Reload the quote tag with the retrieved quote and raise up the button
function handleQuote() {
 if (http.readyState == 4 && http.status == 200) {
 	 document.getElementById('quote').innerHTML = http.responseText;
   document.getElementById('reload').src = '/home/images/reload.gif';
 }
}

function deleteQuote(id) {
	var answer = confirm ("Delete quote " + id + " ?")
	if (answer) {
		reqnum += 1;
		idparam = "&id=";
		ajax("quote", deleteUrl + reqnum + idparam + id, handleQuote);
	}
	else
		updateQuote(id);
}

// Start the update quote thread and depress the button
// Reqnum is so the browser thinks that it's a new page and
// doesn't retrieve it from cache
function updateQuote(id) {
	document.getElementById('reload').src = '/home/images/reload_down.gif';
	reqnum += 1;
	idparam = "&id="
	ajax("quote", url + reqnum + idparam + id, handleQuote);
}

// Start the edit quote thread
function editQuote(id) {
	ajax("quote", "/home/editquote.php?id=" + id + "&mode=" + mode, handleQuote);
}

// Depresses the reload quote button
function quoteDown() {
	document.getElementById('reload').src = '/home/images/reload_down.gif';	
}

// Unpresses the reload quote button
function quoteUp() {
	document.getElementById('reload').src = '/home/images/reload.gif';	
}

// Get a new AJAX object
function getHTTPObj() {
	if (window.XMLHttpRequest) {
    reqXML = new XMLHttpRequest();
    reqXML.overrideMimeType('text/xml');
  }
  else if(window.ActiveXObject)
    reqXML = new ActiveXObject("Microsoft.XMLHTTP"); 
  return reqXML;
}

// Run the ajax - change the html to a load screen and
// setup the thread
function ajax(id, url, func) {
	document.getElementById(id).innerHTML = "<p><i>Loading...</i></p>";
	http.open("GET", url, true);
	http.onreadystatechange = func;
	http.send(null);
}	

