// Create Dealer objects
var Dealers = new Object();							// Top-level dealers object
Dealers.ajax = new Object();							// Dealer AJAX obect container
Dealers.ajax.response = new Object();				// Dealer AJAX response handlers


// Set Dealers variables
Dealers.version = "1.0.0";																	// Specifies the Dealers version
Dealers.debug = false;																		// Specifies the default debug mode
Dealers.dualDebug = true;																	// Specifies whether or not to execute default actions as well when in debug mode
Dealers.ajax.path = "/themes/dealerfinder/ajax/";								   // Specifies the path for the Dealers AJAX


// Retrieve an object
Dealers.object = function(elem) {
	// Set default return value
	var r = false;
	
	// Retrieve object
	var obj = document.getElementById(elem);
	
	// If object exists, set return value to object
	if(obj) { r = obj; }
	
	// Send return value
	return r;
}


// Retrieve input value	
Dealers.input = function(elem) { 
	// Set default return value
	var r = false;
	
	// Retrieve object
	var obj = this.object(elem);
	
	// If object exists, set return value to object value
	if(obj) { r = obj.value; }
	
	// Send return value
	return r;
}


// Show an element
Dealers.show = function(elem, mode) { 
	// Retrieve object
	var obj = this.object(elem);
	
	// If mode isn't specified, set default mode
	if(!mode) { mode = "block"; }
	
	// If object exists, hide object
	if(obj) { obj.style.display = mode; }
	
	// Return true
	return true;
}


// Hide an element
Dealers.hide = function(elem) { 
	// Retrieve object
	var obj = this.object(elem);
	
	// If object exists, hide object
	if(obj) { obj.style.display = 'none'; }
	
	// Return true
	return true;
}


// Set focus to a field
Dealers.focus = function(elem) {
	// Retrieve object
	var obj = this.object(elem);
	
	// If object exists...
	if(obj) {
		// Try to set focus
		try { obj.focus(); obj.select(); }
		
		// Catch failed attempts
		catch(err) { }
	}

	// Return true
	return true;
}


// Change content of object
Dealers.content = function(elem, content) {
	// Retrieve object
	var obj = this.object(elem);
	
	// If object exists, set content to specified content
	if(obj) { obj.innerHTML = content; }
	
	// Return true
	return true;
}


// Process key input
Dealers.keys = function(evt, allFunc, enterFunc, escapeFunc) {
	// Try to capture key input
	try {
		// If key event doesn't exist, capture window event
		if(!evt) { evt = window.event; }
		
		// If all key function is provided, execute all key function
		if(allFunc) { eval(allFunc); }
		
		// If enter key function is provided and key event is key code 13 (enter), execute enter key function
		if(enterFunc) { if(evt.keyCode == 13) { eval(enterFunc); } }

		// If escape key function is provided and key event is key code 27 (escape), execute escape key function
		if(escapeFunc) { if(evt.keyCode == 27) { eval(escapeFunc); } }
	}
	
	// Catch failed attempts...
	catch(err) { 
		// If debug mode is enabled, output an error
		if(this.debug === true) { alert("Unable to read key events!"); } 
	}
	
	// Return true
	return true;
}


