// clear form values
<!--
function clearDefault(el) {
   if (el.defaultValue==el.value) el.value = ""
}

function openDealerMap(url) {
    var map = window.open(url,'dealermap','scrollbars=yes,resizable=yes,menubar=yes,width=675,height=525');
    map.focus();
}

// -->

function fnLoadPngs() {
   if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5 &&  Number(rslt[1]) < 7);

	for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) {
		if (itsAllGood && img.src.match(/\.png$/i) != null) {
			var src = img.src;
			var div = document.createElement("DIV");
			div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizing='crop')"
			div.style.width = img.width + "px";
			div.style.height = img.height + "px";
			div.style.textindent = 0;
			img.replaceNode(div);
		}
	}
   }
}

var ajaxUrl = "";
var searchZip = "";
var searchBrand = "";
var searchRadius = "";
var searchSortBy = "";
var searchFilter = "";
var searchType = "";

function ajaxRefresh(newSearchType) {
   // active dealer results div
   //window.scrollTo(0,0);
   //document.getElementById("dealerresultsloading").style.display = "block";
   //document.getElementById("dealerresultswrapper").style.display = "none";
   searchSortBy = document.getElementById("searchSortBy").value;
   //searchRadius select object must exist
   searchRadius=document.getElementById("searchRadius").value;
   if (newSearchType != null && newSearchType != '') {
	   searchType = newSearchType;
   }
//alert("in:ajaxRefresh:\n searchType:" + searchType + "\n searchRadius:" + searchRadius);
    doAjaxRefresh();
   //ajaxRequest("dealersearchresults", "generatorDealerSearchMessage", "searchRadiusUsed");	
}

function doAjaxRefresh() {
   // active dealer results div
   window.scrollTo(0,0);
   document.getElementById("dealerresultsloading").style.display = "block";
   document.getElementById("dealerresultswrapper").style.display = "none";
   //searchSortBy=document.getElementById("searchSortBy").value;
   //searchRadius select object must exist
   //searchRadius=document.getElementById("searchRadius").value;
   //if (newSearchType != null && newSearchType != '') {
//	   searchType = newSearchType;
  // }
   ajaxRequest("dealersearchresults", "generatorDealerSearchMessage", "searchRadiusUsed");	
}

function clearPageState()
{
    document.getElementById("pageState").innerHTML = "";
} // endfunction

function updatePageState(message)
{
    var pageState = document.getElementById("pageState");
    pageState.innerHTML += message;
} // endfunction

function submitted()
{
    //alert("in:submitted");
    var sr = document.getElementById("notFoundSearchRadius");
    searchZip = document.getElementById("notFoundZipcode").value;
    var s = "zipcode=" + document.getElementById("notFoundZipcode").value + ", sr.options.length=" + sr.options.length;
    for (var i = 0; i < sr.options.length; i++) 
    {
        s += "," + i + ":" + sr.options[i].selected;
      if (sr.options[i].selected)
    {
      searchRadius = sr.options[i].value;
      break;
    } // endif
    } // endfor
   document.getElementById("searchZipContainer").innerHTML = searchZip;

    //alert(s);
    doAjaxRefresh();
    return false;
} // endfunction

function getDealerResults(showShortTitle) {
	   var fapLogo = "<a id=\"fapLogo\" href=\"/dealers/authorizedparts.shtml\">"
		   + "<img src=\"/images/content/icn-deal-fapask.gif\" "
		   + "alt=\"Factory Authorized Parts\" />"
		   + "</a>"; 
	   //fapLogo = "";

   // get params
   var FORM_DATA = createRequestObject();
   searchZip = FORM_DATA["searchZip"];
   if(FORM_DATA["searchFilter"] != undefined) {
   	searchFilter = FORM_DATA["searchFilter"];
   }
   searchBrand = "BR";
   var title;

   // check for home page zip
   if(FORM_DATA["zipcode"]) {
      searchZip = FORM_DATA["zipcode"];
   }

   //check the zip form data value if there is no searchZip value
   if (searchZip == null || searchZip == "") {
	   searchZip = FORM_DATA["zip"];
   }
    var searchZipContainer = "<span id=\"searchZipContainer\">" + searchZip + "</span>";
   // set form url
   document.getElementById("sortByForm").action = this.location;
   document.getElementById("searchZip").value = searchZip;
   document.getElementById("searchFilter").value = searchFilter;

   // generator search 
   if(searchFilter == "Generator") {
	   // reset radius
	   document.getElementById("searchRadius").options.length = 0;
	   document.getElementById("searchRadius").options[0] = new Option('50','50');
	   document.getElementById("searchRadius").options[1] = new Option('100','100');
	   document.getElementById("searchRadius").options[2] = new Option('200','200');

	  if (showShortTitle) {
		  title = "<h1>Dealers In Your Area &mdash; " + searchZipContainer + "</h1>";
	  } else {
		  title = "<h1>Bryant Home Generator Dealer Locator</h1>"
		  + fapLogo + "<h1>Dealers In Your Area &mdash; " + searchZipContainer + "</h1>";
	  }

      searchRadius = "50";
   // FAD
   } else if(searchFilter == "FADOnly") {
	  if (showShortTitle) {
		  title = "<h1>Dealers In Your Area &mdash; " + searchZipContainer + "</h1>";
	  } else {
		  title = "<h1>Bryant Factory Authorized Dealer Locator</h1>"
			  + fapLogo + "<h1>Dealers In Your Area &mdash; " + searchZipContainer + "</h1>";
	  }
      
       searchRadius = "25";
   // normal
   } else {
	  if (showShortTitle) {
		  title = "<h1>Dealers In Your Area &mdash; " + searchZipContainer + "</h1>";
	  } else {
		  title = "<h1>Bryant Dealer Locator</h1>"
		  + fapLogo + "<h1>Dealers In Your Area &mdash; " + searchZipContainer + "</h1>";
	  }
      searchRadius = "25";
   }
 
   document.getElementById("dealerresultswrapper").innerHTML = title +  document.getElementById("dealerresultswrapper").innerHTML;

   ajaxRequest('dealersearchresults', 'generatorDealerSearchMessage', 'searchRadius'); 

   // set form url
   document.getElementById("sortByForm").action = this.location;
   if (searchFilter != "Generator") {
	   document.getElementById("searchRadius").value = searchRadius;
   }
}

