var startCnt = 0
var endCnt = 0
var totCnt = 0
var sumSpaces = "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp"


var dte = new Date();
   var iDay = dte.getDate();
    if ( iDay < 10 ) { iDay = "0" + iDay; }
	var iMonth = (dte.getMonth()+1);
	if ( iMonth < 10 ) { iMonth = "0" + iMonth; }
	var myDateDisp = iDay + "/" + iMonth + "/" + dte.getYear();

//generic function to get the value from any field
function getFieldValue(field)
{
   switch(field.type)
   {
      case "text" :
      case "textarea" :
      case "password" :
      case "hidden" :
         return field.value;

      case "select-one" :
         var i = field.selectedIndex;
         if (i == -1)   return "";
         else   return (field.options[i].value == "") ? field.options[i].text : field.options[i].value;

      case "select-multiple" :
         var allChecked = new Array();
         for(i = 0; i < field.options.length; i++)
            if(field.options[i].selected)
               allChecked[allChecked.length] = (field.options[i].value == "") ? field.options[i].text : field.options[i].value;
         return allChecked;

      case "button" :
      case "reset" :
      case "submit" :
         return "";

      case "radio" :
      case "checkbox" :
         if (field.checked) { return field.value; } else { return ""; }
      default :
         if(field[0].type == "radio")
         {
            for (i = 0; i < field.length; i++)
               if (field[i].checked)
                  return field[i].value;

            return "";
         }
         else if(field[0].type == "checkbox")
         {
            var allChecked = new Array();
            for(i = 0; i < field.length; i++)
               if(field[i].checked)
                  allChecked[allChecked.length] = field[i].value;

            return allChecked;
         }
         else
            var str = "";
            for (x in field) { str += x + "\n"; }
            alert("I couldn't figure out what type this field is...\n\n" + field.name + ": ???\n\n\n" + str + "\n\nlength = " + field.length);
         break;
   }
   
   return "";
}

// Start od Ajax
// Initialise globals
var sPath  = location.pathname.split('.nsf')[0] + '.nsf';	
var sBrokerPath  =  'http://' + location.hostname + '/dirb/brokerlocator.nsf';	
var sMessage;

