/**

	For ThoughtLab's Web Page implementation we only need this on Contact Us at this point in time.
	* Utitlities.js
	* XBElem.js
	* InputSanitizer.js
	
	*Note.  This file has to be loaded after the files it depends on in order to execute properly.

**/
addEventHandler("load", window_load, window);
function clearMe(e)
{
    var target = e.srcElement || e.target;
    if(!target.isChanged)
    {
       target.value="";
    }
    else
    
    target.isChanged=true;
    
}
function window_load(e)
{
    
	var inputs = document.getElementsByTagName("input");
	var textareas = document.getElementsByTagName("textarea");
	
	//
	//Hook up keypressed events for all textbox inputs
	//
	var targetTxt=document.getElementById("ctl00_sendPageControl_tbTargetAddress");
	
	if(targetTxt)
	{
	    targetTxt.isChanged=false;
	    addEventHandler("focus",clearMe,targetTxt);
	}
	for (var i = 0;i<inputs.length;i++)
	{
		if (inputs[i].type == "text")
		{
			//check the input as it goes in
			addEventHandler("keypress", sanitize, inputs[i]);
			
			//and check the input as it goes out
			addEventHandler("change", sanitizeOnChange, inputs[i]);
			
			
		}
	}
	
	//
	//Hook up onChange events for all textarea inputs
	//
	for (var i = 0;i<textareas.length;i++)
	{
		addEventHandler("change", sanitizeOnChange, textareas[i]);
	}		
	 addEventHandler("keypress",noPostBack,window);
}//window_load
function noPostBack(e)
{
    
    if (e.keyCode != 13) {return;}
    cancelEventDefault(e);
}

//Called on EVERY keypress event for EVERY text input
function sanitize(e)
{
    /*IE uses keycode on this event
    Firefox uses charcode for characters, keycode for the arrow keys etc.
    IE doesn't register a key press event for the arrow keys, but firefox uses
    the keycode 39 for the right arrow.  That matches the charcode 39 for the apostraphe
    making the arrow key throw the alert error in firefox.*/
    if(typeof e.isChar != undefined)
	{	    
	    if(e.charCode==0)
	    {
	        return;
	    }
	}
	
	var code = e.charCode || e.keyCode;
	var match = String.fromCharCode(code).match(/[<>']/);
	
	if (match)
	{
		XBElem.prototype.cancelEventDefault(e);
		alert("Invalid character. " + match[0] + " is not allowed.");
	}
}

function sanitizeOnChange(e)
{
	var tgt = e.srcElement || e.target;
	/*NOTE: THE ONLY USE FOR TEXT AREAS ON A STATIC SITE IS TO SEND COMMENTS TO THE WEBMASTER.  THERE IS
	NO DATA ACCESS SO WE ARE ALLOWING THE '*/
	var reBaddies = new RegExp("[<>]", "g");
	var match = tgt.value.match(reBaddies);
	var baddies = "";
	tgt.value = tgt.value.replace(reBaddies, "");
	
	if (match)
	{
		for (var i = 0;i<match.length;i++)
		{
			if (baddies.indexOf(match[i]) == -1)
			{
				baddies += "\n" + match[i];
			}
		}
		
		alert("The following characters are not allowed and have been removed: " + baddies);
	}
}

