function indexInArray(arr,val){
	for(var i=0;i<=arr.length;i++){
		if(arr[i]==val){ 
		return i;
		}
	}
}
function changeBranchPane(index){
	results=document.getElementById('branchpane');
	grabFile("locationpane.php?branch="+index+"&ajax=1",results);
	//stabs = document.getElementsByClassName("snavtabselect");
	//stabs.setAttribute("class", "snavtab");
	//stabs.setAttribute("className", "snavtab");
	
	var par = document.getElementById('branchnav');
	if(par.firstChild) { // check for children
		var oChild = par.firstChild;
		while(oChild) { // run over them
		  if(oChild.nodeType==1) { // element
			// oChild is a first level child of oDiv
			oChild.setAttribute("class", "snavtab");
			oChild.setAttribute("className", "snavtab");
		  }
		  oChild = oChild.nextSibling;
		}
	}
	document.getElementById(index).setAttribute("class", "snavtabselect");
	document.getElementById(index).setAttribute("className", "snavtabselect");
}
function displayFAQ(index,parent){
	var par = document.getElementById(parent);
	if(par.firstChild) { // check for children
		var oChild = par.firstChild;
		while(oChild) { // run over them
			if(oChild.firstChild) { // check for children
				var sChild = oChild.firstChild;
				while(sChild) {
					if(sChild.firstChild) {
						var tChild = sChild.firstChild;
						while(tChild) {
							if(tChild.nodeType==1) {
								tChild.setAttribute("class", "answer hiddenRow");
								tChild.setAttribute("className", "hiddenRow");
							}
							tChild = tChild.nextSibling;
						}
					}
					sChild = sChild.nextSibling;
				}
			}
			oChild = oChild.nextSibling;
		}
	}
	document.getElementById(index).setAttribute("class", "answer");
	document.getElementById(index).setAttribute("className", "answer");
}
function changeSlide(id,slideContainer,slideCaption,slideNav){
	try{
		//slide-nav
		var par = document.getElementById(slideNav);
		if(par.firstChild) { // check for children
			var oChild = par.firstChild;
			while(oChild) { // run over them
			  if(oChild.nodeType==1) { // element
				// oChild is a first level child of oDiv
				oChild.setAttribute("class", "option");
				oChild.setAttribute("className", "option");
			  }
			  oChild = oChild.nextSibling;
			}
		}
		document.getElementById('option-'+id).setAttribute("class", "selection");
		document.getElementById('option-'+id).setAttribute("className", "selection");
	}
	catch(err){
	}
	try{
		//slide-caption
		var par = document.getElementById(slideCaption);
		if(par.firstChild) { // check for children
			var oChild = par.firstChild;
			while(oChild) { // run over them
			  if(oChild.nodeType==1) { // element
				// oChild is a first level child of oDiv
				oChild.setAttribute("class", "hiddenRow");
				oChild.setAttribute("className", "hiddenRow");
			  }
			  oChild = oChild.nextSibling;
			}
		}
		document.getElementById('caption-'+id).setAttribute("class", "null");
		document.getElementById('caption-'+id).setAttribute("className", "null");
	}
	catch(err){
	}
	var par = document.getElementById(slideContainer);
	if(par.firstChild) { // check for children
		var oChild = par.firstChild;
		while(oChild) { // run over them
		  if(oChild.nodeType==1) { // element
			// oChild is a first level child of oDiv
			oChild.setAttribute("class", "slide hiddenRow");
			oChild.setAttribute("className", "slide hiddenRow");
		  }
		  oChild = oChild.nextSibling;
		}
	}
	document.getElementById('slide-'+id).setAttribute("class", "slide");
	document.getElementById('slide-'+id).setAttribute("className", "slide");
}
var timerId;
function restartRotate(slideContainer,slideNav,delay){
	if(delay===undefined){
		delay=4500;
	}
	clearTimeout(timerId); 
 	var timerId = setTimeout(function(){runSlideshow('0',slideContainer,slideNav,delay,imgCount)},delay);
 }
