﻿//-----------------------------------------------------------------------------
// Define some constants.
//-----------------------------------------------------------------------------

// Define a list of Microsoft XML HTTP ProgIDs.
var XMLHTTPREQUEST_MS_PROGIDS = new Array(
	"Msxml2.XMLHTTP.7.0",
	"Msxml2.XMLHTTP.6.0",
	"Msxml2.XMLHTTP.5.0",
	"Msxml2.XMLHTTP.4.0",
	"MSXML2.XMLHTTP.3.0",
	"MSXML2.XMLHTTP",
	"Microsoft.XMLHTTP"
);

// Define ready state constants.
var XMLHTTPREQUEST_READY_STATE_UNINITIALIZED = 0;
var XMLHTTPREQUEST_READY_STATE_LOADING       = 1;
var XMLHTTPREQUEST_READY_STATE_LOADED        = 2;
var XMLHTTPREQUEST_READY_STATE_INTERACTIVE   = 3;
var XMLHTTPREQUEST_READY_STATE_COMPLETED     = 4;

//-----------------------------------------------------------------------------
// Returns an XMLHttpRequest object.
//-----------------------------------------------------------------------------
function getXMLHttpRequest()
{
	var httpRequest = null;

	// Create the appropriate HttpRequest object for the browser.
	if (window.XMLHttpRequest != null)
		httpRequest = new window.XMLHttpRequest();
	else if (window.ActiveXObject != null)
	{
		// Must be IE, find the right ActiveXObject.
		var success = false;
		for (var i = 0; i < XMLHTTPREQUEST_MS_PROGIDS.length && !success; i++)
		{
			try
			{
				httpRequest = new ActiveXObject(XMLHTTPREQUEST_MS_PROGIDS[i]);
				success = true;
			}
			catch (ex)
			{}
		}
	}

	// Display an error if we couldn't create one.
	if (httpRequest == null)
		alert("Error in HttpRequest():\n\nCannot create an XMLHttpRequest object.");

	// Return it.
	return httpRequest;
}

function BusinessLookup(intBusinessID)
{
	/** Purpose:	Uses the xmlHttp object to get the information on a business "instantly"
	  * Params:		intBusinessID			- BusinessID to lookup
	  * Returns:	XML
	  * Devel:		LH 2/7/2007
	  */
	var objBusinessLookup = getXMLHttpRequest();


    // Make a request to get the matching city and state.
    //var url = "city_state.asp?zipCode=" + zipCode;
    var url = "/services/businesses.asp?business=" + intBusinessID;
   
    objBusinessLookup.open("GET", url, false);
    objBusinessLookup.send(null);
  	
	//alert(objBusinessLookup.responseXML.parseError.reason);
	//return objBusinessLookup.responseXML;
	
	//return new XMLDoc(objBusinessLookup.ResponseXML, "");
    //return objBusinessLookup.ResponseXML;
    
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async = "false";
	xmlDoc.loadXML(objBusinessLookup.responseText);
	return xmlDoc;
 }
 
 function XMLLookup(strURL)
 {
	/** Purpose:	Uses the xmlHttp object to get the information on a business "instantly"
	  * Params:		strURL			- URL to get XML from
	  * Returns:	XML
	  * Devel:		LH 2/28/2007
	  */
	var objLookup = getXMLHttpRequest();


    objLookup.open("GET", strURL, false);
    objLookup.send(null);
  	
	  
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async = "false";
	xmlDoc.loadXML(objLookup.responseText);
	return xmlDoc;
 }

function ZipCodeLookup(strZipCode)
{
	/** Purpose:	Gets the city/state associated with a particular zip code
	  * Params:		strZipCode
	  * Returns:	STRING
	  * Devel:		LH 2/7/2007
	  */
	  
	var url = "/services/zipcodes.asp?action=CityStateAbbr(" + strZipCode + ",false)";
	
	var objLookup = getXMLHttpRequest();
	objLookup.open("GET", url, false);
	objLookup.send(null);
	
	return RemoveHTML(objLookup.responseText);
	
}

function RemoveHTML(strText) {
    // Purpose:          Parses a string to remove HTML code
    // Params:          strText                                                                          - String - Text to format
    // Returns:          String                                                                            - Stripped text
    // Devel:  LGS 6/10/2004 CMH 161/22/2006
               
    tmpData = "";
    if (TrimAll(strText) != "") {
        tmpText = strText;
        tmpText.replace(/<BR>/, "\n");
        tmpText.replace(/<br>/, "\n");
        tmpText.replace(/<\/P>/, "\n");
        tmpText.replace(/<\/p>/, "\n");
        isHTML = "False";         // Flag to indicate we're currently inside an HTML tag
        for (intI = 0; intI < tmpText.length; intI++) {       
            if (tmpText.substring(intI,intI+1) == "<") {
                        isHTML = "True";                                                         // Starting a new HTML tag. Don't add this to the string.
            } else if (tmpText.substring(intI,intI+1) == ">") {
                        isHTML = "False";                                                        // Ending an HTML tag. Don't add this to the string. Next character will be added.                                                          
            } else if (isHTML == "False") {
                tmpData = tmpData + tmpText.substring(intI,intI+1);
            }                                                                
        }       
        //tmpData = tmpData.replace(/\/n/, "<BR>");
    }
    
    return tmpData;
}
            
function TrimAll(sString) 
{
    while (sString.substring(0,1) == ' ')
        {
        sString = sString.substring(1, sString.length);
        }
    while (sString.substring(sString.length-1, sString.length) == ' ')
        {
        sString = sString.substring(0,sString.length-1);
        }
    return sString;
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function toggleLayer(whichLayer)
{

    if (document.getElementById)
    {
        // this is the way the standards work
        var style2 = document.getElementById(whichLayer).style;

        if (style2.display == "none") {
            style2.display = "block"
        } else { style2.display = "none"; }
    }
    else if (document.all)
    {
        // this is the way old msie versions work
        var style2 = document.all[whichLayer].style;
        style2.display = style2.display? "":"block";
    }
    else if (document.layers)
    {
        // this is the way nn4 works
        var style2 = document.layers[whichLayer].style;
        style2.display = style2.display? "":"block";
    }
    return style2.display;
}

function InZip(zip) {
    if (zip.value.length == 5) { 
        InitiateCityStateLookup(zip);
    }
}

