var fpResultSet = new Array();
var lastZipEntered;
var fpResponse;

/*

function launchFindPhysician() {
	tooltip('headerArea', 'constructionPopover', 'showoverlay', 50, 100);
}

*/


function launchFindPhysician() {
	if(document.getElementById('fpBorderWrap')) {
		tooltip('headerArea', 'fpBorderWrap', 'showoverlay', 50, 100);
		if(document.getElementById('homepageZipCodeInput') && document.getElementById('homepageDistanceInput')) {
			if(document.getElementById('homepageZipCodeInput').value) {
				zipcode = document.getElementById('homepageZipCodeInput').value;
				if(zipcode == "") {
					fp_throwAlert("empty submit");
				} else {
					distanceIndex = document.locate_searchform.distance.selectedIndex;
					selectObj = document.getElementById("fpSectionA").getElementsByTagName("select").item(0).selectedIndex = distanceIndex;
					if(document.getElementById('homepageDistanceInput')) {
						distance = document.getElementById('homepageDistanceInput');
					}
					fpSubmitZip(zipcode, distance);
				}
			}
		}
	}
	pageTrackergto._trackPageview('/findphysician?launch=true');
	pageTracker._trackPageview('/findphysician?launch=true');
}


function shutdownFindPhysician() {
	var objBody = document.getElementsByTagName("body").item(0);
	Effect.Fade('fpBorderWrap', {duration: 0.25});
	if(document.getElementById('alertMsgContainer')) {
		alertMsgCont = document.getElementById('alertMsgContainer');
		if(alertMsgCont.style.display == "block") {
			alertMsgCont.style.display = "none";
		}
	}
	if(document.getElementById('fpGreenArrowContainer')) {
		objBody.removeChild(document.getElementById('fpGreenArrowContainer'));
	}
	if(document.getElementById('overlayer')) {
		new Effect.Fade(document.getElementById('overlayer'), {duration:0.25});
	}
	if(document.locate_searchform) {
		document.locate_searchform.distance.style.visibility = "visible";
	}
}

function fp_throwAlert(type, address) {
	fp__constructErr();
	switch(type) {
		case "google map not found":
			alertMessage = "The address you selected could not be found.<br />"+address;
			break;
		case "google map failed":
			alertMessage = "google maps failed";
			break;
		case "empty return":
			alertMessage = "The zip code you entered returned no results";
			break;
		case "empty submit":
			alertMessage = "Please enter a valid zip code";
			break;
		case "same zip":
			alertMessage = "You have entered the same zip as before";
			break;
		case "script error":
			alertMessage = "There was an error processing your request, please try again";
	}
	if(alertMessage) {
		alertObj = document.getElementById('alertMsgContainer');
		alertObj.innerHTML = alertMessage;
		tooltip('fpBorderWrap', 'alertMsgContainer', 'show', -100, 20);
		new Effect.Fade('alertMsgContainer', {duration:0.75, delay:4.0});
	}
}

function fpSubmitZip(zip, distance) {
	pageTrackergto._trackPageview('/findphysician?search=true');
	pageTracker._trackPageview('/findphysician?search=true');
	if(document.getElementById('fpGreenArrowContainer')) {
		document.getElementById('fpGreenArrowContainer').style.display = "none";
	}
	document.findPhysicianDynForm.fpZipCode.focus();
	
	if(document.findPhysicianDynForm.fpZipCode.value == "") {
		document.findPhysicianDynForm.fpZipCode.value = document.locate_searchform.zipcode.value;
	}
	
	var pZipCode = document.findPhysicianDynForm.fpZipCode.value;
	var pDistance = document.findPhysicianDynForm.fpDistance.value;
	if(pZipCode.match(/^\d{5}([\-]\d{4})?$/)) {
		if(window.location.href.match(/192.168.99.8/)) {
			url = "/fullterm/findphysician.aspx?zip="+pZipCode+"&distance="+pDistance;
		} else {
			url = "/findphysician.aspx?zip="+pZipCode+"&distance="+pDistance;
		}
		new Ajax.Request(url, { 
			method:'get', 
			onCreate: function(transport) {
				// throw the loading animation
			},
			onSuccess: function(transport) {
				// disable loading animation
				
				// prep response
				document.getElementById('fpBorderWrap').style.width = "602px";
				fpResponse = transport.responseText || "no response text";
				fpLoadResults();
				Effect.BlindDown('fpSectionB', { duration: 0.35 });
			},
			onFailure: function(transport) {
				// throw failure alert
				fp_throwAlert("script error");
			}
		});	
	} else {
		fp_throwAlert("empty submit");
		if(document.getElementById('fpSectionB').style.display != "none" && document.getElementById('fpMapColumn').style.display != "none") {
			document.getElementById('fpMapColumn').style.display = "none";
			Effect.BlindUp('fpSectionB', { duration: 0.15 });
		} else if(document.getElementById('fpSectionB').style.display != "none") {
			Effect.BlindUp('fpSectionB', { duration: 0.15 });
		} else {
			//alert(document.getElementById('fpSectionB').style.display);
			document.getElementById('fpMapColumn').style.display = "none";
			document.getElementById('fpSectionB').style.display = "none";
		}
	}
}


