maxNest = 4;

function getElem(id) { return document.getElementById(id); }
function getValue(id) { return getElem(id).value; }
function hasBeenFilled(text) { return (text.length > 0); }

function number_format (number, decimals, dec_point, thousands_sep) {
	var exponent = "";
	var numberstr = number.toString ();
	var eindex = numberstr.indexOf ("e");
	if (eindex > -1) {
		exponent = numberstr.substring (eindex);
		number = parseFloat (numberstr.substring (0, eindex));
	}
	
	if (decimals != null) {
		var temp = Math.pow (10, decimals);
		number = Math.round (number * temp) / temp;
	}
	var sign = number < 0 ? "-" : "";
	var integer = (number > 0 ? 
	Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
	
	var fractional = number.toString ().substring (integer.length + sign.length);
	dec_point = dec_point != null ? dec_point : ".";
	fractional = decimals != null && decimals > 0 || fractional.length > 1 ? (dec_point + fractional.substring (1)) : "";
	if (decimals != null && decimals > 0) {
		for (i = fractional.length - 1, z = decimals; i < z; ++i)
			fractional += "0";
	}
	
	thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? 
	thousands_sep : null;
	if (thousands_sep != null && thousands_sep != "") {
		for (i = integer.length - 3; i > 0; i -= 3)
			integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
	}
	
	return sign + integer + fractional + exponent;
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if (node == null) node = document;
	if (tag == null) tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function setVisibility(id, visible) {
	getElem(id).style.visibility = (visible ? 'visible' : 'hidden');
	getElem(id).style.display = (visible ? 'block' : 'none');
}
function changeVisibility(id) {
	setVisibility(id, !isVisible(id));
}
function isVisible(id) {
	return (getElem(id).style.visibility != 'hidden');
}

function ajax(url) {
	var xhr_object = null;
	if (window.XMLHttpRequest) var xhr_object = new XMLHttpRequest();
	else if (window.ActiveXObject) var xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	xhr_object.open("GET", url, false);   
	xhr_object.send(null);
	return xhr_object;
}

function dropDownChange(i, code_mother, code_brand) {
	if (code_mother != "-") {
		xhr_object = ajax(BasePath + "/shop/rt_getcats.php?code_mother="+code_mother+"&code_brand="+code_brand);
		if (xhr_object.readyState == 4 && xhr_object.responseText != "-") {
			catsArray = new Array();
			catsArray = xhr_object.responseText.split('@');
			dropdown_below = getElem("cat_level" + i);
			clearDropDown(dropdown_below);
			for(j = 0; j < catsArray.length; j++) {
				catArray = new Array();
				catArray = catsArray[j].split('#');
	 			dropdown_below.options[j+1] = new Option(catArray[1], catArray[0], false, false);
			}
			setVisibility("cat_level" + i, true)
		}
		else clearAndHideDrowDown("cat_level" + i);
		for (k = i+1; k <= maxNest; k++) clearAndHideDrowDown("cat_level" + k);
	}
	else for (k = i; k <= maxNest; k++) clearAndHideDrowDown("cat_level" + k);
}

function clearDropDown(dropdownId) {
	for(j = 1; j < dropdownId.options.length; j++) dropdownId.options[j] = null;
	dropdownId.options.length = 1;
	dropdownId.selectedIndex = 0;
}

function clearAndHideDrowDown(dropdownId) {
	clearDropDown(getElem(dropdownId));
	setVisibility(dropdownId, false);
}

function changeBrand(code_brand) {
	firstDropDown = getElem("cat_level0");
	clearDropDown(firstDropDown);
	xhr_object = ajax(BasePath + "/shop/rt_getcats.php?code_mother=0&code_brand="+code_brand);
	if (xhr_object.readyState == 4 && xhr_object.responseText != "-") {
		catsArray = new Array();
		catsArray = xhr_object.responseText.split('@');
		
		for(j = 0; j < catsArray.length; j++) {
			catArray = new Array();
			catArray = catsArray[j].split('#');
 			firstDropDown.options[j+1] = new Option(catArray[1], catArray[0], false, false);
		}
	}
	for (k = 1; k <= maxNest; k++) clearAndHideDrowDown("cat_level" + k);
}

function getCodeCat() {
	for (k = maxNest; k >= 0; k--) {
		ddk = getValue("cat_level" + k);
		if (ddk != "-") {
			//alert('k = ' + k + ', - != ddk=' + ddk);
			return ddk;
		}
		//else alert('k = ' + k + ', - = ddk=' + ddk);
	}
	//alert('null');
	return null;
}

function showProducts() {
	codeCat = getCodeCat();
	if (codeCat != null) {
		if (getValue('brand') !='-') location.href='list.php?code_cat=' + codeCat + '&code_brand=' + getValue('brand');
		else location.href='list.php?code_cat=' + codeCat;
	}
	else if (getValue('brand') !='-') location.href='list.php?code_cat=0&code_brand=' + getValue('brand');
	else location.href='list.php?code_cat=0';
}

function addToBasket(type, code_type, quantity, opt1) {
	if (quantity == null) quantity = getElem("nb_" + code_type).value;
	ajax(BasePath + "/order/rt_basket.php?type=" + type + "&code_type=" + code_type + "&quantity=" + quantity + "&opt=" + opt1);
	if (type == 'WORKSHOP' || type == 'TRAINING')
		location.href = BasePath + '/order/basket.php';
}

function removeFromFav(code) {
	ajax(BasePath + "/order/rt_delfav.php?code=" + code);
}

function showPicture(path) {
	var mypic = new Image();
	mypic.onload = function() { Tip("<img src='" + FilePath + "/" + path + "'>"); }
	mypic.src = FilePath + "/" + path;
}