function getOpinionAnswerForm(questionId) {
	
	$.ajax({
		url:'/front-productOpinion-answer.ltr',
		method:'post', 
		cache:false,
		data: {
			question_id: questionId
		},
		success: function(html) {
			$('#answerModeForm').html(html);
			$('#answerContent'+questionId).focus();
		}
	});
}

function toggleMeasureBox(id) {
	$('#votesNum' + id).show();
	$('#noVotes' + id).hide();
}
function updateCookie(opinionId) {
	var cookieValue = get_cookie('euroOpinionVote');
	var expireDate = new Date() ;
	expireDate.setTime((1000*60*60*24*100) + expireDate.getTime());
	if(cookieValue == '') {
		cookieValue += opinionId;
	}
	else {
		cookieValue += "|" + opinionId;
	}
	document.cookie = "euroOpinionVote="+cookieValue+"; expires="+expireDate.toGMTString()+"; path=/";
}
function addOpinionAttribute(type, value, number, identifier) {
	number=parseInt(number);
	$.ajax({
		url:'/add-opinion-attribute.ltr',
		method:'post',
		data: {
			type: type,
			value: value,
			number: number,
			identifier: identifier
		},
		success: function(html){
			reWhiteSpace = new RegExp(/^\s+$/);
			if(!reWhiteSpace.test(html)) {
				hideBadWordAlerts();
				$('#usersAttributes' + type).append(html);
				numOfAttributes = number + 1;
				$('#numOfAttr').val = numOfAttributes;
				$('#usersAttributes' + type).find('input').checkbox({cls:'jquery-safari-checkbox'});
				setTimeout("validOpinion('checkbox')",100);
			}
			else {
				hideBadWordAlerts();
				$('#badWordAlert' + type).show();
			}
		},
		error: function() {
		}
	});
}
function createOpinionAttribute(type, identifier) {
	var value = $('#attribute' + type).val();
	var numOfAttributes = $('#numOfAttr').val();
	if(value == '') {
		$.fancybox('<div class="w350"><div class="tcenter f13">Wypełnij pole</div><div class="buttonA mT10"><a href="javascript:void(0)" class="ok"></a></div></div>')
	}
	else {
		addOpinionAttribute(type, value, numOfAttributes, identifier);
		$('#attribute' + type).val('');
	}
}
function hideVotesFromCookie() {
	var cookieValue = get_cookie('euroOpinionVote');
	if(cookieValue != null)	{
		var voted = cookieValue.split('|');
		for(var i=0;i < voted.length;i++) {
			if(voted[i] != '' && document.getElementById('vote_yes_on_' + voted[i]) != null) {
				$('#vote_yes_on_' + voted[i]).hide();
				$('#vote_no_on_' + voted[i]).hide();
				$('#vote_separator_' + voted[i]).hide();
				$('#vote_question_' + voted[i]).hide();
				$('#vote_added_' + voted[i]).show();
			}
		}
	}
}

function hideAddOpinionButton(product) {
	var cookieValue = get_cookie('euroOpinions');
	if(cookieValue != null)	{
		var ids = cookieValue.split('|');
		for(var i=0;i < ids.length;i++) {
			if(ids[i] == product) {
				document.getElementById('addOpinionButton').style.display ='none';
			}
		}
	}
}
function voteHelpfulYes(opinionId, percentage) {
	FrontProductOpinionManager.updateHelpfulCount(opinionId);
	$('#helpful_quantity_' + opinionId).text(parseInt($('#helpful_quantity_' + opinionId).text()) + 1);
	$('#vote_yes_on_' + opinionId).hide();
	$('#vote_no_on_' + opinionId).hide();
	$('#vote_separator_' + opinionId).hide();
	$('#vote_question_' + opinionId).hide();
	$('#vote_yes_off_' + opinionId).show();
	$('#votesNum' + opinionId).hide();
	$('#votesNum' + opinionId + 'y').show();
	$('#noVotes' + opinionId).hide();
	updateCookie(opinionId);
}

function voteHelpfulNo(opinionId, percentage) {
	FrontProductOpinionManager.updateNotHelpfulCount(opinionId);
	var quantity = parseInt(document.getElementById('not_helpful_quantity_' + opinionId).innerHTML);
	
	$('#not_helpful_quantity_' + opinionId).text(quantity + 1);
	$('#vote_yes_on_' + opinionId).hide();
	$('#vote_no_on_' + opinionId).hide();
	$('#vote_separator_' + opinionId).hide();
	$('#vote_question_' + opinionId).hide();
	$('#vote_no_off_' + opinionId).show();
	$('#votesNum' + opinionId).hide();
	$('#votesNum' + opinionId + 'n').show();
	$('#noVotes' + opinionId).hide();
	updateCookie(opinionId);
}
function toggleOpinionContent(id, toHide, toShow) {
	$('#'+toHide + 'Opinion_' + id).hide();
	$('#'+toShow + 'Opinion_' + id).show();
}

