function pageHeight() {
	return window.innerHeight != null ? 
	window.innerHeight: document.documentElement && document.documentElement.clientHeight ? 
	document.documentElement.clientHeight:document.body != null ? 
	document.body.clientHeight:null;
}

function pageWidth() {
	return window.innerWidth != null ? 
	window.innerWidth : document.documentElement && document.documentElement.clientWidth ?       
	document.documentElement.clientWidth : document.body != null ? 
	document.body.clientWidth : null;
}

function winEditor(url,w,h,n) {
	if (w==""||w==undefined) {w=800;}
	if (h==""||h==undefined) {h=700;}
	if (n==""||n==undefined) {n='maintenance';}
	window.open( url, n, 'resizable=yes,height='+h+',width='+w+',left=25,top=25,scrollbars=yes,menubar=no,location=no');
	}

function PDFDisplay(url,w,h,n) {
	if (w==""||w==undefined) {w=900;}
	if (h==""||h==undefined) {h=750;}
	if (n==""||n==undefined) {n='pdf';}
	window.open( url, n, 'resizable=yes,height='+h+',width='+w+',left=15,top=15,scrollbars=yes,menubar=yes,location=no,titlebar=no');
	}

function popSite(url) {
	window.open( url, 'othersite', 'resizable=yes,height=600,width=850,left=50,top=50,scrollbars=yes,menubar=yes,location=yes');
	}

function chkSearch() {
	if (document.search.searchstr.value=="") { 
		return false; 
	} else {
		return true;
	}
}

function DoMath( value1, operator, value2 ) {

	operators	= '+-*/';
	v1			= ( isNaN( parseFloat( value1 ) ) ) ? 0 : parseFloat( value1 );
	v2			= ( isNaN( parseFloat( value2 ) ) ) ? 0 : parseFloat( value2 );

	if ( ( v1==0 && value1!='0'  && value1!='' ) || ( v2==0 && value2!='0'  && value2!='' ) || operators.indexOf( operator )<0 ) {
		return 'Error';
	} else {
		return eval(v1+operator+v2);
	}
}

function calcInputList( frm, list ) {
	result = 0;
	aList = list.split( " " );
	for (i in aList) {
		e = aList[i];
		o = e.substr(0,1);
		v1 = 'document.'+frm+'.'+e.substring(1)+'.value';
		v = eval(v1);
		result = DoMath( result, o, v );
	}
	if ( result != 'Error' && Math.round(result) != result ){
		result = Math.round( result * 100 ) / 100;
	}
	return result;
}

function unCheckRadio(oRadio) {
	  var or = document.getElementsByName(oRadio);
      for (var i = 0; i < or.length; i++) {
         or[i].checked = false;
     }
}

function sessionWarning() {
	var currentTime = new Date()
	var hours = currentTime.getHours()
	var minutes = currentTime.getMinutes()
	if (minutes < 10) minutes = "0" + minutes;
	var thetime = hours + ":" + minutes + " ";
	if(hours > 11){
		thetime = thetime + "PM"
	} else {
		thetime = thetime + "AM"
	}
	alert('Without activity, your session will time out in one minute.\n\nThis message was displayed at: ' + thetime);
}


function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

// this fixes an issue with the old method, ambiguous values 
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}	

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
		( ( path ) ? ";path=" + path : "") +
		( ( domain ) ? ";domain=" + domain : "" ) +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
	
function disableForms() {
var objfms = document.forms;
for(var i=0;i < objfms.length;i++) {
	disableForm( objfms[i] );
	}
}

function disableForm(theform) {
	if (document.all || document.getElementById) {
		for (i = 0; i < theform.length; i++) {
			var tempobj = theform.elements[i];
			if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset" || tempobj.type.toLowerCase() == "image" || tempobj.type.toLowerCase() == "button")
			tempobj.disabled = true;
		}
		return true;
	} else {
		return false;
	}
}
function disableLinks(){
var objLink = document.links;
for(var i=0;i < objLink.length;i++) {
	objLink[i].disabled=true;
	objLink[i].onclick = new Function("return false;");
	}
	return true;
}
function waitaminute(msec,msec2,msg) {
	if (msec == null){
		msec = 100;
	}
	if (msec2 == null){
		msec2 = 1500;
	}
	if (msg == null) {
		msg = 'Processing...';
	}
	//disableForms();
	var x=setTimeout("disableLinks()",msec);
	var t=setTimeout("showwaitdiv('"+msg+"')",msec2);
	return true;
}
function showwaitdiv(msg) {
	document.getElementById('pleasewait').style.display='block';
	document.getElementById('processing').style.display='block';
	if (msg != undefined){
		document.getElementById('processingmessage').innerHTML = msg;
	} else {
		document.getElementById('processingmessage').innerHTML = 'Processing...';
	}
}
function setLinkDisabler(){
	var objLink = document.links;
	for(var i=0;i < objLink.length;i++) {
		objLink[i].onclick = function(){waitaminute();};
	}
}

