function submitForm(form, action, stay) {
	if(!form) {
		form = document.mainForm;
	}
	if(action) {
		form.action = action;
	}
	if(!stay) {
		disableButtons();
		disablePage();
	}
	form.submit();
}

function disablePage() {
	if(document.getElementsByTagName) {
		var body = document.getElementsByTagName('body')[0];
		setCursor(body, 'wait');

/*		var from, elem;
		for(i = 0; i < document.forms.length; i++) {
			form = document.forms[i];
			for(j = 0; j < form.elements.length; j++) {
				elem = form.elements[i];
				if(elem.type == "input") {
					elem.style.display = "none";
				}
			}
		}
*/	}

	/*	
		if(body && body.innerHTML) {
			body.innerHTML += '<div id="disabler" style="' +
				'z-index: 0;' +
				'position: absolute;' +

				'width: 100%;' +
				'height: 100%;' +

				'top: 0px;' +
				'left: 0px;' +

				'cursor: wait;' +
			'"></div>';
		
		}
	*/
}

function goTo(task, form) {
	if(!form) {
		form = document.mainForm;
	}
	// initLoading();
	disableButtons();
	disablePage();
	setTask(task, form);
	
	if(form.action.indexOf('http') < 0 || form.action.indexOf('upload.silverlabxp.de') >= 0) {
		form.action = 'index.php';
	}

	submitForm(form);
}
function setTask(task, form) {
	if(!form) {
		form = document.mainForm;
	}
	form.task.value = task;
}

function enableButtons(form) {
	changeButtons(form, false);
}

function disableButtons(form) {
	if(!form) {
		for(var i = 0; i < document.forms.length; i++) {
			changeButtons(document.forms[i], true);
		}
	} else {
		changeButtons(form, true);
	}
}

function changeButtons(form, status) {
	if(!form) {
		form = document.mainForm;
	}

	for(i = 0; form.elements && i < form.elements.length; i++) {
		var elem = form.elements[i];
		if(elem.type == "button" || elem.type == "submit") {
			elem.disabled = status;
			if(window.disableButton) {
				if(status == true && (typeof window.disableButton) == "function") {
					disableButton(elem);
				} else if(window.deactivateButton) {
					if(status == false && (typeof window.deactivateButton) == "function") {
						deactivateButton(elem);
					}
				}
			}
		}
	}
}

function changeLand(item, land, target) {
	var form = document.mainForm;

	var select = form.elements['sourceid'];
	if(select.type != 'select-one') {
		select = form.elements['destid'];
	}

	var oldlandid = false;
	var oldland = form.elements[item].value;
	for(var i = 0; i < select.length; i++) {
		if(select.options[i].text == oldland) {
			oldlandid = select.options[i].value;
		}
	}

	form.elements[item].value = land;

	try {
		form.elements['custland'].value = land;
	} catch(e) {}

	var landid = false;
	for(var i = 0; i < select.length; i++) {
		if(select.options[i].text == land) {
			landid = select.options[i].value;
		}
	}

	if(
		AddressType[landid] != lastAddressType || 
		(States && States[landid] && (States[landid] > 0 || (States[landid] == 0 && (States[oldlandid] && States[oldlandid] > 0))))
	) {
		disableButtons();
		disablePage();
		
		goTo(target, form);
		
		setTimeout(function() {
			if(window.disableOnLandChange && disableOnLandChange == true) {
				for(var i = 0; i < form.elements.length; i++) {
					var el = form.elements[i];
					if(el.type == "text" || el.type == "select-one") {
						el.disabled = true;
					}
				}
			}
		}, 200);
	} else {
		changeZip(landid);
		return true;
	}
}

function changeZip(landid) {
	var form = document.mainForm;
	var zip = false;

	if(form.elements['cust[zip]']) {
		zip = form.elements['cust[zip]'];
	} else if(form.elements['delivery[zip]']) {
		zip = form.elements['delivery[zip]'];
	}

	if(zip && ZipMaxLength && ZipMaxLength[landid]) {
		var len = ZipMaxLength[landid];

		zip.value = zip.value.substr(0, len);
		zip.maxLength = len;

		form.ziplength.value = len;
	}

	if(ZipType && ZipType[landid]) {
		form.ziptype.value = ZipType[landid];
	}
}

