// Functions to remember a UK postcode - or other address inputs -
// in a browser's cookie, so they can be retrieved when a user returns to the site.

// Read any remembered postcode/location from the browser's cookie
// and input it into the postcode box
function set_postcode_box() {
    if (document.cookie) {
       var abox = document.lpf.a; // find text input box for postcode
       var remember = document.lpf.rem; // and checkbox
       var reg_exp = /pc=([^\;]*)/; // match cookie format "pc=SW9 6UW" for example
       var match = reg_exp.exec(document.cookie);
       if (abox.value == '') {     // check the box is empty first
	    abox.value = match[1]; // set the address box to have the matching postcode 
       };
       if (remember) {
	   remember.checked = 'true'; // and show the user we are remembering it
       }
    };
};


// Stores the entered postcode in the browser's cookie 
// if the remember checkbox is ticked.
function set_postcode_cookie() {
    var remember = document.lpf.rem;
    var abox = document.lpf.a;
    if (remember.checked) {
	var postcode = abox.value;
	// set the cookie format "pc=SW9 6UW" and expiration date far in future.
	document.cookie = 'pc=' + postcode + ';path=/;domain=askthelocal.com;expires=Thu,31-Dec-2020 00:00:00 GMT;';
    };
};

function reset_postcode_cookie() {
    var remember = document.lpf.rem;
    if (!(remember.checked)) {
	// delete the cookie by setting an expiration date in the past.
	if (document.cookie) {
	    document.cookie = 'pc=;path=/;domain=askthelocal.com;expires=Sat,1-Jan-2000 00:00:00 GMT;';
	};
    };  
};
