// POP UP
// usage: popuplink(['js-only url',] this[, w[, h[, scroll[, extras]]]])
// basic usage: <a href="popup.html" target="_blank" onclick="return(popuplink(this));">new pop</a>
// advanced usage: <a href="popup_nojs.html" target="_blank" onclick="return(popuplink('popup_yesjs.html', this, 200, 100, false));">new pop</a>
// site-wide defaults:
POPUP_W = 300;
POPUP_H = 200;
POPUP_SCROLL = true;
POPUP_EXTRAS = 'location=0,statusbar=0,menubar=0';
function popuplink() {
	var undef, i=0, args=popuplink.arguments;
	var url = (typeof(args[i])=='string') ? args[i++] : args[i].getAttribute('href');
	var target = args[i++].getAttribute('target') || '_blank';
	var w = args[i++];
	var h = args[i++];
	var s = (args[i]===undef) ? POPUP_SCROLL : args[i++];
	var features = 'width=' + (w || POPUP_W)
				 + ',height=' + (h || POPUP_H)
				 + ',scrollbars=' + (s ? 'yes,' : 'no,')
				 + (args[i] || POPUP_EXTRAS);
	var win = window.open(url, target, features);
	win.focus();
	return false;
}
// END POP UP

function popWindow(url, name, intWidth, intHeight) {
		winParameters = "width=" + intWidth + ",height=" + intHeight + ",scrollbars=yes,toolbar=no,status=no,menubar=no,location=no";
		popupWin = window.open(url, name, winParameters);
		popupWin.focus();
		return;
	}

function limitText(textArea, length) {
    if (textArea.value.length > length) {
        textArea.value = textArea.value.substr(0,length);
    }
}

function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=760,height=300');");
}

/*
function hide(element){
	document.getElementById(element).style.display="none";
	return;
}
function show(element){
	document.getElementById(element).style.display="block";
	return;		
}	
*/


//START ROLLOVER

//<![CDATA[

	function imageLoader(a) {
		if (document.images) {
			if (!document.p) document.p=new Array();
			var i, j=document.p.length;
			for (i=0; i<a.length; i++)
				if (a[i].indexOf("#")!=0) { document.p[j] = new Image; document.p[j++].src = a[i]; }
		}
	}
	function preloadImages() {
		imageLoader(preloadArray);
	}

	// list images to preload here:
	//var preloadArray = ["homelink_mo.gif"];
	var preloadArray = [];

	// start preloading images after page load
	if (window.addEventListener) {
		window.addEventListener("load", preloadImages, true);
	} else if (window.attachEvent) {
		window.attachEvent("onload", preloadImages);
	} else {
		window.onload = preloadImages;
	}

	HOVER_X = "_mo"; // mouse-over extension
	// USAGE: swap(this, ['newimage.gif'])
	function swap(i) {
		if (document.images && i.childNodes) {
			var a = swap.arguments[1], s = i.childNodes[0];
			if (a) s.src = a;
			else {
				var x = s.src.lastIndexOf('.'), xl = HOVER_X.length;
				if (s.src.substring(x-xl, x) == HOVER_X)
					s.src = s.src.substring(0,x-xl)+s.src.substring(x,s.src.length);
				else s.src = s.src.substring(0,x)+HOVER_X+s.src.substring(x,s.src.length);
			}
		}
	}
	//]]>

//END ROLLOVER
 









/**  addEvent
	 *	Example:
	 *	addEvent(window, 'load', function() {
	 *		document.getElementById('myfield').focus()
	 *	});
	 */
	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 removeEvent( obj, type, fn ) {
		if (obj.removeEventListener) {
			obj.removeEventListener( type, fn, false );
		}
		else if (obj.detachEvent) {
			obj.detachEvent( "on"+type, obj[type+fn] );
			obj[type+fn] = null;
			obj["e"+type+fn] = null;
		}
	}




/** 
 * Son of Suckerfish - for dynamic nav menus in IE */
 
sfHover = function() {
	if (document.all && document.getElementById("navbarFlyout")) { // IE only, plz
		var sfEls = document.getElementById("navbarFlyout").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"), ""); 
			}
		}
	} // end IE test
};





