var SearchResults = 
    {
        Link: { Id:'' },
        GridView: { Id:'' },
        Init: function(lid,gid) { this.Link.Id = lid; this.GridView.Id = gid; }        
    }

function ToggleSearchResultsView(obj,id)
{
    if (docObj(SearchResults.Link.Id))
    {
        docObj(SearchResults.Link.Id).className = '';
        obj.className = 'selected';
    }
    if (docObj(SearchResults.GridView.Id))
    {
        docObj(SearchResults.GridView.Id).style.display = 'none';
        docObj(id).style.display = 'block';  
    }
    
    SearchResults.Link.Id = obj.id;
    SearchResults.GridView.Id = id;
}

var TerritoryPopup = {
        ActiveId:false,
        Init: function() {
                document.onmousemove = TerritoryPopup.Move;
                if (document.captureEvents)
                    document.captureEvents(Event.ONMOUSEMOVE);
            },
        Move: function(e) {
                if (TerritoryPopup.ActiveId)
                {
                    var coords = findMouse(e);
                    var obj = docObj('divTerritory-'+TerritoryPopup.ActiveId);                                               
                    obj.style.top  = (coords.y - parseInt(obj.offsetHeight)/2)+15 + 'px';
                    obj.style.left = (coords.x - parseInt(obj.offsetWidth) - 30) + 'px';
                }            
            },
        Show: function(obj,id,color) {
                var targ = docObj('divTerritory-'+id);
                if (targ && ! this.ActiveId)
                {                    
                    // ensure the last popup is being hidden
                    this.Hide();                      
                    targ.className = 'territory_popup';
                    targ.style.borderColor = '#'+color;
                    this.ActiveId = id;
                }
            },
        Hide: function() {
                if (this.ActiveId)
                {
                    var obj = docObj('divTerritory-'+this.ActiveId);
                    obj.className = 'territory_popup_off';
                    this.ActiveId = false;
                }
            } 
}



if (document.captureEvents)
	document.captureEvents(Event.ONKEYPRESS);

function charWatch(obj, type, e)
{
	var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
	if (key == 13)
		return true;

	var proceed = false; // preliminary conditional
	var allow = false;   // final value to return
		
	switch (type)
	{
	    case 'numeric':
	        // if a number was not pressed
	        proceed = (key < 48 || key > 57);
	        break;
	    case 'email':
	        // if a valid email chars were not pressed
	        proceed = (key < 48 || key > 57); // check numbers
	        if (proceed)
	        {
	            proceed = (key < 64 || key > 90); // check upper case
	            if (proceed)
	            {
	                proceed = (key < 97 || key > 122) // check lower case
	                if (proceed)
	                    proceed = (key != 45 && key != 46 && key != 95); // check -_@
	            }
	        }
	        break;
	}

	if (proceed)
	{
		// check for other keys that have special purposes
		if( key != 8 /* backspace */ && key != 9 /* tab */ &&   key != 13 /* enter */ &&
			key != 35 /* end */ &&      key != 36 /* home */ && key != 37 /* left */ &&
			key != 39 /* right */ &&	key != 46 /* del */
		   )
			allow = false;
		else
		{
			// for detecting special keys (listed above)
			// IE does not support 'charCode' and ignores them in keypress anyway
			if(typeof e.charCode != "undefined")
			{
				// special keys have 'keyCode' and 'which' the same (e.g. backspace)
				if(e.keyCode == e.which && e.which != 0)
					allow = true;
					
				// or keyCode != 0 and 'charCode'/'which' = 0
				else if(e.keyCode != 0 && e.charCode == 0 && e.which == 0)
					allow = true;
			}
		}
	}
	else
		allow = true;

	return allow;
}

function docObj(id)
{
    if (document.getElementById(id))
        return document.getElementById(id);
    else
        return false;
}

function findMouse(e)
{
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)
	{ posx = e.pageX; posy = e.pageY; }
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	return { x:posx, y:posy };
}

function findPos(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent)
	{
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent)
		{
			curleft += obj.offsetLeft
			curtop  += obj.offsetTop
		}
	}
	return { left:curleft, top:curtop };
}
function doInjectionProtection()
{
	if (! docObj('InjectionFree'))
	{
	    var hdn = document.createElement('input');
		    hdn.setAttribute('name','InjectionFree'); hdn.setAttribute('id', 'InjectionFree');
		    hdn.setAttribute('type', 'hidden');
		    document.forms[0].appendChild(hdn);
	}
}
