function _assign(obj,s,r) { obj.onmouseover = function() { obj.src = r; }; obj.onmouseout = function() { obj.src = s; }; obj.border=0; obj.onload=''; }

function _rollover(obj,r) { _assign(obj,obj.src,r); }

function load_tour(tour_name) {
	tour_name = tour_name.toLowerCase();

	var a = $('tab').getElementsByTagName('a');

	for(var i=0;i<a.length;i++)
	{
		a[i].className='menu_deactive';
	}
	$('tab_'+tour_name).className='menu_active';
	//$('tab').style.backgroundImage = 'url(images/tab_'+tour_name+'.jpg)';

	a = $('tab_content').getElementsByTagName('div');
	for(var i=0;i<a.length;i++)
	{
		a[i].style.display = 'none';
	}
	$('tours_'+tour_name).style.display = '';
	Cookie.set('home_selected_tab', tour_name);
}

function date_changed(id)
{
	new Ajax('/booking.php?id='+id, {
		data: $('f1'),
		update: $('booking'),
		onRequest:function(){
			$('booking').innerHTML = '<img src=/ui/clock.gif align=absmiddle> Loading...';
		},
		onComplete: function() {
			calc_total();
			change_hotels(document.fcart.departure_from.value);
		}
	}).request();
}

function intg(expr)
{
	expr = new String(expr).replace(/,/g, '');
	var i = parseInt(expr);
	if (isNaN(i))
	    return 0;
	else
	    return i;
}


function floatg(expr)
{
	expr = new String(expr).replace(/,/g, '');
	var i = parseFloat(expr);
	if (isNaN(i))
	    return 0;
	else
	    return i;
}


function round(num,n)
{
	var i = floatg(num);
    return i.toFixed(n);
}

var room_capacity = 0;
var no_of_person = 0;
var no_of_adult = 0;
var no_of_infant = 0;
function calc_total()
{
	var total = 0;
	var f = $('f1');
	var i;

	i = 1;
	no_of_person = 0;
	no_of_adult = 0;
	no_of_infant = 0;

	while($('g'+i) != undefined)
	{
		total += intg($('g'+i).value) * $('g'+i).title;
		// 1/10/2008 11:29:44 AM - exclude children in no_of_person calculation
		if (!$('lbl_g'+i).innerHTML.test(/infant/i))
			no_of_person += intg($('g'+i).value);
		// infant should not more than adult
		if ($('lbl_g'+i).innerHTML.test(/infant/i)) no_of_infant += intg($('g'+i).value);
		if ($('lbl_g'+i).innerHTML.test(/adult/i)) no_of_adult += intg($('g'+i).value);
		i++;
	}

	i = 1;
	while($('u'+i) != undefined)
	{
		if ($('u'+i).checked) total += no_of_person * $('u'+i).title;
		i++;
	}

	i = 1; room_capacity = 0;
	while($('r'+i) != undefined)
	{

		s = $('r'+i).title.split(",");
		total += intg($('r'+i).value) * s[0];
		if (s[1] == 'x') room_capacity = no_of_person;
		else room_capacity += intg($('r'+i).value) * s[1];
		i++;
	}

	$('total').innerHTML = round(total,2);
	$('totalh').value = round(total,2);
	if ($('totalp') != undefined) $('totalp').innerHTML = no_of_person + no_of_infant;
	if ($('totalc') != undefined) $('totalc').innerHTML = room_capacity + ' persons';
}


//show form list name

function make_drag(obj)
{
	$(obj).makeDraggable({handle:$('handle'),onStart:function(){$(obj).setOpacity(0.7)},onComplete:function(){$(obj).setOpacity(1)}});
}

