var IE7_PNG_SUFFIX = ".png";

var tabChange=function(){}
var loadShortlistAfterUpdate=true;

if (screen.width <= 699 && !getCookie("nomobile")) {
	if(confirm("Would you like to be taken to the Quality Villas mobile site at qualityvillas.mobi?"))
	{
		document.location = "http://www.qualityvillas.mobi";
	}else
	{
		setCookie("nomobile","true",1);
	}

}
function setCookie(c_name,value,exdays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}


function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}


function after_ajax()
{



		
  			 	
  			 	$( ".shortlist-form #arrival-date" ).datepicker({onSelect:function(dateText, inst) { 
				
			
				the_bloody_date=dateText.split('-');
  			 	date=new Date();
				date.setFullYear(the_bloody_date[2]);
				date.setDate(the_bloody_date[0]);
  			 	the_bloody_date[1]=the_bloody_date[1]-1;
  			 	date.setMonth(the_bloody_date[1]);
  			 	
  			   			 	
			 	if(date.getDay()!=6 && (the_bloody_date[1]>4 && the_bloody_date[1] <9 ) && restrict_days==true)
  			 	{
  			 		nicealert(translations['peak']);
  			 		
  			 		jQuery('.shortlist-form #arrival-date').val('');
  			 	}
  			 	else
  			 	{
  			 		
  			 	}
  			 	
  			 	}}
  			 	);
  			 	
  			 	
  			 	
			jQuery('.cancel-review').click(function(){jQuery('.overlay').fadeOut();jQuery('.alert-overlay').fadeOut()})
			
			jQuery('.send-shortlist').click(function(){
	 	
	 		
	 			errors='';
				if(!jQuery('#arrival-date').val())
	 			{
	 				errors+='Your arrival date<br/>';
	 			}
				if(!jQuery('.your-name').val())
	 			{
	 				errors+='Your name<br/>';
	 			}
				if(!jQuery('.your-telephone-number').val())
	 			{
	 				errors+='Your telephone number<br/>';
	 			}
				
				if(errors)
	 			{
	 				nicealert('<b>Please fill in</b><br/>'+errors);
	 				return;
	 			}
	 			
	 				jQuery('#adwords_conversion_target').load("/adwords_conversions/conversion")
	 			
	 			jQuery.post('/shortlist/send/',jQuery('#booking-form').serialize(),function(){
					jQuery('.overlay').fadeOut();
	 				
	 				nicealert(translations['thank_you_shortlist']);
	 				
	 			})
	 	})
	 	kinky=jQuery('#arrival-date').val();
	 	
	 	jQuery('#arrival-date').datepicker( {minDate:new Date()} );
	 	
	 	jQuery('#arrival-date').datepicker( "option", "dateFormat", 'dd-mm-yy' );
	 	
	 	jQuery( "#arrival-date" ).datepicker( "option", "constrainInput", false );
	 	
	 	jQuery( "#arrival-date" ).val(kinky);
	 	
	 	
}	
var shortlistItem=0;
jQuery(document).ready(
	function()
	{	
		
		
			jQuery('.calchen a').each(
		
			function()
			{
				jQuery(this).attr('rel',jQuery(this).attr('href').split('|')[1]);
				jQuery(this).attr('href',jQuery(this).attr('href').split('|')[0]);
			
			}
		
		)
		
		
			jQuery('#savechen').click(function(){
				if($('input[name="terms_and_conditions"]:checked').length!=0){			
					jQuery('form#booking').submit();
				}else
				{
					alert('Please tick to say you have read and accept the terms and conditions');
				}
				
				if(checkemail(jQuery('input#email_address').val()))
				{
					alert('Pleas enter a valid email address');
				}
			});
		
			jQuery('select[name="card[type]"]').change(function(){
			
			
			if(jQuery(this).val()=="Debit"){
				jQuery('#issue_num').show();
			
			}
			else
			{
				jQuery('#issue_num').hide();
			
			}
		});
		
		
		if(jQuery('select[name="card[type]"]').val()=="Debit"){
				jQuery('#issue_num').show();
			
			}
			else
			{
				jQuery('#issue_num').hide();
			
			}




		
		jQuery('select[name=total_number_in_party_inc._babies]').change(function(){
		
			number=jQuery(this).val();
			
			$('.party_member').hide();
			
			for(i=0;i<=number;i++)
			{
				jQuery('.party_member_'+i).show();
			}
			
		});
		jQuery('input').click(function(){jQuery(this)[0].style.color='#000'});
		jQuery('select[name="currency-changer"]').change(function(){
			window.location='/customer/set_currency/'+jQuery(this).val();
		})
		
		jQuery('select[name="currency-changer-pricing"]').change(function(){
		
		
		
			jQuery('.price').hide();
			
			
			jQuery('.price-'+$(this).val()).show();
			
			
			jQuery.ajax({url:'/customer/set_currency/'+$(this).val()+'/true'});
		})
		
		
		
		
		
		if(document.getElementById("contact-map"))
		{
			map = new GMap2(document.getElementById("contact-map"));
			map.addControl(new GLargeMapControl());
			
			
				
					jQuery('.map-close-overlay').click(function(e){
				
					jQuery('.get-directions').fadeOut();
					});
        	
        	
      		 jQuery('.get-directions-now').click(function(){
      		 	window.open('http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q='+jQuery('.directions-postcode').val()+'+to+HP4+1AL&');

      		 })
			
			var center = new GLatLng(51.762,-0.573);
		
				 var qIcon = new GIcon(G_DEFAULT_ICON);
			
      			qIcon.image='/images/map-marker-q.png';
      			qIcon.iconSize=new GSize(43,31);
        		qIcon.shadow=null;
        			
        		 marker=new GMarker(center,{ icon:qIcon });
		
				map.setCenter(center, 14);
				GEvent.addListener(marker, "click", function() {
					jQuery('.get-directions').fadeIn();
				})
				map.addOverlay(marker);
		}

	
				$('*:not(#ajax-search)').click(function (){	$('#ajax-search').fadeOut();});
			
			$('#refine-term,#search').keyup(function()
			{
			
				if($(this).val())
				{
					ajax_search_visible=true;
					$('#ajax-search').fadeIn();
					$('#ajax-search').load('/properties/ajax_search/'+escape($(this).val()));
				}
				else
				{
					ajax_search_visible=false;
					$('#ajax-search').fadeOut();
				}
			
			})
			
		jQuery('.send-to-a-friend-send').click(function(){ 
			$('#send-to-a-friend-loader').show();
			$.get("/properties/ajax_send_to_a_friend/"+property_id+"/"+$('#send-to-a-friend-email').val()+"/"+$('#send-to-a-friend-name').val(),function(txt){
			
			setTimeout(function(){$('#send-to-a-friend-loader').hide();$('#send-to-a-friend').hide();nicealert('The villa information was sent successfully.');},1000);
			
		})
		})
		
		jQuery('.open-send-to-a-friend').click(function(){
			
			jQuery('#send-to-a-friend').slideDown();
		});
		jQuery('.close-send-to-a-friend').click(function(){jQuery('#send-to-a-friend').slideUp() } )
		
		
		jQuery('.clear-on-click').focus(
			
			function()
			{
				
				
				if(!!jQuery(this).attr('clicked')==false)
				{
					jQuery(this).attr('value','');
					jQuery(this).attr('clicked',true);
				}
				
				
				
			}
		
		);
		
		jQuery('.open-in-overlay').click(
			
			function(e)
			{
				scroll(0,0);
				e.preventDefault();
				jQuery('.overlay').fadeIn();
				jQuery('#overlay-target').load(jQuery(this).attr('href'),function(){after_ajax()});
			}
			);
				
		jQuery('#rates input[type=submit]').click(function(e){
		
			e.preventDefault();
			
			if(jQuery('input[name=agree]').val().length > 0)
			{
				jQuery('.owners form').submit();
			}else
			{
				nicealert('please accept our terms and conditions');
			}
			
		});
	
		jQuery('.clear-on-click').blur(
			
			function()
			{
					if(jQuery(this).attr('value')=='')
					{
						jQuery(this).attr('clicked','');
						jQuery(this).attr('value',jQuery(this).attr('rel'));
					}	
			}	
		);

		
		
		jQuery('.alert-overlay').click(function(){ 
			jQuery('.alert-overlay').fadeOut();
		});
		realalert=alert;
		
	
		nicealert=function (alertText,callback)
		{
			jQuery('#alert-target').html(alertText);
		
		
		
		
					jQuery('.alert-overlay').fadeIn('slow');
			
			
			jQuery('.alert-overlay').click(function(){
			
				if(typeof callback=='function')
				{
					callback();
					
				}
				else
				{
					jQuery('.alert-overlay').fadeOut();	
				}
			});
		
		}
		
		jQuery('.select-checkbox-within').click(
		
		
				function()
				{	
					
					if(jQuery(this).attr('type')!="checkbox")
					{
						jQuery(':checkbox',this).attr('checked',!jQuery(':checkbox',this).attr('checked'));
					}
			
				}
			
				
		);
		
		jQuery('.open-popup').click(function(){
		
			locationCatchFunction=function()
		{
				jQuery('.location-selector-popup').fadeOut()
			jQuery('.location-catch').fadeOut();
			;calculateLocationInput();
		}
		jQuery('.location-selector-popup').fadeIn();jQuery('.location-catch').fadeIn();});
		
		jQuery('.location-selector-popup .select-all').click(function(){ 
		
				unselect=jQuery(this).html().indexOf(__translations['unselect'])!=-1;
				
						
			if(unselect)
				{
					jQuery('a',this).html(jQuery('a',this).html().replace(__translations['unselect'],__translations['select']));
				}else
				{
					jQuery('a',this).html(jQuery('a',this).html().replace(__translations['select'],__translations['unselect']));
				}		
				jQuery(this).siblings().each(
				function(){
				
					//realalert(unselect);
					if(unselect)
					{
						jQuery(':checkbox',this).attr('checked',false);
					}
					else
					{
						jQuery(':checkbox',this).attr('checked',true);
					}
				}
				)
					
					
				}
				
				);
		
		
	
		jQuery('.location-catch').click(function(){
			
			locationCatchFunction();
		
		})
		
		jQuery('.location-selector-popup .close').click(
			function(){
				jQuery('.location-catch').fadeOut();
				
				jQuery('.location-selector-popup').fadeOut();calculateLocationInput();}
		);
		
		jQuery('.location-selector-popup .Search').click(
			function(){
				jQuery('.location-catch').fadeOut();
				
				jQuery('.location-selector-popup').fadeOut();calculateLocationInput();
				
				jQuery('.find-a-villa .refined-search').click();
				
				}
		);
		
		calculateLocationInput();
		
		
		jQuery('.booking-form .submit').click(function(){
			errors='';
			jQuery('.booking-form  .required').each(
				function()
				{
					if(jQuery(this).val()=='' || jQuery(this).val()==jQuery(this).attr('rel') )
					{
						if(errors==''){errors='<b>The following fields are required</b><br/>';}
						
						errors=errors+"<br/>"+jQuery(this).attr('rel');
					}
				}
			)
			
			
			if(!errors)
			{

				jQuery('.booking-form form')[0].submit();
			}
			else{
			
				nicealert(errors);
			}
		});
		
		jQuery('.term-search').click(function()
		{
			window.location='/locations/view/'+($('#search').val()=="Villa name or reference no."?'':$('#search').val());
		
		}
		);
		jQuery('.refined-search').click(function()
		{
			date_value=jQuery('#datepicker').val().replace(/\//g,'-');
			
			if(date_value=='Date')
			{
				date_value='0';
			}
						search_query='/locations/view';
			
			
			if(jQuery('#refine-term').val())
			{
				search_query=search_query+'/'+jQuery('#refine-term').val()+':'+jQuery('#refine-location').val();
			}
			else
			{
				search_query=search_query+'/'+jQuery('#refine-location').val();
			}
			search_query=search_query+'/'+(date_value?date_value:'0');
			search_query=search_query+'/'+jQuery('#refine-weeks').val();
				
			search_query=search_query+'/'+(jQuery('#refine-special-offers-only:checked').val()?'1':'0');
			
			search_query=search_query+'/'+(jQuery('#refine-sleeps').val()?jQuery('#refine-sleeps').val():'0')+'/';
		
		
			jQuery("input[name='property_attribute']:checked").each(
			
			
				function()
				{
					search_query=search_query+jQuery(this).val()+',';
				}
			)
			
			search_query=search_query+'/';
			search_query.replace(/\/\//g,'/');
			//realalert(search_query);
			window.location=search_query;
		
		}		
		)
		jQuery("a[href='#brochure']").click(function(){
			
			jQuery('.location-catch').show();
			locationCatchFunction=function(){jQuery('#brochure-request').slideUp();	jQuery('.location-catch').hide();}
			jQuery('#brochure-request').slideDown()});
	
			jQuery('#overlay').click(
			
			function(){jQuery(this).fadeOut();
			jQuery('#overlay').fadeOut();
		
		}
		);
		
		
		
		
		
		
		jQuery("a[href='#newsletter']").click(function(){
			
			jQuery('.location-catch').show();
			locationCatchFunction=function(){jQuery('#newsletter-request').slideUp();	jQuery('.location-catch').hide();}
			jQuery('#newsletter-request').slideDown()});
			jQuery('.close-newsletter-request').click(function(){jQuery('#newsletter-request').slideUp() } )
		
			jQuery('#overlay').click(
			
			function(){jQuery(this).fadeOut();
			jQuery('#overlay').fadeOut();
		
		}
		);
		
		shortlist_links();
		
		
		jQuery('.close-brochure-request').click(function(){
		
		jQuery('#brochure-request').slideUp();
		
		})
		jQuery('.country').change(function()
		{
		
			
		
			
			unselect=true;
			jQuery(':checkbox',jQuery('ul[rel="'+jQuery(this).attr('rel').replace('select-all','')+'"]')).each(
			function(){
						
						
					
						if(jQuery(this).attr('checked'))
						{
							unselect=false;
						}
						
					});
			
			if(unselect)
			{
				jQuery('#select-all-'+jQuery(this).attr('rel')+"").html('select all '+jQuery(this).attr('rel'));
			}
			else
			{
				jQuery('#select-all-'+jQuery(this).attr('rel')+"").html('unselect all '+jQuery(this).attr('rel'));
			}		
					
		});
			
		jQuery('.map-close-overlay').click(function(){jQuery('.map-overlay').fadeOut()})

		save_val=$( "#datepicker" ).val();
		
		if(save_val=='date')
		{
			save_val=__translations['Date'];
		}
		
		$("#datepicker").datepicker({minDate:new Date()});
		
		$('.datepicker').datepicker({minDate:new Date()} );
		
		$('#datepicker').datepicker("option",'monthNames',[__translations['January'], __translations['February'], __translations['March'], __translations['April'], __translations['May'], __translations['June'], __translations['July'], __translations['August'], __translations['September'], __translations['October'], __translations['November'], __translations['December']]);
		
		$('.datepicker').datepicker("option",'monthNames',[__translations['January'], __translations['February'], __translations['March'], __translations['April'], __translations['May'], __translations['June'], __translations['July'], __translations['August'], __translations['September'], __translations['October'], __translations['November'], __translations['December']]);
		
		
		
			
		$('#datepicker').datepicker("option",'dayNamesMin',[__translations['Su'], __translations['Mo'], __translations['Tu'], __translations['We'], __translations['Th'], __translations['Fr'], __translations['Sa']]);
		
		$('.datepicker').datepicker("option",'dayNamesMin',[__translations['Su'], __translations['Mo'], __translations['Tu'], __translations['We'], __translations['Th'], __translations['Fr'], __translations['Sa']]);
		
	

		
		$( ".datepicker" ).datepicker( "option", "dateFormat", 'dd-mm-yy' );
		
		$("#datepicker").datepicker( "option", "dateFormat", 'dd-mm-yy' );
		
			 	jQuery( "#datepicker ").datepicker( "option", "constrainInput", false );
			 	
			 		 	jQuery( ".datepicker" ).datepicker( "option", "constrainInput", false );
			
			
			
		

		
			 if(!save_val)
			 {
			 
	 			$( "#datepicker" ).val(__translations['Date']);
	 		}else
	 		{
	 			
	 			$('#datepicker').val(save_val);
	 		}
		jQuery("#overlay-close").click(function(){ $('.overlay').fadeOut();  })	
		
		jQuery(".send-brochure-request").click(function()
		{
		
			
			errors='';
			jQuery('#brochure-request-form .required').each(
				function()
				{
					if(jQuery(this).val()=='' || jQuery(this).val()==jQuery(this).attr('rel') )
					{
						if(errors==''){errors='<b>The following fields are required</b><br/>';}
						
						errors=errors+"<br/>"+jQuery(this).attr('rel');
					}
				}
			)
			if(checkemail(jQuery('input[name="txtEmail"]').val()))
			{
				errors=errors+"<br/><br/><b>Please enter a valid email address</b>";
			}
			
			if(!errors)
			{
				
				
					
			if(jQuery('input[name="txtAddress2"]').val()=='Address line 2')
			{ 
			
				jQuery('input[name="txtAddress2"]').val(' ');
			
			}
	
	
	
				jQuery('#brochure-loader').fadeIn();
				
				$.post("/requests/ajax_request_brochure", $("#brochure-request-form").serialize(),function(txt){
				
					if(txt==1)
					{
					
						
	 					jQuery('#adwords_conversion_target').load("/adwords_conversions/conversion")
	 			
						jQuery('#brochure-request').slideUp() 
						jQuery('#brochure-loader').fadeOut();
						$('.overlay').fadeOut(); 
						
						nicealert(translations['thank_you_brochure']);
						
						
					}
					else
					{
						nicealert(translations['error_brochure']);
						jQuery('#brochure-loader').fadeOut();
					}
				
				});
				
					
			if(jQuery('input[name="txtAddress2"]').val()=='Address line 2')
			{
			
				jQuery('input[name="txtAddress2"]').val('Address line 2');
			
			}
	
				
			}
			else
			{
				nicealert(''+errors);
				
			}
		}
		);
	
		if(window.location.hash=='#newsletter-thank-you')
		{
			nicealert(translations['newsletter_thankyou']);
			jQuery('#adwords_conversion_target').load("/adwords_conversions/conversion")
	 	
		}
		
		if(window.location.hash=='#currency-changed')
		{
			nicealert(translations['currency_changed']);
		}
		
		
		jQuery(".send-newsletter-request").click(function()
		{
			
			
			errors='';
		
			if(checkemail(jQuery('input[name="Address"]').val()))
			{
				errors=errors+"<b>"+translations['validation_valid_email']+"</b>";
			}
			
			if(!errors)
			{
				jQuery('#newsletter-loader').fadeIn();
				
			
				$('#newsletter-form')[0].submit();
			}
			else
			{
				nicealert(''+errors);
				
			}
		}
		);
	}
)


function shortlist_links()
{

				/*
					jQuery('.shortlist-link a').click(function(e){
			
					e.preventDefault();
					
					shortlistItem=jQuery(this);
					
					//shortlistItem.html('<img src="/images/loader-small-green.gif" alt="loading">');
					if(shortlistItem.attr('class')== 'add-to-shortlist')
					{
								alert("dsdsd x");
     			 	 		return;
						$.ajax({ url: "/shortlist/add/"+jQuery(this).attr('rel'), contextObject: this, success: function(data){
     			 	 	
     			 	 	
     			 	 		
     			 	 		if(!!loadShortlistAfterUpdate)
     			 	 		{
     			 	 			
     			 	 			document.location='/shortlist/view';
     			 	 			return;
     			 	 		}
     			 	 		
     						$('#shorlist-number-of-properties').html(data);
     				
     						
     			 	 		shortlistItem.html('Remove from shortlist');
     			 	 		shortlistItem.attr('class','remove-from-shortlist');
     			 	 		jQuery('.view-shortlist').show();
   		 				  }});
		
					}else{
					
					
     			 	 		
					
						$.ajax({ url: "/shortlist/remove/"+jQuery(this).attr('rel'), contextObject: this, success: function(data){
							if(!!loadShortlistAfterUpdate)
     			 	 		{
     			 	 			document.location='/shortlist/view';
     			 	 			return;
     			 	 		}
     						$('#shorlist-number-of-properties').html(data);
     					
     			  			shortlistItem.html('Add to shortlist');
     			 	 		shortlistItem.attr('class','add-to-shortlist');
     			 	 		
     			 	 	
   		 				  }});
					}
		});
		
		*/
}


function checkemail(email_address){

 var filter=/^.+@.+\..{2,3}$/

 if (filter.test(email_address))
 {
   return false;
 }else {
   return true;
}

}

function loadMapOverlay(id)
{

	//alert(id);
}

function urlEncode(string)
{
	return escape(string).replace(/\//g,'').replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}

locationCatchFunction=function(){nicealert('no function set :(')}
function calculateLocationInput()
{				allSelected=true;
				countries=[];
				textForLabel='';
				totalCountries=0;
				countriesSome=[];
				refineValue='['	
				jQuery('.country').each(
				function()
				{
					
					if(!!jQuery(this).attr('rel'))
					{
					totalCountries++;
					countrySomeSelected=false;
					countryAllSelected=true;
					jQuery(':checkbox',this).each(function(){
						
						if(!jQuery(this).attr('checked'))
						{
							countryAllSelected=false;
						}else
						{
							countrySomeSelected=true;
							textForLabel+=jQuery(this).attr('value')+', ';
							refineValue+=jQuery(this).attr('id').replace('location_','')+',';
						}
					});
						if(countryAllSelected)
						{
							countries.push(jQuery(this).attr('rel'));
						}
						if(countrySomeSelected)
						{
							countriesSome.push(jQuery(this).attr('rel'));
						}
					}
				}
				)
				textForLabel=textForLabel.substr(0,textForLabel.length-2);
				refineValue=refineValue.substr(0,refineValue.length-1)+']';
				if(countries.length==totalCountries)
				{
					textForLabel=__translations['All Destinations'];
					refineValue='all';
				}
				
				if(countries.length==1 && countriesSome.length==1)
				{
					textForLabel=(countries[0]);
				
				}
				if(textForLabel=='')
				{
					textForLabel=__translations['All Destinations'];
				}
				if(refineValue!=']')
				{
					jQuery('#refine-location').val(refineValue);
				}
				else
				{
					jQuery('#refine-location').val('all');
				}
				
				jQuery('.open-popup .locations').val(textForLabel);
			}
			
			
			
			ajax_search_visible=false;

ajax_search_position=0;

function resetSearch()
{

	$('#search').val('');


	$('#ajax-search').fadeOut();
}




