function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function doPopUp(url){
	var newWindow;
	
	newWindow = window.open(url,'name','height=600,width=500,scrollbars=yes,resizable=yes,menubar=yes');
	
	if(window.focus) newWindow.focus();
}

// get target
function getEventTarget(e){
	if(window.event) // IE check
		el = window.event.srcElement;
	if(e && e.target) // standard-compliant browsers
		el = e.target;
	return el;
}

function appendOption(select, name, value)
{
  var elOptNew = document.createElement('option');
  elOptNew.text = name;
  elOptNew.value = value;

  try {
	  select.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
	  select.add(elOptNew); // IE only
  }
}



function getViewportXY(){
	var viewportwidth;
	var viewportheight;
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined')
	{
		viewportwidth = window.innerWidth,
		viewportheight = window.innerHeight
	}
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
	{
		viewportwidth = document.documentElement.clientWidth,
		viewportheight = document.documentElement.clientHeight
	}

	// older versions of IE
	else
	{
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
	return [ viewportwidth,viewportheight ];
}

function trim(str){
	if(str == "") return str;
	else return str.replace(/^\s+|\s+$/g, '');
}

function cursorProgress(e){
	e.style.cursor = "progress";
}

function cursorDefault(e){
	e.style.cursor = "default";
}


function hide(elementId){
	if(document.getElementById(elementId)) document.getElementById(elementId).style.display="none";
	//else alert(elementId + " does not exsist. JS Hide Error");
	else return;
}

function show(elementId){
	if(document.getElementById(elementId)) document.getElementById(elementId).style.display="block";
	//else alert(elementId + " does not exsist. JS Show Error");
	else return;
}

function showElement(e){
	if(e) e.style.display="block";
	else return;
}

function hideElement(e){
	if(e) e.style.display="none";
	else return;
}

function toggleElement(e){
	if(e){
		if(isHiddenElement(e)) showElement(e);
		else hideElement(e);
	}
	else return;
}

function isHiddenElement(e){
	return (e.style.display == "none");
}

function showAndHide(elementId1,elementId2){
	if(document.getElementById(elementId1)) document.getElementById(elementId1).style.display="block";
	//else alert(elementId1 + " does not exsist. JS ShowHide Error");
	else return;
	if(document.getElementById(elementId2)) document.getElementById(elementId2).style.display="none";
	//else alert(elementId2 + " does not exsist. JS ShowHide Error");
	else return;
}


function isDate(dateStr) {

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?

	if (matchArray == null) {
	//alert("Please enter a valid date by clicking the calendar icon.");
	return false;
	}

	month = matchArray[1]; // p@rse date into variables
	day = matchArray[3];
	year = matchArray[5];

	if (month < 1 || month > 12) { // check month range
	alert("Month must be between 1 and 12.");
	return false;
	}

	if (day < 1 || day > 31) {
	alert("Day must be between 1 and 31.");
	return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
	alert("Month "+month+" doesn`t have 31 days!")
	return false;
	}

	if (month == 2) { // check for february 29th
	var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
	if (day > 29 || (day==29 && !isleap)) {
	alert("February " + year + " doesn`t have " + day + " days!");
	return false;
	}
	}
	return true; // date is valid
}

function capitalize(obj) {

	if(obj.value) val = obj.value;
	else if(obj) val = obj;
	else return "";

	newVal = '';
    val = val.split(' ');
	for(var c=0; c < val.length; c++) {
        newVal += val[c].substring(0,1).toUpperCase() +
		val[c].substring(1,val[c].length) + ' ';
    }
	return newVal;
}

/*	var infoLinkHideTimeout = 0;
	var infoLinkPendingHide = false;

	function doHideInfoLink(){
		tipLinkDiv = document.getElementById("cbform-infoLink-div-id");
		tipLinkDiv.style.display = "none";
	}

	function hideInfoLink() {
		if(! infoLinkPendingHide){
			infoLinkPendingHide = true;
			infoLinkHideTimeout = setTimeout("doHideInfoLink()",225);
		}
	}*/

	function showInfoLink(message, e) {
		tipLinkDiv = document.getElementById("cbform-infoLink-div-id");
		messageDiv = document.getElementById("cbform-infoLink-message-div-id");
		tipLinkDiv.style.display = "block";
		messageDiv.innerHTML = message;
		ePos = YAHOO.util.Event.getXY(e);
		ePos[1] = ePos[1] - tipLinkDiv.offsetHeight;
		YAHOO.util.Dom.setXY(tipLinkDiv, ePos);
		if(infoLinkPendingHide){
			clearTimeout(infoLinkHideTimeout);
			infoLinkPendingHide = false;
		}
	}

	function clearCheckedRadios(eId){
		if(document.getElementById(eId).name){
			var radios = document.getElementsByName(document.getElementById(eId).name);
			for(var i = 0; i < radios.length; i++){
				radios[i].checked = false;
			}
		}
	}
	
	function resizeProgressDiv(progressDiv, containerDiv){
		dTop = YAHOO.util.Dom.getY(containerDiv);
		dHeight = containerDiv.offsetHeight;
		
		progressDiv.style.width = containerDiv.offsetWidth+"px";
		
		vXY = getViewportXY();
		sXY = getScrollXY();
		vHeight = vXY[1];
		sHeight = sXY[1];
		defaultPadding = 50;
		
		canFitOnScreen = false;
		// can it fit on screen
		if(dHeight <= vHeight) canFitOnScreen = true;
		
		if(canFitOnScreen){
			// figure out if it is on screen
			if(dTop > sHeight && ((dTop - sHeight) + dHeight) < vHeight){
				// on screen, just set to height
				progressDiv.style.height = dHeight+"px";
			}
			else{
				// off screen				
				//	Does this fit from the top
				// if so scroll to top
				if((dTop + dHeight) <= vHeight){
					window.scroll(0,0);
					progressDiv.style.height = dHeight+"px";
				}
				else{
					// element is to far down on the page
					// to just go to the top
					// see if it will fit with the default padding from the top
					if((defaultPadding + dHeight) <= vHeight){
						window.scroll(0,dTop - defaultPadding);
						progressDiv.style.height = dHeight+"px";
					}
					// otherwise, just scroll to the top of the div and set the height
					else{
						window.scroll(0,dTop);
						progressDiv.style.height = dHeight+"px";
					}
				}
			}
		}
		else{
			// too big for screen
			// set padding on top and bottom and display
			window.scroll(0,dTop - defaultPadding);
			progressDiv.style.height = (vHeight - (defaultPadding * 2)) + 'px';
		}

	}
	
	function getSelectedText()
	{
		var txt = '';
		if (window.getSelection){
			txt = window.getSelection();
		}
		else if (document.getSelection){
			txt = document.getSelection();
		}
		else if (document.selection){
			txt = document.selection.createRange().text;
		}
		else return;
		
		dump(txt.anchorNode);
		
		return txt;
	}
	
	function insertAtCursorUtil(myField, myValue) {
		//IE support
		if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
		}
		//MOZILLA/NETSCAPE support
		else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)
		+ myValue
		+ myField.value.substring(endPos, myField.value.length);
		} else {
		myField.value += myValue;
		}
		}