//SURVEY VOTES
function vote_survey(question_id, site_id) {
	var my_form = document.getElementById('survey_form_'+question_id);
	var radioObj = my_form.answers;
    var answer = 0;
   	var radioLength = radioObj.length;
   	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			answer = radioObj[i].value;
		}
	}
	if(answer == 0){
		alert(mess_err_check);
	}
	else{
		$.getJSON("json/vote_survey.php",{answer_id:answer, pass_site_id:site_id}, print_vote_result);
	}
}

function print_vote_result(vote){
	if(vote.result == 1){
		alert(mess_thx_vote);
	}
	else if(vote.result == 2){
		alert(mess_already_vote);
	}
	else{
		alert(mess_err_vote);
	}

	document.getElementById('show_questions_'+vote.question_id).style.display = 'none';
	document.getElementById('show_votes_'+vote.question_id).style.display = 'block';
	$("#nb_votes_"+vote.question_id).html(vote.nb_votes);

}

//RATE CLIP, MOVIE, ACTOR
function rate_update(target, id, vote, site_id) {
	if(target == 'clip' || target == 'dvd' || target == 'actor'){
		if(id == ''){
			alert(mess_err_form);
			return 0;
		}
	}
	else{
		alert(mess_err_form);
		return 0;
	}

	$.getJSON("json/rate.php",{target:target, id:id, vote:vote, pass_site_id:site_id}, print_rate_result);
	//alert("json/rate.php?id="+id+"&pass_site_id="+site_id+"&target="+target+"&vote="+vote);

}

function print_rate_result(vote){
	if(vote.result == 1){
		showContent('thx_voted_' + vote.id);
		//alert(mess_thx_vote);
	}
	else if(vote.result == 2){
		alert(mess_already_update);
	}
	else{
		alert(mess_err_vote);
	}
}

//ADD FAVORITE CLIP, MOVIE, ACTOR
function add_favorite(target, id, site_id) {
	if(target == 'clip' || target == 'dvd' || target == 'actor'){
		if(id == ''){
			alert(mess_err_form);
			return 0;
		}
	}
	else{
		alert(mess_err_form);
		return 0;
	}

	$.getJSON("json/add_favorites.php",{target:target, id:id, pass_site_id:site_id}, print_favorite_result);
	//alert("json/add_favorites.php?id="+id+"&pass_site_id="+site_id+"&target="+target);
}

function print_favorite_result(entry){
	if(entry.result == 1){
		alert(mess_thx_fav);
	}
	else if(entry.result == 2){
		alert(mess_already_fav);
	}
	else{
		alert(mess_err_fav);
	}
}

//ASK + GUESTBOOK
function submit_entry(target) {
	if(target == 'ask' || target == 'guestbook'){
		var htmlObj = document.form_entry;
	}
	else{
		alert(mess_err_form);
		return 0;
	}

   //a minimum check
	var my_reg = /(@)|(href(=|\s{1}=))|(www)|(<a)|(<img)|(http)/i;
	var current_elements = htmlObj.elements;
	for(var i = 0; i < current_elements.length; i++){
		if(current_elements[i].value == ''){
			alert(mess_err_empty);
   			return 0;
		}
		if(my_reg.test(current_elements[i].value)){
			alert(mess_err_email);
			return 0;
		}
	}

	var name = urlencode(htmlObj.nickname.value);
	var location = urlencode(htmlObj.location.value);
	var message = urlencode(htmlObj.message.value);
	$.getJSON("json/submit_entry.php",{name:name, location:location, message:message, target:target},print_submit_entry);
	//$.getJSON("json/submit_entry.php?name="+name+"&location="+location+"&message="+message+"&target="+target, print_submit_entry);


}

//COMMENTS
function submit_comments(target, id, site_id) {
	if(target == 'clip' || target == 'dvd' || target == 'actor'){
		if(id == ''){
			alert(mess_err_form);
			return 0;
		}
		var htmlObj = document.comment_entry;
	}
	else{
		alert(mess_err_form);
		return 0;
	}

	//a minimum check
	var my_reg = /(@)|(href(=|\s{1}=))|(www)|(<a)|(<img)|(http)/i;
	var current_elements = htmlObj.elements;
	for(var i = 0; i < current_elements.length; i++){
		if(current_elements[i].value == ''){
			alert(mess_err_empty);
   			return 0;
		}
		if(my_reg.test(current_elements[i].value)){
			alert(mess_err_email);
			return 0;
		}
	}

	var name = urlencode(htmlObj.nickname.value);
	var location = urlencode(htmlObj.location.value);
	var comment = urlencode(htmlObj.comment.value);
	$.getJSON("json/add_comments.php",{name:name, location:location, comment:comment, target:target, id:id, pass_site_id:site_id},print_submit_entry);
	//alert("json/add_comments.php?name="+name+"&location="+location+"&comment="+comment+"&target="+target+"&id="+id+"&pass_site_id="+site_id);

}


//USER ACCOUNT & LANGUAGE
function set_language(id){

	if(id == ''){
		alert(mess_err_empty);
		return 0;
	}

	// We must use a POST request here because IE caches our json query.
	// By using a post, we are sure the set_language.php always gets called to save the user language preference.
	// The two other alternative is to pass a random value as parameter or set the meta no cache to the page.
	// ref: http://ajaxian.com/archives/ajax-ie-caching-issue

	//$.getJSON("json/set_language.php",{id:id}, print_language_result);
	$.post("json/set_language.php",{id:id}, print_language_result,"json");
	//window.location.reload(true);

}

function print_language_result(entry){
	if(entry.result == 1){
		//alert(mess_thx_lang);

		// PATCH :  We must reload the page when a user changes his language.
		//			For some reason the javascript reload will only work here after the alert.
		//			Perhaps we should revove the alert entirely when it is a success and simply
		//			let the page refresh (the refresh command in the calling function set_language()).
		window.location.reload(true);

	}
	else{
		alert(mess_err_lang);
	}
}


function print_submit_entry(entry){
	if(entry.result == 1){
		alert(mess_thx_entry);
	}
	else if(entry.result == 2){
		alert(mess_already_entry);
	}
	else{
		alert(mess_err_entry);
	}
}

