
function attemptAjaxHref(o, el_id) {
	if (arguments.length > 2) {
		var scrollTo = arguments[2];
	} else {
		var scrollTo = true;
	}
	var href_parts = o.href.split('?');
	try {
		new Ajax.Updater(el_id, href_parts[0], {
			postBody: href_parts[1] + '&ajaxRequest=1',
			evalScripts:true,
			onComplete: function() { if (scrollTo) { Element.scrollTo(el_id); } }
		});
	} catch(e) {
		alert(e);
		return true;
	}
	return false;
}

function slider(k)
{
	if($(k+'_wrapper').style.display == 'none')
	{
		$(k+'_wrapper').style.display = 'block';
		Effect.SlideDown(k+'_slider');
	}
	else
	{
		Effect.SlideUp(k+'_slider');
		setTimeout("$('"+k+"_wrapper').style.display = 'none';",1000);
	}
	return false;
}


function rand(min, max) {
	var randNum = 0;
	var n = Math.random();
	do {
		n -=parseInt(n);
		n *= 10;
		randNum = parseInt(n);
	} while(randNum>max || randNum<min);
	return randNum;
}

function imgOver(obj) {
	var o = $(obj.id + 'Btn');
	o.src = o.src.replace(/\.(gif|jpg|png)/, '_over.$1');
}
function imgOff(obj) {
	var o = $(obj.id + 'Btn');
	o.src = o.src.replace(/\_over.(gif|jpg|png)/, '.$1');
}

/***************************************************************
*															   *
*															   *
* 			  	  Functions for forms						   *
*															   *
*															   *
***************************************************************/
if (typeof console == 'undefined') {
	console = {
		debug: function() {},
		write: function() {}
	}
}

/**
 * 
 * @param string Object ID
 * @param string New Key Value
 * @param string New Text Value
 * @return bool
 */
function appendToSel(obj_id, new_key, new_val) {
	try {
		var o = $(obj_id);
		if (o == 'undefined' || o == null) {
//			console.debug('Invalid Oject ID to appendToSel:' + obj_id);
			return false;
		}
//		console.debug('object from objID:' + o);
		var opt = document.createElement('OPTION');
		opt.value = new_key;
		opt.text = new_val;
//		console.debug('New Opt' + opt);
		o.appendChild(opt);
	} catch (e) {
//		console.debug('Error Adding New Option to Select Box: ' + e);
		return false;
	}
	return true;
}

/***************************************************************
* Checkbox setting function					                   *
* 1st Param - Bool indicating checked state                    *
* 2nd Param - String indicating class of checkboxes to change. *
***************************************************************/
function setCboxes(checked, theclass) {
	var cboxes = document.getElementsByClassName(theclass);
	for (i=0;i<cboxes.length;i++) {
		cboxes[i].checked = checked;
	}
}

/*********************************************
*                                            *
*                                            *
*                                            *
*   New AJAX Managing Classes and Functions  *
*                                            *
*                                            *
*                                            *
*********************************************/
function saveAjaxForm(formID, divID, post_to_url) {
	var pb = Form.serialize(formID);
	if (pb=='') return $(formID).submit();
	pb += '&ajaxRequest=1';
	try {
		new Ajax.Updater(divID, post_to_url, {
			method:'post',
			postBody: pb,
			evalScripts:true,
			onError: function() { $(formID).submit(); }
		});
	} catch(e) {
		$(formID).submit();
	}
}

var xmlhttp      = null;
var ajaxCallback = null;
function ajaxCall(page, callback)
{ 
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } 
  catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    catch (E) {
      xmlhttp = false;
    }
  }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }

  xmlhttp.open("GET", page, true);

  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4) {
      if (callback) {
        callback(xmlhttp.responseText);
      }
    }
  }
  xmlhttp.send(null);
}

/*********************************************
*                                            *
*                                            *
*                                            *
*  New Window Managing Classes and Functions *
*                                            *
*                                            *
*                                            *
*********************************************/



/*********************************************
*  Functions                                 *
*********************************************/

function validateYesNo( theVar ) {
  if ( !theVar ) return 'no';
  if ( theVar.toLowerCase() == 'no' ) return 'no';
  else if ( theVar == true ) return 'yes';
  else if ( theVar.toLowerCase() != 'true' ) return 'yes';
  else if ( theVar.toLowerCase() != 'yes' ) return 'no';
  return 'yes';
}

