var idContentTheBest=0;
var idDivContent=0;

function initCaptcha(){
	$("#errorMessage").css("display","none");
	$("input[name=prova]").val("");
	initDataTheBest();
	getImageCaptcha();
	ceckField();
}

function initCaptcha2(){
	$("input[name=prova]").val("");
	initDataNews();
	getImageCaptcha();
	ceckField();
}

function initCaptcha3(){
	$("input[name=prova]").val("");
	initDataVideo();
	getImageCaptcha();
	ceckField();
}


function initDataTheBest(){
	arrayTheBest=dataForLightBox.split(",");
	idContentTheBest=arrayTheBest[0];
	idDivContent=arrayTheBest[1];
	var titleToVoteTheBest=$("#titleTheBest"+idDivContent).html()
	$("#ContentToVote").html(titleToVoteTheBest)
	$("#errorMessage").html("")

}

function initDataNews(){
	arrayNews=dataForLightBox.split(",");
	idContentTheBest=arrayNews[0];
	idDivContent=arrayNews[1];
	var titleToVoteTheBest=$("#newsTitle"+idDivContent).html()
	$("#ContentToVote").html(titleToVoteTheBest)
	$("#errorMessage").html("")
}

function initDataVideo(){
	arrayNews=dataForLightBox.split(",");
	idContentTheBest=arrayNews[0];
	idDivContent=arrayNews[1];
	var titleToVoteTheBest=$("#nickNameDiv").html()
	$("#ContentToVote").html(titleToVoteTheBest)
	$("#errorMessage").html("")
}

function getImageCaptcha(){
	var rand = Math.floor(Math.random()*100000000);
	var src = cptch+"captcha.php?r="+rand;
    $("#imageCaptcha").attr("src", src);
    $("input[name=prova]").val("");
}


function sendCaptcha(){
	var valueInsered=$("input[name=prova]").val();
	if(valueInsered==""){
	} else{
		sendCaptchaValue(valueInsered)
	}
}


function ceckField(){
	var valueOfField=$("input[name=prova]").val();
	var len=valueOfField.length
	if(len>0){
		$("#voteCaptchaButton").css({'opacity' : '1', 'filter' : 'alpha(opacity=100)'});

	} else{
		$("#voteCaptchaButton").css({'opacity' : '0.4', 'filter' : 'alpha(opacity=40)'});
	}
}


function sendCaptchaValue(valueCaptcha)
{	$("#errorMessage").css("display","none");
	var rand = Math.floor(Math.random()*100000000);
	var myPathSendCaptcha=pathData+"vote/?cth="+valueCaptcha+"&cpid="+idContentTheBest+"&r="+rand;
		$.ajax({
		  url: myPathSendCaptcha,
		  cache: false,
		  error:function (){
		  },
		  success: function(html){
		  },
		  complete: function(html){
				if (html.status == 200) {
					if (html.responseText.length > 0) {
						ceckAnswer(html.responseText)
					}
				}
		  }
		});
}


function ceckAnswer(errorCode){
	if(errorCode==0){
		$("#errorMessage").css("display","block");
		$("#errorMessage").text("Hai gia' espresso la tua preferenza.");
		$("input[name=prova]").val("");
		getImageCaptcha();

	}else if(errorCode==1){
		chiudiLightBox();
		apriLightBox("votook");

	} else if(errorCode==-1){
		$("#errorMessage").css("display","block");
		$("#errorMessage").text("Codice di sicurezza errato!!!");
		$("input[name=prova]").val("");
		getImageCaptcha();
	}
}