/** Module AJAX data processing module
 * 
 * @author John Smith <ai@phpagency.net>
 * @copyright Copyright (c) 2008, John Smith
 * @version 1.2
 * @link http://championfanwear.com
 */


var xmlHttp = createXmlRequestObject();

/** Function of initialization of XMLHttpRequest object
* Function find the latest version of XMLHttpRequest object
* @return xmlHttp object
*/
function createXmlRequestObject()
{
	var xmlHttp;
	try
	{
		xmlHttp = new XMLHttpRequest();
		
	}
	catch(e)
	{
		var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
										'MSXML2.XMLHTTP.5.0',
										'MSXML2.XMLHTTP.4.0',
										'MSXML2.XMLHTTP.3.0',
										'MSXML2.XMLHTTP',
										'Microsoft.XMLHTTP');
		for( var i=0; i < XmlHttpVersions.length && !xmlHttp; i ++ )
		{
			try
			{
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch(e){}
		}
	}
	if ( !xmlHttp )
		alert('Error creation XMLHttpRequest');
	else 
		return xmlHttp;	
}


/** Function of sending search request to server by AJAX
* @param void
* @return void
*/
function process_license_info( )
{
	if( xmlHttp.readyState == 0 || xmlHttp.readyState == 4 ){
		var email_addr;
		var first_name;
		var last_name;
		var addr;
		var city;
		var state;
		var business_name;
		var profession;
		var web_site;
		var b_phone;
		var a_phone;
		if (document.getElementById){
			obj_main = document.getElementById('email_addr');
			email_addr = obj_main.value;
			obj_main = document.getElementById('first_name');
			first_name = obj_main.value;
			obj_main = document.getElementById('last_name');
			last_name = obj_main.value;
			obj_main = document.getElementById('addr');
			addr = obj_main.value;
			obj_main = document.getElementById('city');
			city = obj_main.value;
			obj_main = document.getElementById('state');
			state = obj_main.value;
			obj_main = document.getElementById('business_name');
			business_name = obj_main.value;
			obj_main = document.getElementById('profession');
			profession = obj_main.value;
			obj_main = document.getElementById('web_site');
			web_site = obj_main.value;
			obj_main = document.getElementById('b_phone');
			b_phone = obj_main.value;
			obj_main = document.getElementById('a_phone');
			a_phone = obj_main.value;
		}
		action = 'add_to_session';
//		var combination_id_temp = '';
		href = "/server_request.php?email_addr="+email_addr+"&first_name="+first_name+"&last_name="+last_name+"&addr="+addr+"&city="+city+"&state="+state+"&business_name="+business_name+"&profession="+profession+"&web_site="+web_site+"&b_phone="+b_phone+"&a_phone="+a_phone+"&action="+action;
//		alert(href);
		xmlHttp.open( "GET",href, true );
		xmlHttp.onreadystatechange = handleServerResponse;
		xmlHttp.send(null);
	}
	else{
		setTimer('process()', 1000 );
	}
}


/** Handler for AJAX request
* @param void
* @return void
*/
function handleServerResponse()
{
	if( xmlHttp.readyState == 4 )
	{
		if( xmlHttp.status == 200 )
		{
			xmlResponse = xmlHttp.responseXML;
			if(!xmlResponse || !xmlResponse.documentElement)
				alert('Incorrect XML structure:\n'+xmlHttp.responseText);
			xmlRoot = xmlResponse.documentElement;
			schoolnameArray = xmlRoot.getElementsByTagName("result_name");
			grandtotalArray = xmlRoot.getElementsByTagName("grandtotal");
			totalArray = xmlRoot.getElementsByTagName("total");
			itemArray = xmlRoot.getElementsByTagName("item");
			obj = document.getElementById('back_ordering_text');
			var search_result = '';
			search_result = schoolnameArray.item(0).firstChild.data;
			var grandtotal = ''; 
			var total = '';
			grandtotal = grandtotalArray.item(0).firstChild.data;
			total = totalArray.item(0).firstChild.data;
			item_id = itemArray.item(0).firstChild.data;
			if( search_result == 1 )
			{
				if (document.getElementById){
				obj = document.getElementById('grandtotal');
				obj.innerHTML = grandtotal;
				
				obj2 = document.getElementById('total');
				obj2.innerHTML = total;
				
				obj3 = document.getElementById('item_id'+item_id);
				obj3.style.display = "none";
				}
			}
			else 
			{
				alert('You can not delete this item now. Please try again...');
//				obj.innerHTML = 'You have problem ....';
			}
		}
		else
		{
			alert('We have problems with ' + xmlHttp.statusText );
		}
	}
}
