
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

var Popup = {
	/* TODO: can we get these by class? */
	types:         ['artist', 'password', 'contact', 'email', 'creditcard', 'sms', 'newphoto', 'newaudio', 'newalbum','newvideo','bankaccount','currentfan','newfan', 'remove','notification','profile','payment','dashboard','bookingpay','mp3info'],
	toggle:        function(t) { this.types.each(function(type) { if(type != t) new Popup.conceal(type) } ); $(t).style.display=='none' ? this.reveal(t) : this.conceal(t) },
	conceal:       function(t) { if($(t)!==null) { new Effect.BlindUp(t,   { duration: 0.25 }); $(t + '_trigger').className = ''; } },
	reveal:        function(t) { if($(t)!==null) { new Effect.BlindDown(t, { duration: 0.5  }); $(t + '_trigger').className = 'active'; } },
	select:        function(t) { this.inputbox(t).className = this.is_unselected(t) ? '' : 'inactive'; this.inputbox(t).value = this.is_unselected(t) ? '' : 'Or, enter a new client'; if(this.is_unselected(t)) this.inputbox(t).focus() },
	input:         function(t) { this.inputbox(t).className = ''; if(this.is_empty(t)){ this.inputbox(t).value='' }; this.selectbox(t).selectedIndex = 0; },
	validate:      function(t) { if(this.is_unselected(t) && this.is_empty(t)) { alert("You must enter a client"); return false; } },
	inputbox:      function(t) { return $('client_input_'+t) },
	selectbox:     function(t) { return $('client_select_'+t) },
	is_empty:      function(t) { return this.inputbox(t).value=="Or, enter a new client" || this.inputbox(t).value=="Please enter a new client" || this.inputbox(t).value=='' },
	is_unselected: function(t) { return this.selectbox(t).selectedIndex == 0 || this.selectbox(t).selectedIndex == 1 }
}

var quickstats = {
	init:   function() { if($('quickstats')) getCookie('onoff')=='on' ? quickstats.show() : quickstats.hide() },
	toggle: function() { $('quickstats').style.display=='none' ? this.show() : this.hide() },
	show:   function() { Element.show('quickstats'); setCookie("onoff", "on"); $('onoff').className='on'; $('onofflabel').innerHTML = 'Hide'; },
	hide:   function() { Element.hide('quickstats'); setCookie("onoff", "off"); $('onoff').className=''; $('onofflabel').innerHTML = 'Show'; }
}
addLoadEvent(quickstats.init)

/* TODO: can we make or find a generic object to do input masking? */
function onlynumbers(evt) {
	evt = (evt) ? evt : event
	var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode)
	if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46 && charCode != 44) {
		return false
	}
	return true
}


/* crappy cookie functions; TODO: improve this */

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function getCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}

function setCookie (name, value) {
  var argv = setCookie.arguments;
  var argc = setCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}

