/* Contribution vote functions */

function getSelectVote(f) {
    for ( var i = 0; i < 5; i++ )
        if ( f.vote[i].checked )
            return f.vote[i].value;
    return 0;
}
function SaveVote( contribution_id, vote, lang_path, vote_error ) {
    var HttpReq = null;
    if (window.ActiveXObject) {
        try { HttpReq = new ActiveXObject('Msxml2.XMLHTTP'); }
        catch (e) {
            try { HttpReq = new ActiveXObject('Microsoft.XMLHTTP'); }
            catch (e) {}
        }
    } else if (window.XMLHttpRequest) {
        try { HttpReq = new XMLHttpRequest(); }
        catch (e) {}
    }
    if (HttpReq) {
        HttpReq.open( 'GET', lang_path + '/contribution/vote/?contribution_id='+contribution_id+'&vote='+vote+'&js=on', false );
        document.getElementById('block_vote').innerHTML = '{lang.voting}';
        HttpReq.send(null);
        document.getElementById('block_vote').innerHTML = '<br />' + HttpReq.responseText;
    } else
        document.getElementById('block_vote').innerHTML = vote_error;
}

/* End Contribution vote functions */


/* Labeller functions */

function switchLabeller()
{
	$.get(
		'/check_label_count.php',
		function(data){
			if(data > 1) {
				$('#captcha_img').attr('src','/confirmation-word.php');
				$('#captcha').show();
			} else {
				$('#captcha').hide();
				$('#captcha_img').attr('src','');
			}
		}
	);
	$('#labeller').css('display', $('#labeller').css('display')=='none'?'block':'none');
}

function addLabels()
{
	var form = new Array;
	$('.label_input').each(function(){
		if(/^\s*$/.test(this.value)) result = true;
	});
	if(!result) {
		$("#label_error").show();
		return false;
	}
    
    if(	$('#labeller').css('display') == 'block' &&
		!/^\s*$/.test($('#confirm').val())
	) {
		$("#captcha_error").show();
		return false;
    }
    
    var lang = document.cookie.match(/ LANG=(en|fr|ru); /);
    var lang = lang ? "/"+lang[1] : "";
    $.post(
		lang+"/contribution/material/label/",
		$("#labeller_form").serialize(),
		function(data){
			var response = JSON.parse(data);
			$("#message").html(response.text).show();
			$("#labeller").hide();
			$("#labeller_button").remove();
		}
	);
    
    return false;
}
/* End Labeller functions */

/* Material comment functions */

function switchCommenter()
{
    var labeller = document.getElementById('commenter');
    if(!labeller) return;
    labeller.style.display = (labeller.style.display == "none" || labeller.style.display == "") ? "block" : "none"; 
}

function validateCommenter()
{
    var comment = document.getElementById("material_comment");
    comment.value = comment.value.replace(/\s+/g, " ").replace(/ +(\S.+\S) +/, "$1");
    return comment.value.replace(' ', '').length > 0;
}

/* Material comment functions */

/* Wiki functions */
var wikicode_shown = false;
function showWikiCode()
{
    if(wikicode_shown) return;
    var list = document.getElementsByTagName("div");
    for (i in list) {
        var el = list[i];
        var el_id = "" + el.id;
        var matches = /^img(\d+)/.exec(el_id);
        if(matches && matches.length){
            document.getElementById(el_id).innerHTML += '<br /' + '><div style="font-size:11px; margin-top:4px; width:280px; line-height:200%; background-color:#99CCFF; color:#222222; border:1px solid #000000;">[[Fichier:bobvoyeur-' + matches[1] + '.jpg|Description]]<' + '/div>';
        }
    }
    wikicode_shown = true;
}
/* End Wiki functions */
