/* functie care face scroll la imagini */
/* autor Tudor */
var sto;
function scroll_pics(a,no)
{
	div_pics = $('chenar_scroll_'+no);
	if(a==0) return;
	else if(a<0)
	{
		$('sf_next').src = "/images/nav_r_act.gif";		
		if(div_pics.scrollLeft<=0) {
			$('sf_next').src = "/images/nav_r_act.gif";
			$('sf_prev').src = "/images/nav_l.gif";			
			return;
		}
	}
	else if(a>0)
	{
		$('sf_prev').src = "/images/nav_l_act.gif";
		if(div_pics.scrollLeft>=(div_pics.scrollWidth-310)) {
			$('sf_next').src = "/images/nav_r.gif";			
			return;
		}
	}
	div_pics.scrollLeft = div_pics.scrollLeft+a;
	sto=setTimeout('scroll_pics('+a+','+no+')',30);
}
function stop_scroll_pics()
{
	clearTimeout(sto);
}

function test(){
}

/* functii pentru voting object*/
/* autor Tudor */

function show_full_stars(i,total,object_type,img1_name,img2_name)
{
	for(j=1;j<=i;j++)
	{
		curent="left";
		if(j%2==0) curent="right";
		$(object_type+"_"+j).src="/images/"+img1_name+"_"+curent+".jpg";
	}
	for(j=i+1;j<=total;j++)
	{
		curent="left";
		if(j%2==0) curent="right";
		$(object_type+"_"+j).src="/images/"+img2_name+"_"+curent+".jpg";
	}
}
function hide_full_stars(total,object_type,img2_name)
{
	for(j=1;j<=total;j++)
	{
		curent="left";
		if(j%2==0) curent="right";
		$(object_type+"_"+j).src="/images/"+img2_name+"_"+curent+".jpg";
	}
}
function vote_object(object_id,object_type,nota){
	var url="/?section=voting&screen=index&ajax=1&object_id="+object_id+"&object_type="+object_type+"&nota="+nota;
	new Ajax.Request(
		url,
		{
		onCreate: function() {$('votare').update("<div style='width:100px; font-size:12px; font-weight:bold'>Se incarca ...</div>");},
		onSuccess: function(resp) {$('votare').update(resp.responseText);}
		}
	);				
}

function update_medie(object_id,object_type){
	var url="/?section=voting&screen=update_medie&ajax=1&object_id="+object_id+"&object_type="+object_type;
	new Ajax.Request(
		url,
		{
		onCreate: function() {$('update_medie').update("<div style='width:320px; font-size:12px; font-weight:bold; display:inline'>Se incarca ...</div>");},
		onSuccess: function(resp) {$('update_medie').update(resp.responseText);}
		}
	);				
}

var quote = 0;

function add_quote(id)
{
	if (quote > 0) return;
	quote = id;
	document.forms['response'].elements['quote'].value = quote;
	$('raspunde_'+quote).src = '/images/forum/reply_q.gif';
}

function arata(id)
{
	$('split_'+id).show();
}

// meniu categorii
//schimba clapeta meniu
function arata_div(id,parinte) {
	$('menu_neselectat_'+div_activ).style.display="block";
	$('menu_selectat_'+div_activ).style.display="none";
	$('menu_neselectat_'+id).style.display="none";
	$('menu_selectat_'+id).style.display="block";
	div_activ=id;	
	var params = 'section=ajax&screen=lista_prod&parinte_id='+parinte;
	new Ajax.Updater('lista_prod','/index.php', { method:'get', parameters:params,evalScripts: true});
}

// schimba pagini
var categ=0;
var parinte=0;
function schimba_pagina(pagina) {
	var params = 'section=ajax&screen=lista_prod&pagina='+pagina+'&categorie_id='+categ+'&parinte_id='+parinte;
	new Ajax.Updater('lista_prod','/index.php', { method:'get', parameters:params,evalScripts: true});
}

function filtreaza_lista_prod(id) {
	var filtru=$('filtru'+id).value
	var params = 'section=ajax&screen=lista_prod&categorie_id='+categ+'&filtru='+filtru+'&parinte_id='+parinte;
	new Ajax.Updater('lista_prod','/index.php', { method:'get', parameters:params,evalScripts: true});
}


/* deschide un popup centrat neresizeble*/
function print_article(url,width,height)
{
	xpoz=Math.round((screen.width-width)/2);
	ypoz=Math.round((screen.height-height)/2);
	if (xpoz<0) xpoz=10;
	if (ypoz<0) ypoz=10;
	newWindow=window.open (url ,'mywindow','toolbar=no, scrollbars=yes, resizable=no ,width='+width+',height='+height+', left='+xpoz+', top='+ypoz);
	newWindow.focus();
}

function get_page_size()
{
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	
	return [pageWidth,pageHeight];
}

