/**
 * IE getElementByID Fix
 */
if (/msie/i.test (navigator.userAgent)) //only override IE
{
	document.nativeGetElementById = document.getElementById;
	document.getElementById = function(id)
	{
		var elem = document.nativeGetElementById(id);
		if(elem)
		{
			//make sure that it is a valid match on id
			if(elem.attributes['id'].value == id)
			{
				return elem;
			}
			else
			{
				//otherwise find the correct element
				for(var i=1;i<document.all[id].length;i++)
				{
					if(document.all[id][i].attributes['id'].value == id)
					{
						return document.all[id][i];
					}
				}
			}
		}
		return null;
	};
}

Event.observe(window, 'load', function() {
	
		if($('no_persons')!=undefined){
			updateCalculatorBirthdates();
		}
		
//		updateLotData();
/*		if($('search_refine')!=undefined){
			Event.observe('search_refine', 'submit', showAjaxPreloaderDetail);
		}
*/
//  		Event.observe('calculator', 'submit', calculatePrice);
//  		Event.observe('ButtonCalculate', 'click', calculatePrice);
/* 		if($('back_cookie_button')){
  			Event.observe('back_cookie_button', 'click', backCookieButton);
  		}
*/
});

function updateLotPL(id){
	
	var spans=$$('ul#lot_list li[class*="active"]');
	spans.each(function(object, index){
		object.removeClassName('active');
	});
	
	$('lot_list_'+id).addClassName('active');
	
	var tables=$$('div#pricelist_holder div');
	tables.each(function(object, index){
		object.hide();
	});
	
	$('lot_pl_'+id).show();
	
	return;
}


/**
 * Adds an fade layer over the detail page
 */
function showAjaxPreloaderDetail(e){
	scroll(0,0);
	e.stop();
	//Effect.Fade('content', {duration:0.5});
	setTimeout('null', 200);
	$('content').update('<div id="preloader_img_holder" align="center"><img src="'+WWW_DIR+'media/ajax-loader.gif" alt="Searching.." /></div>');
	//Effect.Appear('loading_holder', {duration:0.3});
	setTimeout('null', 200);
	$('search_refine').submit();
	return false;
}

function backCookieButton(e){
	scroll(0,0);
	Effect.Fade('data_holder', {duration:0.5});
	setTimeout('null', 200);
	$('loading_holder').update('<div id="preloader_img_holder"><img src="'+WWW_DIR+'media/ajax-loader.gif" alt="Searching.." /></div>');
	Effect.Appear('loading_holder', {duration:0.3});
	setTimeout('null', 200);
	return false;
}

/**
 * Ajax call to price calculator module
 */
