/* general functions */
	function getWidth(elemId) {
		return getObj(elemId).offsetWidth;
	}
	
	function getHeight(elemId) {
		return getObj(elemId).offsetHeight;
	}

	function erase(elemId) {
		getObj(elemId).value = '';
	}
	
	function completeIfNotNull(elemId, value) {
		if (! getObj(elemId).value ) {
			getObj(elemId).value = value;
		}
	}

	function show(elemId) {
		var elem = getObj(elemId);
		
		if ( elem ) {
			elem.className = 'visible';
		}
	}
	
	function hide(elemId) {
		var elem = getObj(elemId);
		
		if ( elem ) {
			elem.className = 'invisible';
		}
	}

	function findPosX(obj)
	{
		var curleft = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curleft += obj.offsetLeft
				obj = obj.offsetParent;
			}
		}
		else if (obj.x)
			curleft += obj.x;
		return curleft;
	}
	
	function findPosY(obj)
	{
		var curtop = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curtop += obj.offsetTop
				obj = obj.offsetParent;
			}
		}
		else if (obj.y)
			curtop += obj.y;
		return curtop;
	}
	
	
	function move(obj, x, y) {
		obj.style.position = 'absolute';
		obj.style.top = y + 'px';
		obj.style.left = x + 'px';
		obj.style.display = '';
	}
	
	function getObj(elemId) {
		return document.getElementById(elemId);
	}
	
	function clientWidth() {
		return filterResults (
			window.innerWidth ? window.innerWidth : 0,
			document.documentElement ? document.documentElement.clientWidth : 0,
			document.body ? document.body.clientWidth : 0
		);
	}
	
	function clientHeight() {
		return filterResults (
			window.innerHeight ? window.innerHeight : 0,
			document.documentElement ? ( document.documentElement.clientHeight > 100 ? document.documentElement.clientHeight : 0 ) : 0,
			document.body ? ( document.body.clientHeight > 100 ? document.body.clientHeight : 0 ) : 0
		);
	}
	
	function scrollLeft() {
		return filterResults (
			window.pageXOffset ? window.pageXOffset : 0,
			document.documentElement ? document.documentElement.scrollLeft : 0,
			document.body ? document.body.scrollLeft : 0
		);
	}
	
	function scrollTop() {
		return filterResults (
			window.pageYOffset ? window.pageYOffset : 0,
			document.documentElement ? document.documentElement.scrollTop : 0,
			document.body ? document.body.scrollTop : 0
		);
	}
	
	function filterResults(n_win, n_docel, n_body) {
		var n_result = n_win ? n_win : 0;
		if (n_docel && (!n_result || (n_result > n_docel)))
			n_result = n_docel;
		return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
	}
	
	function urlencode(s) {
		s = escape(s);
		/*
		while ( s.indexOf('/') != -1 ) {
			s = s.replace('/', '%' + "/".charCodeAt(0)); // %47
		}
		
		while ( s.indexOf('+') != -1 ) {
			s = s.replace('+', '%' + " ".charCodeAt(0)); // $20
		}
		*/
		return s;
	}


/* change or restore an element's css class*/

	function changeClass(elem, newClass) {
		if (! elem.oldClass ) {
			elem.oldClass = elem.className;
		}
		
		elem.className = newClass;
	}
	
	function restoreClass(elem) {
		elem.className = elem.oldClass;
	}




/* transparency functions */

	var ie = (document.all) ? 1 : 0;
	var p = (ie) ? "filter" : "MozOpacity";
	
	function setOpacity(element, opacity) {
		v = (ie) ? "alpha(opacity:"+opacity+")" : opacity/100;
		element.style[p] = v;
		element.style['opacity'] = v;
	}


/* dialogs functions */
	function dialogSimpleDraw() {
		var content = 
			''
			+ '<div class="dialogHidden" id="' + this.elemId + '">'
				+ '<div class="dialogContent">'
					+ '<a class="close" href="javascript:void(0)" onclick="hideDialog(\'' + this.elemId + '\')">'
					+ '</a>'
					+ '	<div class="content" id="' + this.elemId + 'Content">'
					+ '	</div>'
				+ '</div>'
			+ '</div>'
		;
		
		return content;
	}


