/*
* basic helper functions
*/
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) obj.addEventListener( type, fn, false );
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function getElementsByClass(className, elm, tag){
	if(elm == null) elm = document;
	if(tag == null) tag = '*';
	var elems = elm.getElementsByTagName(tag);
	var returnElems = new Array();
	className = className.replace(/\-/g, "\\-");
	var pattern = new RegExp("(^|\\s)" + className + "(\\s|$)");
	for(var i=0; i<elems.length; i++){
		if(pattern.test(elems[i].className)) returnElems.push(elems[i]);
	}
	return returnElems
}



/*
* cookies
*/
function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}
 
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}
 
function eraseCookie(name) {
    createCookie(name,"",-1);
}


/*
* lightbox
*/
function loadWelcomeLight() {
    if (readCookie('ilovecupcakes') == "" || readCookie('ilovecupcakes') == null) {
		openLightBox(); 
        createCookie('ilovecupcakes', 'YES', 3);
		return;
    } else if (readCookie('ilovecupcakes') == 'YES') {
		 return;
    }
}

function openLightBox() {
	document.getElementById("mylightbox").style.display = "block";
	document.getElementById("darken").style.display = "block";
}

function closeLightBox() {
	document.getElementById("mylightbox").style.display = "none";
	document.getElementById("darken").style.display = "none";
}

//POP UP WINDOW
function popUpCalc(url) {
	newwindow=window.open(url,'Sierra Club Green Home Calculator','height=806,width=890, resizable=yes, scrollbars=yes, toolbar=no');
	if (window.focus) {newwindow.focus()}
	return false;
}



/*
* article list functions
*/
function toggleArtList( el , textDiv ) {
	var x = 1;
	
	/*
	while ( $('article_num_' + x) ){
		$('article_num_' + x).className = "";
		$('article_text_' + x).className = "nodisplay";
		x++;
	}
		
	$(el).className = "current_tab_wrap";
	$(textDiv).className = "display";
	*/
	
	while(document.getElementById("article_num_" + x))
	{
		document.getElementById("article_num_" + x).className = "";
		document.getElementById("article_text_" + x).className = "nodisplay";
		//$("#article_num_" + x).className = "";
		//$("#article_text_" + x).className = "nodisplay";
		x++;
	}
	
	//$("#" + el).className = "current_tab_wrap";
	//$("#" + textDiv).className = "display";
	
	document.getElementById(el).className = "current_tab_wrap";
	document.getElementById(textDiv).className = "display";
}

function loadBizSearch()
{
	var busType = document.getElementById('business-type');
	
	var thisSelected = busType.options[busType.selectedIndex].value;
	
	if((thisSelected == "R") || (thisSelected == "OR"))
	{
		document.getElementById('service-select').style.display = 'none';
		document.getElementById('retailer-select').style.display = 'block';
	}
	else if(thisSelected == "S")
	{
		document.getElementById('service-select').style.display = 'block';
		document.getElementById('retailer-select').style.display = 'none';
	}
		
	if(thisSelected == "OR")
	{
		document.getElementById('sort').options.selectedIndex = 2;
		document.getElementById('sort').options[0].disabled = true;
		document.getElementById('sort').options[1].disabled = true;
	}
	else
	{
		document.getElementById('sort').options.selectedIndex = 0;
		document.getElementById('sort').options[0].disabled = false;
		document.getElementById('sort').options[1].disabled = false;
	}
}

