//<script>

//***************************************************************
//FUNCTION:			Class
//DESCRIPTION:      Holds all the functions and variables
//***************************************************************
function CGIS() {
	this.gisReturnXML = "";		//xml object that will be used to parse returned gis calls
	this.errMsg       = "";		//holds error messages
	this.errZipInd    = false;	//indicates whether an error has occurred
	this.errRequired  = false;  //indicates whether required fields are present
	
	this.getGISData       = getGISData;		    // making calls to get county zip or services shops 
	this.parseGISResponse = parseGISResponse;   // parsing the response to see if there are errors
	this.formatGISData    = formatGISData;	    // formats the request data 
	this.parseServices    = parseServices;		// parsing the service shops
}


//***************************************************************
//FUNCTION:			getGISData()
//DESCRIPTION:
//This function will get the county and zip for an inputed address
//The input is the form reference variable so I can populate the values
//when the post call returns
//***************************************************************
function getGISData( address, city, state, zip, county, type ){
	var gisStr = "";
	this.errZipInd = false;
	this.errRequired = false;
				
	//Refresh the list
	idBRList.innerHTML = "";
				
	//Do some execption handling so if an error occurs we can tell our user
	try {
		
		//create posting object and make call to server
//		var oRequest = new ActiveXObject("Microsoft.XMLHTTP");
	    window.status = "Retrieving Data..."
	    
		//Determine the type of command. Create the proper form post string
		switch ( type.toUpperCase() ) {
			case "GETCOUNTYZIP"		:	
				gisStr = formatGISData(address, city, state, zip, county, type);	//getting county zip information
		//		oRequest.open("POST", "/eClaims/inquiry/FindAutoRepairShops/ProcessGIS.asp",false);
		//		oRequest.setRequestHeader ( "Content-Type", "application/x-www-form-urlencoded" );		
				break;
			case "LISTSERVICES"		:	
				gisStr = formatGISData(address, city, state, zip, county, type);	//getting listservices information
		//		oRequest.open("POST", "/eClaims/inquiry/FindAutoRepairShops/ProcessGIS.asp",false);
		//		oRequest.setRequestHeader ( "Content-Type", "application/x-www-form-urlencoded" );		
				break;
			default:	throw new Error("Invalid GIS Command (" + command + "). \nUnable to process your request at this time.");break;
		} 
		window.location.href( "default.asp?" + gisStr );


		//Make sure there is something to send and then make call
		if ( gisStr != "" ) {
		    oRequest.send(gisStr);							//making call to the server
			gisReturnXML = oRequest.responseXML;		//getting response from server	
			parseGISResponse(type, county, zip);		//parsing the return to make sure an error didn't occur
		}
	}
	//Catch any exception that may occur during this process.
	catch(exception) {
	}
	//The will be process either through the try or the catch
	finally{
		//release memory
		delete oRequest;
		//set window status back to no state
		window.status = "";
	}
}


//***************************************************************
//FUNCTION:			parseGISResponse()
//DESCRIPTION:
//This function will do all the error check for the return then call
//the proper fuction based on request. Friendlier message were used.
//***************************************************************
function parseGISResponse( type, county, zip){
	//Check for xml parse error and return error. IF not then get the count and zip.
	if ( gisReturnXML.parseError.errorCode != 0 ){
		//errMsg = "GIS XML error - " + hostReturnXML.parseError.reason;
		errMsg = "We are unable to search for shops at this time. Please try your search again. \nIf the problem persists, select 'Contact Us' to report the problem.";
		throw new Error("GIS XML error - " + gisReturnXML.parseError.reason );
	}
	else if (gisReturnXML.selectSingleNode("//ERROR_REPORT")!=null) {
		//errMsg = gisReturnXML.selectNodes("//ERROR").item(0).selectSingleNode("ERROR_DESC").text;
		//errMsg=gisReturnXML.selectSingleNode("//ERROR_REPORT").text;
		errMsg = "We are unable to search for shops at this time. Please try your search again. \nIf the problem persists, select 'Contact Us' to report the problem.";
		throw new Error(gisReturnXML.selectNodes("//ERROR").item(0).selectSingleNode("ERROR_DESC").text);		
	}	
	else if ( gisReturnXML.documentElement.firstChild.attributes.getNamedItem("STATUS").text == "1" ){
		errMsg = gisReturnXML.documentElement.firstChild.text;
		if (errMsg == "Information Message - No services were found for the search") {
			errMsg = "Currently there are no Blue Ribbon/Premier shops in this local area.";
		} else if (errMsg.indexOf("123 Error") != -1 || errMsg.indexOf("113 Error") != -1 ){
			errMsg = "No shops were found matching your search criteria: " + frmBRShops.efAddress.value + " "+ frmBRShops.efCity.value + ", "+ frmBRShops.efState.value + " "+ frmBRShops.efZip.value+".\n" + "Please re-enter the address information.";
		} else {
			errMsg = "We are unable to search for shops at this time. Please try your search again. \nIf the problem persists, select 'Contact Us' to report the problem.";
		} 
		throw new Error(gisReturnXML.documentElement.firstChild.text);
	} 
	else {
		//Display different types of parsing
		switch (type.toUpperCase()){
			case "GETCOUNTYZIP"		: if (zip.value == "") {zip.value = gisReturnXML.selectSingleNode("GIS/REPLY/ZIP").text;}  break;//populate text boxes
			case "LISTSERVICES"		: parseServices(); break; //show services to the user
		}
	}
}


