
/**
 * Adaptation of Active Ice Shopping Cart Class for use with
 * Swagg 'Request a Quote' system
 *
 * author: Brett Reid
 * email: admin@activeice.co.za
 * updated: 11 july 2006
 *
 **/
 
var timer, loader;
var ajaxCall = '';
 
 /**
 *
 * @params none
 *
 * @constructor
 */
function AI_Cart() {
	
	this.heading = 'My Cart';
	this.cart_div = 'cart_summary';
	this.html = '';
	this.cart_items = 0;
	this.subtotal = '0.00';
	this.postage = '0.00';
	this.total = '0.00';
	this.add_item_url += '?id=';
	this.response_data = '';
	this.products = new Array();
	this.starting = 1;
	this.initialize();
}

 /**
 * Writes the cart as the page loads
 *
 * @params none
 */
AI_Cart.prototype.initialize = function() {
	
	ajaxCall = '/ajax/AI_Cart.php?do=get_data';
	
	var resultString = do_ajax_call( ajaxCall );
	
	if ( resultString.length > 1 && resultString.indexOf("AJAX Error") == -1 ) {
		var resultArray = resultString.split("|");
		for ( var i = 0; i < resultArray.length ; i++ ) {
			product = resultArray[i].split("-");
			this.add_item(product[0],product[1],false);
		}
	} else {
		if ( resultString.length > 1)
			alert(resultString);
	}

}

 /**
 * Add a product to the cart by quantity
 *
 * @param id : product id
 * [@param qty : quantity to add]
 */
AI_Cart.prototype.add_item = function(id) {
	
	// used as flag
	var found = false;
	// if 2nd argument has been passed, set flag
	var qty = ( arguments[1] ) ? arguments[1] : '';
	var run_timers = ( arguments[2] == false ) ? arguments[2] : true;
	
	if (run_timers) {
		this.resetTimer();
		this.startTimer();
	}
	
	// loop thru existing products in cart
	for (var i = 0; i < this.products.length; i++ ) {
		// if we find the product, set flag
		if ( this.products[i].id == id ) {
			found = true;
			break;
		}
	}
	
	if ( found ) {
		// if we found the product, set new quantity
		if ( qty == '' ) {
			this.products[i].quantity++;
			qty = this.products[i].quantity;
		} else {
			this.products[i].quantity = qty;
		}
	} else { // otherwise create new product in cart
		if ( qty == '' ) { qty = 1; }
		this.products[ this.products.length ] = new Product( id, qty );
	}
	
	// check element exists
	if ( document.getElementById('qty' + id) ) {// if quantity zero, clear otherwise set quantity
		document.getElementById('qty' + id).value = ( qty == 0 ) ? '' : qty;
	}
	
	this.update_cart();
}

 /**
 * Remove a product from the cart
 *
 * @param id : product id
 * @param qty : quantity to add
 */
AI_Cart.prototype.remove_item = function(id) {
	
	var qty = 0;
	var p_index = null;
	// if 2nd argument has been passed, set flag
	var del = ( arguments[1] ) ? arguments[1] : false;
	
	this.resetTimer();
	this.startTimer();
	
	// loop thru existing products in cart
	for (var i = 0; i < this.products.length; i++ ) {
		// if we find the product, remember the array index
		if ( this.products[i].id == id ) {
			p_index = i;
			break;
		}
	}
	
	// product wasnt found
	if ( p_index == null ) return;
	
	// if marked to delete or there is only 1 item left
	if ( del || this.products[p_index].quantity < 2 ) { 
		this.products.splice(p_index, 1);	// delete the item off array
	} else {
		// otherwise if quantity still above zero, decrement by one, store new quantity
		this.products[i].quantity--;
		qty = this.products[i].quantity;
	}
	
	// check element exists
	if ( document.getElementById('qty' + id) ) // if quantity zero, clear otherwise set quantity
		document.getElementById('qty' + id).value = ( qty == 0 ) ? '' : qty;
	
	this.update_cart();
}

 /**
 * Updates the cart to display new values
 *
 * @params none
 */
AI_Cart.prototype.delete_product = function(id,row_index) {
	//this.build_cart_html();
	//this.store_session_data();
	// loop thru existing products in cart
	var p_index = 'string';
	for (var i = 0; i < this.products.length; i++ ) {
		// if we find the product, remember the array index
		if ( this.products[i].id == id ) {
			p_index = i;
			break;
		}
	}
	
	if (p_index != 'string') {
		this.products.splice(p_index, 1);
		
		if ( document.getElementById("quoteTable") ) {
			document.getElementById("quoteTable").deleteRow(row_index);
		}

	}
	
	var getData = '';
	
	if ( this.products.length > 0 ) {
		getData += '&contents=';
		for (var i = 0; i < this.products.length; i++ ) {
			getData += this.products[i].id + '-' + this.products[i].quantity + '|';
		}
		getData = getData.substr(0, (getData.length - 1) );
	} else {
		if ( document.getElementById("quoteTable") )
			document.getElementById("quoteTable").style.display = 'none';
	}

	
	ajaxCall = '/ajax/AI_Cart.php?do=store_cart' + getData;
	
	this.resetTimer();
	this.startTimer();
}


 /**
 * Updates the cart to display new values
 *
 * @params none
 */
AI_Cart.prototype.update_cart = function() {
	//this.build_cart_html();
	//this.store_session_data();

	// loop thru existing products in cart
	for (var i = 0; i < this.products.length; i++ ) {
		if ( this.products[i].id == '' )
			this.products.splice(i, 1);
	}
	
	this.cart_items = this.products.length;
	
	if ( document.getElementById( this.cart_div ) ) {
		if ( this.cart_items )
			document.getElementById( this.cart_div ).innerHTML = '('+ this.cart_items+')';
	}
	var getData = '';
	
	if ( this.products.length > 0 ) {
		getData += '&contents=';
		for (var i = 0; i < this.products.length; i++ ) {
			getData += this.products[i].id + '-' + this.products[i].quantity + '|';
		}
		getData = getData.substr(0, (getData.length - 1) );
	}
	
	ajaxCall = '/ajax/AI_Cart.php?do=store_cart' + getData;
	
}

 /**
 * Used 
 *
 * @param id : product id
 * @param qty : quantity to add
 */
AI_Cart.prototype.adjust_qty = function(id, qty) {
	
	var regex = /^\d+$/;
	
	if ( qty == '' || qty == '0' || !regex.test(qty) ) {
		this.remove_item(id, true);
		if ( document.getElementById('qty' + id) )
			document.getElementById('qty' + id).value = '';
	} else {
		this.add_item(id, qty);
	}
}

 /**
 * Updates the cart to display new values
 *
 * @params none
 */
AI_Cart.prototype.startTimer = function() {
	timer = setTimeout("store_session_data()", 3000);
}

 /**
 * Updates the cart to display new values
 *
 * @params none
 */
AI_Cart.prototype.resetTimer = function() {
	clearTimeout(timer);
}

 /**
 * Updates the cart to display new values
 *
 * @params none
 */
function store_session_data() {
	clearTimeout(loader);
	var results = do_ajax_call( ajaxCall );
	hideSaving();
	showSaving();
	loader = setTimeout("hideSaving()", 1000);
}


function showSaving() {
	document.getElementById("saveIcon").style.display = 'block';
}

function hideSaving() {
	document.getElementById("saveIcon").style.display = 'none';	
}



if (typeof(ShoppingCart) != 'object') ShoppingCart = new AI_Cart();

function Product(id, qty) {
	this.id = id;
	this.quantity = qty;
}






