// JavaScript Document

//set <ENTER> key code
var ENTER = 13;

$(function(){
	$('#AlertBox').dialog({
		modal:true,
		height:'auto',
		autoOpen:false
	});
	$('.warning').fadeIn('slow');
	$('.datepicker').datepicker({
		mandatory:true,
		closeText:'X',
		speed:'slow',
		buttonText:'Pick Date',
		buttonImage: '/i/calendar_icon.jpg'
	});
	
	$('.Warning').hide();
	$('.Warning').fadeIn('slow');
	
	AddTextAreaDefault('#SubscribeEmailAddress', "Type Email Address");
	AddTextAreaDefault('#BlogSearch', "Enter Search Terms");
	
});

function SelectTab(Id, TabBoxName){
	//Make all tabs inactive
	$("div[@id^='"+TabBoxName+"_Content_']").attr('style', 'display:none');
	$("td[@id^='"+TabBoxName+"_TabLeft_']").attr('class', 'TabLeftNotSelected');
	$("td[@id^='"+TabBoxName+"_TabCenter_']").attr('class', 'TabCenterNotSelected');
	$("td[@id^='"+TabBoxName+"_TabRight_']").attr('class', 'TabRightNotSelected');
	
	//Active the selected tab
	$('#'+TabBoxName+'_Content_'+Id).attr('style', 'display:block');
	$('#'+TabBoxName+'_TabLeft_'+Id).attr('class', 'TabLeftSelected');
	$('#'+TabBoxName+'_TabCenter_'+Id).attr('class', 'TabCenterSelected');
	$('#'+TabBoxName+'_TabRight_'+Id).attr('class', 'TabRightSelected');
}

String.prototype.trim=function(){
    return this.replace(/^\s*|\s*$/g,'');
}

/////////////////////////////////
// Start Dialog Box Stuff
/////////////////////////////////

function CreateAjaxDialog(UniqueId, Title, Width, Height){
	if(Height < 0)
		Height = 'auto';
	$('#'+UniqueId).dialog({
		autoOpen:false,
		modal:true,
		title:Title,
		height:Height,
		width:Width
	});
}
	
function AlertBox(Text){
	var CloseText = '<br /><br /><div align="center"><input type="button" class="button" onclick="$(\'#AlertBox\').dialog(\'close\');" value="OK" /></div>';
	$('#AlertBox').get(0).innerHTML = Text+CloseText;
	$('#AlertBox').dialog('open');
}

function CreateHTMLDialog(UniqueId, Title, Width){
	$('#'+UniqueId).dialog({
		autoOpen:false,
		modal:true,
		title:Title,
		height:'auto',
		width:Width	
	});
}

/////////////////////////////////
// End Dialog Box Stuff
/////////////////////////////////



//////////////////////////////////////////////
// got this code from http://blog.josh420.com/archives/2007/11/determine-if-any-other-outside-element-was-clicked-with-javascript.aspx
// It is used to detect clicks outside of an element (to close that element)
///////////////////////////////////////////////

function clickedOutsideElement(elemId, evt) {
var theElem = '';
if(window.event)
theElem = getEventTarget(window.event);
else theElem = getEventTarget(evt);

while(theElem != null) {
if(theElem.id == elemId)
return false;

theElem = theElem.offsetParent;
}

return true;
}

function getEventTarget(evt) {
var targ = (evt.target) ? evt.target : evt.srcElement;

if(targ != null) {
if(targ.nodeType == 3)
targ = targ.parentNode;
}

return targ;
}
/*
document.onclick = function(evt) {
if(clickedOutsideElement('AlertBox', evt))
	$('.AlertBox').slideUp('fast');

}
*/
//////////////////////////////////////////////
// End Code Snippet
///////////////////////////////////////////////


/*
Call this function on a form field to add text that will appear until they first
click on the field
*/
function AddTextAreaDefault(Selector, DefaultText){
	$(Selector).each(function(){
		if($(this).val() == '' || $(this).val() == DefaultText){
			$(this).css('color', '#999999');
			$(this).val(DefaultText);
			$(this).css('color', '#999999');
			$(this).bind('focus', function(){
				if($(this).css('color') == '#999999' || DefaultText == $(this).val()){
					$(this).val('');
					$(this).css('color', '#333333');
				}
			});
			$(this).bind('blur', function(){
				if($(this).val() == ''){
					$(this).val(DefaultText);
					$(this).css('color', '#999999');
				}
			});
		}
	});
}