//***************************************************************
//FUNCTION:			formatGISData()
//DESCRIPTION:
//I will build a query string to send to the server to process.
//Looping through the gis elements sent in
//***************************************************************
function formatGISData(address, city, state, zip, county, type) {	
	var lsLine = "----------------------------------------------------------------------\n";
	var lsError = "";
	var requestString = "";
	var service = "CCAPBRRS blocked";

	//checking to make sure the user has entered all required data
	if (address.value == "") lsError += "* Address\n";
	if (city.value    == "") lsError += "* City\n";
	if (state.value   == "") lsError += "* State\n";
	if (lsError != "") {
		alert("The following field(s) are required:\n" + lsLine + lsError + lsLine);
		if (type.toUpperCase() == "GETCOUNTYZIP" ) g_oGIS.errRequired = true;
		return ( fault );
	} else {
		//Building GIS request
		requestString = "address=" + escape(address.value.replace(/&/g,"and")) + "&city=" + escape(city.value.replace(/&/g,"")) + "&state=" + escape(state.value) +	"&zip=" + escape(zip.value) +  "&county=" + escape(county.value.replace(/&/g,"")) +	"&type=" + escape(type) + "&service=" + escape(service);
		return ( requestString );
	}
	

}


//***************************************************************
//FUNCTION:			parseServices()
//DESCRIPTION:
//This function will parse out the services from the return xml string
//and display them to the client 
//***************************************************************
function parseServices( oXMLGIS ) {
	var curNode
	var i,name;
	var errMsg;
	var htmlString = "";
	var bgCol;
	var distance;
	var numShops = 0;

	//reset services
	idBRList.innerHTML = "";

	//List the services
	htmlString = "<table cellSpacing='0' cellPadding='3' width='98%' border='0'>";
	curNode = gisReturnXML.selectSingleNode("GIS/REPLY/SERVICES");
	for ( i = 0; i < curNode.childNodes.length; i ++ ) {
		distance = curNode.childNodes[i].selectSingleNode("DISTANCE").text;
		if (distance < 60) {
			numShops += 1;
			if ((i%2) == 0) { bgCol = "formbg"; } else { bgCol = ""; }
			htmlString += "<tr class="+bgCol+">" +
							"<td class='textsmall' valign='top'><b>"+(i+1)+".</b></td>" +
							"<td class='textsmall'>"+
								"<b>"+curNode.childNodes[i].selectSingleNode("NAME").text+"</b>"+
								"&nbsp;&nbsp;&nbsp;"+
								"<br /><span class='textsmall'>"+curNode.childNodes[i].selectSingleNode("CONTACT").text+" - Contact</span>"+
								"<br>"+curNode.childNodes[i].selectSingleNode("STREET").text+" "+curNode.childNodes[i].selectSingleNode("CITY").text+" "+curNode.childNodes[i].selectSingleNode("STATE").text+" "+curNode.childNodes[i].selectSingleNode("ZIP").text+
								"<br><br>"+curNode.childNodes[i].selectSingleNode("COMMENT").text+"<br>"+
								"<span class='textsmall'><i>*Distance: "+curNode.childNodes[i].selectSingleNode("DISTANCE").text+"</i></span>"+
							"</td>"+
							"<td class='textsmall' valign='top' nowrap>"+
								"<br>Tel:&nbsp; "+curNode.childNodes[i].selectSingleNode("PHONE").text+"&nbsp; "+
							"</td>" +
						"</tr>";
		}
		
	}
	
	htmlString += "</table>";
	
	if ( numShops == 0 ) {
		//htmlString += "<tr class='formbg'><span class='text'>No shops available.</span></tr>";
		alert("Currently there are no Blue Ribbon/Premier shops in this local area.");
		htmlString = "";
	}
	
	// Display the shop list
	idBRList.innerHTML = htmlString;
	
	//release curNode memory
	delete curNode;
}

//***************************************************************
//FUNCTION:			chkNumeric()
//DESCRIPTION:
//Validate Zip code
//***************************************************************
function chkZipCode(field, desc, len) {
	if (field.value == "") return(true);
	if (field.value.length < len) {
		alert("Please enter a valid Zip Code.");
		field.value="";
		field.select();
		return false;
	}
	var val;
	var errors;
	if (field.value > "") val = new String(field.value);
	else return false;
	var bNum = true;
	for (i=0; i<val.length; i++) {
		if (isNaN(parseInt(val.substr(i,1)))) bNum = false;
	}
	if (!bNum) {
		alert("Please enter a valid Zip Code.");
		field.value="";
		field.select();
		return false;
	}
	return true;
}


//</script>