/******************************************************************/
/*                       	Inputs v1.1                           */
/*				   An unobtrusive form input styler 			  */
/*              by Tim Novinger, lateapxr [at] gmail.com          */
/******************************************************************/
/* MIT style license:                                             */
/* http://en.wikipedia.org/wiki/MIT_License                       */
/*																  */
/* Changelog:													  */
/* 	 Version 1.1 (April 11th, 2007)								  */
/*	   - Added CSS class based element border ability			  */
/*																  */
/* 	Version 1.0 (April 10th, 2007)								  */
/*	   - Initial build											  */
/*																  */
/*	Supported browsers at time of last update:					  */
/*	  - Firefox 2.0.0.3											  */
/*	  - Camino 1.0.4											  */
/*	  - Internet Explorer 7 RC1+								  */
/*	  - Internet Explorer 6 w/SP2								  */
/*																  */
/*	Planned Improvments											  */
/*	  - Border Toggle On/Off									  */
/*	  - Opera & Safari Support									  */
/*	  - Add Reset/Submit Button Image Replacement				  */
/*																  */
/******************************************************************/

// ==========================================================================================
// ADD EVENTS TO DOM
function addEvnt(element, type, listener, useCapture) {
    if (element.addEventListener) {
        element.addEventListener(type, listener, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + type, listener);
        return r;
    } else {
        return false;
    }
}

// ==========================================================================================
// INITIALIZE
function init() {
	//CHECK FOR JAVASCRIPT SUPPORT
	if(!document.getElementById || !document.createTextNode){return;}
    
	//FIND INPUTS
	var formInputs = document.getElementsByTagName('input');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];
        
        if (theInput.type == 'text' && theInput.className.match('clrInput')) {  
            /* Add event handlers */          
		   addEvnt(theInput, 'mouseover', change, false);
           addEvnt(theInput, 'mouseout', restore, false);
            
           /* Save the current value */
           if (theInput.value != '') {theInput.defaultText = theInput.value;}
		   if (theInput.className) {theInput.defaultClassName = theInput.className;}
        }
    }
	
	//FIND TEXT AREAS
	var formTAs = document.getElementsByTagName('textarea');
    for (var i = 0; i < formTAs.length; i++) {
        var theTA = formTAs[i];
        
        if (theTA.className.match('clrTextArea')) {  
            /* Add event handlers */          
		   addEvnt(theTA, 'mouseover', change, false);
           addEvnt(theTA, 'mouseout', restore, false);
            
           /* Save the current value */
           if (theTA.textContent != '') {theTA.defaultText = theTA.textContent;}
		   if (theTA.className) {theTA.defaultClassName = theTA.className;}
        }
    }
}

// ==========================================================================================
// CHANGE ELEMENTS
function change(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target){return;}
		//CHANGE INPUTS
		if (target.value == target.defaultText) {target.value = '';}
		if (target.className.match('clrInput')) {  
			if (target.className == target.defaultClassName) {target.className = ' inputborder clrInput';}
		}    
		
		//CHANGE TEXTAREAS
		if (target.textContent == target.defaultText) {target.textContent = '';}
		if (target.className.match('clrTextArea')) {  
			if (target.className == target.defaultClassName) {target.className = ' inputborder clrTextArea';}	
		}
}

// ==========================================================================================
// RESTORE ELEMENTS
function restore(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target){return;}
		//RESTORE INPUT
		if (target.value == '' && target.defaultText) {target.value = target.defaultText;}
		
		//RESTORE TEXT AREA
		if (target.textContent == '' && target.defaultText) {target.textContent = target.defaultText;}
		
		//RESTORE CLASS STYLING
		if (target.className != '' && target.defaultClassName) {target.className = target.defaultClassName;}
}
// ==========================================================================================
addEvnt(window, 'load', init, false);