function showothercities(tofrom){
	Element.hide(tofrom+'link');
	Element.hide(tofrom+'select');
	Element.show(tofrom+'ajax');
	Element.show(tofrom+'_airports_link');
}

function do_submit(){
	if(calculateRooms() && validatedates() && validatecities()){
		return true;
	}else{
		return false;
	}
}
  
function validatedates(){
	checkin=Date.parseDate($('checkin').value,DATE_FORMAT);
	checkout=Date.parseDate($('checkout').value,DATE_FORMAT);
	if($('checkin').value==g_default_cal_val || $('checkout').value==g_default_cal_val || checkin.getTime()>checkout.getTime()){
		alert(Error[0]);
		return false;
	}else{
		$('checkin').disabled = true;
		$('checkout').disabled = true;
		$('checkin_h').disabled = false;
		$('checkout_h').disabled = false;
		$('checkin_h').value = checkin.dateFormat('Y-m-d');
		$('checkout_h').value = checkout.dateFormat('Y-m-d');
		return true;
	}
	
} 

function validatecities(){
	if($('toajax').style.display=='none'){
		$('to').value=$('tos').value;
		
	}
	
	
	if($('to').value==''){
		alert(Error[3]);
		return false;
	}else{
		$('tos').disabled=true;
		return true;
	}
	
} 
  
function calculateRooms(){
	var roomcount=$('roomcount').value;
	var roomarray=Array();
	var roomstring='';
	$('adults').value=0;
	$('children').value=0;
	for(x=1; x<=roomcount; x++){
		roomtype=$('roomtypes_'+x).value;
		if(roomtype=='SB'){
			roomtype=(document.forms.formrooms['singles'+x][0].checked==true)?'SB':'TS';
		}
		if(roomtype=='DB'){
			roomtype=(document.forms.formrooms['doubles'+x][0].checked==true)?'DB':'TB';
		}
		childage=($('withchild'+x).checked==true)?(isNaN(parseInt($('child_'+x).value))?0:parseInt($('child_'+x).value)):0;
		cots=(isNaN(parseInt($('cots_'+x).value))?0:parseInt($('cots_'+x).value));
		
		if(childage>0){
			$('children').value=parseInt($('children').value)+1;
		}
		if(roomtype=='SB' || roomtype=='TS'){
			$('adults').value=parseInt($('adults').value)+1;
		}
		if(roomtype=='DB' || roomtype=='TB'){
			$('adults').value=parseInt($('adults').value)+2;
		}
		if(roomtype=='TR'){
			$('adults').value=parseInt($('adults').value)+3;
		}
		if(roomtype=='Q'){
			$('adults').value=parseInt($('adults').value)+4;
		}
		roomstring+=roomtype+'-'+cots+'-'+childage+':';
		
	}		
	$('rooms').value=roomstring;
	return true;
}
 

  
function roomCountChanged(count){

	for(x=1;x<5;x++){
		if(x<=count){
			Element.show($('room'+x));
		}else{
			Element.hide($('room'+x));
		}
		
	}
	
}  


function roomTypesChanged(roomnr, type){

	if(type=='SB'){
		Element.show('single'+roomnr);
	}
	if(type!='SB'){
		Element.hide('single'+roomnr);
	}
	
	if(type=='DB'){
		Element.show('double'+roomnr);
		Element.show('wchild'+roomnr);
	}
	if(type!='DB'){
		Element.hide('double'+roomnr);
		Element.hide('wchild'+roomnr);
	}
	if(type=='TR'){
		Element.show('triple'+roomnr);
	}
	if(type!='TR'){
		Element.hide('triple'+roomnr);
	}
	if(type=='Q'){
		Element.show('quad'+roomnr);
	}
	if(type!='Q'){
		Element.hide('quad'+roomnr);
	}

}  

function showchildren(roomnr, show){
	if(show){
		Element.show('children'+roomnr);
	}else{
		Element.hide('children'+roomnr);
	}
	
}
  