function ajaxRequest(divId, generatorMessageDivId, searchRadiusUsedDivId)
{ 
   // get the results
   ajaxUrl = '/apps/dealerlocatorws.jsp?searchZip=' + searchZip + '&searchBrand=' + searchBrand + '&searchRadius=' + searchRadius;
   if((searchSortBy != undefined) && (searchSortBy != "Please Select")) {
	   ajaxUrl += "&searchSortBy=" + searchSortBy;
   }
   if((searchFilter != undefined) && (searchFilter != "undefined")) {
	   ajaxUrl += "&searchFilter=" + searchFilter;
   }
   if((searchType != undefined) && (searchType != "undefined")) {
	   ajaxUrl += "&searchType=" + searchType;
   }
   //alert("ajaxUrl=" + ajaxUrl);
    var xhr; 
    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();   }
          catch (e3) {  xhr = false;  }
        }
     }
  
    xhr.onreadystatechange  = function()
    { 
         if(xhr.readyState  == 4)
         {
              if(xhr.status  == 200) {
		  if(xhr.responseText.match(/Sorry, there are no dealers that match this criteria/)) 
          {
            //alert("No Dealers Found! searchZip=" + searchZip);
		  	document.getElementById("sortLabel").style.display = "none";
			//if(searchFilter != "Generator") {
				document.getElementById("searchRadiusLabel").style.display = "none";
			//}

			//another IE hack because IE is a terrible browser. This time it was messing up a simple select box.
			//All these hacks make it very finicky. But it works.
			if(searchFilter != "Generator") {
				var node = document.getElementById(divId);
				var parent = node.parentNode;
				//parent.appendChild(node);
			}
		  } 
            else
            {
                //alert("Dealers Found, searchRadius:" + searchRadius);
                setSelectedIndex(document.getElementById("searchRadius"),searchRadius);
				document.getElementById("searchRadiusLabel").style.display = "block";
    		  	document.getElementById("sortLabel").style.display = "block";

            } // endif

		  // populate div with dealer results
		  removeChildren(document.getElementById(divId));
		  var newDiv = document.createElement("div");
            //clearPageState();
            //updatePageState(xhr.responseText);
		  newDiv.innerHTML = xhr.responseText;
		  
		  document.getElementById(divId).appendChild(newDiv);
		  changeDollarSign(newDiv);
		  // extract the actual search radius used and the generator dealer locator message
		  if(searchFilter == "Generator") {
			  // this is how to extract the generator dealer message (if there is one)
			  if(document.getElementById('noGeneratorDealerMessage')) {
				  var noGeneratorDealerMessage = document.getElementById('noGeneratorDealerMessage').innerHTML;
				 
				  //alert('noGeneratorDealerMessage=' + noGeneratorDealerMessage);

				  if (noGeneratorDealerMessage != null && noGeneratorDealerMessage != '') {
					  document.getElementById(generatorMessageDivId).innerHTML = noGeneratorDealerMessage;
				  } else {
					  document.getElementById(generatorMessageDivId).innerHTML = '';
				  }
			  }
			  
				//update the 'searchRadius' select box, if it exists, to show the actual search radius used
				if (document.getElementById(searchRadiusUsedDivId) != null) {
                    //alert("found:" + searchRadiusUsedDivId);
					var searchRadiusUsed = document.getElementById(searchRadiusUsedDivId).innerHTML;
					var searchRadiusSelect = document.getElementById('searchRadius');
					if (searchRadiusSelect != null) {
						//go through each option and if it's value == searchRadiusUsed, set that as the select's selectedIndex
						for (index = 0; index < searchRadiusSelect.length; index++) {
							if (searchRadiusSelect[index].value == searchRadiusUsed) {
								searchRadiusSelect.selectedIndex = index;
							}
						}
					}
				}

		  }
      
		  // active dealer results div
		  document.getElementById("dealerresultsloading").style.display = "none";
		  document.getElementById("dealerresultswrapper").style.display = "block";
              } else {
                 //alert("Error code " + xhr.status);
		  document.getElementById(divId).innerHTML += "<p>Our Find a Dealer tool is currently experiencing technical difficulties, please try your search again.</p>";

		  // active dealer results div
		  document.getElementById("dealerresultsloading").style.display = "none";
		  document.getElementById("dealerresultswrapper").style.display = "block";
	      }
         }
    }; 
   xhr.open("GET", ajaxUrl,  true); 
   xhr.send(null);
} 

