﻿function ShowHelpMessage(message, btn)
{
  
  var helpMsg = document.getElementById("helpmsg");
  helpMsg.innerHTML = message;
  
  var helpBlock = document.getElementById("helpblock");
  var posY = findPosY(btn) - btn.offsetHeight;
  helpBlock.style.position = 'absolute';
  helpBlock.style.left = '3px';
  helpBlock.style.top = parseInt(posY) + 'px';
  helpBlock.style.display = 'block';
}

function OpenInPopup(url)
{
  var width = 800;
  var height = 600;
  var posLeft = (screen.availWidth - width) / 2;
  var posTop = (screen.availHeight - height) / 2;
  
  var features = 'location=no, menubar=no, resizable=yes, scrollbars=yes, status=yes, titlebar=no, toolbar=no';
  
  features += ', width=' + width;
  features += ', height=' + height;
  features += ', left=' + posLeft;
  features += ', top=' + posTop;
       
  newWindow = window.open(url, '_blank', features);
}

function getHeight() 
{ 
  var screenH = 0; 

  if (document.layers || (document.getElementById && !document.all)) 
  {  
    // Provided by most browsers, but importantly, NOT by Internet Explorer. 
    screenH = window.innerHeight - 100;
  } 
  else if (document.all) 
  { 
    if (parseInt(navigator.appVersion.indexOf("6") == -1)) 
    {
      // Provided by many browsers, including Internet Explorer. 
      screenH = document.body.clientHeight - 100;
    } 
    else 
    {
      // Provided by most DOM browsers, including Internet Explorer. 
      screenH = document.documentElement.clientHeight - 100;
    }
  }

  return screenH; 
}

function onlyOption(source, arguments) 
{
  var name = source.controltovalidate;

  for(var i = 0; i < 26; i++)
  {
    var optionO = document.getElementById(name + '_' + i);

    if(optionO == null)
      return;

    if (optionO.value == 'O') 
    {
      if (optionO.checked) 
      {
        for(var j = 0; j < 26; j++) 
        {
          if (i != j) 
          {
            var optionC = document.getElementById(name + '_' + j);

            if(optionC == null)
              return;

            if(optionC.checked) 
            {
              arguments.IsValid = false;
              return;
            }
          } 
        } 
      } 
      else
      {
        return;
      }
    }
  }
  
  return;
}

function scrollDown()
{
	if (document.forms[0]._ScenarioStart){
	  if(document.forms[0]._ScenarioStart.value == "false")
	  {

		var contentLyr = document.getElementById("content");
		if(contentLyr != null)
		{
			contentLyr.scrollTop = 9000;
		}
	  }
  }
}

function scrollToTop()
{

	var contentLyr = document.getElementById("content");
	if(contentLyr != null)
	{
		contentLyr.scrollTop = 0;
	}
}
        


//Set focus on first interactive element or scroll to bottom if no interactive element is found
function setScrollPosition()
{
//something is scrolling down before this functions is called.
scrollToTop();

	if(!setFocusOnFirstElement2())
	{
		scrollDown();	
	}
}

//Sets focus on first visible interaction element in first form
function setFocusOnFirstElement2()
{
  var foundFocus = false;
  var oHdnStepStart = document.getElementById("hdnStepStart");
  if(oHdnStepStart != null && oHdnStepStart.value == '1')
  {
    return true;
  }
    
  //Loop through all elements in a form, till we can focus one
  for(i = 0; i < document.forms[0].elements.length && !foundFocus; i++)
  {
    var current = document.forms[0].elements[i];
        
    //input tag: we only focus on text, passwords, radio and checkboxes (not buttons or hidden fields)
    if(current.tagName == "INPUT")
    {
      //text in fields must be selected
      if(current.type.toLowerCase() == "text" || current.type.toLowerCase() == "password")
      {
        current.focus();
//        current.select();
	  current.scrollIntoView();
        
        //we have found focus
        foundFocus = true;
      }
      else if(current.type.toLowerCase() == "radio" || current.type.toLowerCase() == "checkbox")
      {
        //current.focus();
	current.scrollIntoView();
        
        //we have found focus
        foundFocus = true;
      }
    }
    //other tags: try to focus them
    else if(current != null)
    {
      try
      {
        current.scrollIntoView();
        current.focus();
        
        //text in textareas must be selected
        if(current.tagName == "TEXTAREA")
        {
          //current.select();
        }
        
        //we have found focus
        foundFocus = true;
      }
      catch(err)
      {
      }
    }
  }  
 
  return foundFocus;
}

function toggleIntroForm(cssClasses)
{
    introFormTable = document.getElementById("FormEngine").getElementsByTagName("table")[0];
   
    if (introFormTable != null)
    {
       if(typeof(introFormTable.toggleVisible) == "undefined")
       {
          introFormTable.toggleVisible = true;
       }
	        
       var show = !introFormTable.toggleVisible;
       
       rows = introFormTable.getElementsByTagName("tr");
      
       for(var i = 0; i < rows.length; i++)
       {
         toggleIntroFormRow(rows[i], cssClasses, show);  
       }
       
       introFormTable.toggleVisible = show;
     }
}


function toggleIntroFormRow(row, cssClasses, show)
{
      spans = row.getElementsByTagName("span");
    
      for(var i = 0; i < spans.length; i++)
      {
        if(cssClasses.indexOf("#" + spans[i].className + "#") != -1)
        {
         row.style.display = show ? "" : "none";
         return;
     }
   }
}