function changeLang(item) {
	var form = document.mainForm;

	disableButtons();
	disablePage();

	form.task.value = "";
	form.lang.value = item.value;
	submitForm(form);
}

function setPickup(id) {
	if(document.mainForm.shopid) {
		document.mainForm.shopid.value = id;
	}
}

function setNewVendorId(id) {
	document.mainForm.newvendorid.value = id;
}

function changeShippingMethod(id, target, form) {
	if(!form) {
		form = document.mainForm;
	}
	disableButtons();
	disablePage();
	var kind = methods[id]["DeliveryKind"];

	form.delmethodid.value = methods[id]["ID"];
	form.delmethodtype.value = methods[id]["DeliveryKind"];
	form.delmethodprice.value = methods[id]["Shippingcosts"];
	form.delmethodname.value = methods[id]["Name"];

	if(kind == 1) {
		form.shopid.value = "";
	}

	goTo(target, form);
}

function changePaymentMethod(id, target, form) {
	if(!form) {
		form = document.mainForm;
	}
	disableButtons();
	disablePage();
	
	if(form.ccCSC) form.ccCSC.value = "";
	if(form.ccInstitute) form.ccInstitute.value = "";
	if(form.ccValidFromYear) form.ccValidFromYear.value = "";
	if(form.ccValidFromMonth) form.ccValidFromMonth.value = "";
	if(form.ccNumber) form.ccNumber.value = "";
	if(form.ccName) form.ccName.value = "";

	if(form.payaccount) form.payaccount.value = "";
	if(form.payblz) form.payblz.value = "";
	if(form.payktnr) form.payktnr.value = "";
	if(form.paybank) form.paybank.value = "";

	form.paymentId.value = methods[id]["ID"];
	form.paymentName.value = methods[id]["Name"];
	form.paymentKind.value = methods[id]["Kind"];
	form.paymentPrice.value = methods[id]["Price"];

	goTo(target, form);
}

function edit(action, id) {
	var form = document.mainForm;
	form.lfdnr.value = id;
	goTo(action, form);
}

function jumpForward(item, to) {
	var isGecko = navigator.userAgent.toLowerCase().indexOf('gecko') >= 0;
	
	if(isGecko && window.keyCode && keyCode == 9) {
		return false;
	}

	var charlength = (isGecko ? 3 : 4);
	if(item.value.length == charlength) {
		item.form.elements[to].focus();
	}
}

function jumpBack(item, to) {
	return false;

	if(item.value.length == '') {
		to = item.form.elements[to];
		to.focus();
		to.value = to.value;
	}
}

function FormatToCurrency(value) {
	var value = FormatNumber(value);
	if(LEADINGCURRENCY==true) {
		return CURRENCY + " " + value;
	}
	return value + " " + CURRENCY;
}	

function FormatNumber(value) {
	if(!window.safari) return value.toFixed(2);

	value = Math.round(value * 100) / 100;
	value = value + "";

	var parts = value.split('.');

	if(!parts[1] || parts[1].length == 0) {
		parts[1] = '00';
	} else if(parts[1].length == 1) {
		parts[1] += '0';
	}

	if(!parts[0]) {
		parts[0] = '0';
	}

	return parts[0] + '.' + parts[1];
}

function getWidth(id) {
	return Number(document.forms["mainForm"].elements["files[" + id + "][width]"].value);
}

function getHeight(id) {
	return Number(document.forms["mainForm"].elements["files[" + id + "][height]"].value);
}

function getBaseWidth(id) {
	var item = window.document.images['img_' + id];

	if(item.style.width != "") {
		return parseInt(item.style.width);
	} else {
		return 80;
	}
}

