


$(document).ready(function() { 
			correctPNG();
			//Selectoption binding		
			$("select").bind("change", function(e)	{	
				var iVal = $("option[selected]", this).attr("value");
				
				
				var params = '&action=' + $(this).attr("name"); 
				params += '&value=' + iVal;

				if(iVal==0) return false;
				$(".multi").attr("disabled", "disabled");
				$.ajax({
				  type: "GET",
				  url: relroot + '/' +lang + '/' + page_id + reqMethod + params,
				  dataType: "xml",
				  error: error,
				  async: false,
				  success: test
				});
			})
			
			
			$(".postal").bind("keydown", function(e) { 
			
				if(e.keyCode == 13 ) { return false;}
			
			
			});
			
			$("input[type=image]").bind("click", function(e)	{	
				$(this).attr("disabled", "disabled");
				var xclass = $(this).attr("class");
				var type = $(this).val();
				
				var params = '&action=' + $(this).attr("name"); 
				params += '&postal=' + $("#postal" + type).attr("value");
				params += '&type=' + type
				
				$.ajax({
				  type: "GET",
				  url: relroot + '/' +lang + '/' + page_id + reqMethod + params,
				  dataType: "xml",
				  error: error,
				  async: false,
				  success: test
				});				
				
				$(this).removeAttr("disabled", "disabled");
				return false;
				
				
				
			})			
						
			
			$(".pageless").bind("click", function() { 
			
				var action = $(this).attr("id"); 
				
				if(page - perpage < 0) return false;
				
				actionpage = page - perpage;
				page = actionpage;
			
				var params = '&action=' + $(this).attr("name");
				params+= '&page=' + page;
				
				$.ajax({
				  type: "GET",
				  url: relroot + '/' +lang + '/' + page_id + reqMethod + params,
				  dataType: "xml",
				  error: error,
				  async: false,
				  success: switchpage
				});						

				var type= $("stores", stores).attr("type");

				if(page < $("store", stores).length)	{
					$(".more"+type).removeClass("hidden");
				}
				
				if(page - perpage < 0)	{
					$(this).addClass("hidden");
				}
				
				
				
				
				
				return false;
			});

			$(".page").bind("click", function() { 
			
				var action = $(this).attr("id"); 
				actionpage = page + perpage;
				page = actionpage;
				var params = '&action=' + $(this).attr("name");
				params+= '&page=' + page;
				
				$.ajax({
				  type: "GET",
				  url: relroot + '/' +lang + '/' + page_id + reqMethod + params,
				  dataType: "xml",
				  error: error,
				  async: false,
				  success: switchpage
				});						

				var type = $("stores", stores).attr("type");

				if(page + perpage >= $("store", stores).length)	{
					$(this).addClass("hidden");
				}
				
				
				if(page > 0)	{ 
					$(".lessmore"+type).removeClass("hidden");
				}
				
				return false;
			});




		});
		
		
		function reset(type, content)	{
			var page=0;
			
			var $result = $("#result" + type);
			//Result is hidden, so lets show it!
			if($($result).hasClass("hidden"))	{
				
				if($("stores", content).length) {
				
					var $shade = $(".scrollimg" + type + " img");
					if($($shade).length == '0')	{
						$shade = $(".scrollimg" + type + " span");
					}
					
					if($($shade).length == 1)	{
						$($shade).animate({top: '-200px'}, timeoutz, 'linear', function() { 
						clear(type, $result);	
						$($result).removeClass("hidden");						
						$(".result" + type).removeClass("hidden");				
							
							var ul = $("ul", $result);
							var i = 0;
							$(ul).removeClass("hidden");
							$("stores store", content).each(function()	{
								i++;
								
								writeStore(this, type, ul);
								if(i == perpage) { return false; }
							});

							if($("store", content).length > perpage) { 
								$(".more" + type).removeClass("hidden");
							}				
							$(".lessmore" + type).addClass("hidden");		
						
						});
					}
				}
				
				
			
			
			} else {

				var $shade = $(".scrollimg" + type + " img");
				if($($shade).length == '0')	{
					$shade = $(".scrollimg" + type + " span");
				}

			
				if($("stores", content).length) {
					if($($shade).length == 1)	{
						clear(type, $result);	
						$($shade).animate({top: '-550px'}, timeoutz, 'linear', function() {  
							clear(type, $result);	
							$($shade).animate({top: '-200px'}, timeoutz, 'linear', function() { 
								clear(type, $result);	

								$($result).removeClass("hidden");						
								$(".result" + type).removeClass("hidden");				
								var ul = $("ul", $result);
								var i = 0;
								$(ul).removeClass("hidden");
								$("stores store", content).each(function()	{
									i++;
									
									writeStore(this, type, ul);
									if(i == perpage) { return false; }
								});
	
								if($("store", content).length > perpage) { 
									$(".more" + type).removeClass("hidden");
								}				
								$(".lessmore" + type).addClass("hidden");		
							
							});
						});
					}
				}
				
				if($("cities", content).length) {
					clear(type, $result);
					$($result).addClass("hidden");
					$($shade).animate({top: '-550px'}, timeoutz, 'linear', function() { });  
				}
				if($("zip", content).length) {
					clear(type, $result);
					$($result).addClass("hidden");	
					$($shade).animate({top: '-550px'}, timeoutz, 'linear', function() { });  
				}
			
			
			}
		}
		
		
		function clear(type, result)	{
		
			var ul = $("ul", result);
			$(ul).text('');
			
			$(".result" + type).addClass("hidden");
			$(".more" + type).addClass("hidden");
			$(".lessmore" + type).addClass("hidden");	
			$(".error" + type).addClass("hidden");	
			
		}

		//Reset the stores...
		function resetall(current_type)	{
				
				
				
				for(i=0; i<=2; i++)	{
					var type = i;
					page = 0;
					
					var $result = $("#result" + i);
					
					if($result!='undefined') {
						if(!$($result).hasClass("hidden"))	{
							var $shade = $(".scrollimg" + type + " img");
							if($($shade).length == '0')	{
								$shade = $(".scrollimg" + type + " span");
							}
							clear(i);
							$($result).addClass("hidden");	
							$($shade).animate({top: '-550px'}, timeoutz, 'linear', function() { });
						} 
					}
					
					var tr = $("#tdzip" + type)
					tr.addClass("hidden");				
					var tr = $("#tdzipexpl" + type)
					tr.addClass("hidden");				
					var tr = $("#tdfind" + type)
					tr.addClass("hidden");	
					
					var tr = $("#tdcities" + type)
					tr.addClass("hidden");
					var select = $("select", tr); 
					select.removeOption(/./);
					select.addClass("hidden");
	
					if(i != current_type)
						$("#country" + i).selectOptions("0", true);							
				}
		}
		
		
		

		
		
		
		
		
		
		
		
		
		
		function test(content)	{
			
			
			
			//if(inUse == true) return false;
			
			//inUse = true;
			
			//Check to see if there's an error!
			if(($("ajaxException", content).length))	{
				document.location = relroot + "/error/exception.php";
			}
			
			if($("zip", content).length)	{
				var type = $("zip", content).attr("type");

				var blaat = $("#result" + type).css("display");
				if(blaat == 'none') timeout = timeoutz;
				else timeout = 0;



				reset(type, content);

				var tr = $("#tdzip" + type)
				tr.removeClass("hidden");				
				var tr = $("#tdzipexpl" + type)
				tr.removeClass("hidden");				
				var tr = $("#tdfind" + type)
				tr.removeClass("hidden");				
							


			}	
			
			if($("stores", content).length) {
				var type = $("stores", content).attr("type");
				reset(type, content);
			}
			if($("error", content).length) {
				var type = $("error", content).attr("type");
				reset(type, content);
			}
			
			
			
			
			if($("cities", content).length)	{
				
				var type =  $("cities", content).attr("type");
				
				resetall(type);


				var tr = $("#tdcities" + type)
				tr.removeClass("hidden");

				var select = $("select", tr); 
				select.removeOption(/./);				
				select.removeClass("hidden");
								
				var sName = select.attr("name");
				var sID 	= select.attr("id");

				var option = new Array("Select city");
				$("cities city", content).each(function() { 
					
					var tid = $(this).attr("TID");
					option[tid] = $(this).attr("desc");
				});
				select.addOption(option, false);
				
			}

			
			//inUse = false;
			$(".multi").removeAttr("disabled");
		}
		
		
		function writeStore(store, type, ul)	{
			br = document.createElement("br");
			li = document.createElement("li");
			span = document.createElement("span");								
			$(span).addClass("resbig");
			$(span).text($(store).attr("store_name"));
			$(li).append(span);
			$(li).append(br);

			$(li).append(
				$(store).attr("street") + " " +
				$(store).attr("addition_1") + " " +
				$(store).attr("addition_2") + " " +
				$(store).attr("number") + " " +
				$(store).attr("subnumber") + " " +
				$(store).attr("postal_cd") + " " +
				$(store).attr("place") + " "
			);
			$(li).append($(br).clone());

			if($(store).attr("operated")!='')	{
				span = document.createElement("span");								
				$(span).addClass("ressmall");
				$(span).text("operated by " + $(store).attr("operated"));
				$(li).append(span);
			}
			$(ul).append(li);			
		
		}
		
		
		function switchpage(content)	{
			stores = content;
			

			var type = $("stores", stores).attr("type");
			var i = 0;
			var ul = $("ul[class*=uresult" + type + "]");
			
			ul.text("");
			
			$("store", stores).each(function()	{
				i++;
				if(i>page) {
					writeStore(this, type, ul);
				}
				if(i == page + perpage) { return false; }
			});
		}
		
		
		
		
		//Error handling
		function error(content)	{
			document.location = relroot + "/error/exception.php";
		}
		
		
function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters)) 
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }    
}
		
		
		