function IsArray(obj) { return obj instanceof Array; }
function IncludeLibraries(libraries) {
	libraries.each(function(library) {
		document.write('<script type="text/javascript" src="' + library + '"></script>');
	});
}
function IncludeStyles(styles) {
	styles.each(function(style) {
		document.write('<link rel="stylesheet" type="text/css" media="all" href="' + style + '" />');
	});
}
function SetNotice(content, status) {
	// :KLUDGE: having to force background colors
	//          highlight effect isn't reading new color
	//          from dynamically set classname
	// status related colors
	var colors = new Array();
	colors['success'] = '#e2f9e3';
	colors['error']   = '#ffcfce';
	var bgcolor = (status) ? colors[status] : colors['success'];
	
	// update notice
	var e = $('notice');
	e.update(unescape(content));
	e.className = 'notice-' + ((status) ? status : 'info');
	e.show();
	new Effect.Highlight(e, {duration:1.0, keepBackgroundImage:true, startcolor:'#ffff88', endcolor:bgcolor, restorecolor:bgcolor});
}
function ClearNotice() {
	var e = $('notice');
	e.update('');
	e.hide();
}
function PopWindow(title, url, width, height, modal) {
	var id = 'popwindow';
	if (!$(id)) {
		BuildWindow({id:id, title:title, url:url, width:width, height:height, modal:modal});
	}
}
function SafeMail(name, domain, addinfo) {
	style     = (addinfo.style)   ? ' class="' + addinfo.style + '"' : '';
	title     = (addinfo.title)   ? ' title="' + addinfo.title + '"' : '';
	subject   = (addinfo.subject) ? '?subject=' + addinfo.subject : '';
	displayed = (addinfo.display) ? addinfo.display : name + '@' + domain;
	mailto    = name + '@' + domain + subject;
	document.write('<a href="mailto:' + mailto + '"' + style + ' ' + title + '>' + displayed + '</a>');
}
function ToggleElement(id) {
	if ($(id)) { $(id).toggle(); }
}
function Mailto(address) {
	var url    = '/ajax_mailto.php';
	var pars   = 'address=' + address;
	new Ajax.Request(url, {
		method:'post',
		parameters:pars,
		onComplete:function(req) {
			window.location = req.responseText;
		}
	});
}
function AutoFormatPhone(formField, areaCodePrefix, areaCodeSuffix, threeFourSeparator) {
  // lock out NS4	alert "here"; 
  if ((!document.layers) && (formField.value.charAt(0) != '0')) {

    // assign default values if nothing passed in
    var areaCodePrefix     = (areaCodePrefix == null) ? '(' : areaCodePrefix;
    var areaCodeSuffix     = (areaCodeSuffix == null) ? ') ' : areaCodeSuffix;
    var threeFourSeparator = (threeFourSeparator == null) ? '-' : threeFourSeparator;
    var previousLength     = 0;
       
    // local values
    var groupLengths    = new Array(3, 3, 4);
    var phonePieces     = new Array();
    var thisInput       = null;
    var regNumberFirst  = new RegExp("^[0-9]");
    
    thisInput = formField.value;
    
    if (thisInput.length == areaCodePrefix.length && thisInput == areaCodePrefix) {
      // no action taken on area code prefix
      previousLength = areaCodePrefix.length; 
      return true;
    } else if (thisInput.length > previousLength && (regNumberFirst.test(thisInput) || (areaCodePrefix.length > 0 && thisInput.substr(0, areaCodePrefix.length) == areaCodePrefix))) {
      // length has incrased (no action taken on backspace) and value begins with a number or area code prefix (other entries not formatted)
      thisInput = thisInput.replace(/[. ()-\/]/gi, '');
      
      // separate string into groups
      phonePieces[0] = thisInput.substr(0, groupLengths[0]);
      phonePieces[1] = thisInput.substr(groupLengths[0], groupLengths[1]);
      phonePieces[2] = thisInput.substr(groupLengths[0] + groupLengths[1], groupLengths[2]);
      
      // prefix for area code
      if (thisInput.length > 0) { phonePieces[0] = areaCodePrefix + phonePieces[0]; }
      
      // suffix for area code, add length of prefix
      if (phonePieces[0].length == groupLengths[0] + areaCodePrefix.length) { phonePieces[0] += areaCodeSuffix; }
      
      // add separator for last seven numbers
      if (phonePieces[1].length == groupLengths[1]) { phonePieces[1] += threeFourSeparator; }
      
      // assign to field
      formField.value = phonePieces.join('');
    }
    previousLength = formField.value.length
  }
}
function StripeTable(id) {
  $A($(id).getElementsByTagName('tbody')).each(function(tbody) {
    $A(tbody.immediateDescendants()).each(function(row, i) {
      row.className = (i % 2) ? 'odd' : 'even';
    });
  });
}
function RemoveRow(id, table) {
  // remove row
  Element.remove(id);
  
  // restripe table
  StripeTable(table);
}
function changeCounty(county) {
	var img = $('county');
	var images = [
		'map_home.png',
		'map_llano.png',
		'map_burnet.png',
		'map_williamson.png',
		'map_lee.png',
		'map_fayette.png',
		'map_caldwell.png',
		'map_bastrop.png',
		'map_blanco_hays.png',
		'map_blanco_hays.png'
	];
	img.src = "/images/" + images[county];
}

function changeLargeCounty(county) {
	var img = $('county');
	var images = [
		'map_lg_home.png',
		'map_lg_llano.png',
		'map_lg_burnet.png',
		'map_lg_williamson.png',
		'map_lg_lee.png',
		'map_lg_fayette.png',
		'map_lg_caldwell.png',
		'map_lg_bastrop.png',
		'map_lg_blanco_hays.png',
		'map_lg_blanco_hays.png'
	];
	img.src = "/images/" + images[county];
}
