function clearField(val, elID){
	if(val=="Friend's email address" || val=="Your email address"){
		$(elID).value="";
		$(elID).style.color="#000000";
	}
}


function clearBoxIf(val, clearVal,itemID){
	if(val.toLowerCase()==clearVal.toLowerCase()){
		$(itemID).value="";
		$(itemID).style.color="#000000";
	}else{
		$(itemID).select();
	}
}



var cX = 0; var cY = 0; var rX = 0; var rY = 0;
function UpdateCursorPosition(e){ cX = e.pageX; cY = e.pageY;}
function UpdateCursorPositionDocAll(e){ cX = event.clientX; cY = event.clientY;}
if(document.all) { document.onmousemove = UpdateCursorPositionDocAll; }
else { document.onmousemove = UpdateCursorPosition; }
function AssignPosition(d) {
if(self.pageYOffset) {
	rX = self.pageXOffset;
	rY = self.pageYOffset;
	}
else if(document.documentElement && document.documentElement.scrollTop) {
	rX = document.documentElement.scrollLeft;
	rY = document.documentElement.scrollTop;
	}
else if(document.body) {
	rX = document.body.scrollLeft;
	rY = document.body.scrollTop;
	}
if(document.all) {
	cX += rX; 
	cY += rY;
	}
d.style.left = (cX+10) + "px";
d.style.top = (cY+10) + "px";
}
function HideContent(d) {
if(d.length < 1) { return; }
document.getElementById(d).style.display = "none";
}
function ShowContent(d,content) {
if(d.length < 1) { return; }
var dd = document.getElementById(d);
AssignPosition(dd);
dd.innerHTML=content;
dd.style.display = "block";
}
function ReverseContentDisplay(d) {
if(d.length < 1) { return; }
var dd = document.getElementById(d);
AssignPosition(dd);
if(dd.style.display == "none") { dd.style.display = "block"; }
else { dd.style.display = "none"; }
}




function initMaps() {
   if (document.getElementById) {
      var mapIds = initMaps.arguments;    // pass string IDs of containing map elements
      var i, j, area, areas;
      for (i = 0; i < mapIds.length; i++) {
        areas = document.getElementById(mapIds[i]).getElementsByTagName("area");

        for (j = 0; j < areas.length; j++) {  // loop thru area elements
           area = areas[j];
           area.onmousedown = imgSwap;    // set event handlers
           area.onmouseout = imgSwap;
           area.onmouseover = imgSwap;
           area.onmouseup = imgSwap;
        }
      }
   }
}

// image swapping event handling
function imgSwap(evt) {
   evt = (evt) ? evt : event;                   // equalize event models
   var elem = (evt.target) ? evt.target : evt.srcElement;
   var imgClass = elem.parentNode.name;         // get map element name
   var coords = elem.coords.split(",");         // convert coords to clip
   var clipVal = "rect(0px " + coords[2] + "px " + coords[3] + "px " + "0px)";
   var imgStyle;
   
   switch (evt.type) {
      case "mousedown" :
         imgStyle = document.getElementById(imgClass + "Down").style;
         imgStyle.clip = clipVal;
         imgStyle.visibility = "visible";
		 
         break;
      case "mouseout" :
  	  	 $('YourStars').style.display="";
         document.getElementById(imgClass + "Over").style.visibility = "hidden";
         document.getElementById(imgClass + "Down").style.visibility = "hidden";
         break;
      case "mouseover" :
	  	 $('YourStars').style.display="none";
         imgStyle = document.getElementById(imgClass + "Over").style;
         imgStyle.clip = clipVal;
         imgStyle.visibility = "visible";
         break
      case "mouseup" :
         document.getElementById(imgClass + "Down").style.visibility = "hidden";
         // guarantee click in IE
         if (elem.click) {
             elem.click();
         }
         break;
   }
   evt.cancelBubble = true;
   return false;
}



function rateContent(contentType, rating, contentID,clipWidth){
	
	var clipVal = "rect(0px " + clipWidth + "px " +  "13px " + "0px)";
	$('YourStars').style.clip = clipVal;
   	new Ajax.Request('ajaxcalls/rateContent.cfm', {
  		method: 'get',
  		parameters: {contentType: contentType, contentID: contentID, rating: rating},
		onSuccess: function(transport){
      		var response = transport.responseText;
      		Set_Cookie("RatingFor_"+contentType+"_"+contentID,rating,365);
			$('avgStarHolder').innerHTML=transport.responseText;
	    },
    	onFailure: function(transport){ 
			var response = transport.responseText || "no response text";
		}
  	});
}




