var checkit = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
var allValid;

function alphabetonly(checkStr)
{
	allValid=true
	for (i = 0; i < checkStr.length; i++)
	{
		ch1 = checkStr.charAt(i);
		for (j = 0; j < checkit.length; j++)
		if(ch1 == checkit.charAt(j))
			break;
		if (j == checkit.length)
		{
			allValid = false;
			break;
		}
	}
}

var checkthis = "0123456789+-/ ";
var allValid1;

function noalphabet(checkStr)
{
	allValid1=true
	for (i = 0; i < checkStr.length; i++)
	{
		ch1 = checkStr.charAt(i);
		for (j = 0; j < checkthis.length; j++)
		if(ch1 == checkthis.charAt(j))
			break;
		if (j == checkthis.length)
		{
			allValid1 = false;
			break;
		}
	}
}

$(function() {

  $("#sell").click(function() {
	// validate and process form
	var title = $("select#cmbtitle").val();		
	var name = $("input#txtfName").val();
	if (name == "") {
	alert("Please enter your name.");
	$("input#txtfName").focus();
	return false;
	}
	
	var phone = $("input#txtTel").val();
	if (phone == "") {
	alert("Please enter your mobile no.");
	$("input#txtTel").focus();
	return false;
	}
	noalphabet(phone);
	if (!allValid1)
	{
	alert("Please enter only numerics for phone number.")
	$("input#txtTel").focus();
	return false;
	}
	
	var phoneOff = $("input#txtOff").val();
	if (phoneOff == "") {
	alert("Please enter your office contact no.");
	$("input#txtOff").focus();
	return false;
	}
	noalphabet(phoneOff);
	if (!allValid1)
	{
	alert("Please enter only numerics for office phone number.")
	$("input#txtOff").focus();
	return false;
	}
	var email = $("input#txt_email").val();
	if (email.indexOf("@") == -1) {
	alert("Please enter a valid email address.");
	$("input#txt_email").focus();
	return false;
	}
	var city = $("input#txtCity").val();
	var country = $("select#country").val();
	var property_type = $("select#property_type").val();
	if (property_type == "") {
	alert("Please enter your property type.");
	$("select#property_type").focus();
	return false;
	}
	var villa = $("input#txtVilla").val();
	var ppty_location = $("input#ppty_location").val();
	if (ppty_location == "") {
	alert("Please enter your property location.");
	$("input#ppty_location").focus();
	return false;
	}
	var bedrooms = $("select#bedrooms").val();
	var bathrooms = $("select#bathrooms").val();
	var development = $("select#development").val();
	if (development == "") {
	alert("Please select area.");
	$("input#development").focus();
	return false;
	}
	var compdate = $("input#compdate").val();
	var ppty_age = $("input#ppty_age").val();
	var sqfeet = $("input#sqfeet").val();
	var orgprice = $("input#orgprice").val();
	var sellprice = $("input#sellprice").val();
	if (sellprice == "") {
	alert("Please enter selling price.");
	$("input#sellprice").focus();
	return false;
	}
	var pool = '';
	if ($('#Pool').is(':checked')) { var pool = $("input#Pool").val(); }
	var garden = '';
	if ($('#Garden').is(':checked')) { var garden = $("input#Garden").val(); }
	var view = '';
	if ($('#View').is(':checked')) { var view = $("input#View").val(); }
	var parking = '';
	if ($('#Parking').is(':checked')) { var parking = $("input#Parking").val(); }
	var gym = '';
	if ($('#Gym').is(':checked')) { var gym = $("input#Gym").val(); }
	var doorman = '';
	if ($('#Doorman').is(':checked')) { var doorman = $("input#Doorman").val(); }
		
	var comments = $("textarea#addinfo").val();
	if (comments == "") {
	alert("Please provide additional information.");
	$("textarea#addinfo").focus();
	return false;
	}
	var code = $("input#code").val();
	if (code == "") {
	alert("Please enter verification code.");
	$("input#code").focus();
	return false;
	}
		
	var dataString = 'title=' + title + '&txtname='+ name + '&email=' + email + '&phone=' + phone + '&phoneOff=' + phoneOff + '&city=' + city + '&country=' + country + '&property_type=' + property_type + '&villa=' + villa + '&ppty_location=' + ppty_location + '&bedrooms=' + bedrooms + '&bathrooms=' + bathrooms + '&development=' + development + '&compdate=' + compdate + '&ppty_age=' + ppty_age + '&sqfeet=' + sqfeet + '&orgprice=' + orgprice + '&sellprice=' + sellprice + '&pool=' + pool + '&garden=' + garden + '&view=' + view + '&parking=' + parking + '&gym=' + gym + '&doorman=' + doorman + '&code=' + code + '&comments=' + comments;
	//alert (dataString);return false;
		
	$.ajax({
	type: "POST",
	url: "process_sell.php",
	data: dataString,
	success: function(data) {
	$('#contact_emailform').html("<div id='message'></div>");
	if (data != '') {	
	$('#message').html("<h2>Listing details Submitted!</h2>")
	.append("<p>We will be in touch soon.</p>")
	.hide()
	.fadeIn(1500, function() {
	  $('#message').append("<img id='checkmark' src='images/check.png' />");
	});
	} else {
	$('#message').html("<h2>Incorrect Verification Code.</h2>")
	.append("<p>Sorry, the code you entered was invalid. Please try again.</p>")
	.hide()
	.fadeIn(1500, function() {
	  $('#message').append("<img id='checkmark' src='images/uncheck.png' />");
	});
	}
	
	}
	});
	return false;
  });
});