function removeChildren(element) {
	  var children = element.childNodes;
	  var i = children.length;
	  while (i-- > 0) {
		  children[i].parentNode.removeChild(children[i]);
	  }
}

function createRequestObject() {
  
  FORM_DATA = new Object();
    // The Object ("Array") where our data will be stored.
  
  separator = ',';
    // The token used to separate data from multi-select inputs
  
  query = '' + this.location;
    // Get the current URL so we can parse out the data.
    // Adding a null-string '' forces an implicit type cast
    // from property to string, for NS2 compatibility.
    
  query = query.substring((query.indexOf('?')) + 1);
    // Keep everything after the question mark '?'.
  
  if (query.length < 1) { return false; }  // Perhaps we got some bad data?
  
  keypairs = new Object();
  numKP = 1;
    // Local vars used to store and keep track of name/value pairs
    // as we parse them back into a usable form.
    
  while (query.indexOf('&') > -1) {
    keypairs[numKP] = query.substring(0,query.indexOf('&'));
    query = query.substring((query.indexOf('&')) + 1);
    numKP++;
      // Split the query string at each '&', storing the left-hand side
      // of the split in a new keypairs[] holder, and chopping the query
      // so that it gets the value of the right-hand string.
  }

  keypairs[numKP] = query;
    // Store what's left in the query string as the final keypairs[] data.
  
  for (i in keypairs) {
    keyName = keypairs[i].substring(0,keypairs[i].indexOf('='));
      // Left of '=' is name.
    keyValue = keypairs[i].substring((keypairs[i].indexOf('=')) + 1);
      // Right of '=' is value.
    while (keyValue.indexOf('+') > -1) {
      keyValue = keyValue.substring(0,keyValue.indexOf('+')) + ' ' + keyValue.substring(keyValue.indexOf('+') + 1);
        // Replace each '+' in data string with a space.
    }
    
    keyValue = unescape(keyValue);
      // Unescape non-alphanumerics
      
    if (FORM_DATA[keyName]) {
      FORM_DATA[keyName] = FORM_DATA[keyName] + separator + keyValue;
        // Object already exists, it is probably a multi-select input,
        // and we need to generate a separator-delimited string
        // by appending to what we already have stored.
    } else {
      FORM_DATA[keyName] = keyValue;
        // Normal case: name gets value.
    }
  }

  return FORM_DATA;
}

function changeDollarSign(resultDiv) {
	var links_array = resultDiv.getElementsByTagName('a');
	
	for(i = 0; i < links_array.length; i++) {
		if(links_array[i].className == "financing") {
			links_array[i].href = "http://www.gemoney.com/en/personal/for_your_home/bryant.html";
		}
	}
}

function setSelectedIndex(selector, target)
{
//alert("in:setSelectedIndex, target:" + target);
if (selector == null)
{
    //alert("selector is null");
    return;
} // endif
//selector.selectedIndex = 0;
//selector.options[0].selected = true;
//return;
//go through each option and if it's value == target, set that as the select's selectedIndex
for (index = 0; index < selector.length; index++) 
{
    //alert("[" + index + "]:" + selector[index].value);
    if (selector[index].value == target) 
    {
	    selector.selectedIndex = index;
	    selector.options[index].selected = true;
        //alert("selectedIndex:" + selector.selectedIndex);
        break;
	} // endif
} // endfor
} // endfunction

/*
    for (var i = 0; i < sr.options.length; i++) 
    {
        s += "," + i + ":" + sr.options[i].selected;
      if (sr.options[i].selected)
    {
      searchRadius = sr.options[i].value;
      break;
    } // endif
    } // endfor
*/