function changeWhiteBorders(box) {
	return true;

	baseWidth = 80;
	borderWidth = 5;

	var form = document.mainForm;

	if(!box || !box.form) {
		return true;
	}

	if(box.checked == true && form.extraInfocbkWhiteBorders.value != 1) {
		form.extraInfocbkWhiteBorders.value = 1;

		for(id in lastsel) {
			var width  = getWidth(id);
			var height = getHeight(id);

			var percentage = ((baseWidth * 100) / width);

			var item = window.document.images['img_' + id];
			item.style.borderStyle = 'solid';
			item.style.borderColor = '#FFF';

			item.style.borderLeftWidth = item.style.borderLeftWidth != "" ? (parseInt(item.style.borderLeftWidth) + borderWidth) : borderWidth;
			item.style.borderRightWidth = item.style.borderRightWidth != "" ? (parseInt(item.style.borderRightWidth) + borderWidth) : borderWidth;
			item.style.borderTopWidth = borderWidth;
			item.style.borderBottomWidth = borderWidth;

			item.style.width = (width * (percentage / 100) - 2 * borderWidth);
			item.style.height = (height * (percentage / 100) - 2 * borderWidth);
		}
	} else if(box.checked == false && form.extraInfocbkWhiteBorders.value == 1) {
		form.extraInfocbkWhiteBorders.value = 0;

		for(id in lastsel) {
			var width  = getWidth(id);
			var height = getHeight(id);

			var percentage = ((baseWidth * 100) / width);

			var item = window.document.images['img_' + id];
			item.style.borderStyle = 'solid';
			item.style.borderColor = '#FFF';

			if(item.style.borderLeftWidth != "") {
				lwidth = (parseInt(item.style.borderLeftWidth) - borderWidth);
				lwidth = (lwidth < 0 ? 0 : lwidth);
				item.style.borderLeftWidth = lwidth;
			} else {
				item.style.borderLeftWidth = 0;
			}

			if(item.style.borderRightWidth != "") {
				rwidth = (parseInt(item.style.borderRightWidth) - borderWidth);
				rwidth = (rwidth < 0 ? 0 : rwidth);
				item.style.borderRightWidth = rwidth;
			} else {
				item.style.borderRightWidth = 0;
			}

			item.style.borderTopWidth = 0;
			item.style.borderBottomWidth = 0;
			
			item.style.width = (width * (percentage / 100));
			item.style.height = (height * (percentage / 100));
		}
	}
}

var fitFillDecreased = false;

function changeFitFill(id, crop) {
	var borderWidth = 3;
	var elem = document.mainForm.elements["files[" + id + "][fitfill]"];

	if(crop == elem.value && elem.value != "") {
		return true;
	} else {
		elem.value = crop;
	}

	/*
		var item = window.document.images['img_' + id];
		item.style.borderStyle = 'solid';
		item.style.borderColor = '#FFF';
	*/

	if(crop == 1) {
		document.images["fit_" + id].src = document.images["fit_" + id].src.replace("_active", "");
		document.images["fill_" + id].src = document.images["fill_" + id].src.replace(".gif", "_active.gif");

	/* You suck
		if(item.style.borderLeftWidth != "") {
			lwidth = (parseInt(item.style.borderLeftWidth) - borderWidth);
			lwidth = (lwidth < 0 ? 0 : lwidth);
			item.style.borderLeftWidth = lwidth;
		} else {
			item.style.borderLeftWidth = 0;
		}

		if(item.style.borderRightWidth != "") {
			rwidth = (parseInt(item.style.borderRightWidth) - borderWidth);
			rwidth = (rwidth < 0 ? 0 : rwidth);
			item.style.borderRightWidth = rwidth;
		} else {
			item.style.borderRightWidth = 0;
		}

		var width  = getWidth(id);
		var height = getHeight(id);

		var percentage = ((80 * 100) / width);

		if(fitFillDecreased == true) {
			item.style.width = (width * (((getBaseWidth(id) * 100) / width) / 100) + 2 * borderWidth);
			item.style.height = height * (((80 * 100) / width) / 100);
		}
	*/
	} else {
		document.images["fit_" + id].src = document.images["fit_" + id].src.replace(".gif", "_active.gif");
		document.images["fill_" + id].src = document.images["fill_" + id].src.replace("_active", "");

	/* This does not work as expected :( giving up
		item.style.borderLeftWidth = item.style.borderLeftWidth != "" ? (parseInt(item.style.borderLeftWidth) + borderWidth) : borderWidth;
		item.style.borderRightWidth = item.style.borderRightWidth != "" ? (parseInt(item.style.borderRightWidth) + borderWidth) : borderWidth;

		var width  = getWidth(id);
		var height = getHeight(id);

		var percentage = ((80 * 100) / width);

		item.style.width = (width * (((getBaseWidth(id) * 100) / width) / 100) - 2 * borderWidth);
		item.style.height = height * (((80 * 100) / width) / 100);

		fitFillDecreased = true;
	*/
	}

	/*
		item.style.borderTopWidth = item.style.borderTopWidth != "" ? item.style.borderTopWidth : 0;
		item.style.borderBottomWidth = item.style.borderBottomWidth != "" ? item.style.borderBottomWidth : 0;
	*/
}

