function URLEncode(plaintext)
{
	if (typeof(plaintext) == 'undefined') return '';
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
/*			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
*/
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}

function error_filler(t, where, url) {
	$(where).innerHTML = '<div class="error">Error <b>' + t.status + '</b> in <b>' + url + '</b>: <b>' + t.statusText + '</b></div>';
}

function ajax_load(url, pars, where, evalonsuccess, hideloading) {
	tip_hide(true); //hide tip on any ajax request
	if (where != '') {
		if ($(where) != null) {
			if (hideloading == true) {
				//do nothing
			} else {
				var toinsert = '<span style="background:#CCFFCC;border:1px solid #77CC77;padding:3px;position:absolute;z-index:2">Se Incarca...</span>'; //hardcoded loading
				$(where).innerHTML = toinsert + $(where).innerHTML;
			}
		} else {
			alert('id="' + where + '" not found. cannot load "' + url + '"');
		}
	}
	
	var myAjax = new Ajax.Request(
		url,
		{
			method: 'get',
			parameters: pars,
			onSuccess: function(t) {
				if (where != '') {
					$(where).innerHTML = t.responseText;
				}
				eval(evalonsuccess);
				t.responseText.evalScripts();
			},
			onFailure: function(t) {
				error_filler(t, where, url);
			}
		}
	);
}

function ajax_load_dialog(url, pars, title, evalonsuccess, w) {
	if (evalonsuccess == 'undefined') evalonsuccess = '';
	ajax_load(
		url,
		pars,
		'',
		'show_dialog("' + title + '", t.responseText, "' + w + '");' + evalonsuccess
	);
}

dialog_observed = false;

function init_shortcut_keys() {
	if (dialog_observed == false) {
		Event.observe(
			document,
			'keypress',
			function(event) {
				switch (event.keyCode) {
					case 27: //esc -> 'press' the X on the dialog
						if (win != 'undefined') {
							win.hide();
						}
					break;
					/*
					case 67: //c -> checkout
						show_dialog_checkout();
						//todo: if not typing
					break;
					*/
					default:
						//status = "Key " + event.keyCode + " was pressed. No action assigned"; //todo: del
					break;
				}
			}
		);
		dialog_observed == true;
	}
}

var droptargetflasherflag = '';

function droptargetflasher(where) { //function will be repeatedly called
	new Effect.Highlight($(where), {
			duration: 0.5,
			startcolor: '#FAFAFA',
			endcolor: '#FFFF99',
			restorecolor: '#FFFF99',
			afterFinishInternal: function(effect) {
				new Effect.Highlight(effect.element, {
						duration: 0.5,
						startcolor: '#FFFF99',
						endcolor: '#FAFAFA',
						restorecolor: '#FAFAFA'
					}
				);
			}
		}
	);
}

function draggablize_byclass(classa, flashtarget, handleprefix) {
	var x = document.getElementsByClassName(classa);
	for (var i = 0; i < x.length; i++) {
		new Draggable(x[i].id,
			{
				ghosting: true,
				revert: true,
				starteffect: function(element) {
					new Effect.Opacity(element, {duration:0.2, from:1.0, to:0.7});
					if (droptargetflasherflag == '') {
						droptargetflasher(flashtarget);
						droptargetflasherflag = window.setInterval("droptargetflasher('" + flashtarget + "')", 1100);
					}
				},
				endeffect: function(element) {
					new Effect.Opacity(element, {duration:0.2, from:0.7, to:1.0});
					if (droptargetflasherflag != '') {
						window.clearInterval(droptargetflasherflag);
						droptargetflasherflag = '';
					}
				},
				handle: (handleprefix ? handleprefix + x[i].id : null)
			}
		);
	}
}

function product_drop(wheredrop, whereupdate, whereflash) {
	if (typeof(whereflash) == 'undefined') whereflash = wheredrop;
	Droppables.add(wheredrop,
		{
			hoverclass: 'drophover',
			onDrop: function(element, dropon, event) {
				cart_load(whereupdate, whereflash, encodeURIComponent(dropon.id), encodeURIComponent(element.id));
			}
		}
	)
}

win = 'undefined';

function create_dialog() {
	if (win == 'undefined') {
		new Zapatec.DialogWindow('', {title:'', modal:true, closeAction:function(){win.hide()}});
	}
}

function show_dialog(titlu, mesaj, w) {
	win.setTitle(titlu);
	win.setContent(mesaj);
	if (w == 'undefined') w = 700;
	win.show();
	win.setWidth(w);
	win.doCenter();
}

function dialog_title_mouse_down(event, win) {
	Zapatec.Window.mouseDown(event, win, {buttonType:'move'});
}

function dialog_title_mouse_move(event, win) {
	Zapatec.Window.mouseMove(event, win, {buttonType:'move'});
}

function dialog_title_mouse_up(event, win) {
	Zapatec.Window.mouseUp(event, win, {buttonType:'move'});
}

function samewindow() {
	//just checking existence of this function when opening links in a new window
}

var tip_locked = false;
var tip_anchor = false;
var tip_visible = false;
var tip_id = false;

function tip_show(obj, tipid, override_locked) {
	if ((tip_locked == false) || (override_locked == true)) {
		Position.prepare();
	
		obj = $(obj);
		var offsets = Zapatec.Utils.getAbsolutePos(obj);
		var left    = offsets.x + obj.clientWidth;
		var top     = offsets.y/* + obj.clientHeight*/;
		
		tip_id = tipid;
		if (typeof(tips_translated['tip_' + tipid]) == 'undefined') {
			//alert('translation for [' + tipid + '] undefined');
			tips_translated['tip_' + tipid] = '<span style="background: #FFFFDD ! important">tip_' + tipid + '</span>';
			var pars = 't=tip_' + tipid + '&random=' + Math.random();
			ajax_load('ajax.translate.php', pars, '');
		}
		$('tip_div_content').innerHTML = tips_translated['tip_' + tipid];
		
		var div = $('tip_div');
		
		div.style.position = 'absolute';
		div.style.width = '200px';
		//div.style.height = '100px';
		div.style.top = top + 'px';
		div.style.left = left + 'px';
		div.style.zIndex = '1492';
		div.style.display = '';
		
		tip_visible = true;
		tip_anchor = obj;
	}
}

function tip_hide(override_locked) {
	if ((tip_locked == false) || (override_locked == true)) {
		var div = $('tip_div');
		div.style.display = 'none';
		tip_visible = false;
		tip_locked = false;
	}
}

function tip_lock(obj, tipid, parentlevels) {
	if (typeof(parentlevels) == 'undefined') parentlevels = 0;
	parentlevels = parentlevels - 0;
	for (i = 0; i < parentlevels; i++) {
		obj = obj.parentNode;
	}
	tip_show(obj, tipid);
	tip_locked = true;
}

function tip_unlock() {
	tip_locked = false;
	tip_hide();
}


