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

function check_add(){
	var z = $('#add_zona'); var zz = jQuery.trim(z.val()); z.val(zz);
	if(zz.length < 1){
		alert('Completeaza campul Zona');
		z.focus();
		return false;
	}
	
	var pm = $('#add_pret_maxim'); var pmpm = jQuery.trim(pm.val()); 
	if(pmpm.length > 0){
		pmpm = parseInt(pmpm);
		if(isNaN(pmpm)){
			pmpm = '';
			pm.val(pmpm);
			alert('Completeaza campul Pret maxim');
			return false;
		}
		else{
			pm.val(pmpm);
			if(pmpm == 0){
				alert('Completeaza campul Pret maxim cu o valoare corecta.');
				pm.focus();
				return false;
			}
		}
	}
	else{
		alert('Completeaza campul Pret maxim');
		pm.focus();
		return false;
	}

	var t1 =$('#add_telefon1'); t1t1 = jQuery.trim(t1.val()); if(t1t1 == 'telefon') t1t1 = ''; t1.val(t1t1);
	var t2 =$('#add_telefon2'); t2t2 = jQuery.trim(t2.val()); if(t2t2 == 'telefon') t2t2 = ''; t2.val(t2t2);
	var t3 =$('#add_telefon3'); t3t3 = jQuery.trim(t3.val()); if(t3t3 == 'telefon') t3t3 = ''; t3.val(t3t3);
	
	var em =$('#add_email'); emem = jQuery.trim(em.val()); if(emem == 'email') emem = ''; em.val(emem);
	
	if((t1t1.length < 1) && (t2t2.length < 1) && (t3t3.length < 1)){
		if(emem.length < 1){
			alert('Scrie macar un numar de telefon sau o adresa de e-mail pentru contact');
			return false;
		}
	}
	if(emem.length > 0){
		if(!validateEmail(emem)){
			alert('Adresa de e-mail este incorecta');
			return false;
		}
	}
	
	var v = $('#add_valabil'); var vv = jQuery.trim(v.val()); v.val(vv);
	if(vv.length < 1){
		alert('Scrie data pana cand e valabil anuntul'); 
		return false;
	}
	
	return true;
}

function check_edit(){
	var z = $('#add_zona'); var zz = jQuery.trim(z.val()); z.val(zz);
	if(zz.length < 1){
		alert('Completeaza campul Zona');
		z.focus();
		return false;
	}
	
	var pm = $('#add_pret_maxim'); var pmpm = jQuery.trim(pm.val()); 
	if(pmpm.length > 0){
		pmpm = parseInt(pmpm);
		if(isNaN(pmpm)){
			pmpm = '';
			pm.val(pmpm);
			alert('Completeaza campul Pret maxim');
			return false;
		}
		else{
			pm.val(pmpm);
			if(pmpm == 0){
				alert('Completeaza campul Pret maxim cu o valoare corecta.');
				pm.focus();
				return false;
			}
		}
	}
	else{
		alert('Completeaza campul Pret maxim');
		pm.focus();
		return false;
	}

	var t1 =$('#add_telefon1'); t1t1 = jQuery.trim(t1.val()); if(t1t1 == 'telefon') t1t1 = ''; t1.val(t1t1);
	var t2 =$('#add_telefon2'); t2t2 = jQuery.trim(t2.val()); if(t2t2 == 'telefon') t2t2 = ''; t2.val(t2t2);
	var t3 =$('#add_telefon3'); t3t3 = jQuery.trim(t3.val()); if(t3t3 == 'telefon') t3t3 = ''; t3.val(t3t3);
	
	var em =$('#add_email'); emem = jQuery.trim(em.val()); if(emem == 'email') emem = ''; em.val(emem);
	
	if((t1t1.length < 1) && (t2t2.length < 1) && (t3t3.length < 1)){
		if(emem.length < 1){
			alert('Scrie macar un numar de telefon sau o adresa de e-mail pentru contact');
			return false;
		}
	}
	if(emem.length > 0){
		if(!validateEmail(emem)){
			alert('Adresa de e-mail este incorecta');
			return false;
		}
	}
	
	var v = $('#add_valabil'); var vv = jQuery.trim(v.val()); v.val(vv);
	if(vv.length < 1){
		alert('Scrie data pana cand e valabil anuntul'); 
		return false;
	}

	
	var newimg = $('#add_file').val();
	if(newimg.length >= 1){
		//check extension
		var extens = newimg.split(".");
		var nr_extens = extens.length;
		if(nr_extens > 1){
			var ext = extens[nr_extens-1];
			var pat = /(jpg|gif)+/i;
			if(!pat.test(ext)){
				alert('Selecteaza o imagine de tip jpg sau gif');
				return false;
			}
		}
		else{
			alert('Selecteaza o imagine de tip jpg sau gif');
			return false;
		}
	}
	
	var newimg2 = $('#add_file2').val();
	if(newimg2.length >= 1){
		//check extension
		var extens = newimg2.split(".");
		var nr_extens = extens.length;
		if(nr_extens > 1){
			var ext = extens[nr_extens-1];
			var pat = /(jpg|gif)+/i;
			if(!pat.test(ext)){
				alert('Selecteaza o imagine de tip jpg sau gif pentru a doua imagine');
				return false;
			}
		}
		else{
			alert('Selecteaza o imagine de tip jpg sau gif pentru a doua imagine');
			return false;
		}
	}
	
	var newimg3 = $('#add_file3').val();
	if(newimg3.length >= 1){
		//check extension
		var extens = newimg3.split(".");
		var nr_extens = extens.length;
		if(nr_extens > 1){
			var ext = extens[nr_extens-1];
			var pat = /(jpg|gif)+/i;
			if(!pat.test(ext)){
				alert('Selecteaza o imagine de tip jpg sau gif pentru a doua imagine');
				return false;
			}
		}
		else{
			alert('Selecteaza o imagine de tip jpg sau gif pentru a doua imagine');
			return false;
		}
	}
	
	return true;

}