/* search dialog */

	function searchDialogOnLoad() {
		this.server.__draw();
		
		this.onShow();
	}
	
	function searchDialogOnShow() {
		if ( this.loaded ) {
			getObj('advancedSearchInput').value = this.data['searchText'];
		}
	}
	
	function showSearchDialog() {
		var searchText = getObj('keywords').value;
		
		dialog = getDialog('advancedSearchDialog123');
		
		//dialog.debug = true;
		
		dialog.data = new Array();
		dialog.data['searchText'] = searchText;
		
		dialog.setSize(400, 350);
		
		dialog.setPosition(-40, findPosY(getObj('advancedSearch'))-175);
		
		dialog.onLoad = searchDialogOnLoad;
		
		dialog.onShow = searchDialogOnShow;

		dialog.load('/ajax_advanced_search.php');
		
		dialog.show();
	}




// product dialog

	function productDialogOnLoad() {
		this.server.__draw();
	}
	
	function productDialogOnBeforeShow() {
		if ( Dialog.productDialogOpen && Dialog.productDialogOpen != this.elemId ) {
			//hideDialog(Dialog.productDialogOpen);
		}
		
		Dialog.productDialogOpen = this.elemId;
		
		return true;
	}
		
	function productDialogOnBeforeHide() {
		Dialog.productDialogOpen = null;
		
		return true;
	}
	
	Dialog.productDialogOpen = null;
	
	function showProductDialog(productId) {
		width = 550;
		height = 450;
		
		//alert( scrollLeft() + ', ' + scrollTop() );
		//alert( clientWidth() + ', ' + clientHeight() );
		
		dialog = getDialog('productDescription' + productId);
		dialog.setSize(width, height);
		
		//dialog.debug = true;
		
		dialog.onBeforeShow = productDialogOnBeforeShow;
		dialog.onBeforeHide = productDialogOnBeforeHide;
		
		dialog.setPosition(
			scrollLeft() + clientWidth()/2 - width/2,
			scrollTop() + clientHeight()/2 - width/2 + 100
		);
		
		dialog.load('/ajax_product_info.php?products_id=' + productId);
		
		dialog.onLoad = productDialogOnLoad;
		
		dialog.show();
	}
	
	function showCompareDialog(productId) {
		width = 1000;
		height = 550;
		
		//alert( scrollLeft() + ', ' + scrollTop() );
		//alert( clientWidth() + ', ' + clientHeight() );
		
		dialog = getDialog('compareBox' + productId);
		dialog.setSize(width, height);
		
		//dialog.debug = true;
		
		dialog.onBeforeShow = productDialogOnBeforeShow;
		dialog.onBeforeHide = productDialogOnBeforeHide;
		
		dialog.setPosition(
			scrollLeft() + clientWidth()/2 - width/2,
			scrollTop() + clientHeight()/2 - width/2 + 100
		);
		
		dialog.load('/ajax_product_compare.php?products_id=' + productId);
		
		dialog.onLoad = productDialogOnLoad;
		
		dialog.show();
	}
	
// information dialog
	Dialog.popupOpen = null;

	function informationDialogOnLoad() {
		this.server.__draw();
	}
	
	function informationDialogOnBeforeShow() {
		if ( Dialog.popupOpen && Dialog.popupOpen != this.elemId ) {
			hideDialog(Dialog.popupOpen);
		}
		
		Dialog.popupOpen = this.elemId;
		
		return true;
	}
		
	function informationDialogOnBeforeHide() {
		if ( dialog.hideTimer ) {
			clearTimeout( dialog.hideTimer );
			dialog.hideTimer = null;
		}
		
		Dialog.popupOpen = null;
		
		return true;
	}
	
	function showInformationDialog(elemId, left, top, width, height) {
		//width = 140;
		//height = 200;
	
		window.status = elemId;
		dialog = getDialog(elemId);

		if ( dialog.hideTimer ) {
			clearTimeout( dialog.hideTimer );
			dialog.hideTimer = null;
		}
		
		if ( dialog.visible ) {
			return false;
		}
		
		dialog.setSize(width, height);
		
		//dialog.debug = true;
		
		dialog.onBeforeShow = informationDialogOnBeforeShow;
		dialog.onBeforeHide = informationDialogOnBeforeHide;
		
		dialog.setPosition(
			findPosX( getObj('customerMenu') ) + 55,
			findPosY( getObj('customerMenu') ) + 20
		);
		
		dialog.onLoad = informationDialogOnLoad;
		
		dialog.show();
	}
	
	function hideInformationDialog(name) {
		dialog = getDialog(name);
		
		if ( dialog.hideTimer ) {
			clearTimeout( dialog.hideTimer );
			dialog.hideTimer = null;
		}

		window.status = "hideDialog('" + name + "')";
	
		dialog.hideTimer = setTimeout(
			"hideDialog('" + name + "')", 
			500
		);
	}
	
	
