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





var  gobj_regExpFloat0Plus = '(^0$)|(^\\+?[1-9][0-9]*$)|(^\\+?(0|[1-9][0-9]*)\\.[0-9]{1,3}$)';


function /*String*/ getPriceDecimalPoint (str_priceDecimalComma) {
	var  str_price = str_priceDecimalComma.replace(',', '.');
	if ('' == str_price) {
		str_price = '0';
	}
	return str_price;
}


function /*boolean*/ validateFormSearch (obj_form) {
	if (document.getElementById) {
		var  obj_fieldProductCategory = document.getElementById('SearchSelectProductCategory');
		if (0 == obj_fieldProductCategory.value) {
			// Error: No product category is selected.
			window.alert(gstr_msgErrorProductCategory);
			obj_fieldProductCategory.focus();
			return false;
		}

		// Checking prices:
		// Checking value of "price from" input field:
		var  obj_fieldPriceFrom = document.getElementById('price_from');
		var  str_priceFrom = getPriceDecimalPoint(obj_fieldPriceFrom.value);
		if (str_priceFrom.match(gobj_regExpFloat0Plus, 'g')) {
			// OK: Value of "price from" input field is correct.
			// Checking value of "price to" input field:
			var  obj_fieldPriceTo = document.getElementById('price_to');
			var  str_priceTo = getPriceDecimalPoint(obj_fieldPriceTo.value);
			if (str_priceTo.match(gobj_regExpFloat0Plus, 'g')) {
				// OK: Value of "price to" input field is correct.
				// Checking price interval:
				var  fl_priceFrom = window.parseFloat(str_priceFrom);
				var  fl_priceTo   = window.parseFloat(str_priceTo);
				if (fl_priceTo > 0 && fl_priceTo < fl_priceFrom) {
					// Error: "Price to" is less than "price from".
					// 0 < price to < price from
					window.alert(gstr_msgErrorPriceInterval);
					obj_fieldPriceTo.focus();
					return false;
				}
			} else {
				// Error: Value of "price to" input field is NOT correct.
				window.alert(gstr_msgErrorPriceToIncorrect);
				obj_fieldPriceTo.focus();
				return false;
			}
		} else {
			// Error: Value of "price from" input field is NOT correct.
			window.alert(gstr_msgErrorPriceFromIncorrect);
			obj_fieldPriceFrom.focus();
			return false;
		}
	}

	// OK: Passed validations.
	// Submitting form by JavaScript (as "x" and "y" GET parameters are added when submitting form by input type=image and they are not needed):
	self.location.href = BASE + 'search/'
			+ '?productcategory_id=' + obj_fieldProductCategory.value
			+ (fl_priceFrom > 0 ? '&price_from=' + obj_fieldPriceFrom.value : '')
			+ (fl_priceTo   > 0 ? '&price_to='   + obj_fieldPriceTo.value   : '')
			+ LANG_PAR_EFFECTIVE_AMP_SIMPLE;
	return false;
} // validateFormSearch() function


function /*boolean*/ validateFormSearchCode (obj_form) {
	if (document.getElementById) {
		if ('' == document.getElementById('SearchInputTextCode').value) {
			window.alert(gstr_msgErrorProductCodeBlank);
			document.getElementById('SearchInputTextCode').focus();
			return false;
		}
	}
	// OK: Passed validations.
	return true;
}




var  STEP_ALTER_PRICE = 0.1;


function /*boolean*/ alterPrice (bool_isPriceFrom, bool_isModeIncrease) {
	var  obj_htmlElementPrice = document.getElementById('price_' + (bool_isPriceFrom ? 'from' : 'to'));
	var  str_price = getPriceDecimalPoint(obj_htmlElementPrice.value);
	if (str_price.match(gobj_regExpFloat0Plus, 'g')) {
		// Value of current price input field is correct.
		var  fl_price = window.parseFloat(str_price);
		fl_price += (bool_isModeIncrease ? 1 : -1) * STEP_ALTER_PRICE;
		fl_price = Math.round(fl_price*1000) / 1000;
		obj_htmlElementPrice.value = getPrecision2FloatSi(Math.max(0, fl_price));
	}
	return false;
}
function /*boolean*/ increasePriceFrom () {
	return alterPrice(true, true);
}
function /*boolean*/ decreasePriceFrom () {
	return alterPrice(true, false);
}
function /*boolean*/ increasePriceTo () {
	return alterPrice(false, true);
}
function /*boolean*/ decreasePriceTo () {
	return alterPrice(false, false);
}


function /*void*/ gotoPageSearchBrand () {
	if (document.getElementById('SearchSelectBrand').value > 0) {
		document.getElementById('FormSearchBrand').submit();
	}
}


