function return_by_id(id) { 
	if (document.getElementById) 
		var return_var = document.getElementById(id); 
	else if (document.all) 
		var return_var = document.all[id]; 
	else if (document.layers) 
		var return_var = document.layers[id]; 
	else return false;
	return return_var; 
}
function htmlencode(txt) {//modified from http://www.yuki-onna.co.uk/html/encode.html
     encodedHtml=escape(txt);
     encodedHtml=encodedHtml.replace(/\//g,"%2F");
     encodedHtml=encodedHtml.replace(/\?/g,"%3F");
     encodedHtml=encodedHtml.replace(/=/g,"%3D");
     encodedHtml=encodedHtml.replace(/&/g,"%26");
     encodedHtml=encodedHtml.replace(/@/g,"%40");
     return encodedHtml;
}
function return_xmlhttp(){
	if (window.XMLHttpRequest) {
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	} else if (window.ActiveXObject) {
	  // code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	} else {
	  alert("Your browser does not support XMLHTTP!");
	}
	return xmlhttp;
}
function javaxml(id,target,param,append) {
	var xmlhttp;
	xmlhttp=return_xmlhttp();
	xmlhttp.open("POST","_xml/"+id+".php",true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", param.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4) {
			txt = xmlhttp.responseText;
			//append requires jquery
			if (append) $(target).append(txt);
			else return_by_id(target).innerHTML=txt;
		}
	}
	xmlhttp.send(param);
}
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com
function replace(f, r, s){
	var ra = r instanceof Array, sa = s instanceof Array, l = (f = [].concat(f)).length, r = [].concat(r), i = (s = [].concat(s)).length;
	while(j = 0, i--)
		while(s[i] = s[i].split(f[j]).join(ra ? r[j] || "" : r[0]), ++j < l);
	return sa ? s : s[0];
}

function add_form(type,val){
	if (val == null) val=false;
	i = $('#booking').find('.form').length;
	param = 'type='+type+'&val='+val+'&i='+i;
	javaxml('form','#booking',param,true);
	$('.form_head').each(function() {
		$(this).children('.close').css('display','block');
		$(this).find('.close').find('a').click(function(e) {
			e.preventDefault();
			$(this).parents('.form_head').next('.form').animate({height:0},200);
			$(this).parents('.form_head').next('.form').remove().delay(200);
			$(this).parents('.form_head').remove().delay(200);
		});
		$(this).next('.form').animate({height:20},1000);
		if ($(this).next('.form').attr('rel')==null) $(this).next('.form').attr('rel',$(this).next('.form').css('height'));
		$(this).children('.form_reveal').toggle(
			function() {
				$(this).text('close form');
				$(this).parent('.form_head').next('.form').animate({height:$(this).parent('.form_head').next('.form').attr('rel')},1000);},
			function() {
				$(this).text('open form');
				$(this).parent('.form_head').next('.form').animate({height:20},1000);}
		);
		$(this).children('.form_reveal').text('open form');
	});
	//$('#booking_button').find('input').value='send forms';
}
function invoice_switch(i){
	if ($('#check_inv'+i).attr('checked')) {
		$('#invoice'+i).css('display','');
		if ($('#check_inv'+i).parent('.form').attr('rel')!=undefined) {
			$('#check_inv'+i).parent('.form').animate({height:('+='+$('#invoice'+i).css('height'))},500);
			new_rel = $('#check_inv'+i).parent('.form').attr('rel').substr(0,$('#check_inv'+i).parent('.form').attr('rel').length-2)*1 + $('#invoice'+i).css('height').substr(0,$('#invoice'+i).css('height').length-2)*1;
			$('#check_inv'+i).parent('.form').attr('rel',new_rel+'px');
		}
	} else {
		if ($('#check_inv'+i).parent('.form').attr('rel')!=undefined) {
			$('#check_inv'+i).parent('.form').animate({height:('-='+$('#invoice'+i).css('height'))},500);
			new_rel = $('#check_inv'+i).parent('.form').attr('rel').substr(0,$('#check_inv'+i).parent('.form').attr('rel').length-2)*1 - $('#invoice'+i).css('height').substr(0,$('#invoice'+i).css('height').length-2)*1;
			$('#check_inv'+i).parent('.form').attr('rel',new_rel+'px');
		}
		$('#invoice'+i).css('display','none');
	}
}
$(function(){
	var dropdown = false;
	$('input:checkbox').each(function(){
		if($(this).parent().next().find('input:checkbox').attr('name')==$(this).attr('name')||$(this).parent().prev().find('input:checkbox').attr('name')==$(this).attr('name')) {
			$(this).parent().click(function(e){
				if(!$(e.target).is('input:checkbox')){
					if($(this).children('input:checkbox').attr('checked')) {
						$(this).children('input:checkbox').attr('checked',false);
					} else {
						$(this).children('input:checkbox').attr('checked',true);
					}
				}
			}).mouseenter(function(e){
				var back = $(this).css('background-color');
				if (back=='transparent') back='#fff';
				$(this).css('background-color',$(this).css('color'));
				$(this).css('color',back);
			}).mouseleave(function(e){
				var back = $(this).css('background-color');
				if (back=='transparent') back='#fff';
				$(this).css('background-color',$(this).css('color'));
				$(this).css('color',back);
			}).css('cursor','pointer');
			$(this).css('margin','0px');
		}
	});
	$('.dropdown').before('<div class="dropdown-select"><a>multiple select (click)</a></div>')
	$('.dropdown').parents('form').submit(function(){$('.dropdown').css('display','');return true;});
	$('.dropdown-select').css('cursor','pointer').css('padding','5px');
	$('.dropdown').css('width','305px').css('background-color','#fff').css('border','1px #444 solid').css('position','absolute').css('left','220px').css('display','none');
	var drop = false;
	$('.dropdown-select').toggle(function(e){
		$('.dropdown').hide()
		$(this).next('.dropdown').show();
		dropdown = $(this).next('.dropdown');
		$(window).bind('click',function(e){
			//alert($(e.target).parents(dropdown).length);
			if ($(e.target).parents(dropdown).length<12) {
				$(dropdown).css('display','none');
				dropdown = false;
				$(window).unbind('click');
			}
		});
	},function(e){
		$(this).next('.dropdown').css('display','none');
		dropdown = false;
	});
	$('select[class=yesno]').each(function(){
		$(this).after('<div>yes <input type="radio" name="'+$(this).attr('name')+'" value="yes" />&nbsp;&nbsp;no <input type="radio" name="'+$(this).attr('name')+'" value="no" /></div>');
		if($(this).val()=='no') $(this).next().children('input[value=no]').attr('checked',true);
		if($(this).val()=='yes') $(this).next().children('input[value=yes]').attr('checked',true);
		$(this).next().find('input[type=radio]').each(function(){
			$(this).click(function(e){
				$(this).parent().prev('select').val($(this).val());
			});
		});
		$(this).css('display','none');
	});
	$('.info').each(function(){
		$(this).css('display','none').before('<img src="_img/i.gif" alt="more information" title="more information" />').prev().mouseover(function(){
			$(this).next().css('display','');
		}).mouseout(function(){
			$(this).next().css('display','none');
		});
	});
});