function toggleQuestionBlock(opinion) {

	if(document.getElementById('opinionQuestion' + opinion).style.display == 'none') {
		document.getElementById('opinionQuestion' + opinion).style.display = 'block';
		document.location.hash = 'opinion' + opinion;
		$('#questionContent' + opinion).focus();
	}
	else {
		document.getElementById('opinionQuestion' + opinion).style.display = 'none';
	}
	document.getElementById('opinionQuestionAsked' + opinion).style.display = 'none';
}
function changeCriteria(pageNr) {

	var sortType = document.getElementById('sort-select').value;
	var itemsOnPage = document.getElementById('quantity-select').value;
	var newLink = window.location.protocol + "//" + window.location.host  + window.location.pathname + '?sort=' + sortType + '&on_page=' + itemsOnPage + '&page_nr=' + pageNr + "#opinions";
	document.location.href=newLink;
}
function checkEmail(email) {
	if (!validateEmail(email)) {
			alert('Proszę wpisać poprawny adres email.');
			return false;
	}
	return true;
}
function checkData(question, nick, email) {
	if(question == '' || nick == '' || email == '') {
		alert('Proszę uzupełnić wszystkie pola');
		return false;
	}
	return true;
}
function clearData(opinionId) {
	
	document.getElementById('questionContent' + opinionId).value = '';
	document.getElementById('questionNick' + opinionId).value = '';
	document.getElementById('questionEmail' + opinionId).value = '';
}
function askOpinionQuestion(question, nick, email, opinionId) {

	if(checkData(question, nick, email) && checkEmail(email)) {

		FrontProductOpinionManager.addProductOpinionQuestion(question, nick, email, opinionId, function(data) {
			if(data == false) {
				alert('Wpisany tekst nie jest poprawny');
			}
			else {
				clearData(opinionId);
				document.getElementById('opinionQuestion' + opinionId).style.display = 'none';
				document.getElementById('opinionQuestionAsked' + opinionId).style.display = 'block';
			}
		});
	}
}

function addOpinionAnswer(answer, questionId) {

	if(answer != '') {

		FrontProductOpinionManager.addProductOpinionAnswer(answer, questionId, function(data) {
			if(data == false) {
				alert('Wpisany tekst nie jest poprawny');
			}
			else {
				document.getElementById('opinionAnswer' + questionId).style.display = 'none';
				document.getElementById('opinionAnswerMessage' + questionId).style.display = 'block';
			}
		});
	}
	else {
		alert('Proszę uzupełnić pole');
	}
}


