/**
 * Atlantic Sportswear
 *
 * @category	ASW
 * @package		Javascript
 * @copyright 	Copyright (c) 2007-2008 Kaaterskil Management, LLC
 * @version 	$Id: ajax.js 86 2009-02-16 23:58:09Z Blair $
 */

//holds an instance of XMLHTTPRequest
var xmlHttp = createXmlHttpRequestObject();

//display error messages (true) or degrade to non-Ajax behavior (false)
var show_errors = true;

//contains the link for the form clicked or submitted by the visitor
var actionObject = '';

//this is ture when checking out, otherwise false
var is_checking_out_cart	= false;
var is_checking_out_info	= false;
var is_checking_out_pmt		= false;
var is_submitting_order		= false;
var is_returning_to_cart	= false;

/**
 * Creates an XMLHTTPRequest object
 */
function createXmlHttpRequestObject(){
	var xmlHttp;
	
	//create the XMLHttpRequest
	try{
		xmlHttp = new XMLHttpRequest();
	}catch(e){
		//assume IE6 or older
		var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0', 
										'MSXML2.XMLHTTP.5.0',
										'MSXML2.XMLHTTP.4.0',
										'MSXML2.XMLHTTP.3.0',
										'MSXML2.XMLHTTP',
										'Microsoft.XMLHTTP'
										);
		//try every id until one works
		for(i = 0; i < XmlHttpVersions.length && !xmlHttp; i++){
			try{
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}catch(e){
				//ignore potential error
			}
		}
	}
	
	//return the XMLHTTPRequest object if successfully created
	if(xmlHttp){
		return xmlHttp;
	}else{
		handleError('Error creating XmlHttpRequestObject.');
	}
}

/**
 * Displays error or degrades to non-Ajax behavior
 */
function handleError(msg){
	if(showErrors){
		//display error message
		alert('Error encountered: \n' + msg);
		return false;
	}else if(!actionObject.tagName){
		//fall back to non-Ajax behavior
		return true;
	}else if(actionObject.tagName == 'A'){
		//fall back to non-Ajax behavior by following the link
		window.location = actionObject.href;
	}else if(actionObject.tagName == 'FORM'){
		//fall back to non-Ajax behavior by submitting the form
		actionObject.submit();
	}
}

/**
 * Add product to cart
 */
function addProductToCart(form, cutoff){
	//validate form
	t = test_add_to_cart(cutoff);
	if(!t){
		return false;
	}
	
	//degrade to non-Ajax behavior for checkout actions
	if(is_checking_out_cart || is_checking_out_info || is_checking_out_pmt 
			|| is_submitting_order || is_returning_to_cart){
		return true;
	}
	
	//degrade to non-Ajax behavior if XMLHttpRequest object is not available
	if(!xmlHttp){
		return true;
	}
	
	//display message
	var e = document.getElementById('cart_message');
	if(e){
		e.innerHTML = 'Updating shopping cart...';
	}
	
	//build the asynchronous url string
	request = form.action + '&AjaxRequest';
	
	//build query string with product attributes and parameters
	params		= '';
	formSelects	= form.getElementsByTagName('select');
	if(formSelects){
		for(i = 0; i < formSelects.length; i++){
			params			+= '&' + formSelects[i].name + '=';
			selected_index	= formSelects[i].selectedIndex;
			params			+= formSelects[i][selected_index].value;
		}
	}
	formInputs	= form.getElementsByTagName('input');
	if(formInputs){
		for(i = 0; i < formInputs.length; i++){
			if(formInputs[i].name.indexOf('submit') == -1){
				params += '&' + formInputs[i].name + '=' + formInputs[i].value;
			}
		}
	}
	
	//connect to server
	try{
		//continue only if XMLHttprequest isn't busy
		if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
			//make a server request
			xmlHttp.open('POST', request, true);
			xmlHttp.setRequestHeader('Content-Type', 
									 'application/x-www-form-urlencoded');
			xmlHttp.onreadystatechange = addToCartStateChange;
			xmlHttp.send(params);
		}
	}catch(e){
		handleError(e.toString());
	}
	
	//stop regular form submit if Ajax action is successful
	return false;
}

/**
 * Retrieve HTTP response
 */
function addToCartStateChange(){
	//read the server response
	if(xmlHttp.readyState == 4){
		//continue only if HTTP status is 'OK'
		if(xmlHttp.status == 200){
			try{
				updateCartSummary();
			}catch(e){
				handleError(e.toString());
			}
		}else{
			handleError(xmlHttp.statusText);
		}
	}
}

/**
 * Process server response
 */
function updateCartSummary(){
	//read the response
	response	= xmlHttp.responseText;
	
	if(response.indexOf('ERRNO') != -1 || response.indexOf('error') != -1){
		handleError(response);
	}else{
		//update the cart summary
		document.getElementById('cart_summary').innerHTML = response;
		
		//show the 'add' message
		var e = find_DOM('cart_message');
		e.innerHTML = 'Product added to cart.';
	}
}