gSubmitting=false;

// This function is called by all TenFold web pages via onload="TFonLoadDefault();"
function TFonLoadDefault() {
	//showContentMainHeight( "before" );
	setContentMainHeight();
	//showContentMainHeight( "after" );
}

function SubmitForm(FormName)
{
  /* Check required fields. */
  var Form = document.getElementById(FormName);
  if (Form && !gSubmitting)
  {
    if (!CheckRequired(Form))
    { 
      if (FormName != 'membersignin')
      {
        gSubmitting=true;
      }
      /* Form will be submitted by the calling event. 
         This ensures the browser remembers field values for autocomplete. */
      return true;
    }
  }
  return false; /* return false to prevent the form submission */
}

function CheckRequired(TheForm)
{
  var bFound = false;
  var FormElements= TheForm.elements;
  var Msgv = document.getElementById(TheForm.name + '_errors');
  if (Msgv && Msgv.innerHTML != "")
  {
   Msgv.innerHTML = "";
  }
  
  var Html = "";
  for (var Index = 0; Index < FormElements.length; Index++)
  {
    var Obj = FormElements[Index];
    if (Obj.className.indexOf('required') != -1)
    {
      if ((Obj.type == 'select-one' && Obj.selectedIndex == 0)
       || (Obj.type == 'checkbox' && Obj.checked != true )
       || (Obj.type != 'select-one' && Obj.type != 'checkbox' && Obj.value == ""))
      {
        
        if (bFound == false)
        {
          Html = "<div class='errors'><span class='errormsg'>Please fill out these required fields</span>:<br>";
        }
        
        if (bFound == true)
        {
          Html += ', ';
        }
        bFound = true;
        var Label = document.getElementById(Obj.name + '_lbl');
        if (Label)
        {
          Html += Label.innerHTML;
        }
      }
    } 
  }
  if (Msgv && bFound)
  {
   Html += "</div>";
   Msgv.innerHTML = Html;
  }
  return bFound;
}

function SetInputValue(InputName, Value)
{
   var input = document.getElementById(InputName);
   if (input)
    {
       input.value = Value;
    }
}


function switchImage(imageName, newImageSrc) {
	if (document.images) {
		document.images[imageName].src = newImageSrc;
	}
} 

function insertEmailAddress( contactName, emailUser, emailHost )
{
   if (emailHost == "" ) emailHost = "tenfold.com";
   if (contactName == "" ) contactName = emailUser + "@" + emailHost;
   document.write( "<a href=" + "mailto:" + emailUser + "@" + emailHost+ ">" + contactName + "</a>" );
}

function setCSSclass( ElementID, CSSstyle ) {
	var ObjectID=document.getElementById(ElementID);
	if( ObjectID != null ) {
		ObjectID.className=CSSstyle;
	}
}


function getWindowHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && document.documentElement.clientHeight ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && document.body.clientHeight ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return( myHeight );
}

function setHeight( objectName, subtractor, minSize ) {
	var windowHeight = getWindowHeight();
	var areaHeight = windowHeight - subtractor;
	if ( areaHeight < minSize )
		areaHeight = minSize;
	var objectID = document.getElementById( objectName );
	if ( objectID != null )
		objectID.style.height = areaHeight;
}

