function ExtractFromString(FullString, SearchParameter, Delimiter) {
// This function will extract a value from a delimited text string 

/* Sample Calls
FullString = "?OpenDocument&lang=en&selecteddate=2006-10"  (in this case, FullString is a Query_String or location.search)
selDate = ExtractFromString(location.search , '&selecteddate=' , '&') returns "2006-10"
selLang = ExtractFromString(location.search , '&lang=' , '&') returns "en"
selYear = ExtractFromString(location.search , '&selecteddate=' , '-') returns "2006" (the year)
selMonth = ExtractFromString(location.search , '&selecteddate=' + selYear + '-' , '&') returns "10" (the month)  
*/

// Ensure the first and last values are delimited
FullString = Delimiter + FullString + Delimiter ;

// Check if the SearchParameter is contained within FullString
PosStart = FullString.indexOf(SearchParameter)

if (PosStart != -1) {
   PosStart =  PosStart + SearchParameter.length
   ReturnValue = FullString.substring(PosStart, FullString.indexOf(Delimiter, PosStart))

    if (ReturnValue != "") {return unescape(ReturnValue)}
       else {return ""}
   }
else {return ""}

}

function loadbody(theparameter,thevalue) {
/* This function will preselect today's date and month when nothing has been entered
or make the selected year, month, location, and interest (event type) also selected in the picklists.
e.g. QueryString = "?OpenDocument&lang=en&selecteddate=2006-10&location=PBC&interest=COMM" */

// Determine the language
var selLang = ExtractFromString(location.search , '&lang=' , '&');

/* AUTO PRINT POP-UP has been disabled
var printversion = ExtractFromString(location.search , '&printable=' , '&');
var PrintMessage;
if (printversion == "YES" ) {
   if (selLang == "en") {PrintMessage = "Best printed using landscape orientation";}
      else {PrintMessage = "FR - Best printed using landscape orientation";}
   alert(PrintMessage);
   window.print();
   return "";
   }
*/

// get the values by parsing the querystring
var selecteddate = ExtractFromString(location.search , '&selecteddate=' , '&');
var selectedyear = ExtractFromString(location.search , '&selecteddate=' , '-');
var selectedmonth = ExtractFromString(location.search , '&selecteddate=' + selectedyear + "-", '&');
var selectedlocation = ExtractFromString(location.search , '&location=' , '&');
var selectedinterest = ExtractFromString(location.search , '&interest=' , '&');

var TodaysDate = new Date( );

// If a year has not been chosen, use this year
if (selectedyear == "") {selectedyear = TodaysDate.getFullYear( )}

// If a month has not been chosen, use this month (note: getMonth() returns a number from 0 to 11)
if (selectedmonth == "") {
   selectedmonth = TodaysDate.getMonth( ) + 1;
   if (TodaysDate.getMonth( ) + 1 < 10) { selectedmonth = "0" + selectedmonth.toString();}
}

var NextOptionValue;

// Make the current YEAR selected in the picklist
for (var x=0; x < window.document.forms['calendarform'].selYear.options.length ; x++) {
   NextOptionValue = window.document.forms['calendarform'].selYear.options[x].value ;

   if (NextOptionValue == selectedyear) {
      window.document.forms['calendarform'].selYear.options[x].selected = true;
      x = window.document.forms['calendarform'].selYear.options.length;
      }
}

// Make the current MONTH selected in the picklist
for (x=0; x < window.document.forms['calendarform'].selMonth.options.length ; x++) {
   NextOptionValue = window.document.forms['calendarform'].selMonth.options[x].value ;

   if (NextOptionValue == selectedmonth) {
      window.document.forms['calendarform'].selMonth.options[x].selected = true;
      x = window.document.forms['calendarform'].selMonth.options.length;
      }
}

// Make the current LOCATION selected in the picklist
for (x=0; x < window.document.forms['calendarform'].selLocation.options.length ; x++) {
   NextOptionValue = window.document.forms['calendarform'].selLocation.options[x].value ;

   if (NextOptionValue == selectedlocation) {
      window.document.forms['calendarform'].selLocation.options[x].selected = true;
      x = window.document.forms['calendarform'].selLocation.options.length;
      }
}

// Make the current INTEREST selected in the picklist
for (x=0; x < window.document.forms['calendarform'].selInterest.options.length ; x++) {
   NextOptionValue = window.document.forms['calendarform'].selInterest.options[x].value ;

   if (NextOptionValue == selectedinterest) {
      window.document.forms['calendarform'].selInterest.options[x].selected = true;
      x = window.document.forms['calendarform'].selInterest.options.length;
      }
}

}

function GetYearMonth(MonthsToAdjust) {
// Get the current year and month that is displayed from the querystring
var selectedyear = ExtractFromString(location.search , '&selecteddate=' , '-');
var selectedmonth = ExtractFromString(location.search , '&selecteddate=' + selectedyear + "-", '&');

var TodaysDate = new Date( );

// If a year has not been chosen, use this year
if (selectedyear == "") {selectedyear = TodaysDate.getFullYear( )}

// If a month has not been chosen, use this month (note: getMonth() returns a number from 0 to 11)
if (selectedmonth == "") {
   selectedmonth = TodaysDate.getMonth( ) + 1;
   if (TodaysDate.getMonth( ) + 1 < 10) { selectedmonth = "0" + selectedmonth.toString();}
}

// Remove leading "0" character before applying parseInt
if (selectedmonth < "10")
 {selectedmonth = selectedmonth.substring(1)}

var newmonth = eval(parseInt(selectedmonth) + parseInt(MonthsToAdjust));
var newyear = selectedyear;

if (newmonth == 0) {
   newmonth = "12";
   newyear = eval(parseInt(selectedyear) - 1)
   }
else if (newmonth == 13) {
   newmonth = "01";
   newyear = eval(parseInt(selectedyear)+ 1)
   }
else if (newmonth < '10') {
   newmonth = "0" + newmonth.toString();
   }

// Return a date in the format YYYY-MM (e.g. 2002-04)
returnVal = newyear.toString() + "-" + newmonth.toString() ;

return (returnVal)
}

function reloadpage(param)
{
// Param will be appended to the query string, e.g. "printable=yes"

Selected_Lang = window.document.forms['calendarform'].selLang.value;
Selected_Year = window.document.forms['calendarform'].selYear.value;
Selected_Month = window.document.forms['calendarform'].selMonth.value;
Selected_Location = window.document.forms['calendarform'].selLocation.value;
Selected_Interest = window.document.forms['calendarform'].selInterest.value;

// alert ("Selected_Lang=" + Selected_Lang + "\n"+ "Selected_Year=" + Selected_Year + "\n"+ "Selected_Month=" + Selected_Month + "\n" + "Selected_Location=" + Selected_Location + "\n"+ "Selected_Interest=" + Selected_Interest + "\n"+ "Param=" + Param);

// e.g. /acp/apps/calendar.nsf/vWeb/calendar?OpenDocument&lang=en&selecteddate=2006-08&location=PAB&interest=COMM

QueryString = "&lang=" + Selected_Lang + "&selecteddate=" + Selected_Year + "-" + Selected_Month + "&location=" + Selected_Location + "&interest=" + Selected_Interest;

if (param > "") {QueryString = QueryString + "&" + param; }

URLToOpen = "./calendar?OpenDocument" + QueryString;

window.document.location.href=URLToOpen;
}
