// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
var openClass = 'popper';
var closeClass = 'popcloser';

function addMyEvent(elem,evt,func){
	if(elem.addEventListener){
		elem.addEventListener(evt,func,false);
	}else if(elem.attachEvent){
		elem.attachEvent('on'+evt,func);
	}else{
		return false;
	}
	return true;
}

var openPopup = function(theurl) {
	window.open(theurl+ '?pop=true','_info','status=0,toolbar=0,location=0,menubar=0,resizable=1,scrollbars=1,width=560,height=680');
}

var infoevent = function(e) {
	if (!e) var e = window.event;
	var tg = (window.event) ? e.srcElement : e.target;
	// if (e.target) targ = e.target;
	// else if (e.srcElement) targ = e.srcElement;
	if (tg.nodeType == 3) // defeat Safari bug
		tg = tg.parentNode;	
	if (tg.nodeName != 'A') // in case you get a SPAN or something inside the A
		tg = tg.parentNode;	
	if (tg.nodeName == 'A'){
		var popupRegExp = new RegExp("(^|\\s)"+openClass+"(\\s|$)");
		if(popupRegExp.test(tg.className)){
			openPopup(tg.href);
		}else{
			var popdownRegExp = new RegExp("(^|\\s)"+closeClass+"(\\s|$)");
			if(popdownRegExp.test(tg.className)){
				closePopup(tg.href);
			}
		}
	}
}

// loads a url into the window opener and closes the window
// as long as there IS an opener
var closePopup = function(theurl) {
	if( opener ) {
		opener.document.location = theurl;
		self.close(); 
	}
}

// grep through DOM and add event listeners to certain class links
function popifyLinks(){
	var links = getElementsByClassName(document,'a',openClass+'|'+closeClass);
	for (x in links) {
		if( !addMyEvent(links[x],'click',infoevent) ){
			links[x].onclick = infoevent;
		}
	}
}

// provide a root dom object, a type of object, and a class name, and will return all matches
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){ //getAttribute("class")){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

var clearZip = function() {
	zipField = document.getElementById("zip_code");
	if( zipField ){
		zipField.value = zipField.value.replace(/[^0-9]/g,"");
	}
}
var fillZip = function() {
	zipField = document.getElementById("zip_code");
	if( zipField ){
		zipField.value = zipField.value.replace(/[^0-9]/g,"");
		if(zipField.value==''){
			zipField.value='Zip Code';
		}
	}
}
var zippifyForm = function(){
	zipField = document.getElementById("zip_code");
	if( zipField ){
		if(zipField.value==''){
			zipField.value='Zip Code';
		}
		if( addMyEvent(zipField.form,'submit',clearZip) ){
			addMyEvent(zipField,'focus',clearZip);			
			addMyEvent(zipField,'blur',fillZip);			
		} else {
			zipField.form.onsubmit = clearZip;
			zipField.onfocus = clearZip;
			zipField.blur = fillZip;
		}
	}
}

if( addMyEvent(window,'load',popifyLinks) ){
	addMyEvent(window,'load',zippifyForm);	
} else {
	window.onload = function(){
		popifyLinks();
		zippifyForm();
	}
}
