/** 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_compare( item_id, type )
{
	if( xmlHttp.readyState == 0 || xmlHttp.readyState == 4 )
	{
		if (document.getElementById){
			
		href = "/server_request.php?item_id="+item_id+"&type="+type;
//		alert(href);
		xmlHttp.open( "GET",href, true );
		xmlHttp.onreadystatechange = handleServerCompare;
		xmlHttp.send(null);
		}		
	}
	else
	{
		setTimer('process()', 1000 );
	}
}


function process_compare_delete( type, delete_string )
{
	if( xmlHttp.readyState == 0 || xmlHttp.readyState == 4 )
	{
		if (document.getElementById){
			
		href = "/server_request.php?type="+type+'&&'+delete_string;
//		alert(href);
		xmlHttp.open( "GET",href, true );
		xmlHttp.onreadystatechange = handleServerCompare;
		xmlHttp.send(null);
		}		
	}
	else
	{
		setTimer('process()', 1000 );
	}
}

/** Handler for AJAX request
* @param void
* @return void
*/
function handleServerCompare()
{
	if( xmlHttp.readyState == 4 )
	{
		if( xmlHttp.status == 200 )
		{
			xmlResponse = xmlHttp.responseText;
			/*
			if(!xmlResponse || !xmlResponse.documentElement)
				alert('Incorrect XML structure:\n'+xmlHttp.responseText);
			*/
			document.getElementById('selected-tovary').innerHTML = xmlResponse;
//			document.location='#top';			
//			document.anchors['top'].focus();
		}
		else
		{
			alert('We have problems with ' + xmlHttp.statusText );
		}
	}
}


function process_compare_more( item_id )
{
	if( xmlHttp.readyState == 0 || xmlHttp.readyState == 4 )
	{
		if (document.getElementById){
			
		href = "/server_request.php?item_id="+item_id+"&type=add_more";
//		alert(href);
		xmlHttp.open( "GET",href, true );
		xmlHttp.onreadystatechange = handleServerCompareMore;
		xmlHttp.send(null);
		}		
	}
	else
	{
		setTimer('process()', 1000 );
	}
}


/** Handler for AJAX request
* @param void
* @return void
*/
function handleServerCompareMore()
{
	if( xmlHttp.readyState == 4 )
	{
		if( xmlHttp.status == 200 )
		{
			xmlResponse = xmlHttp.responseText;
			/*
			if(!xmlResponse || !xmlResponse.documentElement)
				alert('Incorrect XML structure:\n'+xmlHttp.responseText);
			*/
//			document.getElementById('sravnit').innerHTML = xmlResponse;
			window.open('/compare','Compare')
		}
		else
		{
			alert('We have problems with ' + xmlHttp.statusText );
		}
	}
}