// customer info dialog

	function customerInfoDialogOnLoad() {
		this.server.__draw();
	}
	
	function customerInfoDialogOnBeforeShow() {
		if ( Dialog.popupOpen && Dialog.popupOpen != this.elemId ) {
			hideDialog(Dialog.popupOpen);
		}
		
		Dialog.popupOpen = this.elemId;
		
		return true;
	}
		
	function customerInfoDialogOnBeforeHide() {
		if ( dialog.hideTimer ) {
			clearTimeout( dialog.hideTimer );
			dialog.hideTimer = null;
		}
		
		Dialog.popupOpen = null;
		
		return true;
	}
	
	function showCustomerInfoDialog() {
		width = 150;
		height = 130;
		
		dialog = getDialog('customerInfoDialog');
		
		if ( dialog.hideTimer ) {
			clearTimeout( dialog.hideTimer );
			dialog.hideTimer = null;
		}
		
		if ( dialog.visible ) {
			return false;
		}
		
		dialog.setSize(width, height);
		
		//dialog.debug = true;
		
		dialog.onBeforeShow = customerInfoDialogOnBeforeShow;
		dialog.onBeforeHide = customerInfoDialogOnBeforeHide;
		
		dialog.setPosition(
			findPosX( getObj('customerMenu') ) - 45,
			findPosY( getObj('customerMenu') ) + 20
		);
		
		//dialog.load('/ajax_product_info.php');
		
		//dialog.onLoad = customerInfoDialogOnLoad;
		
		dialog.show();
	}
	
	function hideCustomerInfoDialog() {
		dialog = getDialog('customerInfoDialog');
		
		if ( dialog.hideTimer ) {
			clearTimeout( dialog.hideTimer );
			dialog.hideTimer = null;
		}
	
		dialog.hideTimer = setTimeout("hideDialog('customerInfoDialog')", 500);
	}
	
	
// filter dialogs

	function filterDialogOnBeforeShow() {
		if ( Dialog.popupOpen && Dialog.popupOpen != this.elemId ) {
			hideDialog(Dialog.popupOpen);
		}
		
		Dialog.popupOpen = this.elemId;
		
		return true;
	}
	
	function filterDialogOnBeforeHide() {
		if ( dialog.hideTimer ) {
			clearTimeout( dialog.hideTimer );
			dialog.hideTimer = null;
		}
		
		Dialog.popupOpen = null;
		
		return true;
	}
	
	function filterDialogOnShow() {
		//alert(width + ' ' + height);
	}
	
	function showFilterDialog(linkToDiv, contentDiv, width, height) {
		if (! width ) {
			width = 230;
		}
		
		if (! height ) {
			height = 300;
		}
		
		dialog = getDialog(contentDiv);
		
		if ( dialog.hideTimer ) {
			clearTimeout( dialog.hideTimer );
			dialog.hideTimer = null;
		}
		
		if ( dialog.visible ) {
			return false;
		}
		
		//dialog.debug = true;
		
		if (! dialog.data['sizeInfo'] ) {
			// caching the inner content size
			dialog.data['sizeInfo'] = new Array();
			
			//this.elem.style.width = width + 'px';
			this.elem.style.height = height + 'px';
			setOpacity(this.elem, 0);
			this.elem.className = 'dialog';
			
			dialog.data['sizeInfo']['width'] = getWidth(contentDiv + 'Content');
			dialog.data['sizeInfo']['height'] = getHeight(contentDiv + 'Content');
			
			
			setOpacity(this.elem, 100);
			this.elem.className = 'dialogHidden';
			
			//alert(this.sizeInfo['width'] + ' ' + this.sizeInfo['height']);
		}
		
		width = Math.max(width, dialog.data['sizeInfo']['width']);
		height = Math.min(height, dialog.data['sizeInfo']['height']);
		
		dialog.setSize(width, height);
		
		dialog.steps = 4;
		dialog.onBeforeShow = filterDialogOnBeforeShow;
		dialog.onBeforeHide = filterDialogOnBeforeHide;
		
		dialog.data['contentDiv'] = contentDiv;
		dialog.onShow = filterDialogOnShow;
		
		dialog.setPosition(
			findPosX( getObj(linkToDiv) ),
			70
		);
		
		dialog.steps = 2;
		
		//dialog.load('/ajax_product_info.php');
		
		//dialog.onLoad = customerInfoDialogOnLoad;
		
		dialog.show();
	}
	
	function hideFilterDialog(contentDiv) {
		dialog = getDialog(contentDiv);
		
		if ( dialog.hideTimer ) {
			clearTimeout( dialog.hideTimer );
			dialog.hideTimer = null;
		}
	
		dialog.hideTimer = setTimeout("hideDialog('" + contentDiv + "')", 500);
	}
	
	/* product type popup */
	function showFilterProductTypeDialog() {
		showFilterDialog('filterProductTypeMenu', 'filterProductTypeDialog', 150, 250);
	}
	
	function hideFilterProductTypeDialog() {
		hideFilterDialog('filterProductTypeDialog');
	}
	
	/* product manufacturer popup */
	function showFilterProductManufacturerDialog() {
		showFilterDialog('filterProductManufacturerMenu', 'filterProductManufacturerDialog', 200, 250);
	}
	
	function hideFilterProductManufacturerDialog() {
		hideFilterDialog('filterProductManufacturerDialog');
	}
	
	/* product author popup */
	function showFilterProductAuthorDialog() {
		showFilterDialog('filterProductAuthorMenu', 'filterProductAuthorDialog', 200, 250);
	}
	
	function hideFilterProductAuthorDialog() {
		hideFilterDialog('filterProductAuthorDialog');
	}
	
	
