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


function doClear(field) {
    if (field.value == field.defaultValue)
    {
	field.value = ""
    }
}

function toggle(toggleObj,displayObj) {
	if (document.getElementById(toggleObj).style.display == "none") { 
		document.getElementById(toggleObj).style.display = "block";
		document.getElementById(toggleObj).disabled = false;
	}
	else {
		document.getElementById(toggleObj).style.display = "none";
		document.getElementById(displayObj).style.display = "block";
		document.getElementById(toggleObj).disabled = true;
	}
}

function hideShow(obj) {
	if (document.getElementById(obj).style.display == "none") { 
		document.getElementById(obj).style.display = "block";
		document.getElementById(obj).disabled = false;
	}
	else {
		document.getElementById(obj).style.display = "none";
		document.getElementById(obj).disabled = true;
	}
}

function toggleSection(element) {
	var ctrlID = "ctrl_"+element;
	if (document.getElementById(element).style.display == "none") {
		document.getElementById(ctrlID).className='toggleControl_expanded';
		document.getElementById(element).style.display = "block";
	}
	else {
		document.getElementById(ctrlID).className='toggleControl_collapsed';
		document.getElementById(element).style.display = "none";
	}
}

function show(object,showControl,hideControl) {
	document.getElementById(object).style.display = "block";
	if(showControl) document.getElementById(showControl).style.display = "none";
	if(hideControl) document.getElementById(hideControl).style.display = "inline"; /* was block */
}

function hide(object,showControl,hideControl) {
	document.getElementById(object).style.display = "none";
	if(showControl) document.getElementById(showControl).style.display = "inline"; /* was block */
	if(hideControl) document.getElementById(hideControl).style.display = "none";
}

// Causes the 'objectId' div to either appear or disappear
// Pass in newDisplay = '' if you want it to appear
// Pass in newDisplay = 'none' if you want it to disappear
function changeObjectDisplay(objectId, newDisplay) {
    // first get a reference to the cross-browser style object 
    // and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.display = newDisplay;
	return true;
    } else {
	// we couldn't find the object, so we can't change its visibility
	return false;
    }
}

// function getStyleObject(string) -> returns style object
//  given a string containing the id of an object
//  the function returns the stylesheet of that object
//  or false if it can't find a stylesheet.  Handles
//  cross-browser compatibility issues.
//
function getStyleObject(objectId) {
  // checkW3C DOM, then MSIE 4, then NN 4.
  //
  if(document.getElementById && document.getElementById(objectId)) {
	return document.getElementById(objectId).style;
   }
   else if (document.all && document.all(objectId)) {  
	return document.all(objectId).style;
   } 
   else if (document.layers && document.layers[objectId]) { 
	return document.layers[objectId];
   } else {
	return false;
   }
}

function changeObjectVisibility(objectId, newVisibility) {
    // first get a reference to the cross-browser style object 
    // and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.visibility = newVisibility;
	return true;
    } else {
	// we couldn't find the object, so we can't change its visibility
	return false;
    }
}

function getElementsByClassName(classname) {
    if (document.getElementsByTagName) {
         var els = document.getElementsByTagName("*");
         var c = new RegExp('/b^|' + classname + '|$/b');
         var finalSet = new Array();
         var n=0;
         for (var i=0; i < els.length; i++) {
              if (els[i].className) {
                   if(c.test(els[i].className)) {
                   finalSet[n] = els[i];
                   n++;
                   }
              }
         }
         return finalSet;
    } else{return false;}
}


function showClass(objectClass,showControl,hideControl) {
	var theSet = getElementsByClassName(objectClass);
	for (var i=0; i < theSet.length; i++) {
		if (theSet[i].tagName == "TD") { // can't use "block" for table elements
			theSet[i].style.display = "";
		} 
		else if (theSet[i].tagName == "TH") {
			theSet[i].style.display = "";
		} 
		else if (theSet[i].tagName == "TR") {
			theSet[i].style.display = "";
		} 
		else {
			theSet[i].style.display = "block";
		}
	}		
	if (typeof showControl != 'undefined') {
		document.getElementById(showControl).style.display = "none";
	}
	if (typeof hideControl != 'undefined') {
		document.getElementById(hideControl).style.display = "block";
	}
	createCookie(objectClass,'1');
}

