$(document).ready(function() {
$('.atentie').remove();
$.get("/php/token.php",function(txt){
  $(".mail").append('<input type="hidden" name="ts" value="'+txt+'" />');
});	

	required = ["nume","prenume","subiect", "email", "mesajT", "adresa", "oras", "telefon", "judet", "numeL", "prenumeL", "judetL", "orasL", "adresaL"];
	// If using an ID other than #email or #error then replace it here
	email = $("#email");
	// The text to show up within a field when it is incorrect
	emptyerror = "Camp necesar";
	emailerror = "Camp necesar.";

	$("#forma").submit(function(){	
		//Validate required fields
		for (i=0;i<required.length-5;i++) {
			var input = $('#'+required[i]);

			//
			if ((input.val() == "") || (input.val() == emptyerror)) {
				input.addClass("needsfilled");
				input.val(emptyerror);
			} else {
				input.removeClass("needsfilled");
			}
		}

		if (!($("#checkme").is(":checked"))){
		for (i=required.length-5;i<required.length;i++) {
			var input = $('#'+required[i]);
			if ((input.val() == "") || (input.val() == emptyerror)) {
				input.addClass("needsfilled");
				input.val(emptyerror);
			} else {
				input.removeClass("needsfilled");
			}
		}}
		else{
		$('#numeL').val($('#nume').val());
		$('#prenumeL').val($('#prenume').val());
		$('#adresaL').val($('#adresa').val());
		$('#orasL').val($('#oras').val());
		$('#judetL').val($('#judet').val());
		$('#codPL').val($('#codP').val());			
		}


		// Validate the e-mail.
		if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
			email.addClass("needsfilled");
			email.val(emailerror);
		}

		//if any inputs on the page have the class 'needsfilled' the form will not submit
		if ($(":input").hasClass("needsfilled")) {
			return false;
		} else {
	    var plata=$('input:radio[name=plata]:checked').val();
	    if(plata!="card"){
		    $.ajax({ // create an AJAX call...
			data: $(this).serialize(), // get the form data
	        	type: $(this).attr('method'), // GET or POST
	        	url: $(this).attr('action'), // the file to call
	        	success: function(response) { // on success..
	            	    $('#continut').html(response); // update the DIV
     			   }
		    });
		    return false; 
		    }
		    return true;
		}
	});
	
	$("#formaq").submit(function(){	
		//Validate required fields
		for (i=0;i<required.length-5;i++) {
			var input = $('#'+required[i]);

			//
			if ((input.val() == "") || (input.val() == emptyerror)) {
				input.addClass("needsfilled");
				input.val(emptyerror);
			} else {
				input.removeClass("needsfilled");
			}
		}

		// Validate the e-mail.
		if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
			email.addClass("needsfilled");
			email.val(emailerror);
		}

		//if any inputs on the page have the class 'needsfilled' the form will not submit
		if ($(":input").hasClass("needsfilled")) {
			return false;
		} else {
			$.post('php/contact.php', $('#formaq').serialize(), function (data, textStatus) {
        		$('#created').empty();
        		$('#created').append(data);
  			  });
    return false;
			return true;
		}
	});
	
	$("#forma1").submit(function(){	
		//Validate required fields
		for (i=0;i<required.length-5;i++) {
			var input = $('#'+required[i]);

			//
			if ((input.val() == "") || (input.val() == emptyerror)) {
				input.addClass("needsfilled");
				input.val(emptyerror);
			} else {
				input.removeClass("needsfilled");
			}
		}

		// Validate the e-mail.
		if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
			email.addClass("needsfilled");
			email.val(emailerror);
		}

		//if any inputs on the page have the class 'needsfilled' the form will not submit
		if ($(":input").hasClass("needsfilled")) {
			return false;
		} else {
			$.post('php/news.php', $('#forma1').serialize(), function (data, textStatus) {
        		$('#created1').empty();
        		$('#created1').append(data);
  			  });
    return false;
			return true;
		}
	});
	
	// Clears any fields in the form when the user clicks on them
	$(":input").focus(function(){		
	   if ($(this).hasClass("needsfilled") ) {
			$(this).val("");
			$(this).removeClass("needsfilled");
	   }
	});

	$(":input").focus(function(){		
	   if ($(this).hasClass("start") ) {
			$(this).val("");
			$(this).removeClass("start");
	   }
	});

});

