ddsmoothmenu.init({
	mainmenuid: "menu", //menu DIV id
	orientation: 'h', //Horizontal or vertical menu: Set to "h" or "v"
	classname: 'ddsmoothmenu', //class added to menu's outer DIV
	//customtheme: ["#1c5a80", "#18374a"],
	contentsource: "markup" //"markup" or ["container_id", "path_to_menu_file"]
})

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


jQuery(document).ready(function ($) {
	var mortgageCalculator = false;
	$('#listing-btn-mortgage').click(function(){if(!mortgageCalculator) {$('#listing-mortgage-calculator').slideDown(400);setTimeout(function() { mortgageCalculator = true }, 400);} else {$('#listing-mortgage-calculator').slideUp(400);setTimeout(function() { mortgageCalculator = false }, 400);} return false;});
	$('#listing-mortgage-amortization').keyup(function () {this.value = this.value.replace(/[^0-9\.]/g,'');});

	var contactRequest = false;
	$('#listing-contact-request').click(function() {if(!contactRequest) {$('#listing-contact').slideDown(500);setTimeout(function() { contactRequest = true }, 500);} else {$('#listing-contact').slideUp(500);setTimeout(function() { contactRequest = false }, 500);} return false;});
	
	$('#listing-contact-submit').click(function() {
		var retval = true;
		$("#listing-contact-name, #listing-contact-phone, #listing-contact-email").css("border", "1px solid #CFCFCF");
		$("#listing-contact-name, #listing-contact-email").each(function () {
			$(this).val($(this).val().replace(/\,/g,''));
            if (this.value.length == 0) {
                $(this).css("border", "1px solid red").focus();
				retval = false;
				return false;
            }
        });
		
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
        var email = $("#listing-contact-email").val();
        if (!pattern.test(email)) {
            $("#listing-contact-email").css("border", "1px solid red").focus();
            retval = false;
			return false;
        }
		
		if (retval){
			$.post("http://londonontariorealty.com/wp-content/themes/londonontariorealty/ajax.requester.php", {
				"name": $("#listing-contact-name").val(),
				"email": $("#listing-contact-email").val(),
				"phone": $("#listing-contact-phone").val(),
				"property": $("#listing_title").val()
			}, function (data) {
				if (data.response == "Thank You") {
					$('#listing-contact-feedback').html('Your request has been sucessfully sent! An agent will contact you shortly.').slideDown(400);
					$("#listing-contact-name, #listing-contact-phone, #listing-contact-email").each(function (i) {
						this.value = this.defaultValue
					})
				} else {
					$("#listing-contact-feedback").html(data.response).slideDown("slow");
				}
			}, "json");
		}
		return false;
	});
	
    $("#s, #bs, #cname, #cemail, #ccontact, .ovinput, #sccontact").focus(function () {
        if (this.value == this.defaultValue) {
            this.value = ""
        }
    }).blur(function () {
        if (!this.value.length) {
            this.value = this.defaultValue
        }
    });
	
	$(".welcomefrontbg, .welcomefrontinner, .welcomeimage").click(function(){
		$(".welcomefrontbg, .welcomefrontinner, .welcomeimage").each(function() {$(this).fadeOut("slow");});
	});
	
	$("#emailthis").click(function(){
		$(".emailboxs").css("display","block");
		$('html, body').animate({scrollTop:0}, 'slow');
		return false;
	});
	$(".white_content .close").click(function(){
		$(".emailboxs").css("display","none");
		return false;
	});

	$("#shareform").submit(function () {
		$("#scname, #scfromemail, #sctoemail, #sccontact").css("border", "1px solid #B7B7B8");
        $("#scname, #scfromemail, #sctoemail, #sccontact").each(function () {
            if (this.value == this.defaultValue) {
                $(this).css("border", "1px solid red").focus();
                return false
            }
        });
        var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
        var email = $("#scfromemail").val();
        if (!pattern.test(email)) {
            $("#scfromemail").css("border", "1px solid red").focus();
            return false
        }
		var email = $("#sctoemail").val();
        if (!pattern.test(email)) {
            $("#sctoemail").css("border", "1px solid red").focus();
            return false
        }
		var sendcopy = "no";
		if ($("#sharecheck").is(':checked')){sendcopy = "yes"}
        $.post("http://londonontariorealty.com/wp-content/themes/londonontariorealty/ajax.share.php", {
            "scname": $("#scname").val(),
            "scfromemail": $("#scfromemail").val(),
            "sctoemail": $("#sctoemail").val(),
            "sccontact": $("#sccontact").val(),
            "scpropid": $("#scpropid").val(),
			"sccopy": sendcopy
        }, function (data) {
            if (data.response == "Thank You") {
                $("#scresponse").addClass('ctsuccess').html("Thank you for sharing this property!").slideDown("slow");
                $("#scname, #scfromemail, #sctoemail, #sccontact").each(function (i) {
                    this.value = this.defaultValue
                })
				$("#sccopy").attr('checked', false);
            } else {
                alert(data.response)
            }
        }, "json");
        return false
    });
	
	$('#listing-mortgage-calculate').click(function() {
		var retval = true;
		$("#listing-mortgage-amortization, #listing-mortgage-amount, #listing-mortgage-interest").css("border", "1px solid #CFCFCF");
		$("#listing-mortgage-amortization, #listing-mortgage-amount, #listing-mortgage-interest").each(function () {
			$(this).val($(this).val().replace(/\,/g,''));
            if (this.value.length == 0) {
                $(this).css("border", "1px solid red").focus();
				retval = false;
				return false;
            }
        });
		if (retval) {
			var intest = $("#listing-mortgage-interest").val();
			var mortage = $("#listing-mortgage-amount").val();
			var numyears = $("#listing-mortgage-amortization").val();
			var numberofpayments = 12 * numyears;
			var i_interest = (intest/1200) ;
			var n_interest = Math.pow((i_interest + 1),numberofpayments);
			var monpayment = ( (i_interest)*(n_interest) / ( n_interest - 1) );
			monpayment = monpayment * mortage;
			monpayment = addCommas(monpayment.toFixed(2));
			mortage = addCommas(parseInt(mortage).toFixed(2));
			$("#listing-mortgage-feedback").html("For a <span>"+numyears+" year</span> mortgage for <span>$"+mortage+"</span> at the rate of <span>"+intest+"%</span>, your monthly payment is <span>$"+monpayment+"</span>");
		}
		return false;
	});

	$("#wid_calculate").click(function(){
		var retval = true;
		$("#wid_mortgage, #wid_interest, #wid_amortization").css("border", "1px solid #CFCFCF");
		$("#wid_mortgage, #wid_interest, #wid_amortization").each(function () {
			$(this).val($(this).val().replace(/\,/g,''));
            if (this.value == this.defaultValue) {
                $(this).css("border", "1px solid red").focus();
				retval = false;
				return false;
            }
        });
		if (retval) {
			var intest = $("#wid_interest").val();
			var mortage = $("#wid_mortgage").val();
			var numyears = $("#wid_amortization").val();
			var numberofpayments = 12 * numyears;
			var i_interest = (intest/1200) ;
			var n_interest = Math.pow((i_interest + 1),numberofpayments);
			var monpayment = ( (i_interest)*(n_interest) / ( n_interest - 1) );
			monpayment = monpayment * mortage;
			monpayment = addCommas(monpayment.toFixed(2));
			mortage = addCommas(parseInt(mortage).toFixed(2));
			$("#mls_calc_div").html("For a <span>"+numyears+" year</span> mortgage for <span>$"+mortage+"</span> at the rate of <span>"+intest+"%</span>, your monthly payment is <span>$"+monpayment+"</span>");
			retval = false;
		}
		return false;
	});
	
  $("#finditnow").click(function () {
	   $("#searchname, #searchemail, #searchphone").css("border", "1px solid #CFCFCF");
		  $("#searchname, #searchemail, #searchphone").each(function () {
      if (this.value == this.defaultValue) {
        $(this).css("border", "1px solid red").focus();
        return false;
      } else {
        var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
        var email = $("#searchemail").val();
        if (!pattern.test(email)) {
          $("#searchemail").css("border", "1px solid red").focus();
          return false
        } else {
          $("#homesearchform").submit();
        }
      }
    });
  });
	
	 $("#mlsloginform").submit(function() {
		$("#mlslogemail, #mlslogpasswd").each(function () {
			$(this).css("border", "1px solid #CFCFCF").focus();
		});
		var retval = true;
		$("#mlslogemail, #mlslogpasswd").each(function () {
				if (this.value == this.defaultValue) {
					$(this).css("border", "1px solid red").focus();
					retval = false;
					return false;
				}
		});
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		var email = $("#mlslogemail").val();
		if (!pattern.test(email)) {
			$("#mlslogemail").css("border", "1px solid red").focus();
			retval = false;
			return false;
		}
		return retval;
		
	});
	
	$("#mlsregisterform").submit(function() {
		$("#mlsfname, #mlslname, #mlsemail, #mlspasswd, #mlspasswd1").each(function () {
			$(this).css("border", "1px solid #CFCFCF").focus();
		});
		var retval = true;
		if ($("#mlshide").is(":visible")) {
			$("#mlsfname, #mlslname, #mlsemail, #mlspasswd, #mlspasswd1").each(function () {
				if (this.value == this.defaultValue) {
					$(this).css("border", "1px solid red").focus();
					retval = false;
					return false;
				}
			});
			var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
			var email = $("#mlsemail").val();
			if (!pattern.test(email)) {
				$("#mlsemail").css("border", "1px solid red").focus();
				retval = false;
				return false;
			}
			if ($("#mlspasswd").val() != $("#mlspasswd1").val()){
				$("#mlspasswd").css("border", "1px solid red").focus();
				$("#mlspasswd1").css("border", "1px solid red").focus();
				retval = false;
			}
		}
		else {
			$("#mlsfname, #mlslname, #mlsemail").each(function () {
				if (this.value == this.defaultValue) {
					$(this).css("border", "1px solid red").focus();
					retval = false;
					return false;
				}
			});
			var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
			var email = $("#mlsemail").val();
			if (!pattern.test(email)) {
				$("#mlsemail").css("border", "1px solid red").focus();
				retval = false;
				return false;
			}			
			if (retval) $("#mlshide").slideDown();
			retval = false;
		}
		return retval;
	});
	
	$("#widget_mls_btn").click(function () {
		if ($("#widget_mls").val().length ==0){
		$("#widget_mls").focus();
		return false;
		}
	});
/*
	$("#wid_newsletter").click(function(){
		var retval = true;
		$("#wid_name, #wid_email").each(function () {
			if (this.value == this.defaultValue) {
				$(this).css("border", "1px solid red").focus();
				retval = false;
				return false;
			}
		});
		if (retval){
			$.post("http://londonontariorealty.com/wp-content/themes/londonontariorealty/ajax.newsletter.php", {
				"name": $("#wid_name").val(),
				"email": $("#wid_email").val()
			}, function (data) {
				if (data.response == "Thank You") {
					$("#wid_news_response").html("Thank you for signing up for our newsletter").slideDown("slow");
					$("#wid_name, #wid_email").each(function (i) {
						this.value = this.defaultValue
					})
				} else {
					$("#wid_news_response").html(data.response).slideDown("slow");
				}
			}, "json");
		}
		return false;
	});
*/	
    $("#ctform").submit(function () {
		var retval = true;
        $("#ctname, #ctemail, #ctmsg").css("border", "1px solid #B7B7B8");
        $("#ctname, #ctemail, #ctmsg").each(function () {
            if (this.value == this.defaultValue) {
                $(this).css("border", "1px solid red").focus();
				retval = false;
                return false;
            }
        });
        var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
        var email = $("#ctemail").val();
        if (!pattern.test(email)) {
            $("#ctemail").css("border", "1px solid red").focus();
			retval = false;
            return false
        }
		if (retval){
			$.post("http://londonontariorealty.com/wp-content/themes/londonontariorealty/ajax.contact.php", {
				"ctname": $("#ctname").val(),
				"ctemail": $("#ctemail").val(),
				"ctphone": $("#ctphone").val(),
				"ctmsg": $("#ctmsg").val(),
			}, function (data) {
				if (data.response == "Thank You") {
					$("#ctresponse").html("Thank you for contacting us!").slideDown("slow");
					$("#ctname, #ctemail, #ctmsg, #ctphone").each(function (i) {
						this.value = this.defaultValue
					})
				} else {
					alert(data.response)
				}
			}, "json");
		}
        return false;
    });

    $("#dynform").submit(function () {
		var retval = true;
        $("#dynname, #dynemail, #dynphone").css("border", "1px solid #B7B7B8");
        $("#dynname, #dynemail").each(function () {
            if (this.value == this.defaultValue) {
                $(this).css("border", "1px solid red").focus();
				retval = false;
                return false;
            }
        });
        var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
        var email = $("#dynemail").val();
        if (!pattern.test(email)) {
            $("#dynemail").css("border", "1px solid red").focus();
			retval = false;
            return false
        }
		if (retval){
			var vname = $("#dynname").val();
			var vemai = $("#dynemail").val();
			var hidde = $("#dynhidden").val();
			var phone = $("#dynphone").val();
			var vmsgs = $("#dynmsg").val();
			var vaddy = $("#dynaddy").val();
			$.post("http://londonontariorealty.com/wp-content/themes/londonontariorealty/ajax.dynform.php", {
				"name": vname,
				"email": vemai,
				"subject": hidde,
				"phone": phone,
				"msg": vmsgs,
				"addy": vaddy
			}, function (data) {
				if (data.response == "Thank You") {
					$("#dynresp").addClass('res_good').html("Thank you for contacting us!");
					$("#dynname, #dynemail, #dynphone, #dynmsg, #dynaddy").each(function (i) {
						this.value = this.defaultValue
					})
				} else {
					alert(data.response)
				}
			}, "json");
		}
        return false;
    });
	
	$(".deleter").click(function() {	
		var answer = confirm ("Are you sure you want to delete this item?");
		if (!answer)
			return false;
	});
	
});