function hideClass(objectClass,showControl,hideControl) {
	var theSet = getElementsByClassName(objectClass);
	for (var i=0; i < theSet.length; i++) {
		theSet[i].style.display = "none";
	}
	if (typeof showControl != 'undefined') {
		document.getElementById(showControl).style.display = "block";
	}
	if (typeof hideControl != 'undefined') {
		document.getElementById(hideControl).style.display = "none";
	}
	createCookie(objectClass,'0');
//	alert(theSet);
}

//	Select contents of div via button
//	Script by hscripts.com
//	copyright of HIOX INDIA
//	Free javascripts @ http://www.hscripts.com
function fnSelect(objId)
{
   fnDeSelect();
   if (document.selection) 
   {
      var range = document.body.createTextRange();
      range.moveToElementText(document.getElementById(objId));
      range.select();
   }
   else if (window.getSelection) 
   {
      var range = document.createRange();
      range.selectNode(document.getElementById(objId));
      window.getSelection().addRange(range);
   }
}
function fnDeSelect() 
{
   if (document.selection)
             document.selection.empty();
   else if (window.getSelection)
              window.getSelection().removeAllRanges();
} 

function createPopup(theURL,winName,features) {
	window.open(theURL,winName,features);
}

function closeWindow(alertText) {
	
	if(alertText != '') {
		if( confirm(alertText) ) {
			window.close();
		}
	} else {
		window.close();
	}
}

function clearExpense(num) {
	d=document;
	d.getElementById('cat'+num).value='';
	d.getElementById('desc'+num).value='';
	d.getElementById('amt'+num).value='';
}

function confirmRedirect(confirm_text,url) {
	if (confirm_text) {
		question = confirm(confirm_text)
		if (question !="0") {
			top.location = url
		}
	}
	else {
		top.location = url
	}
}

function mustCheckBox(formObj, alert_text) {
	var message = '';
	if ( !formObj.agree.checked)
	{
		message = alert_text;
		alert(message);
		return false;
	}
	return true;
}

function limitLength(field, maxLength) {
	if ( field.value.length > maxLength ) {
		field.value = field.value.slice(0,maxLength);
	}
}

function limitCurrencyEntry(field) {
	allowed = '0123456789.';
	allowedWithoutPeriod = '0123456789';
	allowedValue = '';
	illegalValue = false;
	
	for(var i=0; i< field.value.length; i++) {
		if ( allowed.indexOf(field.value.charAt(i)) != -1) {
			/* Once one period has been found, remove all later periods */
			if( field.value.charAt(i) == '.' ) allowed = allowedWithoutPeriod;
			allowedValue += field.value.charAt(i);
		} else {
			illegalValue = true;
		}
	}
	/* If there is an illegal value, remove it by resetting the field without it */
	if( illegalValue )
		field.value = allowedValue;
}

function submitFormOnce(theform){
//if IE 4+ or NS 6+
	if (document.all||document.getElementById){
		//screen thru every element in the form, and hunt down "submit" and "reset"
		for (i=0;i<theform.length;i++){
			var tempobj=theform.elements[i];
			
			if(tempobj.type.toLowerCase()=="reset" || tempobj.type.toLowerCase()=="submit") {
				
				if(tempobj.value != 'Go Back & Edit' && tempobj.value != 'Preview') {
					//disable em
					tempobj.disabled=true;
				}
			}
		}
	}
    return true;
}

// removes the leading and trailing spaces from a string,
// similar to the java.lang.String.trim() function
// added by lturetsky, taken from http://www.voy.com/1888/58.html
function trim(st) {
    var len = st.length
    var begin = 0, end = len - 1;
    while (st.charAt(begin) == " " && begin < len) {
        begin++;
    }
    while (st.charAt(end) == " " && begin < end) {
        end--;
    }
    return st.substring(begin, end+1);
}

