// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function GetLocation(el){
    var c ={
      x :0,y :0
    };
    while (el){
      c.x +=el.offsetLeft;
      c.y +=el.offsetTop;
      el =el.offsetParent;
    }
    return c;
}

if (Event.prototype) {
	Event.prototype.__defineGetter__('offsetX',function (){
	  return window.pageXOffset + this.clientX - GetLocation(this.srcElement).x;
	});

	Event.prototype.__defineGetter__('offsetY',function (){
	  return window.pageYOffset + this.clientY - GetLocation(this.srcElement).y;
	});
}


function saveForm(fid) {
	var f = $(fid);
	f.submit();
}

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
  var oldonload = window.onload;
  if (typeof window.onload != 'function'){
      window.onload = func;
  } else {
    window.onload = function(){
    oldonload();
    func();
    }
  }
} // end addLoadEvent

function selectOptionWithValue(element, value) {
	var m = $(element);
	var index = -1;
	for(var i = 0; i < m.options.length; i++) {
	  if (m.options[i].value == value) {
	    index = i; 
			break
		}
	} // end for

	if (index >= 0) {
	  m.selectedIndex = index;
  }
}

function setAllCheckboxes(element, checked) {
	var m = $(element);
	var inputs = $A(m.getElementsByTagName('input'));
	inputs.each(function(node) {
		node.checked = checked;
	})
}

function toggleSelects(display) {
  var isIE = window.attachEvent ? true : false; 
  if (!isIE) {
    return;  
  }
  var selects = document.getElementsByTagName('select');
  for(var i = 0; i < selects.length; i++) {
    var node = selects[i];
    if (display) {
      //Element.show(node);
      node.style.visibility = 'visible';
    } else {
     // Element.hide(node); 
      node.style.visibility = 'hidden';
    }
  };
}

function focus(elm) {
  elm = $(elm);
  if (elm && elm.focus) {
    elm.focus();  
  }
}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

// global AJAX handlers
var myGlobalHandlers = {
  onCreate: function() {
    if ($('admin-busy') && Ajax.activeRequestCount>0) {
      Element.show('admin-busy');
    } // end if
  },

  onComplete: function(request) {
    var status = request.transport.status;

    if ($('admin-busy') && Ajax.activeRequestCount==0) {
	    if (status != 200 && console && console.log) {
		    console.log(request.transport.responseText);
		  } // end if
      Element.hide('admin-busy');
    } // end if

    if (status == 501) {
      alertActionBox(request.transport.responseText);  
    }
  }
};

Ajax.Responders.register(myGlobalHandlers);

Element.toggleClassName = function(element, className) {
  element = $(element);
  if (Element.hasClassName(element, className)) {
    Element.removeClassName(element, className);  
  } else {
    Element.addClassName(element, className);  
  }
}
