// Heathrow - heathrow-car-rental.co.uk
// copyright (c) usc (uk) ltd 2011/2012

String.prototype.I=function() {return '<i>'+this+'</i>'}
String.prototype.B=function() {return '<b>'+this+'</b>'}

var IE=navigator.appName.indexOf('Internet Explorer')>-1
var domain='heathrow-car-rental.co.uk'

function ById(n) {var e=null;if (n) {if (document.getElementById) e=document.getElementById(n); else if (document.all) e=document.all[n]};return e}
function ByIdVal(n,v) {var e=ById(n);if (e) if (e.value) return e.value; else if (e.innerHTML) return e.innerHTML;return ''}
function SetVal(e,v) {
	if (e) {
	    if (e.options) try{for(var i in e.options) if (e.options[i].value==v) e.options[i].selected=true} catch(er){}
		try {e.value=v} catch(er) {try {e.innerHTML=v} catch(er) {}}
	}
	return v
}
function SetMe(u) {self.location.href=u}
function trim(v) {return v?(v+'').replace(/^\s+|\s+$/g,''):''}
function pad_num(v) {v=parseInt(v);return (v<10?'0':'')+v}
function get_param() {var u=self.location.href,x=u.lastIndexOf('?');if(x<0) x=u.lastIndexOf('#');return x<0?'':unescape(u.substr(x+1))}
function put_mt(n) {document.writeln('<a href="'+get_root()+'/contact.html">(please use form)</a>')}//get_mt(n))}
function get_root() {var h='http://',u=self.location.href.replace(h,'').split('/');return h+u[0]+(self.location.href.indexOf('localhost')>-1?'/'+u[1]:'')+'/'}
function add_class(id,c) {add_eclass(ById(id),c)}
function add_eclass(e,cs) {if (e&&cs) {if (!e.className) e.className='';var s,c,cx,cy=cs.split(' ');for(cx in cy) {s=e.className;c=cy[cx];e.className=trim(c.charAt(0)=='-'?s.replace(c.substr(1),''):(s.indexOf(c)<0?s+' '+c:s))}}}
function customer_service() {document.writeln('customer.service'+String.fromCharCode(64)+domain)}

// site.js - specifics
var siteinit=false
function map_click() {updateCookieV('pickup-location',this.id)}
function init() {

	cms_init()

	// airport map
	var a,b,ap='STN,LTN,LHR,LCY,LGW'.split(',')
	for(a in ap) {b=ById(ap[a]);if (b) b.onclick=map_click}

	// slider
	if (ById('slider1')) {
		init_slider();
		document.onselectstart=function() {return false}	// ie
		document.onmousedown=function() {return false}		// mozilla
	}

	// various other inits
	if (ById('time1x')) init_timeselects()
	if (window.form_init) form_init()
	if (window.extras) extras()

	show_dropoff()
	siteinit=true
	fireslider(-1)
	if (ById('calendar')) {setTimeout('Calendar(0)',1200)}
}

function init_timeselects() {
	var x,y,v,t,ta='time1',tb='time2',t1=readCookie(ta),t2=readCookie(tb),dx='9.30'
    for(x=0;x<24;x++) for (y=0;y<=30;y+=30) {
        t=pad_num(x)+':'+pad_num(y)
        v=parseFloat(t.replace(':','.')).toFixed(2);
        appendOption(ta+'x',t,v,t1?t1:dx)
        appendOption(tb+'x',t,v,t2?t2:dx)
	}
}

// side-form helpers
function appendOption(e,n,v,x) {var s=ById(e),o=document.createElement('option');o.text=n;o.value=v;o.selected=x!=''&&v==x;try {s.add(o,null)} catch(er) {s.add(o)}}
function chg_dropoff(e) {
	var v=e.checked?1:0;
	siteinit=false;
	var d='dropoff-location',p=readCookie('pickup-location');p=p?p:'LHR';
	writeCookie('dropoff-same',v)
	writeCookie(d,v?p:(p!='other'?'other':'LHR'));
	extrasChange('delivery',p)
	extrasChange('collection',v?p:(p!='other'?'other':'LHR'))//c2=='other'?1:'');
	siteinit=true;
	calendar_update();
}
function show_dropoff() {
	var n='dropoff-same',v=readCookie(n);
	//ById('dropoff-location').className=v?'hidden':'visible';
	//ById(n).checked=v?'checked':'';
	writeCookie(n,v?1:0)
}