function formatPhone (field) {
    field.value = trim(field.value);

    var ov = field.value;
    var v = "";
    var x = -1;

    // is this phone number 'escaped' by a leading plus?
    if (0 < ov.length && '+' != ov.charAt(0)) { // format it
        // count number of digits
        var n = 0;
        if ('1' == ov.charAt(0)) {  // skip it
            ov = ov.substring(1, ov.length);
        }

        for (i = 0; i < ov.length; i++) {
            var ch = ov.charAt(i);

            // build up formatted number
            if (ch >= '0' && ch <= '9') {
                if (n == 0) v += "(";
                else if (n == 3) v += ") ";
                else if (n == 6) v += "-";
                v += ch;
                n++;
            }
            // check for extension type section;
            // are spaces, dots, dashes and parentheses the only valid non-digits in a phone number?
            if (! (ch >= '0' && ch <= '9') && ch != ' ' && ch != '-' && ch != '.' && ch != '(' && ch != ')') {
                x = i;
                break;
            }
        }
        // add the extension
        if (x >= 0) v += " " + ov.substring(x, ov.length);

        // if we recognize the number, then format it
        if (n == 10 && v.length <= 40) field.value = v;
    }
    return true;
}

// Searches for all links with attribute rel="external" and sets them to open in a new window
// This function is run on page load, every time
function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}
	}
}

function pageLoadFunctions() {
	externalLinks();
	// any functions that need to run on page load globally
}

// Custom greybox function to refresh the calling page after close
GB_showCenterRefresh=function(_49,url,_4b,_4c,_4d){
var _4e={caption:_49,center_win:true,height:_4b||500,width:_4c||500,fullscreen:false,callback_fn:_4d,reload_on_close:true};
var win=new GB_Window(_4e);
return win.show(url);
};


// replaced by jQuery dusoment ready function
//window.onload = pageLoadFunctions();


/* ****************************** */
/* ****** JQUERY FUNCTIONS ****** */
/* ****************************** */
$(document).ready(function() { // do not remove this wrapper!
// *** content toggle
	$('a.showMore').click(
		function() {
			$(this).parents('div.contentToggleBlock').find('.contentTruncated').hide(0);
			$(this).parents('div.contentToggleBlock').find('.contentNotTruncated').show(0);
			return false;
		}
	);

	$('a.showLess').click(
		function() {
			$(this).parents('div.contentToggleBlock').find('.contentNotTruncated').hide(0);
			$(this).parents('div.contentToggleBlock').find('.contentTruncated').show(0);
			return false;
		}
	);

// *** functions to run automatically onload
	// show truncated, hide full - this is to avoid hiding full content from googlebot, etc.
	// use with content toggle function above
	$('.contentTruncated').show(0);
	$('.contentNotTruncated').hide(0);
	
	// convert all links with rel attribute set to external to open in new window
	//$('a[@rel=external]').attr('target','_blank'); // TJF 2009-05-13 @attr was deprecated, then removed in jQuery v1.3.2
	$('a[rel=external]').attr('target','_blank');
});

//from javascript: the definitive  guide
/**
 * Add a rollover effect to the specified image, by adding event
 * handlers to switch the image to the specified URL while the
 * mouse is over the image.
 *
 * If the image is specified as a string, search for an image with that
 * string as its id or name attribute.
 * 
 * This method sets the onmouseover and onmouseout event handler properties
 * of the specified image, overwriting and discarding any handlers previously
 * set on those properties.
 */
function addRollover(img, rolloverURL) {
    if (typeof img == "string") {  // If img is a string,
        var id = img;              // it is an id, not an image
        img = null;                // and we don't have an image yet.

        // First try looking the image up by id
        if (document.getElementById) img = document.getElementById(id);
        else if (document.all) img = document.all[id];

        // If not found by id, try looking the image up by name.
        if (!img) img = document.images[id];
        
        // If we couldn't find the image, do nothing and fail silently
        if (!img) return;
    }

    // If we found an element but it is not an <img> tag, we also fail
    if (img.tagName.toLowerCase() != "img") return;

    // Remember the original URL of the image
    var baseURL = img.src;

    // Preload the rollover image into the browser's cache
    (new Image()).src = rolloverURL;

    img.onmouseover = function() { img.src = rolloverURL; }
    img.onmouseout = function() { img.src = baseURL; }
}

/**
 * Find all <img> tags in the document that have a "rollover"
 * attribute on them.  Use the value of this attribute as the URL of an 
 * image to be displayed when the mouse passes over the image and set 
 * appropriate event handlers to create the rollover effect.
 */
function initRollovers() {
    var images = document.getElementsByTagName("img");
    for(var i = 0; i < images.length; i++) {
        var image = images[i];
        var rolloverURL = image.getAttribute("rollover");
        if (rolloverURL) addRollover(image, rolloverURL);
    }
}
