
debug_javascript = false;

function redirect_to_500() {
	window.location.href = 'http://www.gourmet-und-wein.de/js/../error/500.php';
}

function error_message(message) {
	if( debug_javascript ) {
		try {
			$('error').style.visibility = 'visible';
			alert(message);
			$('error').innerHTML = message;
		} catch (ex) {
			alert('function error_message got exception: ' + ex);
		}
	} else {
		redirect_to_500();
	}
}


/*
 ************************************************************
 * Ajax Form functions
 ************************************************************
*/
function display_loading_image(form_id, element_id) {
	// Create image
	var loading_img = document.createElement('img');
	loading_img.setAttribute('src', 'img/loading.gif');
	loading_img.setAttribute('alt', 'loading');
	loading_img.setAttribute('title', 'Lade: Bitte waren Sie');
	loading_img.setAttribute('id', form_id + '_loading_img');
	loading_img.setAttribute('width', '20');
	loading_img.setAttribute('height', '20');

	// Add image to page
	$(element_id).parentNode.appendChild(loading_img);
}

function hide_loading_image(form_id) {
	$(form_id + '_submit').parentNode.removeChild( $(form_id + '_loading_img') );
}


function ajax_error_message(message, ajax_request) {
	error_message(message + '<br />' + (ajax_request != null ? ajax_request.responseText : '') );
}

function send_ajax_form(form, url) {
	try {
		var form_id = form.getAttribute('id');
		display_loading_image(form_id, form_id + '_submit');
		var myAjax = new Ajax.Request( url, { method: 'post', parameters: Form.serialize(form), onComplete: handle_ajax_form_response});
		Form.disable(form);
	} catch ( ex ) {
		error_message('Exception in send_ajax_form: ' + ex);
	}
	return false;
}


function handle_ajax_form_response(ajax_request) {
	try {
		// Check HTTP Status code
		if( ajax_request.status != 200 ) {
			return ajax_error_message('ajax_request.status != 200 :' + ajax_request.status, ajax_request);
		}
		// Get x-request-return_code
		var return_code = ajax_request.getResponseHeader('x-request-return_code');
		if( return_code == null ) {
			return ajax_error_message('no x-request-return_code header', ajax_request);
		}
		// Was the request successfull?
		if( return_code != 0 ) {

			// Get x-request-form_id
			var form_id = ajax_request.getResponseHeader('x-request-form_id');
			if( form_id == null ) {
				return ajax_error_message('no x-request-return_code header', ajax_request);
			}
			handle_form_error(form_id, ajax_request);

		} else {
			// Get x-request-redirect_location
			var redirect_location = ajax_request.getResponseHeader('x-request-redirect_location');
			if( redirect_location != null ) {

				// Get base href
				var base_href = $('base_href').getAttribute('href');

				// Redirect to new location
				window.location.href = base_href + redirect_location;

			} else {
				// Output content
				var form_id = ajax_request.getResponseHeader('x-request-form_id');
				if( form_id == null ) {
					return ajax_error_message('no x-request-return_code header', ajax_request);
				}

				handle_form_output(form_id, ajax_request);
			}
		}
	} catch (ex ) {
		ajax_error_message('Exception in handle_ajax_form_response: ' + ex, ajax_request);
	}
	return true;
}


function handle_form_error(form_id, ajax_request) {
	try {
		display_form_errors(form_id, ajax_request.responseXML);

		hide_loading_image(form_id);
		Form.enable($(form_id));

	} catch( ex ) {
		ajax_error_message('Exception in handle_form_error: ' + ex, ajax_request);
	}
}

