var email_reg = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
var rent_reg = /for-rent/;
var sale_reg = /for-sale/;


function calculate_mortgage(){
  var amount = $('#mc_amount').val();
  var rate = $('#mc_rate').val()/1200;
  var term = $('#mc_term').val();
  var thePayment = Math.round(amount * (rate+(rate/(Math.pow(1 + rate,term * 12)-1))));

  $('#mc_payment').attr('innerHTML', "$" + thePayment);
  return false;
}

function update_saved_listings(link_text, link_url){
  if($("#saved_listings li").size()<5 && $("#saved_links li a:contains('"+link_text+"')").size()==0){
    var li = document.createElement("li");
    li.innerHTML = "<a href='"+link_url+"'>"+link_text+"</a>";
    $("#saved_listings #saved_links").append(li);
  }
  $("#saved_listings").show();
  $("#saved_listings_updated").show();
}

function toggle_price_selection(id, enable){
  var el = $("#"+id);
  if(enable==true){
    el.show();
    $("#price_container").append(el);
  }
  else{
    el.hide();
    $("#price_dropdowns").append(el);
  }
}

function set_price_selection(){
  if($("#category_selection").val()==""){
    toggle_price_selection("price_selection_sale", false);
    toggle_price_selection("price_selection_rental", false);
    toggle_price_selection("price_selection_vacation_rental", false);

    $("#price_label").text("");
    return;
  }
  if($("#category_selection").val()=="Vacation Rental-for-rent"){
    toggle_price_selection("price_selection_sale", false);
    toggle_price_selection("price_selection_rental", false);
        
    toggle_price_selection("price_selection_vacation_rental", true);
    $("#price_label").text("Nightly Rate $USD");
    return;
  }
  if(sale_reg.test($("#category_selection").val())){
    toggle_price_selection("price_selection_rental", false);
    toggle_price_selection("price_selection_vacation_rental", false);
    
    toggle_price_selection("price_selection_sale", true);
    $("#price_label").text("Maximum Price $USD");
    return;
  }
  if(rent_reg.test($("#category_selection").val())){
    toggle_price_selection("price_selection_sale", false);
    toggle_price_selection("price_selection_vacation_rental", false);
    
    toggle_price_selection("price_selection_rental", true);
    $("#price_label").text("Monthly Rate $USD");
    return;
  }
}


function check_email_form(){
  if($("#your_name").val().length<1){
	  alert("Your name can't be blank.");
	  return false;
	}
  if(!email_reg.test($("#your_email").val())){
    alert("Your email is invalid");
    return false;
  }
  if($("#to_name").val().length<1){
	  alert("Friend's name can't be blank.");
	  return false;
	}
  if(!email_reg.test($("#to_email").val())){
    alert("Friend's email is invalid");
    return false;
  }
  return true;
}

function check_form(){
	if(reservation==true){
	  alert('Your request has been successfully submitted. Please check your email!');
	  return false;
	}
	if($("#lead_contact_name").val().length<1){
	  alert("Name can't be blank.");
	  return false;
	}
	if($("#lead_phone").val()!=null){
	  if($("#lead_phone").val().length<1){
	    alert("Phone can't be blank.");
	    return false;
	  }
	}
	if(!email_reg.test($("#lead_email").val())){
	  alert("Email is invalid");
	  return false;
	}
	if($("#lead_comment").val().length<1){
	  alert("Message can't be blank.");
	  return false;
	}
	$('#contact_form').hide();
	$('#contact_message').show();

	reservation = true;
	return true;
}


function check_subscription_form(){
	if($("#lead_contact_name").val().length<1){
	  alert("Name can't be blank.");
	  return false;
	}
	if(!email_reg.test($("#lead_email").val())){
	  alert("Email is invalid");
	  return false;
	}
	
	$('#subscription_form').hide();
	$('#subscription_message').show();

	return true;
}
