/** 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_send_to_friend( item_id )
{
	if( xmlHttp.readyState == 0 || xmlHttp.readyState == 4 )
	{
		if (document.getElementById){
			
		var name = document.getElementById('name');
		var email = document.getElementById('email');
		var FName = document.getElementById('fname');
		var FEmail = document.getElementById('femail');
		var Comment = document.getElementById('comment');
		
//name, email, FName, FEmail, Comment
		var href = "/server_request.php?message_type=send_to_friend&item_id="+item_id+"&name="+name.value+"&email="+email.value+'&FName='+FName.value+'&FEmail='+FEmail.value+'&Comment='+Comment.value;
//		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");
			var obj = document.getElementById('back_ordering_text');
			var search_result = '';
			search_result = schoolnameArray.item(0).firstChild.data;
			if( search_result == 1 )
			{
				ajaxshow('#thankyou');
				hide('send2form')
			}
			else 
			{
				obj.innerHTML = 'You have problem ....';
			}
		}
		else
		{
			alert('We have problems with ' + xmlHttp.statusText );
		}
	}
}


/** Function of sending search request to server by AJAX
* @param void
* @return void
*/
function process_more_information( item_id )
{
	if( xmlHttp.readyState == 0 || xmlHttp.readyState == 4 )
	{
		if (document.getElementById){
			
		var name = document.getElementById('name');
		var email = document.getElementById('email');
		var inquiry = document.getElementById('inquiry');
		
//name, email, FName, FEmail, Comment
		var href = "/server_request.php?message_type=more_information&item_id="+item_id+"&name="+name.value+"&email="+email.value+'&inquiry='+inquiry.value;
//		alert(href);
		xmlHttp.open( "GET",href, true );
		xmlHttp.onreadystatechange = handleServerResponseMoreInfo;
		xmlHttp.send(null);
		}		
	}
	else
	{
		setTimer('process()', 1000 );
	}
}


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

function process_place_order( item_id )
{
	if( xmlHttp.readyState == 0 || xmlHttp.readyState == 4 )
	{
		if (document.getElementById){
		var FieldArray = new Array('name','attention_shipp', 'company_shipp', 'street_shipp', 'city_shipp', 'state_shipp', 'zip_shipp', 'phone_shipp', 'fax_shipp', 'attention_bill', 'company_bill', 'street_bill', 'city_bill', 'state_bill', 'zip_bill', 'phone_bill', 'fax_bill', 'email', 'quantity', 'color', 'size', 'imprint_colors', 'add_information', 'file_upload', 'in_hands_date', 'sales_rep');
	 
		 var tmpl_href = '';
		 var n = 0;
		 while(n != FieldArray.length){
//		 	alert(FieldArray[n]);
		 	var field = document.getElementById(FieldArray[n]);
		 	tmpl_href += '&'+FieldArray[n]+'='+field.value;
		 	n ++;
		 }
		var href = "/server_request.php?message_type=place_order&item_id="+item_id+tmpl_href;
//		alert(href);
		xmlHttp.open( "GET",href, true );
		xmlHttp.onreadystatechange = handleServerResponseMoreInfo;
		xmlHttp.send(null);
		}		
	}
	else
	{
		setTimer('process()', 1000 );
	}
}


/** Handler for AJAX request
* @param void
* @return void
*/
function handleServerResponseMoreInfo()
{
	if( xmlHttp.readyState == 4 )
	{
		if( xmlHttp.status == 200 )
		{
			var xmlResponse = xmlHttp.responseXML;
			if(!xmlResponse || !xmlResponse.documentElement)
				alert('Incorrect XML structure:\n'+xmlHttp.responseText);
			var xmlRoot = xmlResponse.documentElement;
			var schoolnameArray = xmlRoot.getElementsByTagName("result_name");
			var obj = document.getElementById('back_ordering_text');
			var search_result = '';
			search_result = schoolnameArray.item(0).firstChild.data;
			if( search_result == 1 )
			{
				ajaxshow('#thankyou');
				hide('send2form')
			}
			else 
			{
				obj.innerHTML = 'You have problem ....';
			}
		}
		else
		{
			alert('We have problems with ' + xmlHttp.statusText );
		}
	}
}

function process_call_back_top()
{
	if( xmlHttp.readyState == 0 || xmlHttp.readyState == 4 )
	{
		
		var name = document.getElementById('name_top');
		var company = document.getElementById('company_top');
		var phone = document.getElementById('phone_top');

		
		var href = "/server_request.php?message_type=call_me_back&name="+name.value+'&company='+company.value+'&phone='+phone.value;
//		alert(href);
		xmlHttp.open( "GET",href, true );
		xmlHttp.onreadystatechange = handleServerCallMeBackTop;
		xmlHttp.send(null);
	
	}
	else
	{
		setTimer('process()', 1000 );
	}
}


/** Handler for AJAX request
* @param void
* @return void
*/
function handleServerCallMeBackTop()
{
	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;
			var schoolnameArray = xmlRoot.getElementsByTagName("result_name");
			var obj = document.getElementById('back_ordering_text');
			var search_result = '';
			search_result = schoolnameArray.item(0).firstChild.data;
			if( search_result == 1 )
			{
				hide('cb');
				ajaxshow('#cbty');
			}
			else 
			{
				obj.innerHTML = 'You have problem ....';
			}
		}
		else
		{
			alert('We have problems with ' + xmlHttp.statusText );
		}
	}
}


