// <![CDATA[

function formatProducts (prods)
{
	var sList = '';
	var arrProds = prods.split(",");
	for (i=0;i<arrProds.length;i++)
	{
		if (arrProds[i]!='')
			sList += "<br>- " + arrProds[i];
	}
	return sList;	
}

function updateResults( sText, filterType )
{

	var sProduct, sName, sId, sAddress, sPostalCode, sProducts, sBranch;

	sProduct = $("#product").val(); //getting product selected
	
	 $('p#search-results').text("Zoeken ...");
	 $('#results h2.caption-search-results strong').text("Zoek resultaten");	 
	 $('#results h2.caption-nearest-locations strong').text("");
 	 $('p#nearest-locations-search-results').text("");

	 $.ajax({
		 type: "GET",
		 url: "js/get-xml.asp?text=" + sText + "&product=" + sProduct + "&filter=" + filterType,
		 dataType: "xml",
		 success: function(xml)
			{	// tag HTML, tag XML, id P search
				var listTag = new Array("list-branches", "direct-locations", "search-results", "list-nearest", "nearest-locations","nearest-locations-search-results");
				var i = 0;
				for (i = 0; i < 6; i=i+3)
				{
					$("div#" + listTag[i]).empty();
					$(xml).find(listTag[i+1]).each(function(){						
						$(this).find('details').each(function(){ 							
							if ( listTag[i] == "list-nearest" )
							{
								if ( $(this).find('rows').text() != "0" && $(this).find('rows').text() != "" ) 
									{
										$('#results h2.caption-nearest-locations strong').text("Dichtstbijzijnde Locaties");
										$("p#" + listTag[i+2]).text( $(this).find('rows').text() + ' dichtstbijzijnde localtie(s).');
									}
							}
							else
								$("p#" + listTag[i+2]).text( $(this).find('rows').text() + ' gevonden locatie(s).');
						});
						$(this).find('branch').each(function(){
							sId   = $(this).find('branchGUID').text();
							sName   = $(this).find('name').text();
							sAddress  = $(this).find('address1').text();
							sPostalCode = (jQuery.trim($(this).find('postalcode').text()) == '') ? '' : ', ' + $(this).find('postalcode').text();
							sProducts = (jQuery.trim($(this).find('products').text()) == '') ? '' : '<br><strong>Producten:</strong>' + formatProducts($(this).find('products').text());
							sBranch =  "<div class='branch-item'><h3>"+sName+"</h3><p>"+ sAddress + sPostalCode + sProducts +"</p><p class=\'buttons\'><a href='/nl/locations/view_detail.asp?branchguid="+sId+"&lang=NLD&store-locator=yes' onclick=\"openPopUp('/nl/locations/view_detail.asp?branchguid="+sId+"&lang=NLD&store-locator=yes',600,650); return false;\" target='_blank' class='button'>Meer informatie</a> <a href='view-map.asp?branchguid="+sId+"' onclick=\"openPopUp('view-map.asp?branchguid="+sId+"&lang=NLD',600,650); return false;\" target='_blank' class='button'>Bekijk plattegrond</a></div>";
							$("div#" + listTag[i]).append( sBranch );
						});
					});
					$("div#" + listTag[i] + " div.branch-item").css({"width":"282px" });												
				}
			} 
	 });
	 return false;
}


$(document).ready(function(){
	$("form p:last").after("<div id='results'><h2 class='caption-search-results' style='float:left;'><strong></strong></h2><h2 class='caption-nearest-locations' style='float:right;margin-right:99px;'><strong></strong></h2><p id='search-results' style='clear:both;float:left'></p><p id='nearest-locations-search-results' style='float:right;margin-right:170px'></p><div id='list-branches' style='float:left;clear:both;'></div><div id='list-nearest' style='float:right'></div></div>");
	$("#content.inner div.main").css({"width":"635px"});
	$("select#product").css({"width":"210px"});
	$("div#list-branches, div#list-nearest").css({"width":"310px" });

	$("#address").keyup(function(event){
		var length = $(this).val().length;		
		if (length > 2 )
		{	
			if ( event.keyCode  == 8 || event.keyCode  == 13 || ( event.keyCode  >= 48 && event.keyCode  <= 57 ) ||
				 ( event.keyCode  >= 65 && event.keyCode  <= 90 ) || ( event.keyCode  >= 96 && event.keyCode  <= 122 ) ||
					event.keyCode  == 46 || event.keyCode  == 45 || event.keyCode  == 95 || event.keyCode  == 64 || event.keyCode  == 0 || event.keyCode  == 13 )
				updateResults( $(this).val(), 'address' );	
		}
		else
		if (event.keyCode  != 13 && event.keyCode  != 9 && event.keyCode  != 16) // avoid enter, tab and control tab			
			$("#list-branches, #list-nearest, p#search-results, p#nearest-locations-search-results, #results h2 strong").empty();
	});
	
	$("form#store-locator").bind("keypress", function(e) {
             if (e.keyCode == 13) return false;
    });

	$("#product").change(function(event){
		if ($(this).val() == '' && $("#address").val() == '')
			$("#address").focus();
		else
		{
			$("#list-branches, p#search-results, #list-nearest, p#nearest-locations-search-results, #results h2 strong").empty();
			updateResults( $("#address").val(), 'address' );
		}
	});
	
	$("#city").change(function(event){
		if ($(this).val() != '' )
		{
			$("#list-branches, p#search-results, #list-nearest, p#nearest-locations-search-results, #results h2 strong").empty();
			updateResults( $(this).val(), 'city' );
		}
	});	

});
// ]]>