
function getElement(id){
	if (document.getElementById)
		return document.getElementById(id);
	if (document.all)
		return document.all[id];
	
	return null;
}

		
function getDocHeight() {  
	 var viewportheight;
	 
	 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	 if (typeof window.innerHeight != 'undefined') { 
	      viewportheight = window.innerHeight;
	 }
	 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)	
	 else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientHeight !='undefined' && document.documentElement.clientHeight != 0) {
	       viewportheight = document.documentElement.clientHeight;
	 }
	 
	 // older versions of IE	 
	 else { 
	       viewportheight = document.getElementsByTagName('body')[0].clientHeight;
	 }
	return viewportheight; 
}

		
function getDocWidth() { 
	var viewportwidth;
	 
	 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight	 
	 if (typeof window.innerWidth != 'undefined') {
	    viewportwidth = window.innerWidth; 
	 }
	 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)	
	 else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0) {
		viewportwidth = document.documentElement.clientWidth;
	 }
	 
	 // older versions of IE	 
	 else {
	   	viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
	 }
	return viewportwidth; 
}

// jquery popupwindow profiles
/* 
height:600, // sets the height in pixels of the window.
width:600, // sets the width in pixels of the window.
toolbar:0, // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
scrollbars:0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
resizable:1, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
left:0, // left position when the window appears.
top:0, // top position when the window appears.
center:0, // should we center the window? {1 (YES) or 0 (NO)}. overrides top and left
createnew:1 // should we create a new window for each occurance {1 (YES) or 0 (NO)}.
 */
var profiles = { 
	mini: {
		height:200,width:200,status:1,resizable:0
	}, 
	miniCenter: {
		height:200,width:400,center:1,resizable:1,scrollbars:1 
	},		
	mediumCenter: {
		height:400,width:800,center:1,resizable:1,scrollbars:1
	},		
	largeCenter: {
		height:600,width:1000,center:1,resizable:1,scrollbars:1
	},		
	miniPopCenter: {
		height:200,width:400,center:1,resizable:1,toolbar:0,scrollbars:1
	},		
	mediumPopCenter: {
		height:400,width:800,center:1,resizable:1,toolbar:0,scrollbars:1
	},		
	largePopCenter: {
		height:600,width:1000,center:1,resizable:1,toolbar:0,scrollbars:1
	},		
	fullPopCenter: {
		height:screen.height,width:screen.width,center:1,resizable:1,toolbar:0,scrollbars:1
	},		
	fullReportConsole: {
		height:screen.height,width:screen.width,center:1,resizable:1,toolbar:1,scrollbars:1
	},		
	largeNewWindow: {
		height:600,width:1000,center:1,resizable:1,toolbar:1,scrollbars:1,status:1,createnew:1,location:1,menubar:1  
	},	
	windowNotNew: {
		height:300,width:400,center:1,createnew:0
	}
};

$(function() { 
	$('.detail-expander').livequery('click',function(event) { 
	// note, we assume text content of more &raquo; and less &laquo;	
	// we also assume a title of Expand Details and Collapse Details
		event.preventDefault();	
		var which = $(this).attr('target'); 
		if(!which.length) { return false; } 
		if($('#' + which).is(':hidden')) { 
			$(this).html('less &laquo;');
			$(this).attr('title','Collapse Details');
			$('#' + which).show('normal'); 
		}
		else { 
			$('#' + which).hide('normal'); 
			$(this).html('more &raquo;');
			$(this).attr('title','Expand Details');
		}
	});
});


$(function() { 
	$('.popupwindow').livequery('click',function(event) { 
		event.preventDefault();
		// this is a mod of jquery.popupwindow.js
		var settings, parameters, mysettings, b, a, winname;
		
		// overrides default settings
		mysettings = ($(this).attr("rel") || "").split(",");
		
		// default profile
		settings = { 
			height:600, // sets the height in pixels of the window.
			width:600, // sets the width in pixels of the window.
			toolbar:0, // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
			scrollbars:0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
			status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
			resizable:1, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
			left:0, // left position when the window appears.
			top:0, // top position when the window appears.
			center:0, // should we center the window? {1 (YES) or 0 (NO)}. overrides top and left
			createnew:1, // should we create a new window for each occurance {1 (YES) or 0 (NO)}.
			location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
			menubar:0 // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
		};

		// if mysettings length is 1 and not a value pair then assume it is a profile declaration
		// and see if the profile settings exists

		if(mysettings.length == 1 && mysettings[0].split(":").length == 1) {
			a = mysettings[0];
			// see if a profile has been defined
			if(typeof profiles[a] != "undefined") {
				settings = jQuery.extend(settings, profiles[a]);
			}
		}
		else {
			// overrides the settings with parameter passed in using the rel tag.
			for(var i=0; i < mysettings.length; i++) {
				b = mysettings[i].split(":");
				if(typeof settings[b[0]] != "undefined" && b.length == 2) {
					settings[b[0]] = b[1];
				}
			}
		}

		// center the window
		if (settings.center == 1) {
			settings.top = (screen.height-(settings.height + 110))/2;
			settings.left = (screen.width-settings.width)/2;
		}
		
		parameters = "location=" + settings.location + ",menubar=" + settings.menubar + ",height=" + settings.height + ",width=" + settings.width + ",toolbar=" + settings.toolbar + ",scrollbars=" + settings.scrollbars  + ",status=" + settings.status + ",resizable=" + settings.resizable + ",left=" + settings.left  + ",screenX=" + settings.left + ",top=" + settings.top  + ",screenY=" + settings.top;
		
		winname = settings.createnew ? 'PopUpWindow' + Math.random().toString() : 'PopUpWindow'; 
		if(!$(this).attr("suppress")) { // added by kjm 1/1/2009 as a way of suppressing the event if the result of a confirm dialog
			window.open($(this).attr('href'),name,parameters).focus();
		}
		return false; 
	});

});