function changeFitFillAll(crop) {
	var elem = document.mainForm.elements["fitfillall"];

	if(crop == elem.value && elem.value != "") {
		return true;
	} else {
		elem.value = crop;
	}

	if(crop == 1) {
		document.images["fit" ].src = document.images["fit"].src.replace("_active", "");
		document.images["fill"].src = document.images["fill"].src.replace(".gif", "_active.gif");
	} else {
		document.images["fit"].src = document.images["fit"].src.replace(".gif", "_active.gif");
		document.images["fill"].src = document.images["fill"].src.replace("_active", "");
	}
}

function printWindow(win) {
	if(window.opera && document.mainForm && document.mainForm.printOverview) {
		// Haha THATS ONE NICE thing ;)
		// Opera is not able to print the contents of a IFrame only
		// Therefor I create a Popup with the contents of the IFrame
		// Print this window and than close it afterwards :)
		var content = document.mainForm.printOverview.value;
		win = window.open("about:blank", "OperaPrintWindow", "width=1,height=1");
		win.document.open();
		win.document.write(content);
		win.document.close();
		win.focus();
		win.print();
		
		window.setTimeout(function() {
			win.close();
		}, 1000);
	} else {
		win.focus();
		win.print();
	}
}

var ThumbWindow;
function showThumb(url, unit, name, id) {
	if(ThumbWindow) {
		ThumbWindow.close();
	}

	var page = "";

	page += "<html>";
	page += "<head>";
	page += "<style> body { margin: 0px; } </style>";
	page += "<title>" + name + "</title>";
	page += "</head>";
	page += "<body>";
	page += '<a href="#" onClick="top.close()"><img src="' + url + '&' + unit + '=640" border="0" /></a>';
	page += "</body>";
	page += "</html>";

	var width  = document.mainForm.elements["files[" + id + "][width]"].value;
	var height = document.mainForm.elements["files[" + id + "][height]"].value;

	if(unit == 'height') {
		width  = (width / height) * 640;
		height = 640 + 4;
	} else {
		height = (height / width) * 640 + 4;
		width  = 640;
	}

	ThumbWindow = window.open("javascript:void(0)", "ThumbWindow", 
		"height=" + height +
		",width=" + width + 
		",top=" + ((screen.height / 2) - (height / 2)) +
		",left=" + ((screen.width / 2) - (width / 2)) +
		",location=no" +
		",menubar=no" +
		",resizable=no" +
		",scrollbars=no" +
		",status=no" +
		",toolbar=no"
	);
	ThumbWindow.document.write(page);
	ThumbWindow.document.close();
	ThumbWindow.focus();
}

function replaceArrayKey(array, searchKey, replaceKey, item) {
	var tmpArray = new Array();

	for(key in array) {
		tmpArray[key] = array[key];
	}
	
	for(key in array) {
		delete array[key];
	}

	for(key in tmpArray) {
		if(key == searchKey) {
			array[replaceKey] = item;
		} else {
			array[key] = tmpArray[key];
		}
	}

	return array;
}

function getLoadingDiv() {
	var div = document.getElementById("loadingDiv");
	if(!div) {
		div = document.createElement("div");
		document.body.appendChild(div);
	}

	div.id = "loadingDiv";
	div.style.display = "block";
	div.style.backgroundColor = "#FFFFFE";
	div.style.position = "absolute";
	div.style.top = "0px";
	div.style.left = "0px";
	div.style.width = "100%";
	div.style.height = "100%";
	div.style.textAlign = "center";
	div.style.verticalAlign = "middle";
	div.style.zIndex = 9999999;
	div.style.fontSize = "20px";
	setCursor(div, "wait");

	return div;
}

function initLoading(text) {
	if(!text && window.loadingText) {
		text = loadingText;
	} else if(!text) {
		text = '<img src="/basket/images/loading.gif" />';
	}

	var div = getLoadingDiv();
	if(!div) {
		return false;
	}
	
	div.style.display = "block";
	div.innerHTML = text;
}

