//Used on the prayer_request.tpl so that we can limit the number of characters typed into a textarea.
//field = the textarea object
//count = the text object used to display the number of remaining characters
//max   = the maximum number of characters that are allowed
//In the HTML we have:
//  <input readonly type="text" id="chars_left" name="chars_left" size='4' maxlength='4' value="1500"> characters left
//  and
//  <textarea rows="6" name="prayer_details" id="prayer_details" class="prayer_request_textarea" onkeyup="countChars(this.form.prayer_details, this.form.chars_left, 1500);" onkeypress="countChars(this.form.prayer_details, this.form.chars_left, 1500);" onkeydown="countChars(this.form.prayer_details, this.form.chars_left, 1500);"></textarea>
function countChars(field, count, max) {
  // if the length of the string in the input field is greater than the max value, trim it
  if (field.value.length > max) {
    field.value = field.value.substring(0, max);
  } else {
    // calculate the remaining characters
    count.value = max - field.value.length;
  }
}

//Extend the String object so that we can use the trim() function like; sVar.trim();
String.prototype.trim = function() {
  a = this.replace(/^\s+/, '');
  return a.replace(/\s+$/, '');
};

function quickLinks(iSelectID) {
  var url = $(iSelectID).value;
  window.location = url;
}

function pop(url, name, props) {
	window.open(url, name, props);
}

/* This code is for SHow & Hide DIVS */

function showCard() {
  $('ulCardFields').show();
  $('ulCheckFields').hide();
}

function showCheck() {
  $('ulCheckFields').show();
  $('ulCardFields').hide();
}

function bookmarkus()
{
	var url = "http://www.kcm.org";
	var title = "Kenneth Copeland Ministries";
	if (window.sidebar)
	{
		window.sidebar.addPanel(title, url,"");// Mozilla Firefox Bookmark
	} else if( window.external )
	{
		window.external.AddFavorite( url, title);  // IE
	}
	else if(window.opera && window.print)
	{
		return true;  // Opera Hotlist
	}

}

function showDiv(sDivName) {
  var div = document.getElementById(sDivName);
  div.style.display = 'block';

//  .invoke('hide');
//  var aSelects = $A(document.getElementsByTagName('select')).map(Element.extend);
//  alert("There are " + aSelects.length + " selects.");
//  aSelects.each(Element.hide);
//  var aSelects = $A(document.getElementsByTagName('select'));
//  var index=0;
//  for (index=0; index<aSelects.length; index++) {
//    aSelects[index].hide();
//  }
//  alert("There are " + aSelects.length + " select boxes");
}
function hideDiv(sDivName) {
  var div = document.getElementById(sDivName);
  div.style.display = 'none';

  var aSelects = $A(document.getElementsByTagName('select'));
  aSelects.each(Element.show);
}

/* POPUP MAP - START */
map_world = new Image(436,249)
map_world.src = "images/map/map_world.gif"
map_america = new Image(436,249)
map_america.src = "images/map/map_america.gif"
map_canada = new Image(436,249)
map_canada.src = "images/map/map_canada.gif"
map_ukraine = new Image(436,249)
map_ukraine.src = "images/map/map_ukraine.gif"
map_uk = new Image(436,249)
map_uk.src = "images/map/map_uk.gif"
map_africa = new Image(436,249)
map_africa.src = "images/map/map_africa.gif"
map_australia = new Image(436,249)
map_australia.src = "images/map/map_australia.gif"
function mapAmerica() {
  document.emp.src = map_america.src; return true;
}
function mapCanada() {
  document.emp.src = map_canada.src; return true;
}
function mapUk() {
  document.emp.src = map_uk.src; return true;
}
function mapAfrica() {
  document.emp.src = map_africa.src; return true;
}
function mapAustralia() {
  document.emp.src = map_australia.src; return true;
}
function mapUkraine() {
  document.emp.src = map_ukraine.src; return true;
}
function original() {
  document.emp.src = map_world.src; return true;
}
/* POPUP MAP - END */

var menuInterval;
var originalMenu = null;
var subMenuSet = false;

Event.observe(window, "load", function() {
  var subMenus = $$('#sub_nav ul');
  var main = $$('ul#main_nav li');
  var b = $$('body')[0];
  
  b.classNames().each(function(className) { 
	var s = "nav-" + className;
  	if ($(s) != null && originalMenu == null)
	{
		originalMenu = $(s);
	}
  });
  
  main.each(function(item) {
	item.observe('mouseover', 
		function(evt)
		{
			subMenus.each(function(subItem){
				subItem.hide();
				});
			
			var el = Event.element(evt);
			var sel = "#sub_nav ul#nav-" + el.id;
			var subnav = $$(sel);
			
			if (subnav && subnav.length>0) 
				subnav[0].setStyle({display: 'block'});
				subMenuSet = true;
			});
    });
  
  Event.observe(document, "mousemove", function(evt) {
		if (subMenuSet && 
			!Position.within($('sub_nav_box'), evt.pointerX(), evt.pointerY())
			&& !Position.within($('main_nav_box'), evt.pointerX(), evt.pointerY())
			)
		{
			subMenuSet = false;
			$$('#sub_nav ul').each(function(subItem){
				subItem.hide();
				});									
			if (originalMenu != null)	
				originalMenu.show();	
		}
  });
  
});

