function DealerSearchBox_ClientOnKeyUp(event)
{
	search = document.getElementById("DealerSearchBox");
	searchzip = document.getElementById("DealerSearchBox");
	searchResultsLayer = document.getElementById("DealerSearchResultsLayer");
	if (search.value.length >= 3)
	{
		SearchBox.Refresh(search.value);
                
		var FoundCountryList = SearchBox.SearchDealer(search.value);
		if (FoundCountryList.countryList.length > 0)
		{
			searchResultsLayer.style.display = "block";
			searchResultsLayer.style.left = getOffsetLeft(search) + "px";
			searchResultsLayer.style.top = (getposOffset(search, "top") + search.offsetHeight + 2) + "px";
			searchResultsLayer.style.zindex = 40000;
                
			document.getElementById("DealerSearchResultsLayer").innerHTML = FoundCountryList.Render();
		}
		else
		{
			if (event.keyCode == 13)
			{
				var linkhref = SearchBox.href;
				linkhref = linkhref.replace("idCountry", "0");
				linkhref = linkhref.replace("idState", "0");
				linkhref = linkhref.replace("idArea", "0");
				window.location = linkhref;
			}
			else
			{
				searchResultsLayer.style.display = "block";
				searchResultsLayer.style.left = getOffsetLeft(search) + "px";
				searchResultsLayer.style.top = (getposOffset(search, "top") + search.offsetHeight + 2) + "px";
				searchResultsLayer.style.zindex = 40000;
                    
				// if regex match zipcode or postal code then search zip option ...
					
				var gozip = ""
				//var re = new RegExp("(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]{1}\d{1}[A-Za-z]{1} *\d{1}[A-Za-z]{1}\d{1}$)");
				//var re = "(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]{1}\d{1}[A-Za-z]{1} *\d{1}[A-Za-z]{1}\d{1}$)";
				//var re = "(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]{1}\d{1}[A-Za-z]{1} *\d{1}[A-Za-z]{1}\d{1}$)";


					
				if (validateZip( searchzip.value ) == true) {
					if (Langage == "En")
					{
						document.getElementById("DealerSearchResultsLayer").innerHTML = "Search dealers near: " + ((searchzip.value).toUpperCase()) + "<br /><input type='image' src='/ext/img/icon/GObtn.png' width='37' height='21' border='0' name='submit' alt='GO' onclick='HEADshowLocation();' /><hr />" + SearchBox.RenderLink("0", "0", "0", "", "Other location");
					}
					else
					{
						document.getElementById("DealerSearchResultsLayer").innerHTML = "Chercher près de : " + ((searchzip.value).toUpperCase()) + "<br /><input type='image' src='/ext/img/icon/GObtn.png' width='37' height='21' border='0' name='submit' alt='GO' onclick='HEADshowLocation();' /><hr />" + SearchBox.RenderLink("0", "0", "0", "", "Autre recherche");
					}
				} else {
					// not postal code or zipcode
					if (Langage == "En")
					{
						document.getElementById("DealerSearchResultsLayer").innerHTML = "No result found<hr />" + SearchBox.RenderLink("0", "0", "0", "", "Other location");
					}
					else
					{
						document.getElementById("DealerSearchResultsLayer").innerHTML = "Aucun résultat<hr />" + SearchBox.RenderLink("0", "0", "0", "", "Autre recherche");
					}
				}


			}
		}
	}
	if (search.value.length == 0)
	{
		searchResultsLayer.style.display = "none";
	}
            
}


function validateZip( strValue ) {
	var objRegExp  = /(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]{1}\d{1}[A-Za-z]{1} *\d{1}[A-Za-z]{1}\d{1}$)/;

	//check for valid ZipCode and PostalCode
	return objRegExp.test(strValue);
}


HEADinitialize();
	
var HEADgeocoder;
	
function HEADinitialize() {
	HEADgeocoder = new GClientGeocoder();
}

	

function HEADgetCoordinates1(response) {
	if (!response || response.Status.code != 200) {
		alert("Entrez un code postal canadien valide.");
	} else {
		HEADplace = response.Placemark[0];
		document.getElementById("Head1long").value = HEADplace.Point.coordinates[0];
		document.getElementById("Head1lat").value = HEADplace.Point.coordinates[1];
		document.forms["HeadformSubmit"].submit();
	}
}
	
function HEADshowLocation() {
	//alert("boo");
	var HEADadress1 = document.forms["HeadSearchBox1"].DealerSearchBox.value;
	//alert(HEADadress1);
	document.getElementById("HeadMPostalCode").value = document.forms["HeadSearchBox1"].DealerSearchBox.value;
	//alert(document.getElementById("HeadMPostalCode").value);
	HEADgeocoder.getLocations(HEADadress1, HEADgetCoordinates1);
}











function getposOffset(obj, offsettype){
	var totaloffset = (offsettype=="left") ? obj.offsetLeft : obj.offsetTop;
	var parentEl = obj.offsetParent;
	while (parentEl != null){
		totaloffset = (obj=="left") ? totaloffset+obj.offsetLeft : totaloffset + parentEl.offsetTop;
		parentEl=parentEl.offsetParent;
	}
	return totaloffset;
}
        