var last_calc_lot_id='';
function calculatePrice(){
	
	//disable the calc button:
	$('ButtonCalculate').disabled="disabled";
	new Effect.Opacity('ButtonCalculate', {duration:0.6, from:1, to:0.4});
	$('CalculatorHolder').hide();
	$('calculator_loading').show();
	
	var calcType=$('calc_type').value;

	if(calcType=="hotel"){
		var adults=0;
		var youths=0;
		var children=0;
		var children_3=0;
		
		var num_persons=0;
		var addData="";
		if($('no_persons')){
			num_persons=$F('no_persons');
			for(var i=1;i<=num_persons;i++){
				addData+="&p"+i+"bd="+$F('person_bd_'+i);
			}
		}
		addData+='&adults='+$F('calc_adults');
	}else{
		var adults=$F('calc_adults');
		var youths=$F('calc_youths');
		var children=$F('calc_children');
		var children_3=$F('calc_children_3');
		var num_persons=adults;
		var addData="&adults="+adults+"&children_3="+children_3;
	}
	
	//get parameters
	var acc=$F('acc_holder');
	
	
	var lotData=$F('lot').split("|");
	if($('lot_name')){
		var lotTitle=$('lot_name').value;
	}else{
		var lotTitle=$('lot')[$('lot').selectedIndex].innerHTML;
	}

	var category=lotData[0];
	var from_date=$('sd2').value;
	var to_date=$('ed2').value;
	
	// UPDATED 22.10.2008
	var lang=$F('lang');
	//lang var appended to the ajax url request
	// UPDATE END
	var add_bed=0;
	if($('aditional_bed')){
		add_bed=$F('aditional_bed');
		
	}
	
	var pets=0;
	if($('calc_pets')){
		pets=$F('calc_pets');
	}
	
	var params="acc="+acc+"&category="+encodeURIComponent(category)+"&from_date="+from_date+"&to_date="+to_date+"&adults="+adults+"&youths="+youths+"&children="+children+"&children_3="+children_3+"&add_bed="+add_bed+"&lot_id="+lotData[2]+"&pets="+pets+"&num_persons="+num_persons+"&calc_type="+calcType+addData;

	var url = WWW_DIR+'ajax/ajax.priceCalculator.php?cmd=calc_full_price&'+params+"&lang="+lang;
	//send a requst
	new Ajax.Request(url, {
	  method: 'get',
	  onSuccess: function(transport) {

	  		var str=transport.responseText;
	  		var output=null;
	  		if(str.isJSON()==false || str.startsWith('{')==false || str.endsWith('}')==false){
	  			output.notice="System error";
	  		}else{
	  			output=transport.responseText.evalJSON(true);
	  		}
	  		
	   		//update the price holder elements		
	   		$('total_price').update(output.price);
	   		//$('persons').update(output.amount);
	   		//$('period').update(from_date+' - '+to_date);
	   		//$('days').update(output.duration);
	   		$('clean_price').update(output.clean_price);
	   		$('bed_price').update(output.abp);
	   		//$('add_beds').update(add_bed);
	   		$('taxes').update(output.taxes);
	   		$('reg_fee').update(output.reg_fee);
	   			   		
	   		if(parseInt(output.abp)<=0){
	   			$('add_bed_holder').hide();
	   		}else{
	   			$('add_bed_holder').show();
	   		}
	   		
	   		//check if there is some notice
	   		link = '#';
	   		if(output.notice!=""){
	   			$('calc_error').update(output.notice);
	   			
	   			Effect.Appear('error', {duration:0.2});
	   			$('total_price').update("0.00");	   			
	   			$('calculator_loading').hide();
	   			
	   			Effect.Appear('CalculatorHolder', {duration:0.5});
	   			new Effect.Opacity('ButtonCalculate', {duration:1, from:0.4, to:1});
	   			$('ButtonCalculate').disabled="";
	   			return;
	   		}else{
	   			//if there is no notice, show the booking button
				Effect.Appear('ButtonBook', {duration:0.2});
				$('calc_text').update(output.calc_text);	
				link=output.booknowlink;
				if(last_calc_lot_id!=""){
					if($('catering_'+last_calc_lot_id)!=undefined){
						$('catering_'+last_calc_lot_id).hide();
					}
				}
				if($('catering_'+lotData[2])!=undefined){
					$('catering_'+lotData[2]).show();
				}				
	   		}
	   		
	   		
			//alert(lotTitle);	
	   		
	   		//set lot title
	   		//$('lot_prefix').show();
	   		$('lot_title').update(lotTitle);
	  		$('lot_title').show();
	  		$('lot_for').show();
  		
	  		
	   		$('ButtonCalculateBack').disabled="";
			Effect.Appear('ButtonCalculateBack', {duration:0.7});
			last_calc_lot_id=lotData[2];
						
			//check discounts
	   		if(parseInt(output.discount)!=0 && output.discount!=""){
	   			$('discount').update(output.discount);
	   			//$('discount_price').update(output.discount_price);
	   			$('discount_amount').update(output.discount_amount);
	   			$('discount_holder').show();
	   			//$('clean_price').addClassName('strike-red');
	   		}else{
	   			//$('cp_holder').removeClassName('strike-red');
				$('discount_holder').hide();;
	   		}

	   		//show/hide elements
	   		Effect.Fade('calculator_loading', {duration:0.3});
	   		setTimeout("Effect.Appear('price_holder', {duration:0.7});", 500);
	   		setTimeout("$('calculator_loading').hide();", 500);
	   		setTimeout("$('calculator_loading').hide();", 1000);
	   		//$('ButtonBook').onclick=new Function( ('location.replace("'+link+'");') );
	   		str=null;
	  		

	  },
	  onFailure: function(){
	  		$('calculator_loading').update('');
	  		Effect.Appear('error', {duration:0.7});
	  		$('calc_link').disabled="";
			new Effect.Opacity('calc_link', {duration:0.6, from:0, to:1});
	  }
	});
	
	return false;
}