function display_form_errors(form_id, xml_code) {

	var form = $(form_id);
	if( form == null ) {
		return error_message('Form with id: "' + form_id + '" not found');
	}

	// List of all labes from form
	var form_labels = $A(form.getElementsByTagName('label'));
	if( form_labels != null ) {
		// Reset css error class from labes
		form_labels.each(function(label){
			label.className = '';
		});
	}

	// Get ajax_response element
	var ajax_response = null;
	if( true
		&& xml_code
		&& xml_code.firstChild != null
		&& xml_code.firstChild.nodeType == 1
		&& xml_code.firstChild.getAttribute('id') == 'ajax_response'
	) {
		ajax_response = xml_code.firstChild;

	} else if( true
		&& xml_code
		&& xml_code.firstChild != null
		&& xml_code.firstChild.nextSibling != null
		&& xml_code.firstChild.nextSibling.nodeType == 1
		&& xml_code.firstChild.nextSibling.getAttribute != null
		&& xml_code.firstChild.nextSibling.getAttribute('id') == 'ajax_response'
	) {
		ajax_response = xml_code.firstChild.nextSibling;
	} else {
		alert('die');
	}

	// Get error_list element
	var error_list = null;
	if( true
		&& ajax_response.firstChild != null
		&& ajax_response.firstChild.nodeType == 1
		&& ajax_response.firstChild.getAttribute('id') == 'error_list'
	) {
		error_list = ajax_response.firstChild;

	} else if( true
		&& ajax_response.firstChild != null
		&& ajax_response.firstChild.nextSibling != null
		&& ajax_response.firstChild.nextSibling.nodeType == 1
		&& ajax_response.firstChild.nextSibling.getAttribute != null
		&& ajax_response.firstChild.nextSibling.getAttribute('id') == 'error_list'
	) {
		error_list = ajax_response.firstChild.nextSibling;
	}

	// Get error_entries
	var error_entries = new Array();
	if( error_list.firstChild != null ) {
		var child = error_list.firstChild
		do {
			if( child.nodeType == 1 ) {
				error_entries.push(child);
			}
			child = child.nextSibling;
		} while(child != null);
	}

	// Content for the error message div
	var error_div_content = '';
	for (var i = 0; i < error_entries.length; i++) {
		var form_field_id = form_id + '_' + error_entries[i].getAttribute('id');
		if( form_field_id != null && form_labels != null) {
			// Search label for form_field
			var label = form_labels.find(function(label){
				for(var x = label.attributes.length-1 ; x >= 0 ; x--) {
					if( label.attributes[x].name == 'for' ) {
						if(label.attributes[x].value == form_field_id) {
							return true;
						}
						return false;
					}
				}
				return false;
			});
			// Mark found label with error css class
			if( label != null ) {
				label.className = 'error';
			}
		}

		// Add error messages to output
		for (var j = 0; j < error_entries[i].childNodes.length; j++) {
			if( error_entries[i].childNodes[j].nodeType == 1) {
				error_div_content += '<div>' + error_entries[i].childNodes[j].firstChild.nodeValue + '</div>';
			}
		}
	}

	//	Output error message
	var form_error_div = $(form_id + '_error_div');
	if( form_error_div != null ) {
		form_error_div.style.visibility = 'visible';
		form_error_div.innerHTML = error_div_content;
	}
	return true;
}


function handle_form_output(form_id, ajax_request) {
	try {
		$("content").innerHTML = ajax_request.responseText;
	} catch(ex) {
		ajax_error_message('Exception in handle_form_output: ' + ex, ajax_request);
	}
}


function handle_ajax_x_call_response(ajax_request) {
	try {
		// Check HTTP Status code
		if( ajax_request.status != 200 ) {
			return ajax_error_message('ajax_request.status != 200 :' + ajax_request.status, ajax_request);
		}

		// Get x-request-return_code
		var return_code = ajax_request.getResponseHeader('x-request-return_code');
		if( return_code == null ) {
			return ajax_error_message('no x-request-return_code header', ajax_request);
		}

		// Was the request successfull?
		if( return_code != 0 ) {
			// Get x-request-form_id
			var form_id = ajax_request.getResponseHeader('x-request-form_id');
			if( form_id == null ) {
				return ajax_error_message('no x-request-return_code header', ajax_request);
			}

			return handle_form_error(form_id, ajax_request);
		}

		var x_call = ajax_request.getResponseHeader('x-request-x_call');
		if( x_call == null ) {
			return ajax_error_message('no x-request-x_call', ajax_request);
		}

		// Call x_call response function
		eval('handle_' + x_call + '_response(ajax_request)');

	} catch(ex) {
		ajax_error_message('Exception in handle_ajax_x_call_response: ' + ex, ajax_request);
	}
	return true;
}


function body_init() {
	var error_messages = $('error_messages');
	if( error_messages != null) {
		display_form_errors('form_edit', error_messages);
	}
}

/*
 ************************************************************
 * Ajax content functions
 ************************************************************
*/


