/* 
########################################################
Change backgound color of Text boxes, during onFocus and onBlur events.
########################################################

Cross-browser event handling, ori code by Scott Andrew.
Source: http://www.yourhtmlsource.com/forms/clearingdefaulttext.html
Used with permission.
Original code snippets remodeled by jbh03, June 2006. 
- Dynamically adds event listeners to form fields during page load.
- Fires off FOCUS and BLUR event code, as they occur to TEXT type of <Input> objects. 
Note: original code was used to remove default test from TextBoxes, but it didn't work 100% */   
  
function addEvent(element, eventType, lamdaFunction, useCapture) {
	if (element.addEventListener) {
        element.addEventListener(eventType, lamdaFunction, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + eventType, lamdaFunction);
        return r;
    } else {
        return false;
    }
}

addEvent(window, 'load', init, false);

function init() {
    var formInputs = document.body.getElementsByTagName("input");
    //document.write("formInputs.length = " + formInputs.length + "<br>");   
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];
        if (theInput.type == 'text') {  
            // Add event handlers
            addEvent(theInput, 'focus', YELLOW_bkground, false);
            addEvent(theInput, 'blur', WHITE_bkground, false);                     
        }
    }
}

//target = form field (e) that just fired the Focus or Blur events. 
function YELLOW_bkground(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    target.style.backgroundColor='F0F0F0'; //yellow      //'#f0f0f0'; //gray  //light gray input box background //f0f0f1 light buff
}

function WHITE_bkground(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    target.style.backgroundColor='white';
}

/* 
########################################################
AutoTab functions
########################################################

Source: http://www.codestore.net/store.nsf/unid/DOMM-4QY5WW?OpenDocument
Used with permission.
How it works:
Every time a key is pressed it calls the autoTab function. 
This checks whether the current length of the field is within 
its limits. If not then it is "clipped" and focus is given to 
the next field on the form. Checks are also made as to the 
Unicode character of the key that is pressed to verify it was 
not simply a key such as shift or ctrl or whatever. */

var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab( input, len, e ) {
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if( input.value.length >= len && !containsElement( filter, keyCode ) ) {
	input.value = input.value.slice( 0, len );
	input.form[ (getIndex( input ) + 1 ) % input.form.length ].focus();
}
function containsElement(arr, ele) {
	var found = false, index = 0;
	while(!found && index < arr.length)
	if(arr[index] == ele)
	found = true;
	else
	index++;
	return found;
}
function getIndex(input) {
	var index = -1, i = 0, found = false;
	while (i < input.form.length && index == -1)
	if (input.form[i] == input)index = i;
	else i++;
	return index;
	}
	return true;
}

/*#################################################################
 Conditionally Hide/Unhide a sublist <table> of additional choices 
 #################################################################*/

function DisplaySublist() {
	
    var selectBox_1 = document.getElementById("lstClass_at_CHEC");
    var selectBox_2 = document.getElementById("lstClass_at_School");
    var selected_1 = selectBox_1.options[selectBox_1.selectedIndex].value;
    var selected_2 = selectBox_2.options[selectBox_2.selectedIndex].value;
    var panelsublist = document.getElementById("PnlSubList");
       
    if ( (selected_2 == "") && (selected_1.search("Surviving Adolescence") != -1) )
    {// No selection has been made from selectBox_2,
     // and "Surviving Adolescence" is selected from selectBox_1.	
        panelsublist.style.display = "block"; // displays a sublist <table>
        document.getElementById("lblErrMsg_FieldTrip_SelectOne").innerHTML = "";
        document.getElementById("lblErrMsg_FieldTrip_SelectOnlyOne").innerHTML = "";
    }
    else 
    {
        panelsublist.style.display = "none";  // hides a sublist <table>
        document.getElementById("CheckBox1").checked=false;
        document.getElementById("CheckBox2").checked=false;
        document.getElementById("CheckBox3").checked=false;
        document.getElementById("CheckBox4").checked=false;   
    }
}

/*#################################################################
 Conditionally Hide/Unhide a sublist <table> of additional choices 
 #################################################################*/
/*
function DisplaySublist() {
	
	//alert ("1");
    var selectBox_1 = document.getElementById("lstClass_at_CHEC");
    var selectBox_2 = document.getElementById("lstClass_at_School");
    var selected_1 = selectBox_1.options[selectBox_1.selectedIndex].value;
    var selected_2 = selectBox_2.options[selectBox_2.selectedIndex].value;
    var panelsublist = document.getElementById("PnlSubList");
    //alert ("2");
       
    /*----------------------------------------------------------
    If no selection (selected_2) has been made from selectBox_2,
    see if there is a match in any of these 5 strings:
		"Surviving Adolescence, topics:"
		"  - Contraception"
		"  - Respect in Relationships"
		"  - Pressure"
		"  - Sexually Transmitted Infections"
    -----------------------------------------------------------
    if ( (selected_2                         == "") &&
		((selected_1.search("Surviving")     != -1) || 
         (selected_1.search("Contraception") != -1) || 
         (selected_1.search("Respect")       != -1) || 
         (selected_1.search("Pressure")      != -1) || 
         (selected_1.search("Sex")           != -1)) )
    {	
		if (selected_1.search("Contraception") != -1) document.getElementById("CheckBox1").checked=true;   
		if (selected_1.search("Respect")       != -1) document.getElementById("CheckBox2").checked=true;   
		if (selected_1.search("Pressure")      != -1) document.getElementById("CheckBox3").checked=true;   
		if (selected_1.search("Sex")           != -1) document.getElementById("CheckBox4").checked=true;   

        panelsublist.style.display = "block"; // displays a sublist <table>
        document.getElementById("lblErrMsg_FieldTrip_SelectOne").innerHTML = "";
        document.getElementById("lblErrMsg_FieldTrip_SelectOnlyOne").innerHTML = "";
    }
    else 
    {
        panelsublist.style.display = "none";  // hides a sublist <table>
        document.getElementById("CheckBox1").checked=false;
        document.getElementById("CheckBox2").checked=false;
        document.getElementById("CheckBox3").checked=false;
        document.getElementById("CheckBox4").checked=false;   
    }
}
*/

/*#################################################################
  (2) Character Count functions. Ori code from SmartWebby site.
  http://www.smartwebby.com/DHTML/textbox_characters_counter.asp
  Reworked by jbh03, 20061004.
  #################################################################*/

function CheckLimit(iMaxLength) {
//Called by keydown event.
	var objTextElementToCount = event.srcElement;
	if (objTextElementToCount.value.length == iMaxLength) return false;
}
function TotalCount(objCounterDisplay,iMaxLength) { 
//Called by keyup and onPaste events.
	var objTextElementToCount = event.srcElement;
	if (objTextElementToCount.value.length > iMaxLength) {
	//A paste event was used to enter too large a block of text into the textbox.
		objCounterDisplay.value = objTextElementToCount.value.length;  // enter char cnt
		alert ("A Paste event has casued Max Legnth to be exceeded.\n\nThe text will be truncated.");
		objTextElementToCount.value = objTextElementToCount.value.substring(0,iMaxLength); //truncate
		/*----------------------------------------------------------
		To disable Paste, add the property [onpaste="return false"] to the field,
		but perhaps some people like to spell-check their comments elsewhere, first.
		-----------------------------------------------------------*/
	}
	objCounterDisplay.value = objTextElementToCount.value.length;	
}

	  

