// JavaScript Document
//search dropdowns
function locality(el,loc,val){
	if (loc == null){
	loc = 'location';
	}
	if (val == null){
	val = -1;
	}
	str=" ";
	s = el.selectedIndex-1; //hack for 1st select option
	if(s>-1){
		for(i=0;i<locations[s].length;i++){
			//cms hack
			if(val==locations[s][i][0]) str+='<option selected="selected" value="'+locations[s][i][0]+'">'+locations[s][i][1]+'</option>';
			else str+='<option value="'+locations[s][i][0]+'">'+locations[s][i][1]+'</option>';
		}
		$(loc).update('<option value="-1">Select Location</option>'+str);
	}else{
		$(loc).update('<option value="-1">Select Country first</option>');
	}
}
function priceRange(list,pr1,pr2,pricerange){
	if (pr1 == null){
	pr1 = 'pricerange1';
	}
	if (pr2 == null){
	pr2 = 'pricerange2';
	}
	if (pricerange == null){
	pricerange = 'pricerange';
	}
	//check reset
	if(list.selectedIndex == 0){
		$(pr1).selectedIndex = 0;
		$(pr2).selectedIndex = 0;
		$(pricerange).value = -1;
	}else{
		//check from
		if(list==$(pr1)){
		if($(pr1).selectedIndex >= $(pr2).selectedIndex)
		$(pr2).selectedIndex = $(pr1).selectedIndex;
		}
		//check to
		if(list==$(pr2)){
		if($(pr2).selectedIndex <= $(pr1).selectedIndex)
		$(pr1).selectedIndex = $(pr2).selectedIndex;
		}	
		//set variable
		$(pricerange).value = $(pr1).value+','+$(pr2).value;
	}
}

