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() {

  $("#submit_register").click(function() {
	// validate and process form
	var title = $("select#cmbtitle").val();
		
	var fname = $("input#txtfName").val();
	if (fname == "") {
	alert("Please enter your first name.");
	$("input#txtfName").focus();
	return false;
	}

	var lname = $("input#txtlName").val();
	if (lname == "") {
	alert("Please enter your last name.");
	$("input#txtlName").focus();
	return false;
	}

	var nationality = $("select#txt_nationality").val();
	if (nationality == "") {
	alert("Please provide your nationality.");
	$("input#txt_nationality").focus();
	return false;
	}

	var cor = $("select#txt_cor").val();
	if (cor == "") {
	alert("Please provide your country of residence.");
	$("input#txt_cor").focus();
	return false;
	}

	var phone = $("input#txtTel").val();
	if (phone == "") {
	alert("Please enter telephone number.");
	$("input#txtTel").focus();
	return false;
	}
	noalphabet(phone);
	if (!allValid1)
	{
	alert("Please enter only numerics for telephone number.")
	$("input#txtTel").focus();
	return false;
	}
	
	var gender = $("input[@name=gender]:checked").val();

	var email = $("input#txt_email").val();
	if (email.indexOf("@") == -1) {
	alert("Please enter a valid email address.");
	$("input#txt_email").focus();
	return false;
	}	
	
	var dataString = 'title='+ title + '&fname='+ fname + '&lname='+ lname + '&nationality='+ nationality +'&cor='+ cor +'&phone=' + phone + '&gender=' + gender + '&email=' + email;
	//alert (dataString);return false;
		
	$.ajax({
	type: "POST",
	url: "bin/process_register.php",
	data: dataString,
	success: function(data) {
	if (data != '') {
	$('#contact_form').html("<div id='message'></div>");
	$('#message').html("<h2>Thank you for Registering your details.</h2>")
	.append("<p>You will shortly receive an email with an activation link and your user name and password.</p>")
	.hide()
	.fadeIn(1500, function() {
	  $('#message').append("<img id='checkmark' src='images/check.png' style='border:0px' />");
	});
	} else {
	$('#msg').html("User with the same email address already exists.");
	}
	}
	});
	return false;
  });
});