function fp__construct() {
	var objBody = document.getElementsByTagName("body").item(0);
	if(Builder) {
		var fpBorderWrap = Builder.node('div', {id:'fpBorderWrap', style:'position:absolute; display:none; z-index:19999;'}, [
			Builder.node('div', {id:'findPhysicianLayer'}, [
				Builder.node('div', {className:'layerCloseBar'}, [
					Builder.node('a', {href:'javascript:void(0);', onclick:'shutdownFindPhysician(); return false;'}, [
						Builder.node('span', {className:'headerMessage'}, 'Close Button'),
					]),
				]),
				Builder.node('div', {className:'clearBoth'}),
				
				Builder.node('div', {id:'fpLeftColumn'}, [
					Builder.node('div', {id:'fpMainColumn'}, [
						Builder.node('div', {id:'fpSectionA'}, [
							Builder.node('div', {className:'mainHeader'}, [
								Builder.node('span', {className:'mainHeaderA'}, 'Locate a physician'),
								Builder.node('br'),
								Builder.node('span', {className:'mainHeaderB'}, 'Find a physician in your area'),
								Builder.node('div', {className:'clearBoth'}),
							]),
							Builder.node('form', {name:'findPhysicianDynForm', onSubmit:'fpSubmitZip(); return false;'}, [
								Builder.node('div', {className:'formArea'}, [
									Builder.node('div', {className:'zipCodeArea'}, [
										Builder.node('p', {className:'zipCodeText'}, 'ZIP CODE'),
										Builder.node('input', {type:'text', id:'zipCode', name:'fpZipCode', size:'10'}),
									]),
									
									Builder.node('div', {className:'distanceArea'}, [
										Builder.node('p', {className:'distanceText'}, 'DISTANCE'),
										Builder.node('select', {name:'fpDistance', id:'distance'}, [
											Builder.node('option', {value:'10'}, '10 Miles'),
											Builder.node('option', {value:'20'}, '20 Miles'),
											Builder.node('option', {value:'50'}, '50 Miles'),
											Builder.node('option', {value:'100'}, '100 Miles'),
										]),
									]),
									
									Builder.node('div', {className:'distanceArea'}, [
										Builder.node('div', {className:'clearBoth'}),
										Builder.node('div', {className:'btnEnter'}, [
											Builder.node('a', {href:'javascript:void(0);', onclick:'fpSubmitZip(); return false;'}, [
												Builder.node('span', {className:'headerMessage'}, 'Enter'),
											]),
										]),
									]),
									
									Builder.node('div', {className:'clearBoth'}),
								]),
							]),
							Builder.node('div', {className:'clearBoth'}),
						]),
						
						Builder.node('div', {id:'fpSectionB', style:'display:none;'}),
					]),
					Builder.node('div', {id:'fpDisclaimer', style:'margin:10px 0px 0px 0px; padding:0px 0px 0px 0px; width:530px; color:#666666; font-size:10px; line-height:16px;'}, [
						Builder.node('span',  'Hologic does not make any representation with respect to the quality of the physicians or practices listed herein. It merely provides this list of physicians who offer the fetal fibronectin test as a service to consumers. Please note that the list may not be exhaustive.'),
					]),
				]),
				
				Builder.node('div', {id:'fpMapColumn', style:'display:none;'}, [
					Builder.node('div', {id:'mapComment'}, [
						Builder.node('p', {className:'first'}),
						Builder.node('p'),
					]),
					Builder.node('div', {id:'mapCanvas'}, '&nbsp;'),
				]),
				Builder.node('div', {className:'clearBoth'}),
			]),
		]);
		objBody.appendChild(fpBorderWrap);
		
		sandbox = document.getElementsByTagName("form");
		counter = 0;
		for(var x=0; x<sandbox.length; x++) {
			if(sandbox[x].getAttribute("name") == "locate_searchform") { counter++ };
		}
		
		if(document.locate_searchform) {
			document.findPhysicianDynForm.fpZipCode.onchange = function() { fpMatchInput('layerInput'); };
			document.locate_searchform.zipcode.onchange = function() { fpMatchInput('pageInput'); };
			document.findPhysicianDynForm.fpZipCode.value = document.locate_searchform.zipcode.value;
		}
		fp__constructErr();
	}
}

