function getMapOffsets() {
	ohmap_x = getPosX($('ohmap_img')) + 8;
	ohmap_y = getPosY($('ohmap_img'));
}
window.onresize = getMapOffsets;
document.onresize = getMapOffsets;

ohmap_counter = 0;
ohmap_client_names = new Array();

function ohmap_usaOnly()
{
	var img = $('ohmap_mapimage')
	for(var i = 14; i >= 0; i--)
	{
		var areas = img.getElementsByTagName('area');
		img.removeChild(areas.item(i));
	}
	ohmap_areaOffset = 15;
}

function in_array(needle, haystack, strict) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
 
    var found = false, key, strict = !!strict;
 
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
 
    return found;
}

function ohmap_showRegion(r,x,y)
{
	
	if(r == 'Russian Federation')
	{
		r = 'Russia';
	}
	else if(r == 'Czech Republic')
	{
		r = 'Czech Rep';
	}
	//var provinces = ['AB', 'BC', 'MB', 'NB', 'NL', 'NT', 'NS', 'NU', 'ON', 'PE', 'QC', 'SK', 'YT'];
	r = r.toUpperCase();
	r =	r.replace(/^\s+|\s+$/g,"");

	var clientAlt;
	
	if(typeof(ohmap_showmenu) == "undefined" || ohmap_showmenu == false)
		return;
	if(typeof(ohmap_y) == "undefined")
		getMapOffsets();
	
	if(typeof(ohmap_region_clientcounts[r]) == "undefined")
	{
		ohmap_region_clientcounts[r] = 0;
	}
	if(typeof(ohmap_region_testcounts[r]) == "undefined")
	{
		ohmap_region_testcounts[r] = 0;
	}
		
	ohmap_counter++;
	var squareY = ohmap_y + y - 35;
	var squareX = ohmap_x + x + 10;
	var arrowY = squareY + 16;
	var arrowX = squareX - 19;
	var thisclient;
	
	
	var d = $('ohmap_details');
	d.style.top = squareY + "px";
	d.style.left = squareX + "px";
	var da = $('ohmap_details_arrow');
	da.style.top = arrowY + "px";
	da.style.left = arrowX + "px";
	var contents = "<strong>Statistics for " + ohmap_region_names[r] + "</strong><br>" +
			ohmap_region_clientcounts[r] + " " + ohmap_type + (ohmap_region_clientcounts[r] == '1' ? "" : "s") + "<br>" +
			ohmap_region_testcounts[r] + " Patient Testimonial" + (ohmap_region_testcounts[r] == '1' ? "" : "s") + 
			"<br/><br/><div style=\"text-align:center\"><a href=\"javascript:ohmap_selectRegion('" + r + "')\"><strong>View all " +ohmap_region_clientcounts[r] + " " 
			+ ohmap_type + (ohmap_region_clientcounts[r] == '1' ? "" : "s") + "</strong></a></div>";
	$('ohmap_headsection').innerHTML = contents;
	contents = "";
	if(ohmap_am)
	{
		if(typeof(ohmap_regions[r]) == "undefined" || ohmap_regions[r].length < 15)
			contents = ohmap_amAddS + "region,"+r+"/" + ohmap_amAddE;
		else
			contents = ohmap_amMaxNotice;
	}
	
	if(typeof(ohmap_regions[r]) != "undefined" && ohmap_regions[r].length > 0)
	{
		contents += "<strong>Featured " + ohmap_type + "s in " + ohmap_region_names[r] + "</strong><br>";
		for(var i = 0; i < ohmap_regions[r].length; i+=5)
		{
			if(i > 0)
				contents += "<div align=\"center\"><img src=\"" + SHARED_IMAGE_DIR + "mod_ohmap/separator-bar.gif\" style=\"height:1px;width:200px\" alt=\"\"></div>";
			var client_id = ohmap_regions[r][i];
			var client_name = ohmap_regions[r][i+1];
			var client_city = ohmap_regions[r][i+2];
			var client_state = ohmap_regions[r][i+3];
			var client_testcount = ohmap_regions[r][i+4];
			
			contents += "<table><tr><td>";
			if(map_type_id == 46)
					clientAlt = "";
			else
				clientAlt = "Click here to view " + client_name + "'s profile";
			clientImg = "";
			if(typeof(ohmap_client_photos[client_id]) == "undefined")
			{
				clientImg = "<img src=\"" + SHARED_IMAGE_DIR + "others/default_avatar_100x100.gif" + "\" style=\"width:40px;height:40px;cursor:pointer\" alt=\"\" onclick=\"ohmap_selectClient(" + client_id + ",'" + r + "')\" title=\"" + clientAlt + "\">";
			}
			else
			{
				clientImg = "<img src=\"" + ohmap_client_photos[client_id].src + "\" style=\"width:40px;height:40px;cursor:pointer\" alt=\"\" onclick=\"ohmap_selectClient(" + client_id + ",'" + r + "')\" title=\"" + clientAlt + "\">";
			}
			
			contents += clientImg + "</td>" + 
				"<td valign=\"top\" style=\"font-size:8pt;color:#666\"><strong style=\"cursor:pointer\" onclick=\"ohmap_selectClient(" + client_id + ",'" + r + "')\" onmouseover=\"this.style.textDecoration='underline'\" onmouseout=\"this.style.textDecoration='none'\">" + client_name + "</strong>";
			if(ohmap_am)
				contents += ohmap_amDelS + "region,"+r+"/" + ohmap_amDelE;
			contents += "<br>" +
				client_city + ", " + client_state + "<br>" +
				client_testcount + " Testimonial" + (client_testcount == 1 ? "" : "s") + "</td></tr></table>";
			ohmap_client_names[client_id] = client_name;
		}
	}
	$('ohmap_detailsection').innerHTML = contents;
	d.style.display = "block";
	da.style.display = "block";
}

