tableruler = function() {

  if (document.getElementById && document.createTextNode)
  {
    var tables=document.getElementsByTagName('table');
    for (var i=0;i<tables.length;i++)
    {
      if(tables[i].className=='ruler' || tables[i].className=='results')
      {
        var trs=tables[i].getElementsByTagName('tr');
        for(var j=0;j<trs.length;j++)
        {
          if(trs[j].parentNode.nodeName=='TBODY' && trs[j].parentNode.nodeName!='TFOOT')
          {
            trs[j].onmouseover=function(){this.className='ruled';return false}
            trs[j].onmouseout=function(){this.className='';return false}
          }
        }
      }
    }
  }
}
YAHOO.util.Event.addListener(window, 'load', tableruler);


function showModifyView() {
  YAHOO.util.Dom.setStyle('modifyView','display','block');
  YAHOO.util.Dom.setStyle('modifyView','visibility','visible');
}

function closeModifyView() {
  YAHOO.util.Dom.setStyle('modifyView','display','none');
  YAHOO.util.Dom.setStyle('modifyView','visibility','hidden');
}

var isQuoteFormOpen = false;
function toggleQuoteForm()
{
  if (isQuoteFormOpen == true)
  {
    closeQuoteForm();
    isQuoteFormOpen = false;
  }
  else
  {
    showQuoteForm();
    isQuoteFormOpen = true;
  }
}

function showQuoteForm() {
  YAHOO.util.Dom.setStyle('shipQuoteForm','display','block');
  YAHOO.util.Dom.setStyle('shipQuoteForm','visibility','visible');
}

function closeQuoteForm() {
  YAHOO.util.Dom.setStyle('shipQuoteForm','display','none');
  YAHOO.util.Dom.setStyle('shipQuoteForm','visibility','hidden');
  isQuoteFormOpen = false;
}

var toggle = function(el) {
  currentDisplay = YAHOO.util.Dom.getStyle(el,'display');
  currentVisible = YAHOO.util.Dom.getStyle(el,'visibility');

  if((currentDisplay == 'none') && (currentVisible == 'hidden')) {
    show(el);
  } else {
    hide(el);
  }
}

var show = function(el) {
  if(el != null && el != 'undefined')
  {
    YAHOO.util.Dom.setStyle(el,'display','block');
    YAHOO.util.Dom.setStyle(el,'visibility','visible');
  }
}

var hide = function(el) {
  if(el != null && el != 'undefined')
  {
    YAHOO.util.Dom.setStyle(el,'display','none');
    YAHOO.util.Dom.setStyle(el,'visibility','hidden');
  }  
}

//put this in a checkboxes "onclick" event, pass it "this" and the name of the dom element to be toggled
//this is special because Mozilla fires "onchange" on a checkbox as soon as it is clicked
//whereas IE (and Safari) don't fire "onchange" until the checkbox loses focus
var checkboxToggle = function(checkbox, el) {
  if(checkbox.checked == true) {
    show(el);
  } else {
    hide(el);
  }
}

function toggleShipsIn(prearrivalId, status)
{
  var prearrivalElement = document.getElementById(prearrivalId);
  if (prearrivalElement.style.display == "none" || prearrivalElement.style.display == "")
  {
    prearrivalElement.style.display="block";
    prearrivalElement.style.visibility="visible";
  }
  else
  {
    prearrivalElement.style.display="none";
    prearrivalElement.style.visibility="hidden";
  }
}

/* Defining this method here so that it can be overridden in individual files */
function globalClosePasswordHelpPopout()
{
  // when closing the popout, we make sure that the correct divs are visible
  show(document.getElementById("inputPrompt"));
  hide(document.getElementById("dynamicContent"));

  var popoutDiv = document.getElementById("forgotten-password-div");

  // clear out the email input field
  var inputDiv = document.getElementById("email-input");
  inputDiv.value = "";

  // close the popout
  if (popoutDiv != null && popoutDiv != 'undefined') hide(popoutDiv);
}

function showMessage(message, divId)
{
  var innerPopoutDiv = document.getElementById(divId);
  innerPopoutDiv.innerHTML = message;
}

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 var 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;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

//preload the loading image and make it available to all javascript further down the road
var grayLoadingImg = new Image();
grayLoadingImg.src = '/images/ajax-loading-spinner-small-tanbg.gif';

var magicIsAlreadyClicked = 0;
function clickOnce(url)
{
  if (magicIsAlreadyClicked == 0)
  {
    window.location=url;
  }
  magicIsAlreadyClicked += 1;

  if (magicIsAlreadyClicked > 1 )
  {
    // do nothing
  }
}

var sortLabel = "";
function setSortLabel(label)
{
  sortLabel = label;
}
function getSortLabel()
{
  return sortLabel;
}



function replace(string,text,by)
{
    // Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

var emailThisPageSuccess= function(response)
{
  var emailDiv = document.getElementById("emailThisPageMessage");
   // get the JSON object expression from the response
   var jsonExpression = "(" + response.responseText + ")";
   var values = eval(jsonExpression);
   var errors = values.errors;
   if (errors != null)
   {
    var errorText = getErrorText(errors);
    emailDiv.innerHTML = '<div class="error2">' + errorText + '</div>';
   }
   else
   {
    emailDiv.innerHTML = '<div class="error2">Your email has been sent.</div>';
   }
}

var emailThisPageFailure= function()
{
  var emailDiv = document.getElementById("emailThisPageMessage");
  emailDiv.innerHTML = '<div class="error2">Nay!</div>';
}

function submitEmailThisPage()
{
  var emailThisPageCallback = {success:emailThisPageSuccess,failure:emailThisPageFailure};
  YAHOO.util.Connect.setForm("emailThisPageForm");
  var request = vfAsyncRequest("POST", "/do/store/emailThisPage", emailThisPageCallback);
}

 function getErrorText(errors)
 {
   var errorText = '<div class="error2" id="errorText"><table><tr><td>';
   for (var i = 0; i < errors.length; i++)
   {
     errorText += "";
     errorText += '<tr><td><img src="/images/icon-exclamation-point-red-circle.gif"/></td>';
     errorText += '<td class="error2">' + errors[i] + "</td></tr>";
   }
   errorText += '</table><br/></div>';
   return errorText;
 }

 function getRadioGroupCheckedValue(radioGroup)
 {
    for (var i=0; i < radioGroup.length; i++)
    {
      if (radioGroup[i].checked)
      {
        return radioGroup[i].value;
      }
    }
 }


function installSearchEngine()
{
 if (window.external && ("AddSearchProvider" in window.external))
 {
   // Firefox 2 and IE 7, OpenSearch
   window.external.AddSearchProvider("http://vinfolio.com/html/search-plugin.xml");
 }
 else
 {
   // No search engine support (IE 6, Opera, etc).
   alert("No search engine support");
 }
}
