onscroll=function() {
	if (!$("profile_details") && !$("free-plumbing-quote_page") && !$("plumbing-costs_page") && !$("awu") && !$("advertise_page") && $("side_right") && window.XMLHttpRequest) {
		if (document.documentElement.scrollTop>132 || document.body.scrollTop>132) {
			$("side_right").style.position="fixed"; 
			$("side_right").style.top="0";
			$("side_right").style.left="711px";
		}
		else {
			$("side_right").style.position="absolute"; 
			$("side_right").style.top="133px";
		}
	}
}
onload=function() {
	externalLinks();
	if ($("recent_searches")) {
		$("recent_searches").getElementsByTagName("ul")[0].style.display="block";
		$("recent_searches").getElementsByTagName("p")[0].style.cursor="pointer";
		$("recent_searches").getElementsByTagName("p")[0].onclick=function() {
			$("recent_searches").getElementsByTagName("ul")[0].style.display=($("recent_searches").getElementsByTagName("ul")[0].style.display=="block")?"none":"block";
		}
	}
	if ($("ads_by_city")) {
		$("ads_by_city").getElementsByTagName("ul")[0].style.display="none";
		$("ads_by_city").getElementsByTagName("p")[0].style.cursor="pointer";
		$("ads_by_city").getElementsByTagName("p")[0].onclick=function() {
			$("ads_by_city").getElementsByTagName("ul")[0].style.display=($("ads_by_city").getElementsByTagName("ul")[0].style.display=="block")?"none":"block";
		}
	}
	if ($("recent_advertisers")) {
		var Scroller=function(frameId, contentId, tempo, w, h) {
			// default parameters
			var thisObj=this;
			this.nXlocation=0;
			this.nYlocation=0;
			this.nFrameHeight=h;
			this.nFrameWidth=w;
			this.nTempo=tempo;
			this.scrollerStat=true;
			this.objFrame=document.getElementById(contentId);
			// frame parameter
			f=document.getElementById(frameId);
			f.style.height=h+"px";
			f.style.width=w+"px";
			this.MoveScroller=function() {
				this.nXlocation--;
				this.objFrame.style.top=this.nXlocation+"px";
			}
			this.CheckScroller=function() {
				if ((0-this.nXlocation)>this.objFrame.offsetHeight) this.nXlocation=this.nFrameHeight;
			}
			this.RunScroll=function() {
				if (this.scrollerStat) {
					this.MoveScroller();
					this.CheckScroller();
				}
			}
			this.StartScroll=function() {
				setInterval(function() {thisObj.RunScroll();},thisObj.nTempo);
			}
		}
		var s=new Scroller("scrollframe", "scrollcontent", 44, 186, 80);
		s.StartScroll();
	}
	
	/*if ($("total_payment")) {
		$("total_payment").getElementsByTagName("span")[0].innerHTML="$0";
		var dollars;
		function update_payment(total) {
			if ($("su_type").value=="2") {
				 silver
				if (total==1) dollars="30";
				else if (total>1 && total<6) dollars="$"+(total*25);
				else if (total>5 && total<16) dollars="$"+(total*20);
				else if (total>15 && total<26) dollars="$"+(total*15);
				else if (total>25) dollars="contact us for our special offer";
			}
			else if ($("su_type").value=="3") {
				 gold
				if (total==1) dollars="50";
				else if (total>1 && total<6) dollars="$"+(total*40);
				else if (total>5 && total<16) dollars="$"+(total*30);
				else if (total>15 && total<26) dollars="$"+(total*25);
				else if (total>25) dollars="contact us for our special offer";
			}
			$("total_payment").getElementsByTagName("span")[0].innerHTML=dollars;
		}
	}*/
	if ($("awu")) {
		function capitalize(str) {
			newVal="";
			str=str.split(" ");
			for (var c=0; c<str.length; c++) {
				newVal+=str[c].substring(0, 1).toUpperCase()+str[c].substring(1, str[c].length)+" ";
			}
			return newVal;
		}
		var url=location.href;
		var dotCom=url.indexOf(".com/")+5;
		var lastSlash=url.lastIndexOf("/");
		var state=url.substring(dotCom, dotCom+2).toUpperCase();
		var city=url.substring(lastSlash+1, url.length);
		city=capitalize(city.replace("-", " "));
		if (url) $("ems_state").value=state;
		function hide_awu_form() {
			$("ems_fname").className="";
			$("ems_lname").className="";
			$("ems_phone_1").className="";
			$("ems_phone_2").className="";
			$("ems_phone_3").className="";
			$("ems_email").className="";
			$("ems_city").className="";
			$("ems_street").className="";
			$("ems_zip").className="";
			$("opacity_overlay").style.display="none";
			$("awu_form").style.display="none";
			$("ems_form").getElementsByTagName("legend")[0].innerHTML="Looking for a local plumber?";
			$("ems_aid").value="0";
			$("emis_side_placeholder").appendChild($("ems_form"));
		}
		$("ems_cancel").onclick=function() {hide_awu_form();}
		document.onkeyup=function(e) {
			if ($("awu_form").style.display=="block") {
				var keyPressed=(window.event)?event.keyCode:e.keyCode;
				if (keyPressed=="27") {hide_awu_form();}
			}
		}
		var awu_lis=$("awu").getElementsByTagName("li");
		for (var i=0, awu_lisLength=awu_lis.length; i<awu_lisLength; i++) {
			if (awu_lis[i].className=="business" || awu_lis[i].className=="business last") {
				awu_lis[i].getElementsByTagName("div")[0].getElementsByTagName("a")[0].onclick=function() {
					var company_name=this.parentNode.parentNode.parentNode.getElementsByTagName("h4")[0];
					if (company_name.getElementsByTagName("a")[0]) company_name=company_name.getElementsByTagName("a")[0];
					$("ems_form").getElementsByTagName("legend")[0].innerHTML="Get a quote from "+company_name.innerHTML;
					var sendto=this.href.indexOf("sendto=")+7;
					$("ems_aid").value=this.href.substring(sendto), this.href.length;
					$("emis_placeholder").appendChild($("ems_form"));
					$("awu_form").style.display="block";
					$("opacity_overlay").style.display="block";
					$("ems_fname").focus();
					return false;
				}
			}
		}
		/*else if ($("hp_estimate")) {
			$("hp_estimate").getElementsByTagName("a")[0].onclick=function() {
				$("awu_form").getElementsByTagName("legend")[0].innerHTML="Get a plumbing quote";
				$("opacity_overlay").style.display="block";
				$("awu_form").style.display="block";
				$("ems_fname").focus();
				return false;
			}
		}*/
		/* data on clicking on "visit website" */
		var ads_up=$("awu").getElementsByTagName("li");
		for (var i=0, l=ads_up.length; i<l; i++) {
			if ((ads_up[i].className=="business" || ads_up[i].className=="business last") && ads_up[i].getElementsByTagName("p")[1].getElementsByTagName("a")[0]) {
				ads_up[i].getElementsByTagName("p")[1].getElementsByTagName("a")[0].onmousedown=function() {
					var link_href=this.parentNode.parentNode.getElementsByTagName("div")[0].getElementsByTagName("div")[0].getElementsByTagName("a")[0].href;
					var sendto=link_href.indexOf("sendto=")+7;
					this.href="/go_to_ad_url.php?eid="+link_href.substring(sendto, link_href.length);
					return true;
				}
				/*ads_up[i].getElementsByTagName("p")[1].getElementsByTagName("a")[0].onclick=function() {
					var link_href=this.parentNode.parentNode.getElementsByTagName("div")[0].getElementsByTagName("div")[0].getElementsByTagName("a")[0].href;
					var sendto=link_href.indexOf("sendto=")+7;
					ajax.collectAdClick(link_href.substring(sendto), link_href.length);
					return true;
				}*/
			}
		}
	}
	if ($("listingsfree")) {
		var phone_spans=$("listingsfree").getElementsByTagName("span");
		for (var i=0, k=phone_spans.length; i<k; i++) {
			phone_spans[i].onclick=function() {
				ajax.getPhone(this.parentNode.parentNode.id.substring(3));
			}
		}
	}
	if ($("search")) {
		if ($("wrap").className!="city_page" && !$("free-plumbing-quote_page") && !$("plumbing-costs_page")) {
			$("n_query").blink_status=true;
			var blink_search=setInterval(function() {
				if ($("n_query").blink_status) $("n_query").className=($("n_query").className=="n_query1")?"n_query2":"n_query1";
				else $("n_query").className="";
			}, 500);
		}
		function findPos(obj) {
			var curleft=curtop=0;
			if (obj.offsetParent) {
				do {
					curleft+=obj.offsetLeft;
					curtop+=obj.offsetTop;
				}
				while (obj=obj.offsetParent);
			}
			return [curleft, curtop];
		}
		var ol_results=document.createElement("ol");
		ol_results.id="results";
		ol_results.style.left=(findPos($("n_query"))[0]+2)+"px";
		ol_results.style.top=(findPos($("n_query"))[1]+$("n_query").offsetHeight-2)+"px";
		$("search").getElementsByTagName("fieldset")[0].insertBefore(ol_results, $("n_query").nextSibling);
		$("n_query").value=($("n_query").value=="")?"Enter zip / city":$("n_query").value;
		$("n_query").onfocus=function() {
			this.blink_status=false;
			this.value=(this.value=="Enter zip / city")?"":this.value;
		}
		$("n_query").onblur=function() {
			this.blink_status=true;
			this.value=(this.value=="")?"Enter zip / city":this.value;
		}
		$("results").style.display="none";
		$("n_query").setAttribute("autocomplete", "off");
		$("n_query").onkeyup=function(e) {
			if (/^\d+$/.test(this.value)) $("n_query").maxLength="5";
			else {
				$("n_query").maxLength="200";
				var keyPressed=(window.event)?event.keyCode:e.keyCode;
				var results=$("results").getElementsByTagName("li");
				var results_length=results.length;
				if (keyPressed=="40" || keyPressed=="38") {
					if ($("results").getElementsByTagName("li").length>0) {
						if (!$("hover")) $("results").getElementsByTagName("li")[0].getElementsByTagName("a")[0].id="hover";
						else {
							for (var i=0; i<results_length; i++) {
								if (results[i].getElementsByTagName("a")[0].id=="hover") var current_hover=i;
							}
							results[current_hover].getElementsByTagName("a")[0].id="";
							if (keyPressed=="40") {
								if (current_hover==results_length-1) results[0].getElementsByTagName("a")[0].id="hover";
								else results[current_hover+1].getElementsByTagName("a")[0].id="hover";
							}
							if (keyPressed=="38") {
								if (current_hover==0) results[results_length-1].getElementsByTagName("a")[0].id="hover";
								else results[current_hover-1].getElementsByTagName("a")[0].id="hover";
							}
						}
						$("n_query").value=$("hover").firstChild.data;
					}
				}
				else {
					for (var i=results_length-1; i>=0; i--) {
						results[i].removeChild(results[i].getElementsByTagName("a")[0]);
						$("results").removeChild(results[i]);
					}
					ajax.perform(this.value);
				}
				/*e.preventDefault();
				e.stopPropagation();*/
			}
		}
		$("results").onkeydown=function(e) {
			var keyPressed=(window.event)?event.keyCode:e.keyCode;
			if (keyPressed=="40" || keyPressed=="38") {
				var results=$("results").getElementsByTagName("li");
				var results_len=results.length;
				for (var i=0; i<results_len; i++) {
					if ($("results").getElementsByTagName("li")[i].getElementsByTagName("a")[0].id=="hover") {
						$("results").getElementsByTagName("li")[i].getElementsByTagName("a")[0].id="";
						if (keyPressed=="38") var new_hover=(i==0)?results_len-1:i-1;
						else var new_hover=(i==results_len-1)?0:i+1;
					}
				}
				$("results").getElementsByTagName("li")[new_hover].getElementsByTagName("a")[0].id="hover";
				//$("hover").focus();
				e.preventDefault();
				e.stopPropagation();
				return false;
			}
		}
		document.getElementsByTagName("body")[0].onclick=function() {
			if ($("results").style.display=="block") $("results").style.display="none";
		}
		$("search").onsubmit=function() {
			if ($("n_query").value=="" || $("n_query").value=="Enter Zip Code" || $("n_query").value=="Enter City & State") return false;
			else return true;
		}
	}
	function hideMoreShowHidden(elms, countMany, removeElm) {
		for (var i=countMany; i<elms.length; i++) {
			elms[i].className="";
		}
		removeElm.parentNode.removeChild(removeElm);
	}
	function collapseMany(elms, countMany, elmType) {
		if (elms.length>countMany) {
			var elmType2Create=(elmType=="li")?"p":"span";
			for (var i=countMany; i<elms.length; i++) elms[i].className="more";
			var more=document.createElement(elmType2Create);
			more.id="more_"+elmType;
			var elmClickOn=(elmType=="li")?document.createElement("p"):more;
			elmClickOn.appendChild(document.createTextNode("More..."));
			//elms[0].parentNode.appendChild(more); IE is stupid and hides pre-created element, so taking a different approach
			elms[0].parentNode.insertBefore(more, elms[countMany]);
			if (elmType=="li") more.appendChild(elmClickOn);
			elmClickOn.onclick=function() {hideMoreShowHidden(elms, countMany, more);}
		}
	}
	if ($("additional_requests")) {
		collapseMany($("additional_requests").getElementsByTagName("li"), 5, "li");
	}
	if ($("services_area_zipcodes")) {
		collapseMany($("services_area_zipcodes").getElementsByTagName("span"), 22, "span");
	}
	if ($("billing_same_as_personal")) {
		if (document.getElementsByName("su_type")[0].checked || document.getElementsByName("su_type")[4].checked) {
			$("fieldset_billing").style.display="none";
			$("fieldset_credit").style.display="none";
		}
		var su_type=document.getElementsByName("su_type");
		for (var i=0, su_typeLength=su_type.length; i<su_typeLength; i++) {
			su_type[i].onclick=function() {
				if (this.value==2 || this.value==3 || this.value==4) {
					$("fieldset_billing").style.display="block";
					$("fieldset_credit").style.display="block";
				}
				else {
					$("fieldset_billing").style.display="none";
					$("fieldset_credit").style.display="none";
				}
			}
		}
		$("billing_same_as_personal").onclick=function() {
			if (this.checked) {
				$("su_billing_first_name").value=$("su_first_name").value;
				$("su_billing_last_name").value=$("su_last_name").value;
				$("su_billing_address1").value=$("su_address1").value;
				$("su_billing_address2").value=$("su_address2").value;
				$("su_billing_city").value=$("su_city").value;
				$("su_billing_state").value=$("su_state").value;
				$("su_billing_zipcode").value=$("su_zipcode").value;
				$("su_billing_email").value=$("su_email").value;
				$("su_billing_business_name").value=$("su_business_name").value;
				$("su_billing_website").value=$("su_website").value;
				$("su_billing_phone_work").value=$("su_phone_work").value;
				$("su_billing_phone_ext").value=$("su_phone_ext").value;
				$("su_billing_phone_home").value=$("su_phone_home").value;
				$("su_billing_fax").value=$("su_fax").value;
			}
		}
	}
	if ($("fun_menu")) {
		$("fun_menu").getElementsByTagName("a")[0].innerHTML="<span class=\"letter_1\">F</span><span class=\"letter_2\">u</span><span class=\"letter_3\">n</span> <span class=\"letter_4\">S</span><span class=\"letter_5\">e</span><span class=\"letter_6\">c</span><span class=\"letter_7\">t</span><span class=\"letter_8\">i</span><span class=\"letter_9\">o</span><span class=\"letter_10\">n</span>";
		$("fun_menu").getElementsByTagName("a")[0].onmouseover=function() {
			this.className="fun_hover";
		}
		$("fun_menu").getElementsByTagName("a")[0].onmouseout=function() {
			this.className="";
		}
	}
	if ($("fun_page")) {
		var jokes_p=$("content").getElementsByTagName("p");
		var jokes_p_len=jokes_p.length;
		var jokes_colors=["#24309c", "#007155", "#007e99", "#d68d00", "#d9144b", "#5c9600"];
		var z=0;
		for (var i=0; i<jokes_p_len; i++) {
			if (jokes_p[i].className=="joke") {
				jokes_p[i].style.color=jokes_colors[z];
				z++;
				if (z==jokes_colors.length) z=0;
			}
		}
	}
	var ajax={
		request:function() {
			if (window.XMLHttpRequest) {return new XMLHttpRequest();}
			else if (window.ActiveXObject) {
				try {return new ActiveXObject("Msxml2.XMLHTTP");}
				catch (e) {
					try {return new ActiveXObject("Microsoft.XMLHTTP");}
					catch (e) {}
				}
			}
			else return false;
		},
		get:function(url, fn, ogirinal_q) {
			http_request=new this.request();
			if (http_request) {
				http_request.onreadystatechange=function() {
					if (http_request.readyState==4) {
						if (http_request.status==200) {
							if (fn) {
								if (http_request.getResponseHeader("Content-Type")=="text/xml;") {
									fn(http_request.responseXML, ogirinal_q);
								} else {
									fn(http_request.responseText, ogirinal_q);
								}
							}
							
						}
					}
				};
				http_request.open("GET", url, true);
				http_request.send(null);
			}
		},
		syncget:function(url, fn, ogirinal_q) {
			http_request=new this.request();
			if (http_request) {
				http_request.onreadystatechange=function() {
					if (http_request.readyState==4) {
						if (http_request.status==200) {
							if (fn) {
								if (http_request.getResponseHeader("Content-Type")=="text/xml;") {
									fn(http_request.responseXML, ogirinal_q);
								} else {
									fn(http_request.responseText, ogirinal_q);
								}
							}
							
						}
					}
				};
				http_request.open("GET", url, false);
				http_request.send(null);
			}
		},
		perform:function(q) {
			if (q.length>0) {
				this.get("http://www.aplumbers.com/search_by_str.php?q="+q, this.handle, q);
			}
			else $("results").style.display="none";
		},
		handle:function(obj, ogirinal_q) {
			if (ogirinal_q==$("n_query").value) {
				var xml=obj.documentElement;
				if (xml.hasChildNodes()) {
					var urls=obj.getElementsByTagName("url");
					var txts=obj.getElementsByTagName("txt");
					var a="";
					var result=obj.getElementsByTagName("result");
					for (var i=0, j=result.length; i<j; i++) {
						var new_li_a=document.createElement("a");
						new_li_a.href=urls[i].firstChild.data;
						new_li_a.appendChild(document.createTextNode(txts[i].firstChild.data));
						new_li_a.onmouseover=function() {
							var results=$("results").getElementsByTagName("li");
							var results_length=results.length;
							for (var i=0; i<results_length; i++) {
								if (results[i].getElementsByTagName("a")[0].id=="hover") results[i].getElementsByTagName("a")[0].id="";
							}
							this.id="hover";
						}
						/*new_li_a.onmouseout=function() {
							this.id="";
						}*/
						var new_li=document.createElement("li");
						new_li.appendChild(new_li_a);
						$("results").appendChild(new_li);
					}
					$("results").style.display="block";
				}
			}
		},
		getPhone:function(id) {
			this.get("http://www.aplumbers.com/phone_by_id.php?id="+id, this.insertPhone, id);
		},
		insertPhone:function(obj, id) {
			var xml=obj.documentElement;
			if (xml.hasChildNodes()) {
				var ad_phone=document.createTextNode(obj.getElementsByTagName("phone")[0].firstChild.data);
				$("ad_"+id).getElementsByTagName("div")[0].insertBefore(ad_phone, $("ad_"+id).getElementsByTagName("span")[0]);
				$("ad_"+id).getElementsByTagName("div")[0].removeChild($("ad_"+id).getElementsByTagName("span")[0]);
			}
		},
		addCities:function(id) {
			var http=(document.location.protocol=="https:")?"https":"http";
			this.get(http+"://www.aplumbers.com/sign-up/cities.php?id="+id, this.insertCities, id);
		},
		insertCities:function(txt, id) {
			var cities=eval(txt);
			var newOL=document.createElement("ol");
			newOL.id="ol_cities_"+id;
			for (i=0, cl=cities.length; i<cl; i++) {
				var newInput=document.createElement("input");
				newInput.type="checkbox";
				newInput.id="ad_city_"+cities[i][0];
				newInput.name="ad_cities[]";
				newInput.value=cities[i][0];
				if ($("total_payment")) {
					newInput.onclick=function() {
						$("total_cities").value=(this.checked)?parseInt($("total_cities").value)+1:parseInt($("total_cities").value)-1;
						update_payment($("total_cities").value);
					}
				}
				var newLabel=document.createElement("label");
				newLabel.setAttribute("for", "ad_city_"+cities[i][0]);
				newLabel.appendChild(document.createTextNode(cities[i][1]));
				var newLI=document.createElement("li");
				newLI.appendChild(newInput);
				newLI.appendChild(newLabel);
				newOL.appendChild(newLI);
			}
			$("ad_cities").appendChild(newOL);
		}/*,
		collectAdClick:function(ad_id) {
			this.syncget("http://www.aplumbers.com/collect_ad_click.php?ad_id="+ad_id);
		}*/
	}
}
function $(id) {return document.getElementById(id);}
function externalLinks() {
	for (var i=0; i<document.links.length; i++) {
		if (document.links[i].getAttribute("rel")=="external") {
			document.links[i].onclick=function() {
				window.open(this.href);
				return false;
			}
		}
	}
}