// hire/slider helpers
var firing=false
function gethiredays() {var d=readCookie('days');return parseInt(!d?1:d)}
function id2num(n) {var i=ById(n),v=i?trim(i.innerHTML):0;return v=='-'||v==''||v=='FREE'?0:parseFloat(v)}
function init_hire_slider(e) {slide(e,'horizontal',370,1,30,30,0,'display1')}
function init_slider() {var d=gethiredays();put_scale();pushSlider(d,0);fireslider(d)}
function put_scale() {var r='',x;for(x=1;x<31;x++) r+=(x==1||x%5==0)?'<span>'+x+'</span>':'<b>|</b>';ById('slider1scale').innerHTML=r}
function fireslider(v) {
	if (!firing&&siteinit) {
		firing=true
		v=v<1?gethiredays():v;v=v<1?1:v
		var x,td,t=0.0
		for(x=1;x<=30;x++) {
		    y=0
			if (x<=14) {td=ById('xd'+x);add_eclass(td,x<v?'gray -active':(x==v?'active -gray':'-active -gray'))}
			if (td) y=td.innerHTML.match(/\d+\.\d+/)[0]
			if (td) t+=x<=v?parseFloat(y):0
		}
		if (v!=gethiredays()) {
			calendar_setfromdays(v)
			calendar_update()
		}
		if (window.updateHireCosts) updateHireCosts(v,t);
		firing=false
	}
}

// cookies.js v2.0: cookie-less cookie, using CMS
var Cookie=''
function updateCookieV(n,v) {writeCookie(n,v);calendar_update()}
function updateCookie(e) {updateCookieV(e.id,e.value)}
function setCookie(v) {
	Cookie='';var nv,i,a=getCookie(v);for(i in a) if (a[i]) {nv=a[i].split('=');addCookie(nv[0],nv[1],1)}
	if (ById('cookie')) ById('cookie').innerHTML=v
	if (ById('calendar')) {Calendar(0)}
}
function getCookie(a) {return (arguments[0]?arguments[0]:Cookie).replace(/;/g,'\n').split('\n')}
function killCookie(n) {writeCookie(n,'')}
function readCookie(n) {var i,j=0,c=getCookie();n=trim(n);for(i in c) if (c[i]) {nv=c[i].split('=');if (nv[0]==n) {return nv[1]}};return n.indexOf('ex-')==0?0:''}
function writeCookie(n,v) {var i,j=0,k,oc=Cookie,c=getCookie();n=trim(n);v=trim(v);if (readCookie(n)==v) return v;if (n) {Cookie='';for(i in c) if (c[i]) {k=0;nv=c[i].split('=');if (nv[0]==n) {nv[1]=v;j=1;k=1};addCookie(nv[0],nv[1],k)};if (j==0) addCookie(n,v,1)};return v}
function addCookie(n,v,c) {
	var e=ById(n),f=ById(n+'x');Cookie+=n+'='+v+'\n';
	var r=''
    if (c) {
	    if (n.indexOf('ex-')==0&&window.extrasChange) extrasChange(n.substr(3),v);
		if (e) if (e.value!=v) {SetVal(e,v);add_eclass(e,'changed');cookieDebug(e.value==v?n:n.red(),v.red())}
		if (f) if (f.value!=v) {r+=f.id+'='+v+'\n';SetVal(f,v);add_eclass(f,'changed')}
	}
	cookieDebug(n,v,Cookie)
	if (r) alert(r)
}

// cms helpers
var TimeOn=new Date(),lastCookie='',cmsFrame=null,cmsStat=get_root()+'/cms/bin/ccms-stat.php'
function cms_init() {
	Cookie='';
	if (ById('cookie')) Cookie=ById('cookie').innerHTML;
	if (!cmsFrame) {
		var i=document.createElement('iframe')
		with(i){setAttribute('id','cms-stat');setAttribute('frameborder','no');setAttribute('scrolling','no');setAttribute('src',cmsStat+'?init=1')}
		cmsFrame=i;ById('cms-stat-holder').appendChild(i)
		onunload=cms_stat
	}
}
function cms_stat() {
	if (cmsFrame&&lastCookie!=Cookie) {
		cmsFrame.src=cmsStat+'?cookie='+Cookie.replace(/\n/g,';')
		lastCookie=Cookie;
	}
}

