$(function() {
/* This snippet removes the right border on the main table
*/

var loc = window.location;
var regex = new RegExp("/view|target|creative/","i");

if (! regex.test(loc))
{
  $.getDocHeight = function(){
    return Math.max(
        $(document).height(),
        $(window).height(),
        /* For opera: */
        document.documentElement.clientHeight
    );
  };

  var h = $.getDocHeight() * .6;
  $('#contentbody').css('min-height',h);
}

$("table.data_table tr td:last-child").css({ "border-right":"none" });

var corrAlign = new Array();
$("table.data_table th").each(function(i, headerObj){
	$.each( ['Name','User Name','Account Manager','Partner','Publisher','Keyword','Event Type','Description','Event'], function() {
		if (this == $(headerObj).text())
		{
			var col = $(headerObj).prevAll().length;
			corrAlign.push(col);
			$(headerObj).css({'text-align':'left','padding-left':'8px'});
		}
	});
});
$.each(corrAlign, function(i,col) {
	$('table.data_table tr').each(function() {
		var tdObj = $(this).find('td').eq(col);		
		$(tdObj).css('text-align','left');
	});
});
/* IE7 hack */
var width = $("#sub_nav_div").width();
$("ul.sub_nav a").each(function () {
      $(this).width(width);
});

/* Tools drop-down menu */
$('#tools_sub_nav').hover(function(){$('#sub_nav_div').show();}, function(){$('#sub_nav_div').hide();});
$('#sub_nav_div').hover(function(){$(this).show();}, function(){$(this).hide();});
$('ul.sub_nav a').hover(function(){$(this).parent().css('background-color','#BA1421');}, function(){$(this).parent().css('background-color','#90101A');});

$('select').each(function(){

  $('option:selected', this).each(function() {
   	 $(this).css('color', '#989898');

  })
  });


});

function showhide(a, b)
{
	var id = document.getElementById(a);
	
	if (undefined != b)
	{
		var img = document.getElementById(b);
	}
	if (id.style.display == 'none') {	
		id.style.display = 'block';
		if (undefined != img)
		{
			img.className = 'img_toggle_down';
		}
	}
	else
	{
	    id.style.display = 'none';
	    if (undefined != img)
	    {
	    	img.className = 'img_toggle_up';
	    }
	}
}

function toggle(a) {
	var id = document.getElementById(a);
	if (id.style.display == 'none')
	{ 
		id.style.display = 'block';
	}
	else
	{ 
		id.style.display = 'none';
	}
}

function collapse(a,b) {
	var id = document.getElementById(a);
	var img = document.getElementById(b);
	id.style.display = 'none';
	img.className = 'img_toggle_up'; 
}

function checkAll(src, dest)
{
	var chkAll = document.getElementById(src);
	var checks = document.getElementsByName(dest);
	var boxLength = checks.length;
	for (i = 0; i < boxLength; i++)
	{
		checks[i].checked = chkAll.checked;
	}
}

/*
 * Set all checkboxes with checkboxName to have value
 * @param checkboxName string Name of the set of checkboxes to affect
 * @param value boolean Whether to set the checkboxes to checked (true) or unchecked (false)
 */
function setAllCheckboxes(checkboxName, value)
{
	var checkboxes = document.getElementsByName(checkboxName);
	for (i = 0; i < checkboxes.length; i++)
	{
		checkboxes[i].checked = value;
	}
}

/*
 * Invert the checkbox selection for checkboxName
 * @param checkboxName string Name of the set of checkboxes to affect
 */
function invertCheckboxes(checkboxName)
{
	var checkboxes = document.getElementsByName(checkboxName);
	for (i = 0; i < checkboxes.length; i++)
	{
		checkboxes[i].checked = !checkboxes[i].checked;
	}
}

////////////////////////////////////////////////////////////////// 
// functions moved from various templates to a cnetral place
//////////////////////////////////////////////////////////////////

/*  
* refresh the main window
* @parameter: none	
*/
function refresh()
{
	var sURL = unescape(window.location.pathname);
	window.location.replace( sURL );
}
/*  
* remove the thickbox and then refresh the main window, generally, it will be called after an edit operation
* @parameter: none	
*/
function cleanAndRefresh()
{
	window.parent.tb_remove();
	var sURL = unescape(window.location.pathname);
	window.location.replace( sURL );
}



//////////////// utility function . originally from tp-ajax-js.js
function isModernBrowser() {
    return (typeof document.getElementById!='undefined');
}

function dom(id) {
	if (isModernBrowser())
	{
    	return document.getElementById(id);
	}
	else if (typeof document.all!='undefined')
	{
		return document.all[id];
	}
	else
	{
		return false;
	}
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