/***
	 *
	 * JavaScript Code for Notiz Popups
	**/
	var NS=navigator.appName=="Netscape";
	var IE=navigator.appName=="Microsoft Internet Explorer";

	var mousemove = false;
	var current_elem = '';
	
	if (IE) {
		document.onmouseover = MoveHandlerIE;
	} else {
		document.onmouseover = MoveHandlerModern;
	}

	function popup(element, pop_header,pop_text){

		var pop_html = '<table cellspacing="2" summary="Tooltip" class="tooltip">';

		pop_html += '<tr><td class="popheader">' + pop_header + '</td></tr>';
		pop_html += '<tr><td valign="top" class="poptext">' + pop_text + '</td></tr>';
		pop_html += '</table>';
		
		// Save name of Element in global name space
		current_elem = element;


		// Make the div follow the mouse.
		mousemove = true;

		document.getElementById("information_" + element).style.visibility = "visible";
		document.getElementById("information_" + element).innerHTML = pop_html;
	}
	
	function popup_logo(element, pop_header,pop_text){

		var pop_html = '<table cellspacing="2" summary="Tooltip" class="tooltip_logo">';

		pop_html += '<tr><td class="popheader">' + pop_header + '</td></tr>';
		pop_html += '<tr><td valign="top" class="poptext">' + pop_text + '</td></tr>';
		pop_html += '</table>';
		
		// Save name of Element in global name space
		current_elem = element;


		// Make the div follow the mouse.
		mousemove = true;

		document.getElementById("information_" + element).style.visibility = "visible";
		document.getElementById("information_" + element).innerHTML = pop_html;
	}


	function popout(element) {
		// Div no longer needs to follow the mouse.
		mousemove = false;
		document.getElementById("information_" + element).style.visibility = "hidden";
	}

	function MoveHandlerModern(e) {
		if(mousemove) {
			Xpos = e.pageX + 20;
			Ypos = e.pageY + 10;
			document.getElementById("information_" + current_elem).style.left = Xpos + "px";
			document.getElementById("information_" + current_elem).style.top = Ypos + "px";
			return true;
		}return true;
		
	}

	function MoveHandlerIE() {

		if(mousemove) {

			var scrollPosY;
			var scrollPosX;
			if (typeof window.pageYOffset != 'undefined') {
			   scrollPosX = window.pageXOffset;
			   scrollPosY = window.pageYOffset;
			}
			else if (typeof document.compatMode != 'undefined' &&
				document.compatMode != 'BackCompat') {
			   scrollPosX = document.documentElement.scrollLeft;
			   scrollPosY = document.documentElement.scrollTop;
			}
			else if (typeof document.body != 'undefined') {
			   scrollPosX = document.body.scrollLeft;
			   scrollPosY = document.body.scrollTop;
			}

			Xpos = window.event.x + scrollPosX + 15;
			Ypos = window.event.y + scrollPosY - 10;

			document.getElementById("information_" + current_elem).style.left = Xpos + "px";
			document.getElementById("information_" + current_elem).style.top = Ypos + "px";
		}
		return true;
	}
	
	function open_window( path ) {
		window.open(path,"img_db_window","width=650,height=550,screenX=0,screenY=0,location=0,menubar=0,toolbar=0,status=0,titlebar=0,resizeable=1,scrollbars=1");
		return void(0);
	}
	
	/* Funktion um in einer textarea Text zu markieren */
	function insert(target_name, field, aTag, eTag) {
	 
	  var input = document.getElementById(target_name + field);
	  //alert(input);
	  input.focus();
	  /* für Internet Explorer */
	  if(typeof document.selection != 'undefined') {
	    /* Einfügen des Formatierungscodes */
	    var range = document.selection.createRange();
	    var insText = range.text;
	    range.text = aTag + insText + eTag;
	    /* Anpassen der Cursorposition */
	    range = document.selection.createRange();
	    if (insText.length == 0) {
	      range.move('character', -eTag.length);
	    } else {
	      range.moveStart('character', aTag.length + insText.length + eTag.length);      
	    }
	    range.select();
	  }
	  /* für neuere auf Gecko basierende Browser */
	  else if(typeof input.selectionStart != 'undefined')
	  {
	    /* Einfügen des Formatierungscodes */
	    var start = input.selectionStart;
	    var end = input.selectionEnd;
	    var insText = input.value.substring(start, end);
	    input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
	    /* Anpassen der Cursorposition */
	    var pos;
	    if (insText.length == 0) {
	      pos = start + aTag.length;
	    } else {
	      pos = start + aTag.length + insText.length + eTag.length;
	    }
	    input.selectionStart = pos;
	    input.selectionEnd = pos;
	  }
	  /* für die übrigen Browser */
	  else
	  {
	    /* Abfrage der Einfügeposition */
	    var pos;
	    var re = new RegExp('^[0-9]{0,3}$');
	    while(!re.test(pos)) {
	      pos = prompt("Einfügen an Position (0.." + input.value.length + "):", "0");
	    }
	    if(pos > input.value.length) {
	      pos = input.value.length;
	    }
	    /* Einfügen des Formatierungscodes */
	    var insText = prompt("Bitte geben Sie den zu formatierenden Text ein:");
	    input.value = input.value.substr(0, pos) + aTag + insText + eTag + input.value.substr(pos);
	  }
	}