function runSlideshow(imgNo,slideContainer,slideCaption,slideNav,delay,imgCount) { 
	clearTimeout(timerId);
	if(delay===undefined){
		delay=4500;
	}
	if(imgCount===undefined){
		imgCount=0;
		var par = document.getElementById(slideContainer);
		if(par.firstChild) { // check for children
			var oChild = par.firstChild;
			while(oChild) { // run over them
			  if(oChild.nodeType==1) { // element
				imgCount++;
			  }
			  oChild = oChild.nextSibling;
			}
		}
	}
	if(imgNo>=imgCount){
		imgNo=0;
	}
	changeSlide(imgNo,slideContainer,slideCaption,slideNav);
	imgNo++;
	timerId = setTimeout(function(){runSlideshow(imgNo,slideContainer,slideCaption,slideNav,delay,imgCount)},delay);
}
function changeTab(TABNUM,TABSET,ATABSTYLE,PANESET,PANESTYE){
	//Tab ids must follow the form TABSET+TABNUM where TABNUM is a number denoting the Tab and Pane
	//Pane ids must then follow the form PANESET+TABNUM
	//ATABSTYLE must be the class to be set as that of the active tab
	//PANESTYLE must be the style of the active pane
	//All other panes will be set to a hidden class.
	//document.getElementById(TABSET+TABNUM)	
}
function changeCompanyPane(index){
	//Array of branch info boxes
	var infobox = new Array()
   infobox[0] = "infocompany"; 
   infobox[1] = "infoourmission";
   infobox[2] = "infopress";
   
	for (x in infobox){
   document.getElementById(infobox[x]).className="hiddenRow";//close all
   document.getElementById(infobox[x].substr(4)).className="snavtab";//deselect tab
	}
   		document.getElementById(index).className='visibleRownull'; //open index
		document.getElementById(index.substr(4)).className='snavtabselect';//select tab
}
function changeMaintenancePane(index){
	//Array of branch info boxes
	var infobox = new Array()
   infobox[0] = "infoweekly"; 
   infobox[1] = "infomethods"; 
   infobox[2] = "infoorganic";
   infobox[3] = "infonursery";
   infobox[4] = "infoaccessories";
   infobox[5] = "infoharvest";
   
	for (x in infobox){
   document.getElementById(infobox[x]).className="hiddenRow";//close all
   document.getElementById(infobox[x].substr(4)).className="snav";//deselect tab
   document.getElementById('mpic'+x).className="hiddenRow";//close all pics
	}
	i=indexInArray(infobox,index); //find position of index in infobox array
   		document.getElementById(index).className='visibleRownull'; //open index
		document.getElementById('mpic'+i).className= 'mpic roundpic visibleRownull'; //show picture that matches index
		document.getElementById(index.substr(4)).className='snavselect';//select tab
}
function changeInstallationPane(index){
	//Array of branch info boxes
	var infobox = new Array()
	infobox[0] = "infogardengallery"; 
	infobox[1] = "infoboxframe"; 
	infobox[2] = "infogopherwire"; 
	infobox[3] = "infosoil";
	infobox[4] = "infoirrigation";
	infobox[5] = "infotimer";
	infobox[6] = "infolabor";
   
	for (x in infobox){
   document.getElementById(infobox[x]).className="hiddenRow";//close all
   document.getElementById(infobox[x].substr(4)).className="snav";//deselect tab
   document.getElementById('mpic'+x).className="hiddenRow";//close all pics
	}
	i=indexInArray(infobox,index); //find position of index in infobox array
   		document.getElementById(index).className='visibleRownull'; //open index
		document.getElementById('mpic'+i).className= 'mpic roundpic visibleRownull'; //show picture that matches index
		document.getElementById(index.substr(4)).className='snavselect';//select tab
}