function fpMatchInput(whichObj) {
	switch(whichObj) {
		case "layerInput":
			document.locate_searchform.zipcode.value = document.findPhysicianDynForm.fpZipCode.value;
			break;
		case "pageInput":
			document.findPhysicianDynForm.fpZipCode.value = document.locate_searchform.zipcode.value;
			break;
	}
	return false;
}


function fp__constructErr() {
	var objBody = document.getElementsByTagName("body").item(0);
	if(document.getElementById('alertMsgContainer')) {
		objBody.removeChild(document.getElementById('alertMsgContainer'));
	}
	var alertMsgContainer = Builder.node('div', {id:'alertMsgContainer', style:'display:none; position:absolute; color:#FF0000; font-weight:bold; border:5px solid #C2DCD3; width:275px; background-color:#FFFFFF; padding:10px; z-index:19999;'});
	objBody.appendChild(alertMsgContainer);
}


function fpLoadResults() {
	container = document.getElementById('fpSectionB');	
	
	document.getElementById('fpMapColumn').style.display = "none";
	elements = container.getElementsByTagName("*");
	while(elements.length>0) {
	    container.removeChild(elements[0]);
	}
	
	if(document.locate_searchform) {
		document.locate_searchform.distance.style.visibility = "hidden";
	}
	
	if(fpResponse.match(/\:\:/)) {	
		resultsHandler = fpResponse.split(":;");
		resultHeader = document.createElement("div");
		resultHeader.className = "resultRowContainer bgWhite";
		container.appendChild(resultHeader);
			resultHeaderRow = document.createElement("div");
			resultHeaderRow.className = "resultRow tableHeading";
			resultHeader.appendChild(resultHeaderRow);
				resultHeaderMiles = document.createElement("div");
				resultHeaderMiles.className = "resultMiles";
				resultHeaderMiles.innerHTML = "MILES";
				resultHeaderRow.appendChild(resultHeaderMiles);
				resultHeaderName = document.createElement("div");
				resultHeaderName.className = "resultName";
				resultHeaderName.innerHTML = "NAME";
				resultHeaderRow.appendChild(resultHeaderName);
				resultHeaderLocation = document.createElement("div");
				resultHeaderLocation.className = "resultLocation";
				resultHeaderLocation.innerHTML = "LOCATION";
				resultHeaderRow.appendChild(resultHeaderLocation);
				resultHeaderClear = document.createElement("div");
				resultHeaderClear.className = "clearBoth2";
				resultHeaderRow.appendChild(resultHeaderClear);
		var resultRowContainer = new Array();
		var resultRow = new Array();
		var resultRowData = new Array();
		var resultRowDataP = new Array();
		var resultRowClear = new Array();
		for(var x=0; x<resultsHandler.length-1; x++) {
			if(x < 10) {
				resultRowContainer[x] = document.createElement("div");
				resultRowContainer[x].className = "resultRowContainer";
				modulus = x%2;
				if(modulus == 0 || x == 0) {
					resultRowContainer[x].className = resultRowContainer[x].className+" bgGrey";
				} else {
					resultRowContainer[x].className = resultRowContainer[x].className+" bgWhite";
				}
				container.appendChild(resultRowContainer[x]);
				resultRow[x] = document.createElement("div");
				resultRow[x].className = "resultRow";
				resultRowContainer[x].appendChild(resultRow[x]);
				sandbox = resultsHandler[x].split("::");
				resultRowData[x] = new Array();
				resultRowDataP[x] = new Array();
				for(var i=0; i<sandbox.length-1; i++) {
					switch(i) {
						case 0:
							var myClass = "resultMiles";
							break;
						case 1:
							var myClass = "resultName";
							sandbox[i] = "<a href=\"javascript:void(0);\" onclick=\"fpActivateMap(this);\">"+sandbox[i]+"</a>";
							break;
						case 2:
							var myClass = "resultLocation";
							break;
					}
					resultRowData[x][i] = document.createElement("div");
					resultRowData[x][i].className = myClass;
					if(i == 3) {
						resultRowData[x][i].style.display = "none";
					}
					resultRow[x].appendChild(resultRowData[x][i]);
					resultRowDataP[x][i] = document.createElement("p")
					if(sandbox[i]) {
						resultRowDataP[x][i].innerHTML = sandbox[i];
					}
					resultRowData[x][i].appendChild(resultRowDataP[x][i]);
				}
				resultRowClear[x] = document.createElement("div");
				resultRowClear[x].className = "clearBoth2";
				resultRow[x].appendChild(resultRowClear[x]);
			}
		}
		
		// if applicable, generate pagination
		if(resultsHandler.length > 10) {
			var pagination = Builder.node('div', {style:'text-align:center', id:'paginationContainer'});
			if(parseInt(resultsHandler.length%10) > 0) {
				numPages = parseInt(resultsHandler.length/10)+1;
			} else {
				numPages = parseInt(resultsHandler.length/10);
			}
			var paginationArray = new Array();
			for(var o=0; o<numPages; o++) {
				if(o == 0) {
					paginationArray[o] = Builder.node('div', {style:'margin:3px 2px 0px 2px; width:15px; display:inline; font-weight:bold;'}, o+1);
				} else {
					paginationArray[o] = Builder.node('div', {style:'margin:0px 2px 0px 2px; width:15px; display:inline; font-weight:bold;'}, [
						Builder.node('a', {href:'javascript:void(0);', onclick:'fpReloadResults('+parseInt(o+1)+')'}, o+1),
					]);
				}
				pagination.appendChild(paginationArray[o]);
			}
			container.appendChild(pagination);
		}
	} else {
		fp_throwAlert("empty return");
	}
}