function validateInt( theVar ) {
  if ( !theVar ) return 0;
  return parseInt(theVar);
}

function openerCheck() {
  if ( !windowOpen( window.opener ) ) {
    window.close();
  }
}

function windowOpen(win) {
   if ( !win || win.closed ) {
      return false;
   }
   return true;
}

function WindowFormat(name, width, height, scrolling, scroll, scrollbars, resizable, toolbar, fullScreen)
{
  if ( !width )      width      = null;
  if ( !height )     height     = null;
  if ( !scrolling )  scrolling  = 'yes';
  if ( !scroll )     scroll     = 'yes';
  if ( !scrollbars ) scrollbars = 'yes';
  if ( !resizable )  resizable  = 'yes';
  if ( !toolbar )    toolbar    = 'yes';
  if ( !fullScreen ) fullScreen = 'no';
  if ( !name )       name       = width+height+scrolling+scroll+scrollbars+resizable+toolbar;
  
  this.setWidth( width );
  this.setHeight( height );
  this.setScrolling( scrolling );
  this.setScroll( scroll );
  this.setScrollBars( scrollbars );
  this.setResizable( resizable );
  this.setToolbar( toolbar );
  this.setName( name );
  this.setFS( fullScreen );
}

WindowFormat.prototype.setWidth     = function(amt)   { this.width      = validateInt(amt);     }
WindowFormat.prototype.setHeight    = function(amt)   { this.height     = validateInt(amt);     }
WindowFormat.prototype.setScrolling = function(yesNo) { this.scrolling  = validateYesNo(yesNo); }
WindowFormat.prototype.setScroll    = function(yesNo) { this.scroll     = validateYesNo(yesNo); }
WindowFormat.prototype.setScrollBars= function(yesNo) { this.scrollbars = validateYesNo(yesNo); }
WindowFormat.prototype.setResizable = function(yesNo) { this.resizable  = validateYesNo(yesNo); }
WindowFormat.prototype.setToolbar   = function(yesNo) { this.toolbar    = validateYesNo(yesNo); }
WindowFormat.prototype.setFS        = function(yesNo) { this.fullscreen = validateYesNo(yesNo); }
WindowFormat.prototype.setName      = function(nombre){ this.name       = nombre; }
WindowFormat.prototype.getFullString= function() 
{
  return 'width='     +this.width     +','+
         'height='    +this.height    +','+
         'scrolling=' +this.scrolling +','+
         'scroll='    +this.scroll    +','+
         'scrollbars='+this.scrollbars+','+
         'resizable=' +this.resizable +','+
         'toolbar='   +this.toolbar+','+
         'fullscreen='+this.fullscreen;
}
WindowFormat.prototype.getDetails   = function() 
{
  var retStr = "WindowFormat [Object]\n";
  retStr += " .name = "       + this.name       + "\n";
  retStr += " .width = "      + this.width      + "\n";
  retStr += " .height = "     + this.height     + "\n";
  retStr += " .scrolling = "  + this.scrolling  + "\n";
  retStr += " .scroll = "     + this.scroll     + "\n";
  retStr += " .scrollbars = " + this.scrollbars + "\n";
  retStr += " .resizable = "  + this.resizable  + "\n";
  retStr += " .toolbar = "    + this.toolbar    + "\n";
  retStr += " .fullscreen = " + this.fullscreen + "\n";
  return retStr;
}




/*********************************************
*  WindowManager Class                       *
*********************************************/