/*
* search select things
*/
function initBusSearch() {
	if(!$('business-type')) return;

	var busType = $('business-type');
	var retSel = $('retailer-select');
	var serSel = $('service-select');

	retSel.style.display = 'none';
	//serSel.style.display = 'none';

	if(busType.selectedIndex > -1)
	{
		var thisSelected = busType.options[busType.selectedIndex].value;
		
		if((thisSelected == "R") || (thisSelected == "OR"))
		{
			$('retailer-select').style.display = 'block';
		}
		else if(thisSelected == "S")
		{
			$('service-select').style.display = 'block';
		}
	}
    
	busType.onchange = function() {
		
		var thisSelected = busType.options[busType.selectedIndex].value;
		
		if((thisSelected == "R") || (thisSelected == "OR"))
		{
			$('service-select').style.display = 'none';
			$('retailer-select').style.display = 'block';
		} else if(thisSelected == "S") {
			$('service-select').style.display = 'block';
			$('retailer-select').style.display = 'none';
		}
		
		if(thisSelected == "OR")
		{
			$('sort').options.selectedIndex = 2;
			$('sort').options[0].disabled = true;
			$('sort').options[1].disabled = true;
		}
		else
		{
			$('sort').options.selectedIndex = 0;
			$('sort').options[0].disabled = false;
			$('sort').options[1].disabled = false;
		}
	}
}

addEvent(window, 'load', initBusSearch);




function clearZip()
{
	//var zip = $('business-zip');
	var zip = document.getElementById("business-zip");
	
	if(zip.value == 'zip code')
	{
		zip.value = '';
	}
}

function checkClear()
{
	//var zip = $('business-zip');
	var zip = document.getElementById("business-zip");
	
	if(zip.value == '')
	{
		zip.value = 'zip code';
	}
}






