/* MVG web site scripts (gr, 11-09) */
function openWindow(URL,w,h) {
 window.open(URL,"vidwin","toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+w+",height="+h);
}

function clrField(formName,varName) {
 tmpFld = eval('document.'+formName+'.'+varName);
 tmpFld.value = '';
}

var digits = '0123456789';
function checkDigits(objName) { /* allow only numbers in a field */
 var strPass = objName.value;
 var strLength = strPass.length;
 var lchar = objName.value.charAt((strLength)-1);
 if(digits.indexOf(lchar) == -1) {
  var tst = objName.value.substring(0,(strLength)-1);
  objName.value = tst;
 }
}

function unhide(divID) {
    var item = document.getElementById(divID);
    if (item) {
        item.className=(item.className=='hidden')?'unhidden':'hidden';
    }
}

function mypopup(link) {
 mywindow = window.open(link,"mywindow","location=1,status=1,scrollbars=1,width=500,height=600");
 mywindow.moveTo(0,0);
}

/* handle document arguments (peter bromberg) ==> */
function PageQuery(q) {
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; }	
}
function queryString(key){
var page = new PageQuery(window.location.search); 
return unescape(page.getValue(key)); 
}
function displayItem(key){
if(queryString(key)=='false') 
{
document.write("you didn't enter a ?name=value querystring item.");
}else{
document.write(queryString(key)+"<BR>");
}
}
/* <== */

function getProfile(srch){
 window.location ="http://www.maumeevalleygrowers.com/showprofiles.htm?srchterm="+srch
}

/* find/display grower profile info based on input search term */
function findGrower(srch) {
 if (queryString(srch) == 'false') { // search term not given || not touched
   document.write('<p>Please enter your search terms in the box beneath "MVG Info Search", then click "Search".');
   document.write(' Click the [Help] link for more information.</p>');
   setStyleById("mvgsrch","backgroundColor","#ffff00");
   document.mvgsrch.searchterm.focus();
   return true;
 }
 var regex = new RegExp(queryString(srch),"gi"); // build regex from search term - global, case-insensitive search
 setStyleById("mvgsrch","backgroundColor","#ffffff");
 if (queryString(srch) == '') {
   document.write('<p><em>Search results for "*":</em></p>');
 } else { 
   document.write('<p><em>Search results for "'+queryString(srch)+'":</em></p>');
 }
 var foundOne = 0;
 for (i = 0; i < growers.profiles.length; i++) { // search the offerings
  var found = 0;
  if (growers.profiles[i].plants.search(regex) != -1) { 
    found=1; growers.profiles[i].plants = '<strong>&gt;&gt;</strong>'+growers.profiles[i].plants;
  }
  if (growers.profiles[i].seasonal.search(regex) != -1) {
    found=1; growers.profiles[i].seasonal = '<strong>&gt;&gt;</strong>'+growers.profiles[i].seasonal;
  } 
  if (growers.profiles[i].hardgoods.search(regex) != -1) {
   found=1; growers.profiles[i].hardgoods = '<strong>&gt;&gt;</strong>'+growers.profiles[i].hardgoods;
  } 
  if (growers.profiles[i].holidays.search(regex) != -1) {
   found=1; growers.profiles[i].holidays = '<strong>&gt;&gt;</strong>'+growers.profiles[i].holidays;
  } 
  if (growers.profiles[i].additional.search(regex) != -1) {
   found=1; growers.profiles[i].additional = '<strong>&gt;&gt;</strong>'+growers.profiles[i].additional;
  } 
  if (growers.profiles[i].unique.search(regex) != -1) {
   found=1; growers.profiles[i].unique = '<strong>&gt;&gt;</strong>'+growers.profiles[i].unique;
  } 
  if (found == 1) {
   foundOne = 1;
   document.write('<h2>'+growers.profiles[i].business+'</h2><dl>');
   document.write('<dt>Address</dt><dd>'+growers.profiles[i].address+'</dd>');
   document.write('<dt>Phone</dt><dd>'+growers.profiles[i].phone+'</dd>');
   document.write('<dt>MVG Web Page</dt><dd><a href="http://'+growers.profiles[i].web+'">'+growers.profiles[i].web+'</a></dd>');
   document.write('<dt>Months of Operation</dt><dd>'+growers.profiles[i].months+'</dd>');
   document.write('<dt>Days/Hours Open</dt><dd>'+growers.profiles[i].dayhours+'</dd>');
   document.write('<dt>Plant Materials</dt><dd>'+growers.profiles[i].plants+'</dd>');
   document.write('<dt>Seasonal Items</dt><dd>'+growers.profiles[i].seasonal+'</dd>');
   document.write('<dt>Hard Goods</dt><dd>'+growers.profiles[i].hardgoods+'</dd>');
   document.write('<dt>Holiday products</dt><dd>'+growers.profiles[i].holidays+'</dd>');
   document.write('<dt>Additional Services</dt><dd>'+growers.profiles[i].additional+'</dd>');
   document.write('<dt>Unique Products/Services/Events</dt><dd>'+growers.profiles[i].unique+'</dd></dl>');
   document.write('<p style="text-align:center;"><span style="letter-spacing:2em;font-size:150%;color:green">&bull;&bull;&bull;</span></p>');
  }
 }
 if (foundOne == 0) { // search term yielded nothing
    document.write('<p>Nothing was found for the above search term(s). Please try again or click the [Help] link.</p>');
    setStyleById("mvgsrch","backgroundColor","#ffff00");
    document.mvgsrch.searchterm.focus();
 }
 return(true);
}