function populateBuy(name) {
	
	$('price_label').update('Price');
	
	pricerange = document.getElementById(name);
    for (var i = pricerange.options.length; i > 0; i--)pricerange.options[i] = null;
	
	if(pricerange.name=='pricerange1') var offset = 0; else var offset = 1;
	if(pricerange.name!='pricerange2') pricerange.options[1] = new Option('\u20ac 25,000', '25000');
	
	pricerange.options[2-offset] = new Option('\u20ac 50,000', '50000');
	pricerange.options[3-offset] = new Option('\u20ac 100,000', '100000');
	pricerange.options[4-offset] = new Option('\u20ac 150,000', '150000');
	pricerange.options[5-offset] = new Option('\u20ac 200,000', '200000');
	pricerange.options[6-offset] = new Option('\u20ac 250,000', '250000');
	pricerange.options[7-offset] = new Option('\u20ac 300,000', '300000');
	pricerange.options[8-offset] = new Option('\u20ac 350,000', '350000');
	pricerange.options[9-offset] = new Option('\u20ac 400,000', '400000');
	pricerange.options[10-offset] = new Option('\u20ac 450,000', '450000');
	pricerange.options[11-offset] = new Option('\u20ac 500,000', '500000');
	pricerange.options[12-offset] = new Option('\u20ac 550,000', '550000');
	pricerange.options[13-offset] = new Option('\u20ac 600,000', '600000');
	pricerange.options[14-offset] = new Option('\u20ac 650,000', '650000');
	pricerange.options[15-offset] = new Option('\u20ac 700,000', '700000');
	pricerange.options[16-offset] = new Option('\u20ac 750,000', '750000');
	pricerange.options[17-offset] = new Option('\u20ac 800,000', '800000');
	pricerange.options[18-offset] = new Option('\u20ac 900,000', '900000');
	pricerange.options[19-offset] = new Option('\u20ac 1,000,000', '1000000');
	
	if(pricerange.name!='pricerange1') pricerange.options[20-offset] = new Option('\u20ac 1,5000,00 +', '1500000');
}
function populateRent(name) {
	
	$('price_label').update('Price (Monthly)');

	pricerange = document.getElementById(name);
    for (var i = pricerange.options.length; i > 0; i--)pricerange.options[i] = null;
	
	if(pricerange.name=='pricerange1') var offset = 0; else var offset = 1;
	if(pricerange.name!='pricerange2') pricerange.options[1] = new Option('\u20ac 50', '50');
	
	pricerange.options[2-offset] = new Option('\u20ac 100', '100');
	pricerange.options[3-offset] = new Option('\u20ac 150', '150');
	pricerange.options[4-offset] = new Option('\u20ac 200', '200');
	pricerange.options[5-offset] = new Option('\u20ac 250', '250');
	pricerange.options[6-offset] = new Option('\u20ac 300', '300');
	pricerange.options[7-offset] = new Option('\u20ac 350', '350');
	pricerange.options[8-offset] = new Option('\u20ac 400', '400');
	pricerange.options[9-offset] = new Option('\u20ac 450', '450');
	pricerange.options[10-offset] = new Option('\u20ac 500', '500');
	pricerange.options[11-offset] = new Option('\u20ac 550', '550');
	pricerange.options[12-offset] = new Option('\u20ac 600', '600');
	pricerange.options[13-offset] = new Option('\u20ac 650', '650');
	pricerange.options[14-offset] = new Option('\u20ac 700', '700');
	pricerange.options[15-offset] = new Option('\u20ac 750', '750');
	pricerange.options[16-offset] = new Option('\u20ac 800', '800');
	pricerange.options[17-offset] = new Option('\u20ac 850', '850');
	pricerange.options[18-offset] = new Option('\u20ac 900', '900');
	pricerange.options[19-offset] = new Option('\u20ac 950', '950');
	pricerange.options[20-offset] = new Option('\u20ac 1000', '1000');
	pricerange.options[21-offset] = new Option('\u20ac 2000', '2000');
	pricerange.options[22-offset] = new Option('\u20ac 3000', '3000');
	pricerange.options[23-offset] = new Option('\u20ac 4000', '4000');
	pricerange.options[24-offset] = new Option('\u20ac 5000', '5000');
	pricerange.options[25-offset] = new Option('\u20ac 6000', '6000');
	
	if(name!='pricerange1') pricerange.options[26-offset] = new Option('\u20ac 7000 +', '7000');
}
function populateHoliday(name) {
	
	$('price_label').update('Price (Per night)');
	
	pricerange = document.getElementById(name);
    for (var i = pricerange.options.length; i > 0; i--)pricerange.options[i] = null;
	
	if(pricerange.name=='pricerange1') var offset = 0; else var offset = 1;
	if(pricerange.name!='pricerange2') pricerange.options[1] = new Option('\u20ac 10', '10');
	
	pricerange.options[2-offset] = new Option('\u20ac 25', '25');
	pricerange.options[3-offset] = new Option('\u20ac 50', '50');
	pricerange.options[4-offset] = new Option('\u20ac 75', '75');
	pricerange.options[5-offset] = new Option('\u20ac 100', '100');
	pricerange.options[6-offset] = new Option('\u20ac 125', '125');
	pricerange.options[7-offset] = new Option('\u20ac 150', '150');
	pricerange.options[8-offset] = new Option('\u20ac 175', '175');
	pricerange.options[9-offset] = new Option('\u20ac 200', '200');
	pricerange.options[10-offset] = new Option('\u20ac 225', '225');
	pricerange.options[11-offset] = new Option('\u20ac 250', '250');
	pricerange.options[12-offset] = new Option('\u20ac 300', '300');
	pricerange.options[13-offset] = new Option('\u20ac 350', '350');
	pricerange.options[14-offset] = new Option('\u20ac 400', '400');	
	pricerange.options[15-offset] = new Option('\u20ac 450', '450');
	pricerange.options[16-offset] = new Option('\u20ac 500', '500');
	pricerange.options[17-offset] = new Option('\u20ac 550', '550');
	pricerange.options[18-offset] = new Option('\u20ac 600', '600');
	pricerange.options[19-offset] = new Option('\u20ac 650', '650');
	pricerange.options[20-offset] = new Option('\u20ac 700', '700');
	pricerange.options[21-offset] = new Option('\u20ac 750', '750');
	pricerange.options[22-offset] = new Option('\u20ac 800', '800');
	pricerange.options[23-offset] = new Option('\u20ac 850', '850');
	pricerange.options[24-offset] = new Option('\u20ac 900', '900');
	pricerange.options[25-offset] = new Option('\u20ac 950', '950');
	pricerange.options[26-offset] = new Option('\u20ac 1000', '1000');
	
	if(name!='pricerange1') pricerange.options[27-offset] = new Option('\u20ac 1000+', '1001');
}
function populate_pw(name, type, v) {
	pricerange = document.getElementById(name);
	for (var i = pricerange.options.length; i > 0; i--)pricerange.options[i] = null;
	if(type==1){
		pricerange.options[1] = new Option('Less than \u20ac 25,000', '25000');
		pricerange.options[2] = new Option('Less than \u20ac 50,000', '50000');
		pricerange.options[3] = new Option('Less than \u20ac 100,000', '100000');
		pricerange.options[4] = new Option('Less than \u20ac 150,000', '150000');
		pricerange.options[5] = new Option('Less than \u20ac 200,000', '200000');
		pricerange.options[6] = new Option('Less than \u20ac 250,000', '250000');
		pricerange.options[7] = new Option('Less than \u20ac 300,000', '300000');
		pricerange.options[8] = new Option('Less than \u20ac 350,000', '350000');
		pricerange.options[9] = new Option('Less than \u20ac 400,000', '400000');
		pricerange.options[10] = new Option('Less than \u20ac 450,000', '450000');
		pricerange.options[11] = new Option('Less than \u20ac 500,000', '500000');
		pricerange.options[12] = new Option('Less than \u20ac 550,000', '550000');
		pricerange.options[13] = new Option('Less than \u20ac 600,000', '600000');
		pricerange.options[14] = new Option('Less than \u20ac 650,000', '650000');
		pricerange.options[15] = new Option('Less than \u20ac 700,000', '700000');
		pricerange.options[16] = new Option('Less than \u20ac 750,000', '750000');
		pricerange.options[17] = new Option('Less than \u20ac 800,000', '800000');
		pricerange.options[18] = new Option('Less than \u20ac 900,000', '900000');
		pricerange.options[19] = new Option('Less than \u20ac 1,000,000', '1000000');
		pricerange.options[20] = new Option('\u20ac 1,0000,00 +', '1000001');
	}else{
		pricerange.options[1] = new Option('Less than \u20ac 50', '50');
		pricerange.options[2] = new Option('Less than \u20ac 100', '100');
		pricerange.options[3] = new Option('Less than \u20ac 150', '150');
		pricerange.options[4] = new Option('Less than \u20ac 200', '200');
		pricerange.options[5] = new Option('Less than \u20ac 250', '250');
		pricerange.options[6] = new Option('Less than \u20ac 300', '300');
		pricerange.options[7] = new Option('Less than \u20ac 350', '350');
		pricerange.options[8] = new Option('Less than \u20ac 400', '400');
		pricerange.options[9] = new Option('Less than \u20ac 450', '450');
		pricerange.options[10] = new Option('Less than \u20ac 500', '500');
		pricerange.options[11] = new Option('Less than \u20ac 550', '550');
		pricerange.options[12] = new Option('Less than \u20ac 600', '600');
		pricerange.options[13] = new Option('Less than \u20ac 650', '650');
		pricerange.options[14] = new Option('Less than \u20ac 700', '700');
		pricerange.options[15] = new Option('Less than \u20ac 750', '750');
		pricerange.options[16] = new Option('Less than \u20ac 800', '800');
		pricerange.options[17] = new Option('Less than \u20ac 850', '850');
		pricerange.options[18] = new Option('Less than \u20ac 900', '900');
		pricerange.options[19] = new Option('Less than \u20ac 950', '950');
		pricerange.options[20] = new Option('Less than \u20ac 1000', '1000');
		pricerange.options[21] = new Option('Less than \u20ac 2000', '2000');
		pricerange.options[22] = new Option('Less than \u20ac 3000', '3000');
		pricerange.options[23] = new Option('Less than \u20ac 4000', '4000');
		pricerange.options[24] = new Option('Less than \u20ac 5000', '5000');
		pricerange.options[25] = new Option('Less than \u20ac 6000', '6000');
		pricerange.options[26] = new Option('\u20ac 6000 +', '6001');
	}
	for (var i = 0; i < pricerange.options.length; i++) if(pricerange.options[i].value==v) pricerange.options[i].selected=true;
}

