//function fetching second rolldown select by first selected option, options to fetch are generated and included into html by php

function switchOptions(elements,group,containerId,name){
	var container=document.getElementById(containerId);
	if (container.innerText) container.innerText='';
	if (container.innerHTML) container.innerHTML='';
	
	var oNewOption = new Option();
  oNewOption.value = 'NULL';
 	oNewOption.text = '↓ any '+name+' ↓';

 	if (navigator.appName == 'Microsoft Internet Explorer'){
 		container.add(oNewOption);
 	}else{
 	  container.add(oNewOption,container.options[container.options.length]);
 	}

  for (el in elements[group]){
		var oNewOption = new Option();
	  oNewOption.value = el;
	  oNewOption.text = elements[group][el];
	 	if (navigator.appName == 'Microsoft Internet Explorer'){
	 		container.add(oNewOption);
	 	}else{
	 	  container.add(oNewOption,container.options[container.options.length]);
	 	}
	  //container.scrollIntoView();
	}
	return true;
}

//funcitions to operate rating mechanism

function highlight(nr, name, type){
	for (i=1; i<=nr; i++){
		document.getElementById(name+i).src='gfx/grade_'+type+'.gif';
	}
	for (i=nr+1; i<6; i++){
		document.getElementById(name+i).src='gfx/grade_no.gif';
	}
}

function ajaxFunction(id,vote){
  var xmlHttp;
  try{// Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e){// Internet Explorer
    try{
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e){
      try{
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e){
        //document.getElementsByTagName('meta')[2].content=5;//browser does not support AJAX anyway, returning to html refresh
        }
      }
    }
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
				processAjaxAnswer(xmlHttp.responseText);
			}
		}
	xmlHttp.open('GET','http://www.matchmywine.com/ajax/vote.php?id='+ id +'&grade='+vote,true);
  xmlHttp.send(null);
	
  return false;
  }
  
  function processAjaxAnswer(text){
  	if (text=='voted'||text=='') 
  		voted=true;
  	else
  		voted=false;
  		
  	values = text.split('|');
  	if (!voted&&values.length&&(values[0]>0)&&(values[0]<7)){
  		highlight(values[0], 'grade_','ok');
  		document.getElementById('votesCount').innerHTML='(Votes: '+ values[1] + ' '+ (voted?' - this ip has voted already':' - your vote was accepted') +')';
  		avg=values[0];
  	}else{
  		avg = document.grade;
  	}
  	
		for (i=1; i<6; i++){
			document.getElementById('iconLinkContainer_'+i).innerHTML= '<img src="gfx/grade_'+(i<=avg?'ok':'no')+'.gif" alt="grade" />';
		}
		
  }