//Search availability.
function SearchAvailability(step, adate, ddate, applydate, resid){
	
	if(!DateValidate(adate, "dd/mm/yyyy") || !DateValidate(ddate,"dd/mm/yyyy")){	
		$j('<div class="failed">Please ensure you have filled in the correct arrival and departure date.</div>').appendTo($j('#errorMsg'));
	} else {
		
		$j.ajax({
			type:'POST',
			url : '/booking/PostManager/Process',
			data: {step:step, arrival_date:adate, departure_date:ddate, resid:resid?resid:''},
			async: true,
			beforeSend: function(){
				Dialog('Checking availability. Please wait......<br/>If it takes too long, click here to <a href="/booking">refresh</a>');
			},
			success:function(dt){
				DialogClose();
				$j('#errorMsg').empty();
				var s = jd(dt);
				if(s.success == 1){
					if(s.availability != '') {
						$j('#step_2').empty().html(s.availability);
						if(applydate){
							$j('#arrival_date').val(s.startdate);
							$j('#departure_date').val(s.enddate);
						}
						$j('#thirdStep').empty();
					}
					
				} else {
					
					if(s.msg != '') $j('<div class="failed">'+s.msg+'</div>').appendTo($j('#errorMsg'));
					$j('#step_2').empty();	
					$j('#thirdStep').empty();						
				}
			}
		});
	}
}

//Clicking on Book Button
function BookDate(rid, bdate, roomid){
	
	$j.ajax({
		type:'POST',
		url : '/booking/PostManager/Process',
		data: {step:2, residence:rid, book_date:bdate, roomid:roomid},
		async: true,
		beforeSend: function(){
			Dialog('Loading data. Please wait......<br/>If it takes too long, click here to <a href="/booking">refresh</a>');
		},
		success:function(dt){
			DialogClose();
			$j('#thirdStep').empty();
			$j('#errorMsg').empty();
			var s = jd(dt);
			if(s.success == 1){
				
			} else {
				$j(s.html).appendTo($j('#thirdStep'));
										
			}
		}
	});	
}

function CalculatePrice(id, price, val){
	
	if(val == "") {
		$total = "$0.00";
	} else {
		$total = parseFloat(price) * parseInt(val);
		$total = "$"+$total.toFixed(2);
	}
	
	$j('#service_price_'+id).text($total);
	
}

/*Clicking on Submit button in Step 2 */
function BookNow(f)
{
	//check if any dates are selected, at least one must be selected.
	var bookdateChecked = false;
	var bookdate = f.elements['bookdate[]'];
	$j(bookdate).each(function(i){
		if(this.checked) {
			bookdateChecked = true;							   
		}
    });
	if(!bookdateChecked){
		$j('#errorMsg').empty().html('<div class="failed">Please confirm your dates in Step 3 by ticking the boxes.</div>');
		window.scrollTo(0,0);
		return;
	}
	if(SelectedValue(f.elements['checkin_time']) == ""){
		$j('#errorMsg').empty().html('<div class="failed">Please confirm your arrival time.</div>');
		window.scrollTo(0,0);
		return;
	}
	if(IsEmpty(f.first_name.value)){
		$j('#errorMsg').empty().html('<div class="failed">Please enter your first name in Step 6.</div>');
		window.scrollTo(0,0);
		return;
	} 
	if(IsEmpty(f.last_name.value)){
		$j('#errorMsg').empty().html('<div class="failed">Please enter your last name in Step 6.</div>');
		window.scrollTo(0,0);
		return;
	}
	if(IsEmpty(f.email.value)){
		$j('#errorMsg').empty().html('<div class="failed">Please enter your email address in Step 6.</div>');
		window.scrollTo(0,0);
		return;
	}
	if(!EmailValidate(f.email.value)){
		$j('#errorMsg').empty().html('<div class="failed">Please enter a valid email address in Step 6.</div>');
		window.scrollTo(0,0);
		return;
	}
	if(IsEmpty(f.email_confirm.value)){
		$j('#errorMsg').empty().html('<div class="failed">Please confirm your email address in Step 6. This email address must be the same as the one you have entered.</div>');
		window.scrollTo(0,0);
		return;
	}
	if(!EmailValidate(f.email_confirm.value)){
		$j('#errorMsg').empty().html('<div class="failed">Please enter a valid email address in "Re-type Email" in Step 6.</div>');
		window.scrollTo(0,0);
		return;
	}
	if(f.email.value != f.email_confirm.value){
		$j('#errorMsg').empty().html('<div class="failed">Please ensure you have re-typed your email correctly in Step 6.</div>');
		window.scrollTo(0,0);
		return;
	}
	if(IsEmpty(f.mobile.value)){
		$j('#errorMsg').empty().html('<div class="failed">Please enter your mobile number in Step 6.</div>');
		window.scrollTo(0,0);
		return;
	}
	if(IsEmpty(f.phone.value)){
		$j('#errorMsg').empty().html('<div class="failed">Please enter your phone number in Step 6.</div>');
		window.scrollTo(0,0);
		return;
	}
	if(IsEmpty(f.address.value)){
		$j('#errorMsg').empty().html('<div class="failed">Please enter your address in Step 6.</div>');
		window.scrollTo(0,0);
		return;
	}
	if(IsEmpty(f.state.value) && f.country.value == 'country_AU'){
		$j('#errorMsg').empty().html('<div class="failed">Please enter your state in Step 6.</div>');
		window.scrollTo(0,0);
		return;
	}
	if(IsEmpty(f.postcode.value)){
		$j('#errorMsg').empty().html('<div class="failed">Please enter your postcode in Step 6.</div>');
		window.scrollTo(0,0);
		return;
	}
	if(IsEmpty(f.country.value)){
		$j('#errorMsg').empty().html('<div class="failed">Please enter your country in Step 6.</div>');
		window.scrollTo(0,0);
		return;
	}
	
	//get selected bookdate
	var bookdate = new Array();
	$j(f.elements['bookdate[]']).each(function(){
		if(this.checked){
			bookdate.push(this.value);
		}
	});
	
	bookdate = bookdate.join("+");
	
	$j.ajax({
		type:'POST',
		url: '/booking/PostManager/Process',
		data: {step:6, residence:f.residence_id.value, book_date:bookdate, roomid:f.room_id.value},
		async: true,
		beforeSend: function(){
			Dialog('Validating booking data. Please wait......<br/>If it takes too long, click here to <a href="/booking">refresh</a>');
		},
		success:function(dt){
			var s = jd(dt);
			if(s.error == 1) {
				DialogClose();
				Dialog(s.msg);
			} else {
				f.SubmitFormBtn.disabled = 'disabled';
				f.submit();
			}
		}	
	});
	
	//f.SubmitFormBtn.disabled = 'disabled';
	//f.submit();
	
}

function SetDate(dateVal){

	var x = dateVal.split("/");
	var newDate = x[1]+"/"+x[0]+"/"+x[2];
	var z = Date.parse(newDate) + 24*60*60*1000;
	var t = new Date();
	t.setTime(z);
	$j('#departure_date').val(t.getDate() + "/" +
		((t.getMonth()+1).toString().length == 1 ? "0"+ (t.getMonth()+1) : (t.getMonth()+1))
		+ "/"+t.getFullYear());
	
	
}

function MakeBooking(f){
	
	if(!$j('#payment_options').attr('checked')){
		//alert('this');
		Dialog('Please identify your preferred payment options by ticking the payment options.');
		//DialogClose(3);
	} else {
		f.action = '/booking/PostManager/Process';
		f.submit();
	}


}

