var commentsCaptchaRequested = false;

function openPopup (url, name, w, h) 
{
	newwin = window.open(url, name, 'width='+ w +',height='+ h +',top='+ (screen.height/2-h/2) +',left='+ (screen.width/2-w/2) +',toolbar=0,resizable=1,scrollbars=yes');
	if (window.focus) {newwin.focus()}
}


function enableCaptcha(id, state) {
        //if (state && !commentsCaptchaRequested && !getCookie('user_name') && document.getElementById('message'+ id).value.length > 0 && ('comentariu' != document.getElementById('message'+ id).value && 'Inainte de a scrie un comentariu, asigura-te ca ai completat in contul tau numele si adresa de mail' != document.getElementById('message'+ id).value)) {
		if (state && !commentsCaptchaRequested && !getCookie('user_name') && (document.getElementById('commentsDetails').value.length > 0)  && 'Inainte de a scrie un comentariu, asigura-te ca ai completat in contul tau numele si adresa de mail' != document.getElementById('commentsDetails').value) {
				document.getElementById("imgCaptcha"+ id).src = "/cms/images/captcha.jpg.php?" + Math.random();
                document.getElementById('divCaptcha'+ id).style.display = 'block';
                commentsCaptchaRequested = true;
        }
}

function enableRegisterCaptcha(id)
{
	document.getElementById(id).src = "/cms/images/captcha.jpg.php?" + Math.random();
}

function submitComments (e) {
	if (e.anonymous_author_name.value.search('^[ \t\r\n]*$')>-1) {
		alert('Nu ati completat campul Nume !'); 
		return false;
	}
	if (e.message.value.search('^[ \t\r\n]*$')>-1 || 'comentariu' == e.message.value) {
		alert('Nu ati completat campul Comentariu !'); 
		return false;
	}

	e.submit();
}


function afisarealfabet(frm, link) {
	
	var x=document.getElementById("frm");
	for (var i=0;i<x.length;i++)
	  {
		if (x.elements[i].value == 'nimic')
		{
		  window.location.href = link;
		} 	
	 
	  if (x.elements[i].value == 'data')
		{
		  window.location.href= link+'/data/';
		} 	
	    if (x.elements[i].value == 'alfabet') 
		{
		 
		 //window.location.href= link+'/toate/';
		 document.getElementById("alfabet").className="DisplayBlock";
		}

	  }
}


function afisareadropdown(frm, link) {
	var x = document.getElementById("frm");
	var v = document.getElementById("anul");
	for (var i=0;i<x.length;i++)
	  {
		if (x.elements[i].value == '-') 
		{
         window.location.href= link;
		 document.getElementById("alfabet").className="NoDisplay";
		 document.getElementById("anul").className="NoDisplay";
		 document.getElementById("gen").className="NoDisplay";
		 //document.getElementById("data").className="NoDisplay";
		}

	 	if (x.elements[i].value == 'alfabet') 
		{
         document.getElementById("alfabet").className="DisplayBlock";
		 document.getElementById("anul").className="NoDisplay";
		 document.getElementById("gen").className="NoDisplay";
		
		}
	     if (x.elements[i].value == 'anul') 
		{
		 
		 document.getElementById("alfabet").className="NoDisplay";
		 document.getElementById("anul").className="Display";
		 document.getElementById("gen").className="NoDisplay";
		 
		}
		 if (x.elements[i].value == 'gen') 
		{
		  document.getElementById("alfabet").className="NoDisplay";
		  document.getElementById("gen").className="Display";
		  document.getElementById("anul").className="NoDisplay";
		 
		}
		if (x.elements[i].value == 'data') 
		{
			window.location.href= link+'/data/';
		 }
	  }
}

function listfilmanul() {
	var x=document.getElementById("anul");
    
	if (x.options[x.selectedIndex].value != '-')
	{
		 window.location.href='/filme/' + x.options[x.selectedIndex].value + '/';
	}
}

function listfilmgen() {
	var x=document.getElementById("gen");
    
	if (x.options[x.selectedIndex].value != '-')
	{
		 window.location.href='/filme/gen-' + x.options[x.selectedIndex].value + '/';
	}
}

function login() {
	$.ajax({
		type: "POST",
		url: "/login",
		data: "username=" + $('#user').val() + "&password=" + $('#pass').val(),
		async: false,
		success: function(msg){
	   	if (msg == 'ok') 
		{
			window.location.reload( true );
		} else 
		{
			document.getElementById('error_msg').innerHTML = "Logarea nu s-a putut realiza.";
		}
	}
	});
	return false;
}

function refreshPageSetImageWidth (r) {
	var calcwidth=0;
	
	var elem;
	elem = document.getElementById('scale'); 
	//alert(elem);
	calcwidth = Math.ceil(r*100/5);
	//alert (calcwidth);
	elem.style.width = calcwidth + "%";
	//alert (elem);
	//alert ("sfarsit de functie");
}

