/*
	NetMinistry/VOSpace Javascript UI Kit v1.0

	Created Monday 02/06/06 10:33:58 AM by CPruitt
	This file defines constants, classes, functions & subs used for managing the user interface

*/


/*  LAUNCHER / Core App UI Support  */

conf_contextualFrameIdString = 'nmframeset_launcher_global';

function nmDisplayContextualInfo(fv_url){
	window.top.nmContextualView.location.href=fv_url;
	return true;
}

function nmLauncherDisplayContextualView(fv_heightValue){
	if (window.top.document.getElementById(conf_contextualFrameIdString)){
		window.top.document.getElementById(conf_contextualFrameIdString).setAttribute('rows', '69,' + fv_heightValue + ',*', 0);
		return true;
	} else {
		alert('Your current browser does not support the JavaScript features required by VOSpace.com.  Please see our system requirements for more information.');
		return false;
	}
}

function nmLauncherCloseContextualView() {
	return window.top.nmLauncherDisplayContextualView(0);
}

function nmCopyAppMenuHTMLFrom(fv_idString) {
	if (document.getElementById(fv_idString)) {
		window.top.nmMenuView.document.getElementById('launcher_ui_menubar_appmenu').innerHTML = document.getElementById(fv_idString).innerHTML;
		return true;
	} else {
		alert('Error loading Application Menu.')
		return false;
	}
	
}

function nmFormSetLabel(fvLabelID, fvLabelValue)
{
	

	var theLabel = document.getElementById(fvLabelID);
	
	if (theLabel != null)
	{
		theLabel.innerHTML = fvLabelValue;
	} else {
		//alert('Label Not Found');
	}

	return true;
}


function nmFormSetFieldByID(fvFieldID, fvFieldValue)
{
	
}




	
function nmFormSetField(fvTargetForm, fvFieldName, fvFieldValue)
{
	
	var targetForm = fvTargetForm;
	var frmField = null;

	//	Determine target field to set
	frmField = eval('targetForm.' + fvFieldName);
	
	if(frmField != null)
	{
		//	Determine what kind of field we are working with
		fieldType = frmField.type;
		//	If field name appears multiple times (e.g. radio button fields) get type from first
		if (fieldType == undefined & frmField.length > 0)
		{
			//alert('Field Length (uikit.js) Set By Name: Len: ' + frmField.length)
			fieldType = frmField[0].type;
		}		
		
		//	Switch to appropriate handler function based on field type
		switch(fieldType)
		{
		case 'text':
			nmFormSetTextField(frmField, fvFieldValue)
			break;
		case 'password':
			nmFormSetPasswordField(frmField, fvFieldValue)
			break;
		case 'checkbox':
			nmFormSetCheckboxField(frmField, fvFieldValue);
			break;
		case 'select-one':
			nmFormSetSelectOneField(frmField, fvFieldValue);
			break;
		case 'radio':
			nmFormSetRadioField(frmField, fvFieldValue);
			break;
		case 'hidden':
			nmFormSetHiddenField(frmField, fvFieldValue)
			break;
		case 'textarea':
			nmFormSetHiddenField(frmField, fvFieldValue)
			break;
		case 'file':
			break;
		default:
			//alert('Case not set: ' + fieldType + " - Len: " + frmField.length);
			nmLog('Field Type Not Handled: ' + frmField.type);
		}
		
		
	}else{
		//alert('Not Found');
	}
}


function nmFormSetFieldObject(fvFieldObject, fvFieldValue)
{
	
	//var targetForm = fvTargetForm;
	var frmField = null;

	//	Determine target field to set
	frmField = fvFieldObject;
	
	if(frmField != null)
	{
		//	Determine what kind of field we are working with
		fieldType = frmField.type;
		//	If field name appears multiple times (e.g. radio button fields) get type from first
		if (fieldType == undefined & frmField.length > 0)
		{
			fieldType = frmField[0].type;
		}		
		
		//	Switch to appropriate handler function based on field type
		nmLog(frmField.name + ' - Field Type: ' + frmField.type + ' = ' + fvFieldValue);
		
		switch(fieldType)
		{
		case 'text':
			nmFormSetTextField(frmField, fvFieldValue)
			break;
		case 'password':
			nmFormSetPasswordField(frmField, fvFieldValue)
			break;
		case 'checkbox':
			nmFormSetCheckboxField(frmField, fvFieldValue);
			break;
		case 'select-one':
			nmFormSetSelectOneField(frmField, fvFieldValue);
			break;
		case 'radio':
			nmFormSetRadioField(frmField, fvFieldValue);
			break;
		case 'hidden':
			nmFormSetHiddenField(frmField, fvFieldValue)
			break;
		case 'textarea':
			nmFormSetHiddenField(frmField, fvFieldValue)
			break;
		case 'file':
			break;
		default:
			//alert('Case not set: ' + fieldType + " - Len: " + frmField.length);
			nmLog('Field Type Not Handled: ' + frmField.type);
		}
		
		
	}else{
		//alert('Not Found');
	}
}


		
		
		/*  FIELD SETTING FUNCTIONS  */