/* on load */
	function setAuthorsBox() {
		if ( getObj('products_authorsBox') ) {
			var authorsBox = new Server('ajax_authors.php');
			authorsBox.load('', getObj('products_authorsBox'));
		}
	}
	
	//onLoadList.push(setAuthorsBox);
	
	function pageOnLoad() {
		for (i in onLoadList) {
			onLoadList[i]();
		}
	}
	
	
/* add to wishlist */
	// wishlist dialog

	function addToWishlist(productId, noCheck) {
		var productList;
		
		if (! noCheck ) {
			var productListInput = getObj('attributes_list_' + productId);
			
			if (! productListInput ) {
				// probabil suntem intr-un listing, deschidem dialogul de informatii produs
				showProductDialog(productId);
				return;
			}
			
			productList = productListInput.value;
		}
		else {
			productList = '';
		}
		
		// daca exista lista de atribute, putem sa adaugam direct in wishlist
		
		var attributes = productList.split(',');
		
		var params = 'action=add_to_wishlist&products_id=' + productId;
		
		for (i in attributes)
		if ( parseInt('0' + attributes[i], 10) ) {
			var select = getObj('attr_' + attributes[i]);
			params += '&id[' + attributes[i] + ']=' + select.value;
		}
		
		width = 550;
		height = 450;
		
		dialog = getDialog('addToWishList' + productId);
		dialog.setSize(width, height);
		
		//dialog.debug = true;
		
		dialog.onBeforeShow = productDialogOnBeforeShow;
		dialog.onBeforeHide = productDialogOnBeforeHide;
		
		dialog.setPosition(
			scrollLeft() + clientWidth()/2 - width/2,
			scrollTop() + clientHeight()/2 - width/2 + 100
		);
		
		dialog.load('/ajax_wishlist.php?' + params);
		
		dialog.onLoad = productDialogOnLoad;
		
		dialog.show();
		
		//alert(params);
	}
	
	
