
/*
 ************************************
 *   DHTML functions for AJAX-use   *
 *       © Pascal Pfiffner          *
 ************************************
 */

var resetObj = null;
var resetText = '';
var highlightObj = null;

var is_IE = (navigator.appName.indexOf("Explorer") > -1);


// a link was clicked, handle this
function linkClicked(which) {
	var url = which.getAttribute('href');
	if(!url)
		return false;
	
	// initiate getStuff
	var ourURI = url.replace(/index.pl/, "getStuff.pl");
	
	if(requestPage(ourURI)) {
		
		// while we wait for data, show the user that something is happening
		resetObj = which;
		resetText = which.innerHTML;
		which.innerHTML = _loading ? _loading : "loading...";
		
		// change CSS class
		if(highlightObj && highlightObj.className)
			highlightObj.className = highlightObj.className.replace(/(^| +)active_item/, "");
		which.className = which.className.replace(/(^| +)active_item/, "") + " active_item";
		highlightObj = which;
		which.blur();
		
		return true;
	}
	
	// no HttpRequest-Implementation; abort AJAX and let the browser fetch the whole page
	else {
		return false;
	}
}


// a link was clicked, handle this
function formSubmitted(form, button_id) {
	var url = form.getAttribute('action');
	if(!url)
		return false;
	
	var id = form.parentNode.id ? form.parentNode.id : '';
	var button = getObj(button_id);
	
	// collect POST-vars
	var post_vars = 'get=submit_comment&caller=' + id + '&';
	for(var i = 0; i < form.length; i++) {
		var value = form[i].value ? form[i].value.replace(/&/g, "<AMP>") : '';			// well be reversed by &repAmp (Perl function)
		var value = value ? value.replace(/\+/g, "<PLUS>") : '';
		post_vars += form[i].name + "=" + encodeURI(value) + "&";
	}
	
	
	// do it! And while we wait for data, show the user that something is happening
	if(_cgi && requestPage(_cgi, post_vars)) {
		if(button) {
			button.disabled = true;
			button.value = "sending...";
		}
		
		return true;
	}
	
	// no HttpRequest-Implementation; abort AJAX and let the browser send the form
	else {
		return false;
	}
}


// get the formatted comments for one entry
function getThisComments(event) {
	var which = event.target;
	var entry = which.getAttribute('lang');
	
	return getComments(entry, which);
}

function getComments(entry, caller) {
	if(!entry || !caller) {
		return false;
	}
	
	caller = getObj(caller);
	caller.blur();
	
	// show what is happening
	resetObj = caller;
	resetText = caller.innerHTML;
	caller.innerHTML = _loading ? _loading : "loading...";
	
	// do it!
	if(_cgi && requestPage(_cgi + "?get=comments&caller=" + caller.id + "&entry_id=" + entry)) {
		return true;
	}
	else {
		return false;
	}
}


// get the formatted commentform
function getThisCommentform(event) {
	var which = event.target;
	var entry = which.getAttribute('lang');
	
	return getCommentform(entry, which);
}

function getCommentform(entry, caller) {	
	caller = getObj(caller);
	try {	caller.blur();	} catch(exc) {}
	
	// show what is happening
	resetObj = caller;
	resetText = caller.innerHTML;
	caller.innerHTML = _loading_form ? _loading_form : "loading form...";
	
	// do it!
	if(_cgi) {
		if(requestPage(_cgi + "?get=commentform&caller=" + caller.id + "&entry_id=" + entry)) {
			return true;
		}
	}
	else {
		return false;
	}
}