function getOffsetLeft(elm) {
	var mOffsetLeft = elm.offsetLeft;
	var mOffsetParent = elm.offsetParent;

	while(mOffsetParent) {
		mOffsetLeft += mOffsetParent.offsetLeft;
		mOffsetParent = mOffsetParent.offsetParent;
	}

	return mOffsetLeft;
}


function SearchBoxEngine()
{
	this.countryList = new Array();
	this.href = "/iiix/dealer/?idC=idCountry&idS=idState&idR=idArea&T=townName";
	this.searchPage = "/ext/js/s.dealer.L4.searchbox.php?search=";
	this.searchFragment = "";
            
	this.Refresh = function(searchFragment)
	{
		if (searchFragment.length == 3 || searchFragment.indexOf(this.searchFragment) == -1)
		{
			xmlhttp = getHTTPObject();
			xmlhttp.open("GET", this.searchPage + searchFragment, false);
			xmlhttp.send(null);

			this.countryList = new Array();
                    
			eval(xmlhttp.responseText);
		}
		this.searchFragment = searchFragment;
	}
            
	this.SearchDealer = function(textSearch)
	{
		var countriesFound = new SearchBoxEngine();
		var searchExp = new RegExp(textSearch, "gi");
		var index = 0;
               
		if (this.countryList.length != 0)
		{
			for (index=0;index<this.countryList.length;index++)
			{
				countryMatch = (this.countryList[index].name.search(searchExp) > -1);
				if (countryMatch)
				{
					countriesFound.countryList.push(new Country(this.countryList[index].code, FindAndMakeBold(this.countryList[index].name, textSearch)));
				}
				states = this.countryList[index].SearchState(textSearch);

				//si il y a des provinces on les ajoute au niveau superieur
				if (states.length > 0)
				{
					if (!countryMatch)
					{
						countriesFound.countryList.push(new Country(this.countryList[index].code, this.countryList[index].name));
					}
					countriesFound.countryList[countriesFound.countryList.length-1].states = states;
				}
			}
		}
                
		return countriesFound;
	}
            
	this.Render = function()
	{
		renderText = "";

		for (country=0;country<this.countryList.length;country++)
		{
			renderText = renderText + "<div>" + this.RenderLink(this.countryList[country].code, "0", "0", "", this.countryList[country].name) + "</div>";
			for (state=0;state<this.countryList[country].states.length;state++)
			{
				renderText = renderText + "<div style=\"margin-left: 10px\">" + this.RenderLink(this.countryList[country].code, this.countryList[country].states[state].code, "0", "", this.countryList[country].states[state].name) + "</div>";
				for (area=0;area<this.countryList[country].states[state].areas.length;area++)
				{
					renderText = renderText + "<div style=\"margin-left: 20px\">" + this.RenderLink(this.countryList[country].code, this.countryList[country].states[state].code, this.countryList[country].states[state].areas[area].code, "", this.countryList[country].states[state].areas[area].name) + "</div>";
					for (town=0;town<this.countryList[country].states[state].areas[area].towns.length;town++)
					{
						renderText = renderText + "<div style=\"margin-left: 30px\">" + this.RenderLink(this.countryList[country].code, this.countryList[country].states[state].code, this.countryList[country].states[state].areas[area].code, this.countryList[country].states[state].areas[area].towns[town].name, this.countryList[country].states[state].areas[area].towns[town].name) + "</div>";
					}
				}
				for (town=0;town<this.countryList[country].states[state].towns.length;town++)
				{
					renderText = renderText + "<div style=\"margin-left: 20px\">" + this.RenderLink(this.countryList[country].code, this.countryList[country].states[state].code, 0, this.countryList[country].states[state].towns[town].name, this.countryList[country].states[state].towns[town].name) + "</div>";
				}
			}
		}

		if (Langage == "En")
		{
			renderText = renderText + this.RenderLink("0", "0", "0", "", "<hr />Other location");
		}
		else
		{
			renderText = renderText + this.RenderLink("0", "0", "0", "", "<hr />Autre recherche");
		}
		return renderText;
	}
            
	this.RenderLink = function(idC, idS, idR, town, text)
	{
		link = this.href;
		link = link.replace("idCountry", idC);
		link = link.replace("idState", idS);
		link = link.replace("idArea", idR);
                
		if (town != "")
		{
			link = link.replace("townName", town);
			link = link.replace("<b>", "");
			link = link.replace("</b>", "");
		}
		else
		{
			link = link.replace("&T=townName", "");
		}
		return "<a href=\"" + link + "\">" + text + "</a>";
	}
}

