﻿/************************************************************
*
*  TL Master.js
*
*  Do not modify this file.  Website specific JS should be in Site.js or in a page-specific JS file.
*  
************************************************************/
/*This is to accomodate the move player needing $ to be their wrapper for document.getElementById
so all jquery on the client front end of this application will use $ now instead of $. 
It needs to be done in the master to get that variable declared in time to safely execute
this section's code.  We may want to consider just doing this by default on all our projects anyway. 
*/
//var $ = jQuery.noConflict();

$(document).ready(function(e){window_load(e);});
var inputs;
function window_load(e)
{
 	var inputs = $("input");//document.getElementsByTagName("input");
	var textareas = $("textarea"); //document.getElementsByTagName("textarea");
	var images = $("img");//document.getElementsByTagName("img");
	//
	//Hook up keypressed events for all textbox inputs
	//
	inputs.keypress(function(e)
	{
	
	    sanitize(e);
	});
	inputs.change(function(e)
	{
	    sanitizeOnChange(e);
	});

	//
	//Hook up onChange events for all textarea inputs
	//
	textareas.change(function(e)
	{
	    sanitizeOnChange(e);
	});
	 
     //hook up any anchor tags with the css class popUp
     $("a.popup").click(function(e)
     {
        popUpLink(e);
     });
     
     addEventHandler("keypress",noPostBack,window);
   
    // call the site window onLoad function if it exists
    if (typeof Site_Window_Load == 'function')
    {
        Site_Window_Load(e);
    }
    // call the page-specific window onLoad function if it exists
    if (typeof Page_Window_Load == 'function')
    {
        Page_Window_Load(e);
    }
	 
}

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)
	{
		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);
	}
}


///old xbelem stuff that I am keeping for the time being
function cancelEventDefault(evt)
{
	//if (evt.preventDefault == null)
	if(!evt.preventDefault)
	{
		evt.returnValue = false;
	}
	else
	{
		evt.preventDefault();
	}	
}
function addEventHandler(eventName, identifier, node)
{
	node = this.DOMNode || node;//if called from 'tieToButton' the node is passed in because 'this' refers to the window.
	
	eventName = eventName.replace(/^on/i, "");	//Allow user to include "on" at in the event name. E.G. "onclick" vs "click"
	
	if (typeof node.attachEvent != "undefined")
	{
		node.attachEvent("on" + eventName, identifier);
	}
	else if (typeof node.addEventListener != "undefined")
	{
		node.addEventListener(eventName, identifier, false);
	}
}


