var ie = /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);

$(document).ready(function(){
  // Initialize lefthand navigation menu
	$(".menus dd").hide();
  // open menu of page you are on
  var my_effective_location = (typeof(effective_location) == "undefined") ? $.url.setUrl(document.location.href).attr("file") : effective_location;
	$(".leftnav .menus ul li a").each(function () {
	  if ($.url.setUrl(this.href).attr("file") == my_effective_location) {
	    $(this).parent().parent().parent().show();
	  }
	});
  // clicking a header
	$(".menus dt a").click(function(){
	  if ($.url.setUrl(this.href).attr("file") != $.url.setUrl(document.location.href).attr("file")) { return true; }
		$(".menus dd:visible").slideUp("slow");
		if ($(this).parent().next().is(':hidden')) { $(this).parent().next().slideDown("slow"); }
		return false;
	});
	// change value of form variables to verify not a robot
	$('[@name=SubmitFormValue]').val('omahabe');
});

/* Popup window functions */
function popUp(url,w,h,attrstring) {
  if (typeof w == 'undefined' ) w = '1024';
  if (typeof h == 'undefined' ) h = '768';
  if (typeof attrstring == 'undefined' ) attrstring='toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1';  
  attr_wh = 'width=' + w + ',height=' + h + ',outerwidth=' + w + ',outerheight=' + h;
	sgpw=window.open(url,"",attrstring + "," + attr_wh);
	return false;
}

/* Sorting an html table */
function sortTable() {
  $('table.datatable').each(function() {
    var $table = $(this);
    $('th', $table).each(function(column) {
      if ($(this).is('.sort-alpha')) {
        $(this).addClass('clickable').hover(function() {
          $(this).addClass('hover');
        }, function() {
          $(this).removeClass('hover');
        }).click(function() {
          var rows = $table.find('tbody > tr').get();
          rows.sort(function(a, b) {
            var keyA = $(a).children('td').eq(column).text().toUpperCase();
            var keyB = $(b).children('td').eq(column).text().toUpperCase();
            if (keyA < keyB) return -1;
            if (keyA > keyB) return 1;
            return 0;
          });
          $.each(rows, function(index, row) {
            $table.children('tbody').append(row);
          });
        });
      }
    });
  });
}

//Validating form input - isblank for required object
function isblank(obj) {
  var s = obj.value;
  if (s.length==0) { s = ' '; }
  for(var i=0;i<s.length;i++) {
    var c=s.charAt(i);
    if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
  }
  alert(obj.id + ' is required. Please enter and submit the form again.');
  obj.focus();
  return true;
}

//Validating form input - if provided, is email address valid
function invalidEmail(obj,name) {
	var validstr='^[\\w . ! # $ % & * + \\- ~]*@[A-z 0-9 \\-]*\\.[A-z 0-9 . \\-]*[A-z]$';
	var emailadr=obj.value;
	if ((emailadr != '') && (!emailadr.match(validstr)))  {
    alert(obj.id + ' provided is an invalid email address. Please correct and submit the form again.');
    obj.focus();
    return true;
	} else {
	  return false;
	}
}

/* Trim whitespace from right and left */
function trimAll(sString) {
	while (sString.substring(0,1) == ' ') { sString = sString.substring(1, sString.length); }
	while (sString.substring(sString.length-1, sString.length) == ' ') { sString = sString.substring(0,sString.length-1); }
	return sString;
}

/* Replace special characters in a a string */
function replaceSpecialChar(str) {
  str = str.replace(/­/g,'-');
  str = str.replace(/¹/g,"'");
  str = str.replace(/Ž/g,'');
  str = str.replace(/…/g,'...');
  str = str.replace(/“/g,'"');
  str = str.replace(/”/g,'"');
  str = str.replace(/°/g,'&deg;');
  str = str.replace(/’/g,"'");
  str = str.replace(/‘/g,"'");
  str = str.replace(/–/g,'-');
  str = str.replace(/—/g,'-');
  str = str.replace(/©/g,'&copy;');
  str = str.replace(/®/g,'&reg;');
  str = str.replace(/´/g,"'");
  str = str.replace(/¼/g,'&frac14;');
  str = str.replace(/½/g,'&frac12;');
  str = str.replace(/¾/g,'&frac34;');
  str = str.replace(/è/g,'&egrave;');
  str = str.replace(/é/g,'&eacute;');
  return str;
}

/* Calculate page size including scrollable width and height */
function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
			yWithScroll = window.innerHeight + window.scrollMaxY;
			xWithScroll = window.innerWidth + window.scrollMaxX - 16;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			yWithScroll = document.body.scrollHeight;
			xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
			yWithScroll = document.body.offsetHeight;
			xWithScroll = document.body.offsetWidth;
		}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}

/* Calculate page size of viewable page */
function getPageSize() {
	var xWidth = 0, yHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		xWidth = window.innerWidth - 20;
		yHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		xWidth = document.documentElement.clientWidth;
		yHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		xWidth = document.body.clientWidth;
		yHeight = document.body.clientHeight;
	}
	arrayPageSize = new Array(xWidth,yHeight);
//  window.alert( 'Width = ' + myWidth );
//  window.alert( 'Height = ' + myHeight );
	return arrayPageSize;
}

/* Calculate scrolled amount */
function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}

/* Find absolute position of an element */
function findPosition( oElement ) {
	if( typeof( oElement.offsetParent ) != 'undefined' ) {
		for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
			posX += oElement.offsetLeft;
			posY += oElement.offsetTop;
		}
		return [ posX, posY ];
	} else {
		return [ oElement.x, oElement.y ];
	}
}

/* Show calendar event in modal popup */
function showCalEvent(anchor) {
  $("#modalPopup").html('<div class="modalBorder"><div onclick="hideModalPopup();" class="modalPopupClose">X</div>'+anchor.name+'</div>');
  showModalPopup(findPosition(anchor)[0],findPosition(anchor)[1]);
  return false;
}

/* Show modal popup div */
function showModalPopup(posX, posY) {
	if (ie) { $('select').hide(); }
	$('#modalPopup').css('visibility','hidden');
	$('#modalPopup').show("fast",function(){
    posX = (typeof posX == 'undefined') ? Math.max(getScrollXY()[0],getScrollXY()[0] + (getPageSize()[0] - $('#modalPopup')[0].offsetWidth)/2) : posX;
    posY = (typeof posY == 'undefined') ? Math.max(getScrollXY()[1],getScrollXY()[1] + (getPageSize()[1] - $('#modalPopup')[0].offsetHeight)/2) : posY;
    posX = ((posX + $('#modalPopup')[0].offsetWidth) > getPageSize()[0]) ? (getPageSize()[0] - $('#modalPopup')[0].offsetWidth) : posX;
    posY = ((posY + $('#modalPopup')[0].offsetHeight) > getPageSize()[1]) ? (getPageSize()[1] - $('#modalPopup')[0].offsetHeight) : posY;
		$('#modalPopup').css('left', posX + 'px');
		$('#modalPopup').css('top', (posY+15) + 'px');
		$('#modalPopup').css('visibility','visible');
	});
	return false;
}

function hideModalPopup() {
	$('#modalPopup').fadeOut("slow");
	if (ie) { $('select').show(); }
	return false;
}