//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//:::                                                                         :::
//:::  This routine calculates the distance between two points (given the     :::
//:::  latitude/longitude of those points). It is being used to calculate     :::
//:::  the distance between two ZIP Codes or Postal Codes using our           :::
//:::  ZIPCodeWorld(TM) and PostalCodeWorld(TM) products.                     :::
//:::                                                                         :::
//:::  Definitions:                                                           :::
//:::    South latitudes are negative, east longitudes are positive           :::
//:::                                                                         :::
//:::  Passed to function:                                                    :::
//:::    lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees)  :::
//:::    lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees)  :::
//:::    unit = the unit you desire for results                               :::
//:::           where: 'M' is statute miles                                   :::
//:::                  'K' is kilometers (default)                            :::
//:::                  'N' is nautical miles                                  :::
//:::                                                                         :::
//:::  United States ZIP Code/ Canadian Postal Code databases with latitude   :::
//:::  & longitude are available at http://www.zipcodeworld.com               :::
//:::                                                                         :::
//:::  For enquiries, please contact sales@zipcodeworld.com                   :::
//:::                                                                         :::
//:::  Official Web site: http://www.zipcodeworld.com                         :::
//:::                                                                         :::
//:::  Hexa Software Development Center © All Rights Reserved 2004            :::
//:::                                                                         :::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

function distance(lat1, lon1, lat2, lon2, unit) {
	var radlat1 = Math.PI * lat1/180
	var radlat2 = Math.PI * lat2/180
	var radlon1 = Math.PI * lon1/180
	var radlon2 = Math.PI * lon2/180
	var theta = lon1-lon2
	var radtheta = Math.PI * theta/180
	var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
	dist = Math.acos(dist)
	dist = dist * 180/Math.PI
	dist = dist * 60 * 1.1515
	if (unit=="K") { dist = dist * 1.609344 }
	if (unit=="N") { dist = dist * 0.8684 }
	return dist
}

function getMVG(zip,miles){
 window.location ="http://www.maumeevalleygrowers.com/showmvg.htm?srchzip="+zip+"&srchmiles="+miles
}