function showForm() {
	var hash = window.location.hash;
	if(hash == '#opinionForm') {
		$('#opinionFormAdd').show();
		$('#formButton').hide();
	}
}
function validForm(){
	$('#opinionForm').attr('valid','true');
	validOpinion('grade');
	validOpinion('name');
	validOpinion('email');
	validOpinion('text');
	validOpinion('checkbox');
	var i=0;
	$('.add_opinion').each(function(){
		if($(this).val()!='')i=$(this).attr('id').substr(9);
	});
	if(!$('.statusError').length)$('#opinionFormAdd').submit();
	else $.scrollTo($('.statusError:first'),500);
}
function startOpinion(grade){
	$('.opinion_box:last .vote_box').append('<div id="paginatorCopy">'+$('#paginator').html()+'</div>');
	showForm();
	$('#your-note a').hover(function(){
		var id=$(this).attr('id').substr(5);
		setActivStar(id);
	},function(){
		setActivStar($('#grade').val()/1);
	}).click(function(){
		var id=$(this).attr('id').substr(5);
		$('#grade').val(id);
		validOpinion('name');
	});
	
	
	$('#shortOpinion').example('Wpisz minimum dwa słowa');
	$('#opinion').example('Wpisz kilkanaście słów o produkcie');
	validateNewOpinion();
	setActivStar($('#grade').val());
	setInterval("setOpinionHelper();",333);
	$('#recomendedYes, #recomendedNo').click(function(){
		var tt=$(this).attr('title');
		$('#recomended').val(tt);
		validOpinion('text');
	});
	$('.checkboxTop input').click(function(){
		setTimeout("validOpinion('checkbox')",100);
	});
	if(grade>0){
		$('#grade').val(grade);
		setActivStar(grade);
	}
}
function validateNewOpinion(){
	$('#opinionForm').attr('valid','false');
	$('#nick, #grade').keyup(function(){
		validOpinion('name');
	});
	$('#email').keyup(function(){
		validOpinion('email');
	});
	$('#shortOpinion, #opinion, #recomended').keyup(function(){
		validOpinion('text');
	});	
	$('#checkboxTop input').focus(function(){
		validOpinion('checkbox');
	});
	
}
function validOpinion(id){
	if($('#opinionForm').attr('valid')=='true'){
		switch (id) {
			case 'email':
				var y1=0;
				var email=$('#email').val();
				if(email=='' || email.indexOf('@')==-1){
					y1=1;
				}else {
					if(!validateEmail(email))y1=2;
				}
				if(y1>0){
					$('#emailError').addClass('statusError');
					if(y1>0)$('#inputEmail').show();
					else $('#inputEmail').hide();
				}else {
					$('#emailError').removeClass('statusError');
					$('#emailError .error').hide();
				}
				
			break;
			case 'name':
				var y1=0,y2=0;
				if($('#nick').val()==''){
					y1=1;
				}
				var grade=$('#grade').val();
				if(grade=='' || grade==0){
					y2=1;
				}
				if(y1>0 || y2>0){
					$('#nameError').addClass('statusError');
					if(y1>0)$('#inputNick').show();
					else $('#inputNick').hide();
					
					if(y2>0)$('#inputGrade').show();
					else $('#inputGrade').hide();
				}else {
					$('#nameError').removeClass('statusError');
					$('#nameError .error').hide();
				}
			break
			case 'text':
				
				var tt=$('#shortOpinion').val();
				var y1=1;
				if(/\w+\s+\w+/.test(tt)){// || tt!=$('#shortOpinion').attr('title')
					y1=0;
				}
				if(tt==$('#shortOpinion').attr('title')){
					y1=1;			
				}
				var y2=0;
				var tt=$('#opinion').val().length;
				if(tt<50){
					y2=1;
				}
				y3=0;
				if($('#recomended').length){
					var tt=$('#recomended').val();
					var y3=1;
					if(tt=='yes' || tt=='no'){ //&& tt!=$('#opinion').attr('title')
						y3=0;
					}
				}
				
				var oo=0;
				if($('#fancybox-inner .statusErrorBox').length)oo=1;
				if(y1>0 || y2>0 || y3>0){
						
					$('#textError').addClass('statusError');
					if(y1>0)$('#inputShort').addClass('e').show();
					else $('#inputShort').removeClass('e').hide();
					
					if(y2>0)$('#inputText').addClass('e').show();
					else $('#inputText').removeClass('e').hide();
					
					if(y3>0)$('#inputRecomended').show();
					else $('#inputRecomended').hide();
					if(oo==0)$.fancybox.resize();
					
				}else {
					$('#textError').removeClass('statusError');
					$('#textError .error').hide();
					if(oo==1)$.fancybox.resize();
				}
			break;
			case 'checkbox':
				var y1=0,y2=0,y3=0;
				if($('.checkboxTop .plus input:checked, .checkboxTop .minus input:checked').length<2){
					y1=1
				}
				if($('.checkboxTop .uses input:checked').length==0){
					y2=1
				}
				if($('.checkboxTop .userTypes input:checked').length==0){
					y3=1
				}
				
				if(y1>0 || y2>0 || y3>0){
					$('#checkboxError').addClass('statusError');
					
					if(y1>0)$('#inputPM').show();
					else $('#inputPM').hide();
					
					if(y2>0)$('#inputUS').show();
					else $('#inputUS').hide();
					
					if(y3>0)$('#inputUT').show();
					else $('#inputUT').hide();
					
				}else {
					$('#checkboxError').removeClass('statusError');
					$('#checkboxError .error').hide();
				}
			break;
			default:
			break;
		}
	}
}
function setOpinionHelper(){
	var y=getPageScroll();
	var delta=($('#left-column').height()-$('#opinionHelper').height()-$('#opinionHelperM').height())+180;
	var odstep=0;
	if(y[1]>delta){
		odstep=y[1]-delta;
		if(odstep<0)odstep=0;
	}
	$('#opinionHelperM').css('height',odstep);
}
function setActivStar(id){
	$('#grades span').hide();
	if(id==0){
		$('#your-note a').removeClass('activ');
	}else{
		$('#your-note a:lt('+id+')').addClass('activ');
		$('#your-note a:gt('+(id-1)+')').removeClass('activ');
		$('#grades span:eq('+(id-1)+')').show();
	}
	validOpinion('grade');
}
