function leftTrim(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	return sString;
}

function rightTrim(sString)
{
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function trimAll(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function ShowScreenshot(URL)
{
	var now = new Date();
	var popupURL = URL;
	var popup = window.open(popupURL,"Screenshot",'screenX=100,screenY=100,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=540,height=400');
	if( navigator.appName.substring(0,8) == "Netscape" )
	{
		popup.location = popupURL;
	}
	popup.focus();
}

function ShowInfo(URL)
{
	var now = new Date();
	var popupURL = URL;
	var popup = window.open(popupURL,"Hilfe",'screenX=100,screenY=100,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=540,height=400');
	if( navigator.appName.substring(0,8) == "Netscape" )
	{
		popup.location = popupURL;
	}
	popup.focus();
}

function itemMouseOver(item, color)
{
	item.bgColor = color;
}

function itemMouseOut(item, color)
{
	item.bgColor = color;
}

function itemToggleVisibility(itemID)
{
	var x=document.getElementsByName(itemID)
	for(var i=0; i<x.length; i++)
	{
		var current = x[i].style.visibility;
		if(x[i].style.visibility=='collapse')
			x[i].style.visibility='visible';
		else
			x[i].style.visibility='collapse';
	}
}

function searchUL(UL)
{
	do
	{
		if(UL) UL = UL.nextSibling;
		if(UL && UL.nodeName == "UL") return UL;
	} while(UL);
	return false;
}

function showBlock(id)
{
	var LI = document.getElementById(id);
	var UL = searchUL(LI.firstChild);
	UL.style.display = "block";
	UL.style.visibility = "visible";
}

function hideBlock(id)
{
	var LI = document.getElementById(id);
	var UL = searchUL(LI.firstChild);

	var check = LI.className;
	if(!check.match(/expanded/i))
	{
		//aktiven zweig nicht schliessen!
		UL.style.display = "none";
		UL.style.visibility = "hidden";
	}
}

function updateShoppingCart(cmd, id, element) {
	var $shoppingCartShortview = $('#shopping_cart_shortview');
	if($shoppingCartShortview.length) {
		$.ajax({
			url: '/rpc/UpdateShoppingCart',
			method: 'GET',
			async: true,
			cache: false,
			data: {
				cmd: cmd,
				id: id
			},
			success: function(data, textStatus, jqXHR) {
				$shoppingCartShortview.show();
				$shoppingCartShortview.html(data);
				//update content elements
				$shoppingCartHeaderInfo = $('#shopping_cart_header_info');
				if($shoppingCartHeaderInfo.length) {
					$shoppingCartHeaderInfo.load('/rpc/UpdateShoppingCart', { cmd: 'headerinfo'});
				}
				$shoppingCart = $('#shopping_cart');
				if($shoppingCart.length) {
					$('#page_main_col1_content').load('/rpc/UpdateShoppingCart', { cmd: 'shoppingcart'});
				} else {
					$itemActions = $('#itemactions_' + id);
					if($itemActions.length) {
						$itemActions.load('/rpc/UpdateShoppingCart', { cmd: 'item', id: id});
					}
				}
			}
		});
	}
	return false;
}

function setAutocompleter(inputID, wordKeySelectorID, sourceKeySelectorID, resetInput, keywordsID) {
	var $input = $('#'+inputID);
	var $workKey = $('#'+wordKeySelectorID);
	var $sourceKey = $('#'+sourceKeySelectorID);

	var wordKeyValue = $workKey.length ? $workKey.val() : '';
	var sourceKeyValue = $sourceKey.length ? $sourceKey.val() : '';

	if(resetInput==true) {
		$input.val('')
	}
	$input.autocomplete({
		minLength: 2,
		source: function( request, response ) {
			$.ajax({
				url: '/rpc/search_suggest',
				dataType: "json",
				data: {
					dataType: 'json',
					type: 'relations',
					word_key: wordKeyValue,
					source: sourceKeyValue,
					query: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {
						return {
							label: item.title + (item.hits ? " (" + item.hits + ")" : ""),
							value: item.title
						}
					}));
				},
				cache: false
			});
		}

	});

	if(keywordsID!='') {
		var $keywordsField = $('#'+keywordsID);
		if($keywordsField.length) {
			if(search_query_opt!='' && search_query_opt=='schlagwoerter') {
				$keywordsField.show();
			} else {
				$keywordsField.hide();
			}
		}
	}
	return false;
}

function setSearchKeyword(selectorID, inputID) {
	var selector = document.getElementById(selectorID);
	var inputField = document.getElementById(inputID);
	inputField.value=selector.options[selector.selectedIndex].value;

}

function clearFormField(field, checkInput) {
	if(field.value==checkInput) {
		field.value='';
	}
}

function toggleVisibility($target) {
	$('#' + $target).slideToggle();
	return false;
}

function openConfirmAjax(content_url, target, title) {
	$.ajax({
		url: '/rpc/provider_hint',
		method: 'GET',
		async: false,
		success: function(data, textStatus, jqXHR) {
			$dialogContainer = $('#dialogContainer');
			if(!$dialogContainer.length) {
				$dialogContainer = $('<div id="dialogContainer" class="content"></div>');
				$('body').append($dialogContainer);
			}
			$dialogContainer.html(data);
			$dialogContainer.dialog({
				title: title,
				resizable: false,
				draggable: false,
				height: 'auto',
				width: 300,
				modal: true,
				dialogClass: 'content-dialog',
				buttons: [
				          { text: "Weiter", click: function() { $( this ).dialog( "close" ); $dialogContainer.remove(); window.location = target; return true; }},
				          { text: "Zurück", click: function() { $( this ).dialog( "close" ); $dialogContainer.remove(); }}
				]
			});
		}
	});
}

