/** 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( item_id, user_id, action )
{
	if( xmlHttp.readyState == 0 || xmlHttp.readyState == 4 )
	{
		href = "/server_request.php?item_id="+item_id+"&user_id="+user_id+"&saved_action="+action;
		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");
			obj = document.getElementById('back_ordering_text');
			var search_result = '';
			search_result = schoolnameArray.item(0).firstChild.data;
			if( search_result == 1 )
			{
				if (document.getElementById){ 
				obj = document.getElementById('add_to_wishlist'); 
				obj.style.display = ""; 
				}
			}
			else 
			{
				obj.innerHTML = 'You have problem ....';
			}
		}
		else
		{
			alert('We have problems with ' + xmlHttp.statusText );
		}
	}
}

/** Function of sending search request to server by AJAX for save current status
* @param void
* @return void
*/
/** Function of sending search request to server by AJAX
* @param void
* @return void
*/
function process2( item_id, user_id )
{
	f=document.forms.incart;
	url_add = '';
	for(i=0;i<=f.gr_count.value;i++){
		if(f.gr_ids[i].checked) url_add+="&gr_ids[]="+f.gr_ids[i].value;
	}
	if(url_add==""){
		alert("Please choose at least one registry.");
		return false;
	}
	if( xmlHttp.readyState == 0 || xmlHttp.readyState == 4 )
	{
		href = "/server_request.php?item_id="+item_id+"&user_id="+user_id+'&saved_action=add_giftregistry'+url_add;
		xmlHttp.open( "GET",href, true );
		xmlHttp.onreadystatechange = handleServerResponse2;
		xmlHttp.send(null);
	}
	else
	{
		setTimer('process()', 1000 );
	}
}


/** Handler for AJAX request
* @param void
* @return void
*/
function handleServerResponse2()
{
	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");
			obj = document.getElementById('back_ordering_text');
			var search_result = '';
			search_result = schoolnameArray.item(0).firstChild.data;
			if( search_result == 1 )
			{
				if (document.getElementById){ 
					obj = document.getElementById('add_to_registry'); 
					obj.style.display = "none"; 
					obj2 = document.getElementById('add_to_registryd'); 
					obj2.style.display = ""; 
				}
			}
			else 
			{
				obj.innerHTML = 'You have problem ....';
			}
		}
		else
		{
			alert('We have problems with ' + xmlHttp.statusText );
		}
	}
}