var refreshRate = 15; 
function autoReload(counterid) { 
	if(refreshRate != 1) { 
		refreshRate--; 
		if(document.getElementById(counterid)) { 
			document.getElementById(counterid).innerHTML = refreshRate; 
		}
	}
	else { 
		location.href = location.href; 
		// self.location.reload(true); 
	}
	setTimeout(function() { autoReload(counterid) },1000); 
} 
				
function setDivScroller(which,offset) { 
	var which = (which != null) ? which : 'bodydiv'; 
	var offset = (offset != null) ? offset : 70;  
	$('#' + which).height(1); 
	// $('#' + which).height($(document).height() - offset); 
	$('#' + which).height($(document).height() - offset);
}	

$(function() {
	$("tr.trselector").livequery(function() { 
		$(this).hover( 
			function() { $(this).addClass("trselector-hover");}, 
			function() { $(this).removeClass("trselector-hover");}
		);
	});
}); 

$(function() { 
	$("tr.trhighlighter")
		.live("mouseover",function() { $(this).addClass("trhighlighter-hover"); }) 
		.live("mouseout",function() { $(this).removeClass("trhighlighter-hover"); })
}); 
	
$(function() { 
	$(".btclick").livequery(function() { 
		$(this).bt(
			$(this).attr('title'),
			{
				trigger: ['click','mouseout']
			}
		);
	});	
});
	
$(function() { 
	$(".bthover").livequery(function() { 
		$(this).bt(
			$(this).attr('title'),
			{
				trigger: ['mouseover','mouseout']
			}
		);
	});	
});
	
$(function() { 
	$("input,textarea,select,checkbox").livequery(function() { 
		if($(this).attr('title').length) { 
			var pos = 'right';
			var ac = 'bt-active';
			if($(this).is(':text') || $(this).is(':password')) { ac = 'bt-active-text'; }
			if($(this).is(':checkbox') || $(this).is(':radio')) { ac = 'bt-active-checkbox'; }
			if($(this).attr("rel") == 'btnone') { return; }
			else if($(this).attr("rel") == 'btbottom') { pos = 'bottom'; }
			else if($(this).attr("rel") == 'btleft') { pos = 'left'; }
			else if($(this).attr("rel") == 'bttop') { pos = 'top'; }
			else if($(this).attr("rel") == 'btany') { pos = 'most'; }
			$(this).bt(
				$(this).attr('title'),
				{
					trigger: ['focus', 'blur'],
					positions: [pos],
					fill: '#ffff80',
					activeClass: ac
				}
			);
		};
	});	
}); 

$(function() { 
	$(".fg-button").livequery(function() { 
		$(this).hover(
			function() { 
				// $(this).removeClass("ui-state-default");
				$(this).addClass("ui-state-hover");
			},
			function() { 
				$(this).removeClass("ui-state-hover");
				// $(this).addClass("ui-state-default");
			}
		);
	});
});

function loadMainData(url) {
	var url = (url != null) ? url : ''; 
	if(url.length) {  	
		showNavThrobber(); 
		$('#main-data').load(url,
			function() {  
				hideNavThrobber();
			}
		);	
	}
}

function resortDataTable(url,so,sortdir,suffix) { // allows th's in data table to fire a resorted load
	var url = (url != null) ? url : ''; 
	var so = (so != null) ? so : 'id'; 
	var sortdir = (sortdir != null) ? sortdir : 'asc'; 
	var suffix = (suffix != null) ? suffix : ''; 
	var token = '?'; 
	if(url.length) {  	
		if(url.indexOf('?') > -1) { token = '&'; }
		url += token + 'so=' + so + '&sortdir=' + sortdir + suffix; 		
		showNavThrobber();
		$('#main-data').load(url,
			function() { 
				hideNavThrobber(); 
			}
		);	
	}
}

