$(document).ready(function() {
  if ($.browser.mozilla) {
    $("input.sub").addClass("firefox");
  }
 $("input,textarea").bind("focus",function() {
    $(this).addClass("focus");
 }).bind("blur",function() {
    $(this).removeClass("focus");
 });

  $(".subsplash").bind("mouseenter",function() {
    $(this).addClass("hover");
  }).bind("mouseleave",function() {
    $(this).removeClass("hover");
  });

//$(".ProductLister").each(function() {
 // if ($(this).find("ul.ProductList li").length==0) {
  //  $(this).hide();
  //  $(this).parent().hide();
 // }
});

$("#CategoryList li").bind("mouseenter",function() {
   $(this).addClass("hover");
}).bind("mouseleave",function() {
   $(this).removeClass("hover");
});

// check for postcode

    $(this).find("input.postcode").each(function() {
        var ths = $(this);
		var formC = $(this).parent().parent();
        var find = $("<input type='button' value='Find Address' />");
        var clear = $("<input type='button' class='postclear' value='Clear Address' />");
        $(ths).after(find);
        $(find).after(clear);
        $(ths).css({display: "inline",marginRight: "8px"});
        $(find).css({display: "inline",marginRight: "8px"});

        $(clear).bind("click",function() {
           $("input.address2",formC).val("");
           $("input.towncity",formC).val("");
           $("county",formC).val("");
           if ($("input.address1",formC).size()==0) {
            var sl = $("select.address1",formC);
            var ad = $("<input type='text' />");
                sl.before(ad);
                ad.attr("id",sl.attr("id"));
                ad.attr("name",sl.attr("name"));
                ad.attr("class",sl.attr("class"));

                sl.remove();
           }
        });


        $(find).bind("click",function() {

            $.getJSON("/remote.php?w=getpostcode&postcode=" + escape($(ths).val()),function(data) {
                $("input.address2",formC).val(data.address.address2);
                $("input.towncity",formC).val(data.address.town);
                $(".county",formC).val(data.address.county);
                $(".country",formC).val("United Kingdom");

                var ad1,sl;
if ($("input.address1",formC).size()>0) {
                ad1 = $("input.address1",formC);
                sl = $("<select></select>");
                ad1.before(sl);
                sl.attr("id",ad1.attr("id"));
                sl.attr("name",ad1.attr("name"));
                sl.attr("class",ad1.attr("class"));

                ad1.remove();
} else {
 sl = $("select.address1",formC);
 $("option",sl).remove();
}

                // change input to select
                var opt;
                  opt = $("<option value=''>Please choose an address</option>");
                  sl.append(opt);

                for(var i in data.premises) {
                  if (typeof(data.premises[i])=="function") continue;
                  opt = $("<option></option>");
                  opt.val(data.premises[i] + " " + data.address.address1);
                  opt.text(data.premises[i] + " " + data.address.address1);
                  sl.append(opt);
                }

sl.css({border: "1px solid orange"});

setTimeout(function() {
 sl.css({border: "1px solid black"});
},1000);


            });
            
        });
    });



