
//This function is already present in the jsUtils.js file.
function VideoProfiles( manager )
{
	if (!manager) {
    return null;
  }
  
  PrintToLogfile('VIDIVIC-LOG: function call: manager.VideoProfiles()');
  var variantList = new VBArray( manager.VideoProfiles() );
  PrintToLogfile('VIDIVIC-LOG: function result: VideoProfiles = ' + variantList.toArray());
  return variantList.toArray();
}

function ClearComboBox( combobox )
{		
  var firstChild = combobox.firstChild;
  while ( firstChild ) {
  	combobox.removeChild ( firstChild );
  	firstChild = combobox.firstChild;
  }
}	

function VideoInputs( manager )
{
	if (!manager) {
		return null;
	}
	
	PrintToLogfile('VIDIVIC-LOG: function call: manager.VideoInputs()');
	var variantList = new VBArray( manager.VideoInputs() );
	PrintToLogfile('VIDIVIC-LOG: function result: VideoInputs = ' + variantList.toArray());
	return variantList.toArray();
}


function UpdateDefaultVideoProfile( combobox )
{				
	var _combo = myGetElementById(combobox);
	if (!_combo) {
		return;
	}			
	
	var manager = GetManager();	
	if (!manager) {
	  return null;
	}					

  //profile_id = ItemIndex(_combo);		
  profile_id = _combo.value;    
	
	PrintToLogfile('VIDIVIC-LOG: function call: manager.SetDefaultVideoProfile(' + profile_id + ')');
  error = manager.SetDefaultVideoProfile( profile_id );          
  PrintToLogfile('VIDIVIC-LOG: function result: SetDefaultVideoProfile = ' + error, true);
  if (error != ISDKERROR_NONE) {
  	alert( showErrorMsg( error ) );
  }      
}	

function UpdateDefaultInputDevice( combobox )
{		
	var _combo = myGetElementById(combobox);
	if (!_combo) {
		return;
	}		
		
	var manager = GetManager();	
	if (!manager) {
		return null;
	}	
	
	device_id = _combo.value;
	
	//alert(device_id);
	
	PrintToLogfile('VIDIVIC-LOG: function call: manager.SetCurrentVideoInput(' + device_id + ')');
	error = manager.SetCurrentVideoInput( device_id );
	PrintToLogfile('VIDIVIC-LOG: function result: SetCurrentVideoInput = ' + error, true);
	if (error != ISDKERROR_NONE) {
	  alert( showErrorMsg( error ) );
	}
	
	
	
	/*
	var manager = GetManager ();
	var inputList = isdkGetElementById ( combobox );
	if ( ( !manager ) || ( ! inputList ) )
	  return;

	var selectedInput = inputList.value;
	if ( ( !selectedInput ) || ( selectedInput.length == 0 ) )
	{
		alert ( 'Please select a valid video input device' );
		return;
	}

  alert(selectedInput);
	var result = manager.SetCurrentVideoInput ( selectedInput );
	if ( result != ISDKERROR_NONE )
	  alert ('Failed to set the video input to ' + selectedInput + '. Error: ' + isdkStrError ( result ) );
	//else
	 // window.location.reload();	
  */	 
}	

function loadVideoInputDevices( combobox )
{
	var manager = GetManager();	
	if (!manager) {
		return;
	}		
	
	var _combo = myGetElementById( combobox);
	if (!_combo) {
		return;
	}				
	
	ClearComboBox(_combo);	
	
	var devices = VideoInputs( manager );
  //alert('devices : ' + devices.length);
	if (!devices || devices.length == 0) {
		return;
	}						  

  PrintToLogfile('VIDIVIC-LOG: function call: manager.CurrentVideoInput()');
	selectedDevice = manager.CurrentVideoInput();						
	PrintToLogfile('VIDIVIC-LOG: function result: CurrentVideoInput = ' + selectedDevice.toString());

	PrintToLogfile('VIDIVIC-LOG: function call: manager.SetCurrentVideoInput(' + selectedDevice + ')');
	error = manager.SetCurrentVideoInput( selectedDevice );
	PrintToLogfile('VIDIVIC-LOG: function result: SetCurrentVideoInput = ' + error, true);

	
  for (i = 0; i < devices.length; i++) {
  	if (devices[i].toLowerCase() != 'default') {
    
      //if( !devices[i] )
      //  alert('devices[i] is empty');
      //else
      //  alert('device name : ' + devices[i]);
    
		  var device = document.createElement( 'option' );								
		  device.value = devices[i];															
		  device.selected = device.value == selectedDevice;								
		  device.innerHTML = ' ' + device.value;						
		  _combo.appendChild( device );
		}  
	}		
		
	return selectedDevice;  		
}

function checkVideoInputDevices( )
{
  //return true;
  
	var manager = GetManager();	
	if (!manager) {
		return;
	}		
	
	var devices = VideoInputs( manager );
	if (!devices || devices.length == 0) {
		return false;
	}						  
  return true;

}

function loadVideoProfiles( combobox )
{							  
  var _combo = myGetElementById( combobox );
  if (!_combo) {
  	return;
  }	
	
	var manager = GetManager();	
	if (!manager) {
		return;
	}				
	
	ClearComboBox(_combo);	
	
	var profiles = VideoProfiles( manager );
	if (!profiles || profiles.length == 0) {
    return;
  }		    
    	
  PrintToLogfile('VIDIVIC-LOG: function call: manager.DefaultVideoProfile()');  	
	selectedProfileId = manager.DefaultVideoProfile();				  
	PrintToLogfile('VIDIVIC-LOG: function result: DefaultVideoProfile = ' + selectedProfileId.toString());  	
	
	for (i = 0; i < profiles.length; i++) {
		var profile = document.createElement( 'option' );
		profile.value = profiles[i];											
		profile.selected = profile.value == selectedProfileId;						
		PrintToLogfile('VIDIVIC-LOG: function call: manager.NameForVideoProfile(' + profile.value + ')');  	
		var profileName = manager.NameForVideoProfile( profile.value );
		PrintToLogfile('VIDIVIC-LOG: function result: NameForVideoProfile = ' + profileName);  	
		profile.innerHTML = ' ' + profileName;						
		_combo.appendChild( profile );
	}		
		
	return selectedProfileId;  	
}