function finishLoading() {
	var div = getLoadingDiv();
	if(!div) {
		return false;
	}

	div.style.display = "none";
}

var windowWidth = 0;
var windowHeight = 0;
function getWindowSize(type) {
	if(windowWidth == 0 || windowHeight == 0) {
		// Retrieve this information only once an presave it
		try {
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		} catch(e) {
			if(window.innerWidth) {
				windowWidth = window.innerWidth;
				windowHeight = window.innerHeight;
			}
		}
		
		try {
			Events.add(window, "resize", function() {
				windowWidth = 0;
				windowHeight = 0;
			});
		} catch(e) {
			// No Events object
		}
	}

	// return the presaved information
	if(type == 'width') {
		return windowWidth;
	} else if(type == 'height') {
		return windowHeight;
	}
	
	return false;
}

var sw = 0;
var sh = 0;
function isScrollable(type) {
	var scrollable = false;
	
	var width = getWindowSize('width');
	var height = getWindowSize('height');

	if(sw == 0 || sh == 0) {
		try {
			if(window.outerWidth) {
				sw = window.outerWidth;
				sh = window.outerHeight;
			} else if(document.body && document.body.scrollWidth) {
				sw = document.body.scrollWidth;
				sh = document.body.scrollHeight;
			}
		} catch(e) {}
	}

	if(type == 'x') {
		if(sw > width) {
			scrollable = true;
		}
	} else if(type == 'y') {
		if(sh > height) {
			scrollable = true;
		}
	} else {
		if(sw > width || sh > height) {
			scrollable = true;
		}
	}

	return scrollable;
}

function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;

	if(typeof(window.pageYOffset) == 'number') {
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if(typeof(document.body.scrollLeft) == 'number') {
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	}

	return {x: scrOfX, y: scrOfY};
}

String.prototype.entities = function() {
	var chars = new Array (
		'&' /*,'à','á','â','ã','ä','å','æ','ç','è','é',
		'ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô',
		'õ','ö','ø','ù','ú','û','ü','ý','þ','ÿ','À',
		'Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë',
		'Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö',
		'Ø','Ù','Ú','Û','Ü','Ý','Þ','€','\"','ß','<',
		'>','¢','£','¤','¥','¦','§','¨','©','ª','«',
		'¬','­','®','¯','°','±','²','³','´','µ','¶',
		'·','¸','¹','º','»','¼','½','¾'*/
	);
	
	var entities = new Array (
		'amp'/*,'agrave','aacute','acirc','atilde','auml','aring',
		'aelig','ccedil','egrave','eacute','ecirc','euml','igrave',
		'iacute','icirc','iuml','eth','ntilde','ograve','oacute',
		'ocirc','otilde','ouml','oslash','ugrave','uacute','ucirc',
		'uuml','yacute','thorn','yuml','Agrave','Aacute','Acirc',
		'Atilde','Auml','Aring','AElig','Ccedil','Egrave','Eacute',
		'Ecirc','Euml','Igrave','Iacute','Icirc','Iuml','ETH','Ntilde',
		'Ograve','Oacute','Ocirc','Otilde','Ouml','Oslash','Ugrave',
		'Uacute','Ucirc','Uuml','Yacute','THORN','euro','quot','szlig',
		'lt','gt','cent','pound','curren','yen','brvbar','sect','uml',
		'copy','ordf','laquo','not','shy','reg','macr','deg','plusmn',
		'sup2','sup3','acute','micro','para','middot','cedil','sup1',
		'ordm','raquo','frac14','frac12','frac34'*/
	);
	
	newString = this;
	for (var i = 0; i < chars.length; i++) {
		myRegExp = new RegExp();
		myRegExp.compile(chars[i], 'g')
		newString = newString.replace(myRegExp, '&' + entities[i] + ';');
	}

	return newString;
}

function hasOffsetBug(obj) {
	if(document.all) {
		while((obj = obj.parentNode) != null) {
			if(obj.tagName == "TD") {
				return obj;
			}
		}
	}
	return false;
}

