function BrowserInfo()
{
    var agent = navigator.userAgent.toLowerCase();
    this.major = parseInt(navigator.appVersion);
    this.minor = parseFloat(navigator.appVersion);
    this.isNav  = ((agent.indexOf('mozilla')!=-1) && ((agent.indexOf('spoofer')==-1) && (agent.indexOf('compatible') == -1)));
    this.isNav4 = (this.isNav && (this.major == 4));
    this.isNav6 = (this.isNav && ( this.major >= 5));
    this.isIE   = (agent.indexOf("msie") != -1);
    this.isIE4  = (this.isIE && (this.major == 4));
    this.ie4  = this.isIE4
    this.isIE4Up  = (this.isIE && (this.major >= 4));
    this.isIE5  = ( this.isIE && (this.major == 4) && (agent.indexOf("msie 5.0")!=-1));
    this.isIE55  = ( this.isIE && (this.major == 4) && (agent.indexOf("msie 5.5")!=-1));
    this.isIE60  = ( this.isIE && (this.major == 4) && (agent.indexOf("msie 6.0")!=-1));
    this.isIE55Up = this.isIE55 || this.isIE60;
    this.isIE50Up = this.isIE5 || this.isIE55Up;
    this.isOpera = (agent.indexOf("opera") != -1);
    this.op3 = (agent.indexOf("opera") != -1);

    this.isSafari = agent.indexOf("safari") != -1;
    // IE 5 or greater and Netscape 6 or greater.

    this.isHTML4 = (this.isNav6 || this.isIE50Up);
    this.isDOM4 = (this.isNav6 || this.isSafarai)
}

var browserinfo = new BrowserInfo()

function handleKeyPress(evt)
{
  var event = browserinfo.isIE ? window.event : evt;
  var character = event.keyCode;
  
  if ( character == 13 )
  {
    return false;
  }
  return true;
}

if ( browserinfo.isIE ) {document.onkeydown = handleKeyPress;}
if ( browserinfo.isNav ) {document.onkeypress = handleKeyPress; document.onclick =handleKeyPress;}
  
function getElmById(aID){ 
var element = null;
 if ( browserinfo.isIE ) { element = jsGetObjectRef(aID); }
 else { element = document.getElementById(aID); }
 return element;
} 

function jsGetObjectRef(sID, sName) { //, sInForm
  var oObject;
  var bIsIE   = document.all?true:false;
  var bIsNav  = !bIsIE;
   
  if (typeof sName != "string" ) {sName = sID;}
  if (bIsIE) {
    oObject = eval("document.all." + sID);
  } else { //Navigator
    if (frames.length > 0) {
      navLoop:
      for (var h = 0; h < frames.length; h++) {
        for (var i = 0; i < frames[h].document.forms.length; i++) {
          for (var j = 0; j < frames[h].document.forms[i].elements.length; j++) {
            if (frames[h].document.forms[i].elements[j].name == sName) {
              oObject = frames[h].document.forms[i].elements[j];
              break navLoop;
            }
          }
        }
      }
    } else {
      navNFLoop:
      for (var i = 0; i < document.forms.length; i++) {
        for (var j = 0; j < document.forms[i].elements.length; j++) {
          if (document.forms[i].elements[j].name == sName) {
            oObject = document.forms[i].elements[j];
            break navNFLoop;
          }
        }
      }
    }
    //oObject = eval("document.forms['" + sInForm + "']." + sName);
  }
  return oObject;
} //jsGetObjectRef

function jsResize(sWidth, sHeight) {
  window.resizeTo(sWidth,sHeight);
}

function jsResizeScreen(sWidth) {
  sHeight = window.outerHeight;
  window.resizeTo(sWidth,sHeight);
}

function jsPopup(sUrl) {
  var winOpen = window.open(sUrl,'Information','width=760,height=550,menubar=no,toolbar=no')
}

function jsPopupScroll(sUrl) {
  var winOpen = window.open(sUrl,'Information','width=760,height=550,menubar=no,toolbar=no,scrollbars=yes')
}

function jsPrint(sUrl) {
  var winOpen = window.open(sUrl,'Print','width=790,height=550,menubar=yes,resizable=yes,scrollbars=yes')
}

function jsNRCSSearch() {
  var keyword = jsGetObjectRef('searchfield','searchfield');
  var sURL = 'http://www.usda.gov/wps/portal/!ut/p/_s.7_0_A/7_0_1OB?navid=SEARCH&amp;q=' + keyword.value;
  var winOpen = window.open(sURL,'Search','width=790,height=580,menubar=yes,toolbar=yes,resizable=yes,scrollbars=yes');
}

function jsDisplayDiv(item, node){ 
  var itm=document.getElementById(item);
  var nde=document.getElementById(node);

  if (itm.style.display == 'none') {
    nde.src='images/minus.gif';
    itm.style.display ='block';
  } else {
    nde.src='images/plus.gif';
    itm.style.display ='none';
  }
}

function jsWindow(sUrl, sTitle, sOptions) {
  var winOpen = window.open(sUrl, sTitle, sOptions)
}
  
function PopulateEnergyUnitLink() {
  // Get selected
  var powerDDL = getElmById("ddlPowerSource"); 
  var powerID = powerDDL.options[powerDDL.selectedIndex].value;
  var energyUnit = "[energy unit]";
  
  // Identify desired label
  switch (powerID)
  {
      case '1': { energyUnit = '$ / KWH'; break; }
      case '2': { energyUnit = '$ / Gallon'; break; }
    case '3': { energyUnit = '$ / MCF'; break; }
    case '4': { energyUnit = '$ / Gallon'; break; }
    case '5': { energyUnit = '$ / Gallon'; break; }
    default: { energyUnit = '[energy unit]'; break; }
    
  }
  
  // Write to link
  var oMode = getElmById("lblEnergyUnit");
  oMode.innerHTML = energyUnit;  
  oMode.style.visibility="visible"; 
}

function SetRateForCrop() { 

  var oCropValueList = getElmById("hiddenCropRateList");
  var oddCrops = getElmById("ddlCrops");
  var otxtValue = getElmById("txtIrrigationRate");
  var cropId = -1;

  if(typeof oddCrops == "object" )
  {
    if( oddCrops.selectedIndex > 0 )
    {
      cropId = oddCrops.options[ oddCrops.selectedIndex ].value;
    }
  }

  if(typeof oCropValueList == "object" )
  {
    // split the list into a java script array by splitting on delimitter.
    var aCropValue = oCropValueList.value.split("|");
    var aIdsValues;
    var noVal = 0;
    
    for( var i=0; i<aCropValue.length; i++)
    {
      // split out crop ids and values
      aIdsValues = aCropValue[i].split("^");

      //loop for id to get value
      for( var j=0; j<aIdsValues.length; j=j+2)
      {
        //if id then put value (the next element) into the text box
        if(aIdsValues[j] == cropId)
        { 
          if(typeof otxtValue == "object" )
          {
            if (aIdsValues[j+1]!=noVal)
            {
              otxtValue.value = aIdsValues[j+1];
              j = aIdsValues.length;
              i = aCropValue.length;
            }
            else 
            {
              otxtValue.value = " ";
              j = aIdsValues.length;
              i = aCropValue.length;
            }
          }
        }
      }
    }
  }
}