function Country(code, name)
{
	this.code = code;
	this.name = name;
	this.states = new Array();
            
	this.SearchState = function(textSearch)
	{
		var statesFound = new Array();
		var searchExp = new RegExp(textSearch, "gi");
                
		//For each State
		if (this.states.length != 0)
		{
			for (stateIndex=0;stateIndex<this.states.length;stateIndex++)
			{
				stateMatch = (this.states[stateIndex].name.search(searchExp) > -1);
				if (stateMatch)
				{
					statesFound.push(new State(this.states[stateIndex].code, FindAndMakeBold(this.states[stateIndex].name, textSearch)));
				}
                        
				areas = this.states[stateIndex].SearchArea(textSearch);
				//si il y a des provinces on les ajoute au niveau superieur
				if (areas.length > 0)
				{
					if (!stateMatch)
					{
						statesFound.push(new State(this.states[stateIndex].code, this.states[stateIndex].name));
					}
					statesFound[statesFound.length-1].areas = areas;
				}
                        
				towns = this.states[stateIndex].SearchTown(textSearch);
				//si il y a des provinces on les ajoute au niveau superieur
				if (towns.length > 0)
				{
					if (!stateMatch)
					{
						statesFound.push(new State(this.states[stateIndex].code, this.states[stateIndex].name));
					}
					statesFound[statesFound.length-1].towns = towns;
				}
			}
		}
                
		return statesFound;
	}
}
        
function State(code, name)
{
	this.code = code;
	this.name = name;
	this.areas = new Array();
	this.towns = new Array();
            
	this.SearchArea = function(textSearch)
	{
		var result = new Array();
		var searchExp = new RegExp(textSearch, "gi");
                
		//For each Area
		if (this.areas.length != 0)
		{
			for (areaIndex=0;areaIndex<this.areas.length;areaIndex++)
			{
				areaMatch = (this.areas[areaIndex].name.search(searchExp) > -1);
				if (areaMatch)
				{
					result.push(new Area(this.areas[areaIndex].code, FindAndMakeBold(this.areas[areaIndex].name, textSearch)));
				}
                        
				towns = this.areas[areaIndex].SearchTown(textSearch);
				//si il y a des villes on les ajoute au niveau superieur
				if (towns.length > 0)
				{
					if (!areaMatch)
					{
						result.push(new Area(this.areas[areaIndex].code, this.areas[areaIndex].name));
					}
					result[result.length-1].towns = towns;
				}
			}
		}
                
		return result;
	}
            
	this.SearchTown = function(textSearch)
	{
		var result = new Array();
		var searchExp = new RegExp(textSearch, "gi");
                
		//For each town
		if (this.towns.length != 0)
		{
			for (townIndex=0;townIndex<this.towns.length;townIndex++)
			{
				if (this.towns[townIndex].name.search(searchExp) > -1)
				{
					result.push(new Town(FindAndMakeBold(this.towns[townIndex].name, textSearch)));
				}
			}
		}
                
		return result;
	}
}
        
function Area(code, name)
{
	this.code = code;
	this.name = name;
	this.towns = new Array();
            
	this.SearchTown = function(textSearch)
	{
		var result = new Array();
		var searchExp = new RegExp(textSearch, "gi");
                
		//For each town
		if (this.towns.length != 0)
		{
			for (townIndex=0;townIndex<this.towns.length;townIndex++)
			{
				if (this.towns[townIndex].name.search(searchExp) > -1)
				{
					result.push(new Town(FindAndMakeBold(this.towns[townIndex].name, textSearch)));
				}
			}
		}
                
		return result;
	}
}
        
function Town(name)
{
	this.name = name;
}
        
function FindAndMakeBold(text, textSearch)
{
	var result = "";
            
	searchExp = new RegExp(textSearch, "gi");
            
	while (text.search(searchExp) > -1)
	{
		result = result + text.substr(0, text.search(searchExp));
		result = result + "<b>" + text.substr(text.search(searchExp), textSearch.length) + "</b>";
		text = text.substr(text.search(searchExp)+textSearch.length);
	}
	result = result + text;
            
	return result;
}
        
function getHTTPObject()
{
	var xmlhttp = false;

	/* Compilation conditionnelle d'IE */
	/*@cc_on
          @if (@_jscript_version >= 5)
             try
             {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
             }
             catch (e)
             {
                try
                {
                   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (E)
                {
                   xmlhttp = false;
                }
             }
          @else
             xmlhttp = false;
          @end @*/

	/* on essaie de créer l'objet si ce n'est pas déjà fait */
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		try
		{
			xmlhttp = new XMLHttpRequest();
		}
		catch (e)
		{
			xmlhttp = false;
		}
	}

	if (xmlhttp)
	{
		/* on définit ce qui doit se passer quand la page répondra */
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState == 4) /* 4 : état "complete" */
			{
				if (xmlhttp.status == 200) /* 200 : code HTTP pour OK */
				{
			/*
                      Traitement de la réponse.
                      Ici on affiche la réponse dans une boîte de dialogue.
                      */
			//alert(xmlhttp.responseText);
			}
			}
		}
	}
	return xmlhttp;
}

        
var SearchBox = new SearchBoxEngine();
var Langage = "Fr";