var loadInDialog = function(href,title,modal,callback,selfclose,autoclose,width,height){ 
	var href = (href != null) ? href : ''; 
	var title = (title != null) ? title : 'Untitled'; 
	var selfclose = (selfclose != null) ? selfclose : false;  
	var autoclose = (autoclose != null) ? autoclose : -1;  
	var modal = (modal != null) ? modal : false;   
	var width = (width != null) ? width : 600; 
	var height = (height != null) ? height : 400; 
	if(!href.length) { 
		return false; 
	} 
	
	$('#general-dialog').dialog({
		width: width, 
		height: height,
		modal: modal, 
		closeOnEscape: true, 
		stack: true, 
		title: title,
		dialogClass: 'std-dialog-primary',
		close: function(event, ui) { $(".flash").css("visibility","visible"); $(this).dialog('destroy'); }, 
		open: function() { 
			$(".flash").css("visibility","hidden");
			if(selfclose) { 
				$('div.ui-dialog a.ui-dialog-titlebar-close').addClass('none'); 
			} 
			if(autoclose > -1) { 
				$(this).load(href,null,function() { setTimeout('location.href = location.href',1000); }); 
			} 
			else { 
				$(this).load(href); 
			} 
		}	
	}); 
	if(callback != null) { (typeof callback == 'string') ? window.location.href = callback : callback(); }
	return false;
}


var confirmDialog = function(msg,title,modal,callback,loadhere){ 
	var msg = (msg != null) ? msg : ''; 
	if(!msg.length) { return false; } else { msg = msg.replace(/\n/gi,'<br \/>'); } 
	var title = (title != null) ? title : 'Untitled'; 
	var modal = (modal != null) ? modal : false;  
	var loadhere = (loadhere != null) ? loadhere : false;
	
	$('#confirm-dialog').dialog({
		width: 600, 
		modal: modal, 
		closeOnEscape: true, 
		stack: true, 
		title: title,
		dialogClass: 'std-dialog-confirm',
		buttons: { 
			"Ok": function() { 
				if(callback != null) { 
					if(typeof callback == 'string') {
						if(loadhere) { 
							$(this).parent().find('div.ui-dialog-buttonpane:first').hide(); 
							$(this).html('<div class="pad"><div class="inline-throbber" style="display: block;"><div>Processing ... one moment please</div></div></div>'); // loading bar
							$(this).load(callback,null,function() { setTimeout('location.href = location.href',1000); } );
						} 
						else { 
							window.location.href = callback; 
						} 
					} 
					else { 
						callback(); 
					} 
				}
			},
			"Cancel": function() { $(".flash").css("visibility","visible"); $(this).dialog('destroy'); }
		},
		close: function(event, ui) { $(".flash").css("visibility","visible"); $(this).dialog('destroy'); }, 
		open: function() { $(".flash").css("visibility","hidden"); $(this).parent().find('a.ui-dialog-titlebar-close:first').addClass('none'); $(this).html('<div class="pad">' + msg + '</div>'); } 	
	}); 
	return false;
}

function resetIframe() { 
	$('#modalIFWindow').jqmHide(); 
	$('#jqmIFModalTitleText').html('Untitled');
	$('#jqmIFContent').html('').attr('src', 'about:blank'); 
	self.location.href = document.location.href; 
}

function markMessageLIRead(which) { 
	$('#' + which).addClass('message-read'); 
	$('#' + which + ' span').addClass('lite'); 
	$('#' + which + ' span').html(' [read]'); 
}

function markMessageTRRead(which) { 
	$('#' + which).addClass('message-read'); 
	$('#' + which + ' td span.status').html('Read'); 
} 

function showNavThrobber() { 
	$('#navthrobber').css('visibility','visible'); 
}

function hideNavThrobber() { 
	$('#navthrobber').css('visibility','hidden'); 
}

function checkboxCheckAll(id) { 
	var id = (id != null) ? id : ''; // parent element within which to check checkboxes
	if(id.length) { 
		$('#' + id + ' input:checkbox').attr('checked',true);
	}
	return false;
}

function checkboxUncheckAll(id) { 
	var id = (id != null) ? id : ''; // parent element within which to check checkboxes
	if(id.length) { 
		$('#' + id + ' input:checkbox').removeAttr('checked');
	}
	return false;
}



/* 
function openMessageBroadcaster(url) { 
	var url = (url != null) ? url : ''; 
	if(url.length) {
		$('#broadcast-message').dialog('open');
		$('#broadcast-message').load(url);
		return false;
	}
} */