/* add to cart */
	function confirmationDialogOnLoad() {
		this.server.__draw();
	}
	
	function confirmationDialogOnBeforeShow() {
		if ( this.hideTimer ) {
			clearTimeout(this.hideTimer);
		}
		
		return true;
	}
	
	function confirmationDialogOnShow() {
		this.hideTimer = setTimeout("hideDialog('" + this.elemId + "')", 5000);
	}
		
	function confirmationDialogOnBeforeHide() {
		//this.elem.innerHTML = '';

		if ( this.hideTimer ) {
			clearTimeout(this.hideTimer);
		}
		
		return true;
	}

	/*
	function addToCart(productId) {
		if ( window.top.cart_content ) {
			window.top.cart_content.update();
		}
		
		//productId = parseInt('0'+productId, 10);
		
		productId = 0;
		
		width = 600;
		height = 200;
		
		dialog = getDialog('addToCart' + productId, 'auxDialogs', dialogSimpleDraw);

		dialog.setSize(width, height);
		
		//dialog.drawInto = 'auxDialogs';
		
		//dialog.debug = true;
		
		//dialog.draw = dialogSimpleDraw;
		//dialog.redraw();
		
		//alert( getObj('addToCart' + productId).innerHTML );
		
		dialog.transparency = true;
		dialog.onBeforeShow = confirmationDialogOnBeforeShow;
		dialog.onShow = confirmationDialogOnShow;
		dialog.onBeforeHide = confirmationDialogOnBeforeHide;
		dialog.onLoad = confirmationDialogOnLoad;
		
		var left = scrollLeft() + clientWidth()/2 - width/2;
		var top = scrollTop() + clientHeight()/2 - height/2;
		
		dialog.setPosition(left, top);
		
		dialog.load('/ajax_product_added.php?products_id=' + productId);
		
		dialog.show();
	}
	*/
	
	
	function checkTermsCheckbox()
	{
		var ret=true;
		if(!document.getElementById("confirmTerms").checked)
		{
			ret=false;
			alert('Confirmati acordul pentru timpul de livrare!');
		}
		return ret;
	}
	

	
/* print order */
	function printOrderDialogOnLoad() {
		this.server.__draw();
	}
	
	function printOrderDialogOnBeforeShow() {
		return true;
	}
	
	function printOrderDialogOnShow() {
	}
		
	function printOrderDialogOnBeforeHide() {
		//this.elem.innerHTML = '';

		return true;
	}

	function printOrder() {
		width = 600;
		height = 550;
		
		dialog = getDialog('printOrder', 'auxDialogs');

		dialog.setSize(width, height);
		
		dialog.transparency = true;
		dialog.onBeforeShow = printOrderDialogOnBeforeShow;
		dialog.onShow = printOrderDialogOnShow;
		dialog.onBeforeHide = printOrderDialogOnBeforeHide;
		dialog.onLoad = printOrderDialogOnLoad;
		
		var left = scrollLeft() + clientWidth()/2 - width/2;
		var top = scrollTop() + clientHeight()/2 - height/2;
		
		dialog.setPosition(left, top);
		
		dialog.load('/ajax_print_order.php');
		
		dialog.show();
	}
	
	
	


// random information dialog

	function infoDialogOnLoad() {
		this.server.__draw();
	}
	
	function infoDialogOnBeforeShow() {
		if ( Dialog.infoDialogOpen && Dialog.infoDialogOpen != this.elemId ) {
			//hideDialog(Dialog.productDialogOpen);
		}
		
		Dialog.infoDialogOpen = this.elemId;

		return true;
	}
		
	function infoDialogOnBeforeHide() {
		Dialog.infoDialogOpen = null;
		
		return true;
	}
	
	Dialog.infoDialogOpen = null;

	function showInfoDialog(name) {
		width = 400;
		height = 200;
		
		dialog = getDialog(name + 'Dialog', 'auxDialogs', dialogSimpleDraw);
		dialog.setSize(width, height);
		
		//dialog.debug = true;
		
		dialog.onBeforeShow = infoDialogOnBeforeShow;
		dialog.onBeforeHide = infoDialogOnBeforeHide;
		
		dialog.setPosition(
			scrollLeft() + clientWidth()/2 - width/2,
			scrollTop() + clientHeight()/2 - width/2 + 100
		);
		
		getObj(dialog.elemId + 'Content').innerHTML = getObj(name).innerHTML;
		
		dialog.show();
	}
	

function checkPrintOrderForm()
{
	var myreturn=true;
	x=document.getElementById('errorPrintOrderNume');
	if(document.getElementById('printOrderNume').value.length<=0)
	{
		x.style.display='';
		myreturn=false;
	}
	else
	{
			x.style.display='none';
	}
	
	x=document.getElementById('errorPrintOrderInstitutie');
	if(document.getElementById('printOrderInstitutie').value.length<=0)
	{
		x.style.display='';
		myreturn=false;
	}
	else
	{
			x.style.display='none';
	}
	
	x=document.getElementById('errorPrintOrderLocalitate');
	if(document.getElementById('printOrderLocalitate').value.length<=0)
	{
		x.style.display='';
		myreturn=false;
	}
	else
	{
			x.style.display='none';
	}
	
	x=document.getElementById('errorPrintOrderTelefon');
	if(document.getElementById('printOrderTelefon').value.length<=0)
	{
		x.style.display='';
		myreturn=false;
	}
	else
	{
			x.style.display='none';
	}
	
	return myreturn;
}

