/* 
	JS/searchCookie.js
	
	this bunch of code sets a cookie (triggered by a search off the homepage) that contains
	from and to airports. This is then pickled up next time the page is reloaded. 
	Also sets the cheapflights table dropdown to match if it is present. 
	JC 28/01/2009
	
	
*/


function SetSearch() { 
	var cookie = new Cookie("flytc_saveYourLastSearch");
	cookie.from1Select = document.getElementById("from1Select").options[document.getElementById("from1Select").selectedIndex].value;
	cookie.to1Select = document.getElementById("to1Select").options[document.getElementById("to1Select").selectedIndex].value;
	cookie.store(365);
}

function GetSearch() { 
	var cookie = new Cookie("flytc_saveYourLastSearch");
	
	// this block sets the cheapsflights destination dropdown (dynamic routing table on the homepage) to be the same as the 
	// destination saved by the search cookie if it can.
	if ( (document.getElementById("cheapFlights")) && (cookie.to1Select) ) {
		for (var i = 0; i<document.getElementById("cheapFlightsSelector").length; i++ ) {
			if (document.getElementById("cheapFlightsSelector").options[i].value == cookie.to1Select) {
				document.getElementById("cheapFlightsSelector").value = cookie.to1Select;
				//we also need to trigger the onchange event to rebuild the list
				buildCheapFlightsRoutes();
				break;
			}
		}
	}

//alert(cookie.from1Select + "\n" + cookie.to1Select);
	// this bit sets the dropdowns in the search box
	if (cookie.from1Select) document.getElementById("from1Select").value = cookie.from1Select;
	if (cookie.to1Select) document.getElementById("to1Select").value = cookie.to1Select;
	
	setAgeLabels();
}
	
// below taken from david flanagans javascript definitive guide ;-) buy it it's good.	

function Cookie(name) {
    this.$name = name;  
    var allcookies = document.cookie;
    if (allcookies == "") return;
    var cookies = allcookies.split(';');
    var cookie = null;
    for(var i = 0; i < cookies.length; i++) {
    		var cn = cookies[i].substring(0, name.length+1);
        if (cn.search(name) >=0 ) {        																																		
            cookie = cookies[i];
            break;
        }
    }
    if (cookie == null) return;
    var cookieval = cookie.substring(name.length+1);
    var a = cookieval.split('&'); 
    for(var i=0; i < a.length; i++)  
        a[i] = a[i].split(':');
    for(var i = 0; i < a.length; i++) {
        this[a[i][0]] = decodeURIComponent(a[i][1]);
    }
}


Cookie.prototype.remove = function(path, domain, secure) {
//alert("remove");
    for(var prop in this) {
        if (prop.charAt(0) != '$' && typeof this[prop] != 'function') 
            delete this[prop];
    }
    this.store(0, path, domain, secure);
}

Cookie.prototype.store = function(daysToLive, path, domain, secure) {   
    var cookieval = "";
    for(var prop in this) {
        if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')) 
            continue;
        if (cookieval != "") cookieval += '&';
        cookieval += prop + ':' + encodeURIComponent(this[prop]);
    }
    var cookie = this.$name + '=' + cookieval;
    if (daysToLive || daysToLive == 0) { 
        cookie += "; max-age=" + (daysToLive*24*60*60);
        //for IE: 
        var aDate = new Date();
        aDate.setTime( (aDate.getTime() + (daysToLive*24*60*60*1000)) );
        cookie += "; expires=" + aDate.toGMTString();
    }
    if (path) cookie += "; path=" + path;
    if (domain) cookie += "; domain=" + domain;
    if (secure) cookie += "; secure";
    document.cookie = cookie;
}

Cookie.enabled = function() {
//alert("enabled");
    if (navigator.cookieEnabled != undefined) return navigator.cookieEnabled;
    if (Cookie.enabled.cache != undefined) return Cookie.enabled.cache;
    document.cookie = "testcookie=test; max-age=10000";  
    var cookies = document.cookie;
    if (cookies.indexOf("testcookie=test") == -1) {
        return Cookie.enabled.cache = false;
    }
    else {
        document.cookie = "testcookie=test; max-age=0"; 
        return Cookie.enabled.cache = true;
    }
}