
Event.observe(window, 'load', function() {
  var e = $A(document.getElementsByTagName('*')).find(function(e) {
    return (e.tagName.toUpperCase() == 'INPUT' && (e.type == 'text' || e.type == 'password'))
        || e.tagName.toUpperCase() == 'TEXTAREA';
  });
  if (e) e.focus();
});
function confirmSubmit(msg)
{
var agree=confirm(msg);
if (agree)
	return true;
else
	return false;
}


function action_toggle(btn, element, focus) {
   new Effect.toggle(element, 'blind', {duration: 0.2});
   Element.toggleClassName(btn,'active');
   var e = document.getElementById(focus);
   if (e) e.focus();
 }

function showflash(msg)
{
Element.update('success-flash', msg);
Element.show('success-flash');
setTimeout("fade_flash()", 5000);
}

function fade_flash()
{
Effect.BlindUp('success-flash', {duration: .2}); 
}

function toggleRecurDate(elm){
	//alert(elm.value);
	if (elm.value == 'never'){
	elm.up(0).next(0).hide();
    }else{
	elm.up(0).next(0).show();
	}
}

function toggleRecurForever(elm){
	//alert(elm.value);
	if (elm.value == 'never'){
	elm.up(0).next(0).hide();
    }else{
	elm.up(0).next(0).show();
	}
}

function toggle_category(category, type) {
  element = document.getElementById('category_'+category);
  if(element){	
    Element.toggleClassName($('category_'+category),'expand');
    Element.toggleClassName($('category_'+category+'_link'),'expand');

    var elements = $(type).getElementsBySelector('[category="'+category+'"]');

     elements.each(function(elm) {
	
	 if ($('category_'+category+'_link').hasClassName('expand'))
	 {
         //only show the main items  
         if ($(elm).hasClassName('company-cat') || $(elm).hasClassName('item') || $(elm).hasClassName('expense-cat') || $(elm).hasClassName('category') || $(elm).hasClassName('item-cat'))
         {
	
	     //NEED to add expand
	     $(elm).removeClassName('expand')
	     $(elm).down(1).removeClassName('expand')
         Element.show($(elm));
         }
     }
     else
     {
	   Element.hide($(elm));
     }
     });
  }


}

function toggle_children(elm, type) {

  element = document.getElementById(elm);
  if (element){
	Element.toggleClassName(element,'expand');
	Element.toggleClassName(element.down(1),'expand');
	var elements = $(type).getElementsBySelector('[company="'+elm+'"]');
	  elements.each(function(co_elm) {
	  Element.toggle(co_elm);
	});
  }
	
}


function append_blank(html, receiver) {
  var child = document.getElementById(html).innerHTML;
  var parent = document.getElementById(receiver);
  new Insertion.Bottom(parent, child);
}

function recurring_toggle(elm){
Effect.Fade(elm);
}

function recur_until_toggle(elm){
	var p = elm.next(1);
	Element.toggle(p);
	var hidden = elm.next(0)
	if (hidden.readAttribute('value') =='yes')
	{
      hidden.writeAttribute('value', 'no');
    }else{
	  hidden.writeAttribute('value', 'yes');
    }
}

function active_entry_toggle(prefix, id){
	url=prefix+'/entry_toggle/'+id
	new Ajax.Request(url, {
	  method: 'get'
	  });
	
	Element.hide('active_checkbox_'+id);
	Element.show('active_loading_'+id);
	
}

function active_account_toggle(prefix, id, section){
	url=prefix+'/account_toggle/'+id+'?section='+section;

	new Ajax.Request(url, {
	  method: 'get'
	  });
	
	  Element.show('fa-active_loading_'+id);
	
}