function ohmap_incrementCounter()
{
	if(typeof(ohmap_showmenu) == "undefined" || ohmap_showmenu == false)
		return;
	ohmap_counter++;	
}

function ohmap_mouseout()
{
	if(typeof(ohmap_showmenu) == "undefined" || ohmap_showmenu == false)
		return;
	window.setTimeout("ohmap_hideRegion()", 100);
}
function ohmap_hideRegion()
{
	if(ohmap_counter > 0)
		ohmap_counter--;
	if(ohmap_counter == 0)
	{
		$('ohmap_details').style.display="none";
		$('ohmap_details_arrow').style.display="none";
		ohmap_counter = 0;
	}
}
function ohmap_selectRegion(r)
{
	var rpcclient = new xmlrpc_client('xmlremote', WEB_ADDRESS);
	rpcclient.addParam(map_type_id);
	rpcclient.addParam(r);
	if(document.proform && document.proform.surgtype)
		rpcclient.addParam(document.proform.surgtype.options[document.proform.surgtype.selectedIndex].value);
	rpcclient.call('ohmap.registerRegionClick', ohmap_callback);
}
function ohmap_selectClient(c, r)
{
	var rpcclient = new xmlrpc_client('xmlremote', WEB_ADDRESS);
	rpcclient.addParam(map_type_id);
	rpcclient.addParam(c);
	rpcclient.addParam(r);
	rpcclient.call('ohmap.registerClientClick', ohmap_callback);
}
function ohmap_callback(ret)
{
	//suppress annoying JavaScript errors when users move mouse during page load
	var areas = document.getElementsByTagName("area");
	for(var i = 0; i < areas.length; i++)
	{
		areas[i].removeAttribute("onmouseover");
		areas[i].removeAttribute("onmouseout");
	}
	window.location.href=ret;
}

function collapseAdminBox(button, action)
{
	if(action == 'hide')
	{
		new Effect.SlideUp(button);
		button.onclick=function onclick(event){collapseAdminBox(this,'expand');};
		button.src = SHARED_IMAGE_DIR + "buttons/btn_arrowup.gif";
		button.alt = button.title = "Collapse this Box";
	}
	else
	{
		new Effect.SlideDown( button );
		button.onclick=function onclick(event){collapseAdminBox(this,'hide');};
		button.src = SHARED_IMAGE_DIR + "buttons/btn_arrowdown.gif";
		button.alt = button.title = "Expand this box";
	}
}

function editSurgeonCancel(ret)
{
	window.location.href=ret;
}