// Popup window function
function openDecScroll(url, name, w, h) {
    popupWin = window.open(url, name, 'resizable=1,scrollbars=1,menubar=1,width=' + w + ',height=' + h + '');
  
    // focus window (only for Navigator >= 3.0)
    if ( (navigator.appName != "Microsoft Internet Explorer") &&  (navigator.appVersion.substring(0,1) == "3") ){
	popupWin.focus();
    }
}



//half validate new conference form
// TODO check for these fields also
//     var abstract = document.insertForm.abstract.value
//     var icon = document.insertForm.icon.checked
//     var avail = document.insertForm.avail.value

function validateConference(){
    var confid = document.insertForm.confid.value
	var shortname = document.insertForm.shortname.value
	var longname = document.insertForm.longname.value
	var period = document.insertForm.period.value
	var location = document.insertForm.location.value
	var chairman = document.insertForm.chairman.value
	var deadline = document.insertForm.deadline.value
	var leng_cont = document.insertForm.leng_cont.value

	if (confid.length<=0    ||
	    shortname.length<=0 ||
	    longname.length<=0  ||
	    period.length<=0    ||
	    location.length<=0  ||
	    chairman.length<=0  ||
	    deadline.length<=0  ||
	    leng_cont.length<=0 )
	    {
		alert("Error: some field was empty! Please fill in all fields")
		return false
	    }
}



//half validate new contribution form
function validateContribution(){
    var author = document.new_contrForm.newauthor.value
	var title = document.new_contrForm.title.value
	var argument = document.new_contrForm.argument.value
	var session = document.new_contrForm.session.value
	var flag = ( argument.length<=0 && session.length<=0 )

	if ( author.length<=0 || title.length<=0 || flag ) {
	    alert("Error: some field was empty! Please fill in all fields")
	    return false
	}
}


// permits search into a long select
// Original:  Darko Egersdorfer (darkoe@za.bynx.com)
// This script and many more are available free online at
// The JavaScript Source!! http://javascript.internet.com
function promptSearch (thisform, thisfield) {
    var elnum = parseInt(-1);
    var k = parseInt(-1);
    for (var j = 0; j < document.forms[thisform].elements.length; j++) {
	if (document.forms[thisform].elements[j].name == thisfield) {
	    elnum = j;
	    break;
	}
    }
    var myvar = "";
    var srch = "";
    var menuLength = "";
    menuLength = eval("document." + thisform + "." + thisfield + ".length");
    srch = prompt("Search this menu for...", "");
    if (srch ) {
	srch = srch.toUpperCase();
	for (var i = 0; i < menuLength; i++) {
	    myvar = eval("document." + thisform + "." + thisfield + ".options[" + i + "].text.toUpperCase()");
	    if (myvar.indexOf(srch) >= 0) {
		document.forms[thisform].elements[elnum].options[i].selected = true;
		break;
	    }
	}
    }
    return false;
}





function CheckAll()
{
    form = document.recipients;
    for (var i = 0; i < form.elements.length; i++)
	{
	    var e = form.elements[i];
	    if (e.type == 'checkbox'){
		if (e.name != 'allbox1')
		    e.checked = form.allbox1.checked;
	    }
	}
}




/*

Tratto da Mike West

 */

function TypeAheadSelect(inputId, listId ){ //, outputId) {
    var self = this;	
    var theInput = document.getElementById(inputId);
    //    var theOutput = document.getElementById(outputId);
    var theList = document.getElementById(listId);
    
    theInput.addEventListener('keyup',function(e){

	if (!e) e = window.event;
	var keycode = e.keyCode;
	switch (keycode) {
	case 38: // up arrow
	    /*
	      We want to respond to the up arrow by moving the current 
	      value up one in our list.  But we don't want to run off 
	      the top of the list, so we get the maximum value: 0, or 
	      the currentIndex - 1.  So if we're at the 8th value, 
	      we'll move to the 7th, but if we're already at the top, 
	      we'll stay at the 0th (numbering starts at 0 in JavaScript)
	    */
	    var newIndex = Math.max(0, theList.selectedIndex-1);
	    theList.selectedIndex  = newIndex;
	    break;
            
	case 40: // down arrow
	    /*
	      Same thing as the up arrow here, except we want to get 
	      the minimum value between the last element in the list, 
	      and the currentIndex + 1 to make sure we don't run off
	      the end.
	    */
	    var newIndex = Math.min(theList.options.length-1, theList.selectedIndex+1);
	    theList.selectedIndex  = newIndex;
	    break;
    
    
	default:
	    /*
	      If it's not the up/down arrow, or the enter key, then process
	      it by telling the SELECT box to set itself to the correct value.
	      We'll talk about telling the SELECT element how to do that in 
	      a few moments.
                
	      The timeout code is here in order to deal with fast typists: we
	      don't want to tell the SELECT box to filter itself for every keypress
	      if we're in the middle of typing a word.  That would slow things 
	      and potentially cause problems if the value of the INPUT field 
	      changed during the middle of an event.  So we store a timer that
	      waits 25 milliseconds before telling the SELECT object (stored in 
	      this.haystack) to filter itself using the currently typed-in text.
                
	      Every time a key is pressed, we clear out that timer, and reset it
	      for another 25 milliseconds.  That solves our problem.
	    */
	    clearTimeout(theInput.timer);
	    theInput.timer = setTimeout(
					"self.filter()",
					25
					);

	    break;
	}
    },
			      false);

                
    
    filter  = function () {
	var needle = theInput.value;
	if (needle == ""){
	    theList.selectedIndex = 0;
	    return;
	}


	needle = needle.toLowerCase();
	var low = 0;
	var high = theList.options.length;
	var j = 0;


	//binary search through the list
	while (high > low) {
	    j = Math.floor((high+low)/2);
	    //	    theOutput.value = theOutput.value + "needle="+needle+"; high="+high+"; j="+j+"; low="+low+"; item("+j+")="+theList.options.item(j).text.toLowerCase()+"\n";
	    if (needle <= theList.options.item(j).text.toLowerCase()) {
		high = j;
	    } else {
		low = j+1;
	    }
	}


	theList.selectedIndex = high;

    }            

    
}



// //da pixaco: interessante metodo per avere un numero variabili di input fields
// maxuploads = 21;  // eins mehr als angezeigt wird!
// for(i=4;i<=maxuploads;i++) {
//     document.writeln("<TR id=\"upload_"+i+"\" style=\"display:none\">");
//     document.writeln("<TD align=\"right\"><B>"+i+".&nbsp; </B></TD>");
//     if (i == maxuploads)
// 	next = i+1; else next = i + 1;
//     document.writeln("<TD><INPUT style=\"WIDTH: 310\" type=\"file\" accept=\"image/jpeg\" size=\"40\" name=\"afile\" onclick=\"javascript:showupload('upload_"+next+"');\" onchange=\"javascript:showupload('upload_"+next+"');\"></TD>");
//     document.writeln("</TR>");
// };

// function showupload(uploadid)
// {
//     if ("upload_"+maxuploads == (uploadid)) {
// 	document.getElementById("uploadfirst").style.display = "";
//     }else{
// 	document.getElementById(uploadid).style.display = "";
//     }
// }




