
debug_javascript = false;

function redirect_to_500() {
	window.location.href = 'http://www.kopierer-online.com/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', '32');
	loading_img.setAttribute('height', '32');

	// 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);
	}
}


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;
	}
}


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);
	}
}


function body_init() {
	var error_messages = $('error_messages');
	if( error_messages != null) {
		display_form_errors('form_picture_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.onmousemove = MoveHandlerIE;
	} else {
		document.onmousemove = MoveHandlerModern;
	}

	function popup(pop_header,pop_text){

		var pop_html = '<table class="pop_html" border="0" cellspacing="0" summary="Die folgende Tabelle stellt ein virtuelles Postit dar. Beim Überfahren des Glossareintrags mit der Maus wird ein Popup geöffnet"><tr><td><table class="postit" border="0" cellspacing="1" cellpadding="1">';

		pop_html += '<tr><td width="100%" class="popheader" abbr="Hier findet sich die Überschrift des Glossareintrages">' + pop_header + '</td></tr>';
		pop_html += '<tr><td valign="top" class="poptext_bright" abbr="Hier findet sich die Beschreibung">' + pop_text + '</td></tr>';
		pop_html += '</table></td></tr></table>';

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

		document.getElementById("information").style.visibility = "visible";
		document.getElementById("information").innerHTML = pop_html;
	}

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

	function MoveHandlerModern(e) {
		if(mousemove) {
			Xpos = e.pageX - 100;
			Ypos = e.pageY + 10;
			document.getElementById("information").style.left = Xpos + "px";
			document.getElementById("information").style.top = Ypos + "px";
			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").style.left = Xpos + "px";
			document.getElementById("information").style.top = Ypos + "px";
		}
	}
