/**
 * Include script for javascript application layer.
 * 
 * @author R.J.T. de Vries <rdevries@thirdwave.nl>
 * @version 1.00, 01/18/2008
 * @access public
 */

//------------------------------------------------------------------------------
// PHASE I: INCLUDE ALL NECESSARY JAVASCRIPT FILES.
//------------------------------------------------------------------------------

document.write("<script src='/inc/jscripts/googleTracking.js'></script>");
document.write("<script src='/inc/jscripts/event.js'></script>");
document.write("<script src='/inc/jscripts/functions.js'></script>");
document.write("<script src='/inc/jscripts/dateinput.js'></script>");
document.write("<script src='/inc/jscripts/formHandler.js'></script>");

//------------------------------------------------------------------------------
// PHASE II: INITIALIZATION FUNCTION, CALLED ON DOCUMENT LOAD EVENT
//------------------------------------------------------------------------------

/**
 * Date input management object, that takes care of automatically continuing to
 * next field input.
 * @var object dateInput
 * @access global
 */
var dateinput = null;

/**
 * Object for handling automatic completions. Currently implements zipcode 
 * completion.
 */
var formHandler = null;

/**
 * Initialization function.
 *
 * @param		boolean		refresh		should the nav-list be refreshed? default to true.
 * @return 	void
 * @access	public
 */
function init() {
	var inputs, textareas, objs, i;
			
	// add events to the username and password input elements.
	inps = document.getElementsByTagName('input');
	addedTo = [];
	for ( i = 0; i < inps.length; i++ ) {
		if ( inps[i].className.indexOf('deftxt') != -1 ) {
			addEvent(inps[i], 'focus', removeDefTxt);
			addEvent(inps[i], 'blur', restoreDefTxt);
			if ( !in_array(inps[i].form, addedTo) ) {
				addEvent(inps[i].form, 'submit', removeDefaultTexts);
				addedTo.push(inps[i].form);
			}
			if ( inps[i].getAttribute('defaultText') !== inps[i].value ) {
				inps[i].className = inps[i].className.replace(/ ?deftxt/, '');
			}
			if ( inps[i].getAttribute('value') !== inps[i].value ) {
				inps[i].className = inps[i].className.replace(/ ?deftxt/, '');
			}
		}
	}
	// bust out of frame.
	if ( self.parent.frames.length != 0 ) {
		self.parent.location = document.location;
	}

	addEvent(document.body, 'selectstart', function() {
		var e = window.event, eventSrc = getEventSrc(e);
		if ( eventSrc.tagName.toLowerCase() == 'input' || eventSrc.tagName.toLowerCase() == 'textarea' ) {
			return true;
		} else {
			return false;
		}
	});
	
	objs = document.getElementsByTagName('object');
	for ( i = 0; i < objs.length; i++ ) {
		objs[i].outerHTML = objs[i].outerHTML;
	}

	// initialize dateinput.
	dateinput = new Dateinput;
	dateinput.init();
	
	// initialize the formHandler
	formHandler = new FormHandler;
	formHandler.init();
} // init()

/**
 * Remove the default text from inputs to which this event was attached.
 * 
 * @param		object	[e]	event object for Mozilla based browsers.
 * @return	void
 */
function removeDefTxt(e) {
	if ( !e ) e = window.event;
	var eventSrc = getEventSrc(e);
	if ( eventSrc.getAttribute('defaultText') == eventSrc.value 
			&& eventSrc.className.match(/deftxt/)) {
		eventSrc.value = '';
		eventSrc.className = eventSrc.className.replace(/ ?deftxt/, '');
	}
	setTimeout( function() { eventSrc.focus(); }, 100);
} // removeDefTxt()

/**
 * A function to remove all default texts from the defaultText fields when the
 * form is submitted.
 * 
 * @param e
 * @return
 */
function removeDefaultTexts(e) {
	var it,
			eventSrc,
			inps;
			
	e = e || window.event;
	eventSrc = getEventSrc(e);
	
	inps = document.getElementsByAttribute('defaultText', false, 'input', eventSrc);
	for ( it in inps ) {
		if ( inps[it].className.match(/ ?deftxt/) ) {
			inps[it].value = '';
		}
	}
} // removeDefaultTexts()

/**
 * Restore the default value for inputs to which this event was attached.
 * 
 * @param		object	[e]	event object for Mozilla based browsers.
 * @return	void
 */
function restoreDefTxt(e) {
	if ( !e ) e = window.event;
	var eventSrc = getEventSrc(e);
	if ( eventSrc.value == '' ) {
		eventSrc.value = eventSrc.getAttribute('defaultText');
		eventSrc.className = trim(eventSrc.className + ' deftxt');
	}
} // restoreDefTxt()

window.onload = init;

