
function opinionVote(myvote, vars) {
	
	if (myvote=='popieram')
		document.getElementById('opvote_'+vars).vote.value='yes';
	else if (myvote=='nie-popieram')
		document.getElementById('opvote_'+vars).vote.value='no';
		
	advAJAX.submit(document.getElementById('opvote_'+vars), {
    onSuccess : function(obj) { 
			var re = new RegExp('<[^>]+>', "g");
			var wasrank=parseInt(document.getElementById('rank_'+vars).innerHTML.replace(re, ''));
			var newrank=wasrank+parseInt(obj.responseText);
			var txt='';
		
			if (newrank==0)
				txt='<strong style="color: gray;">'+newrank+'</strong>';
			else if (newrank<0)
				txt='<strong style="color: red;">'+newrank+'</strong>';
      else 
				txt='<strong style="color: green;">'+newrank+'</strong>';
		
		
		document.getElementById('rank_'+vars).innerHTML=txt; 
		document.getElementById('opv1_'+vars).onclick='javascript:void(0);'; 
		document.getElementById('opvote1_'+vars).src='images/site/smallokvote_gray.gif'; 
		document.getElementById('opv2_'+vars).onclick='javascript:void(0);'; 
		document.getElementById('opvote2_'+vars).src='images/site/smallnovote_gray.gif'; 
		
		},
    onError : function(obj) { alert("Error: " + obj.status); }
});


}


function vote(vars) {
	if (document.getElementById('voted').value==''){
		document.getElementById('myvote').value=vars;
		document.getElementById('extracomment').style.display='block';
		advAJAX.submit(document.getElementById('glosowanie'), {
			onSuccess : function(obj) { 
				//setprogresbar ----------------------------
				var responce=parseInt(obj.responseText);
				if (responce== (-1) || responce== 1) {				
					var txt='<table class="answer"><tr>';
					var totalvotes=parseInt(document.getElementById('totalvotes').value);
					var yesvotes=parseInt(document.getElementById('yesvotes').value);
					var novotes=parseInt(document.getElementById('novotes').value);
					
					totalvotes++;
				
					if (responce== 1)
						yesvotes++;
					else
						novotes++;
					
				
					if (totalvotes!=0){
						var yesperc= Math.round((yesvotes / totalvotes) * 100);
						var noperc= Math.round((novotes / totalvotes) * 100);
					} else {
						var yesperc=50;
						var noperc=50;
					}
							
					txt += '<td class="opis"><strong>TAK</strong><br />('+yesperc+'%)</td>';
					txt += '<td><table><tr><td class="positive" style="width: '+yesperc+'%;"></td>';
					txt += '<td class="negative" style="width: '+noperc+'%;"></td></tr></table></td>';
					txt += '<td class="opis"><strong>NIE</strong><br />('+noperc+'%)</td>';
					txt += '</tr><tr><td class="totalopinions" colspan=4>Na podstawie '+totalvotes+' opinii użytkowników</td></tr></table>';
					
					document.getElementById('answerbar').innerHTML=txt;
					document.getElementById('yesbigvotea').onClick='javascript:void(0);'; 
					document.getElementById('yesbigvote').src='images/site/takbig_gray.gif'; 
					document.getElementById('nobigvotea').onClick='javascript:void(0);'; 
					document.getElementById('nobigvote').src='images/site/niebig_gray.gif'; 
				} else
					return;
				//------------------------------------------
				document.getElementById('voted').value='yes';
			},
			onError : function(obj) { alert("Error: " + obj.status); }
		});
	}
}



function comment() {
	if (document.getElementById('commenttext').value.length<10) { alert('Zbyt krótki komentarz'); }
	else {
	
		advAJAX.submit(document.getElementById('glosowanie'), {
			onSuccess : function(obj) { 
				if (obj.responseText.substr(0,13)=='Wystąpił błąd') {
					alert(obj.responseText);
				} else {
					document.getElementById('newopinion').innerHTML=obj.responseText; 
					document.getElementById('newopinion').style.display='block';
					document.getElementById('extracomment').style.display='none';
				}
			},
			onError : function(obj) { alert("Error: " + obj.status); }
		});
	}
}




function validate_newQuestionForm(thisform) {
	var err=new Array();
	with (thisform) {
		if (!userid.value && validate_email(email)==false) {
			err[0] = 'Niepoprawny adres e-mail';
		}

		if (validate_field(title)==false) err[err.length++] = 'Pole Twoje pytanie jest wymagane';
		if (captcha.value.length==0) err[err.length++] = 'Należy podać wynik dodania aby unikać spamu.';
		//if (validate_sbox(upcat)==false) err[err.length++] = 'Kategoria Nadrzędna jest wymagana';
		//if (validate_sbox(subcat)==false) err[err.length++] = 'Kategoria Podrzędna jest wymagana';
		//if (subcat.value=='nogoodcat') 
		//	if (validate_field(extrasubcat)==false) err[err.length++] = 'Prosze podac nazwe podkategorii';
		
		var filename=document.getElementById('filename');
		if (filename.value.length > 0) {
			var f=filename.value.toLowerCase();
			var f=f.split('.');
			var fl=f.length;
			fl--;
			var ext=f[fl];
			if (ext=='jpg' || ext=='gif' || ext=='jpeg' || ext=='png' || ext=='bmp')
				document.getElementById('imgextension').value=ext;
		} 
  }
	
	if (err.length>0){
		var txt='';
		for(i=0;i<err.length;i++){
			j=i+1;
			txt+=j+'- '+err[i]+"\n";
		}
		alert(txt);
		return false;
	} else 
		return true;
}



function validate_email(field)
{
with (field)
  {
  apos=value.indexOf("@");
  dotpos=value.lastIndexOf(".");
	len=value.length;
  if (apos<1||dotpos-apos<1||len-dotpos<2)
    { return false; }
  else { return true; }
  }
}

function validate_field(field)
{
with (field)
  {
	wart=value;
	if (wart.length<5)
    { return false; }
  else { return true; }
  }
}


function validate_sbox(field)
{
with (field)
  {
	if (!value)
    { return false; }
  else { return true; }
  }
}


function removeAllOptions(selectbox)
{
	var i;
	for(i=selectbox.options.length-1;i>=0;i--)
	{
		selectbox.remove(i);
	}
}


function addOption(selectbox, value, text )
{
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;

	selectbox.options.add(optn);
}
