function is_numeric(sText){
	var ValidChars = "0123456789";
	var IsNumber = true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++){
		 Char = sText.charAt(i);
		 if (ValidChars.indexOf(Char) == -1){
				IsNumber = false;
			}
		}
	return IsNumber;
}
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);
};


wait2 = null;
total2 = 1;
current_promo2 = 1;

function showPromo2(promo2, h1) {
	var vh1 = h1;
	if(vh1.indexOf('~') > 1) {
		clearTimeout(wait2);
		current_promo2 = promo2;
		
		vh1s = vh1.split('~');
		
		$('#topH').fadeOut();
		$('#topH').empty().html(vh1s[promo2-1]).fadeIn();
		
		wait2 = setTimeout("nextPromo2('"+h1+"')", 4000);
	}
}

function nextPromo2(h1) {
	vh1s = h1.split('~');
	total2 = vh1s.length;
	if(current_promo2 < total2) showPromo2(parseInt(current_promo2)+1, h1);
	else showPromo2(1,h1);
}


$(document).ready(function(){
	$('body').after('<div id="h1elm">'+$('#topH').html()+'</div>');
	var h1 = $('#h1elm').html();
	$('#topH').show();
	showPromo2(1, h1);
	
	$('form.adCom').submit(function(){

		adComNume = jQuery.trim($('input[name=adComNume]').val());
		adComEmail = jQuery.trim($('input[name=adComEmail]').val());
		adComMesaj = jQuery.trim($('textarea[name=adComMesaj]').val());
		adComCaptcha = jQuery.trim($('input[name=captcha]').val());
		
		
		if(adComNume == '') alert("Va rugam introduceti numele dumneavoastra");
		else if(adComNume.length > 50) alert("Numele nu poate fi mai lung de 50 de caractere");
		else if(adComEmail == '') alert("Va rugam introduceti adresa dumneavoastra de email");
		else if(!isEmail(adComEmail)) alert("Va rugam introduceti o adresa valida de email");
		else if(adComMesaj == '') alert("Va rugam introduceti mesajul dumneavoastra");
		else if(adComMesaj.length < 10) alert("Mesajul dumneavoastra trebuie sa contina cel putin 10 caractere");
		else if(adComCaptcha.length < 4 || adComCaptcha.length > 6) alert("Completati corect codul de securitate");
		else {
			var html = $.ajax({
			type: 'POST',
			url: 'server.php',
			data: 'cod='+$('#captcha').val()+'&action=getCaptcha',
			async: false
			}).responseText;
			
			if(html == '') {
				alert("Completati corect codul de securitate");
				document.getElementById('mcptch').src = document.getElementById('mcptch').src + '?' + (new Date()).getMilliseconds();
			} else {
				return true;
			}
		}
		return false;
	});
	
	$('#cainpt').focus(function() {
		$(this).attr('rel', $(this).val());
		$(this).val('');
	});
	$('#cainpt').blur(function() {
		var val = $(this).val();
		var rel = $(this).attr('rel');
		$(this).val((val == '' ? rel : (val != rel ? val : rel)));
	});
});