function check_tb1()
{
	var f = document.f;

	if (f.fdepart.value == '')
	{
		alert("Please insert Departure Date.");
		f.fdepart.focus();
		return;
	}
	if (f.freturn.value == '')
	{
		alert("Please insert Return Date.");
		f.freturn.focus();
		return;
	}
	if (f.freturn.value < f.fdepart.value)
	{
		alert("Return date cannot be less than depart date.")
		return;
	}
	if (f.cname.value == '')
	{
		alert("Please insert Contact Person's Name.");
		f.cname.focus();
		return;
	}
	if (f.ctel.value == '')
	{
		alert("Please insert Contact Person's Tel.");
		f.ctel.focus();
		return;
	}
	if (f.cemail.value == '')
	{
		alert("Please insert Contact Person's Email.");
		f.cemail.focus();
		return;
	}
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(f.cemail.value) != true)
	{
	    f.cemail.focus();
		alert("You have enter an invalid email!")
	    return;
	}
	$('tb1').style.display='none';
	$('tb2').style.display='';
	$('subtitle').innerHTML = 'Please fill in the information below. You can fill up to 5 participants.';
}

function goback()
{
	$('tb1').style.display='';
	$('tb2').style.display='none';
	$('subtitle').innerHTML = 'Please fill in the information below.';
}

function formsubmit()
{
	if (validate_data())
	document.f.submit();
}

function showform(id,desc)
{
	var obj = $('qform');

	$('id').value = id;
	$('desc').value = desc;

	obj.style.display = '';
	obj.style.left =  (window.getScrollLeft()+(window.getWidth()-390)/2) + 'px';
	obj.style.top = (window.getScrollTop()+(window.getHeight()-350)/2) +'px';

	$('curtain').setStyle('width', window.getScrollWidth() + 'px')
	$('curtain').setStyle('height', window.getScrollHeight() + 'px');
	$('curtain').setOpacity(0);
	$('curtain').style.display = '';

	// fade in curtain
	new Fx.Style('curtain', 'opacity', {duration:500}).start(0,0.8);
	//$('curtain').setOpacity(0.8);
}

function form_cancel()
{
	$('qform').style.display='none';
	$('curtain').style.display='none';
}

function validate_data()
{
	var table = document.getElementById('tb2');
	var a = table.getElementsByTagName("input");

	for (i=0;i<a.length;i++)
	{
	    var s = a[i].name;
		if (s.match(/\.*name\.*/))
		{
			if (a[i].value != '' && a[i+1].value == '')
			{
				a[i+1].focus();

				var s = a[i+1].name;
				t = s.replace(/^\d+/,'');
				t = t.replace(/\d+$/,'');
				y = s.match(/\d+$/);
				alert("Please insert "+t+"#"+y+".");
				return false;
			}
		}
	}

		if(a[0].value == '')
	    {
			a[0].focus();

			var s = a[0].name;
			t = s.replace(/^\d+/,'');
			t = t.replace(/\d+$/,'');
			y = s.match(/\d+$/);
			alert("Please insert "+t+"#1.");
			return false;
		}

		if(a[1].value == '')
	    {
			a[1].focus();

			var s = a[1].name;
			t = s.replace(/^\d+/,'');
			t = t.replace(/\d+$/,'');
			y = s.match(/\d+$/);
			alert("Please insert "+t+"#1.");
			return false;
		}


	return true;
}

function check_valid()
{

	// no of persons must be > 0
	if (no_of_person <= 0)
	{
		alert("There must be at least 1 person going for this tour ;)");
		return false;
	}

	// infant should not more than adult
	if (no_of_infant > no_of_adult)
	{
		alert("Number of infant must not exceed number of adult.");
		return false;
	}

	// check maximum no_of_person
	if (no_of_person > $('f1').total_group_quantity.value)
	{
		alert('Sorry, you can\'t book for more than '+$('f1').total_group_quantity.value+' persons (does not include infants)');
		return false;
	}

	if (no_of_person > room_capacity)
	{
		alert("Too many people for room capacity. Please change the room choices.");
		return false;
	}

	if (no_of_person < room_capacity)
	{
		var r = confirm('Your total person is lesser than room capacity.\nClick OK to proceed.\nClick Cancel to change your choices.');
		if (!r) return false;
	}

	return true;
}

