function setCookie()
{
	// If no cookie set then create a referer and entry point cookie
	if ( ! ( readCookie('aps_cookie') || readCookie('aps_referrer') ) )
	{
		var date = new Date();
		date.setTime(date.getTime()+(1*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	
		var value = escape(document.referrer);
		var name = 'aps_referrer';
		document.cookie = name+"="+value+expires+"; path=/";
		
		var entry_value = window.location.href;
		entry_value = entry_value.substr( 39 );
		var entry_name = 'aps_entry';
		document.cookie = entry_name+"="+entry_value+expires+"; path=/";

		// HRS - Test sw			
		var searchString = document.location.search;

		// strip off the leading '?'
		searchString = searchString.substring(1);

		var nvPairs = searchString.split("&");

		for (i = 0; i < nvPairs.length; i++)
		{
			var nvPair = nvPairs[i].split("=");
			var name = nvPair[0];
			var value = nvPair[1];
	
			if ( name == 'pp' )
			{
				var pp_name = 'aps_pp';
				document.cookie = pp_name+"="+value+expires+"; path=/";
			}
			else if ( name == 'kw' )
			{
				var kw_name = 'aps_kw';
				document.cookie = kw_name+"="+value+expires+"; path=/";
			}
			else if ( name == 'ad' )
			{
				var kw_name = 'aps_ad_id';
				document.cookie = kw_name+"="+value+expires+"; path=/";
			}
			else if ( name == 'id' )
			{
				var kw_name = 'aps_id';
				document.cookie = kw_name+"="+value+expires+"; path=/";
			}
			else if ( name == 'aff' )
			{
				var kw_name = 'aps_aff';
				document.cookie = kw_name+"="+value+expires+"; path=/";
			}
		}
	}
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

// set ret date if date changes

function setReturnMonth()
{
	if ( document.SelForm.ddm.value > document.SelForm.rdm.value )
	{
		document.SelForm.rdm.value = document.SelForm.ddm.value;
	}
}

function setReturnYear()
{
	if ( document.SelForm.ddy.value > document.SelForm.rdy.value )
	{
		document.SelForm.rdy.value = document.SelForm.ddy.value;
	}
}


// Functions required for the airport parking quote form

function check_dates( fromDate, toDate )
{
	if ( fromDate > toDate )
	{
		return false;
	}
	return true;
}


function valid_dates( fromDate, toDate )
{
	//var frDate = isDate( fromDate );
	//var retDate = isDate( toDate );
	var frDate = isDate( 2000 + Math.abs(document.SelForm.ddy.value), Math.abs(document.SelForm.ddm.value), Math.abs(document.SelForm.ddd.value) );
	var retDate = isDate( 2000 + Math.abs(document.SelForm.rdy.value), Math.abs(document.SelForm.rdm.value), Math.abs(document.SelForm.rdd.value) );

	if ( frDate != 0 )
	{
		alert( getDateMsg( frDate ) );
		return false;
	}

	if ( retDate != 0 )
	{
		alert( getDateMsg( retDate ) );
		return false;
	}

	return true;
}


function isDate( y, m, d )
{
//	var y = date.getFullYear();
//	var m = Math.abs( date.getMonth() ) + 1;
//	var d = date.getDate();
	
	//alert( 'date = ' + y + m + d );
	 
	d = Math.abs(d) || 0, m = Math.abs(m) || 0, y = Math.abs(y) || 0;
	
	return arguments.length != 3 ? 1 : d < 1 || d > 31 ? 2 : m < 1 || m > 12 ? 3 : /4|6|9|11/.test(m) && d == 31 ? 4
	: m == 2 && (d > ((y = !(y % 4) && (y % 1e2) || !(y % 4e2)) ? 29 : 28)) ? 5 + !!y : 0;
};



function getDateMsg(x)
{
    return x == 0 ? "Valid Date"
    : x == 1 ? "Invalid date format"
    : x == 2 ? "Invalid day"
    : x == 3 ? "Invalid month"
    : x == 4 ? "Invalid date\nTip\n30 days have April, June, September and November \nall the rest have 31 except February...."
    : x == 5 ? "February has only 28 days"
    : x == 6 ? "In leap years, February has 29 days": "nothing =]";
};

// function which checks whether the dates are valid, greater than today, and 
// return date is after departure datre
function check_form()
{
	var fromDate = new Date( 2000 + Math.abs(document.SelForm.ddy.value), Math.abs(document.SelForm.ddm.value) - 1, document.SelForm.ddd.value );
	var toDate = new Date( 2000 + Math.abs(document.SelForm.rdy.value),Math.abs(document.SelForm.rdm.value) - 1,document.SelForm.rdd.value  );
	
	//alert( 'year = ' + fromDate.getFullYear() );
	
	if ( ! valid_dates( fromDate, toDate ) )
	{
		// error message already displayed
		return false;
	}
	
	if ( ! check_dates( fromDate, toDate ) )
	{
		alert ( 'Please check your departure date, as it is before your return date' );
		return false;
	}

	// compare dates, to allow people to book today, subtract 1 from now
	var now = new Date();
	now.setDate(now.getDate()-1);
	//alert ( "now =" +  now.toDateString() + " from = " + fromDate.toDateString() ); 
	if ( now > fromDate )
	{
		alert ( 'Please change your departure date, as it is before today.' );
		return false;
	}
	
	if ( document.SelForm.ap.value == '' )
	{
		alert ( 'Please select the airport you require' );
		return false;
	}

	var str = new String( document.SelForm.tc.value );
	
	if ( ! ( str.lastIndexOf( document.SelForm.ap.value ) > 1 ) )
	{
		document.SelForm.tc.value = document.SelForm.tc.value + document.SelForm.ap.value;
		//alert ( document.SelForm.tc.value );
	} 
	return true;
}

// legacy


function popUp(px,agent,tmp,ap,ddd,ddm,ddy,dth,dtm,rdd,rdm,rdy,rth,rtm,tc)
{
	var px = px.value;
	var agent = agent.value;
	var tmp = tmp.value;
	var ap = ap.options[ap.selectedIndex].value;
	var ddd = ddd.options[ddd.selectedIndex].value;
	var ddm = ddm.options[ddm.selectedIndex].value;
	var ddy = ddy.options[ddy.selectedIndex].value;
	var dth = dth.options[dth.selectedIndex].value;
	var dtm = dtm.options[dtm.selectedIndex].value;
	var rdd = rdd.options[rdd.selectedIndex].value;
	var rdm = rdm.options[rdm.selectedIndex].value;
	var rdy = rdy.options[rdy.selectedIndex].value;
	var rth = rth.options[rth.selectedIndex].value;
	var rtm = rtm.options[rtm.selectedIndex].value;
	var tc = tc.value;

	// append the ap to the tracking code
	tc = tc + '-' + ap;
	
	var urlString = "http://parking.essentialtravel.co.uk/airportParking/results.asp?px=" + px + "&agent=" + agent + "&tmp=" + tmp + "&ap=" + ap + "&ddd=" + ddd + "&ddm=" + ddm + "&ddy=" + ddy + "&dth=" + dth + "&dtm=" + dtm + "&rdd=" + rdd + "&rdm=" + rdm + "&rdy=" + rdy + "&rth=" + rth + "&rtm=" + rtm + "&tc=" + tc ;
	//alert("str = " + urlString );
	
	posx = (screen.width/2) - (790/2)
	posy = (screen.height/2) - (540/2)
	if(posx < 0) { posx = 0; }
	if(posy < 0) { posy = 0; }
	
	openWindow(urlString,'travel','width=790,height=540,scrollbars=yes,status=yes,toolbar=yes,resizable=yes,location=no,left='+posx+',top='+posy);

}


function openWindow(theURL,winName,features) {
	//parkingDisplay = window.url(theURL,winName,features);
	parkingDisplay = window.location.href(theURL);
	parkingDisplay.focus();
}

function fromDate()
{
	start = new Date();
	
	start.setDate( start.getDate() + 1 );

	var d, m, y;
	d = start.getDate();
	m = start.getMonth();
	y = start.getFullYear();

	document.SelForm.ddd.selectedIndex = d -1; // as were using indexes we need to subtract 1 as they start from 0
	document.SelForm.ddm.selectedIndex = m;
	document.SelForm.ddy.selectedIndex = y-2004;

}

function toDate()
{
	fromDate();

	end = new Date();
	end.setDate( end.getDate() + 8 );

	var d, m, y;
	d = end.getDate();
	m = end.getMonth();
	y = end.getFullYear();
	
	//var i = 0; if ( d > 27 || (d < 25 && m == 2 ) ) { i = 1; d = 0; }
	//alert( 'd =' + d + ' m= ' + m + ' y = ' + y );

	document.SelForm.rdd.selectedIndex = d - 1; // as were using indexes we need to subtract 1 as they start from 0
	document.SelForm.rdm.selectedIndex = m;
	document.SelForm.rdy.selectedIndex = y-2004;
}
function init()
{
	if ( document.SelForm.rdy.value == '04' )
	{
		toDate();
	}
}

function doBookmarkLink() 
{
 title = "Airport Parking Search"; 
 url = "http://www.airport-parking-search.co.uk";

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
 }
