/**

	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 window_load(e)
{
	var inputs = document.getElementsByTagName("input");
	var textareas = document.getElementsByTagName("textarea");
	
	//
	//Hook up keypressed events for all textbox inputs
	//
	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++)
	{//alert(textareas[i].id);
		//check the input as it goes in
		addEventHandler("change", sanitizeOnChange, textareas[i]);
	}
	//hook up the mine images on the about us page
	var images=document.getElementById("mineImages");
	if(images)
	{
	    var anchors=images.getElementsByTagName("a");
	    if(anchors)
	    {
	        for(var i=0;i<anchors.length;i++)
	        {
                addEventHandler("click",popUpUrl,anchors[i]);	
	        }
	    }
	}
}//window_load

function popUpUrl(e)
{
    var tgt = e.srcElement || e.target;
    
    window.open(tgt.parentNode.href,'structurePopUp','width=620,height=480,toolbar=no,resizable=no,directories=no,menubar=no,scrollbars=no');
    cancelEventDefault(e);
}
//Called on EVERY keypress event for EVERY text input
function sanitize(e)
{
	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);
	}
}