function ajaxCall(url, callback) {

	var req = init();
	req.onreadystatechange = processRequest;
	function init() {
		if (window.XMLHttpRequest) {
			return new XMLHttpRequest();
			}
		else if (window.ActiveXObject) {
			return new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
	function processRequest () {
		if (req.readyState == 4) {
			if (req.status == 200) {
				if (callback) callback(req.responseText);
				}
			}
		}
	this.doGet = function() {
		req.open("GET", url, true);
	     	req.send(null);
		}
	}

function getMessage(agent, city, freeText, pageStart) {
       var path = sBrokerPath + "/" + agent + "?OpenAgent&city=" + city + "&SearchFor=" + freeText + "&PageStart=" + pageStart + "&Date=" + dte ;
	var ai = new ajaxCall(path , returnResult);
	ai.doGet();
}

function returnResult(returnData) {
	var sTemp = returnData.slice( returnData.indexOf( '<body text="#000000">' ) + 22, returnData.indexOf( '</body>' ) );
	document.getElementById('results').innerHTML =  sTemp;
}

function getData(agent, broker) {        
     var path = sBrokerPath + "/" + agent + "?OpenAgent&broker=" + broker;
	var ai = new ajaxCall(path , returnBrokerData);
	ai.doGet();
}

function returnBrokerData(returnData) {
        var sTemp = returnData.slice( returnData.indexOf( '<body text="#000000">' ) + 22, returnData.indexOf( '</end>' ) );
		//NR Get Parms from returned data
		var Title = sTemp.slice( 0, sTemp.indexOf( '~' ) );
		var Details = sTemp.slice( sTemp.indexOf( '~' )+1, sTemp.indexOf( '#' ) );
		var Coordinates = sTemp.slice( sTemp.indexOf( '#' )+1, sTemp.length );
		
try
{		
        //NR Create a new Window
		newwindow=window.open("http://" + location.hostname + "/dirvz/vero/vero.nsf/Content/brokerlocator?OpenDocument","","resizable=1,menubar=0,height=600,width=500"); 
  	    newwindow.document.open();
	   
		vTemp = "<html>" + "\n";
		vTemp = "<head>" + "\n";   
        vTemp = vTemp + "<title>" + Title + "</title>" + "\n"; 		
		vTemp = vTemp + "<script type='text/javascript' src='http://www.google.com/jsapi?key=ABQIAAAAi8EY6YiChxCuQrqlplOawBSdOe2W_tYHjWy90XPWZQWbf02RbRQO2is6AQ34y--zavFbBg0xVE-bkA'>" + "\n";
		vTemp = vTemp + "</script>" + "\n";    
		vTemp = vTemp + "<script type='text/javascript'>" + "\n";
		
		//NR Handles error when flash not installed, required for "Street View"
		vTemp = vTemp + "handleNoFlash = function(errorCode) {" + "\n";
		vTemp = vTemp + " if (errorCode == 603) {" + "\n";
		vTemp = vTemp + "   alert('Error: Flash doesnt appear to be supported by your browser');" + "\n";
		vTemp = vTemp + "   return;" + "\n";
		vTemp = vTemp + "}" + "\n";
		vTemp = vTemp + "}" + "\n";
		
		//NR Function to load the maps and controls
		vTemp = vTemp + "function mapsLoaded() {" + "\n";        
		vTemp = vTemp + "var map = new google.maps.Map2(document.getElementById('map'));" + "\n";        
		vTemp = vTemp + "map.setCenter(new google.maps.LatLng(" + Coordinates + "), 13);" + "\n";   
		vTemp = vTemp + "map.addControl(new GSmallMapControl());" + "\n";
		vTemp = vTemp + "map.addControl(new GMapTypeControl());" + "\n";
		vTemp = vTemp + "map.openInfoWindowHtml(map.getCenter(),'" + Details + "');" + "\n";
		
		//NR Function to load the maps and controls
		vTemp = vTemp + "var fenwayPark = new GLatLng(" + Coordinates + ");" + "\n";
		vTemp = vTemp + "panoramaOptions = { latlng:fenwayPark };" + "\n";
		vTemp = vTemp + "myPano = new GStreetviewPanorama(document.getElementById('pano'), panoramaOptions);" + "\n";
		vTemp = vTemp + "GEvent.addListener(myPano, 'error', handleNoFlash);" + "\n";
		vTemp = vTemp + "}" + "\n"; 
		
		//NR Executes this line that waits until the page loads then excutes the function
		vTemp = vTemp + "google.load('maps', '2', {'callback' : mapsLoaded});" + "\n";

		vTemp = vTemp + "</script>" + "\n";
		vTemp = vTemp + "</head>" + "\n";
		vTemp = vTemp + "<body>" + "\n";
		
		//NR Out put divs
		vTemp = vTemp + "<div id='map' style='width:500px;height:300px'></div>" + "\n";
		vTemp = vTemp + "<div id='pano' style='width:500px;height:300px'></div>" + "\n";
		vTemp = vTemp + "</div>" + "\n";
		vTemp = vTemp + "</body>" + "\n";
		vTemp = vTemp + "</html>" + "\n";
		
		//IE Needs to reload the document	   
	   newwindow.document.write(vTemp); 
	   newwindow.document.close();
	   if(navigator.appName == "Microsoft Internet Explorer") {
           newwindow.location.reload();
		 //document.getElementById('maps').innerHTML =  vTemp;

}
		   }
		   catch(err)
		   {
		   alert("Unable to open a window to display the map.\n Please ensure pop up windows from this site are\n not being blocked by your Browser or Google Toolbar");
		   }

}
function getAllData(agent, city, freeText, pageStart) {
     
      var path = sBrokerPath + "/" + agent + "?OpenAgent&city=" + city + "&SearchFor=" + freeText + "&PageStart=" + pageStart + "&Date=" + dte ;
	//alert(path);
	var ai = new ajaxCall(path , returnAllData);
	ai.doGet();
}

function returnAllData(returnData) {
     var sTemp = returnData.slice( returnData.indexOf( '<body text="#000000">' ) + 22, returnData.indexOf( '</body>' ) );
	 //alert(sTemp);
    xdata = sTemp.substr(0,sTemp.length-2);
    arAll = xdata.split(",");
}

function getFirst() {
var sOput
var rowCnt = 0
sOput = ""
totCnt = arNames.length;
startCnt = 1;
if(totCnt > 20){
  sNav = "<a href='#' onClick='javascript:getNext()'> Next</a> >";
  rowCnt = 20;
      } else{
  sNav = "";
  rowCnt = totCnt
}  
endCnt = rowCnt;
var sHeader = 'Currently showing results ' + startCnt + ' to ' + endCnt + ' of ' + totCnt + '<br/>'
for(i = 0; i < rowCnt; i++) {
   v = (i + 1) + ""; 
   sOput = sOput + '<span>' + v + '. ' + '<a href="#">' + (arNames[i].replace("'","")).replace("'","") + '</a></span><br/>'
  }      
 document.getElementById('results').innerHTML = pNav + sNav + "<br/>" + sHeader + sOput + sNav;
}
  
function getPrev() {
var sOput = ""
if ((startCnt - 20) < 2) {
    startCnt = 1;
    endCnt = 20;
    pNav = "";
      }else{
    pNav = "<&nbsp<a href='#' onClick='javascript:getPrev()'>Previous</a>" + sumSpaces;
 startCnt = startCnt - 20;
  }
if ((startCnt + 20) > totCnt) {
    endCnt = totCnt;
          }else{
     endCnt = startCnt + 19;
  }
   
  sNav = "<a href='#' onClick='javascript:getNext()'>Next</a>&nbsp><br/>";
var sHeader = 'Currently showing results ' + startCnt + ' to ' + endCnt + ' of ' + totCnt + '<br/>'
for(i = startCnt; i < endCnt + 1; i++) {
    v = i + ""; 
	hay =  (arNames[i-1].replace("'","")).replace("'","");
   sOput = sOput + '<span>' + v + '. ' + '<a href="#" onclick="javascript:getBrokerData(\'' + getFieldValue(document.getElementById('citySel')) + "~" + hay + '\')">' + (arNames[i-1].replace("'","")).replace("'","") + '</a></span><br/>'
     //sOput = sOput + '<span>' + v + '. ' + '<a href="#">' + (arNames[i-1].replace("'","")).replace("'","") + '</a></span><br/>';
   }      
document.getElementById('results').innerHTML = pNav + sNav + sHeader + sOput + pNav + sNav;
}

function getNext() {
var sOput = ""
startCnt = startCnt + 20;
if ((endCnt + 20) > totCnt) {
    endCnt = totCnt;
    sNav = "";
      }else{
    sNav = "<a href='#' onClick='javascript:getNext()'>Next</a>&nbsp><br/>";
        endCnt = endCnt + 20;
  }
pNav = "<&nbsp<a href='#' onClick='javascript:getPrev()'>Previous</a>" + sumSpaces;
var sHeader = 'Currently showing results ' + startCnt + ' to ' + endCnt + ' of ' + totCnt + '<br/>'
for(i = startCnt; i < endCnt + 1; i++) {
    v = i + ""; 
	hay =  (arNames[i-1].replace("'","")).replace("'","");
	
   sOput = sOput + '<span>' + v + '. ' + '<a href="#" onclick="javascript:getBrokerData(\'' + getFieldValue(document.getElementById('citySel')) + "~" + hay + '\')">' + (arNames[i-1].replace("'","")).replace("'","") + '</a></span><br/>'
     //sOput = sOput + '<span>' + v + '. ' + '<a href="#">' + (arNames[i-1].replace("'","")).replace("'","") + '</a></span><br/>';
   }      
document.getElementById('results').innerHTML = pNav + sNav + sHeader + sOput + pNav + sNav;
//var NURL = new String(this.location.href + "#Fish");
//alert(NURL);
//window.location.href = NURL;

//this.location.href = "#Fish";
}