function open_window(myurl){
	params  = 'width=800';
	params += ', height=800';
	params += ', top='+(screen.height/2 -400)+', left='+(screen.width/2 - 400)
	newwindow=window.open(myurl,'Localizare',params+',menubar=no,resizable=no,toolbar=no,location=no,titlebar=no,directories=no');
	if (window.focus) {newwindow.focus()}
}
function friendly_url(str){
	var nr = str.length;
	var new_str = '';
	if(nr > 0){
		for(i=0;i<nr;i++){
			
			c = str[i].replace(/^\s+|\s+$/g, " ")
                        .replace(/[_|\s]+/g, " ")
                        .replace(/[^A-Za-z\u0400-\u04FF0-9-]+/g, " ")
                        .replace(/[-]+/g, " ")
                        .replace(/^-+|-+$/g, " ")
                        .replace(/[-]+/g, " ")
						
			new_str +=c;
		}
	}
	return new_str;
}

function create_search(){
	var tip_anunt = $('#tip_anunt').val();
	var tip_imobil = $('#tip_imobil').val();
	var z = $('#zona'); var zz = jQuery.trim(z.val()); z.val(zz);
	var pm = $('#pret_maxim'); var pmpm = jQuery.trim(pm.val()); pm.val(pmpm);
	var valuta = $('#valuta').val();
	
	new_url = 'anunturi-'+tip_anunt;
	if(tip_imobil == '') tip_imobil = 'orice_imobil';
	new_url += '-'+tip_imobil;
	if(zz == '') zz = 'orice zona';
	new_url += '-'+friendly_url(zz);
	if(pmpm == '') pmpm = 'orice_suma';
	new_url += '-'+pmpm;
	if(valuta == '') valuta = 'orice_valuta';
	new_url += '-'+valuta;
	new_url += '.html';
	
	jQuery.ajax({
		type: "POST",
		url: "statistici_cautare2.php",
		data: "tip="+tip_anunt+"&zona="+zz+"&tip_imobil="+tip_imobil+"&pm="+pmpm+"&v="+valuta,
		success: function(msg){
				jQuery.ajax({
					type: "POST",
					url: "set_session.php",
					data: "search="+tip_anunt+"*|*"+tip_imobil+"*|*"+zz+"*|*"+pmpm+"*|*"+valuta,
					success: function(d){
						if(d=='1')				window.location = new_url;

					}
				});
				//window.location = new_url;
		}
	});
	
	//$.post("statistici_cautare2.php", { tip: tip_anunt, zona: zz, nume: tip_imobil, pm: pmpm, v: valuta }, function(data){  } );
}

var chart;
$(document).ready(function() {
	if($('#statistici').length){
				// define the options
				var options = {
					chart: {
						renderTo: 'statistici',
						defaultSeriesType: 'column'
					},
					title: {
						text: 'Variatia anunturilor de inchirieri'
					},
					subtitle: {
						text: 'statistici 2010'
					},
					xAxis: {
						categories: [
							'Ian', 
							'Feb', 
							'Mar', 
							'Apr',
							'Mai',
							'Iun',
							'Iul',
							'Aug',
							'Sep',
							'Oct',
							'Nov',
							'Dec'
						]
					},
					yAxis: {
						min: 0,
						title: {
							text: 'Anunturi (ofer)'
						}
					},
					legend: {
						layout: 'vertical',
						backgroundColor: '#FFFFFF',
						align: 'left',
						verticalAlign: 'top',
						x: 600,
						y: 20
					},
					tooltip: {
						formatter: function() {
							return ''+
								this.x +': '+ this.y +' anunturi';
						}
					},
					plotOptions: {
						column: {
							pointPadding: 0.1,
							borderWidth: 0
						}
					},
				    series: []
				}
				
				jQuery.getJSON('get_statistici_lunare.php', function(data) {
					 $.each(data.items, function(i,item){
						options.series.push({
							name: item.nume,
							data: item.valori
						});
					  });
					  
					  
				
					chart = new Highcharts.Chart(options);
				});
}			
});
	
				

