$(document).ready(function(){
	$("#contactform").submit(function(){
		var fname = $('input[name=fname]').val();
		var lname = $('input[name=lname]').val();
		var email = $('input[name=email]').val();
		var phone = $('input[name=phone]').val();
		var message = $('textarea[name=message]').val();
		var security_code = $('input[name=security_code]').val();
		var error = '';
		var focus = '';
		remove_all_error();
		if ( fname == '' )
		{
			error += "Please enter your name\n";
			show_error('input[name=fname]', "Please enter your name");
		}
		else if ( fname.length < 3 )
		{
			error += "Name was too short\n";
			show_error('input[name=fname]', "Name was too short");
		}
		if ( error != '' )
			focus = 'input[name=fname]';
		if ( email == '' )
		{
			error += "Please enter your email address\n";
			show_error('input[name=email]',"Please enter your email address");
		}
		else if ( ! valid_email(email) )
		{
			error += "Please enter a valid email address\n";
			show_error('input[name=email]', "Please enter a valid email address");
		}
		if ( focus == '' && error != '' )
			focus = 'input[name=email]';
		if ( phone == '' )
		{
			error += "Please enter your phone number\n";
			show_error('input[name=phone]',"Please enter your phone number");
		}
		else if ( ! valid_number(phone) )
		{
			error += "Please enter a valid phone number\n";
			show_error('input[name=phone]', "Please enter a valid phone number");
		}
		if ( focus == '' && error != '' )
			focus = 'input[name=phone]';
		if ( message == '' )
		{
			error += "Please enter your message\n";
			show_error('textarea[name=message]', "Please enter your message");
		}
		if ( security_code == '' )
		{
			error += "Please enter captcha code\n";
			show_error('input[name=security_code]',"Please enter captcha code");
		}
		
		if ( focus == '' && error != '' )
			focus = 'textarea[name=message]';
			
		
		if ( error == '' )
		{
			var datastring = 'fname='+fname+'&lname='+lname+'&email='+email+'&phone='+phone+'&message='+message+'&security_code='+security_code;
			/*$.post('contact2.php', datastring, function(data){ $("#contactform")[0].reset(); $("#contactform").append(data); });
			return false;
			*/
			//start the ajax
			$.ajax({
				url: "contact2.php",	
				type: "POST",
				data: datastring,		
				cache: false,
				success: function (html) {				
					var msgpart  = html.substring(0,8);
					if(msgpart === 'Success:'){
						$("#contactform")[0].reset(); 
						var successMsg = html.substring(8);
						$("#contactformMsg").html(successMsg);
					} else $("#contactformMsg").html(html);				
				}		
			});
		}
		$(focus).focus();
		return false;
	});
	$("#quickcontact").submit(function(){
		var qname = $('input[name=qname]').val();
		var qemail = $('input[name=qemail]').val();
		var qmessage = $('textarea[name=qmessage]').val();
		var captcha = $('input[name=captcha]').val();
		var error = '';
		var focus = '';
		remove_all_error();
		if ( qname == '' )
		{
			error += "required\n";
			show_error('input[name=qname]', "required");
		}
		else if ( qname.length < 3 )
		{
			error += "too short\n";
			show_error('input[name=qname]', "too short");
		}
		if ( error != '' )
			focus = 'input[name=qname]';
		if ( qemail == '' )
		{
			error += "required\n";
			show_error('input[name=qemail]',"required");
		}
		else if ( ! valid_email(qemail) )
		{
			error += "invalid email\n";
			show_error('input[name=qemail]', "invalid email");
		}
		if ( captcha == '' )
		{
			error += "Please enter captcha code\n";
			show_error('input[name=captcha]',"Please enter captcha code");
		}
		if ( focus == '' && error != '' )
			focus = 'input[name=qemail]';
		if ( qmessage == '' )
		{
			error += "required\n";
			show_error('textarea[name=qmessage]', "required");
		}
		if ( focus == '' && error != '' )
			focus = 'textarea[name=qmessage]';
		if ( error == '' )
		{
			
			var datastring = 'qname='+qname+'&qemail='+qemail+'&qmessage='+qmessage+'&captcha='+captcha;
			/*$.post('qcontact.php', 'qname='+qname+'&qemail='+qemail+'&qmessage='+qmessage, function(data){ 
			
			$("#quickcontact")[0].reset(); $("#quickcontact").append(data); });
			return false;
			*/
			//start the ajax
			$.ajax({
				url: "qcontact.php",	
				type: "POST",
				data: datastring,		
				cache: false,
				success: function (html) {				
					var msgpart  = html.substring(0,8);
					if(msgpart === 'Success:'){
						$("#quickcontact")[0].reset(); 
						var successMsg = html.substring(8);
						$("#quickcontactMsg").html(successMsg);
					} else $("#quickcontactMsg").html(html);				
				}		
			});
		}
		$(focus).focus();
		return false;
	});
	$("#newsletterform").submit(function(){
		var nemail = $('input[name=nemail]').val();
		var error = '';
		var focus = '';
		remove_all_error();
		if ( nemail == '' )
		{
			error += "required\n";
			show_error('input[name=nemail]',"required");
		}
		else if ( ! valid_email(nemail) )
		{
			error += "invalid email\n";
			show_error('input[name=nemail]', "invalid email");
		}
		if ( focus == '' && error != '' )
			focus = 'input[name=nemail]';
		if ( error == '' )
		{
			$.post('newsletter.php', 'nemail='+nemail, function(data){ $("#newsletterform")[0].reset(); $("#newsletterform").append(data); });
			return false;
		}
		$(focus).focus();
		return false;
	});
	$("#careerform").submit(function(){
		
		$.ajaxFileUpload
		(
			{
				url:'careers.php', 
				secureuri:false,
				fileElementId:'filecv',
				dataType: 'text',
				success: function (data, status)
				{
					$("#careerform").append(data);
				},
				error: function (data, status, e)
				{
					alert(e);
				}
			}
		)
		return false;
	});
});

function valid_email( str )
{
	return str.match(/^[A-Za-z0-9._-]+@[A-Za-z0-9._-]+\.[A-Za-z]+$/i);
}
function valid_number( str )
{
	return str.match(/^[0-9.-]+$/i);
}
function valid_url( str )
{
	return str.match(/^(http:\/\/|https:\/\/|)[A-Za-z0-9._-]+\.[A-Za-z0-9_-]+[A-Za-z0-9._\/-]*$/);
}
function show_error( obj, error )
{
	remove_error(obj);
	$(obj).parent().css('position', 'relative').append('<div class="error_message" style="color:#d00;font-size:11px;font-style:italic;position:absolute;top:-18px;right:0;text-align:right;">'+error+'</div>');
	setTimeout("$('.error_message').fadeOut(2000)", 10000);
}
function remove_error( obj )
{
	$(obj).parent().find('.error_message').remove();
}
function remove_all_error()
{
	$('.error_message').remove();
}