function casamea_alert(mesaj,type)
{
	if(type==undefined) type = 'succes';
	if($("alert_box")!=undefined) $("alert_box").parentNode.removeChild($("alert_box"));
	par = document.createElement("DIV");
	par.setAttribute("id","alert_box");
	par.className = "alert_parinte";
	par.style.display= "none";
	my_alert = document.createElement("DIV");
	my_alert.className = type;
	par.appendChild(my_alert);
	my_alert.appendChild(document.createTextNode(mesaj));
	scroll_y = document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop;
	scroll_x = document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft;
	position_y = scroll_y+(document.documentElement.clientHeight?document.documentElement.clientHeight:window.innerHeight)/2-55/2;
	position_x = scroll_x+(document.documentElement.clientWidth?document.documentElement.clientWidth:window.innerWidth)/2-390/2;
	par.style.top = position_y+"px";
	par.style.left = position_x+"px";
	document.body.appendChild(par);
	$('alert_box').appear({ duration: 0.4});
}

Event.observe(window.document,'mousedown',function(){
	if($("alert_box")!=undefined){
		$("alert_box").fade({ duration: 0.4});
	}
});
Event.observe(window.document,'keyup',function(e){
	if($("alert_box")!=undefined && (e.keyCode=="27" || e.keyCode=="13")){
		$("alert_box").fade({ duration: 0.4});
	}
});


function validate(str)
{
	var err = 0;
	var eml = 0;
	var str;
	var vector = str.split(",");
	for (i=0;i<vector.length;i++)
    {
        if (document.getElementById(vector[i]).value == "")
		{
            document.getElementById(vector[i]).style.backgroundColor = "#ffd8d8";
            document.getElementById(vector[i]).style.border = "1px solid #fe9f9f";
            err++;
        }
		else
		{
            document.getElementById(vector[i]).style.backgroundColor = "white";
            document.getElementById(vector[i]).style.border = "1px solid #cccccc";
			if (vector[i]=='email')
			{
				var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2,})?)$/i
				if (!filter.test(document.getElementById(vector[i]).value))
				{
					eml = 1;
					document.getElementById(vector[i]).style.backgroundColor = "#ffd8d8";
					document.getElementById(vector[i]).style.border = "1px solid #fe9f9f";
					err++;
				}
				else
				{
					eml = 0;
				}
			}
        }
    }
	if (err > 0)
	{
		var err_msg = "Va rugam sa completati toate campurile obligatorii \r\n";
		if (eml > 0)
		{
			err_msg += "\r\n Adresa de email introdusa nu este valida";
		}
		casamea_alert(err_msg, 'eroare');
		return false;
	}
	else
	{
		document.forms['response'].elements['semafor'].value = 1;
		return true;
	}
}


/* functie care schimba clapetele video */
/* autor Tudor */
function change_clapeta_video(id, page, excluded_id){
	new Ajax.Request('/index.php?section=articol&screen=cautare_video',
	{
		method:'post',
		parameters: {cat_video: id, page: page, text : $F('text_video'), excluded_id: excluded_id},			
		onSuccess: function(transport)
		{
			var response = transport.responseText;
			//alert(response);
			$('cautare_video').innerHTML = response;
			//alert($('cautare_video').innerHTML);
		}
	});				
}

var old_pic = '';
function img_hover(id)
{
	var str = old_pic = $(id).src;
	$(id).src = str.replace('gri_', '');
}
function img_out(id)
{
	$(id).src = old_pic;
}

function alow_search(val,nr){
	if(val.length<3) {
		casamea_alert('Cautarea trebuie sa contina cel putin 3 caractere!','avertizare');
		return false;	
	}
	if($('inp_cauta_'+nr).value.toLowerCase()=="cuvant cheie"){
		casamea_alert('Va rugam introduceti un cuvant cheie!','avertizare');
		return false;
	}
	return true;
}

//verifica valabilitatea unei date de tip dd/mm/yyyy
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
	for (i = 0; i < s.length; i++){   
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}

function stripCharsInBag(s, bag){
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++){   
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("Data trebuie sa fie in formatul : zz/ll/aaaa")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("introduceti o luna corecta")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Introduceti o zi corecta")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Introduceti un an corect intre "+minYear+" si "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Introduceti o data corecta")
		return false
	}
return true
}

//verifica daca adresa de e-mail este valida

/* forum */
function sticky(id)
{
	var pozitie = prompt('Ce pozitie va avea topicul ?');

	if ( (isNaN(pozitie)) || (pozitie == '') || (pozitie < 0) )
	{
		// nu este numar sau nu a introdus nimic ori numarul introdus este mai mic decat zero
		casamea_alert('Trebuie sa introduceti un numar mai mare decat zero. Mai incercati o data');
	}
	else
	{
		new Ajax.Request('/index.php?section=forum&screen=sticky&ajax=1&layout=ajax',
		{
			method:'post',
			parameters: {id: id, poz: pozitie},
			onSuccess: function(transport)
			{
				var response = transport.responseText;
				if (response == 1)
				{
					$('sticky_link').hide();
				}
			}
		});
	}
}

function spam(id)
{
	new Ajax.Request('/index.php?section=forum&screen=spam&ajax=1&layout=ajax',
	{
		method:'post',
		parameters: {id: id},
		onSuccess: function(transport)
		{
			var response = transport.responseText;
			if (response == 1)
			{
				//$('row_'+id).style.background = 'red';
				Effect.BlindUp('row_'+id);
			}
		}
	});
}

function validateEmail(email) 
{ 
 var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
 return email.match(re) 
}
