/* Code is organized as follows:
 * 1) Thickbox Code
 * 2) Popup Code
 * 3) Cookie Code
 * 4) CheckEnterForSubmit() function
 */


/********************  Thickbox Begin  ********************/
/*
 * Thickbox 2.1 - One Box To Rule Them All.
 * By Cody Lindley (http://www.codylindley.com)
 * Copyright (c) 2006 cody lindley
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 * Thickbox is built on top of the very light weight jQuery library.
 */
var refreshParent = false;
var scrollMode = false;

//on page load call TB_init
$100(document).ready(TB_init);
$100(window).scroll(TB_changeScrollMode);

//add thickbox to href elements that have a class of .thickbox
function TB_init(){
    $100("a.thickbox").unbind("click").click(function(){
	var t = this.title || this.name || null;	
	var g = this.rel || false;	
	TB_show(t,this.href,g);
	this.blur();
	return false;
	});
}


function TB_show(caption, url, imageGroup) {//function called when the user clicks on a thickbox link
	try {
	    if (document.getElementById("TB_HideSelect") == null) {
	    $100("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
	    //$100("#TB_overlay").click(TB_remove);
	    }
    	
	    if(caption==null){caption=""};
    	
    	
    	
	    //$100("body").append("<div id='TB_load'><img src='img/loadingAnimation.gif' /></div>");
	    TB_load_position();
		
		
        if(url.indexOf("?")!==-1){ //If there is a query string involved
		    var baseURL = url.substr(0, url.indexOf("?"));
        }else{ 
   		    var baseURL = url;
        }
        var urlString = /\.jpg|\.jpeg|\.png|\.gif|\.bmp/g;
        var urlType = baseURL.toLowerCase().match(urlString);
		
		var queryString = url.replace(/^[^\?]+\??/,'');
		var params = TB_parseQuery( queryString );
		
		var pageSize = TB_getPageSize();
		TB_WIDTH = (params['width']*1) + 30;
		if (pageSize[0] < TB_WIDTH)
		    TB_WIDTH = pageSize[0] - 20;
		ajaxContentW = TB_WIDTH - 30;
		
		if (params['height'] == "*")
	    {
	        TB_HEIGHT = pageSize[1] - 50;
	    }
	    else
	    {
		    TB_HEIGHT = (params['height']*1) + 40;
		    if (pageSize[1] < TB_HEIGHT)
		        TB_HEIGHT = pageSize[1] - 70;
		}
		ajaxContentH = TB_HEIGHT - 45;
		
		urlNoQuery = url.split('TB_');
	    var rand_no = Math.random();
        var frameURL = urlNoQuery[0] + "d=" + rand_no;
        if( baseURL != null && baseURL != "" )
        {
		    $100("#TB_window").append("<div id=\"popupTitleBar\"><div id=\"popupTitle\">" + caption + "</div><div id=\"popupControls\"><a href='#' id='TB_closeWindowButton' title='Close'><img class=\"TB_CloseButton\" style=\"margin:0px; border-width:0px\" src=\"/08/images/close.gif\" alt=\"close\" border=\"0\" width=\"24\" height=\"24\" /></a></div></div><div id=\"TB_ContentShadow\">&nbsp;</div><iframe frameborder='0' hspace='0' src='"+frameURL+"' id='TB_iframeContent' name='TB_iframeContent' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;' onload='TB_showIframe()'> </iframe>");
        }else
        {
        	$100("#TB_window").append("<div style=\"display:none;\"></div>");
        }
        
		$100("#TB_closeWindowButton").click(TB_remove);
		
		$100("#TB_ajaxContent").html($100('#' + params['inlineId']).html());

		TB_position();
		
		$100("#TB_load").remove();
		$100("#TB_window").css({display:"block"}); 
		
        $100(window).unbind("resize");
		$100(window).resize(TB_position);
		$100(window).resize(TB_overlaySize);
		
		$100(window).unbind("scroll");
				
	    $100(window).scroll(TB_position);
	    $100(window).scroll(TB_overlaySize);
	    if (scrollMode == false)
	        TB_overlaySizeFirstTime();
	    else
	        TB_overlaySize();
	        
		document.onkeyup = function(e){ 			
			if (e == null) { // ie			   
				keycode = event.keyCode;
			} else { // mozilla
			
				keycode = e.which;
			}
			if(keycode == 27){ // close
				TB_remove();
			}	
		}
		
	} catch(e) {
		alert( e );
	}
}

//helper functions below

function TB_showIframe(){
	
	//$100("#TB_load").remove();
	$100("#TB_window").css({display:"block"});
}

function TB_remove() {		
    $100(window).unbind(); 
    if(refreshParent == true)
    { parent.location.href = parent.location.href;}
	$100('#TB_window,#TB_overlay,#TB_HideSelect').remove();
	$100("#TB_load").remove();
	TB_overlaySize();
	return false;
}

function TB_position() {
    var pagesize = TB_getPageSize();	
	var arrayPageScroll = TB_getPageScrollTop();	
	$100("#TB_window").css({width:TB_WIDTH+"px",left: (arrayPageScroll[0] + (pagesize[0] - TB_WIDTH)/2)+"px", top: (arrayPageScroll[1] + (pagesize[1]-TB_HEIGHT)/2)+"px" });
}

function TB_changeScrollMode()
{
    scrollMode = true;
}

function TB_overlaySizeFirstTime()
{
    $100("#TB_HideSelect").css({"height":"100%","width":"100%"});
	$100("#TB_overlay").css({"height":"100%", "width":"100%"});
}
function TB_overlaySize(){
	if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {	
		yScroll = window.innerHeight + window.scrollMaxY;
		xScroll = window.innerWidth + window.scrollMaxX;
		var deff = document.documentElement;
		var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
		var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
		xScroll -= (window.innerWidth - wff);
		yScroll -= (window.innerHeight - hff);
	} else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
		xScroll = document.body.scrollWidth;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari		
		yScroll = document.body.offsetHeight;		
		xScroll = document.body.offsetWidth;
  	} 
  	
  	$100("#TB_overlay").css({"height":yScroll +"px", "width":xScroll +"px"});
	$100("#TB_HideSelect").css({"height":yScroll +"px","width":xScroll +"px"});
	scrollMode = true;
	//alert("Scroll Time");
	
}

