/*--------------------------------------------------|
| fdsCommon.js                                      |
| This file contains common JavaScript code for the |
| FDS website.                                      |
|---------------------------------------------------|
| Copyright (c) 2005 Formation Design Systems       |
|--------------------------------------------------*/

function loadkbarticle(URL)
{
  document.getElementById('kbarticleplaceholder').innerHTML="<iframe height=100% width=100% src='" + URL + "'></iframe>";
}

/* Style Switcher */

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

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 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;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

// this function is needed to work around 
// a bug in IE related to element attributes
function hasClass(obj) {
   var result = false;
   if (obj.getAttributeNode("class") != null) {
       result = obj.getAttributeNode("class").value;
   }
   return result;
}   

/* Usage: stripe(id of table,row1 color, row2 color); */
function stripe(id) {

    // the flag we'll use to keep track of 
    // whether the current row is odd or even
    var even = false;
  
    // if arguments are provided to specify the colours
    // of the even & odd rows, then use the them;
    // otherwise use the following defaults:
    var evenColor = arguments[1] ? arguments[1] : "#fff";
    var oddColor = arguments[2] ? arguments[2] : "#eee";
  
    // obtain a reference to the desired table
    // if no such table exists, abort
    var table = document.getElementById(id);
    if (! table) { return; }
    
    // by definition, tables can have more than one tbody
    // element, so we'll have to get the list of child
    // &lt;tbody&gt;s 
    var tbodies = table.getElementsByTagName("tbody");

    // and iterate through them...
    for (var h = 0; h < tbodies.length; h++) {
    
     // find all the &lt;tr&gt; elements... 
      var trs = tbodies[h].getElementsByTagName("tr");
      
      // ... and iterate through them
      for (var i = 0; i < trs.length; i++) {

        // avoid rows that have a class attribute
        // or backgroundColor style
        if (! hasClass(trs[i]) &&
            ! trs[i].style.backgroundColor) {
 		  
          // get all the cells in this row...
          var tds = trs[i].getElementsByTagName("td");
        
          // and iterate through them...
          for (var j = 0; j < tds.length; j++) {
        
            var mytd = tds[j];

            // avoid cells that have a class attribute
            // or backgroundColor style
            if (! hasClass(mytd) &&
                ! mytd.style.backgroundColor) {
        
              mytd.style.backgroundColor =
                even ? evenColor : oddColor;
            
            }
          }
        }
        // flip from odd to even, or vice-versa
        even =  ! even;
      }
    }
}

function IsRadio(ss)
{
  switch(ss.type)
  {
    case "select-one":
    case "text":
    case "textarea": 
      return false;
      break; 

    case "radio":
    default:
      if (ss[0].type == "radio")
        return true;
	}
  return false;
}

function FieldRequired(ss)
{
//DEBUG  alert(ss.name+": type = "+ss.type+", value = "+ss.value+", length="+ss.length);
  switch(ss.type)
  {
    case "radio": { return CheckRequired(ss); break; }
    case "select-one": { return SelectionRequired(ss); break; }
    case "text":
    case "textarea": { return ContentRequired(ss); break; }
    default:
    {
      if (ss[0].type == "radio")
        return CheckRequired(ss);
      break;
    }
  }
  return true;
}

function ContentRequired(ss) {
if(ss.value.length > 0) { return false; }
return true;
}

function CheckRequired(ss) {
for(var i = 0; i < ss.length; i++) {
  if(ss[i].checked) { return false; }
  }
return true;
}

function SelectionRequired(ss) {
for(var i = 0; i < ss.length; i++) {
  if(ss[i].selected) {
    if(ss[i].value.length) { return false; }
    }
  }
return true;
}

function insertAfter(parent, node, referenceNode) {
	parent.insertBefore(node, referenceNode.nextSibling);
}

function FlagField(ss) 
{
	var errorID="";

	if(IsRadio(ss))
		errorID = 'error'+ss[0].name;
	else 	
		errorID = 'error'+ss.name;
  if(!document.getElementById(errorID))
	{
		var errorClass='fieldError';
  	var errorIndicator=document.createElement('img');
  	errorIndicator.id=errorID;
		errorIndicator.src='/extras/FDS/error.gif';
  	errorIndicator.alt='Error';
  	errorIndicator.title='Error!';
  	ss.className=errorClass;
    if (IsRadio(ss))
      insertAfter(ss[ss.length-1].parentNode,errorIndicator,ss[ss.length-1].nextSibling);
    else
  	  insertAfter(ss.parentNode,errorIndicator,ss);
	}
}

function ClearFlag(ss)
{
  var errorID = "";
	if(IsRadio(ss))
		errorID = 'error'+ss[0].name;
	else 	
		errorID = 'error'+ss.name;

  var oldFlag = document.getElementById(errorID);
  if (oldFlag)
	{
	  ss.className="";
		oldFlag.parentNode.removeChild(oldFlag);
	}
}