function fpReloadResults(pageNum) {
	container = document.getElementById('fpSectionB');	
	elements = container.getElementsByTagName("*");
	while(elements.length>0) {
	    container.removeChild(elements[0]);
	}
	
	var resultsHandler = fpResponse.split(":;");
	
	startingX = pageNum * 10 - 10;
	endingX = pageNum * 10;
	if(startingX < 0) {
		startingX = 0;
	}
	if(endingX > resultsHandler.length) {
		endingX = resultsHandler.length;
	}
	// alert("pagenum:" + pageNum + " start:" + startingX + " end:" + endingX);
	
	resultHeader = document.createElement("div");
	resultHeader.className = "resultRowContainer bgWhite";
	container.appendChild(resultHeader);
		resultHeaderRow = document.createElement("div");
		resultHeaderRow.className = "resultRow tableHeading";
		resultHeader.appendChild(resultHeaderRow);
			resultHeaderMiles = document.createElement("div");
			resultHeaderMiles.className = "resultMiles";
			resultHeaderMiles.innerHTML = "MILES";
			resultHeaderRow.appendChild(resultHeaderMiles);
			resultHeaderName = document.createElement("div");
			resultHeaderName.className = "resultName";
			resultHeaderName.innerHTML = "NAME";
			resultHeaderRow.appendChild(resultHeaderName);
			resultHeaderLocation = document.createElement("div");
			resultHeaderLocation.className = "resultLocation";
			resultHeaderLocation.innerHTML = "LOCATION";
			resultHeaderRow.appendChild(resultHeaderLocation);
			resultHeaderClear = document.createElement("div");
			resultHeaderClear.className = "clearBoth2";
			resultHeaderRow.appendChild(resultHeaderClear);
	var resultRowContainer = new Array();
	var resultRow = new Array();
	var resultRowData = new Array();
	var resultRowDataP = new Array();
	var resultRowClear = new Array();
	for(var x=startingX; x<endingX; x++) {
		resultRowContainer[x] = document.createElement("div");
		resultRowContainer[x].className = "resultRowContainer";
		modulus = x%2;
		if(modulus == 0 || x == 0) {
			resultRowContainer[x].className = resultRowContainer[x].className+" bgGrey";
		} else {
			resultRowContainer[x].className = resultRowContainer[x].className+" bgWhite";
		}
		container.appendChild(resultRowContainer[x]);
		resultRow[x] = document.createElement("div");
		resultRow[x].className = "resultRow";
		resultRowContainer[x].appendChild(resultRow[x]);
		sandbox = resultsHandler[x].split("::");
		resultRowData[x] = new Array();
		resultRowDataP[x] = new Array();
		for(var i=0; i<sandbox.length-1; i++) {
			switch(i) {
				case 0:
					var myClass = "resultMiles";
					break;
				case 1:
					var myClass = "resultName";
					sandbox[i] = "<a href=\"javascript:void(0);\" onclick=\"fpActivateMap(this);\">"+sandbox[i]+"</a>";
					break;
				case 2:
					var myClass = "resultLocation";
					break;
			}
			resultRowData[x][i] = document.createElement("div");
			resultRowData[x][i].className = myClass;
			if(i == 3) {
				resultRowData[x][i].style.display = "none";
			}
			resultRow[x].appendChild(resultRowData[x][i]);
			resultRowDataP[x][i] = document.createElement("p");
			resultRowData[x][i].appendChild(resultRowDataP[x][i]);
			if(sandbox[i]) {
				resultRowDataP[x][i].innerHTML = sandbox[i];
			}
		}
		resultRowClear[x] = document.createElement("div");
		resultRowClear[x].className = "clearBoth2";
		resultRow[x].appendChild(resultRowClear[x]);
	}
	
	// if applicable, generate pagination
	if(resultsHandler.length > 10) {
		var pagination = Builder.node('div', {style:'text-align:center', id:'paginationContainer'});
		if(parseInt(resultsHandler.length%10) > 0) {
			numPages = parseInt(resultsHandler.length/10)+1;
		} else {
			numPages = parseInt(resultsHandler.length/10);
		}
		var paginationArray = new Array();
		for(var o=0; o<numPages; o++) {
			if(o == pageNum-1) {
				paginationArray[o] = Builder.node('div', {style:'margin:3px 2px 0px 2px; width:15px; display:inline; font-weight:bold;'}, o+1);
			} else {
				paginationArray[o] = Builder.node('div', {style:'margin:0px 2px 0px 2px; width:15px; display:inline; font-weight:bold;'}, [
					Builder.node('a', {href:'javascript:void(0);', onclick:'fpReloadResults('+parseInt(o+1)+')'}, o+1),
				]);
			}
			pagination.appendChild(paginationArray[o]);
		}
		container.appendChild(pagination);
	}
}