function ClearOptions(OptionList) {
   for (x = OptionList.length; x >= 0; x = x - 1) {
      OptionList[x] = null;
   }
}


function AddToOptionList(OptionList, OptionValue, OptionText) {
   OptionList[OptionList.length] = new Option(OptionText, OptionValue);
}  
  
  

function paxNrChange(){
	var adults=$('adults').value;
	var childrenlist=$('children');
	var infantlist=$('infants');
	ClearOptions(childrenlist);
	ClearOptions(infantlist);
	for(x=0;x<(10-adults);x++){
		AddToOptionList(childrenlist, x,x)
		if(x<=adults)
			AddToOptionList(infantlist, x,x)
	}
	
}
  
  
function resetDefaults(){
	roomstr=$('rooms').value;
	roomtypes=roomstr.split(':');
	roomcnt=(roomstr.substr((roomstr.length-1),1)==':')?(roomtypes.length-1):roomtypes.length;
	for(x=0; x<roomcnt; x++){
		rnr=x+1;
		room=roomtypes[x].split('-');
		switch(room[0]){
			case 'SB':
				$('roomtypes_'+rnr).selectedIndex=0;
				Element.show('single'+rnr);
				document.forms.formrooms['singles'+rnr][0].checked=true;
				break;
			case 'TS':
				$('roomtypes_'+rnr).selectedIndex=0;
				Element.show('single'+rnr);
				document.forms.formrooms['singles'+rnr][1].checked=true;
				break;
			case 'DB':
				Element.hide('single'+rnr);
				$('roomtypes_'+rnr).selectedIndex=1;
				Element.show('wchild'+rnr);
				Element.show('double'+rnr);
				document.forms.formrooms['doubles'+rnr][0].checked=true;
				if(room[2]>0){
					$('withchild'+rnr).checked=true
					$('child_'+rnr).selectedIndex=room[2]-2;
					Element.show('children'+rnr);
				}
				break;
			case 'TB':
				Element.hide('single'+rnr);
				$('roomtypes_'+rnr).selectedIndex=1;
				Element.show('wchild'+rnr);
				Element.show('double'+rnr);
				document.forms.formrooms['doubles'+rnr][1].checked=true;
				if(room[2]>0){
					$('withchild'+rnr).checked=true
					$('child_'+rnr).selectedIndex=room[2]-2;
					Element.show('children'+rnr);
				}
				break;
			case 'TR':
				Element.hide('single'+rnr);
				Element.show('triple'+rnr);
				$('roomtypes_'+rnr).selectedIndex=2;
				break;
			case 'Q':
				Element.hide('single'+rnr);
				Element.show('quad'+rnr);
				$('roomtypes_'+rnr).selectedIndex=3;
				break;
		}
		
	}

	$('roomcount').selectedIndex=roomcnt-1;
	roomCountChanged(roomcnt);
}  
  
function hidecalendars(){
		if($('cal_iefix')) Element.hide('cal_iefix');
		if($('cal2_iefix')) Element.hide('cal2_iefix');
		if($('cal2').style.display!='none')
			new Effect.Fade($('cal2'),{duration:.2});
		if($('cal').style.display!='none')
			new Effect.Fade($('cal'),{duration:.2});

}  
//$('container_1').onclick=hidecalendars;
var g_caltimer;
var dohide=true;
function trytohidecalendars(){
	if(dohide)
		g_caltimer=setTimeout('hidecalendars();', 50);
	dohide=true;
}

function cancelCalHide(){
//alert('cancelinam' + g_caltimer);
	dohide=false;
	clearTimeout(g_caltimer);
}

function other_on_select(o, pos) {
	if (o.value == 'other')
	switch (pos) {
		case 0:
			showothercities('to');
			$('to').select();
			break;
	} 
}

document.onclick=trytohidecalendars;
  
 
  add_onload("resetDefaults();");
