//declare countylist
var multiple_counties="";
// declare a global  XMLHTTP Request object
var XmlHttpObj;
var myLoc;
// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla
function CreateXmlHttpObj()
{
	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
	try
	{
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpObj = null;
		}
	}
	// if unable to create using IE specific code then try creating for Mozilla (FireFox) 
	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObj = new XMLHttpRequest();
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
// COURSE DROPDOWN MENU
/////////////////////////////////////////////////////////////////////////////////////////////////////
function Load_All_ListingActivity(loc){ 
  multiple_counties="Y";
  Load_ListingActivity(loc);
  
}
// called from onChange or onClick event of the EventID dropdown list
function ReLoad_ListingActivity(loc){
      ClearAllFields(loc);
      Load_ListingActivity(loc);
}
function Load_ListingActivity(loc) 
{
  var requestUrl;
  myLoc=loc;
  // use the following line if using asp
  requestUrl = "/xml_data-location/"+loc;
    
	CreateXmlHttpObj();	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		XmlHttpObj.onreadystatechange = ListingActivityChangeHandler;
		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);		
	}
}
// this function called when state of  XmlHttpObj changes
// we're interested in the state that indicates data has been
// received from the server
function ListingActivityChangeHandler()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			Populate_ListingActivity(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
      alert("There was a problem retrieving the requested information: " + XmlHttpObj.status);
			//alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}
// populate the contents of the Video Div
function Populate_ListingActivity(ListingActivityNode){
//var ListingActivityNode='';
if (ListingActivityNode == null) {
  //NULLIFY
/*
  //General
  document.getElementById("CurrentListings").innerHTML    = 'N/A';
  document.getElementById("PendingListings").innerHTML    = 'N/A';
  document.getElementById("OpenHouses").innerHTML         = 'N/A';
  //YearToDaye
  document.getElementById("NewListingsYear").innerHTML    = 'N/A';
  document.getElementById("SoldListingsYear").innerHTML   = 'N/A';
*/
}
else {
  var CurrentListingsNode  = ListingActivityNode.getElementsByTagName('CurrentListings');
  var PendingListingsNode  = ListingActivityNode.getElementsByTagName('PendingListings');
  var OpenHousesNode       = ListingActivityNode.getElementsByTagName('OpenHouses');
  var NewListingsYearNode  = ListingActivityNode.getElementsByTagName('NewListingsYear');
  var SoldListingsYearNode = ListingActivityNode.getElementsByTagName('SoldListingsYear');
//  var loc=CurrentListingsNode[0].getAttribute('title');
//  alert(loc);
  //General
  document.getElementById("CurrentListings"+myLoc).innerHTML   = GetInnerText(CurrentListingsNode[0]);
  document.getElementById("PendingListings"+myLoc).innerHTML   = GetInnerText(PendingListingsNode[0]);
  document.getElementById("OpenHouses"+myLoc).innerHTML        = GetInnerText(OpenHousesNode[0]);
  //Year To Date
  document.getElementById("NewListingsYear"+myLoc).innerHTML   = GetInnerText(NewListingsYearNode[0]);
  document.getElementById("SoldListingsYear"+myLoc).innerHTML  = GetInnerText(SoldListingsYearNode[0]);
}
if (multiple_counties == "Y") {
  try_the_next(myLoc);
}
}
function ClearAllFields(loc){
  //Show Loading
  document.getElementById('CurrentListings'+loc).innerHTML='<span style="color:#999999;font-size:11px;">Loading...</span>';
  document.getElementById('PendingListings'+loc).innerHTML='<span style="color:#999999;font-size:11px;">Loading...</span>';
  document.getElementById('OpenHouses'+loc).innerHTML='<span style="color:#999999;font-size:11px;">Loading...</span>';
  document.getElementById('NewListingsYear'+loc).innerHTML='<span style="color:#999999;font-size:11px;">Loading...</span>';
  document.getElementById('SoldListingsYear'+loc).innerHTML='<span style="color:#999999;font-size:11px;">Loading...</span>';
}

// returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}

function encodeMyHtml(val) {
//     encodedHtml = escape(encodeHtml.htmlToEncode.value);
     var encodedHtml = escape(val);
     encodedHtml = encodedHtml.replace(/\//g,"%2F");
     encodedHtml = encodedHtml.replace(/\?/g,"%3F");
     encodedHtml = encodedHtml.replace(/=/g,"%3D");
     encodedHtml = encodedHtml.replace(/&/g,"%26");
     encodedHtml = encodedHtml.replace(/@/g,"%40");
     var newVal=encodedHtml;
     //encodeHtml.htmlEncoded.value = encodedHtml;
     return newVal;
   } 

function set_event_day(){
  var ed=document.getElementById("EventDay");
  var sl=document.getElementById("CourseDate");
  ed.value=sl.selectedIndex+1;
}
function htmlentities( s ){
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: htmlentities('Kevin & van Zonneveld');
    // *     returns 1: 'Kevin &amp; van Zonneveld'
 
    var div = document.createElement('div');
    var text = document.createTextNode(s);
    div.appendChild(text);
    return div.innerHTML;
}

function html_entity_decode(str) {
  //jd-tech.net
  var  tarea=document.createElement('textarea');
    tarea.innerHTML = str; return tarea.value;
    tarea.parentNode.removeChild(tarea);
  }
function try_the_next(myLoc){
XmlHttpObj=null;
  if (myLoc == "AllCounties") {
    Load_All_ListingActivity("Androscoggin");
  }
  if (myLoc == "Androscoggin") {
    Load_All_ListingActivity("Aroostook");
  }
  if (myLoc == "Aroostook") {
    Load_All_ListingActivity("Cumberland");
  }
  if (myLoc == "Cumberland") {
    Load_All_ListingActivity("Franklin");
  }
  if (myLoc == "Franklin") {
    Load_All_ListingActivity("Hancock");
  }
  if (myLoc == "Hancock") {
    Load_All_ListingActivity("Kennebec");
  }
  if (myLoc == "Kennebec") {
    Load_All_ListingActivity("Knox");
  }
  if (myLoc == "Knox") {
    Load_All_ListingActivity("Lincoln");
  }
  if (myLoc == "Lincoln") {
    Load_All_ListingActivity("Oxford");
  }
  if (myLoc == "Oxford") {
    Load_All_ListingActivity("Penobscot");
  }
  if (myLoc == "Penobscot") {
    Load_All_ListingActivity("Piscataquis");
  }
  if (myLoc == "Piscataquis") {
    Load_All_ListingActivity("Sagadahoc");
  }
  if (myLoc == "Sagadahoc") {
    Load_All_ListingActivity("Somerset");
  }
  if (myLoc == "Somerset") {
    Load_All_ListingActivity("Waldo");
  }
  if (myLoc == "Waldo") {
    Load_All_ListingActivity("Washington");
  }
  if (myLoc == "Washington") {
    Load_All_ListingActivity("York");
  }
}