function toggle_worsheet_cookie(elm,cookie_name,id, active_class, invert){

    var cookie = readCookie(cookie_name);
     if (cookie){
	   var cookie_arr = cookie.split(":");
    }else{
	   var cookie_arr = [];
    }

    if (elm.hasClassName(active_class)){
	  if(invert == '1'){
		cookie_arr = cookie_arr.without(id);
      }else{
        cookie_arr.push(id);
      }
    }else{
	  if(invert == '1'){
	  cookie_arr.push(id);
      }else{
      cookie_arr = cookie_arr.without(id);
      }
    }
    
    cookie_arr = cookie_arr.uniq();
    var new_cookie_value = cookie_arr.join(":");
    createCookie(cookie_name,new_cookie_value, 1000);
}



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;
}




/*

Project:	Input Placeholder Text
Title:		Automatic population of form fields with contents of title attributes
Created:	13 August 2005 by Jon Gibbins (aka dotjay)
Modified:	13 November 2006 by Jon Gibbins (aka dotjay)


Potential additions:

Add in handling of default text if an initial value is detected (line 60, line 93, etc) using something like:
if (!el.defaultValue) continue;
el.onfocus = function() {
	if (this.value == this.defaultValue) this.value = "";
}
el.onblur = function() {
	if (this.value == "") this.value = this.defaultValue;
}

Notes:

Add the following classes to text inputs or textareas to get the desired behaviour:

auto-select
	Will pre-populate the input with the title attribute and select the text when it receives focus.

auto-clear
	Will pre-populate the input with the title attribute and clear the text when it receives focus.
	Note: if auto-select and auto-clear are set, auto-select takes precedence.

populate
	Will just populate the input with the title attribute.

*/

window.onload = function () {

	if (!document.getElementsByTagName) return true;

	ourForms = document.getElementsByTagName('form');

	// go through each form
	var numForms = ourForms.length;
	for (var i=0;i<numForms;i++) {

		// go through each form element
		var numFormElements = ourForms[i].elements.length;
		for (var j=0;j<numFormElements;j++) {

			var el = ourForms[i].elements[j];

			// ignore submit buttons
			if (el.type == "submit") continue;

			// if we got a text type input
			if (el.type == "text") {
				// only populate if we want it to
				// note: might want title attribute but no pre-population of inputs
				var ourClassName = el.className;
				if (ourClassName.match('auto-select') || ourClassName.match('auto-clear') || ourClassName.match('populate')) {
					// only populate if empty
					if (el.value == '') el.value = el.title;
				}

				// add auto select if class contains auto-select
				// note: else if below so auto-select takes precedence (assuming select is better than clear)
				if (el.className.match('auto-select')) {
					el.onfocus = function () {
						if (this.value == this.title) this.select();
					}
					if (el.captureEvents) el.captureEvents(Event.FOCUS);
				}

				// add auto clear if class contains auto-clear
				else if (el.className.match('auto-clear')) {
					el.onfocus = function () {
						if (this.value == this.title) this.value = '';
					}
					if (el.captureEvents) el.captureEvents(Event.FOCUS);

					el.onblur = function () {
						if (this.value == '') this.value = this.title;
					}
					if (el.captureEvents) el.captureEvents(Event.BLUR);
				}
			}

			// if we got a textarea
			if (el.type == "textarea") {
				// only populate if we want it to
				// note: might want title attribute but no pre-population of inputs
				var ourClassName = el.className;
				if (ourClassName.match('auto-select') || ourClassName.match('auto-clear') || ourClassName.match('populate')) {
					// only populate if empty
					if (el.value == '') el.value = el.title;
				}

				// add auto select if class contains auto-select
				if (el.className.match('auto-select')) {
					el.onfocus = function () {
						if (this.value == this.title) this.select();
					}
					if (el.captureEvents) el.captureEvents(Event.FOCUS);
				}

				// add auto clear if class contains auto-clear
				else if (el.className.match('auto-clear')) {
					el.onfocus = function () {
						if (this.value == this.title) this.value = '';
					}
					if (el.captureEvents) el.captureEvents(Event.FOCUS);

					el.onblur = function () {
						if (this.value == '') this.value = this.title;
					}
					if (el.captureEvents) el.captureEvents(Event.BLUR);
				}
			}

		}

	}

}
