
// wrapper for getElementById
function $(id) {
	return document.getElementById(id);	
}

// initilisation function called onload
function init() {
	if (getUrlParameters('s') != '') {
		nav();
	}
	setTimeout('fade_banner(1)', 1000);
	// try this if we're on the home page
	try {
		if (document.location.hash.substr(1)) {
			home_nav(document.location.hash.substr(1));
		} else {
			home_nav('ideas');
		}
		setTimeout('home_fade()', 10000);
	} catch(e) { }
}

// home page image fader
function home_fade() {
	// this is wrapped in a try because we need to try and run it even if we're not on the home page
	try {
		fade(1, 0, 1);
	} catch(e) {}
}

// set the opacity level of an id to a certain percentage
function setOpacity(id, opacity) {
	try {
		var element = $(id);
		element.style.opacity = (opacity / 100);
		element.style.MozOpacity = (opacity / 100);
		element.style.KhtmlOpacity = (opacity / 100);
		element.style.filter = 'alpha(opacity='+opacity+')'; 
		element.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity='+opacity+')';
	} catch(e) {}
}

// fade in the top banner on every site
function fade_banner(opacity) {
	var banner_banner = $('banner_banner');
	var banner_specials = $('banner_specials');
	var banner_lastminute = $('banner_lastminute');	
	
	banner_banner.style.display = 'block';
	banner_specials.style.display = 'block';
	banner_lastminute.style.display = 'block';
	// while its not fully faded in, fade in
	if (opacity < 100) {
		setOpacity('banner_banner', opacity);
		setOpacity('banner_specials', opacity);
		setOpacity('banner_lastminute', opacity);	
		setTimeout("fade_banner("+(opacity+1)+")", 30);
	} else {
		// after we've faded in, rotate the ad
		var rand = Math.floor(3*Math.random())+1;
		$('banner_banner2').innerHTML = '<a href="http://web.travelonline.com/specials/'+rand+'.html"><img src="http://web.travelonline.com/specials/'+rand+'.gif" alt="" class="banner" /></a>';
		setTimeout("banner_rotate(1)", 4000);
		setOpacity('banner_banner2', 0);
		$('banner_banner2').style.display = 'block';
	}
}
// rotate
function banner_rotate(opacity) {
	if (opacity < 100) {
		setOpacity('banner_banner2', opacity);
		setTimeout("banner_rotate("+(opacity+1)+")", 5);
	}
}

// onfocus for search box in nav
function search_focus() {
	var q = $('q');
	if (q.value == 'Search...') {
		q.value = '';
		q.style.fontStyle = 'normal';
		q.style.color = '#000000';
	}
}
// onblur for search box in nav
function search_blur() {
	var q = $('q');
	if (q.value == '') {
		q.value = 'Search...';	
		q.style.fontStyle = 'italic';
		q.style.color = '#999999';
	}
}
// submit for search
function search_submit() {
	var q = $('q');
	if (q.value != '' && q.value != 'Search...') {
		return true;
	}
	return false;
}

// get url parameters (_GETish)
function getUrlParameters(name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if (results == null) {
		return '';
	} else { 
		return results[1];
	}
}

/* Determine the index for the value specified (will pad with 0 at the beginning - for dates etc) */
function find_index(field, value) {
	field = $(field);
	for (i = 0; i < field.length; i++) {
		if (field[i].value == value) {
			return i;	
		}
	}
}

function ajax_sender() {
	if (this.request == '') {
		this.request = '?sid='+Math.random();	
	} else {
		this.request = this.request + '&sid='+Math.floor(Math.random()*99999999);	
	}
    this.xmlHttp.open('get', this.page + this.request);
    var xmlHttpObject = this.xmlHttp;
    var target = this.target;
	var type = this.type;
	var status_start = this.status_start;
	var status_finish = this.status_finish;

	try {
		eval(status_start);	
	} catch(e) { }

    if (target != '') {
        this.xmlHttp.onreadystatechange = function() {
            if (xmlHttpObject.readyState == 4) {
				switch (type) {
					case 'select': {
						$(target).selectedIndex = find_index(target, xmlHttpObject.responseText);
						break;
					}
					case 'alert': {
						alert(xmlHttpObject.responseText);
						break;
					}
					case 'div': {
						$(target).innerHTML = xmlHttpObject.responseText;
						break;	
					}
				}
				try {
					eval(status_finish);	
				} catch(e) {
					
				}
            }
        };
    }
    this.xmlHttp.send(null);
}

function ajax(page, request, element, type, status_start, status_finish) {
    if (arguments.length < 2) {
        return;
    }
    this.target = element;
    this.page = page;
	this.type = type;
    if (request == null) {
        this.request = '';
    } else {
        this.request = request;
    }
	this.status_start = status_start;
	this.status_finish = status_finish;
    try {
        // Firefox, Opera 8.0+, Safari
        this.xmlHttp = new XMLHttpRequest();
    } catch (e) {
        // Internet Explorer
        try {
            this.xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
        } catch (e) {
            try {
                this.xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
            } catch (e) {
                alert('Your browser does not support AJAX!');
                return false;
            }
        }
	}
    this.send = ajax_sender;
	this.send();
}




//////////////////////////////////
// iframe quote wizard code
//////////////////////////////////
af_value = getUrlParameters('af');

