/****
* Name: gForm
* Author: g6Solution
* Version: 1.0
* Web: http://g6.mk
* (c) 2010
*****/

$(document).ready(function(){
	$("#sendMail").click(function(){
		if (
			validate("name","namereq") &&
			validate("email","emailreq") &&
			validate("message","msgreq")
			) {
			//send
			$("#loading").show();
			$.post("contactform", { name: $("#name").val(), email: $("#email").val(), message: $("#message").val(), submit:"true"} , function(data)
			{
				$("#loading").hide();
				var elem="#message"+data+"Sent";
				$(elem).fadeIn("fast",function(){
					setTimeout(function()
					{	 
						$(elem).fadeOut("slow");
					}, 5000);
				});
			});
			}			
	});
$("#sendShareMail").click(function(){
		if (
			validate("name","namereq") &&
			validate("email","emailreq") &&
			validate("fname","fnamereq") &&
			validate("femail","femailreq")
			) {
			//send
			$("#loading").show();
			$.post("sharemail", { name: $("#name").val(), email: $("#email").val(), oid: $("#oid").val(), message: $("#message").val(), fname: $("#fname").val(), femail: $("#femail").val(), submit:"true"} , function(data)
			{
				$("#loading").hide();
				var elem="#message"+data+"Sent";
				$(elem).fadeIn("fast",function(){
					setTimeout(function()
					{	 
						$(elem).fadeOut("slow");
					}, 5000);
				});
			});
			}	
	});
	$("#sendReservation").click(function(){
		//alert('a');
		if (
			validate("name","namereq") &&
			validate("date","datereq") &&
			validate("city","cityreq") &&
			validate("email","emailreq") &&
			validate("tel","telreq") &&
			validate("dfrom","datumreq") &&
			validate("dto","datumreq")
			) {
				$("#loading").show();
				$.post("reservationform", { name: $("#name").val(), dateb: $("#date").val(), city: $("#city").val(), email: $("#email").val(), telno: $("#tel").val(), zakolku: $("#how").val(), datefrom: $("#dfrom").val(), dateto: $("#dto").val(), message: $("#message").val(),  pid: $("#ponuda").val(), submit:"true"} , function(data)
				{
					//alert(data);
					$("#loading").hide();
					var elem="#message"+data+"Sent";
					$(elem).fadeIn("fast",function(){
						setTimeout(function()
						{	 
							$(elem).fadeOut("slow");
						}, 5000);
					});
				});
			}
	});
});

function validate(id,output)
{
	var flag=true;
	id="#"+id;
	output="#"+output;
	if($(id).val()=="" || $(id).val().length < 4)
	{
		flag=false;
		$(output).fadeIn("slow",function(){
			setTimeout(function()
			{	 
				$(output).fadeOut("slow");
			}, 5000);
		});
	}
	else if(id=="#email" || id=="#femail")
	{
		email=$(id).val();
		if(!emailIsValid(email))
		{
			flag=false;
			output=id+"invalid";
			outputOld=id+"req";
			$(output).fadeIn("slow",function(){
				setTimeout(function()
				{	 
					$(output).fadeOut("slow");
					$(outputOld).fadeOut("fast");
				}, 5000);
			});
		}
	}
	else if(id=="#tel")
	{
		telno=$(id).val();
		if(!telIsValid(telno))
		{
			flag=false;
			output=id+"invalid";
			outputOld=id+"req";
			$(output).fadeIn("slow",function(){
				setTimeout(function()
				{	 
					$(output).fadeOut("slow");
					$(outputOld).fadeOut("fast");
				}, 5000);
			});
		}
	}
	return flag;
}

function emailIsValid(email)  
{  
	var pattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
	return pattern.test(email);  
}  

function telIsValid(telno)
{
	var allowed = "0123456789-+/ ";
	var isValid = true;
	var Char;
	for (i = 0; i < telno.length && isValid == true; i++) 
	{ 
		Char = telno.charAt(i); 
		if (allowed.indexOf(Char) == -1) 
		{
			isValid = false;
		}
	}
	return isValid;
}