/* probably dont need this */
sfHover = function() {
	var abc = document.getElementById("drop_nav");
	
	if(abc)
	{
		sfEls = abc.getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


//TOGGLE STYLE BASED ON LINK CLICK 
	 function toggleTextTab( $_tabID ,  $_firstTab ,  $_maxTab ) { 
	 
	 $_thisTabMax = parseInt( $_maxTab ) + 1;
	 //alert ( "number of tabs = " + $_thisTabCount );
	 
	 $_thisFirstTab = parseInt( $_firstTab );
	 //alert ( "first tab = " + $_thisFirstTab );
	
	 //hold a shortcut to the tab wrap based on the id you passed in
	 $_thisTab_wrap = document.getElementById( "tab_wrap_" + $_tabID );
	  
	 //hold the class of the tab
	 $_thisTab_tab = document.getElementById( "tab_" + $_tabID );
	 
	 //hold the class of the h3
	 $_thisTab_h3 = document.getElementById( "tab_h3_" + $_tabID );
	 
	 //hold the class of the containing div
	 $_thisTab_textBox = document.getElementById( "text_box_" + $_tabID );
		 
		 
	 // if you are not currently on that tab, turn all of the other tabs off, and turn that one on
	 //alert ( $_thisTab_wrap.className + "\n tab count = " + $_thisTabCount );
	 //if (  $_thisTab_wrap.className == "tab_flex_nc_bg clearfix" ) {
		//cycle through all of the other tabs and turn them "OFF"
		for ( jssInc = $_thisFirstTab ; jssInc < $_thisTabMax  ; jssInc++ ){
			//alert ( "tab count = " + $_thisTabCount );
			//alert ( "first tab = " + $_thisFirstTab );
			//alert ( "jssInc = " + jssInc );
			//alert ( "otherTextTabs[ jssInc ] = " + otherTextTabs[ jssInc ] );
			
			//hold / change the class of the tab_wrap
			$_thisTabFriend_wrap = document.getElementById( "tab_wrap_" + jssInc );
			//alert ( "tab_wrap_" + jssInc );
			//alert ( document.getElementById( "tab_wrap_" + jssInc ) );
			$_thisTabFriend_wrap.className = "tab_flex_bg_off clearfix";
			
			//hold / change the class of the tab
			$_thisTabFriend_tab = document.getElementById( "tab_" + jssInc );
			$_thisTabFriend_tab.className = "tab_flex_off";
			
			//hold / change the class of the H3
			$_thisTabFriend_h3 = document.getElementById( "tab_h3_" + jssInc );
			$_thisTabFriend_h3.className = "off";
			
			//hold / change the class of the containing div
			$_thisTabFriend_textBox = document.getElementById( "text_box_" + jssInc );
			//alert ( $_thisTabFriend_textBox.id );
			$_thisTabFriend_textBox.className = "nodisplay";
			
		}
		
	
		/*
		tab_wrap_ 
			class="tab_flex_current_bg clearfix left" OR class="tab_flex_nc_bg clearfix left"
			class="tab_flex_current_bg clearfix right" OR class="tab_flex_nc_bg clearfix right"
		
		tab_  
			class="tab_flex" OR class="tab_flex_nc"
		
		tab_h3_
			class="" OR class="nc"
			
		text_box_
			class="hidden" OR class = visible
		*/
		$_thisTab_textBox.className = "display";
		$_thisTab_wrap.className = "tab_flex_bg_on clearfix";
		$_thisTab_tab.className = "tab_flex_on";
		$_thisTab_h3.className = "on";
		
	 //}

	 
	 }//end toggleSelection($_imageID); 



















//PRINT THE CURRENT DATE
function date(){
			var d_names = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
			
			var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
			
			var d = new Date();
			var curr_day = d.getDay();
			var curr_date = d.getDate();
			var sup = "";
			if (curr_date == 1 || curr_date == 21 || curr_date ==31)
			   {
			   sup = "st";
			   }
			else if (curr_date == 2 || curr_date == 22)
			   {
			   sup = "nd";
			   }
			else if (curr_date == 3 || curr_date == 23)
			   {
			   sup = "rd";
			   }
			else
			   {
			   sup = "th";
			   }
			var curr_month = d.getMonth();
			var curr_year = d.getFullYear();
			
			document.write(d_names[curr_day] + ", " + m_names[curr_month] +  " " + curr_date + ", " + curr_year);
}



//REPLACE DEFAULT TEXT IN INPUT BOXES

    function ClearField(FieldToClear,DefaultText)
	{
		if(FieldToClear.value == DefaultText)
		{
			FieldToClear.value = "";
		}//end if
	}//end ClearField()
	
	function ReplaceField(FieldToClear,DefaultText)
	{
		if(FieldToClear.value == "")
		{
			FieldToClear.value = DefaultText;
		}//end if
	}//end ClearField()


// GLOBAL SCRIPTS INCLUDING 
// OPENING LINKS IN A NEW WINDOW
// REPLACING FLASH FOR VALIDATION


/*
JSTarget function by Roger Johansson, www.456bereastreet.com
*/
var JSTarget = {
	init: function(att,val,warning) {
		if (document.getElementById && document.createElement && document.appendChild) {
			var strAtt = ((typeof att == 'undefined') || (att == null)) ? 'class' : att;
			var strVal = ((typeof val == 'undefined') || (val == null)) ? 'non-html' : val;
			var strWarning = ((typeof warning == 'undefined') || (warning == null)) ? '' : warning;
			var oWarning;
			var arrLinks = document.getElementsByTagName('a');
			var oLink;
			var oRegExp = new RegExp("(^|\\s)" + strVal + "(\\s|$)");
			for (var i = 0; i < arrLinks.length; i++) {
				oLink = arrLinks[i];
				if ((strAtt == 'class') && (oRegExp.test(oLink.className)) || (oRegExp.test(oLink.getAttribute(strAtt)))) {
					oWarning = document.createElement("em");
					oWarning.appendChild(document.createTextNode(strWarning));
					oLink.appendChild(oWarning);
					oLink.onclick = JSTarget.openWin;
				}
			}
			oWarning = null;
		}
	},
	openWin: function(e) {
		var event = (!e) ? window.event : e;
		if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) return true;
		else {
		    var oWin = window.open(this.getAttribute('href'), '_blank');
			if (oWin) {
				if (oWin.focus) oWin.focus();
				return false;
			}
			oWin = null;
			return true;
		}
	},
	/*
	addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
	*/
	addEvent: function(obj, type, fn) {
		if (obj.addEventListener)
			obj.addEventListener(type, fn, false);
		else if (obj.attachEvent) {
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() {obj["e"+type+fn]( window.event );}
			obj.attachEvent("on"+type, obj[type+fn]);
		}
	}
};
JSTarget.addEvent(window, 'load', function(){JSTarget.init("rel","external","");});

