
var vurl = window.top.location.toString();

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 in_array(needle,haystack) {
	return new RegExp('(^|\,)'+needle+'(\,|$)','gi').test(haystack);
}

$(document).ready(function() {
	
	$('#frmcauta').submit(function() {
		if($('#cauta_text').val() == '') {
			alert('Va rugam specificati un cuvant');
			return false;
		} else {
			return true;
		}
	});

	$('#cauta_text, #nume_newsletter, #email_newsletter').focus(function() {
		$(this).css({background: '#F5F5F5'});
		$(this).attr('rel', $(this).val());
		$(this).val('');
	});
	$('#cauta_text, #nume_newsletter, #email_newsletter').blur(function() {
		var val = $(this).val();
		var rel = $(this).attr('rel');
		$(this).css({background: ''});
		$(this).val((val == '' ? rel : (val != rel ? val : rel)));
	});
	
	function newsletter(ntip) {
		if(ntip != '') {
			var vnume = $('#nume_newsletter').val();
			var vemail = $('#email_newsletter').val();
			var vtip = $('input[@name=tip_newsletter]:checked').val();
			
			if(vnume == '' || !isEmail(vemail)) {
				alert('Va rugam completati corect campurile');
			} else if(ntip == 'abonare' && vtip == undefined) {
				alert('Va rugam alegeti tipul de newsletter pentru care sunteti interesat');
			} else {
				$.ajax({
					url: 'server.php',
					type: 'POST',
					dataType: 'html',
					data: 'nume='+vnume+'&email='+vemail+'&tip='+vtip+'&modalitate='+ntip+'&action=getNewsletter',
					success: function(html) {
						if(html != '') {
							alert(html);
						} else {
							if(ntip == 'abonare')  alert('Va multumim, ati fost adaugat cu succes in baza noastra de date');
							else alert('Va multumim, ati fost sters din baza noastra de date');
							
							$('#nume_newsletter').val('Nume');
							$('#email_newsletter').val('Email');
						}
					}
				});
			}
		}
	}

	
	$('#submitabon').click(function() {
		newsletter('abonare');
		return false;
	});
	
	$('#submitdezabon').click(function() {
		newsletter('dezabonare');
		return false;
	});
	
	if($('a.hasPic').size()>0) $('a.hasPic').lightBox();
	
	$('#contactform').submit(function() {
		var vnume = $('#nume').val();
		var vemail = $('#email').val();
		var vtelefon = $('#telefon').val();
		var vsubiect = $('#subiect').val();
		var vmesaj = $('#mesaj').val();
		
		if(vnume == '' || (vtelefon == '' && !isEmail(vemail)) || vsubiect == '' || vmesaj == '') {
			alert('Va rugam completati toate campurile');
		} else {
			return true;
		}
		
		return false;
	});
	

	$('#frmvideo').change(function() {
		vid = $('#fisier_video').val();

		if(vid == '') {
			alert('Va rugam alegeti un filmulet');
		} else {
			$.ajax({
				url: 'server.php',
				type: 'POST',
				dataType: 'html',
				data: 'id='+vid+'&action=getVideo',
				success: function(html) {
					if(html != '') {
						vpsp = html.split('~!~');
						var vnid = vpsp[0];
						var vnnume = vpsp[1];
						var vnautostart = vpsp[2];
						var vnimg = vpsp[3];
						
						var s1 = new SWFObject('images/mediaplayer.swf', 'mediaplayer', '340', '281', '7');
						s1.addParam('allowfullscreen', 'true');
						s1.addVariable('width', '340');
						s1.addVariable('height', '281');
						s1.addVariable('autostart', vnautostart);
						s1.addVariable('file', '../galerie/'+vnid+'/'+vnid+'.flv');
						s1.addVariable('image', 'images/img/img.php?src=../../galerie/'+vnid+'/'+vnimg);
						s1.write('video');
					}
				}
			});
		}
		
		return false;
	});

	$('#refresh').click(function() {
		 $('#mcptch').attr('src', $('#mcptch').attr('src')+'?'+(new Date()).getMilliseconds());
		return false;
	});
	
	
	$('#frmcomment').submit(function(){

		adComNume = $('#nume').val();
		adComEmail = $('#email').val();
		adComMesaj = $('#comentariu').val();
		adComCaptcha = $('#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');
				$('#mcptch').attr('src', $('#mcptch').attr('src')+'?'+(new Date()).getMilliseconds());
			} else {
				return true;
			}
		}
		return false;
	});
	
	$('.nota').change(function() {
		var id = $(this).attr('rel');
		var nota = $(this).val();
		
		if(nota == '') {
			alert('Va rugam alegeti o nota.');
		} else {
			window.location.href = $('base').attr('href')+'concurs/trimite_nota/'+id+'/'+nota+'.html';
		}
	})
	
});