function WindowManager() {
  this.winArray = new Array();
}
WindowManager.prototype.getDetails   = function() 
{
  var retStr = "WindowManager [Object]\n";
  retStr += " .winArray [Array]\n";
  for(var i in this.winArray ) {
    retStr += "  '"+this.winArray[i].name+"' [Window Object]\n";
  }
  return retStr;
}
WindowManager.prototype.cleanup = function() {
  for(var i in this.winArray ) {
    if ( !this.winArray[i] || this.winArray[i].closed ) {
      this.winArray[i] = null;
    }
  }
}
WindowManager.prototype.showBlockedWarn = function() {
  alert('Oops! An important window was blocked from showing. Could it be your popupblocker?');
}
WindowManager.prototype.openWin = function( location, wndFrmtObj ) 
{
  if ( !location )   return;
  if ( !wndFrmtObj ) wndFrmtObj = new WindowFormat();

  if (location.indexOf('?') == -1) location += '?width='+wndFrmtObj.width+'&height='+wndFrmtObj.height+'&fullscreen='+wndFrmtObj.fullscreen;
  else location += '&width='+wndFrmtObj.width+'&height='+wndFrmtObj.height+'&fullscreen='+wndFrmtObj.fullscreen;
  
  if ( this.winArray[wndFrmtObj.name] ) 
  {
    if ( !this.winArray[wndFrmtObj.name].closed && this.winArray[wndFrmtObj.name] ) 
    {
      if ( this.winArray[wndFrmtObj.name].location == location ) {
        this.winArray[wndFrmtObj.name].focus();
        return;
      }
      else {
        this.winArray[wndFrmtObj.name].close();
      }
    }
    else {
      this.winArray[wndFrmtObj.name] = null;
    }
  }
   
  this.winArray[wndFrmtObj.name] = window.open(location, wndFrmtObj.name, wndFrmtObj.getFullString());
  
  if ( !this.winArray[wndFrmtObj.name] ) {
    this.showBlockedWarn();
  }
}

// Create the window manager
var w          = new WindowManager();


/*********************************************
*  OnLoad Framework                          *
*********************************************/
var onLoadList = new Array();
var onLoadHash = new Array();

function registerOnLoad(func) {
  if ( typeof(onLoadHash[func.toString()]) == 'undefined' ) {
    onLoadList.push(func);
    onLoadHash[func.toString()] = 1;
  }
}
function callOnLoadList() {
  for(var i=0; i<onLoadList.length;i++) {
    onLoadList[i]();
  }
}




var maxZIndex = 10000;
function getZ() {
	return ++maxZIndex;
}
function centerMe(id) {
	var o = $(id);
	var winsize = getWinSize();
//	console.debug('winsize: ' + winsize);
	var myd = Element.getDimensions(o);
//	console.debug('element dimensions: ' + myd.width + 'x' + myd.height);
	var winscroll = getWinScroll();
//	console.debug('winscroll:' + winscroll);
	var newl = (winsize[0]-myd.width) / 2 + (winscroll[1]);
//	console.debug('new left: ' + newl);
	if (newl < 0) {
		newl = 0;
//		console.debug('new left: ' + newl);
	}
	o.style.left = newl;
	var newt = (winsize[1]-myd.height) / 2 + (winscroll[0]);
//	console.debug('new top: ' + newt);
	if (newt < 0) {
		newt = 0;
//		console.debug('new top: ' + newt);
	}
	o.style.top = newt;
}


function getWinSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth, myHeight];
}

function getWinScroll() {
	if (navigator.appName == "Microsoft Internet Explorer")
		return [document.body.scrollTop,document.body.scrollLeft];
	else	
		return [window.pageYOffset, window.pageXOffset];

}


var AJAXATTEMPTS = [];
function attemptAjaxSubmit(f, u, id) {
	return true;
	try {
		var pb = Form.serialize(f);
		pb += '&ajaxRequest=1';
		if (AJAXATTEMPTS[id] != null) return true;
		AJAXATTEMPTS[id] = 1;
	
		if (arguments.length == 4) target = arguments[3];
		else target= 'ajaxdiv';
		
		if ($(target) == null) {
			alert('hi');
			var h = '<div id="'+target+'" style="display:none;"></div>';
			new Insertion.Bottom(body, h);
		}

		new Ajax.Updater(target,u, {
			method:'post',
			postBody: pb,
			evalScripts:true
		});
	} catch (e) {
		console.debug(e);
		return false;
		return true;
	}
	return false;
}

/*********************************************
* Popup Window 	                             *
*********************************************/
var popups = [];
function winPopup(url, title, params)
{
	popups[title]  = window.open( url, title, params);	
	popups[title].focus();	
}

function openAmbassadorWindow() {
	var win = window.open('/ambassador/ambassador.php','AmbassadorWin','width=755,height=509');
	win.focus();
	return false;
}