function valid_names()
{
	s = document.fnamelist.getElementsByTagName('input')
	for(i=0; i<s.length; i++)
	{
		if (/^name/.test(s[i].name))
		{
			if (s[i].value == '')
			{alert("Please insert the tourists' name");
				s[i].select();
				s[i].focus();
				return false;
			}

		}

		if (/^age/.test(s[i].name))
		{
			if (intg(s[i].value)<=0)
			{alert("Please insert the tourists' age");
				s[i].select();
				s[i].focus();
				return false;
			}

		}
	}

	s = document.fnamelist.getElementsByTagName('select')
	for(i=0; i<s.length; i++)
	{
		if (s[i].value=='')
		{
			alert("Please select a valid option for "+s[i].title);
			s[i].focus();
			return false;
		}
		s[i].disabled = false;
	}
	return true;
}

function lock_gender(n,g)
{
	if(document.fnamelist.elements[n].value == 'Dr')
	{
		//document.fnamelist.elements[g].disabled = false;
	}
	else
	{
		//document.fnamelist.elements[g].disabled = true;
		if (document.fnamelist.elements[n].value == 'Mr')
			document.fnamelist.elements[g].selectedIndex = 1;
		else
			document.fnamelist.elements[g].selectedIndex = 2; 
	}
}

// return radio button's value
function getRadioValue(objlist)
{
	var i;
	if (objlist)
	{
		for(i=0;i<objlist.length;i++)
		{
		    if (objlist[i].checked) return objlist[i].value;
		}
	}
	return undefined;
}

function show_content(d)
{
        if ($('content_def')!=undefined) $('content_def').style.display = 'none';
        if ($('content_por')!=undefined) $('content_por').style.display = 'none';
        if ($('content_ch')!=undefined) $('content_ch').style.display = 'none';
        if ($('content_jp')!=undefined) $('content_jp').style.display = 'none';
        if ($('content_fr')!=undefined) $('content_fr').style.display = 'none';
        $(d).style.display = '';
}

function change_hotels(dep)
{
	var dp = dep.toLowerCase();
	$('bookingbox_hotels').innerHTML = '<p><a href="'+get_octo_url(dp)+'" target=_blank><img border=0 src="/images/hotels/210x210_'+dp+'.jpg"></a></p>'
}

function get_octo_url(dp)
{
	dp = dp.toLowerCase();
	if (dp=='london')
		return 'http://www1.octopustravel.com/searchAction.form?brandCode=LON_INTF01_ONL&country=GB&languageCode=EN&currency=GBP&destinationId=2643743';
	else if (dp=='cambridge')
		return 'http://www1.octopustravel.com/searchAction.form?brandCode=LON_INTF01_ONL&country=GB&languageCode=EN&currency=GBP&destinationId=2653941';
	else if (dp=='oxford')
		return 'http://www1.octopustravel.com/searchAction.form?brandCode=LON_INTF01_ONL&country=GB&languageCode=EN&currency=GBP&destinationId=2640729';
	else if (dp=='canterbury')
		return 'http://www1.octopustravel.com/searchAction.form?brandCode=LON_INTF01_ONL&country=GB&languageCode=EN&currency=GBP&destinationId=2653877';
	else if (dp=='edinburgh')
		return 'http://www1.octopustravel.com/searchAction.form?brandCode=LON_INTF01_ONL&country=GB&languageCode=EN&currency=GBP&destinationId=2650225';
	else if (dp=='Ramsgate')
		return 'http://www1.octopustravel.com/searchAction.form?brandCode=LON_INTF01_ONL&country=GB&languageCode=EN&currency=GBP&destinationId=2639660';
 	else
 	    return 'http://www1.octopustravel.com/searchAction.form?brandCode=LON_INTF01_ONL';

}
