//Start Diagnostic Scripts

function tester () {

	document.write("<h2>Hello World!</h2>");

	}

function tester2 () {

	document.write("<h4>" + location.pathname + "</h4>")

	}

//End Diagnostic Scripts


//Start Production Scripts



function doscripts () {

// This function executes all script that need to be run on page load. The
// doscripts() function is manually called in a stand-alone script block 
// located immediately below the <body> tag, rather than by the <body> tag 
// onLoad action, to assure that the all scripts execute in advance of page load.

	Is();
	BrowserAdjustments();
	HomeSite();
	CurrentMonth();
}



function breadcrumb () { 

// This function dynamically generates the Breadcrumb trail with links to section
// home pages at each directory hierarchy level. It assumes the prior execution of 
// the HomeSite() function to calculate the Home Page URL.

	// Retrieve the current path
	bcpath = location.pathname

	// Parse the current path without the filename into an array
	bcpath = bcpath.slice(0,bcpath.lastIndexOf("/"))
	var pathArray = bcpath.split(/\//g)

//	Diagnostics
//	document.write("<h4>The Bread Crumb Trail is: " + bcpath + "</h4>")
//	document.write("<h4>Path Levels: " + pathArray.length + "</h4>")


	// Output a static wwwroot home page link followed by links to home pages for each 
	// directory level in the array. Replace underscores with spaces in the display
	// and format with greater than signs. Non-root home page file should be named
	// "<directory name>_home.html". That is, the home page for the directory 
	// "Pubs_and_forms" should be named "pubs_and_forms_home.html".
	//
	// Test for array item null string added 3/16/07 as workaround for Mozilla 
	// (Firefox/Netscape) string.split bug.

	var i = 0
	var hrefpath = ""
	document.write("<p class='breadcrumb'><br/>You are Here:  <a href=" + hpage + ">NFC Home</a> &gt; ")
	while (i < pathArray.length) {
		if (pathArray[i]) {
			hrefpath = hrefpath + "/" + pathArray[i]
			bchref = hrefpath + "/" + pathArray[i] + "_home.html"
			bcpath = "<a href='" + bchref + "'>" + pathArray[i].replace(/_/g, " ") + "</a>"
			if (i != pathArray.length - 1) {
				bcpath = bcpath + " &gt; "
			}
			document.write(bcpath)
		}
		i++
	} 
	document.write("</p>")
}


function Is () {

// Limited Function Browser Sniff

    // convert all characters to lowercase to simplify testing
    var agt=navigator.userAgent.toLowerCase();

    // *** BROWSER VERSION ***
    // Note: On IE5, these return 4, so use is.ie5up to detect IE5.

    this.major = parseInt(navigator.appVersion);
    this.minor = parseFloat(navigator.appVersion);

    // Note: Opera and WebTV spoof Navigator.  We do strict client detection.
    // If you want to allow spoofing, take out the tests for opera and webtv.
    this.nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
                && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
                && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
    this.nav2 = (this.nav && (this.major == 2));
    this.nav3 = (this.nav && (this.major == 3));
    this.nav4 = (this.nav && (this.major == 4));
    this.nav4up = (this.nav && (this.major >= 4));
    this.navonly = (this.nav && ((agt.indexOf(";nav") != -1) ||
                          (agt.indexOf("; nav") != -1)) );
    this.nav6 = (this.nav && (this.major == 5));
    this.nav6up = (this.nav && (this.major >= 5));
    this.gecko = (agt.indexOf('gecko') != -1);

    this.ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
    this.ie3    = (this.ie && (this.major < 4));
    this.ie4    = (this.ie && (this.major == 4) && (agt.indexOf("msie 4")!=-1) );
    this.ie4up  = (this.ie  && (this.major >= 4));
    this.ie5    = (this.ie && (this.major == 4) && (agt.indexOf("msie 5.0")!=-1) );
    this.ie5_5  = (this.ie && (this.major == 4) && (agt.indexOf("msie 5.5") !=-1));
    this.ie5up  = (this.ie  && !this.ie3 && !this.ie4);
    this.ie5_5up =(this.ie && !this.ie3 && !this.ie4 && !this.ie5);
    this.ie6    = (this.ie && (this.major == 4) && (agt.indexOf("msie 6.")!=-1) );
    this.ie6up  = (this.ie  && !this.ie3 && !this.ie4 && !this.ie5 && !this.ie5_5);

    // KNOWN BUG: On AOL4, returns false if IE3 is embedded browser
    // or if this is the first browser window opened.  Thus the
    // variables is.aol, is.aol3, and is.aol4 aren't 100% reliable.
    this.aol   = (agt.indexOf("aol") != -1);
    this.aol3  = (this.aol && this.ie3);
    this.aol4  = (this.aol && this.ie4);
    this.aol5  = (agt.indexOf("aol 5") != -1);
    this.aol6  = (agt.indexOf("aol 6") != -1);

    this.opera = (agt.indexOf("opera") != -1);
    this.opera2 = (agt.indexOf("opera 2") != -1 || agt.indexOf("opera/2") != -1);
    this.opera3 = (agt.indexOf("opera 3") != -1 || agt.indexOf("opera/3") != -1);
    this.opera4 = (agt.indexOf("opera 4") != -1 || agt.indexOf("opera/4") != -1);
    this.opera5 = (agt.indexOf("opera 5") != -1 || agt.indexOf("opera/5") != -1);
    this.opera5up = (this.opera && !this.opera2 && !this.opera3 && !this.opera4);

    this.webtv = (agt.indexOf("webtv") != -1);

    this.TVNavigator = ((agt.indexOf("navio") != -1) || (agt.indexOf("navio_aoltv") != -1));
    this.AOLTV = this.TVNavigator;

    this.hotjava = (agt.indexOf("hotjava") != -1);
    this.hotjava3 = (this.hotjava && (this.major == 3));
    this.hotjava3up = (this.hotjava && (this.major >= 3));

}
// The following lines are Macintosh specific code that is not required for the I2I web site
// var is;
// var isIE3Mac = false;
// this section is designed specifically for IE3 for the Mac

// if ((navigator.appVersion.indexOf("Mac")!=-1) && (navigator.userAgent.indexOf("MSIE")!=-1) && 
// (parseInt(navigator.appVersion)==3))
//       isIE3Mac = true;
// else   is = new Is(); 


function BrowserAdjustments () {

// This function makes web page content adjustments to compensate for browser incompatabilities. It
// assumes the prior execution of the Is() browser sniff function. 

	var i2icss=document.styleSheets[0];
	var i2irules=i2icss.cssRules? i2icss.cssRules: i2icss.rules
	for (i=0; i<i2irules.length; i++) {
		if (i2irules[i].selectorText.toLowerCase() == ".search-query-iespacer") {
			var search_query_iespacer=i2irules[i] 
			break;
		}
	}

	if (ie == true) {
//		document.write("IE - The Height Before = " + search_query_iespacer.style.height + "<br>");
		search_query_iespacer.style.height="30px";
//		document.write("IE - The Height After =  " + search_query_iespacer.style.height + "<br>"); 
		}
	else {
//		document.write("FF - The Height Before = " + search_query_iespacer.style.height + "<br>");
		search_query_iespacer.style.height="1px";
//		document.write("FF - The Height After =  " + search_query_iespacer.style.height + "<br>");
		}
} 


function HomeSite () {

// This function dynamically generates an absolute link to the NFC Home Page
// and Home Page search site target.
//
// It first determines the correct domain for the home page based on the 
// domain of the calling page and the "hpprod" variable, as follows:
//
//      If the calling domain is "interstage2i", the domain hosting the 
//	home page is "interhomestage"
//
//	If the calling domain is "i2i.nfc.usda.gov" and the "hpprod" variable
//	is set to "eta", the domain hosting the home page is "interhome.nfc.usda.gov"
//
//	If the calling domain is "i2i.nfc.usda.gov" and the "hpprod" variable
//	is set to "iis", the domain hosting the home page is "www.nfc.usda.gov"
//
// The filename for the home page URL (typically "index.html" or "index-test.html") 
// is specified in the "hpfile" variable.
//
// The link specification in the calling page should be formatted as follows:
//
//	<a href="#" onClick="site();return false;" ...
//
// The function then determines the correct search site domain to use if the current
// page is a Home Page, as follows: 
//
//      If the Home Page domain is "interhomestage", the search site domain is 
//	interstage2i.
//
//      If the Home Page domain is "interhome.nfc.usda.gov", the search site domain  
//	is i2i.nfc.usda.gov.
//
//      If the Home Page domain is "www.nfc.usda.gov", the search site domain is  
//	i2i.nfc.usda.gov.
	 

	var hpprod = "iis"
	var hpfile = "index.html"
	var site = document.domain

	if (site == "interstage2i") {

		hpage = "http://interhomestage/" + hpfile
	}
		
	else if (site == "i2i.nfc.usda.gov") {

		if (hpprod == "eta") {
			hpage = "http://interhome.nfc.usda.gov/" + hpfile }
		else if (hpprod == "iis") {
		 	hpage = "http://www.nfc.usda.gov/" + hpfile }
		else {
		 	hpage = "http://interhome.nfc.usda.gov/" + hpfile }
	}

	else if (site == "interhomestage") {

		searchsite = "http://interstage2i/search?NS-query-pat=/text/i2i-query.pat"

	}
		
	else if (site == "interhome.nfc.usda.gov") {

		searchsite = "http://i2i.nfc.usda.gov/search?NS-query-pat=/text/i2i-query.pat"

	}

	else if (site == "www.nfc.usda.gov") {

		searchsite = "http://i2i.nfc.usda.gov/search?NS-query-pat=/text/i2i-query.pat"

	}

	else {

		searchsite = "http://i2i.nfc.usda.gov/search?NS-query-pat=/text/i2i-query.pat"
	}

}


function ssite () {

// This function is used inline in HTML code to select the appropriate search site domain as 
// determined by the HomeSite() function.  

	frames['queryframe'].location.href = searchsite
	
}


function site () {

// This function is used inline in HTML code to select the appropriate Home Page domain as 
// determined by the HomeSite() function.  

	top.location.href = hpage;
}


function csite (rellink) {

// This function dynamically converts Home Page relative links to absolute links
// that point to the appropriate (i.e., staging or production) content site.

// The link specification in the calling page should be formatted as follows:
//
//	<a href="#" onClick="csite("Relative URL"> );return false;" ...

	var site = document.domain
//	document.write(site + "<br>");
//	document.write(rellink + "<br>");
	

	if (site == "interhomestage") {

		cpage = "http://interstage2i/" + rellink }
		
	else if (site == "interhome.nfc.usda.gov") {

		cpage = "http://i2i.nfc.usda.gov/" + rellink }
		

	else {

		cpage = "http://i2i.nfc.usda.gov/" + rellink }

	location.href = cpage;

}


function CurrentMonth () {

// This function, which runs on page load as a function of doscripts(), identifies the current
// month and year and builds the filename for the calendar iframe.

CurrentDate=new Date()

	if (ie == true) {
		CurrentYear=CurrentDate.getYear().toString()
		}
	else {
		CurrentYear=(CurrentDate.getYear()+1900).toString()
		}

CurrentMonth=(CurrentDate.getMonth()+1).toString()

if (CurrentMonth.length == 1) {
	CurrentMonth= "0" + CurrentMonth
	}  

MonthFile="/Customer_Support/calendar/" + CurrentYear + CurrentMonth + ".html"
}


function CalendarFrame () {

// This function loads the current calendar document into the Customer_Support iframe based on
// the month and year calculated in by the currentmonth() function

frames['calFrame'].location.href = MonthFile

// frames['calFrame'].location.href = "/Customer_Support/calendar/200709.html"
}


function CalNext () {

// This function displays the next calendar month file based on the currently displayed
// month file. Note: This function does not identify the upper calendar page boundry as
// client side JavaScript does not support server file existance testing.

curMonthURL=location.href
curDate=curMonthURL.substring(curMonthURL.lastIndexOf("/")+1,curMonthURL.indexOf(".html"))
curCalMonth=parseInt(curDate.substr(4,2),10)
curCalYear=parseInt(curDate.substr(0,4),10)

if (curCalMonth == 12) {
	nextCalMonth = "01"
	nextCalYear = ++curCalYear }
	
else {
	
	nextCalMonth = ++curCalMonth
	nextCalYear = curCalYear.toString()
	nextCalMonth = nextCalMonth.toString() 
	
	if (nextCalMonth.length == 1) {
		nextCalMonth= "0" + nextCalMonth
	}  
}

nextMonth="/Customer_Support/calendar/" + nextCalYear+nextCalMonth + ".html"

// document.write(location.href.length)

location.href = nextMonth

}


function CalPrev () {

// This function displays the next calendar month file based on the currently displayed
// month file. Note: This function does not identify the upper calendar page boundry as
// client side JavaScript does not support server file existance testing.

curMonthURL=location.href
curDate=curMonthURL.substring(curMonthURL.lastIndexOf("/")+1,curMonthURL.indexOf(".html"))
curCalMonth=parseInt(curDate.substr(4,2),10)
curCalYear=parseInt(curDate.substr(0,4),10)

if (curCalMonth == 1) {
	prevCalMonth = "12"
	prevCalYear = --curCalYear }
	
else {
	
	prevCalMonth = --curCalMonth
	prevCalYear = curCalYear.toString()
	prevCalMonth = prevCalMonth.toString() 
	
	if (prevCalMonth.length == 1) {
		prevCalMonth= "0" + prevCalMonth
	}  
}

prevMonth="/Customer_Support/calendar/" + prevCalYear+prevCalMonth + ".html"

// document.write(location.href.length)

location.href = prevMonth

}
//------------- Event Calendar JS File ----------------------------------------------------------------------------------------------------------//

//currentmonth and currentyear are global variables to keep track of which month is currently displayed in the calendar.
var currentmonth, currentyear

function loadCalendar (monthtype) {
//This function populates tds in the calendar table. It is called by a page onload handler and by onclick handlers
//of "next" and "previous" tds in the calendar.
//The calendar is 2 html tables.
//The first table, "calheader" contains 3 rows of calendar header info.
//The first row is a literal header, "Events Calendar".
//The second row displays the current month and year, e.g., "December 2007", and "previous" and "next" links.
//The third row is a literal header for the days of the week, "S, M, T, W, T, F, S".
//The second table, "calweeks", contains the weeks of a month. It has 6 rows (A month has at least four weeks and may have up to 6 weeks).
//Each week row contains 7 tds, one for each day of the week. Each td is populated with the month day number.
//Optionally, if the date of a td has an entry in the nfcEvents array (defined in nfcevents.js), the month day number is
//formatted as a link to a page that describes the day's events.
//Week rows 5 and 6 are initially hidden; they are only displayed if there dates in those weeks.
//Optional input parameter monthtype indicates if the calendar is to be loaded with a month prior to the currently
//displayed month or with a month following the currently displayed month. There is no input parameter if this function is called by 
//the page onload handler. In that case, the calendar is populated for the current month.

    //caldate is a date object used to populate tds in the calendar. It is initialized to the first day of today's month.
    var caldate = new Date();
    caldate.setDate(1);
    //If there is an input parameter, reset caldate to the month prior or following the currently displayed month.
    if (monthtype == "previous") {
        caldate.setFullYear(currentyear)
        caldate.setMonth(currentmonth - 1);                
    } else if (monthtype == "next") {
        caldate.setFullYear(currentyear);
        caldate.setMonth(currentmonth + 1);
    //Else if the user is returning to this page, load the calendar with the month that was displayed on his last page visit.
    //Here's a web trick that will make sure that when/if the user returns to this page after having linked away, the calendar
    //will still display the month the user last selected. This is useful when the user wants to look at all of next month's
    //events: he advances the calendar by clicking on the "next" link, clicks on a day link in the calendar to go to the
    //event description page, and then clicks the back button to return to this page. When he gets back to this page, next month's
    //calendar is still displayed, i.e., he doesn't have to start over and advance the calendar again. This is accomplished by
    //using the window's "name" property as a global variable that maintains its value across visited pages.
    //If the current value of the window's name property contains "nfcdate:", then it was set by this page. In that case,
    //reset caldate to the year and month contained in the window's name property, e.g."nfcdate:200711".
    } else if (window.name.indexOf("nfcdate:") != -1) {
        caldate.setFullYear(window.name.substr(8,4));
        caldate.setMonth(window.name.substr(12,2)); 
    }
    
    //Update the global variables.
    currentyear = caldate.getFullYear();
    currentmonth = caldate.getMonth();
    //Update the window's name property with the values of the global variables.
    window.name = "nfcdate:" + currentyear + currentmonth
    
    //Update the month and year in calheader.
    //monthnames is reference array for translating month numbers to month names.
    var monthnames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    document.getElementById("caltitle").innerHTML = monthnames[currentmonth] + " " + currentyear;            
        
    //Initialize calweeks rows 5 and 6 to nondisplay status.
    document.getElementById("week5").style["display"] = "none";
    document.getElementById("week5").style["visibility"] = "hidden";
    document.getElementById("week6").style["display"] = "none";
    document.getElementById("week6").style["visibility"] = "hidden";

    //Populate the calendar.
    //caltds is a collection of the 42 tds in calweeks.
    var caltds = document.getElementById("calweeks").getElementsByTagName("td");
    var nfceventday, nfceventmonth, nfceventdate
    var relativepath
    //Loop through the 42 day tds in the calendar ...
    //calday is a loop control variable. It is also used to identify which day-of-the-week td is current.
    //It is also used as an index on the caltds collection.
    for (var calday = 0; calday < 42; calday++) {
        //If the value of calday refers to a day-of-the-week that matches the day-of-the-week of the current value of caldate,
        //populate the corresponding td, unless caldate has been incremented beyond currentmonth. 
        if ((caldate.getDay() == calday%7) && (caldate.getMonth() == currentmonth)) {
            //Populate the td with the date value from caldate. If there is an entry for the date in the nfcEvents array,
            //format the date value as a link to the page that describes the days events.
            //Format the value to be used as an array index into nfcEvents. Prepend a leading zero on the date or
            //month value if it is less than 10.
            nfceventmonth = (caldate.getMonth() < 9) ? "0" + (caldate.getMonth() + 1) : (caldate.getMonth() + 1);
            nfceventday = (caldate.getDate() < 10) ? "0" + caldate.getDate() : caldate.getDate();
            nfceventdate = nfceventmonth + "-" + nfceventday + "-" + caldate.getFullYear();
            if (nfcEvents[nfceventdate]) {   
                if (location.href.indexOf("Events") == -1) {
                    relativepath = "Events/";
                } else {
                    relativepath = "";
                }                     
                caltds[calday].innerHTML = "<div onClick=\"window.location=\'" + relativepath + nfcEvents[nfceventdate] + "\'\" style=\"height:21px; text-decoration:none; font-size:12px; font-weight:bold; color:#ffffdd; cursor:pointer;\">" + caldate.getDate() + "</div>"
                //tds with events have a light aqua background.
                caltds[calday].style["backgroundColor"] = "#ff0000";
            } else {                        
                caltds[calday].innerHTML = caldate.getDate();
                //tds without events have a light yellow background.
                caltds[calday].style["backgroundColor"] = "#ffffdd";
            }
            //Increment caldate.
            caldate.setDate(caldate.getDate() + 1);
            //If we have already populated 4 weeks/rows of tds, unhide calweeks row 5.
            if (calday == 28) {
                document.getElementById("week5").style["display"] = "";
                document.getElementById("week5").style["visibility"] = "visible";
            }
            //If we have already populated 5 weeks/rows of tds, unhide calweeks row 6.
            if (calday == 35) {
                document.getElementById("week6").style["display"] = "";
                document.getElementById("week6").style["visibility"] = "visible";
            }
        //Else the value of calday does not refer to a day-of-the-week that matches the day-of-the-week of the current value 
        //of caldate. This will be the case until we loop enough times to find the day-of-the-week that the month starts on
        //or we have gone past the end of the month. In either case, populate the corresponding td with a blank.
        } else {
            caltds[calday].innerHTML = "&nbsp;"
            //The background color of tds without dates is white.
            caltds[calday].style["backgroundColor"] = "#ffffff";
        }
    }                   
}

//End Production Scripts