function process_call_back_left()
{
	if( xmlHttp.readyState == 0 || xmlHttp.readyState == 4 )
	{
		
		var name = document.getElementById('name_left');
		var company = document.getElementById('company_left');
		var phone = document.getElementById('phone_left');

		
		var href = "/server_request.php?message_type=call_me_back&name="+name.value+'&company='+company.value+'&phone='+phone.value;
//		alert(href);
		xmlHttp.open( "GET",href, true );
		xmlHttp.onreadystatechange = handleServerCallMeBackLeft;
		xmlHttp.send(null);
	
	}
	else
	{
		setTimer('process()', 1000 );
	}
}


/** Handler for AJAX request
* @param void
* @return void
*/
function handleServerCallMeBackLeft()
{
	if( xmlHttp.readyState == 4 )
	{
		if( xmlHttp.status == 200 )
		{
			var xmlResponse = xmlHttp.responseXML;
			if(!xmlResponse || !xmlResponse.documentElement)
				alert('Incorrect XML structure:\n'+xmlHttp.responseText);
			var xmlRoot = xmlResponse.documentElement;
			var schoolnameArray = xmlRoot.getElementsByTagName("result_name");
			var obj = document.getElementById('back_ordering_text');
			var search_result = '';
			search_result = schoolnameArray.item(0).firstChild.data;
			if( search_result == 1 )
			{
				hide('cb');
				ajaxshow('#cbty');
			}
			else 
			{
				obj.innerHTML = 'You have problem ....';
			}
		}
		else
		{
			alert('We have problems with ' + xmlHttp.statusText );
		}
	}
}


function process_call_back_right()
{
	if( xmlHttp.readyState == 0 || xmlHttp.readyState == 4 )
	{
		
		if (document.getElementById){
		var name = document.getElementById('name_right');
		var company = document.getElementById('company_right');
		var phone = document.getElementById('phone_right');
		}
		
		var href = "/server_request.php?message_type=call_me_back&name="+name.value+'&company='+company.value+'&phone='+phone.value;
//		alert(href);
		xmlHttp.open( "GET",href, true );
		xmlHttp.onreadystatechange = handleServerCallMeBackRight;
		xmlHttp.send(null);
	
	}
	else
	{
		setTimer('process()', 1000 );
	}
}


/** Handler for AJAX request
* @param void
* @return void
*/
function handleServerCallMeBackRight()
{
	if( xmlHttp.readyState == 4 )
	{
		if( xmlHttp.status == 200 )
		{
			xmlResponse = xmlHttp.responseXML;
			if(!xmlResponse || !xmlResponse.documentElement)
				alert('Incorrect XML structure:\n'+xmlHttp.responseText);
			var xmlRoot = xmlResponse.documentElement;
			var schoolnameArray = xmlRoot.getElementsByTagName("result_name");
			var obj = document.getElementById('back_ordering_text');
			var search_result = '';
			search_result = schoolnameArray.item(0).firstChild.data;
			if( search_result == 1 ){
				hide('cb1');
//				ajaxshow('#cbty1');
				show('cbty1');
			}
			else{
				obj.innerHTML = 'You have problem ....';
			}
		}
		else
		{
			alert('We have problems with ' + xmlHttp.statusText );
		}
	}
}


function process_product_table( divId, prodId )
{
	if( xmlHttp.readyState == 0 || xmlHttp.readyState == 4 )
	{
		
		document.getElementById(divId).innerHTML = '<br />Loading Price - please wait <br /> <br /> <br />';
		
		var href = "/server_request_products.php?message_type=create_prod_table&prod_id="+prodId;
		xmlHttp.open( "GET",href, true );
		xmlHttp.onreadystatechange = handleServerProductTable;
		xmlHttp.send(null);
	
	}
	else
	{
		setTimer('process()', 1000 );
	}
}


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



function process_newsletter()
{
	if( xmlHttp.readyState == 0 || xmlHttp.readyState == 4 )
	{
		if (document.getElementById){
			var newsletter = document.getElementById('newsletter');
		}
		else{
			alert('Not support this method.');
		}
		var patterns = new Array();
			
		patterns['email'] = /^[^@\s]+@([-a-z0-9\u00c0-\u00ff]+\.)+[a-z]{2,}$/i;
		if(!newsletter.value.match(patterns['email'])) {
			alert("Please fill out your e-mail address");
			newsletter.focus();
			return;
		}
				
		
		var href = "/server_request.php?message_type=process_newsletter&email="+newsletter.value;
//		alert(href);
		xmlHttp.open( "GET",href, true );
		xmlHttp.onreadystatechange = handleServerNewsletter;
		xmlHttp.send(null);
	
	}
	else
	{
		setTimer('process()', 1000 );
	}
}


/** Handler for AJAX request
* @param void
* @return void
*/
function handleServerNewsletter(){
	if( xmlHttp.readyState == 4 )
	{
		if( xmlHttp.status == 200 )
		{
			xmlResponse = xmlHttp.responseXML;
			if(!xmlResponse || !xmlResponse.documentElement)
				alert('Incorrect XML structure:\n'+xmlHttp.responseText);
			var xmlRoot = xmlResponse.documentElement;
			var schoolnameArray = xmlRoot.getElementsByTagName("result_name");
			var obj = document.getElementById('back_ordering_text');
			var search_result = '';
			search_result = schoolnameArray.item(0).firstChild.data;
			if( search_result == 1 ){
				hide('nl');
				ajaxshow('#nlty');
			}
			else{
//				alert(search_result);
				if (document.getElementById){
					hide('nl');
					show('nlty');
					var obj_error = document.getElementById('nlty');
					obj_error.innerHTML = 'Error from Constant Contact ....'+search_result;
				}
//				obj.innerHTML = 'You have problem ....';
			}
		}
		else{
			
			alert('We have problems with ' + xmlHttp.statusText );
			
		}
	}
}