function checkboxLimit( o, limit ){
       var l = document.getElementsByName(o.name)
        var found=0;
        for( var i=0; i < l.length; ++i) 
        {
                if( l[i].checked )
                        ++found;
        }
        if(found>limit) {
			alert("Please Select a maximum of " + limit);
          return false;
        }
}

function showBubble( text ) {
	document.getElementById( 'bubble' ).innerHTML = text;
	document.getElementById( 'bubble' ).style.display = "block";
}
function hideBubble() {
	document.getElementById( 'bubble' ).style.display = "none";
	document.getElementById( 'bubble' ).innerHTML = '';
}
function toggleLayer( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}
function DashPos(){
	var DashDiv = document.getElementById("DashContainer");
	var winX = pageWidth();
	var winY = pageHeight();
	var cookieLeft = Get_Cookie('Dashboard-x');
	var cookieTop = Get_Cookie('Dashboard-y');
	if (parseFloat(cookieLeft) > 0){
		var DashLeft = Math.min(parseFloat(cookieLeft),winX-325);
		if (DashLeft < parseFloat(cookieLeft)) Set_Cookie('Dashboard-x',DashLeft);
	} else {
		var DashLeft = 10;
	}
	if (parseFloat(cookieTop) > 0){
		var DashTop = Math.min(parseFloat(cookieTop),winY-90);
		if (DashTop < parseFloat(cookieTop)) Set_Cookie('Dashboard-y',DashTop);
	} else {
		var DashTop = 10;
	}
	if (Get_Cookie('DashFlow')=='hidden'){
		document.getElementById('DashFlow').style.display='none';
		document.getElementById('DashButton').src = 'images/framework/maximize.jpg';
	} else {
		document.getElementById('DashFlow').style.display='block';
		document.getElementById('DashButton').src = 'images/framework/minimize.jpg';
	}
	DashDiv.style.top = DashTop + 'px';
	DashDiv.style.left = DashLeft + 'px';
	DashDiv.style.display='block';
}
function toggleDashFlow () {
	if(document.getElementById('DashFlow').style.display=='none'){
		document.getElementById('DashFlow').style.display='block';
		document.getElementById('DashButton').src = 'images/framework/minimize.jpg';
		Set_Cookie('DashFlow', 'visible');
	} else {
		document.getElementById('DashFlow').style.display='none';
		document.getElementById('DashButton').src = 'images/framework/maximize.jpg';
		Set_Cookie('DashFlow','hidden');
	}
}
function CheckUploadType( obj, fileName, fileTypes ) {
	if (!fileName) return;
	dots = fileName.split(".")
	fileType = "." + dots[dots.length-1].toLowerCase();
	fileTypes[0] = "." + fileTypes[0];
	if (fileTypes.join(".").toLowerCase().indexOf(fileType) != -1) {
		obj.className = "";
	} else {
		obj.style.backgroundColor='pink';
		alert("This file will not be accepted due to its type (" + fileType + ").\n\nWe only accept the following types of files: \n\n" + (fileTypes.join(" .")) + "\n\nPlease re-select another file.");
	}
}
function SameAsBuilder (p,a,w) {
	var d=new Date();
	var t = escape(d.toString());
	$.get('/ajax/MakeSameAs.cfm', {p: p, a: a, w: w, t: d },function(data){alert(data.replace(/^\s+|\s+$/g,""));});
	//alert( 'Created' );
	return false;
}
function slideSwitch() {
    var $active = $('#slideshow IMG.active');
    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');
    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next() : $('#slideshow IMG:first');
    // uncomment the 3 lines below to pull the images in random order
    //var $sibs  = $active.siblings();
    //var rndNum = Math.floor(Math.random() * $sibs.length );
    //var $next  = $( $sibs[ rndNum ] );
    $active.addClass('last-active');
    $next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() { $active.removeClass('active last-active'); });
}
function uploadRevert(ipt,prmpt,outdir,origfile) {
	document.getElementById( ipt + '_revert' ).style.display='none';
	$.get("../ajax/UploadDisplay.cfm", { file: document.getElementById( ipt + '_orgfil' ).value, dir: outdir, title: prmpt }, function(data){ document.getElementById( 'thumb_' + ipt ).innerHTML = data });
	var action = document.getElementById( ipt + '_action');
	if ( action.value == 'upload' ) {
		$.get("../ajax/UploadDelete.cfm", { file: document.getElementById( ipt + '_value').value, dir: outdir });
	}
	action.value = '';
	document.getElementById( ipt ).value = document.getElementById( ipt + '_value').value;
	document.getElementById( ipt + '_value').value = '';
	document.getElementById( ipt + '_curfil').value = origfile;
	document.getElementById( ipt + '_upload' ).style.display='inline';
	if ( document.getElementById( ipt + '_contrl').value.indexOf( 'S' ) >= 0 ) document.getElementById( ipt + '_select' ).style.display='inline';
	if ( document.getElementById( ipt + '_contrl').value.indexOf( 'D' ) >= 0 ) document.getElementById( ipt + '_delete' ).style.display='inline'; 
	if ( document.getElementById( ipt + '_contrl').value.indexOf( 'T' ) >= 0 ) document.getElementById( ipt + '_detach' ).style.display='inline';
	if (origfile != '') origfile = origfile + '&nbsp;';
	document.getElementById( ipt + '_name' ).style.color='#333';
	document.getElementById( ipt + '_name' ).innerHTML = origfile;
	document.getElementById( ipt + '_client' ).value = '';
	return false;
}
function uploadDelDet( ipt, prmpt, actn ) {
	document.getElementById( ipt + '_action' ).value = actn;
	document.getElementById( ipt + '_value' ).value = document.getElementById( ipt ).value;
	document.getElementById( ipt + '_curfil' ).value = '';
	document.getElementById( ipt  ).value = '';
	document.getElementById( ipt + '_detach' ).style.display='none';
	document.getElementById( ipt + '_upload' ).style.display='none';
	document.getElementById( ipt + '_select' ).style.display='none';
	document.getElementById( ipt + '_delete' ).style.display='none';
	document.getElementById( 'thumb_' + ipt ).innerHTML='';
	$.get("../ajax/UploadDisplay.cfm", { file: '', dir: '', title: prmpt }, function(data){ document.getElementById( 'thumb_' + ipt ).innerHTML = data });
	document.getElementById( ipt + '_name' ).innerHTML='';
	document.getElementById( ipt + '_revert' ).style.display='inline';
	var haschanged = document.getElementById('datachanged');
	if (haschanged) document.getElementById('datachanged').value = 1;
	return false;
}