//ExpandCollapse
function checkHide(FORM,HID,NO) {
	if (document.forms[FORM].elements[HID].checked==true) {
		document.getElementById(HID).className='visibleRow'+NO;
	} else {
		document.getElementById(HID).className='hiddenRow';
	}
}
function showHide(HID,IMG,NO) {
	if (document.getElementById(IMG).src.indexOf('plus') != -1) {
		document.getElementById(IMG).src='images/minus.png';
		document.getElementById(HID).className='visibleRow'+NO;
	} else {
		document.getElementById(IMG).src='images/plus.png';
		document.getElementById(HID).className='hiddenRow';
	}
}
function showHide2(HID,IMG,NO) {
var expands=["exp1","exp2","exp3","exp4","exp5"];
var eximgs=["eximg1","eximg2","eximg3","eximg4","eximg5"];
for (x in expands){
	document.getElementById(expands[x]).className='hiddenRow';
	document.getElementById('b'+expands[x]).className='why roundpic';}
for (x in eximgs){
	document.getElementById(eximgs[x]).src='images/plus.png';}
	document.getElementById(HID).className='visibleRow'+NO;
	document.getElementById('b'+HID).className='why bexp roundpic';
	document.getElementById(IMG).src='images/minus.png';
}
function quickHide(HID,NO) {
	if (document.getElementById(HID).className=='hiddenRow'){
		document.getElementById(HID).className='visibleRow'+NO;}
		else {document.getElementById(HID).className="hiddenRow";}
}
function referHide(FUNC) {
	if(FUNC=='add'){
		if (document.getElementById('refer2').className=='hiddenRow') {
			document.getElementById('refer2').className='visibleRow1';
			document.getElementById('cancelrefer').className='visibleRownull';}
			else{
				if (document.getElementById('refer3').className=='hiddenRow') {
					document.getElementById('refer3').className='visibleRow1';
					document.getElementById('addrefer').className='hiddenRow';}
					}
				}
	else{
		if (document.getElementById('refer3').className=='visibleRow1') {
			document.getElementById('refer3').className='hiddenRow';
			document.TB.friend3.value="";
			document.TB.email3.value="";
			document.getElementById('addrefer').className='visibleRownull';}
		else{
			if (document.getElementById('refer2').className=='visibleRow1') {
				document.getElementById('refer2').className='hiddenRow';
				document.TB.friend2.value="";
				document.TB.email2.value="";
				document.getElementById('cancelrefer').className='hiddenRow';}
				}
		}
}
function showHide4(HID,IMG,NO) {
	if (document.getElementById(HID).className=='hiddenRow'){
		document.getElementById(HID).className='visibleText';
		if (document.getElementById(IMG).src.indexOf('plus') != -1) {document.getElementById(HID).innerHTML = "Expand";}
		else {if(NO==0){
			document.getElementById(HID).innerHTML = "";}
			else{document.getElementById(HID).innerHTML = "Collapse";}}
	} else {
		document.getElementById(HID).className='hiddenRow';
	}
}
function navxpander(HID,HIDCLASS,NAV,NAVCLASS){
	if(document.getElementById(HID).className==HIDCLASS){
		document.getElementById(HID).className="hiddenRow";
	}
	else{
		document.getElementById(HID).className=HIDCLASS;
	}
	if(document.getElementById(NAV).className==NAVCLASS){
		document.getElementById(NAV).className="";
	}
	else{
		document.getElementById(NAV).className=NAVCLASS;
	}
}
function expandPost(id){
	preview = 'preview'+id;
	fullentry = 'fullentry'+id;
	expandpost = 'expandpost'+id;
	hidepost = 'hidepost'+id;
	previewState = document.getElementById(preview).getAttribute('class');
	fullentryState = document.getElementById(fullentry).getAttribute('class');
	if(fullentryState=='hiddenRow'){
		document.getElementById(preview).className = 'hiddenRow';
		document.getElementById(fullentry).className = 'fullentry';
		document.getElementById(expandpost).className = 'hiddenRow';
		document.getElementById(hidepost).className = 'hidepost';
	}
	else{
		document.getElementById(preview).className = 'preview';
		document.getElementById(fullentry).className = 'hiddenRow';
		document.getElementById(expandpost).className = 'expandpost';
		document.getElementById(hidepost).className = 'hiddenRow';
	}
}
function expandlock(obj,button,objclass,setClass,activeclass){
	//TODO: IE doesn't recognize class, change for IE
	objState = document.getElementById(obj).className;
	if(objState=='hiddenRow'){
		document.getElementById(obj).className = objclass;
		document.getElementById(button).className = activeclass;
	}
	else{
		document.getElementById(obj).className = 'hiddenRow';
		document.getElementById(button).className = setClass;
	}
}
//Formval
function validate_required(field,alerttxt)
{
with (field)
  {
  if (value==null||value=="")
    {
    alert(alerttxt);return false;
    }
  else
    {
    return true;
    }
  }
}
function validate_email(field,alerttxt)
{
with (field)
  {
	if (value==null||value=="")
    {alert(alerttxt);return false;}
  else
    {
	apos=value.indexOf("@");
	dotpos=value.lastIndexOf(".");
	lastpos=value.length;
  if ( apos < 1 || (dotpos-apos) < 2 || (lastpos-1) == dotpos)
    {alert(alerttxt);return false;}
  else {return true;}
  }
    }
}
function validate_quickcontact(thisform){
		with (thisform){
		  if (validate_required(email,"You must submit a valid email address.")==false){
			  email.focus();
			  return false;
			  }  
		  if (validate_required(name,"You must submit your name.")==false){
			  name.focus();return false;
		  }
		  if (validate_email(email,"Not a valid e-mail address.")==false){
				email.focus();return false;
			}
		  if (document.TB.refer.checked==true){ 
			if ((document.TB.email1.value.length!==0)){
				if (validate_email(email1,"You must enter a valid email address for your friend.")==false){
				email1.focus();return false;
				}
			}
		  }
		
			if ((document.TB.email2.value.length!=0)){
				if (validate_email(email2,"You must enter a valid email address for your friend.")==false){
					email2.focus();
					return false;
				}
			}
			if ((document.TB.email3.value.length!=0)){
				if (validate_email(email3,"You must enter a valid email address for your friend.")==false){
				email3.focus();
				return false;
				}
			}
			if (document.TB.yardsurvey.checked==true){
				if (validate_required(address,"You must enter your address.")==false){
					address.focus();
					return false;
					}
				if (validate_required(ZIP,"You must enter the name of your city.")==false){
					ZIP.focus();
					return false;
					}
				if (validate_required(ZIP,"You must submit a valid ZIP code.")==false){
					ZIP.focus();
					return false;
					}
				if (validate_required(phone,"You must enter a ten digit phone number.")==false){
					address.focus();
					return false;
					}
			}
		}
}
function validate_commentform(thisform){
	with (thisform){
			if (validate_required(name,"You must submit your name.")==false){
				name.focus();
				return false;
				}
		  if (validate_required(comment,"You can't submit a blank comment!")==false){
			  comment.focus();
			  return false;
			  }
			  return true;
		}
}
function validate_field(field,thisval){
	if(thisval == 'req') {
		if(field.value == null || field.value == '') {
			field.setAttribute('class', 'invalid');
		} else {
			field.setAttribute('class', 'hundred');
		}
	}
	if(thisval.indexOf('char')!=-1) {
		var characters = thisval.substr(4);	
		if(field.value.length != characters) {
			field.setAttribute('class', 'invalid');
		} else {
			field.setAttribute('class', 'hundred');
		}
	}
	if(thisval == 'email') {
		apos = field.value.indexOf('@');
		dotpos = field.value.lastIndexOf('.');
		lastpos = field.value.length;
		if(field.value == null || field.value == '' || apos < 1 || (lastpos-1) == dotpos || (dotpos-apos) < 2) {
			field.setAttribute('class', 'invalid');
		} else {
			field.setAttribute('class', 'hundred');
		}
		
	}	
}
function numbersonly(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}