// handles XML delivered by getStuff.pl, gets called by "receiveXML()"
function handleGetStuff(xmldoc) {
	resetClickedObject();
	if(!xmldoc) {
		return false;
	}
	
	var root = xmldoc.responseXML.documentElement;
	var status = root.getAttribute('status');
	var title = root.getAttribute('title');						title = title ? fromXML(title) : '';		// I hate IE for not having "getAttribute()"...
	var caller = getObj(root.getAttribute('caller'));
	var target = root.getAttribute('target');
	var targettype = root.getAttribute('targettype');
	
	
	// if object got supplied relative by getStuff.pl, indicated by '{xy}', eval:
	if(target && target.match(/^\{([^\}]+)\}$/)) {
		target = eval(target.replace(/\{|\}/g, ''));
	}
	
	var targetelem = getObj(target);
	if(!targetelem) {
		if(confirm("No target defined: " + target + "\n\nDo you wish to see some debug-information?"))
			alert(req.responseText);
		return false;
	}
	
	cleanNode(targetelem);
	
	// do it, dooo it!
	if('OK' == status) {
		var parent = getElementNodes(root);
				
		// *****************************************
		// add options for a menu
		if("select" == targettype) {
			alert('not implemented');
			return;
			/*
			targetelem.appendChild(createOptionElement(0, (_choose ? _choose : "choose...")));
			for(var i = 0; i < child.length; i++) {
				targetelem.appendChild(createOptionElement(child[i].getAttribute('id'), child[i].getAttribute('name')));
			}
			targetelem.disabled = false;	*/
		}
		
		// *****************************************
		// add the comments
		else if("list_inner" == targettype) {
			var elements = getElementNodes(root);
			
			// list comments
			if(elements.length > 0) {
				for(var i = 0; i < elements.length; i++) {
					var first = getElementNodes(elements[i])[0];
					var comment = convertToXHTML(first);
					if(comment) {
						targetelem.appendChild(comment);
					}
				}
			}
			
			// show alt text
			else {
				targetelem.appendChild(createGenericElement('div', false, 'commentblock small', root.getAttribute('alt')));
			}
			
			// show the correct number of items, unhide, and change the link
			if(caller) {
				if(caller.innerHTML) {
					caller.innerHTML = caller.innerHTML.replace(/\d+/, elements.length);
				}
				else if(caller.value) {
					caller.value = caller.value.replace(/\d+/, elements.length);
				}
				show(caller, targetelem);
				
				caller.setAttribute('onclick', "toggle('" + caller.id + "', '" + targetelem.id + "'); return false;");
			}
		}
		
		
		// *****************************************
		// fill with supplied XHTML (e.g. the commentform)
		else if("inner" == targettype) {
			var elements = getElementNodes(root);
			var inner = convertToXHTML(elements[0]);
			
			// append
			targetelem.appendChild(inner);
			
			// unhide and change link
			if(caller) {
				show(caller, inner.parentNode);
				caller.setAttribute('onclick', "toggle('" + caller.id + "', '" + targetelem.id + "'); return false;");
			}
		}
		
		
		// *****************************************
		// default (no type given); replace innerHTML with the XML we got
		else {
			targetelem.innerHTML = 'nothing to do';
		}
		
		
		// if there is an execute-commend, execute it!
		if(root.getAttribute('execute')) {
			eval(root.getAttribute('execute'));
		}
	}
	
	else if(('NOAUTH' == status) || ('DB_ERROR' == status)) {
		var node = ("select" == targettype) ? createOptionElement(0, root.getAttribute('alt')) : createGenericElement('div', false, 'subtitle', root.textContent);
		targetelem.appendChild(node);
	}
	
	else {
		var node = ("select" == targettype) ? createOptionElement(0, "error") : createGenericElement('div', false, 'subtitle', "generic AJAX error");
		targetelem.appendChild(node);
	}
}


// resets object which got changed to "loading..."
function resetClickedObject() {
	if(resetObj) {
		if('select' == resetObj.nodeName.toLowerCase())
			resetObj.text = resetText;
		else if('input' == resetObj.nodeName.toLowerCase())
			resetObj.value = resetText;
		else
			resetObj.innerHTML = resetText;
		resetObj = null;
	}
}


// checks the given input field for a valid email address (on every keystroke)
var validation_input_obj = null;
var validation_mark_obj = null;
var validation_disable_box = null;
var validation_disable_was_checked = false;
var EMAIL_REQUIRED = false;

function mailValidation(input_id, mark_id, disable_id) {
	validation_input_obj = getObj(input_id);
	validation_mark_obj = getObj(mark_id);
	validation_disable_box = getObj(disable_id);
	
	if(validation_input_obj) {
		validation_input_obj.onkeyup = validateEmail;
	}
}

// the actual check
function validateEmail() {
	if(validation_input_obj) {
		
		// check the given string
		if('' != validation_input_obj.value) {
			var valid = validation_input_obj.value.match(/^[\w\d\._\-]+@[\w\d\._\-]+\.\w{2,4}$/);
			if(validation_mark_obj) {
				validation_mark_obj.setAttribute(is_IE ? 'className' : 'class', valid ? '' : 'red');
			}
			if(validation_disable_box) {
				validation_disable_box.disabled = valid ? false : true;
				try {
					if(!valid) {
						validation_disable_was_checked = validation_disable_box.checked;
						validation_disable_box.checked = false;
					}
					else if(valid && validation_disable_was_checked) {
						validation_disable_box.checked = true;
						validation_disable_was_checked = false;
					}
				}
				catch(exc) {  }
			}
		}
		
		// field is empty
		else {
			if(validation_mark_obj) {
				validation_mark_obj.setAttribute(is_IE ? 'className' : 'class', EMAIL_REQUIRED ? 'red' : '');
			}
			if(validation_disable_box) {
				try {
					validation_disable_was_checked = validation_disable_box.checked;
					validation_disable_box.checked = false;
					validation_disable_box.disabled = true;
				}
				catch(exc) {  }
			}
		}
	}
}

function stopMailValidation() {
	if(validation_input_obj) {
		validateEmail();
		validation_input_obj.onkeyup = null;
		validation_input_obj = null;
		validation_mark_obj = null;
		validation_disable_box = null;
	}
}






