var activeSpot = null;

// Sets various id strings you feed into this to an 'active' class
// Feed in as many args as you like
function setNav()
{
	var args = setNav.arguments;
	for(i=0; i<args.length; i++)
	{
		if (document.getElementById(args[i]))
		{
			document.getElementById(args[i]).className = 'active';
		}
	}
}

// Turn 'on' a cow spot
function setSpot(spot)
{
	rollover(spot);
	activeSpot = spot;
}

// Handle rollover images - insert '_over' and take it out again
function rollover(target)
{
	if (document.getElementById(target).src.indexOf('_over') == -1)
	{
	    newImg = document.getElementById(target).src.replace(/\.(.{3,4})$/g, "_over.$1");
	    document.getElementById(target).src = newImg;
	}
}
function rollout(target)
{
	if (target != activeSpot)
	{
		newImg = document.getElementById(target).src.replace(/\_over/g, "");
		document.getElementById(target).src = newImg;
	}	
}

// Handle rollover images from Kent's files - switch 'Off' for 'On'
function rolloverK(target)
{
	if (document.getElementById(target).src.indexOf('On') == -1)
	{
	    newImg = document.getElementById(target).src.replace(/Off\.(.{3,4})$/g, "On.$1");
	    document.getElementById(target).src = newImg;
	}
}
function rolloutK(target)
{
	if (document.getElementById(target).src.indexOf('Off') == -1)
	{
	    newImg = document.getElementById(target).src.replace(/On\.(.{3,4})$/g, "Off.$1");
	    document.getElementById(target).src = newImg;
	}
}

// Everybody's favorite preloader from Dreamweaver
function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

// Popup window - this one has scrollbares, resizing, the whole 9
function popUp(url, x, y)
{
	if (isIE() == true)
	{
		y += 40;
	}
    var args = "width=" + x + ",height=" + y + ",status=1,resizable=1,scrollbars=1,menubar=1,location=1";
    var date = new Date();
    var now = date.getTime();
    var newName = (now).toString(10);

    window.open(url,newName,args);
}

function isIE()
{
	//Detect IE5.5+
	version=0;
	if (navigator.appVersion.indexOf("MSIE")!=-1) {
		temp=navigator.appVersion.split("MSIE");
		version=parseFloat(temp[1]);
	}
	if (version >= 5.5)
	{ //NON IE browser will return 0
		return true;
	} else {
		return false;
	}	
}

function checkAvail(theForm)
{
	var why = '';
	why += checkField(theForm.address.value, "Home Address");
	if (checkField(theForm.city.value, "City") && checkField(theForm.zip.value, "Zip Code"))
	{
		why += "Please enter your city or zip code";
	}

	if (why != "")
	{
		alert(why);
		return false;
	}
	return true;
}
function checkValid(theForm)
{
	var why = '';
	why += checkField(theForm.first_name.value, "First Name");
	why += checkField(theForm.last_name.value, "Last Name");
	why += checkField(theForm.address.value, "Home Address");
	why += checkField(theForm.how_heard.value, "How Did You Hear About Us");
	if (checkField(theForm.city.value, "City") && checkField(theForm.zip.value, "Zip Code"))
	{
		why += "Please enter your city or zip code\n";
	}
	why += checkEmail(theForm.email.value, "Email");
	why += checkField(theForm.phone.value, "Phone");
	if (theForm.zip.value && theForm.zip.value.length < 5) why += "Zip code must be at least 5 digits";

	if (why != "")
	{
		alert(why);
		return false;
	}
	return true;
}


function checkField (val, field)
{
 var error = "";
 if (val == "") {
   error = "You must enter a value in the " + field + " field.\n";
 }
 return error;
}

function checkEmail(val, field)
{
 var error = "";
 if ((val == "" || val.length < 3) ||
 (val.indexOf("@") == "-1") ||
 (val.indexOf(".") == "-1")) {
   error = "Please enter a valid " + field + " address.\n";
 }

 return error;
}

function doPopups() {
  if (!document.getElementsByTagName) return false;
  var links=document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("popup")) {
      links[i].onclick=function() {
        // Below - to open a full-sized window, just use: window.open(this.href);
        window.open(this.href, "", "top=40,left=40,width=690,height=570,scrollbars");
        return false;
      }
    }
  }
}

function printSelection(node){

  var content=node.innerHTML
  var pwin=window.open('','print_content','width=100,height=100');

  pwin.document.open();
  pwin.document.write('<html><body onload="window.print()">'+content+'</body></html>');
  pwin.document.close();
 
  setTimeout(function(){pwin.close();},1000);

}