function get_cookie (cookie_name) {
  var results = document.cookie.match ('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');
  if (results) {
    return (unescape (results[2]));
  } else {
    return null;
  }
}

// http://www.sitepoint.com/blogs/2009/08/19/javascript-json-serialization/
// Use the following in case JSON is not native
var JSON = JSON || {}; 

// implement JSON.parse de-serialization
JSON.parse = JSON.parse || function (str) {
    if (str === "") str = '""';
    eval("var p=" + str + ";");
    return p;
};


/* Custom function for the website */
function last_search_url () {

    var myCambridge = JSON.parse(get_cookie('mycambridge_data'));    
    
    if (myCambridge['upn']) {
        var upn = myCambridge['upn'];
        var address = myCambridge['address'];
        
        address = address.replace(/\+/g," ");
    
        var html = '<p><strong>Your last search: </strong><br/><a href="http://my.cambridge.gov.uk/property/'+ upn +'">'+ address+ '</a></p>';
        html +=  '<p>To find your local services for another area, please enter your postcode or address</p>'; 
    } else {
        var html = '<p>Please enter your postcode or address to find about your local services</p>';
    }
    // 'javascript_content' is the name of the id the div of the form element
    document.getElementById('javascript_content').innerHTML = html;
};

function getWardName() {
    var myCambridge = JSON.parse(get_cookie('mycambridge_data'));  
    if (myCambridge['ward_name']) {
        var ward_name = myCambridge['ward_name'];
        ward_name = ward_name.replace(/\+/g," ");
        var html = 'And your ward is: '+ward_name;
        document.getElementById('ward_name').innerHTML = html;
    } else {
        return null;
    }
}


 
if (window.onload) {
   var _previousOnload = window.onload;
}
// Define the new window.onload
window.onload = function(evt) {
    // execute a previous window.onload if it exists
    if (_previousOnload) {
        _previousOnload.call(this, evt);
    }
    
    // call our function
    last_search_url();  
    
    getWardName();  

    // clean the onload
    this.onload = null;
};

