/*
**     JavaScript Source Code
**     Created by Kalin Ganev
**     <KalinGanev [AT] Gmail (DOT) com>
**     Date Created:  2008-06-15
**     Last Modified: 2010-06-26
*/





/**
  If finds prices of all product items associated with given product type id,
  multiplicates them to quantities selected by user and sums the result.
*/
function /*float*/ calculatePricePrefinal (int_idProductType) {
	var  obj_form = document.getElementById('FormMenu');
	var  obj_regExpPrices = new RegExp('^price_type'+int_idProductType+'_', 'g');
	var  fl_pricePrefinal = 0;

	// Looping through all form fields:
	for (i=0; i<obj_form.length; i++) {
		if (obj_form[i].id.match(obj_regExpPrices)) {
			// OK: Found a "price" hidden input field (a product item price).
			// Finding a "quantity" select-box corresponding to current product item:
			str_parsCurr = obj_form[i].id.substring(5, obj_form[i].id.length);
			obj_selectBoxQtyCurr = document.getElementById('qty' + str_parsCurr);
			if (obj_selectBoxQtyCurr) {
				// OK: A "quantity" select-box corresponding to current product item exists.
				// Calculating price of current product item according to its quantity
				// and adding the result to prefinal price:
				fl_pricePrefinal += parseFloat(obj_form[i].value) * parseInt(obj_selectBoxQtyCurr.value);
			}
		}
	} // for - looping through all form fields

	return fl_pricePrefinal;
} // calculatePricePrefinal() function


function /*void*/ printPricePrefinal (int_idProductType) {
	obj_spanPricePrefinal = document.getElementById('price_prefinal_type' + int_idProductType);
	if (obj_spanPricePrefinal) {
		obj_spanPricePrefinal.innerHTML = getPrecision2FloatSi(calculatePricePrefinal(int_idProductType));
	}
}



function /*void*/ setVisibilityImgLoading (int_idProductType, bool_isVisible) {
	if (obj_img = document.getElementById('ImgLoading' + int_idProductType)) {
		// OK: Current "loading" image exists.
		obj_img.style.visibility = bool_isVisible ? 'visible' : 'hidden';
	}
}


function /*void*/ handleResponseAddProductsToCart () {
	if (4 == gobj_httpRequest.readyState) {
		try {
			window.eval(gobj_httpRequest.responseText);
		} catch (e) {
			// A server error arose.
			if (DEBUG) {
				window.alert('An error arose while contacting server!' + '\nDebug Information:\n' + gobj_httpRequest.responseText);
			}
		}
	}
}


function /*void*/ addProductsToCart (int_idProductType) {
	// Showing "loading" icon:
	window.setVisibilityImgLoading(int_idProductType, true);
	// Getting positive quantities for all product items associated with current product type:
	var  obj_form = document.getElementById('FormMenu');
	var  obj_regExpQuantities = new RegExp('^qty_type'+int_idProductType+'_', 'g');
	var  str_parametersProductItems = '';

	// Looping through all form fields:
	for (i=0; i<obj_form.length; i++) {
		if (obj_form[i].id.match(obj_regExpQuantities)) {
			// OK: Found a "quantity" select-box (a product item quantity) associated with current product type.
			if (parseInt(obj_form[i].value) > 0) {
				// OK: Current product item quantity is greater that 0.
				// Getting ID of current product item:
				str_idProductItemCurr = obj_form[i].id.substring(obj_form[i].id.indexOf('_item')+5, obj_form[i].id.length);
				str_parametersProductItems += '' == str_parametersProductItems ? '?' : '&';
				str_parametersProductItems += 'productitem_ids[]=' + str_idProductItemCurr + '&productitem_quantities[]=' + obj_form[i].value;
				// Resetting current "quantity" select-box and prefinal price caption:
				obj_form[i].selectedIndex = 0;
				document.getElementById('price_prefinal_type'+int_idProductType).innerHTML = '0,00';
			}
		}
	} // for - looping through all form fields

	if ('' == str_parametersProductItems) {
		// Error: Product item quantities are NOT selected.
		window.alert(gstr_msgErrorNoQuantities);
		// Hiding "loading" icon:
		window.setVisibilityImgLoading(int_idProductType, false);
	} else {
		// OK: Some product item quantities are selected.
		// Adding language parameter to get parameters:
		str_parametersProductItems += LANG_PAR_EFFECTIVE_AMP_SIMPLE;
		// Sending a request to server using AJAX technology:
		gobj_httpRequest.open('get', BASE + 'ajax/cart-add/' + str_parametersProductItems, true);
		gobj_httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		gobj_httpRequest.onreadystatechange = handleResponseAddProductsToCart;
		gobj_httpRequest.send(null);
	}
} // addProductsToCart() function


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




if (document.images) {
	var  gobj_imageAdded = new Image();
	gobj_imageAdded.src = BASE + 'images/added.gif';
}


function /*void*/ displayImageAdded (int_idProductType) {
	// Hiding "loading" icon:
	window.setVisibilityImgLoading(int_idProductType, false);
	if (document.getElementById) {
		if (document.images) {
			document.getElementById('ImgAdded' + int_idProductType).src = gobj_imageAdded.src;
		}
	}
}