// this function gets the cookie, if it exists
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

/*
only the first 2 parameters are required, the cookie name, the cookie
value. Cookie time is in milliseconds, so the below expires will make the 
number you pass in the Set_Cookie function call the number of days the cookie
lasts, if you want it to be hours or minutes, just get rid of 24 and 60.

Generally you don't need to worry about domain, path or secure for most applications
so unless you need that, leave those parameters blank in the function call.
*/
function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	// if the expires variable is set, make the correct expires time, the
	// current script below will set it for x number of days, to make it
	// for hours, delete * 24, for minutes, delete * 60 * 24
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	//alert( 'today ' + today.toGMTString() );// this is for testing purpose only
	var expires_date = new Date( today.getTime() + (expires) );
	//alert('expires ' + expires_date.toGMTString());// this is for testing purposes only

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}







function zipCheck(postalCode){
  var validFormat = /^[0-9]{5}[ |-][0-9]{4}$|^[0-9]{5}$|^[A-Z][0-9][A-Z][ |-]?[0-9][A-Z][0-9]$/

  if(isValid = validFormat.test(postalCode)){
  		return isValid;
	}else{
		alert("Please enter a valid US or Canadian postal code.");	
		return isValid;	
	}
}

function openWin(subject){
	subject=subject.toLowerCase();
	if(subject=='pressroom' || subject=='steve ertel'){
		window.location='email.cfm?contentID=6879';
	
	}else if(subject=='papercalc'){
		window.location='/email.cfm?contentID=5589';
	
	}else if(subject == 'jennifer witherspoon'){
		window.location='email.cfm?contentID=6542';

	}else if(subject == 'douglas zabel' || subject=='Colin Rowan'){
		window.location='email.cfm?contentID=6546';
	
	}else if(subject == 'georgette shepherd'){
		window.location='email.cfm?contentID=6544';
	
	}else if(subject == 'jessica mendelowitz'){
		window.location='email.cfm?contentID=6541';
	
	}else if(subject == 'charlie miller' || subject == 'nicole st. claire'){
		window.location='email.cfm?contentID=6879';
	
	}else if(subject == 'kathleen goldstein'){
		window.location='email.cfm?contentID=6863';

	}else if(subject == 'keith gabyn'){
		window.location='email.cfm?contentID=6538';
	
	}else if(subject == 'melanie janin'){
		window.location='email.cfm?contentID=6540';	
	
	}else if(subject == 'careers'){
		window.location='email.cfm?contentID=6880';		
	
	}else if(subject == 'partnerships'){
		window.location='email.cfm?contentID=6862';		
	
	}else if(subject == 'hybrids'){
		window.location='email.cfm?contentID=6033';

	}else if(subject == 'tf_site' || subject == 'farm_bill' || subject == 'agriculture' || subject == 'catalog' || subject == 'toxic' || subject == 'nox' || subject == 'fedex' || subject == 'nonroad'  || subject == 'riogrande'  || subject == 'mercury'  || subject == 'asthma'  || subject == 'tf_dangerousdays'  || subject == 'tf_hetch' || subject == 'tf_job'  || subject == 'tf_esaStory' || subject == 'tf_article'){
		window.location='email.cfm?contentID=5450';

	}else if(subject == 'volunteer' || subject == 'greengiftsuggestions' || subject == 'members' || subject == 'nlwvdanc' || subject == 'nlwvdana' || subject == 'nlwvdanb' || subject == 'nlwxcagw' || subject == 'nlwvdand' || subject == 'agwactcc' || subject == 'nlwxcagb' || subject == 'nlwvdang' || subject == 'nlwvdane' || subject == 'feedback' || subject == 'car' || subject == 'newsletter' || subject == 'privacy' || subject == 'redesign'){
		window.location='email.cfm?contentID=6787';

	}else{
		window.location='email.cfm?contentID=6787';		
	}
}


function textCounter(field, countfield, maxlimit) {
	

  if (field.value.length > maxlimit){
  	field.value = field.value.substring(0, maxlimit);
	}else{
		countfield.value = maxlimit - field.value.length;
	}
 }
 
 
 function pollVote(formName){
 	var url = 'ajaxcalls/pollResults.cfm';

	new Ajax.Updater('PollBox', url, {
		method: 'post',
		parameters: $(formName).serialize(true)
		/*onSuccess: function(transport) {
			new Effect.Highlight('content_Holder',  {startcolor:'#A3AEC2', endcolor:'#FFFFFF'});

		},
		onFailure: function(transport) {
			document.write(transport.responseText);
			 new Effect.Highlight('content_Holder',  {startcolor:'#A3AEC2', endcolor:'#FFFFFF'});
			 alert("Error, Classification did not save");
		}*/
	});

	return false;
}