function findPosY(objName) {
	eval("obj = document.getElementById('" + objName + "')");
    var curtop = 0;
    if(obj.offsetParent)
        while(1){
          curtop += obj.offsetTop;
          if(!obj.offsetParent) break;
          obj = obj.offsetParent;
        }
    else if(obj.y) curtop += obj.y;
	curtop = curtop -1;
    return curtop;
}

/*function findPosX(objName) {
	eval("obj = document.getElementById('" + objName + "')");
    var curleft = 0;
    if(obj.offsetParent)
        while(1){
          curleft += obj.offsetLeft;
          if(!obj.offsetParent) break;
          obj = obj.offsetParent;
        }
    else if(obj.x) curleft += obj.x;
	curleft = curleft - 1;
    return curleft;
}*/

function findScrollY() {
	scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
	} else if( document.body && document.body.scrollTop) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
	} else if( document.documentElement && document.documentElement.scrollTop) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
	}
	return scrOfY;
}

function findScrollX() {
	scrOfX = 0;
	if( typeof( window.pageXOffset ) == 'number' ) {
		//Netscape compliant
		scrOfX = window.pageXOffset;
	} else if( document.body && document.body.scrollLeft) {
		//DOM compliant
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && document.documentElement.scrollLeft) {
		//IE6 standards compliant mode
		scrOfX = document.documentElement.scrollLeft;
	}
	return scrOfX;
}


function showInline(id, i, supplier_code_id) {	// id = which window to display, i = 1:show window OR 0:close window, supplier_code_id = the supplier code index
	if (i == 1) {
		$('rate_buttons').style.display = 'none';
		$(id+'_div').style.display = 'block';
		if(af_value !="") agent_value = "1"; else agent_value = $('agent').value;	// affiliates always get agent rates
		switch (id) {
			case 'wizard': {
				$(id+'_iframe').src = 'http://dm.travelonline.com/w/wizard.php?agent='+agent_value+'&supplier_code='+$('supplier_code-'+supplier_code_id).value+'&option_code='+$('option_code').value+'&referer='+$('referer').value+'&acctour='+$('acctour').value+'&tour='+$('tour').value+'&pass='+$('pass').value+'&af='+af_value+'&iframe=1';
				break;
			}
			case 'bookingwizard': {
				$(id+'_iframe').src = 'http://dm.travelonline.com/w/wizard.php?agent='+agent_value+'&supplier_code='+$('supplier_code-'+supplier_code_id).value+'&option_code='+$('option_code').value+'&referer='+$('referer').value+'&acctour='+$('acctour').value+'&tour='+$('tour').value+'&pass='+$('pass').value+'&af='+af_value+'&iframe=1';
				break;
			}
			case 'lastminute': {
				$(id+'_iframe').src = $('last_minute').value;
				break;
			}
			case 'specials': {
				$(id+'_iframe').src = 'http://www.specials.travelonline.com/fp_search.php?'+$('specials').value;
				break;
			}
		}
		growInline("'"+id+"'");
		smoothScrollTo(findScrollX(), findScrollY(), 0, (findPosY('rate')-50))
	} else {
		$('rate_buttons').style.display = 'block';
		$(id+'_div').style.display = 'none';
		$(id+'_iframe').src = 'http://web.travelonline.com/loading.html';
	}
}

function smoothScrollToProcess(from_x, from_y, to_x, to_y) {
	if ((from_y <= (to_y-10)) || (from_y >= (to_y+10))) {
		window.scrollTo(from_x, from_y);
		if (from_y < to_y) {
			timer = window.setTimeout("smoothScrollToProcess("+findScrollX()+", "+(findScrollY()+10)+", "+to_x+", "+to_y+");", 4);
		} else {
			timer = window.setTimeout("smoothScrollToProcess("+findScrollX()+", "+(findScrollY()-10)+", "+to_x+", "+to_y+");", 4);
		}
	}
}

function smoothScrollTo(from_x, from_y, to_x, to_y) {
	if (from_y < to_y) {
		timer = window.setTimeout("smoothScrollToProcess("+findScrollY()+", "+(findScrollY()+10)+", "+to_x+", "+to_y+");", 4);
	} else {
		timer = window.setTimeout("smoothScrollToProcess("+findScrollY()+", "+(findScrollY()-10)+", "+to_x+", "+to_y+");", 4);
	}
}

function growInlineProcess(id, height) {
	height = height + 10;
	$(id+'_iframe').style.height = height+'px';
	$(id+'_middle').style.height = height+'px';
	if (height < 440) {
		window.setTimeout("growInlineProcess('"+id+"', "+height+");", 4);
	}
}

function growInline(id) {
	timer = window.setTimeout("growInlineProcess("+id+", 100);", 4);
}

function showSeasonRates() {
	window.open("http://dm.travelonline.com/common/show_seasons.php?supplier_code="+$('supplier_code-0').value+"", "seasonrates", "menubar=0,location=0,resizable=0,scrollbars=1,width=350,height=350");
}

function destinations(type) {
	if (type == 'az') {
		if ($('destinations-az').innerHTML == '') {
			ajax('/destinations.html', '', 'destinations-az', 'div', '', '$(\'destinations-regions\').style.display = \'none\';');
		} else {
			$('destinations-az').style.display = 'block';
			$('destinations-regions').style.display = 'none';
		}
		
	}
	if (type == 'regions') {
		$('destinations-regions').style.display = 'block';
		$('destinations-az').style.display = 'none';
	}
}