/*
**     JavaScript Source Code
**     Created by Kalin Ganev
**     <KalinGanev [AT] Gmail (DOT) com>
**     Date Created:  2009-03-31
**     Last Modified: 2011-11-17
*/





function handleResponseAddProductsToCart () {
	if (4 == gobj_httpRequest.readyState) {
		try {
			window.eval(gobj_httpRequest.responseText);
			if (window.reloadCart) {
				// It's "Order preview" page (function reloadCart is defined).
				// Reloading content of shopping cart:
				window.reloadCart();
			}
		} catch (e) {
			// A server error arose.
			if (DEBUG) {
				window.alert('An error arose while contacting server!' + '\nDebug Information:\n' + e + '\n' + gobj_httpRequest.responseText);
			}
		}
	}
}


function /*void*/ addProductItemToCart (int_idProductItem, int_quantity) {
	// Adding language parameter to get parameters:
	// Sending a request to server using AJAX technology:
	gobj_httpRequest.open('get', BASE + 'ajax/cart_add/?productitem_id=' + int_idProductItem
			+ '&quantity=' + int_quantity + LANG_PAR_EFFECTIVE_AMP_SIMPLE, true);
	gobj_httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	gobj_httpRequest.onreadystatechange = handleResponseAddProductsToCart;
	gobj_httpRequest.send(null);
}


function /*boolean*/ onClickAddProductItemToCart (int_idPlaceProduct, int_idProductItem) {
	if (document.getElementById && (obj_fieldQuantity
			= document.getElementById('productitem_quantity_place_id' + int_idPlaceProduct + '_productitem_id' + int_idProductItem))) {
		// OK: Current quantity input field exists.
		var  int_quantity = 0;
		if ('' == obj_fieldQuantity.value) {
			int_quantity = 1;
		} else {
			if (obj_fieldQuantity.value.match(/^[0-9]+$/i)) {
				// OK: Correct quantity.
				int_quantity = window.parseInt(obj_fieldQuantity.value);
			} else {
				// Error: Incorrect quantity.
				window.alert(gstr_msgErrorProductItemQuantity2);
				obj_fieldQuantity.focus();
			}
		}
		if (int_quantity > 0) {
			window.addProductItemToCart(int_idProductItem, int_quantity);
		}
	}
} // onClickAddProductItemToCart() function


/** Called upon Ajax response after adding product(s) to shopping cart. */
function /*void*/ updatePanelShoppingCartInfo (str_htmlContent) {
	if (document.getElementById('ShoppingCartInfo')) {
		document.getElementById('ShoppingCartInfo').innerHTML = str_htmlContent;
	}
}


