
// NOTE:  "formFieldNameSelect" and "defaultText" should be defined in the including page.

function populateOpenerFields()
{
	//get the current form
	var thisform = document.forms[0];

	//clear all current entries in opener form
	clearSelectOptionsInOpener();
	
	//get the appropriate field names from the current form
	for(i=0; i<thisform.elements.length; i++)
	{
		var input = thisform.elements[i];
		if(input.type == "checkbox" && input.checked)
		{
			//create option in the opener window
			createSelectOptionInOpener(input.value, input.value);
		}
	}	
	
	if(opener.document.forms[0].elements[formFieldNameSelect].options.length == 0)
	{
		//createSelectOptionInOpener(defaultText, "");
		opener.setDefaultOptionInOpener(formFieldNameSelect);
	}
}


function populateInputAssistedOpenerField()
{
	//get the current form
	var thisform = document.forms[0];
	var zips = "";
	var firstvalue_processed = false;
	var totalZipCDs = 0;
	
	//get the appropriate field names from the current form
	for(i=0; i<thisform.elements.length; i++)
	{
		var input = thisform.elements[i];
		
		if(input.type == "text" && input.value != null && input.value != ""){
		
			if(!firstvalue_processed){
				firstvalue_processed = true;
				zips = input.value;
				totalZipCDs++;
			}else{
				//concatenate
				zips = zips + ", " + input.value;
				totalZipCDs++;
			}
		}
	}
	createInputAssistedInOpener(zips, (totalZipCDs > 1));
	//alert(zips);	
}

function createInputAssistedInOpener(text, disableField)
{
	opener.setInput(formFieldNameSelect, text);
    
    if (disableField)
     {
       opener.disableFormField (distanceSelect);
       opener.resetSelection(distanceSelect);
     }
    else
     {
       opener.enableFormField (distanceSelect);
     }
}

function createSelectOptionInOpener(text, value)
{
	opener.addNewOption(formFieldNameSelect, text, value, false);
}

function clearSelectOptionsInOpener()
{
	opener.clearSelectOptionsInOpener(formFieldNameSelect);
}

function checkOptionsSelectedInOpener()
{
	select = opener.document.forms[0].elements[formFieldNameSelect];
	for(i=0; i<select.options.length; i++){
		openerValue = select.options[i].value;
		form = document.forms[0];
		for(j=0; j<form.elements.length; j++)
		{
			if(form.elements[j].type == "checkbox")
			{
				if(form.elements[j].value == openerValue)
				{
					form.elements[j].checked = true;
				}
			}
		}
	}	
}

function addZipsSelectedInOpener()
{
	inputtext = opener.document.forms[0].elements[formFieldNameSelect];
	var zips = inputtext.value;
	var zipcodes = zips.split(", ");
	
	for(i=0; i<zipcodes.length; i++){
		document.forms[0].elements[i].value = zipcodes[i];
	}
}

function setInput(formFieldNameSelect, text)
{
	input = document.forms[0].elements[formFieldNameSelect];
	input.value = text;
}

// This function will disable the selected form field.

function disableFormField (formFieldName)
{
    document.forms[0].elements[formFieldName].disabled=true;
}

// This function will enable the selected form field.

function enableFormField (formFieldName)
{
    document.forms[0].elements[formFieldName].disabled=false;
}

// This function will set the selected index to 0 for the selection list.

function resetSelection (formFieldName)
{
    document.forms[0].elements[formFieldName].selectedIndex=0;
}

function addNewOption(formFieldNameSelect, text, value, defaultSelected, selected)
{
	select = document.forms[0].elements[formFieldNameSelect];
	addIndex = select.options.length;
	
	index = text.lastIndexOf("#");
	var txt = text.substring(index+1);
	select.options[addIndex] = new Option(txt, value, defaultSelected, selected);
}

function clearSelectOptionsInOpener()
{
	select = opener.document.forms[0].elements[formFieldNameSelect];
	selectlength = select.options.length;
	for(i=0; i<selectlength; i++)
	{
		select.options[0] = null;
	}
}



function setDefaultOptionInOpener(formFieldNameSelect)
{
	//addNewOption(formFieldNameSelect, defaultText, "", false);
	addNewOption(formFieldNameSelect, eval(formFieldNameSelect+"_defaultText"), "", false);
}


//used just before http submit to set all options as selected
//so recieving action knows that all options within the checkbox
//have been selected
function setAllSelectOptionsAsSelected(formFieldNameSelect)
{
	select = document.forms[0].elements[formFieldNameSelect];
	selectlength = select.options.length;
	for(i=0; i<selectlength; i++)
	{
		
		if (select.options[i].value.length > 0)
		{
		//	alert(select.options[i].value);
			select.options[i].selected = true;
		}else{
			select.options[i].value = "";
			select.options[i].selected = true;
		//	alert("empty string detected");
		}
	}
}



//used to prevent individual selection of an option in the select
//box.  Allowing individual option selections would confuse the user
function deselectAll(formFieldNameSelect){		
	//blur from this field so the options and select box do not
	//stay selected.  Or give focus to something other than this
	//select box.  To Be Implemented

	select = document.forms[0].elements[formFieldNameSelect];

	selectlength = select.options.length;
	for(i=0; i<selectlength; i++){
		select.options[i].selected = false;	
	}
}