function clearPollOther(questionNumber){
other="otherquestion"+questionNumber;

if($(other)){
	$(other).value="";
}

}


function otherPollFocus(questionNumber){
other="QuestionOther"+questionNumber;

if($(other)){
	$(other).checked=true;
}

}


function submitPollForm(pollform){
formdata=Form.serialize($(pollform));
new Ajax.Updater('PollHolder','ajaxcalls/submitpoll.cfm',
  {
   method:'post',
   postBody: formdata,
	asynchronous: false,
    onSuccess: function(transport){
		
		//document.write(transport.responseText);
		
    },
    onFailure: function(transport){
		document.write(transport.responseText);
	}
  });

  return false;
}




//slide show functions
function showSlideImage(slideID){
	if($F('transition')=="fade"){
		var queueParam = { scope: 'outThenIn', position: 'end' };
		Effect.Fade('slideImage', { duration: 0.5, queue: queueParam,
   			afterFinish: function() {
    		 $('slideImage').src=$('image'+slideID).src;
			 Effect.Appear('slideImage', { duration: 1, queue: queueParam });
  		 }
		});
	}else{
		$('slideImage').src=$('image'+slideID).src;
	}
	
	if($('image'+slideID).name.length >0){
		$('caption1').innerHTML="Photography: "+$('image'+slideID).name;
	}else{
		$('caption1').innerHTML="";
	}
	$('caption2').innerHTML=$('image'+slideID).alt;
	$('caption3').innerHTML=$('caption'+slideID).innerHTML;
	$('slideTitle').innerHTML=$('slideTitle'+slideID).innerHTML;
	
	if($('image'+slideID).lang.length >0){
		$('slideHREF').href=$('image'+slideID).lang;
	}else{
		$('slideHREF').href="javascript: void(0);";
	}
	
	var children = $('chooseSlide').childNodes; 
	 for (var i = 0; i < children.length; i++){
		children[i].style.backgroundColor="#26466D";
	  };
	
	
	$('slideLink'+slideID).style.backgroundColor="#696969";
}

function showNextSlide(){
var children = $('chooseSlide').getElementsByTagName('a'); 
var foundGreySlide=false;
	 for (var i = 0; i < children.length; i++){
		if((children[i].style.backgroundColor=="rgb(105, 105, 105)" || children[i].style.backgroundColor=="#696969") && i !=(children.length-1)){
			slideID=children[Number(i)+1].id.substring(9);
			showSlideImage(slideID);
			i=children.length;
			foundGreySlide=true;
		}
	  };
	if(foundGreySlide==false){
		slideID=children[0].id.substring(9);
		showSlideImage(slideID);
	}
}

function showPrevSlide(){
var children = $('chooseSlide').childNodes; 
	 for (var i = 0; i < children.length; i++){
		if((children[i].style.backgroundColor=="rgb(105, 105, 105)" || children[i].style.backgroundColor=="#696969") && i !=0){
			slideID=children[Number(i)-1].id.substring(9);
			showSlideImage(slideID);
			i=children.length;
		}
	  };
}
var SlideTime;
var slidePlay=0;
function playShow(speed){
	if(slidePlay==1){
		showNextSlide();
	}
	SlideTime=setTimeout("playShow("+speed+")",speed);
	slidePlay=1;
}


function playPauseSlideShow(speed){
if($('playPauseLink').innerHTML=="Pause"){
	$('playPauseLink').innerHTML="Play";
	clearTimeout(SlideTime);
}else{
	$('playPauseLink').innerHTML="Pause";
	playShow(speed);
}

}


// end slideshow functions



function clearHomeRadio(){
	obj=document.getElementById('donateForm');
	children = obj.getElementsByTagName('input'); 
	for(var x=0; x<children.length; x++){
		children[x].checked=false;	
	}
}
function clearHomeText(){
	obj=document.getElementById('donateForm');
	children = obj.getElementsByTagName('input'); 
	for(var x=0; x<children.length; x++){
		if(children[x].type=="text"){
			children[x].value="";
		}	
	}
}


function gup(name){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
