//function that programmatically creates a form with method post
function postwith (to,p) {
  var myForm = document.createElement("form");
  myForm.method="post" ;
  myForm.action = to ;
  for (var k in p) {
    var myInput = document.createElement("input") ;
    myInput.setAttribute("name", k) ;
    myInput.setAttribute("value", p[k]);
    myForm.appendChild(myInput) ;
  }
  document.body.appendChild(myForm) ;
  myForm.submit() ;
  document.body.removeChild(myForm) ;
}

//event handler when a key has been entered in the search field
function pressKey(e){
var keynum;
var keychar;
if(window.event) // IE
{
keynum = e.keyCode;
}
else if(e.which) // Netscape/Firefox/Opera
{
keynum = e.which;
}
//if the enter button has been pressed
if(keynum==13){
var x = document.getElementById('content2');
if(x.length==1){
var suggestion= x.options[0].text;
if(suggestion=="Pas de candidat trouvé" ||suggestion=="Réessayez plus tard"){
alert("Votre recherche n'a pas été concluante");
}else{
x.options[0].selected = true;
selectCandidate();
}
}else{
alert("Choisissez une suggestion ou continuez à raffiner votre recherche");
}
return false;
}
}

//event handler when the magnifier has been clicked
function clickMagnifier(){
var x = document.getElementById('content2');
if(x.length==1){
var suggestion= x.options[0].text;
if(suggestion=="Pas de candidat trouvé" ||suggestion=="Réessayez plus tard"){
alert("Votre recherche n'a pas été concluante");
}else{
x.options[0].selected = true;
selectCandidate();
}
}else{
alert("Choisissez une suggestion ou continuez à raffiner votre recherche");
}
}

//function to hide the suggestion list
function hideContent(){
document.getElementById('nameField').focus();
document.getElementById('content1').innerHTML = "";
document.getElementById('content1').style.visibility="hidden";
}

// handles the response from the search xmlhttp request when some values has been added into the search field
function handleSearchName(){
switch(xmlHttp.readyState) {
                    case 4:
                          document.getElementById('content1').innerHTML = "";
                          document.getElementById('content1').innerHTML = xmlHttp.responseText;
						  document.getElementById('content1').style.visibility="visible";
						  
						  
                        
                    break;
            
                    default:
                        return false;
                    break;     
                }
            }

// handles the response from the search xmlhttp request when only the search filters have been used	
function handleSearchName2(){
switch(xmlHttp.readyState) {
                    case 4:
                          document.getElementById('content1').innerHTML = "";
                          document.getElementById('content1').innerHTML = xmlHttp.responseText;
						  document.getElementById('content1').style.visibility="visible";
						  document.getElementById('content2').focus();
                        
                    break;
            
                    default:
                        return false;
                    break;     
                }
            }
			
 
//function to search for candidates 
function searchName(){

 xmlHttp = false;
// searching the correct XmlHTTp object corresponding to the browser
        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            xmlHttp = new XMLHttpRequest();
            if (xmlHttp.overrideMimeType) {
                xmlHttp.overrideMimeType('text/xml');
                
            }
        } else if (window.ActiveXObject) { // IE
            try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!xmlHttp) {
            alert('Ende :( Kann keine XMLHTTP-Instanz erzeugen');
            return false;
        }
//getting the parameters from the search components
var name = document.getElementById('nameField').value;

var x = document.getElementById('circoField');
var ind = x.selectedIndex;
var circo= x.options[ind].text;

var y = document.getElementById('partisField');
var ind = y.selectedIndex;
var parti= y.options[ind].value;

//hiding the suggestion list if nothing has been changed or entered in the search components
if(document.getElementById('nameField').value=="" && parti=="Choisir Parti" && circo=="Choisir Circonsc."){
document.getElementById('content1').style.visibility="hidden";
}
//if only a search filter has been used
else if(document.getElementById('nameField').value=="" &&(parti!="Choisir Parti" || circo!="Choisir Circonsc.")){
var params ="circo="+circo;
params=params+"&parti="+parti;
var url="./dbInterface/Candidates2.php";
xmlHttp.onreadystatechange=handleSearchName2;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(params);
}
//if a name has been entered into the search field
else{
var params ="circo="+circo;
params=params+"&candidat="+name;
params=params+"&parti="+parti;
var url="./dbInterface/Candidates.php";
xmlHttp.onreadystatechange=handleSearchName;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(params);
}
}

//function to select a candidate and redirect to its information page
function selectCandidate(){
var y = document.getElementById('content2');
var ind1 = y.selectedIndex;
var selected= y.options[ind1].text;
postwith('CandidateInfo.php',{cand:selected});

}