function pollVote(box_id, poll_id) {
	
	xmlAsyncRequestText ('/ajax?'+box_id+'&submit=1&itemID='+poll_id+'&service_type=PollContest'
		+'&rnd='+Math.random()
		, Form_MakeQueryString('fp'+poll_id+'_'+box_id)
		, 'dinamicContent'+box_id+'_'+poll_id);
	return false;
}
function pollShowResults(box_id, poll_id) {
	xmlAsyncRequestText ('/ajax?'+box_id+'&submit=1&itemID='+poll_id+'&service_type=PollContest&output=results'
		+'&rnd='+Math.random()
		, ''
		, 'dinamicContent'+box_id+'_'+poll_id);
	return false;
}

function generateRandomNumber(total) {
	return Math.ceil(Math.random()*total);
}

function rezervaBilet(id){
	var newWin;
	newWin = window.open("http://www.cinemapro.ro/tickets/?siteId=13&showId="+id,"CPL","width=870,height=670,scrollbars=1,menubar=no,resizable=no,top=" + ( screen.width - 770 ) / 2 + ",left=" + ( screen.height - 570 ) / 2 + ",status=no");
	newWin.focus();
}

function recomanda(text){
	var newWin;
	newWin = window.open("/recomanda?item="+text,"recomanda","width=400,height=400,scrollbars=1,menubar=no,resizable=no,top=" + ( screen.width - 770 ) / 2 + ",left=" + ( screen.height - 570 ) / 2 + ",status=no");
	newWin.focus();
}

function renderUser() {
	//alert(getCookie("username"));
	if (getCookie("username") == "" || getCookie("username") == null ) 
	{
		if (document.getElementById("logedout"))
		{
			document.getElementById('logedout').style.display = 'block';
		}
	} else 
	{
		if (document.getElementById("logedin")) 
		{
			document.getElementById('loged_user').innerHTML = getCookie("username").replace('+','&nbsp;');
			document.getElementById('logedin').style.display = 'block';
		}
	}
}

function readCookie(name) {
	
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return '';
	
}

function showBanners()
{
	try
	{
		var left_0 = document.getElementById("left_0");
		var left_1 = document.getElementById("left_1");
		
		var right_0 = document.getElementById("right_0");
		var right_1 = document.getElementById("right_1");

		var width_left = 0;
		var width_right = 0;

		if (screen.width>=1280)
		{
			if(left_0.src.search(/1024/)>=0) 
			{
				left_0.className = "banner_left_show";
			//	width_left = left_0.offsetWidth;
			}
			else
			{
				if(left_1.src.search(/1024/)>=0) 
				{
					left_1.className = "banner_left_show";
				//	width_left = left_1.offsetWidth;
				}	
			}
				
			if(right_0.src.search(/1024/)>=0) 
			{
				right_0.className = "banner_right_show";
			//	width_right = right_0.offsetWidth;
			}
			else
			{
				if(right_1.src.search(/1024/)>=0) 
				{
					right_1.className = "banner_right_show";
			//		width_right = right_1.offsetWidth;
				}
			}
		}
		else
		{
			if(left_0.src.search(/768/)>=0) 
			{
				left_0.className = "banner_left_show";
		//		width_left = left_0.offsetWidth;
			}
			else
			{
				if(left_1.src.search(/768/)>=0) 
				{
					left_1.className = "banner_left_show";
			//		width_left = left_1.offsetWidth;
				}	
			}
				
			if(right_0.src.search(/768/)>=0) 
			{
				right_0.className = "banner_right_show";
			//	width_right = right_1.offsetWidth;
			}
			else
			{
				if(right_1.src.search(/768/)>=0) 
				{
					right_1.className = "banner_right_show";
				//	width_right = right_1.offsetWidth;
				}
			}
		}
		
/*		if(width_left > 0 &&  width_right > 0)
		{
			var total_width = width_left+900+width_right+6+'px';
			document.getElementById("siteAndBanners").style.width = total_width;
			document.getElementById("bannerLeft").style.width = width_left+'px';
			document.getElementById("bannerRight").style.width = width_right+'px';
		}
		else
		{
			
			document.getElementById("BigAllContainer").className = "";
		} */
	}
	catch (err)
	{
	//	document.getElementById("BigAllContainer").className = "";
	}
}
function submitSearch () 
{
	var choosed_option = $('#okidoki input:checked').val();
	var submit_search_url = 'http://okidoki.ro/search?query=';
	var keywords = $('input[name=q]').val();
	if(keywords != '')
	{
		keywords = keywords.replace(/ /g, "+");
		if(choosed_option == "cinemapro"){
			window.location = "http://www.cinemapro.ro/search?q=" + keywords;
		}
		if(choosed_option == "okidoki"){
			submit_search_url = submit_search_url + keywords;
			window.location = submit_search_url;
		}
	}
	else
	{
		alert("Cautare invalida!");
	}
	return false;
}

$(document).ready(function() {
	if ($('#commentsForm').length > 0)
	{
		$('#commentsForm').nextComments();
	}
});