function getYPosInTable(obj) {
	var pos = obj.offsetTop;
	while((obj = obj.offsetParent) != null) {
		var td = null;
		if(td = hasOffsetBug(obj)) {
			var add = 0;
			var align = ((getStyle(td, "vertical-align") || td.getAttribute("valign")) + "").toLowerCase();
			if(align == "center" || align == "middle" || !align || align == "auto") {
				add = Math.round((obj.parentNode.offsetHeight - obj.offsetHeight) / 2);
			} else if(add == "bottom") {
				add = obj.parentNode.offsetHeight - obj.offsetHeight;
			}
			pos += getYPosFor(obj.parentNode) + add;
		} else {
			pos += obj.offsetTop;
		}
	}
	return pos;
}

function getYPosFor(obj, searchUntil, searchForTagName, stopSearchBefore) {
	var y = obj.offsetTop;
	while((obj = obj.offsetParent) != null) {
		if(searchUntil != null && (searchUntil == obj.id || searchUntil == obj.name)) {
			return y;
		}
		
		var tag = (stopSearchBefore && obj.offsetParent ? obj.offsetParent.tagName  : obj.tagName).toLowerCase();
		if(searchForTagName != null && searchForTagName == true && tag == searchUntil.toLowerCase()) {
			return y;
		}

		y += obj.offsetTop;
	}
	return y;
}

function getXPosFor(obj, searchUntil, searchForTagName, stopSearchBefore) {
	var x = obj.offsetLeft;
	while((obj = obj.offsetParent) != null) {
		if(searchUntil != null && (searchUntil == obj.id || searchUntil == obj.name)) {
			return x;
		}

		var tag = (stopSearchBefore && obj.offsetParent ? obj.offsetParent.tagName  : obj.tagName).toLowerCase();
		if(searchForTagName != null && searchForTagName == true && tag == searchUntil.toLowerCase()) {
			return x;
		}

		x += obj.offsetLeft;
	}
	return x;
}

function getParamFromUrl(url, param) {
	var query = url.split(/\?/)[1];
	if(!query) {
		return false;
	}
	
	var parts = query.split(/&/);

	for(var i = 0; i < parts.length; i++) {
		var part = parts[i];
		if(part == "") {
			continue;
		}
		
		var keyval = part.split(/=/, 2);
		
		if(keyval[0] == param) {
			return keyval[1];
		}
	}
	
	return false;
}

/* Base 64 Encoding */
String.prototype.Base64Charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
String.prototype.toBase64 = function() {
	var input = this;
	var output = "";
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;
	
	do {
		chr1 = input.charCodeAt(i++);
		chr2 = input.charCodeAt(i++);
		chr3 = input.charCodeAt(i++);
		
		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;
		
		if (isNaN(chr2)) {
			enc3 = enc4 = 64;
		} else if (isNaN(chr3)) {
			enc4 = 64;
		}
	
		output = output + 
			this.Base64Charset.charAt(enc1) + 
			this.Base64Charset.charAt(enc2) + 
			this.Base64Charset.charAt(enc3) + 
			this.Base64Charset.charAt(enc4);
	} while (i < input.length);

	return output;
}
String.prototype.fromBase64 = function() {
	var input = this;
	var output = "";
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;
	
	// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
	
	do {
		enc1 = this.Base64Charset.indexOf(input.charAt(i++));
		enc2 = this.Base64Charset.indexOf(input.charAt(i++));
		enc3 = this.Base64Charset.indexOf(input.charAt(i++));
		enc4 = this.Base64Charset.indexOf(input.charAt(i++));
		
		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;
		
		output = output + String.fromCharCode(chr1);
		
		if (enc3 != 64) {
			output = output + String.fromCharCode(chr2);
		}
		if (enc4 != 64) {
			output = output + String.fromCharCode(chr3);
		}
	} while (i < input.length);
	
	return output;
}

function getStyle(el, style) {
	if(!document.getElementById) return;
	var value = el.style[toCamelCase(style)];

	if(!value)
		try {
			if(document.defaultView)
				value = document.defaultView.getComputedStyle(el, "").getPropertyValue(style);
			else if(el.currentStyle)
				value = el.currentStyle[toCamelCase(style)];
		} catch(e) {
			return;
		}
	
	return value;
}

/** toCamelCase(input)
 * Converts string input to a camel cased version of itself.
 * For example:
 * toCamelCase("z-index"); // returns zIndex
 * toCamelCase("border-bottom-style"); // returns borderBottomStyle.
 */