function growerZip(zip,miles) {
 if (queryString(zip) == '' || queryString(miles) == '' || queryString(zip) == 'false' || queryString(miles) == 'false') {
   // search term not given || not touched
   document.write('<p>Please enter your zip code and a number of miles in the boxes beneath "MVG Grower Near You", then click "Find Growers".');
   document.write(' Click the [Help] link for more information.</p>');
   setStyleById("mvgzip","backgroundColor","#ffff00");
   document.mvgzip.searchzip.focus();
   return true;
 }
 var startlat = '';
 var startlon = '';
 var startcity = '';
 var startstate = '';
 for (i=0; i<=nZips; i++) { // get local zip code's lat & lon
  if (area.zipdata[i].zip == queryString(zip)) {
    startlat = area.zipdata[i].lat;
    startlon = area.zipdata[i].lon;
    startcity = area.zipdata[i].city;
    startstate = area.zipdata[i].state;
    break;
  }
 }
 if (startlat == '') { // no lat|lon values for this zip code
   document.write('<p>The zip code you entered ('+queryString(zip)+') is not in our ');
   document.write('database. Please try again or click the [Help] link.</p>');
   setStyleById("mvgzip","backgroundColor","#ffff00");
   document.mvgzip.searchzip.focus();
   return true;
 }
 document.write('<h2>Within '+queryString(miles)+' miles of Zip Code '+queryString(zip)+' ('+startcity+', '+startstate+')</h2>');
 var found = 0;
 setStyleById("mvgzip","backgroundColor","#ffffff");
 for (i=0; i<growers.zipdata.length; i++) { // check starting lat/lon against those of the growers to find those within the distance given 
  var d = distance(startlat,startlon,growers.zipdata[i].lat,growers.zipdata[i].lon,"M");
  if (d <= queryString(miles)) { // gotcha
     found = 1;
     document.write('<p><strong>');
     document.write((d.toFixed(1) < 1.0 ? '&lt;1' : d.toFixed(1)));
     document.write(' miles ==&gt; <em>'+growers.zipdata[i].business+'</em></strong><br />&nbsp;&nbsp;&nbsp;');
     document.write(growers.zipdata[i].address+', '+growers.zipdata[i].csz+'<br />&nbsp;&nbsp;&nbsp;');
     document.write('Web: <a href="'+growers.zipdata[i].url+'">'+growers.zipdata[i].url+'</a></p>');
  }
 }
 if (found == 0) { // none found within the distance given for that zip
   document.write('<p>No growers were found within '+queryString(miles)+' miles of zip code '+queryString(zip)+'.</p>');
 }
 return true;
}

// setStyleById: given an element id, style property and 
// value, apply the style. (devworld.apple.com)
// args:
//  i - element id
//  p - property
//  v - value
//
function setStyleById(i, p, v) {
	var n = document.getElementById(i);
	n.style[p] = v;
}

function showCal() {
// change next line for the desired starting directory
 var startPath = "http://www.maumeevalleygrowers.com/img/calendar/MVGCal12/";
 var pyear = "2012"; // change for desired year
 var months = new Array(13);
 months[1] = "jan"; months[2] = "feb";
 months[3] = "mar"; months[4] = "apr";
 months[5] = "may"; months[6] = "jun";
 months[7] = "jul"; months[8] = "aug";
 months[9] = "sep"; months[10] = "oct";
 months[11] = "nov"; months[12] = "dec";
 var dateObj = new Date();
 var pmonth = months[dateObj.getMonth() + 1];
 document.write('<img src="'+startPath+'/thumbs/'+pmonth+'.jpg" alt="MVG wallpaper" ');
 document.write('title="MVG '+pyear+' Calendar: '+pmonth.charAt(0).toUpperCase()+pmonth.substr(1)+'" />');
 document.write('<br /> '+'<a class="nava" target="_blank" href="'+startPath+pmonth+'.jpg">'+pmonth.charAt(0).toUpperCase()+pmonth.substr(1)+' '+pyear+'</a></p>');
// document.write('<span style="font-size:10pt;">');
// document.write('<a class="nava" target="_blank" href="'+startPath+'/1600/'+pmonth+'.jpg">Largest</a> | ');
// document.write('<a class="nava" target="_blank" href="'+startPath+'/1200/'+pmonth+'.jpg">Large</a> | ');
// document.write('<a class="nava" target="_blank" href="'+startPath+'/1024/'+pmonth+'.jpg">Medium</a> | ');
// document.write('<a class="nava" target="_blank" href="'+startPath+'/800/'+pmonth+'.jpg">Small</a>');
// document.write('</span></p>');
// document.write('<p>[<a class="nava" href="http://www.maumeevalleygrowers.com/documents/MVGCalendar'+pyear+'.pdf">Download the '+pyear+' MVG Calendar</a>]</p>');
}

/* random image rotation
   ( based on http://www.quirksmode.org/js/rotate.html) */
function randImages()
{
var pictures = new Array(
 'rotation/cal01.jpg',
 'rotation/cal02.jpg',
 'rotation/cal03.jpg',
 'rotation/cal04.jpg',
 'rotation/cal05.jpg',
 'rotation/cal06.jpg',
 'rotation/cal07.jpg',
 'rotation/cal08.jpg',
 'rotation/cal10.jpg',
 'rotation/cal11.jpg',
 'rotation/cal12.jpg',
 'rotation/cal13.jpg');

var old = 0;
var current = 0;

if (!document.images) return
while (current == old)
{
 current = Math.floor(Math.random()*pictures.length);
}
old = current;
document.write('<img src="http://www.maumeevalleygrowers.com/img/calendar/MVGCal12/'+pictures[current]+'" alt="MVG 2012 Calendar Images" title="MVG 2012 Calendar Images" />');
}