function uploadAjaxBuilder(btn,ipt,prmpt,outdir,w,h) {
	var button = $(btn);
	new AjaxUpload(button,{
		action: '../ajax/UploadAjax.cfm',
		name: ipt,
		data: { 'field': ipt, 'outdir' : outdir, 'prompt' : prmpt, 'width': w, 'height': h },
		onSubmit : function(file, ext){
			this.disable();
			showwaitdiv('Uploading...');
		},
		onComplete: function(file, response){
			this.enable();
			response = response.trim();
			if (response.indexOf('Error:') == 0) {
				document.getElementById('processing').style.display='none';
				document.getElementById('pleasewait').style.display='none';
				alert( response );
			} else {
				var origfile = document.getElementById( ipt + '_orgfil' ).value;
				var fixed = fixFilename(file)
				$.get("../ajax/UploadDisplay.cfm", { file: response, dir: outdir, title: prmpt }, function(data){ document.getElementById( 'thumb_' + ipt ).innerHTML = data; });
	 			document.getElementById( ipt + '_value' ).value = response;
	 			document.getElementById( ipt + '_action' ).value = 'upload';
	 			document.getElementById( ipt + '_client' ).value = file;
				document.getElementById( ipt + '_detach' ).style.display='none';
				document.getElementById( ipt + '_upload' ).style.display='none';
				document.getElementById( ipt + '_select' ).style.display='none';
				document.getElementById( ipt + '_delete' ).style.display='none';
				document.getElementById( ipt + '_revert' ).style.display='inline';
				document.getElementById( ipt + '_name' ).style.color='red'; 
				if ( response != fixed && fixed != origfile ){
					document.getElementById( ipt + '_name' ).innerHTML = response + '&nbsp;';
					document.getElementById( ipt + '_curfil' ).value = response;
					alert( 'The file you uploaded ('+fixed+') was renamed to '+response+' for uniqueness.\n\nThis implies that a file named '+fixed+' already exists on the server and is NOT associated with this data record.\n\nAfter clearing this alert, you may:\n\nClick UPDATE to keep this uniquely-named but possibly duplicate file\n\nClick CANCEL or REVERT to avoid a possible duplication')
				} else {
					document.getElementById( ipt + '_name' ).innerHTML = fixed + '&nbsp;'; 
					document.getElementById( ipt + '_curfil' ).value = fixed;
				}
				if ( response != fixed && fixed == origfile ){
					alert( 'A file of the same name as your upload ('+fixed+') already exists on the \nserver and is associated with this data record.\n\nAfter clearing this alert, you may:\n\nClick UPDATE to *overwrite* the current file\n\nClick CANCEL to avoid overwriting the existing file');
				}
				document.getElementById('processing').style.display='none';
				document.getElementById('pleasewait').style.display='none';
				var haschanged = document.getElementById('datachanged');
				if (haschanged) document.getElementById('datachanged').value = 1;
			}
		}
	});
	return false;
}
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
function fixFilename(str){
	var ext = '';
	if (str.indexOf('.')>-1){
		ext = str.substr( str.lastIndexOf( "." ), 10 );
		str = str.substr( 0, str.lastIndexOf( "." ) );
	}
	str = str.replace( /[\s-]/g, '_' );
	str = str.replace( /[^a-zA-Z0-9_]/g, '' )+ext;
	return str;
}