function isEmail(str) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return (filter.test(str) ? true : false);
}


function is_alpha(test, v) {

	if (v == "a") var lists = "abcdefghijklmnoqprstuvwxyzABCDEFGHIJKLMNOQPRSTUVWXYZ_-./ ";
	if (v == "b") var lists = "1234567890abcdefghijklmnoqprstuvwxyzABCDEFGHIJKLMNOQPRSTUVWXYZ_-!,.?()/ ";
	if (v == "c") var lists = "1234567890_-/.\\ ";
	if (v == "d") var lists = "1234567890";
	else var lists = "1234567890abcdefghijklmnoqprstuvwxyzABCDEFGHIJKLMNOQPRSTUVWXYZ_-./ ";

	var match = true;
	var found = false;
	var chars = "";

	for (var i = 0; i < test.length; i++) {
		chars = test.charAt( i );
		found = false;
		for (var j = 0; j < lists.length; j++) {
			if(lists.charAt( j ) == chars) {
				found = true;
				break;
			}
		}
		if (!found) {
			match = false;
			break;
		}
	}
	return match;
}

$(document).ready(function(){
	
	$('#email').blur(function(){verifica1(this)});
		function verifica1(t) {
		var vemaill = $(t).val();

		if (!isEmail(vemaill)) {
			$('.erremaill').remove();
			$('.utilizatorNou').after('<p class="error erremaill">- Adresa de email nu a fost completata corect, incearca din nou </p>');
			$(t).css({width: "203px", height: "18px", border: "1px solid #91020C"});
			$(t).focus(function() { $(t).css({width:"203px", height: "18px"}); });
			return false;
		} else {
				$('.erremaill').remove();
				$(t).css({width: "203px", height: "18px", border: "1px solid #A4FFA4"});
				$(t).focus(function() { $(t).css({width:"203px", height: "18px"}); });
				return true;
			}
		};

	$('#nume').blur(function(){verifica2(this)});
	function verifica2(t) {
		var vnume = $(t).val();
		
		if (vnume.replace(/[^0-9]/g,'').length > 0 || vnume.replace(/^\s+|\s$/g, '').length < 3 || vnume.replace(/^\s+|\s$/g, '').length > 50 || !is_alpha(vnume, 'a') || vnume.replace(/[^a-zA-Z]/g,'').length < 3 ||  vnume == 'Nume') {
			$('.errnume').remove();
			$('.utilizatorNou').after('<p class="error errnume">- Numele nu a fost completat corect</p>');
			$(t).css({width: "203px", height: "18px", border: "1px solid #91020C"});
			$(t).focus(function() { $(t).css({width:"203px", height: "18px"}); });
			return false;
		} else {
			$('.errnume').remove();
			$(t).css({width: "203px", height: "18px", border: "1px solid #A4FFA4"});
			$(t).focus(function() { $(t).css({width:"203px", height: "18px"}); });
			return true;
		}
	};
	
		$('#telefon').blur(function(){verifica4(this)});
	function verifica4(t) {
		var vtel = $(t).val();
		
		if (!is_alpha(vtel, 'd')) {
			$('.errtel').remove();
			$('.utilizatorNou').after('<p class="error errtel">- Nr. de telefon nu s-a completat corect</p>');
			$(t).css({width: "203px", height: "18px", border: "1px solid #91020C"});
			$(t).focus(function() { $(t).css({width:"203px", height: "18px"}); });
			return false;
		} else {
			$('.errtel').remove();
			$(t).css({width: "203px", height: "18px", border: "1px solid #A4FFA4"});
			$(t).focus(function() { $(t).css({width:"203px", height: "18px"}); });
			return true;
		}
	};
	$('#mesaj').blur(function(){verifica3(this)});
	function verifica3(t) {
		var vmesaj = $(t).val();
		
		if (vmesaj.replace(/[^0-9]/g,'').length > 0 || vmesaj.replace(/^\s+|\s$/g, '').length < 3) {
			$('.errmesaj').remove();
			$('.utilizatorNou').after('<p class="error errmesaj">- Mesajul nu poate fi gol, trebuie sa scrii ceva</p>');
			$(t).css({width: "204px", height: "129px", border: "1px solid #91020C"});
			$(t).focus(function() { $(t).css({width:"204px", height: "129px"}); });
			return false;
		} else {
			$('.errmesaj').remove();
			$(t).css({width: "204px", height: "129px", border: "1px solid #A4FFA4"});
			$(t).focus(function() { $(t).css({width:"204px", height: "129px"}); });
			return true;
		}
	};
	
	
	
	$('.utilizatorNou').parent().submit(function() {
			er = '';
			if(!verifica1('#email')) er = 'x';
			if(!verifica2('#nume')) er = 'x';
			if(!verifica4('#telefon')) er = 'x';
			if(!verifica3('#mesaj')) er = 'x';
			if (er == 'x') {
				scroll(0,10000);
				return false;	
			} else {
				return true;
			}
		});	
});

$(document).ready(function(){
	$('#next').click(function(){
		var vnext = $(this).attr('rel');
	
		if(vnext != ''){
			$.ajax({
					url: 'server.php',
					type: 'POST',
					dataType: 'html',
					data: 'id_port='+vnext+'&action=getnext',
					success: function(html) {
						if(html != '') {
							$('.portoftxt').empty().html(html);
						}
					}
				});
			$.ajax({
					url: 'server.php',
					type: 'POST',
					dataType: 'html',
					data: 'id_port='+vnext+'&action=getnexttitle',
					success: function(html) {
						if(html != '') {
							$('#title').empty().html(html);
						}
					}
				});
			$.ajax({
					url: 'server.php',
					type: 'POST',
					dataType: 'html',
					data: 'id_port='+vnext+'&action=getnextpoza',
					success: function(html) {
						if(html != '') {
							$('#show').empty().html(html);
						}
					}
				});
			$.ajax({
					url: 'server.php',
					type: 'POST',
					dataType: 'html',
					data: 'id_port='+vnext+'&action=getnextID',
					success: function(html) {
						if(html != '') {
							
						}
					}
				});
		}
		return false;		
	});
	$('#ant').click(function(){
		var vant = $(this).attr('rel');
		if(vant != ''){
			$.ajax({
					url: 'server.php',
					type: 'POST',
					dataType: 'html',
					data: 'id_port='+vant+'&action=getant',
					success: function(html) {
						if(html != '') {
							$('.portoftxt').empty().html(html);
						}
					}
				});
			$.ajax({
					url: 'server.php',
					type: 'POST',
					dataType: 'html',
					data: 'id_port='+vant+'&action=getanttitle',
					success: function(html) {
						if(html != '') {
							$('#title').empty().html(html);
						}
					}
				});
			$.ajax({
					url: 'server.php',
					type: 'POST',
					dataType: 'html',
					data: 'id_port='+vant+'&action=getantpoza',
					success: function(html) {
						if(html != '') {
							$('#show').empty().html(html);
						}
					}
				});
			$.ajax({
					url: 'server.php',
					type: 'POST',
					dataType: 'html',
					data: 'id_port='+vant+'&action=getantID',
					success: function(html) {
						if(html != '' || html == '') {
							
						}
					}
				});
		}
		return false;
	});
});


