﻿// JScript File

function openPart(delunik, avd, context) {
	var url = 'Part.aspx?delunik=' + delunik + "&avd=" + avd + "&ctx=" + context;
	popupWin = window.open(url, '_blank', 'width=700,height=842,resizable=yes,scrollbars=yes');
	popupWin.focus();
}

function addToCart(wnd, delunik) {
	if (addToCartInner(wnd, delunik)) {
		wnd.window.close();
	}
}

function addToCartInner(wnd, delunik) {
	if (wnd.window.opener == undefined) {
		return false;
	}
	var txtDelunik = wnd.window.opener.document.getElementById('RightColumn1_ShoppingCartSmall1_txtDelunik');
	var btnAddToCart = wnd.window.opener.document.getElementById('RightColumn1_ShoppingCartSmall1_btnAddToCart');
	if (btnAddToCart != null) {
		txtDelunik.value = delunik;
		btnAddToCart.click();
		wnd.window.opener.focus();
		return true;
	} else {
		return addToCartInner(wnd.window.opener, delunik);
	}
	return false;
}

function openImage(url) {
	popupWin = window.open(url, 'ImageViewer', 'width=680,height=566,resizable=yes');
}

function openCabasImage(url) {
	popupWin = window.open(url, 'ImageViewer', 'width=680,height=566,resizable=yes');
}

function viewInfo(url) {
	popupWin = window.open(url, 'ViewInfo', 'width=800,height=566,resizable=yes,scrollbars=yes');
	return null;
}

function toggleObj(objId) {

	if (objId != "") {
		if (document.getElementById(objId).style.display != 'block') {
			document.getElementById(objId).style.display = 'block';
		} else {
			document.getElementById(objId).style.display = 'none';
		}
	}
}

function makeNumeric(str) {

	var num = (str.replace(",", ".") * 1);
	if (isNaN(num)) {
		return 0;
	}
	return num;
}

function formatNumeric(num) {

	var str = (num + "").replace(".", ",");
	if (str.indexOf(",") < 0)
		str += ",00";
	if (str.length > 6) {
		return str.substr(0, str.length - 6) + " " + str.substr(str.length - 6);
	} else {
		return str;
	}
}

function bindAutoCompleteBoxes() {

	$(".searchpart").autocomplete({
		source: function (request, response) {
			$.ajax({
				url: "/AutoComplete.asmx/PartCodeCompletionList",
				data: "{ 'input': '" + request.term + "' }",
				dataType: "json",
				type: "POST",
				contentType: "application/json; charset=utf-8",
				dataFilter: function (data) { return data; },
				success: function (data) {
					response($.map(data.d, function (item) {
						return {
							// These are the values in the autocomplete list, NOT the value that was selected.
							value: item
						}
					}))
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					alert(XMLHttpRequest);
					alert(textStatus);
					alert(errorThrown);
				}
			});
		},
		minLength: 2,
		focus: function (event, ui) {
			var newValue = ui.item.label;
			newValue = newValue.replace(/ <em>\(.*\)<\/em>/, "");
			//            alert(newValue);
			$(".searchpart").val(newValue);
			return false;
		},
		select: function (event, ui) {
			var newValue = ui.item.label;
			newValue = newValue.replace(/ <em>\(.*\)<\/em>/, "");
			//            alert(newValue);
			$(".searchpart").val(newValue);
			return false;
		}

	})

    .data("autocomplete")._renderItem = function (ul, item) {
    	return $("<li></li>")
				.data("item.autocomplete", item)
				.append("<a>" + item.label + "</a>")
				.appendTo(ul);
    };

}



