var browser=navigator.userAgent.toLowerCase();
var isFF=((browser.indexOf('mozilla')!=-1) && (browser.indexOf('spoofer')==-1) && (browser.indexOf('compatible')==-1) && (browser.indexOf('opera')==-1) && (browser.indexOf('webtv')==-1) && (browser.indexOf('hotjava')==-1));
var isIE=((browser.indexOf("msie")!=-1) && (browser.indexOf("opera")==-1));
var isOP=(browser.indexOf("opera")!=-1);

function insert(insert, type, elem){
	if(type==1){
		if(isIE){
			el = document.getElementById(elem);
			el.focus();
			document.selection.createRange().text='['+insert+']'+document.selection.createRange().text+'[/'+insert+']';
		}else{
			el = document.getElementById(elem);
			var text=el;
			el.focus();
			text=text.value.substring(0, text.selectionStart)+'['+insert+']'+text.value.substring(text.selectionStart, text.selectionEnd)+'[/'+insert+']'+text.value.substring(text.selectionEnd, text.value.length);
			el.value=text;
		}
	}else{
		if(isIE){
			el = document.getElementById(elem);
			el.focus();
			document.selection.createRange().text='['+insert+']http://'+document.selection.createRange().text+'[/'+insert+']';
		}else{
			el = document.getElementById(elem);
			var text=el;
			el.focus();
			text=text.value.substring(0, text.selectionStart)+'['+insert+']http://'+text.value.substring(text.selectionStart, text.selectionEnd)+'[/'+insert+']'+text.value.substring(text.selectionEnd, text.value.length);
			el.value=text;
		}
	}
}

var topstiri = "";
var limita = 25;
var more = 0;
var showmore = 0;
var oldestid = 0;

function getStiriTop(lang) {
	$("#topstiriloading").show();
	$.getJSON("news.php?mod=top&lang="+lang,function(data){
		$("#stiridetop").empty();
		$.each(data.items, function(i,item){
			$("<li></li>").html("<li><a href='"+item.id+"'>"+item.titlu+"</a></li>").appendTo("#stiridetop");
		});
		$("#topstiriloading").hide();
	});
};
function getStiri(lang,sid) {
	$("#stiriloading").show();
	var mod = "";
	var lastid = "";
	var type = "";
	if(sid!=0){mod = "sid="+sid+"&";}
	if(more==1){lastid = "last="+oldestid+"&";}
	if(showmore==1){type = "type=more&";}
	$.getJSON("news.php?"+type+mod+lastid+"limit="+limita+"&lang="+lang,function(data){
		oldestid = data.items[data.items.length-1].id;
		$("#toatestirile").empty();
		$.each(data.items, function(i,item){
			stiritop = "";
			if(item.id != "0"){
				if(item.top=="1"){stiritop = "<img src='img/common/exclamation.gif'> ";}
				$("<li></li>").html("<table cellspacing='0' cellpadding='0'><tr><td valign='top' rowspan='2'><span class='timp'>"+item.ora+"</span></td><td valign='top'>"+stiritop+"<a href='?mod=news&id="+item.id+"' class='titlu_stire' id='a"+item.id+"' name='a"+item.id+"'>"+item.titlu+"</a></td></tr><tr><td valign='top'><a href='index.php?source="+item.sid+"' class='sursa'>"+item.sursa+"</a></td></tr></table>").appendTo("#toatestirile");
			} else {
				$("<li></li>").html("<div class='time'><span class='timeext'><span class='timeint'>"+item.titlu+"</span></span></div>").css("background", "none").appendTo("#toatestirile");
			}
		});
		$("a.anchorLink").attr("href","#a"+oldestid)
		$("#stiriloading").hide();
	});
	showmore = 0;
};
function getStiriMore(lang,sid) {
	$("#stiriloading").show();
	more = 1;
	showmore = 1;
	getStiri(lang,sid);
};

function valbaza(id, text){
	if (id.value == ""){
		id.value = text;
	}
}
function valnula(id, text){
	if (id.value == text){
		id.value = "";
	}
}

function formValidator(type){
	if(type=="upload"){
		titlu = document.getElementById('uploadtitlu');
		nume = document.getElementById('uploadname');
		city = document.getElementById('uploadcity');
		email = document.getElementById('uploademail');
		phone = document.getElementById('uploadphone');
		text = document.getElementById('uploadtext');
		
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

		if (isEmpty(titlu.value)){
			alert("Va rugam sa introduceti Titlul materialului.");
			titlu.focus();
			return (false);
		}
		if (isEmpty(nume.value)){
			alert("Va rugam sa introduceti Numele.");
			nume.focus();
			return (false);
		}
		if (isEmpty(city.value)){
			alert("Va rugam sa introduceti Orasul.");
			city.focus();
			return (false);
		}
		if (isEmpty(email.value)){
			alert("Va rugam sa introduceti Email-ul.");
			email.focus();
			return (false);
		} else if(reg.test(email.value) == false) {
			alert("Va rugam sa introduceti un Email valid.");
			return false;
		}
		if (isEmpty(phone.value)){
			alert("Va rugam sa introduceti Nr. de telefon.");
			phone.focus();
			return (false);
		}
		if (isEmpty(text.value)){
			alert("Va rugam sa introduceti Comentariul.");
			text.focus();
			return (false);
		}
	} else {
		nume = document.getElementById('commentname');
		comentariu = document.getElementById('commenttext');
		if (nume.value == ""){
			alert("Va rugam sa va introduceti numele.");
			nume.focus();
			return (false);
		}
		if (comentariu.value == ""){
			if(type==1){
				alert("Introduceti comentariul.");
			}else{
				alert("Introduceti intrebarea.");
			}
			comentariu.focus();
			return (false);
		}
	}
	return (true);
}