function TB_load_position() {

	var pagesize = TB_getPageSize();
	var arrayPageScroll = TB_getPageScrollTop();
	
	$100("#TB_load")
	.css({left: (arrayPageScroll[0] + (pagesize[0] - 100)/2)+"px", top: (arrayPageScroll[1] + ((pagesize[1]-100)/2))+"px" })
	.css({display:"block"});
}

function TB_parseQuery ( query ) {
   var Params = new Object ();
   if ( ! query ) return Params; // return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) continue;
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}

function TB_getPageScrollTop(){
	var yScrolltop;
	var xScrollleft;
	if (self.pageYOffset || self.pageXOffset) {
		yScrolltop = self.pageYOffset;
		xScrollleft = self.pageXOffset;		
	}else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){	 // Explorer 6 Strict
		yScrolltop = document.documentElement.scrollTop;
		xScrollleft = document.documentElement.scrollLeft;
	}else if (document.body) {// all other Explorers
		yScrolltop = document.body.scrollTop;
		xScrollleft = document.body.scrollLeft;
	}	
	yScrolltop = yScrolltop - 15;	
	arrayPageScroll = new Array(xScrollleft,yScrolltop) 
	return arrayPageScroll;
}

function TB_getPageSize(){	
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
	arrayPageSize = new Array(w,h) 
	return arrayPageSize;
}

 function highlightRows()
{
	if (document.getElementById && document.createTextNode)
	{
		var tables=document.getElementsByTagName('TABLE');
		for (var i=0;i<tables.length;i++)
		{
			if (tables[i].className=='Grid' || tables[i].className=='GridAlt')
			{
				var trs=tables[i].getElementsByTagName('tr');
				for(var j=0;j<trs.length;j++)
				{
					var rowNode = trs[j];
					if (rowNode.className == 'hl')
					{
						rowNode.onmouseover=function(){this.style.backgroundColor='#ffffdc'; };
						rowNode.onmouseout=function(){this.style.backgroundColor='#ffffff'; }; 
					}
				}
			}
		}
	}
} 
