var popUpLocator = '';
var effectedAlert = "";
var currentZip;
var searchForm;

function search_locations(form){
	searchForm = form;
	var aj = new Ajax("/includes/modules/base/controllers/autoSuggest.php");
	currentZip = searchForm.zip.value;
	//aj.send_request("", "table=zipCodes&field=zip&search=" + currentZip, "show_locations(responseText);");
}

function show_locations(form){
	var groupID = get_cookie("gLID");
	var href = window.location.href + "";
	
	href = href.charAt(href.length - 1) == "/" ? href.substring(0, href.length - 1) : href;
	
	var hrefParts = href.split("/");
	var siteDirectory = hrefParts[hrefParts.length - 1];
	//alert(hrefParts[hrefParts.length - 1]);
	//alert(window.location.href);
	
	searchForm = form;
	showHideDivs('searchResults','','show');
	document.getElementById("searchResults").innerHTML = "";
	document.getElementById("searchWarning").innerHTML = "";
	
	/*if (!groupID || groupID == false) {
		document.getElementById("searchWarning").innerHTML = "Please refresh your page and try again. Thank you.";
	}else*/ 
	if(form.zip.value != "" && form.practice && form.practice.options[form.practice.selectedIndex].value != ""){
		show_searching();
		var aj = new Ajax("/includes/modules/locations/controllers/frontEndList.php");
		aj.send_request("searchResults", "siteDirectory=" + siteDirectory + "&mode=search&zip=" + searchForm.zip.value + "&miles=" + searchForm.miles.value + "&address=" + searchForm.address.value + "&practice=" + form.practice.options[form.practice.selectedIndex].value);
	}else if(form.zip.value != ""){
		show_searching();
		var aj = new Ajax("/includes/modules/locations/controllers/frontEndList.php");
		aj.send_request("searchResults", "siteDirectory=" + siteDirectory + "&mode=search&zip=" + searchForm.zip.value + "&miles=" + searchForm.miles.value + "&address=" + searchForm.address.value);
	}
	else{
		document.getElementById("searchWarning").innerHTML = "Please enter a valid zip code.";
	}
}

function show_name(form){
	var groupID = get_cookie("gLID");

	searchForm = form;
	showHideDivs('searchResults','','show');
	document.getElementById("searchResults").innerHTML = "";
	document.getElementById("searchWarning").innerHTML = "";
	
	if (!groupID || groupID == false) {
		document.getElementById("searchWarning").innerHTML = "Please refresh your page and try again. Thank you.";
	}else if(searchForm.physicianLastName.value == "" && searchForm.physicianFirstName.value == ""){
		document.getElementById("searchWarning").innerHTML = "Please enter part of the first or last name.";
	}else{
		show_searching();
		var aj = new Ajax("/includes/modules/locations/controllers/frontEndList.php");
		aj.send_request("searchResults", "mode=search&physicianLastName=" + searchForm.physicianLastName.value + "&physicianFirstName=" + searchForm.physicianFirstName.value);
	}
}

function admin_list_ajax(searchText){
	searchText = (searchText == "" ? "false" : searchText);
	var aj = new Ajax("/includes/modules/locations/controllers/adminList.php");
	aj.send_request("adminList", "mode=search&searchText=" + searchText);
}

function zip_list_ajax(searchDateStart, searchDateEnd){
	//searchText = (searchText == "" ? "false" : searchText);
	var aj = new Ajax("/includes/modules/locations/controllers/zipSearch.php");
	aj.send_request("adminList", "mode=search&searchDateStart=" + searchDateStart + "&searchDateEnd=" + searchDateEnd);
}

// shows the editInsert window for the given id
function show_edit(id){	
	position_edit();
	
	var aj = new Ajax(urlPath + "/includes/modules/locations/controllers/editInsert.php?am=1");
	aj.send_request("editInsert", "formAction=edit&id=" + id);
}

// creates a pop up for the geocode locator
function pop_up_locator(url){
	if (!popUpLocator.closed && popUpLocator.location) {
			popUpLocator.location.href = url;
	}
	else {
		popUpLocator = window.open(url,'name','height=700,width=750,resizable=1,scrollbars=1');
		if (!popUpLocator.opener) popUpLocator.opener = self;
	}
	if (window.focus) {popUpLocator.focus()}
	
}

// positions the edit insert form to be on the right side of the screen according to the scroll height
function position_edit(){
	var elem = document.getElementById("editInsert");
	elem.innerHTML = "";
	var offset = get_window_offset();

	// adjust differently if scroll height
	if(offset.h < 10){
		off = 0;
	}else{
		off = 75;
	}
	elem.style.marginTop = (offset.h - off) + "px";
}

// puts the geocode in the location form and submits them
function pick_points(lat, lng, exactAddress){
	var form = document.forms["editInsertForm"];
	
	form.latitude.value = lat;
	form.longitude.value = lng;	
	form.exactAddress.value = exactAddress;	
	form.submit();
}

function alert_wait(divID){
	effectedAlert = divID;
	var form = document.forms["uploadForm"];	
	document.getElementById(effectedAlert).innerHTML = "WARNING : Wait for geocode proccessing to be done before taking any more actions.";
	document.getElementById("uploadFormWrapper").style.display = "none";
	show_loading("WARNING : Please wait until all geocodes are found.");
}

function show_searching(){
	effectedAlert = "searchResults";
	show_loading("Searching locations...");
}

function show_loading(message){
	document.getElementById(effectedAlert).innerHTML = "<img src='/images/loader.gif' alt=' '  /><h3 style='display:inline; margin: 0px 0px 0px 10px'>" + message + "</h3>";
}

function hide_wait(){
	var form = document.forms["uploadForm"];
	document.getElementById(effectedAlert).innerHTML = "";
	document.getElementById("recordsEffected").innerHTML = "Completed.";
	document.getElementById("uploadFormWrapper").style.display = "block";
}