function openMessageBroadcaster(url) { 
	var url = (url != null) ? url : ''; 
	loadInDialog('broadcast-message',url,'Broadcast System Message',true); 
	return false; 
} 

function initTinyMCEEditor(which) { 
	tinyMCE.init({
		mode : "exact",
		elements: which,
		theme : "advanced",
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote",
		theme_advanced_buttons2 : "undo,redo,|,link,unlink,anchor,image,|,help",
		theme_advanced_buttons3 : "",
		theme_advanced_buttons4 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left", 
		theme_advanced_resizing : false
	});
}

function initTinyMCEEditorAdmin(which) { 
	tinyMCE.init({
		mode : "exact",
		elements: which,
		theme : "advanced",
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,|,code,|,help",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_buttons4 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left", 
		theme_advanced_resizing : false,
		encoding : 'raw'
	});
}

function returnLetterGrade(num) { 
	// takes a number from 1-100 and returns the american letter grade equivalent
	var num = (num != null) ? num : 0; 
	if(num >= 90) { 
		return "a"; 
	}
	else if(num >= 80) { 
		return "b"; 
	}
	else if(num >= 70) {
		return "c";
	}
	else if(num >= 60) {
		return "d";
	}
	else {
		return "f";
	}	
}
			
function setRunDetails(obj) { 
	var runid = obj.id; 
	for(var o in obj) { 
		if(o != 'id') { 
			$('#rs' + runid + '_' + o).html(obj[o]); 
		}
	} 
	if(obj.complete) { 
		$('#rs' + runid + '_sitescore').html(obj.sitescore);		
		$('#rs' + runid + '_sitescorecell').html(obj.sitescore);		
		$('#rs' + runid + '_architecturehealth').html(obj.architecturehealth);		
		$('#rs' + runid + '_backlinkshealth').html(obj.backlinkshealth);		
		$('#rs' + runid + '_contenthealth').html(obj.contenthealth);			
		$('#rs' + runid + '_badge').addClass('psbadge-' + returnLetterGrade(obj.sitescore));	
		$('#rs' + runid + '_badge').show();	
		$('#rs' + runid + '_reports').show();
		$('#rs' + runid + '_summary').stopTime('rs' + runid + '_throb');	
		$('#rs' + runid + '_summary').unblock();	
		removeTrackedRun(runid);
	}
	else {
		$('#rs' + runid + '_message').html(obj.status);	
	} 
}

// removes values from an array; returns the array with values removed
function removeVal(arr, valToRemove){
	if(!arr || !arr.length) return arr;
	var j = new Array(); 
	for(var i = 0; i < arr.length; i++) { 
		if(arr[i] != valToRemove) { 
			j[j.length] = arr[i];
		}
	}
	return j;
} 

// keep track of runs on detail page
var runTracker = new Array();

function addTrackedRun(runid) { 
	if(!runid) return false; 
	runTracker[runTracker.length] = runid; 
}
function removeTrackedRun(runid) { 
	var mightreload = false;
	if(!runid || !runTracker.length) return false; 
	if(runTracker.length == 1) mightreload = true; // if there is only a single run in runTracker, it might be the one we're handling
	runTracker = removeVal(runTracker,runid); 
	if(!runTracker.length && mightreload) { // if there was one run in runtracker, and now there is none, we need to reload the page
		location.href = location.href; 
	}
}
			
function setRunModal(obj) { 
	var runid = obj.id; 
	$('#rs' + runid + '_summary').block({ 
		centerX: false,
		centerY: false,
		message: '<div class="pad10" id="rs' + runid + '_message">Getting status...</div>', 
		css: { border: '1px solid #999' },   
		overlayCSS: {backgroundColor: '#ccc', opacity: '0.5'} 
    }); 
	addTrackedRun(runid); 
}		

function setDivDisabled(which) { 
	$("label,input,select",$("#" + which)).each(function() { 
		$(this).css({ 'color':'#999' });
		$(this).attr('disabled','disabled');
		$(this).click(function(){ alert('Functionality is either disabled or under development');});
	});
}
			
function showProcessingModal() { 
	// shows a processing... page level modal dialog 
	$.blockUI({ 
		centerX: false,
		centerY: false,
		message: '<div class="pad">Processing...please wait</div>', 
		css: { border: '1px solid #999' },   
		overlayCSS: {backgroundColor: '#ccc', opacity: '0.5'} 
    }); 
}

function suggestProjectName(value,target) { 
	var value = (value != null) ? value : ''; 
	var target = (target != null) ? target : ''; 
	if(target.length && !$('#' + target).val().length) { 
		var regex = new RegExp("http[s]?:\/\/","i");
		var clean = value.replace(regex,"").split("/");
		$('#' + target).val(clean[0]);
	}
}