// inits
	addEvent(window,'load',sfHover);

	
	
	Event.observe(window, "load", function() {
        inputClickCheck("sendEmailInput"); // pass in id to attach onclick to
        inputClickCheck("sendPostalInput"); // pass in id to attach onclick to
        membershipClickCheck("span.radio_levels input"); // pass in id to attach onclick to
        
  
    });
	
function inputClickCheck(el) {    
    var emailForm = $(el);
    if(!emailForm) {
        return;
    }
    var checkedValue = emailForm.down('input');   // grab the input (because this an asp:list item it is wrapped in a span, so have to traverse down)   
    checkedValue.onclick = selectClicked; // if input is clicked
}


function selectClicked(e)
{
    var e = e || window.event;  // ie check
    var findResult = Event.findElement(e, 'input').parentNode;  // grab parentnode (span) of clickevent
    
    var relResult = findResult.readAttribute("rel");    // rel of span indicates which divs we are going to turn of first by default
    var relDivs = $$('div.' + relResult);   // grab divs which match rel string
    hidePanelSet(relDivs); // hide them
    
    
    var classResult = findResult.readAttribute("class");  // class of span indicates which div we are going to turn on that correspond to radio button
    var classMatchDiv = $(classResult);   // grab div to turn on
    classMatchDiv.show();   // show div
}


function hidePanelSet(panelSetClass) {
    panelSetClass.invoke('hide');   // hide all divs
}





function hideLevels(tag, init_load) {
    if (init_load == true) {
        var loadTag = tag.readAttribute("value");
        var divId = $(loadTag);
        if(!divId) {
            return;
        }
        $('custom_input').hide();
        $('playbill').hide();
        hideLevels();   // turn off all other list items in div first
        var divChildren = $(divId).childElements();
        $(divChildren).invoke('show');  // turn on correct 65 level list items 
    }
    else {     
        $('custom_input').hide();
        $('playbill').hide();
        var benefits_list = $$('div#benefits_list ul li');
        $(benefits_list).invoke('hide');  // turn all membership levels off
        var benefits_p = $$('div#benefits_list p'); // if the results are in a paragraph instead of li's
        $(benefits_p).invoke('hide');  // turn all membership levels off
        if(!benefits_p) {
            return;
        }
    }
    

}

function membershipClickCheck(tag) {
    var foundEl = $$(tag);      // what type of tag we are looking for value in 
    for (i=0; i<foundEl.length; i++) {       // get all of tag type 
       if (foundEl[0]) {
          init_load = true;
          hideLevels(foundEl[0], init_load);      // turn first element in array on by default (before an onclick happens)      
       }
       var results = foundEl[i];    // this is the tag we want to attach onclick event to
       results.onclick = tagClicked; // when input is clicked, send to tagClicked function             
    }
    if(!foundEl) {
        return;
    }
}



function tagClicked(e)
{
    var e = e || window.event;  // ie check
    var findResult = Event.findElement(e, 'input');  // grab input tag that was clicked
    var tagValue = findResult.readAttribute("value");  // store value as a string
    var divId = $(tagValue);
    if(!divId) {
        return;
    }
    _divId = divId.id.lastIndexOf('_');
	amt_num = divId.id.substring(_divId + 1);   // we are going to check the total requested, so we can show special messaging above a certain amount
    hideLevels();   // turn off all other list items in div first
    divId.show();
    if (amt_num >= 1000) {          // if radio buttom amount is over 1,000 than show playbill messaging
	    $('playbill').show();	    
	}
	if (tagValue == "custom_amt") {     // if you have clicked the custom amount radio button
        input_div = $('custom_input')
        input_div.show();   // show custom amount div
        inputField = input_div.getElementsByTagName("INPUT")[0];    // grab input
        $(inputField).observe('keyup', customValue);         // observe key up on input
      
    }
    function customValue(e) {
        var e = e || window.event;
		var element = Event.element(e);
		init_el_value = element.value;
		var el_value = init_el_value.replace(new RegExp("\\,\\b|\\$\\b"), "");     // strip out comma or dollar sign if entered
		if (el_value >= 1000) {     // if user has entered amount over 1,000
	        $('playbill').show();	// show playbill messaging    
	    } else {
	        $('playbill').hide();	// if they go under amount, hide custom messaging
	    }
    }
    var divChildren = $(divId).childElements();
    $(divChildren).invoke('show');  // turn on correct li's that match radio button
}

