function paramValue(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");

  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function launchCenteredWindow(url, rw, rh) {
	w = rw;
    h = rh;
	left_screen = (screen.width - w) / 2;
    top_screen = (screen.height - h) / 2;
	buBookWindowHandle = window.open(url, '_blank', 'fullscreen=no,status=0,top='+top_screen+',left='+left_screen+',width='+rw+',height='+rh+',toolbar=0,resizable=1,scrollbars=0');
    buBookWindowHandle.focus();
}

function confirmURL(message, url) {
	if (!confirm(message))
	   return;
	   
	window.location = url;
}

function reportContent(url) {
	if (!confirm('Would you like to report this as inappropriate content?'))
	   return;
	   
	window.location = url;
}

function confirmTwice(message1, message2) {
	if (!confirm(message1))
	   return false;
	
	if (message2 == null || message2 == "")
	    message2 = message1;
	       
	if (!confirm(message2))
	   return false;
	   
	return true;
}

function confirmTwiceURL(message, url) {
	if (!confirm(message))
	   return false;
	   
	if (!confirm('Are you ABSOLUTELY SURE you want to continue?'))
	   return false;
	   
	window.location = url;
	return true;
}

function limitText(limitField, limitNum) {
    if (limitField.value.length > limitNum) 
        limitField.value = limitField.value.substring(0, limitNum);
}

function gotoSelectURI(urlprefix, list) {
	if (list.selectedIndex == 0) 
	   return;
	   
	window.location = list.options[list.selectedIndex].value
}

function toggleShowHide(divId) {
	current = document.getElementById(divId).style.display;
	if (current == "block")
		document.getElementById(divId).style.display = "none";
	else
		document.getElementById(divId).style.display = "block";
}

function showHide(divId, state)	{
	if (state == false)
		document.getElementById(divId).style.display = "none";
	else
		document.getElementById(divId).style.display = "block";
}

function isEmptyTextField(aTextField) {
   if ((aTextField.value.length==0) || (aTextField.value==null))
      return true;
   else
      return false;
}

function isValidSelectField(fieldName, fieldDecription) {
   field = document.getElementById(fieldName)
   if (field.selectedIndex == 0) {
      alert("Please select " + fieldDecription)
      field.focus();
      return false;
   }

   return true;
}

function isValidTextField(fieldName, fieldDecription) {
   field = document.getElementById(fieldName)
   if ((field.value.length == 0) || (field.value == null)) {
      alert("Please specify " + fieldDecription)
      field.focus();
      return false;
   }

   return true;
}

//  check for valid numeric strings	
function isNumeric(strString)   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)  {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1) {
         blnResult = false;
         }
   }
	  
   return blnResult;
}
   
function copyToClipboard() {
   copiedTxt = document.selection.createRange();
   copiedTxt.execCommand("Copy");
}

function selectAndCopyToClipboard(fieldName) {
   field = document.getElementById(fieldName)
   field.select()	
   copiedTxt = field.createTextRange()
//   copiedTxt.execCommand("Copy")
}

function roundTwoDigits(original) {
	rounded = "" + (Math.round(original * 100) / 100);
	idxDecimal = rounded.indexOf(".")
//	alert(rounded + "\n" + idxDecimal + "\n" + rounded.substring(idxDecimal).length)
	if (idxDecimal < 0)
	    rounded = rounded + ".00"
	else
	if (rounded.substring(idxDecimal+1).length < 2)
	    rounded = rounded + "0"
	
	return rounded;
}

/* Sidebar slide-down content tabs */
function setupjQuerySlideNav() {
 jQuery('.toggleDivs').click(function() {
	var theDiv = jQuery(this).nextAll("div:first");
	var vis = $(theDiv).css("display");
	if (vis == 'none') {
		jQuery(".toggleDiv").slideUp("slow");
		jQuery(".toggleDivs.active").removeClass("active");
		jQuery(theDiv).slideDown("slow");
		jQuery(this).addClass("active");
	} else {
		jQuery(".toggleDiv").slideUp("slow");
		jQuery(".toggleDivs.active").removeClass("active");
	}
	return false;
  });
}

/* Tabs via jQuery & CSS */
/* Tabs are created from LI elements with TABNAME attributes. Tab content is stored in a DIV with the same TABNAME attribute. */
var loaded_tabs = {};

function setupjQueryTabs(tabs_id) {
	jQuery('#' + tabs_id + ' ul.jqueryTabs li > a').click(function() {
		deselectTabs(tabs_id);
	  selected_tab_name = jQuery(this).attr("tabname");
		displaySelectedTab(selected_tab_name, tabs_id);
		loadSelectedTab(selected_tab_name, tabs_id);
		return false;
  });
}

function loadSelectedTab(selected_tab_name, tabs_id) {
	var selected_tab = jQuery('#' + tabs_id).find('div[tabname="' + selected_tab_name + '"]');
	  /* Load the new tab contents if necessary */
		var span = selected_tab.children('span');
		var ajax_url = span.attr('ajax_url');
		if ( ajax_url != null && ajax_url.length > 0 && loaded_tabs[tabs_id + ajax_url] == null) {
			jQuery.ajax( {url: ajax_url, dataType: 'html', success: function(data) {span.html(data); loaded_tabs[tabs_id + ajax_url] = 1;} });
		}
}

function deselectTabs(tabs_id) {
	var tabs = jQuery('#' + tabs_id + ' li');
  tabs.removeClass("selected");
	tabs.each(function() {
		jQuery('#' + tabs_id+' div[tabname]').css('display', 'none');
	});
}

function displaySelectedTab(selected_tab_name, tabs_id) {
	var selected_tab_header = jQuery('#' + tabs_id).find('a[tabname="' + selected_tab_name + '"]');
	var selected_tab_div = jQuery('#' + tabs_id).find('div[tabname="' + selected_tab_name + '"]');
  selected_tab_div.css('display','block');
  selected_tab_header.parent().addClass("selected");
	
}

function select_all_checkboxes(formObject, fieldName, checked) {
	for (var i=0; i < formObject.elements.length; i++) {
		var e = formObject.elements[i];
		if (e.type == 'checkbox' && e.name == fieldName && !e.disabled)
			e.checked = checked;
	}
}