function fpActivateMap(obj) {
	fpInit();
	// destroy existing onstates and set new one based on click
	elems = document.getElementById('fpSectionB').getElementsByTagName("div");
	for(var x=0;x<elems.length; x++) {
		elems[x].className = elems[x].className.replace(/onState/, "");
	}
	obj.parentNode.parentNode.parentNode.parentNode.className = obj.parentNode.parentNode.parentNode.parentNode.className+" onState";
	address = obj.parentNode.parentNode.parentNode.childNodes[2].childNodes[0].innerHTML;
	name = obj.innerHTML;
	phone = '';
	
	if(document.getElementById('fpMapColumn').style.display != "block") {
		document.getElementById('fpBorderWrap').style.width = "1004px";
		document.getElementById('fpMapColumn').style.display = "block";
	}
	
	setTimeout("showAddress(address)", 1000);
}





/* Maps stuff */
var map = null;
var geocoder = null;

function fpInit() {
  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("mapCanvas"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	geocoder = new GClientGeocoder();
  }
}

function showAddress(address) {
	geocoder.getLatLng(
	  address,
	  function(point) {
		if (!point) {
		  fp_throwAlert("google map not found", address);
		} else {
		  map.setCenter(point, 13);
		  var marker = new GMarker(point);
		  map.addOverlay(marker);
		  marker.openInfoWindowHtml(address);
		}
	  }
	);
}



function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height and/or width less then height of the viewport
	if(yScroll < windowHeight){ pageHeight = windowHeight; } else { pageHeight = yScroll; }
	if(xScroll < windowWidth){ pageWidth = xScroll; } else { pageWidth = windowWidth; }

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