// Forms
function isblank(s) {
   for(var i = 0; i < s.value.length; i++) {
      var c = s.value.charAt(i);
      if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
   }
   return true;
}
function checkemail(email_var){
	if (isblank(email_var)) return errorMessage(email_var, 'Please enter your Email');
	if (!isblank(email_var))
	{
		var valid = true;	// Initially set valid to true
		var atcount = 0;
		var email = email_var.value;
		var email_length = email_var.value.length;
		
		for (index=0; index<email_length; index++)
		{
			if ((email.substring(index,index+1) == ":") || (email.substring(index,index+1) == ",") || (email.substring(index,index+1) == "'") || (email.substring(index,index+1) == " ") )
			{
				valid = false;
			}
		}
		
		for (i=0; i<email_length; i++)
		{
			if (email.substring(i,i+1) == "@")
			{
				++atcount;
				if (email.substring(i-1,i) == ".")
					valid = false;
				if (email.substring(i+1,i+2) == ".")
					valid = false;
			}
		}
			if (email.substring(email_length-1,email_length) == "@")
			valid = false;
			if (email.substring(email_length-1,email_length) == ".")
			valid = false;
			if (email.substring(0,1) == "@")
			valid = false;
			if (email.substring(0,1) == ".")
			valid = false;
			if (email_var.value.indexOf(".") == -1)
			valid = false;
			if (atcount > 1 || atcount == 0)
			valid = false;
		
		if (!valid) {
			return errorMessage(email_var, 'Please enter a correct Email');
		}
	}
}
var errors = 0;
function errorMessage(el, message,newline){
	if(newline) line="<BR>"; else line='';
	$(el).up().down('label').insert('<span class="error">&nbsp;&nbsp;'+line+'['+message+']</span>');
	errors = errors+1;
}
function clearErrors(form){
	spans = $(form).select('span[class="error"]');
	for(i=0; i<spans.length;i++) spans[i].remove();
	errors=0;
}
function checkSignUp(form){
	clearErrors(form);
	if (isblank(form.name)) errorMessage(form.name, 'Please enter your Name');
	if (isblank(form.surname)) errorMessage(form.surname, 'Please enter your Surame');
	checkemail(form.email);
	if (isblank(form.password)) errorMessage(form.password, 'Please enter a Password');
	if (isblank(form.confirm_password)) errorMessage(form.confirm_password, 'Please re-type Password');
	if(form.password.value!=form.confirm_password.value) errorMessage(form.confirm_password, 'Passwords do not match!');
	if(errors) return false;
}
function checkLogin(form){
	clearErrors(form);
	checkemail(form.log_email);
	if (isblank(form.log_password)) errorMessage(form.log_password, 'Please enter a Password');
	if(errors) return false;
}
function checkForgotPassword(form){
	clearErrors(form);
	checkemail(form.email);
	if(errors) return false;
}
function checkPersonalDetails(form){
	clearErrors(form);
	if (isblank(form.name)) errorMessage(form.name, 'Please enter your Name');
	if (isblank(form.surname)) errorMessage(form.surname, 'Please enter your Surame');
	if(form.type.selectedIndex == 0) errorMessage(form.type, 'Please select an option');
	if (isblank(form.mobile)) errorMessage(form.mobile, 'Please enter your Mobile Number');
	if (isblank(form.phone)) errorMessage(form.phone, 'Please enter your Phone Number');
	if (isblank(form.town)) errorMessage(form.town, 'Please enter your Town / City');
	if(errors) return false;
}
function checkShortLets(form){
	clearErrors(form);
	if (isblank(form.name)) errorMessage(form.name, 'Please enter your Name');
	checkemail(form.email);
	if (isblank(form.numofpeople)) errorMessage(form.numofpeople, 'Required');
	
	//date
	var d1;
	var d2;
	if (isblank(form.arrival)) errorMessage(form.arrival, 'Please select Date');
	else d1 = new Date(form.arrival.value.replace(/-/gi, "/"));
	if (isblank(form.departure)) errorMessage(form.departure, 'Please select Date');
	else d2 = new Date(form.departure.value.replace(/-/gi, "/"));
	if(d1>d2) {alert('Please make sure that your return date\nis not earlier than your arrival date.'); return false;}
	
	if(errors) return false;
}
function checkLoginDetails(form){
	clearErrors(form);
	checkemail(form.email);
	if (isblank(form.password)) errorMessage(form.password, 'Please enter a Password');
	if (isblank(form.confirm_password)) errorMessage(form.confirm_password, 'Please re-type Password',true);
	if(form.password.value!=form.confirm_password.value) errorMessage(form.confirm_password, 'Passwords do not match!',true);
	if(errors) return false;
}
function add_to_favs(type,web_ref){
	var params = 'web_ref=' + web_ref +'&add='+type;
	var myAjax = new Ajax.Updater('client_favorites', '/ajax/favorites.php', {
		method: 'get', 
		parameters: params
	});
}
function form_submit(form,arr){
	for(i=0;i<arr.length;i++){
	$(form).select('[name="'+arr[i][0]+'"]')[0].value=''+arr[i][1];
	}
	$(form).submit();
}
function printIT(web_ref){
	window.open('property-flyer.php?web_ref='+web_ref,'printwin','left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');
}
function changeFrequency(e,s){
	window.location='process.php?t=13&search_id='+s+'&f='+e.value;	
}
function changeCurrency(cur){
	var myAjax = new Ajax.Request('/ajax/currency.php', { 
	method: 'get', 
	parameters: 'c='+cur, 
	onSuccess: function() { 
		if($('page_form')) $('page_form').submit();
		else window.location.reload(true);	
	}
	});
}
function favsNotLogged(e){
	e.checked=false;
	Lightview.show({ href: '#loginBox', options: {topclose: true, autosize:true}});
}
function submitSidebarForm(t){
		$(t).checked=true;
		$('country').selectedIndex=0;
		$('location').selectedIndex=0;
		$('pricerange').value=-1;
		$('property_type_id').selectedIndex=0;
		$('ref_no').value='';	
		$('sidebar-form').submit();
}
function calendar(offset,property_ref){
	var params = 'property_ref='+property_ref+'&offset=' + offset;
	var myAjax = new Ajax.Updater('calendar', 'http://www.doorsrealestate.com/ajax/calendar.php', {
		method: 'get', 
		parameters: params
	});
}
