/**
 * @name		core_ajax.php
 * @author		Mark Edward Tan (PHP Developer / Bodybuilder)
 * @package		ajax/core_ajax.php
 * @version		1.0
 * @since		September 19, 2007
 * @link		http://www.markedwardtan.com/
 * 
 */


/**
 * Create the XMLHttpRequest Object
 * @name	createRequestObject
 * @author	Mark Edward Tan (PHP Developer / Bodybuilder)
 * @return	xhr_object - The XML HTTP Request Object
 */
function createRequestObject(){
	var xhr_object = false;
    
    if(window.XMLHttpRequest){
		xhr_object = new XMLHttpRequest();
	} else if (window.ActiveXObject){
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	}
    return xhr_object;
}

/** 
 * The core AJAX functionality of www.markedwardtan.com
 * @name			process_ajax
 * @author			Mark Edward Tan (PHP Developer / Bodybuilder)
 *
 * @param
 * ajax_url			filename of the AJAX server file. (ie. edit_ajax.php)
 * ajax_parameters	the AJAX parameters to be processed
 * div_id			the <div> innerHTML to be changed
 * success_effects	the success ajax effects
 * failure_effects	the failure ajax effects
 */
function process_ajax(ajax_url, div_id, parameters, success_function_name, failure_function_name){	
	
	// create our global request object
	var http = createRequestObject();
	
	// for our ajax effects
	var do_effects;
	
	// proceed only if a XHR object was successfully created
	if(http){
	    // always use POST to be safe
	    http.open('POST', ajax_url, true);
	    //http.open('POST', 'processreg.php', true); // original
	    
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
		http.setRequestHeader("Content-length", parameters.length);
		http.setRequestHeader("Connection", "close");
		//http.onreadystatechange = handleResponse;
		http.onreadystatechange = function(){
			
			// show me the magic!
			if(div_id == 'shoutbox_wrapper'){
				setFade(0);
			} else if(div_id == 'gb_content'){
				setFade_guestbook(0);
			}
			
			if(http.readyState == 4 && http.status == 200){
				
				var response_array = http.responseText.split("|");
				var is_valid_response = response_array[0];
				
				// validation from PHP
				if(is_valid_response == 'FALSE'){
					var error_msg = response_array[1];
					// assign the function to be called if not passed the validation from PHP
					do_effects = failure_function_name + '("'+is_valid_response+'", "'+error_msg+'")';
					
					// execute the onFailure effects
					eval(do_effects);
					
					// show me the magic - shoutbox or guestbook innerHTML =P
					if(div_id == 'shoutbox_wrapper'){
						fade(0);
					} else if(div_id == 'gb_content'){
						fade_guestbook(0);
					}
					
				} else {
					document.getElementById(div_id).innerHTML = http.responseText;
					
					// show me the magic - shoutbox or guestbook innerHTML =P
					if(div_id == 'shoutbox_wrapper'){
						fade(0);
					} else if(div_id == 'gb_content'){
						fade_guestbook(0);
					}
					
					//alert(http.responseText);
					
					// assign the function to be called onSuccess
					do_effects = success_function_name + '()';
					
					// execute the onSuccess effects
					eval(do_effects);
				}
			} 
			
			try{ 
				if(http.status != 200) {						
					//alert(http.readyState + ' - ' + http.status);
					
					var error_status = http.status;
					// assign the function to be called onFailure
					do_effects = failure_function_name + '("'+error_status+'", "")';
					
					// execute the onFailure effects
					eval(do_effects);
				}
				
			} catch(e){
				/*
				Catch the firebug error msg: "Component returned failure code: 0x80040111 
				(NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]"
				*/
			}
			
		}
		http.send(parameters);
	}
}

/**
 * Can you feel me? Amazing isn't it? =P
 * @name	fade
 * @author	Mark Edward Tan (PHP Developer / Bodybuilder)
 */
function fade(amt) {
	if(amt <= 100) {
		setFade(amt);
		amt += 10;
		setTimeout("fade("+amt+")", 100);
    }
}

function fade_guestbook(amt) {
	if(amt <= 100) {
		setFade_guestbook(amt);
		amt += 10;
		setTimeout("fade_guestbook("+amt+")", 100);
    }
}

/**
 * My SECRET WEAPON! Galing noh? =D
 * @name	setFade
 * @author	Mark Edward Tan (PHP Developer / Bodybuilder)
 */
function setFade(amt) {
	//var obj = document.getElementById(div_id);
	var obj = document.getElementById('shoutbox_wrapper');
	
	amt = (amt == 100)?99.999:amt;
  
	// IE
	obj.style.filter = "alpha(opacity:"+amt+")";
  
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = amt/100;
  
	// Mozilla and Firefox
	obj.style.MozOpacity = amt/100;
  
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = amt/100;
}

function setFade_guestbook(amt) {
	var obj = document.getElementById('gb_content');
	
	amt = (amt == 100)?99.999:amt;
  
	// IE
	obj.style.filter = "alpha(opacity:"+amt+")";
  
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = amt/100;
  
	// Mozilla and Firefox
	obj.style.MozOpacity = amt/100;
  
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = amt/100;
}