//Blog
function expandPost(id){
	preview = 'preview'+id;
	fullentry = 'fullentry'+id;
	expandpost = 'expandpost'+id;
	hidepost = 'hidepost'+id;
	previewState = document.getElementById(preview).getAttribute('class');
	fullentryState = document.getElementById(fullentry).getAttribute('class');
	commentexpander = 'commentexpander'+id;
	commenttab = 'commenttab'+id;
	postend = 'postend'+id;
	if(fullentryState=='hiddenRow'){
		document.getElementById(preview).className = 'hiddenRow';
		document.getElementById(fullentry).className = 'fullentry';
		document.getElementById(expandpost).className = 'hiddenRow';
		document.getElementById(hidepost).className = 'hidepost';
		document.getElementById(commentexpander).className = 'visibleRow';
		document.getElementById(commenttab).className = 'activepostendlink';
		document.getElementById(postend).className = 'activepostend';
	}
	else{
		document.getElementById(preview).className = 'preview';
		document.getElementById(fullentry).className = 'hiddenRow';
		document.getElementById(expandpost).className = 'expandpost';
		document.getElementById(hidepost).className = 'hiddenRow';
		document.getElementById(commentexpander).className = 'hiddenRow';
		document.getElementById(commenttab).className = 'postendlink';
		document.getElementById(postend).className = 'postend';
	}
}
function expandcomments(id){
	commentexpander = 'commentexpander'+id;
	commenttab = 'commenttab'+id;
	postend = 'postend'+id;
	commentexpanderState = document.getElementById(commentexpander).getAttribute('class');
	if(commentexpanderState=='hiddenRow'){
		document.getElementById(commentexpander).className = 'visibleRow';
		document.getElementById(commenttab).className = 'activepostendlink';
		document.getElementById(postend).className = 'activepostend';
	}
	else{
		document.getElementById(commentexpander).className = 'hiddenRow';
		document.getElementById(commenttab).className = 'postendlink';
		document.getElementById(postend).className = 'postend';
	}
}
function postcomment(thisform,entryid){
		thisform.submit_comment.disabled=true;
		thisform.submit_comment.setAttribute('class', 'posting');
		if(validate_commentform(thisform)){
			results = document.getElementById("commentexpander"+entryid);
			 var poststr = "entryid=" + encodeURIComponent(thisform.entryid.value ) +
			 		"&name=" + encodeURIComponent( thisform.name.value ) +
                    "&comment=" + encodeURIComponent( thisform.comment.value )+
					"&submit_comment=" + encodeURIComponent( thisform.submit_comment.value );
			makePOSTRequest('postcomment.php?ajax=1',poststr,results);
		}
		else{
			thisform.submit_comment.disabled=false;
			thisform.submit_comment.setAttribute('class', 'postme');
		}
}
function makePOSTRequest(url, parameters,results) {
	 var http_request = getHTTPObject();
      http_request.onreadystatechange = function() {
		parseResponse(http_request,false,results);//this is what happens once complete
	}
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
 }
 function blogpage(page,parameters,path){
	results=document.getElementById('blogleftcol');
	grabFile("/blogfeed.php?page="+page+parameters,results);
	scroll(0,0);
	window.location.hash = 'blog'+path+'/p' + page;
	recentHash = window.location.hash;
}
function ajaxLoad(url,results,data){
	data = typeof(data) != 'undefined' ? data : '';
	$.ajax({
	  url: url,
	  data: data,
	  success: function(html){
		$(results).hide().html(html).fadeIn(750);
	  }
	});
}
function snavLoad(selectId,ulId,url,results,data){
	ajaxLoad(url,results,data);
	$('ul' + ulId + ' > li > a').removeClass('snavselect').addClass('snav');
	$(selectId).removeClass().addClass('snavselect');
}
function accordionDisplay(selectId,accordionClass){
	$('.'+accordionClass+'-select').removeClass(accordionClass+'-select').addClass(accordionClass);
	$('#'+selectId).removeClass(accordionClass).addClass(accordionClass+'-select');
	$('#'+selectId + ' > div.accordion-content').slideDown(400);
	$('.'+accordionClass+' > div.accordion-content').slideUp(400);
}

