$(document).ready
(   function() 
    {
        var postCodeValue = null;
        var rememberMe = false;
        var COOKIE_NAME = 'PostcodeCookie';
        var options = { path: '/', expires: -1};
 	    if($('#phDivError').length > 0 ){
            $.cookie(COOKIE_NAME, null, options);
	    }
	    else
	    {
	        var cookie_val = $.cookie(COOKIE_NAME);
	        if(cookie_val){
        	    var aCookieVal = cookie_val.split("=");
                if(aCookieVal.length >1)
                {
                    postCodeValue = aCookieVal[1];
                    rememberMe = true;
                }
            }
	    }
  	    
  	    if (postCodeValue != null)
  	    {
      	    $("input[id$='_txtSearch']").each(function()
      	    {
      	        $(this).val(postCodeValue);
      	    });
  	    }
  	    
   	    // there could be multiple remember the post code controls
  	    $("input[id$='_chkRemPostCode']").each(function()
  	    {
  	        var oldID = $(this).attr('id');
  	        var checkedValue = $(this).attr('value');
      	    var jHtml = $("<input type='checkbox' name='" + oldID + "' id='" + oldID + "'/>");
      	    
            if (rememberMe) {
      	      jHtml = $("<input type='checkbox' checked='checked' name='" + oldID + "' id='" + oldID + "'/>");
      	    }
      	    
      	    jHtml.addClass('chk-postcode');
      	    $(this).after(jHtml).remove();
      	    //a checkbox needs a label associated with it
      	    //remove any pre-existing label elements
      	    $("input[id='" + oldID + "'] + label").remove();
      	    jHtml.after("<label for='" + oldID + "'> Remember postcode</label>");  	    
  	    });
  	    
  	   $("input[id$='_chkRemPostCode']").click(function()
  	    {
  	        $(this).attr('value',$(this).attr('checked'));
  	    });
    }
);