function toCamelCase(s) {
	for(var exp = toCamelCase.exp; 
		exp.test(s); s = s.replace(exp, RegExp.$1.toUpperCase()) );
	return s;
}
toCamelCase.exp = /-([a-z])/;

function posMoveItem(item,x,y) {
	var itemWidth = item.offsetWidth;
	var itemHeight = item.offsetHeight;

	var itemLeft = false;
	var itemTop = false;
	if(window.Mouse) {
		itemLeft = Mouse.actual.x + 10;
		itemTop  = Mouse.actual.y + 10;
	} else {
		itemLeft = x + 10;
		itemTop = y + 10;
	}

	var windowWidth = getWindowSize('width');
	var windowHeight = getWindowSize('height');
	
	var scrolled = getScrollXY();

	if(itemLeft + itemWidth >= windowWidth + scrolled.x) {
		itemLeft -= (itemLeft + itemWidth) - (windowWidth + scrolled.x);
	}

	if(itemTop + itemHeight >= windowHeight + scrolled.y) {
		itemTop -= (itemTop + itemHeight) - (windowHeight + scrolled.y);
	}
	
	item.style.left = itemLeft;
	item.style.top  = itemTop;
}

var debugLast = 0;
function resetDebug() {
	debugLast = new Date().getTime();
}

var DebugObject = null;
function debug(msg) {
	if(DebugObject == null) {
		DebugObject = new DebugHandler();	
	}
	DebugObject.writeln(msg);
	return;
	
	/* end here */
	
	var debugNow = new Date().getTime();

	if(debugLast == 0) {
		debugLast = debugNow;
		return true;
	}
	
	var elapsed = debugNow - debugLast;
	alert(elapsed + " milliseconds since last debug");
	
	debugLast = debugNow;
}
var Debug = debug;

function debugtxt(str) {
	var debug = document.getElementById("debug");
	if(debug) {
		var id = debug.innerHTML.split("\n").length + 1;
		debug.innerHTML = debug.innerHTML + id + ": " + str + "<br />\n";
		debug.scrollTop = 100000;
	}
}

function findNode(xmlDoc, nodeName) {
	if(xmlDoc.hasChildNodes() == false) {
		return false;
	}

	var node = xmlDoc.firstChild;
	var found = false;

	do {
		if(node.nodeName == nodeName) {
			found = true;
			break;
		}
	} while(node = node.nextSibling)

	if(found == false) {
		return false;
	}

	var childs = node.childNodes;
	for(var i = 0; i < childs.length; i++) {
		if(childs[i].nodeType == 1) {
			return node;
		}
	}

	return node.firstChild;
}

String.prototype.trim = function() {
	return this.replace(/^\s*|\s*$/g, "");
}

function FileSize(size) {
	this.size = size;
}
FileSize.prototype = {
	size: 0,
	inMB: function(asNumber) {
		return this.format(Math.round((this.size / 1024 / 1024) * 100) / 100, asNumber);
	},
	inKB: function(asNumber) {
		return this.format(Math.round((this.size / 1024) * 100) / 100, asNumber);
	},
	toString: function() {
		return this.size;
	},
	format: function(str, asNumber) {
		if(asNumber == true) {
			return Number(str);
		}

		str = str + "";

		var parts = str.split(".");
		if(parts.length == 2) {
			if(parts[1].length < 2) {
				for(var i = parts[1].length; i < 2; i++) {
					str += "0";
				}
			}
		} else {
			str += ".00";
		}
		
		return str;
	}
}

function getString(id) {
	if(!window.langDef) {
		return "";
	}

	if(langDef[id]) {
		return langDef[id];
	} else {
		return "";
	}
}

function setCursor(elem, cur) {
return true;
	try {
		elem.style.cursor = cur;
	} catch(e) {
		if(cur == "pointer") {
			elem.style.cursor = cur;
		}
	} finally {}
}

function var_dump(obj) {
	Dumper.alert(obj);
}

