function validRequired(formField,fieldLabel)
{
	var result = true;
	
	if (formField.value == "")
	{
		alert(fieldLabel);
		formField.focus();
		result = false;
	}
	
	return result;
}
// Email Validation
function checkEmail(myForm,mess)
{
 if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.value))
 {
	return (true)
 }
	alert(mess);
	myForm.focus();
	return (false)
}


function isNotNumeric(str){
		for (var i = 0; i < str.length; i++)
		{
				var ch = str.substring(i, i + 1);
				if((ch < '0' || '9' < ch)) 
				{
					if(ch == "+" || ch == "-" || ch == "." || ch == ")" || ch == "("  || ch == " ") continue;
					return true;
				}
		}
		return false;
}
function isNotAlphabets(str){
		for (var i = 0; i < str.length; i++)
		{
				re = / /gi				//Replace the space between words with no space
				str = str.replace(re,"");
			
				var ch = str.substring(i, i + 1);
				
				if((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) 
				{
					return true;
				}
		}
		return false;
}

function checkMyForm() {
		 if(!validRequired(document.frm.contact_name,"Please enter your name"))
			return false;
		 if(isNotAlphabets(document.frm.contact_name.value))
		 {
			alert("Invalid characters in Name.");
			document.frm.contact_name.focus();
			return false;
		 }

			
		if (!checkEmail(document.frm.email,"Please enter valid email"))
			return false;
				 
return true;
}


$(document).ready(function(){

	$("#frm").submit(function(){

	if(!checkMyForm())
	{
		return false;
	}

		
/*	$("#disp_thanks").html("Please wait...");
	$("#disp_thanks").show();*/
	
	$.post("bin/signup.php",{contact_name:$("#contact_name").val(), email:$("#email").val(), company:$("#company").val() } , function(data)
	{
		if(data)
		{
			// show div in effect
					$('#Newsletter_form').html("<div id='message'></div>");
					$('#message').html("<img id='checkmark' src='images/check.png' />")
					.append("<p>Thank you, your details have been submitted<br>We will be in touch soon!</p>")
					.hide()
					.fadeIn(1500, function() {
					  $('#message');
					});
		}
  	});
		
	});		
		
});

function clearFields() {
	$('#frm input').focus(function()
	{
		if ($(this).attr('rel') != 'cleared')
		{
			$(this).val('').attr('rel', 'cleared');
		}		
	});
	$('#frm input').blur(function()
	{
		if($(this).val() == "")
		{	
			$(this).val(this.defaultValue).attr('rel', '');
		}		
	});
}

$(function($) {
	clearFields();
});