// URL encode a string
Dealers.urlencode = function(str) { return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40'); }


// Create AJAX server
Dealers.ajax.server = function(script, mode, block) {
	// Create connection server object
	var conn = new Object;
	
	// Try to create an XML HTTP request
	try { conn.ajax = new XMLHttpRequest(); }
	
	// Catch failed attempt...
	catch (e) {
		// Try to create an ActiveX XML HTTP request via Msxml2
		try { conn.ajax = new ActiveXObject("MSXML2.XMLHTTP.3.0"); }
		
		// Catch failed attempt...
		catch (e) { 
			// Try to create an ActiveX XML HTTP request via Microsoft
			try { conn.ajax = new ActiveXObject("Microsoft.XMLHTTP"); }
			
			// Catch failed attempt
			catch (e) { alert("An error has occurred while trying to establish asynchronous interaction!"); }
		}
	}

	// If mode is post, save server mode
	if(mode == "post" || mode == "POST") { conn.mode = "post"; }
	
	// If mode is get, save server mode
	else if(mode == "get" || mode == "GET") { conn.mode = "get"; }
	
	// If block is specified...
	if(block) { 
		// Set response type
		conn.response = "display";
		
		// Set connection block
		conn.block = block; 
	}
	
	// If a block isn't specified...
	else {
		// Set response type
		conn.response = "process";
		
		// Set connection block
		conn.block = null; 
	}
	
	// Save full script
	conn.script = this.path+conn.response+"/"+script;
	

	// Set default parameters
	conn.params = "";

	// Return connection value
	return conn;
}


// Send AJAX request
Dealers.ajax.send = function(conn) {
	// If connection response is display, set response handler
	if(conn.response == "display") { conn.ajax.onreadystatechange = function() { Dealers.ajax.response.display(conn); } }
	
	// If connection response isn't display, set response handler
	else { conn.ajax.onreadystatechange =  function() { Dealers.ajax.response.process(conn); } }

	// If mode is post...
	if(conn.mode == "post") {
		// Open connection
		conn.ajax.open("POST", conn.script, true);
		
		// Set server headers
		conn.ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
		
		// Set server content length
		conn.ajax.setRequestHeader("Content-length", conn.params.length);
		
		// Set AJAX expiration
		this.expire(conn);

		// Set server connection
		conn.ajax.setRequestHeader("Connection", "close");
		
		// Send request
		conn.ajax.send(conn.params);
	}	
	
	// If mode is get...
	if(conn.mode == "get") {
		// Open connection
		conn.ajax.open("GET", conn.script+"?"+conn.params, true);
		
		// Set AJAX expiration
		this.expire(conn);
		
		// Send request
		conn.ajax.send(null);
	}
	
	// Return true
	return true;
}


// Set AJAX request expiration
Dealers.ajax.expire = function(conn) {
	// Set expiration headers
	conn.ajax.setRequestHeader("Cache-Control", "no-cache, must-revalidate");
	conn.ajax.setRequestHeader("Pragma", "no-cache");
	conn.ajax.setRequestHeader("Expires", "-1");
	
	// Return true
	return true;
}


// Process mode AJAX response handler
Dealers.ajax.response.process = function(conn) {
	// If server response is code 4 (done and ready)...
	if(conn.ajax.readyState == 4) { 
		// If debug mode is enabled...
		if(Dealers.debug === true) {
			// Build debug message
			var msg = "AJAX Server\n-----------------------------\n"+
			"Type:  "+conn.response+"\n"+
			"Mode:  "+conn.mode+"\n"+
			"Script:  "+conn.script+"\n\n"+
			"Response\n-----------------------------\n"+conn.ajax.responseText;
			
			// Output message
			alert(msg);
			
			// If dual mode is enabled...
			if(Dealers.dualDebug === true) {
				// If pre-finish is a function, run pre-finished function
				if(typeof conn.prefinish == 'function') { conn.prefinish(); }
				
				// Execute response as javascript
				eval(conn.ajax.responseText); 
				
				// If finish is a function, run finished function
				if(typeof conn.finish == 'function') { conn.finish(); }
			}
		}
		
		// If debug mode isn't enabled...
		else { 
			// If pre-finish is a function, run pre-finished function
			if(typeof conn.prefinish == 'function') { conn.prefinish(); }
			
			// Execute response as javascript
			eval(conn.ajax.responseText); 
			
			// If finish is a function, run finished function
			if(typeof conn.finish == 'function') { conn.finish(); }
		}
	}
	
	// Return true
	return true;
}


// Display mode AJAX response handler
Dealers.ajax.response.display = function(conn) {
	// If server response is code 4 (done and ready)...
	if(conn.ajax.readyState == 4) { 
		// If debug mode is enabled...
		if(Dealers.debug === true) {
			// Build debug message
			var msg = "AJAX Server\n-----------------------------\n"+
			"Type:  "+conn.response+"\n"+
			"Mode:  "+conn.mode+"\n"+
			"Script:  "+conn.script+"\n"+
			"Block:  "+conn.block+"\n\n"+
			"Response\n-----------------------------\n"+conn.ajax.responseText;
			
			// Output message
			alert(msg);
			
			// If dual mode is enabled...
			if(Dealers.dualDebug === true) { 
				// If pre-finish is a function, run pre-finished function
				if(typeof conn.prefinish == 'function') { conn.prefinish(); }
				
				// If transition is enabled...
				if(conn.transition == true) {
					// Try to transition
					try { 
						// Fade out content
						$("#"+conn.block).fadeOut(conn.speed, function() {
							// Set content to response
							Dealers.content(conn.block, conn.ajax.responseText);
							
							// Fade content back in in
							$("#"+conn.block).fadeIn(conn.speed);
						}); 
					}
					
					// If fade in failed, set content to response
					catch(err) { Dealers.content(conn.block, conn.ajax.responseText); }
				}
				
				// If transition isn't enabled, set content to response
				else { Dealers.content(conn.block, conn.ajax.responseText); }
				
				// If finish is a function, run finished function
				if(typeof conn.finish == 'function') { conn.finish(); }
			}
				
		}
		
		// If debug mode isn't enabled...
		else { 
			// If pre-finish is a function, run pre-finished function
			if(typeof conn.prefinish == 'function') { conn.prefinish(); }
			
			// If transition is enabled...
			if(conn.transition === true) {
				// Try to transition
				try { 
					// Fade out content
					$("#"+conn.block).fadeOut(conn.speed, function() {
						// Set content to response
						Dealers.content(conn.block, conn.ajax.responseText);
						
						// Fade content back in in
						$("#"+conn.block).fadeIn(conn.speed);
					}); 
				}
				
				// If fade in failed, set content to response
				catch(err) { Dealers.content(conn.block, conn.ajax.responseText); }
			}
			
			// If transition isn't enabled, set content to response
			else { Dealers.content(conn.block, conn.ajax.responseText); }
			
			// If finish is a function, run finished function
			if(typeof conn.finish == 'function') { conn.finish(); }
		}
	}
	
	// Return true
	return true;
}