// JavaScript Document
var xmlDoc;
var req;
var xml_file = "/bsd/offices/lib/xml/officeinfo.xml";
var browsertype = '';
var xml_loaded = 0;
var xml_notsupported = 0;
var browserTypeVer = sniffer.getVersion();
// handle onreadystatechange event of req object
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {

         } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
         }
    }
}


function loadXML()
{
if(xml_loaded == 0)
	{
	if (isSafari)
    // branch for native XMLHttpRequest object
   		{
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", xml_file, true);
        req.send(null);
		}
	// code for IE
	else if (isIE)
		{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async=false;
		xmlDoc.load(xml_file);
		browsertype = 'IE';
		xml_loaded = 1;  
		}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation &&	document.implementation.createDocument)
		{
		xmlDoc=document.implementation.createDocument("","",null);
		xmlDoc.load(xml_file);
		browsertype = 'notIE';
		xml_loaded = 1;
		}
 	else if(isOther)
		xml_notsupported = 1;
	}
}

function getparamval(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function getinfo(area)
{
//var office_code = getparamval('o');
var office_code = document.getElementById(area.id).id;
var offices = xmlDoc.getElementsByTagName("office");
var num_offices = offices.length;
var office_info = new Array();
for (var i=0; i < num_offices; i++)
	{
	if(offices[i].getAttribute('code') == office_code)
		{
		for(var j=0;j<offices[i].childNodes.length; j++)
			{
					if(browsertype == 'IE')				
						{
						 var current_node = offices[i].childNodes[j].nodeName;
						 if (current_node == 'ophours')
							current_node = offices[i].childNodes[j].getAttribute('time');
						else
							current_node = offices[i].childNodes[j].text;
						office_info[j] = current_node;
						}
					else
						if(offices[i].childNodes[j].nodeType == 1)
						{
						 var current_node = offices[i].childNodes[j].nodeName;
						 if (current_node == 'ophours')
							current_node = offices[i].childNodes[j].getAttribute('time');
				 		else
							current_node = offices[i].childNodes[j].textContent;
						office_info[j] = current_node;
						}
			}
		var name_loc = office_info.length;
		office_info[name_loc] = offices[i].getAttribute('name');
//		alert(office_info[name_loc]+":"+name_loc);
		}
	}

return office_info;
}

function getinfo2(area)
{
//var office_code = getparamval('o');
var office_code = document.getElementById(area.id).id;
var offices = req.responseXML.getElementsByTagName("office");
var num_offices = offices.length;
var office_info = new Array();
for (var i=0; i < num_offices; i++)
	{
	if(offices[i].getAttribute('code') == office_code)
		{
		for(var j=0;j<offices[i].childNodes.length; j++)
			{
					if(browsertype == 'IE')				
						{
						 var current_node = offices[i].childNodes[j].nodeName;
						 if (current_node == 'ophours')
							current_node = offices[i].childNodes[j].getAttribute('time');
						else
							current_node = offices[i].childNodes[j].text;
						office_info[j] = current_node;
						}
					else
						if(offices[i].childNodes[j].nodeType == 1)
						{
						 var current_node = offices[i].childNodes[j].nodeName;
						 if (current_node == 'ophours')
							current_node = offices[i].childNodes[j].getAttribute('time');
				 		else
							current_node = offices[i].childNodes[j].textContent;
						office_info[j] = current_node;
						}
			}
		var name_loc = office_info.length;
		office_info[name_loc] = offices[i].getAttribute('name');
//		alert(office_info[name_loc]+":"+name_loc);
		}
	}

return office_info;
}


function format_telephone(phone)
{
var f_phone = '(' + phone.substr(0,3) + ') ' + phone.substr(3,3) + '-' + phone.substr(6,4);
//alert(f_phone);
return f_phone
}

function test(area, mode)
{

if(xml_notsupported != 1)
{
if(mode == 'over')
	{
	msg = '';
	var coords = document.getElementById(area.id).coords;
	var ar_coords = coords.split(",");
	var x_offset = parseInt(ar_coords[0]);
	x_offset += 190;
	if (x_offset > 583)
		x_offset = 503;
	else if (isIE)
		x_offset -= 2;
	else if (isFF)
		x_offset -= 8;
	else if (isOpera)
		x_offset -=9;
	else if (isSafari)
		x_offset -= 8;
	else if (isOther)
		x_offset -=4;
	var y_offset = parseInt(ar_coords[1]);
	y_offset -= 0;
	if(isIE)
		y_offset += 55;
	else if(isFF)
		y_offset += 55;
	else if(isOpera)
		y_offset += 52;
	else if(isSafari)
		y_offset +=50;
	else if(isOther)
		y_offset += 10;
//	alert(x_offset + ":" + y_offset);
	document.getElementById('i_office_info').style.left = x_offset + "px";
	document.getElementById('i_office_info').style.top = y_offset + "px";
	document.getElementById('i_office_info').style.display='block';
	var office_info = new Array();
//	office_info = getinfo(area);
	if(browsertype == 'IE')
		{
		office_info = getinfo(area);
		var arLen = office_info.length - 1;//last item in array
		var office_name = office_info[arLen];
		var office_street = office_info[0];//first item in array
		var office_city = office_info[1];//second item in array
		var office_hours1 = office_info[3];//fourth item in array
		office_hours1 = office_hours1.replace(/ through /,"-");
		var office_hours2 = '';
		var office_telephone = '';
		re = /^\d*$/;
		var test = re.test(office_info[4]);//possibly fith item in array - test for all numeric - if it fails then its hours, else its a phone number
		if(!test)
			{
			office_hours2 = office_info[4];
			office_hours2 = office_hours2.replace(/ through /,"-");
			office_telephone = format_telephone(office_info[5]);
			}
		else
			{
			office_hours2 = '';
			office_telephone = format_telephone(office_info[4]);
			}
		}
	else if (isSafari)
		{
		office_info = getinfo2(area);
		var arLen = office_info.length - 1;//last item in array
		var office_name = office_info[arLen];
		var office_street = office_info[1];//first item in array
		var office_city = office_info[3];//second item in array
		var office_hours1 = office_info[7];//fourth item in array
		office_hours1 = office_hours1.replace(/through/,"-");
		var office_hours2 = '';
		var office_telephone = '';
		re = /^\d*$/;
		var test = re.test(office_info[9]);//possibly fith item in array - test for all numeric - if it fails then its hours, else its a phone number
		if(!test)
			{
			office_hours2 = office_info[9];
			office_hours2 = office_hours2.replace(/ through /,"-");
			office_telephone = format_telephone(office_info[11]);
			}
		else
			{
			office_hours2 = '';
			office_telephone = format_telephone(office_info[9]);
			}
		}
	else
		{
		office_info = getinfo(area);
		var arLen = office_info.length - 1;//last item in array
		var office_name = office_info[arLen];
		var office_street = office_info[1];//first item in array
		var office_city = office_info[3];//second item in array
		var office_hours1 = office_info[7];//fourth item in array
		office_hours1 = office_hours1.replace(/ through /,"-");
		var office_hours2 = '';
		var office_telephone = '';
		re = /^\d*$/;
		var test = re.test(office_info[9]);//possibly fith item in array - test for all numeric - if it fails then its hours, else its a phone number
		if(!test)
			{
			office_hours2 = office_info[9];
			office_hours2 = office_hours2.replace(/ through /,"-");
			office_telephone = format_telephone(office_info[11]);
			}
		else
			{
			office_hours2 = '';
			office_telephone = format_telephone(office_info[9]);
			}
		}

	div = document.getElementById("name");
    div.innerHTML = "";
    // blast new HTML content into "details" <div>
    div.innerHTML = office_name;
//	document.getElementById('name').innerHTML = office_name;
	div = document.getElementById("street");
    div.innerHTML = "";
    // blast new HTML content into "details" <div>
    div.innerHTML = office_street;

//	document.getElementById('street').innerHTML = office_street;
	div = document.getElementById("city");
    div.innerHTML = "";
    // blast new HTML content into "details" <div>
    div.innerHTML = office_city;
//	document.getElementById('city').innerHTML = office_city;
	div = document.getElementById("hours");
    div.innerHTML = "";
    // blast new HTML content into "details" <div>
    div.innerHTML = office_hours1 + '\n' + office_hours2;
//	document.getElementById('hours').innerHTML = office_hours1 + '\n' + office_hours2;
	div = document.getElementById("telephone");
    div.innerHTML = "";
    // blast new HTML content into "details" <div>
    div.innerHTML = office_telephone;
//	document.getElementById('telephone').innerHTML = office_telephone;
	}
if(mode == 'out')
	{
		document.getElementById('i_office_info').style.display='none';
		document.getElementById('name').innerHTML = '';
		document.getElementById('street').innerHTML = '';
		document.getElementById('city').innerHTML = '';
		document.getElementById('hours').innerHTML = '';
		document.getElementById('telephone').innerHTML = '';
	}
}
}
var isSafari = /Safari/.test(browserTypeVer);
var isIE = /IE/.test(browserTypeVer);
var isFF = /FireFox/.test(browserTypeVer);
var isOpera = /Opera/.test(browserTypeVer);
if (!isSafari && !isIE && !isFF & !isOpera)
	var isOther = true;