/**
 * Does a window location redirect | should be used instead if location replace
 */
function goToPage(page) {
	window.location = page;
}


/**
 * Gets the number of additional beds for the selected lot
 */
function updateLotData(){
	var acc=$F('acc_holder');
	var lotData=$F('lot').split("|");
	var category=lotData[0];
	var lotId=lotData[2];
	
		
	$('aditional_bed').update('<option>N/A</option>');
	if(lot_add_beds.has(lotId)){
		var accBeds=lot_add_beds[lotId];
		var output='';
		for(var i=0;i<=accBeds;i++){
				output+='<option>'+i+'</option>';
		}
		
		$('aditional_bed').update(output);
	}	
	
	/*
	var url = WWW_DIR+'ajax/ajax.priceCalculator.php?cmd=get_add_bed&acc='+acc+'&category='+category;
	new Ajax.Request(url, {
	  method: 'get',
	  onSuccess: function(transport) {
	  		var data=transport.responseText;
	  		
	  		var output='';
	  		if(data>0){
	  			for(var i=0;i<=data;i++){
	  				output+='<option>'+i+'</option>';
	  			}	  			
	  		}else{
	  			output='<option>0</option>';
	  		}
	  		
	  		$('aditional_bed').update(output);
	  		data=null;
	  		
	},
	  onFailure: function(){
	  		$('calculator_loading').update('');
	  		Effect.Appear('calc_error', {duration:0.7});
	  		$('calc_link').disabled="";
			new Effect.Opacity('calc_link', {duration:0.6, from:0, to:1});
	  }
	});
	
	return false;
	*/
	  		
}


/**
 * Redirects  the user to the lot page, focuses the booking form and populates it with calculator elements
 */
function calcBookNow(persons, birthdates, pets, additional_bed, from, to, lot_id){
	var link="&num_persons="+persons+"&birthdates="+birthdates+"&pets="+pets+"&additional_bed="+additional_bed+"&from="+from+"&to="+to;
	location.replace(WWW_DIR+'booking-request/?form=booking&acc='+acc_id+'&lot='+lot_id);
	
	/*
	//filter out the acc url from lot url
	var base="http://"+location.host+location.pathname;
	base=base.replace(WWW_DIR, '');
	
	if(base.include(calendar_language+'/')){
		base=base.split("/");
		base = WWW_DIR+base[0]+'/'+base[1]+'/';		
	}else{
		base=base.split("/");
		base = WWW_DIR+base[0]+'/';
	}
	
	var lot=apartment;
	
	if(base.include('hotel')){
		lot=hotelroom;
	}
	link=link.gsub(/#form_holder/, '');
	link=base+lot+"-"+lot_id+"/?form=bookingForm"+link+"#from_holder";
	location.replace(link);
	*/
}

/**
 * Shows the calculator after the calculation has been done
 */
function showCalculator(){
	$('price_holder').hide();
	//$('lot_prefix').hide();
	$('lot_title').update('');
	$('lot_for').hide();
	$('error').hide();
	$('ButtonCalculate').disabled="disabled";
	new Effect.Opacity('ButtonCalculate', {duration:0.6, from:1, to:0});
	$('ButtonBook').hide();
	//$('error').hide();
	$('calculator_loading').hide();
	
	Effect.Appear('CalculatorHolder', {duration:0.5});
	new Effect.Opacity('ButtonCalculate', {duration:1, from:0.4, to:1});
	$('ButtonCalculate').disabled="";		
}

/**
 * Updates the amount of birthdates on the calculator
 */
function updateCalculatorBirthdates(){
	var persons=$F('no_persons');
	
	for(var i=1;i<=15;i++){
		$('person_'+i+'_holder').hide();
	}
	
	for(var i=1;i<=persons;i++){
		$('person_'+i+'_holder').show();
	}
	
	return;
}

var last_calclotid='';
function updateLightviewCalc(lotId, lotSelectId){
	if(last_calclotid!=""){
		$('lot_fac_'+last_calclotid).hide();
	}
	
	$('lot_fac_'+lotId).show();
	last_calclotid=lotId;
	
	$('lot').value=lotSelectId;
	
	updateLotData();
	return;
}	

