function do_lookup(id_field, desc_field) {
    id = eval("window.document.forms['form1']." + id_field);
    if (id.value == "") {
	do_browse(id_field, desc_field);
    } else {
	if (isNaN(parseFloat(id.value))) {
	    desc = eval("window.document.forms['form1']." + desc_field);
	    desc.value = id.value;
	    do_search(id_field, desc_field);
	} else {
	    do_browse(id_field, desc_field);
	}
    } 
}

function do_browse(id_field, desc_field) {
    id = eval("window.document.forms['form1']." + id_field);
    if (id.value == "") {
	mywindow = window.open("cpv-cats?code_field=" + id_field + "&desc_field=" + desc_field + "&back=0&arg=", "cpv_window",
	       "width=600, height=400, left=100, top=100, scrollbars=yes, resizable=yes, status=no");
    } else {
	mywindow = window.open("cpv-browse?code_field=" + id_field + "&desc_field=" + desc_field + "&back=0&arg=" + id.value, "cpv_window",
	       "width=600, height=400, left=100, top=100, scrollbars=yes, resizable=yes, status=no");
    }
}

function do_search(id_field, desc_field) {
    desc = eval("window.document.forms['form1']." + desc_field);
    if (desc.value == "") {
	alert("You must enter a keyword or phrase to search for")
    } else {
	openString = "cpv-search?code_field=" + id_field + "&desc_field=" + desc_field + "&arg=" + desc.value;
	mywindow = window.open(openString, "cpv_window",
	       "width=600, height=400, left=100, top=100, scrollbars=yes, resizable=yes, status=no");
    }
}

function addOption(cpvId,cpvDesc)
	{		
		var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);
		var NSX = (navigator.appName == "Netscape");
		var IEX = (navigator.appName == "Microsoft Internet Explorer");	
				
	        if (IEX)
	        {
	                addOptionIE(cpvId,cpvDesc);
	        } 
	        
	        else if (NSX)
	        {
	                addOptionNS(cpvId,cpvDesc);
	        }
	         else
	         	{	addOptionXX(cpvId,cpvDesc);

	         	}	
	}
	
	
// (1) addOptionNS(theForm)
function addOptionNS(cpvId,cpvDesc)
	{
			var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);
	        var destList  = window.document.forms['form2'].selectedCpvs;
	        var full_name=cpvId + " " + cpvDesc;
	        var found = false;
	        for (var i = 0; i < destList.length; i++) {
    			if (destList.options[i] != null) {
      				if (destList.options[i].text == full_name) {
        				found = true;
        				alert("cpv already in use");
        				break;
      				}
    			}
  			}
  			if (found != true) {
		        var newOpt  = new Option(cpvId + " " + cpvDesc,cpvId);
		        var selLength = document.forms['form2'].selectedCpvs.length;
		        document.forms['form2'].selectedCpvs.options[selLength] = newOpt;
		        
	        }
	}
	
function addOptionXX(cpvId,cpvDesc)
	{
	        var destList  = window.document.forms['form2'].selectedCpvs;
	        var full_name=cpvId + " " + cpvDesc;
	        var found = false;
	        for (var i = 0; i < destList.length; i++) {
    			if (destList.options[i] != null) {
      				if (destList.options[i].text == full_name) {
        				found = true;
        				alert("cpv already in use");
        				break;
      				}
    			}
  			}
  			if (found != true) {	        
		        var newOpt  = document.createElement("OPTION");
		        var newText = document.createTextNode(cpvId + " " + cpvDesc);
		        newOpt.setAttribute("value",cpvId);
		        newOpt.appendChild(newText);
				var selLength = document.getElementById('selectedCpvs').length;
		        document.getElementById('selectedCpvs').appendChild(newOpt);
		      
	        }
	}
	
		
// (2) addOptionIE(theForm)
function addOptionIE(cpvId,cpvDesc)
	{
			var destList  = window.document.forms['form2'].selectedCpvs;
	        var full_name=cpvId + " " + cpvDesc;
	        var found = false;
	        for (var i = 0; i < destList.length; i++) {
    			if (destList.options[i] != null) {
      				if (destList.options[i].text == full_name) {
        				found = true;
        				alert("cpv already in use");
        				break;
      				}
    			}
  			}
  			if (found != true) {
		        var newOpt = document.createElement("OPTION");
		        newOpt.text=cpvId + " " + cpvDesc;
		        newOpt.value=cpvId;
		        form2.selectedCpvs.add(newOpt);
		    }
	}

function deleteFromList() {
	
  var destList  = window.document.forms['form2'].selectedCpvs;
  var len = destList.options.length;
  for(var i = (len-1); i >= 0; i--) {
    if ((destList.options[i] != null) && (destList.options[i].selected == true)) {
      destList.options[i] = null;
    }
  }
}

function makePrimary() {
  var destList  = window.document.forms['form2'].selectedCpvs;
  var buffer = destList.options[0].text;
  var len = destList.options.length;
  for(var i = (len-1); i >= 0; i--) {
    if ((destList.options[i] != null) && (destList.options[i].selected == true)) {
      destList.options[0].text = destList.options[i].text;
      destList.options[i].text = buffer;
      destList.options[i].selected = false;
      window.document.forms['form2'].primaryCpv.value = destList.options[0].text;
    }
  }
}




function removePlaceholder() {
  var destList  = window.document.forms[0].cpvList;
  if (destList.options[0].text.substring(0,1) == "=") {
      destList.options[0] = null;
  }
}

// Marks all the items as selected for the submit button.  

function selectList(sourceList) {
    
	if (sourceList.options.length == 0){
		var msg = document.getElementById('jsSelect').value;
		alert(msg);
		return false;
	}
    for(var i = 0; i < sourceList.options.length; i++) {
	if (sourceList.options[i] != null)
	    sourceList.options[i].selected = true;
    }
    
    return true;
}

function selectListPopup(sourceList) {
	if (sourceList.options.length == 0){
		var msg =	document.getElementById('jsSelect').value;
		alert(msg);
		return false;
	}
    for(var i = 0; i < sourceList.options.length; i++) {
		if (sourceList.options[i] != null)
		    sourceList.options[i].selected = true;
    }
   	document.forms['form2'].submit();
	var msgSaved =	document.getElementById('jsSaved').value;
	alert(msgSaved);
	window.close();    
}


		

