function addOnloadEvent(fnc){

  if ( typeof window.addEventListener != "undefined" ) {
	window.addEventListener( "load", fnc, false );
  }
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else
      window.onload = fnc;
  }
}

function radiusAction() {
	if (document.getElementById('radiusDrop') && document.getElementById('radiusText')) {
		if (document.getElementById('radiusDrop').value != "0") {
			document.getElementById('radiusText').style.color = "#000000";
			document.getElementById('radiusText').disabled = false;
		} else {
			document.getElementById('radiusText').style.color = "#cccccc";		
			document.getElementById('radiusText').disabled = true;
			document.getElementById('radiusText').value = "zip code or city, state";
		}
	}
}

function getCategories() {
	if (document.getElementById('catDrop') && document.getElementById('findDealsSubCategories')) {
		var selectedCat = document.getElementById('catDrop').value;
		var url = "filebin/ajax/php/getCategories.php?cat="+selectedCat;
		var xml = new JKL.ParseXML(url);
		var data = xml.parse();
		var subcategories = data.instance;
		if (subcategories) {
			document.getElementById('findDealsSubCategories').innerHTML = subcategories;		
		}
	}
}

function getPrograms(home, site) {

	var selectedProgram = "selectedProgram_"+site;
	var searchBottomContent = "searchBottomContent_"+site;	
	if (document.getElementById(selectedProgram) && document.getElementById(searchBottomContent)) {
		var selectedState = document.getElementById(selectedProgram).value;
		var url = "filebin/ajax/php/getPrograms.php?state="+selectedState+"&home="+home;
		var xml = new JKL.ParseXML(url);
		var data = xml.parse();
		var programs = data.instance;
		if (programs) {
			document.getElementById(searchBottomContent).style.height = "auto";		
			document.getElementById(searchBottomContent).innerHTML = programs;		
		}
	}
}

function seeAllNat(search_id, url_use) {
	if (document.getElementById("seeAllDiv") && document.getElementById("seeAllLast")) {
		document.getElementById("seeAllDiv").innerHTML = "Loading...";
		document.getElementById("seeAllLast").innerHTML = "Loading...";
	}
	setTimeout("seeAllNatAction('"+search_id+"','"+url_use+"')",100);	
}

function seeAllNatAction(search_id, url_use) {
	var url = "filebin/ajax/php/seeAllNat.php";
	var query = "id="+search_id+"&url_use="+url_use+"&action=show";
	var xml = new JKL.ParseXML(url, query);
	var data = xml.parse();
	if (document.getElementById("nationalResultsList") && data.instance.result) {
		document.getElementById("nationalResultsList").innerHTML = data.instance.result;
		if (document.getElementById("seeAllDiv") && document.getElementById("seeAllLast")) {
			document.getElementById("seeAllDiv").innerHTML = "<a href='#' onclick='closeNat("+search_id+",\""+url_use+"\"); return false;'>Close National Results -</a>";
			document.getElementById("seeAllLast").innerHTML = "<a href='#' onclick='closeNat("+search_id+",\""+url_use+"\"); return false;'>Close National Results -</a>";
		}
	}
}

function closeNat(search_id, url_use) {
	if (document.getElementById("seeAllDiv") && document.getElementById("seeAllLast")) {
		document.getElementById("seeAllDiv").innerHTML = "Closing...";
		document.getElementById("seeAllLast").innerHTML = "Closing...";
	}
	setTimeout("closeNatAction('"+search_id+"','"+url_use+"')",100);	
}

function closeNatAction(search_id, url_use) {
	var url = "filebin/ajax/php/seeAllNat.php";
	var query = "id="+search_id+"&url_use="+url_use+"&action=close";
	var xml = new JKL.ParseXML(url, query);
	var data = xml.parse();
	if (document.getElementById("nationalResultsList") && data.instance.result && data.instance.total) {
		document.getElementById("nationalResultsList").innerHTML = data.instance.result;
		if (document.getElementById("seeAllDiv") && document.getElementById("seeAllLast")) {
			document.getElementById("seeAllDiv").innerHTML = "<a href='#' onclick='seeAllNat("+search_id+",\""+url_use+"\"); return false;'>See All ("+data.instance.total+") National Results +</a>";
			document.getElementById("seeAllLast").innerHTML = "<a href='#' onclick='seeAllNat("+search_id+",\""+url_use+"\"); return false;'>See All ("+data.instance.total+") National Results +</a>";
		}
	}
}

function scrollToResults() {
	if (document.getElementById('middle')) {
		var divTop = document.getElementById('middle').offsetTop;
		window.scroll(0,divTop);
	}
}

function submitSearch(e) {
	var keycode;
	if (window.event) {
		keycode = window.event.keyCode;
	} else if (e) {
		keycode = e.which;
	} else {
		return true;
	}
	
	if (keycode == 13) {
		if (document.findDealsForm) {
			if (document.getElementById('buttonText')) {
				document.getElementById('buttonText').innerHTML = "<div class='findDealsSubmited'>SEARCHING...</div>";
			}
			document.findDealsForm.submit();
		}
	   return false;
	} else {
	   return true;
	}
}

function startSearch() {
	if (document.findDealsForm) {
		if (document.getElementById('buttonText')) {
			document.getElementById('buttonText').innerHTML = "<div class='findDealsSubmited'>SEARCHING...</div>";
		}
		document.findDealsForm.submit();
	}
}

function toggle_check_all() {
	if (document.getElementById('subcat_all')) {
		if (document.getElementById('subcat_all').checked == true) {
			document.getElementById('subcat_all').checked = false;
		} 
	}
}