var Dumper = {
	spacer : " ",
	spaces : 4,
	depths : -1,
	info   : "", last : "",
	indent : function() {
		for(var i = 0; i < this.depths * this.spaces; i++) this.info += this.spacer;
	},
	append : function(string) {
		this.info += string;
	},
	create : function(obj) {
		this.depths++;
	
		for(key in obj) {
			if(typeof(obj[key]) == "object") {
				this.append(key + ": {\n");
				this.create(obj[key]);
				this.append("},\n");
			} else {
				var val = typeof(obj[key]) == "string" ? '"' + obj[key] + '"' : obj[key];
				this.indent();				
				this.append(key + ": " + val + ",\n");
			}
		}

		if(this.create.caller == this.create) {
			this.depths--;
		} else {
			this.last = this.info;
			this.depths = -1;
			this.info = "";
		}
	},
	alert  : function(obj) {
		this.create(obj);
		alert(this.last);	
	},
	write  : function(obj) {
		this.create(obj);
		document.writeln(this.last.replace(/\n/g, "<br />"));
	}
}

function createDiv(id, cursor, className, onuse) {
	var div = document.createElement("div");
	
	div.id = id;
	if(className) div.className = className;
	if(cursor) div.style.cursor = cursor;
	if(onuse) div.onmousedown = onuse;
	
	return div;
}
function appendDivs(elems, parent) {
	if(elems.length != null && typeof(elems) != "string") {
		for(i in elems) {
			parent.appendChild(elems[i]);
		}	
	} else {
		parent.appendChild(elems);
	}
}
	
function cm2px(number, dpi) {
	dpi = dpi || 72;
	return number * (dpi / 2.54);
}
function px2cm(number, dpi) {
	dpi = dpi || 72;
	return number / (dpi / 2.54);
}

function setOpacity(item, value) {
	var opac = value / 100;

	item.style.opacity = opac;
	item.style.MozOpacity = opac;
	item.style.KhtmlOpacity = opac;
	
	if(item.filters) {
		item.filters.alpha.opacity = value;
	}
}

function postParam(name, value) {
	if(window.ie) {
		var p = document.createElement('<input type="hidden" name="' + name + '" value="' + value + '">');
	} else {
		var p = document.createElement("input");
			p.setAttribute("type", "hidden");
			p.setAttribute("name", name);
			p.setAttribute("value",  value);
	}
	return p;
}

function checkTermsAndPrivacy() {
	var form = document.mainForm;
	var btn = document.getElementById("NextButton");
	
	var t = form.termsAccepted;
	var p = form.privacyAccepted;
	
	if((t && p && t.checked && p.checked) || (t && !p && t.checked) || (!t && p && p.checked)) {
		btn.disabled = false;
		deactivateButton(btn);
	} else {
		btn.disabled = true;
		disableButton(btn);
	}
}

function toggleTCCheckBox(name) {
	var input = document.mainForm.elements[name + "Accepted"];
	input.checked = !input.checked;
	checkTermsAndPrivacy();
}

function showTCUrl(url) {
	window.open(url, "_blank");
}

function addClassName(item, name, remove) {
	var regexp = new RegExp("\\S*" + remove, "g");
	item.className = item.className.replace(regexp, "").replace(/\s+/g, " ") + " " + name;
}	


// This are some functions, which handle the Next/Last/Delete Product buttons
function getLfdnr() {
	return document.mainForm.lfdnr.value;
}
function selectOpenerItem(id) {
	opener.selectItem(opener.document.getElementById(id));
}
function deleteProduct() {
	if(confirm(str_confirmLayoutDelete)) {
		selectOpenerItem(getLfdnr());
		opener.aDeleteItems();
		
		if(window.closeEditWindow) {
			closeEditWindow();
		} else {
			self.close();
		}
	}
}
function changeProduct(type) {
	var lfdnr=0;
	if(window.layoutEditFrame && window.layoutEditFrame.closed != false) {
		lfdnr = window.layoutEditFrame.document.getElementsByName("lfdnr")[0].value;
	} else {
		lfdnr = document.mainForm.lfdnr.value;
	}
	var next = false;
	var last = false;
	for(key in opener.lastsel) {
		if(next == true) {
			break;	
		}
		if(key == lfdnr) {
			if(type == "previous") {
				key = last;
				break;
			} else if(type == "next") {
				next = true;
			}
		}
		var last = key;
	}
	selectOpenerItem(key);
	if(!window.Cropper) {
		initLoading();
	}
	opener.aEditItem(true);
}

function repaintScreenForOpera() {
	if(window.opera) {
		document.documentElement.style.backgroundColor = 'transparent';
	}
}