function getWindowSize() {
  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 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 popUpGraphic( URL, xWidth, yHeight ) {
	var closeGraphic = document.getElementById("PopUpGraphicClose");
	var graphicFrame = document.getElementById("PopUpGraphicFrame");
	if ( !closeGraphic || !graphicFrame ) return true; // Return true to let normal href processing continue

	var Offset = getScrollXY();
	var winSize = getWindowSize();
	var scrollOffX = Offset[0], scrollOffY = Offset[1];
	var windowSizeX = winSize[0], windowSizeY = winSize[1];
    // Center popup iFrame in our main window
	if (xWidth > 0 && yHeight > 0) {	
	    var xc = (windowSizeX - 2 - xWidth) / 2;
		var yc = (windowSizeY - (30 + yHeight)) / 2;
		if (xc < 0 ) xc=0;
		if (yc < 10 ) yc=10;
		closeGraphic.style.top=(scrollOffY+yc)+"px";
		closeGraphic.style.left=xc+"px";
		closeGraphic.style.width=(xWidth+2)+"px";
		closeGraphic.style.height=(yHeight+30)+"px";
		graphicFrame.style.top=(scrollOffY+yc+30)+"px";
		graphicFrame.style.left=xc+"px";
		graphicFrame.style.width=(xWidth+2)+"px";
		graphicFrame.style.height=(yHeight+2)+"px";
	}
    // Make them visible
	closeGraphic.style.visibility="visible";
	graphicFrame.src=URL;
    graphicFrame.style.visibility="visible";
	return false; // Return false to prevent normal href processing
}

function pushDownGraphic() {
	var closeGraphic = document.getElementById("PopUpGraphicClose");
	var graphicFrame = document.getElementById("PopUpGraphicFrame");
	if ( closeGraphic && graphicFrame ) {
		closeGraphic.style.visibility="hidden";
		graphicFrame.style.visibility="hidden";
		graphicFrame.src='';
	}
	return;
}

function popupWindow( URL, xWidth, yHeight ) { /* height and width arugments are optional */
	var windowOptions='toolbar=false,scrollbars=false,menubar=false,location=false,directories=false,resizable=true';
	if( arguments.length >= 2 && xWidth > 0 ) windowOptions += ',width='+(xWidth+20);
	if( arguments.length >= 3 && yHeight > 0 ) windowOptions += ',height='+(yHeight+20);
	// Center new window on the user's screen
	if (window.screen && xWidth>0 && yHeight>0) {
	    var xc = ((screen.availWidth - 20) - xWidth) / 2;
	    var yc = ((screen.availHeight - 20) - yHeight) / 2;
	    windowOptions += ",left=" + xc + ",screenX=" + xc;
	    windowOptions += ",top=" + yc + ",screenY=" + yc;
	}
	var popupWindow = window.open( URL, 'PopupWindow', windowOptions );
	if (window.focus) {
		popupWindow.focus();
	}
	return false; // Return false to prevent normal href processing
}

function hideshow( tag ) {
	var tagID = tag.id;
	var text = document.getElementById(tagID + "_target");
	if(text.style.display == 'block') {
		text.style.display = 'none';
		tag.innerHTML = "more&raquo;";
	}
	else {
		text.style.display = 'block';
		tag.innerHTML = "less&laquo;";
	}
	return false;
}

//=================================================================
// These functions are for images with hover maps
//=================================================================
function CreateHoverMapImage( name, baseImage, imageMap, width, height, hoverImages, numHoverImages  ) {
	
	this.width = width;
	this.height = height;
	this.activeHover = -1;
	this.numHoverImages = numHoverImages;

	var html = "";

	// This outer Div sets up the container for everything else
	html += '<div id="' + name + '" style="position:relative; ';
	html += '     width:' + width + 'px; height:' + height + 'px; overflow:hidden;">\n';

	// this div contains the base image and is always visible
	html += '  <div id="' + name + '_baseimage" style="position:absolute; top:0; left:0; z-index:1;';
	html += '       width:' + width + 'px; height:' + height + 'px; ">\n';
	html += '     <img id="' + name + '_image"';
	html += '          src="' + baseImage + '" width=' + width + ' height=' + height + '/>\n';
	html += '  </div>\n';
	
	// This div will contain the highlighting overlays when called by the image map
	this.hoverImageList = new Array() // preload the hover images
	for ( var i=0; i<numHoverImages; i++ ) {
		this.hoverImageList[i] = new Image()
		this.hoverImageList[i].src = hoverImages[i]
		html += '  <div id="' + name + '_hover_' + i + '"';
		html += '       style="position:absolute; top:0; left:0; z-index:-10; display:none;';
		html += '              width:' + width + 'px; height:' + height + 'px; ">\n';
		html += '    <img src="' + hoverImages[i] + '" width=' + width + ' height=' + height + '/>\n';
		html += '  </div>\n';
	}
	
	// this div contains the image map and is connected to a transparent overlay image
	// this overlay image will allow us to change the highlight overlay without affecting the image map
	html += '  <div id="' + name + '_mapper" style="position:absolute; top:0; left:0; z-index:20; ';
	html += '              width:' + width + 'px; height:' + height + 'px; ">\n';
	html += '     <img src="images/spacer.gif" width=' + width + ' height=' + height + ' ';
	html += '          usemap="#' + imageMap + '" />\n';
	html += '  </div>\n';
	html += '</div>\n';
	
	// Put the html in the page where I'm at
	document.write( html );

	// Find and hold onto the hover containers so I can easily find it and raise them
	this.hoverContainers = new Array();
	for ( var i=0; i<numHoverImages; i++ ) {
		this.hoverContainers[i] = document.getElementById( name + '_hover_' + i );
	}
	return( this );
}

function HoverMapImage( hovermap, hoverNumber ) {
	if ( hovermap.activeHover >= 0 ) {
		hovermap.hoverContainers[hovermap.activeHover].style.zIndex = -10;
		hovermap.activeHover = -1;
	}
	hovermap.hoverContainers[hoverNumber].style.zIndex = 10;
	hovermap.hoverContainers[hoverNumber].style.display = "inline";
	hovermap.activeHover = hoverNumber;
}

function NoHoverMapImage( hovermap ) {
	if ( hovermap.activeHover >= 0 ) {
		hovermap.hoverContainers[hovermap.activeHover].style.zIndex = -10;
		hovermap.activeHover = -1;
	}
}

function setContentMainHeight() {
	var menuColumn = document.getElementById( "MenuColumn" );
	var menuColumnHeight = menuColumn ? (menuColumn.offsetHeight-168) : -1;
	
	var moreInfoColumn = document.getElementById( "MoreInfoColumn" );
	var moreInfoColumnHeight = moreInfoColumn ? (moreInfoColumn.offsetHeight-20) : -1;

	var contentMain = document.getElementById( "ContentMainWide" );
	if ( ! contentMain  ) {
		contentMain = document.getElementById( "ContentMain" );
	}
	if ( contentMain ) {
		var targetHeight = menuColumnHeight>moreInfoColumnHeight ? menuColumnHeight : moreInfoColumnHeight;
		//alert( "targetHeight=" + targetHeight + "  menuColumnHeight=" + menuColumnHeight +  " moreInfoColumnHeight=" + moreInfoColumnHeight + " contentMainHeight=" + contentMain.offsetHeight );
		
		if ( targetHeight > contentMain.offsetHeight ) {
			//contentMain.style.height = targetHeight + "px";
			contentMain.style.minHeight = targetHeight + "px";
			//alert( "set height to: " + targetHeight + "px" );
		}
	}
}

function showContentMainHeight( tag ) {
	var msg = "";
	var contentMain = document.getElementById( "ContentMainWide" );
	if ( ! contentMain  ) {
		contentMain = document.getElementById( "ContentMain" );
		msg += 'used ContentMain\n';
	}
	msg += 'ContentMain.style.minHeight is: ' + contentMain.style.minHeight + '\n';
	msg += 'ContentMain.offsetHeight is: ' + contentMain.offsetHeight + '\n';
	alert( tag + '\n' + msg );
}