function nmFormSetTextField(fvFieldObj, fvFieldValue)
{
	//alert("Call Function for: Text Form Field");
	fvFieldObj.value = fvFieldValue;
	return true;
}



function nmFormSetPasswordField(fvFieldObj, fvFieldValue)
{
	//alert("Call Function for: Text Form Field");
	fvFieldObj.value = fvFieldValue;
	return true;
}



function nmFormSetHiddenField(fvFieldObj, fvFieldValue)
{
	//alert("Call Function for: Text Form Field");
	fvFieldObj.value = fvFieldValue;
	return true;
}



function nmFormSetCheckboxField(fvFieldObj, fvFieldValue)
{
	var defaultFieldVal = fvFieldObj.value
	//alert("Call Function for: Check Box Form Field - " + defaultFieldVal);
	if(defaultFieldVal == fvFieldValue)
	{
		fvFieldObj.checked = true;
	} else {
		fvFieldObj.checked = false;
	}
	return true;
}


function nmFormSetSelectOneField(fvFieldObj, fvFieldValue)
{
	//alert("Call Function for: Select-One Field - " + fvFieldObj.length);
	
	//if(!hasOptions(obj)){alert('has no options');} else {alert('has options');}
	
	
	for(opt = 0; opt<fvFieldObj.options.length; opt++)
	{
		//alert('option: ' + fvFieldObj.options[opt].value);
		
		if(fvFieldObj.options[opt].value == fvFieldValue)
		{
			//alert('SHOULD SELECT PREVIOUS');
			fvFieldObj.options[opt].selected = true;
		}
		
	}
	
	//fvFieldObj.value = fvFieldValue;
	//alert('select-one function end');
	return true;
}

function nmFormSetRadioField(fvFieldObj, fvFieldValue)
{
	//alert("Call Function for: RADIO Field - Len: " + fvFieldObj.length);
	
	//if(!hasOptions(obj)){alert('has no options');} else {alert('has options');}
	
	//for(var opt in fvFieldObj)
	for(opt = 0; opt<fvFieldObj.length; opt++) {
		//alert('option: ' + fvFieldObj[opt].value);
		
		if(fvFieldObj[opt].value.toLowerCase() == fvFieldValue.toLowerCase())
		{
			//alert('SHOULD SELECT PREVIOUS');
			fvFieldObj[opt].checked = true;
		}
		
	}
	
	//fvFieldObj.value = fvFieldValue;
	return true;
}


function nmConcatFieldsToField(sourceFieldName, targetFieldID, charToSepWith) {
	//alert('begin');
	
	var fldSrc, fldTgt;
	var chSep = '';
	//alert('vars defined');
	var outString = '';
	
	//fldSrc = document.getElementById(sourceFieldID)
	fldTgt = document.getElementById(targetFieldID);
	fldSrc = eval('fldTgt.form.' + sourceFieldName);
	
	if (charToSepWith) {chSep = charToSepWith;}
	
	//alert('vars set');
	//alert(fldSrc.length);
	
	if (fldSrc.type == undefined){
		for (i=0; i<(fldSrc.length - 1); i++) {
			//alert(i);
			outString = outString + fldSrc[i].value + chSep;
			//alert(outString);
		}
		//l = fldSrc.length;
		outString = outString + fldSrc[(fldSrc.length - 1)].value;
	}
	else {
		//alert('no');
	}
	
	fldTgt.value=outString;
	//alert(end);
}


		/*  END FIELD SETTING FUNCTIONS  */


//	Returns value of single select list when given field ID
function nmGetSelectListValueForID(formFieldID) {
	var outval = 0
	if (document.getElementById(formFieldID)) {
		listObj = document.getElementById(formFieldID)
		for(opt = 0; opt<listObj.options.length; opt++) {
			//alert('option: ' + listObj.options[opt].value);
			if(listObj.options[opt].selected == true)
			{
				outval = listObj.options[opt].value;
			}
			
		}
	}
	return outval;
}


//	Returns value of single select list when passed a field object
function nmGetSelectListValueForObj(formField_obj) {
	var outval = 0;
	if (formField_obj) {
		listObj = formField_obj
		for(opt = 0; opt<listObj.options.length; opt++) {
			//alert('option: ' + listObj.options[opt].value);
			if(listObj.options[opt].selected == true)
			{
				outval = listObj.options[opt].value;
			}
			
		}
	} else {
		outVal = false;
	}
	return outval;
}







//		Let the system know that this kit was successfully loaded
var nmui_kit_loaded = true;
