//-----------------------------------------------------
// send an error report
//-----------------------------------------------------
function sendErrorReport()
{
  var manager = GetManager();
  manager.Print('VIDIVIC-LOG: function call: manager.SendLogFile()');
  result = manager.SendLogFile('support@vidivic.com');
  PrintToLogfile('VIDIVIC-LOG: function result: SendLogFile = '+result);
}

//-----------------------------------------------------
// checks for test conference and ends it if needed
//-----------------------------------------------------
function checkForTestConference()
{
  var _action = isdkGetHrefValue('action');
  var _page   = isdkGetHrefValue('page');

  if(_page == 'preferences' && _action == 'testconference')
    return;

  var _intest = isdkGetCookie('Test_Conference');
  if (_intest)
  {
    conf_id = isdkGetCookie ( 'Test_Conference' );
    DoStopTestCall();
  }
}

//-----------------------------------------------------
// checks for active call and ends it
//-----------------------------------------------------
function checkForActiveCall()
{
  var _incall = isdkGetCookie(g_IdCookieName);

  if (_incall)
  {
    conf_id = isdkGetCookie ( g_IdCookieName );
    DoStopCall(conf_id, false, '');
  }
}


//-----------------------------------------------------
// Keep manager alive (set cookie)
//-----------------------------------------------------
function keepAlive()
{
  var manager = GetManager();
  if(!manager)
    return;

  var _keepAlive = isdkGetCookie('keepManagerAlive');

  if(!_keepAlive)
    isdkSetCookie('keepManagerAlive', 'manager_has_been_started');
}

//-----------------------------------------------------
// No longer keep manager alive (delete cookie)
//-----------------------------------------------------
function killAlive()
{
  isdkDeleteCookie('keepManagerAlive');
}


//-----------------------------------------------------
// Print Debug
//-----------------------------------------------------
function PrintToLogfile( errMsg, isErr )
{
	if (typeof(isErr) == "undefined") {
		isErr = false;
	}

	var manager = GetManager();
	if ( ! manager )
		return false;

  if( (isErr) || ( addDebugInformation ) )
     manager.Print( errMsg );

  return;
}


//-----------------------------------------------------
// Get manager object
//-----------------------------------------------------
function GetManager()
{
	if (!window["Intellivic_Manager"])
  {
		return null;
  }
 
	try
	{
	  //PrintToLogfile('VIDIVIC-LOG: function call: manager.Version()');
	  var _managerVersion = Intellivic_Manager.Version();
	  //PrintToLogfile('VIDIVIC-LOG: function result: Version = '+_managerVersion);
	}
	catch (err)
	{
	  return null;
	}

	try {
		var path;
		//PrintToLogfile('VIDIVIC-LOG: function call: manager.Path(1)');
		path =  Intellivic_Manager.Path( 1 );
		//PrintToLogfile('VIDIVIC-LOG: function result: Path = '+path);
		if (path == '') {
			return null;
		}
	}
	catch(err) {
		//Gulp
		return null;
	}
  //the manager was found, we can set the current version cookie
  isdkSetCookie( 'vidivic_user_cab_ver', current_cab_version, 10368000, '/');
  if(myGetElementById('version_warning'))
    myGetElementById('version_warning').style.display = 'none';
  //alert('manager version in get manager functie : '+manager.Version());
	return Intellivic_Manager;
}
//-----------------------------------------------------

//-----------------------------------------------------
// Get calls list
//-----------------------------------------------------
function Calls( manager )
{
	if ( ! manager )
		return null;

  PrintToLogfile('VIDIVIC-LOG: function call: manager.Calls()');
	var variantList = new VBArray ( manager.Calls () );
	PrintToLogfile('VIDIVIC-LOG: function result: Calls = '+variantList.toArray());

	return variantList.toArray ();
}
//-----------------------------------------------------


//-----------------------------------------------------
// Get videowindow Object
//-----------------------------------------------------
function GetVideoWindow()
{
	if ( !window["Intellivic_Video_Window"] )
		return null;

	return Intellivic_Video_Window;
}
//-----------------------------------------------------

//-----------------------------------------------------
// DoStartCall()
//-----------------------------------------------------
function DoStartCall( conf_id )
{
  if(!conf_id)
    return false;

	if ( !window["Intellivic_Video_Window"] )
		return null;

  if ( OnStartAudioVideo( conf_id ) )
  {
	  if(myGetElementById('buttonStop'))
	    myGetElementById('buttonStop').style.display = 'block';
    if(myGetElementById('showLoading'))
      myGetElementById('showLoading').style.display = 'none';

		if( !checkVideoInputDevices() )
		{
  	  if(myGetElementById('showNoVideoMsg'))
        myGetElementById('showNoVideoMsg').style.display = 'block';
      if(myGetElementById('videoResizeControls'))
        myGetElementById('videoResizeControls').style.display = 'none';
    }
    else
    {
      setVideoWindowSize();
      if(myGetElementById('videoWindow'))
        myGetElementById('videoWindow').style.display = 'block';
      if(myGetElementById('videoResizeControls'))
        myGetElementById('videoResizeControls').style.display = 'block';        
    }
    return true;
  }
  else
  {
    return false;
  }

}
//-----------------------------------------------------

//-----------------------------------------------------
//  ()
//-----------------------------------------------------
function DoStartTestConference( conf_id )
{
  if(!conf_id)
    return false;

	if ( !window["Intellivic_Video_Window"] )
		return null;

  if ( OnStartTestConference( conf_id ) )
  {
    if(myGetElementById('videoWindow'))
      myGetElementById('videoWindow').style.display = 'block';
    return true;
  }
  else
  {
    return false;
  }

}
//-----------------------------------------------------

//-----------------------------------------------------
// DoStopCall()
//-----------------------------------------------------
function DoStopCall( conf_id , redirect, page )
{
  if(!conf_id) {
    return;
  }

  if(OnEndCall())
  {
    //we keep the activex alive once it has started
    //iWebLogoff();

    if(redirect)
    {
      //window.location = "index.php?page="+_PAGE_AGENDA;
      window.location = "index.php?page=" + page;
    }
  }
}
//-----------------------------------------------------


//-----------------------------------------------------
// DoStopCall()
//-----------------------------------------------------
function DoStopTestCall(  )
{
  conf_id = isdkGetCookie ( 'Test_Conference' );

  if(!conf_id)
    return false;

	var manager = GetManager();
	if ( ! manager )
		return false;

  PrintToLogfile('VIDIVIC-LOG: function call: manager.ReleaseAvForCall('+conf_id+')');
	var result = manager.ReleaseAvForCall ( conf_id );
	PrintToLogfile('VIDIVIC-LOG: function result: manager.ReleaseAvForCall() = '+result, true );
	if ( ! ( ( result == ISDKERROR_NONE ) || ( result == ISDKERROR_UNKNOWN_ID ) ) )
	{
		alert ( 'Failed to end call. Error: ' + showErrorMsg( result ) );
		return false;
	}
	else
  {
    CleanupCall(conf_id);
    return true;
  }

}
//-----------------------------------------------------

//-----------------------------------------------------
// OnEndCall()
//-----------------------------------------------------
function OnEndCall ()
{
	var callId = isdkGetCookie ( g_IdCookieName );
	if ( ( ! callId ) || ( callId.length == 0 ) )
		return;

	var manager = GetManager();
	if ( ! manager )
		return;

  PrintToLogfile('VIDIVIC-LOG: function call: manager.ReleaseAvForCall('+callId+')');
	var result = manager.ReleaseAvForCall ( callId );
	PrintToLogfile('VIDIVIC-LOG: function result: manager.ReleaseAvForCall('+callId+') = ' + result, true);

	if ( ! ( ( result == ISDKERROR_NONE ) || ( result == ISDKERROR_UNKNOWN_ID ) ) )
		alert ( 'Failed to end call. Error: ' + showErrorMsg( result ) );
	else
  {
    isdkDeleteCookie ( g_IdCookieName );
    if(myGetElementById('videoWindow'))
      myGetElementById('videoWindow').style.display = 'none';
	  if(myGetElementById('showNoVideoMsg'))
      myGetElementById('showNoVideoMsg').style.display = 'none';
  }
	return true;
}
//-----------------------------------------------------


//-----------------------------------------------------
// Starts an audio and video call
//-----------------------------------------------------
function OnStartAudioVideo ( conf_id )
{
	var manager = GetManager ();
	if ( ! manager )
		return false;

	if ( ! conf_id )
		return false;

	// check if there is not already another call/session with another participant
	var ck = isdkGetCookie ( g_IdCookieName );

	if (( ck ) && ( ck != conf_id) )
	{
	  PrintToLogfile('VIDIVIC-LOG: function call: manager.AvStatusForCall('+ck+')');
	  callsstat = manager.AvStatusForCall( ck );
	  PrintToLogfile('VIDIVIC-LOG: function result: manager.AvStatusForCall('+ck+') = ' + callsstat);
	  if ( (callsstat>=0) && (callsstat != ISDKCALLSTATUS_DISCONNECTED ) )
	  {
		  alert ( 'Cannot start an audio/video call with because a call/session is already open');
		  return false;
	  }
	  else isdkDeleteCookie ( g_IdCookieName );
	}
	PrintToLogfile('VIDIVIC-LOG: function call: manager.CreateCall('+conf_id+')');
	var callId = manager.CreateCall ( conf_id );
	PrintToLogfile('VIDIVIC-LOG: function result: manager.CreateCall('+conf_id+') = ' + callId);
	if ( ! callId )
	{
	  PrintToLogfile('VIDIVIC-LOG: function call: manager.LastError()');
		window.location = 'index.php?page='+_PAGE_AGENDA+'&conf='+conf_id+'&err='+manager.LastError();
		PrintToLogfile('VIDIVIC-LOG: function result: manager.LastError() = ' + manager.LastError());
		return false;
	}
  
  PrintToLogfile('VIDIVIC-LOG: function call: manager.VideoInputs()');
  var variantList = new VBArray ( manager.VideoInputs () );
  PrintToLogfile('VIDIVIC-LOG: function result: manager.VideoInputs() = '+ variantList, true);
  var videoinputs = variantList.toArray ();

  if ( videoinputs.length ) // We have a camera
  {
  	PrintToLogfile('VIDIVIC-LOG: function call: manager.StartAudioVideoForCall('+callId+')');
  	var result = manager.StartAudioVideoForCall ( callId );
  	PrintToLogfile('VIDIVIC-LOG: function result: manager.StartAudioVideoForCall('+callId+') = '+ result, true);
  }
  else // We don't have a camera
  {
  	PrintToLogfile('VIDIVIC-LOG: function call: manager.StartAudioForCall('+callId+')');
  	var result = manager.StartAudioForCall ( callId );
  	PrintToLogfile('VIDIVIC-LOG: function result: manager.StartAudioForCall('+callId+') = '+ result, true); 
    /*var result = manager.StartAudioVideoForCall ( callId );
  	PrintToLogfile('VIDIVIC-LOG: function result: manager.StartAudioForCall('+callId+') = '+ result, true);  
    manager.SetPictureVideoInputForCall(callId,"c:\\foto.jpg");*/
  }

	if ( result != ISDKERROR_NONE )
	{
		alert ( 'Failed to start call. Error: ' + showErrorMsg (result) );
		return false;
	}

	isdkSetCookie ( g_IdCookieName, callId, 1 );
	return true;
}
//-----------------------------------------------------

//-----------------------------------------------------
// Starts the test conference
//-----------------------------------------------------
function OnStartTestConference ( conf_id )
{
	var manager = GetManager ();
	if ( ! manager )
		return false;

	if ( ! conf_id )
		return false;

	var ck = isdkGetCookie ( g_IdCookieName );

	if (( ck ) && ( ck != conf_id) )
	{
	  PrintToLogfile('VIDIVIC-LOG: function call: manager.AvStatusForCall('+ck+')');
	  callsstat = manager.AvStatusForCall( ck );
	  PrintToLogfile('VIDIVIC-LOG: function result: manager.AvStatusForCall('+ck+') = ' + callsstat);

	  if ( (callsstat>=0) && (callsstat != ISDKCALLSTATUS_DISCONNECTED ) )
	  {
		  alert ( 'Cannot start an audio/video call with because a call/session is already open');
		  return false;
	  }
	  else isdkDeleteCookie ( g_IdCookieName );
	}
	PrintToLogfile('VIDIVIC-LOG: function call: manager.CreateCall('+conf_id+')');
	var callId = manager.CreateCall ( conf_id );
	PrintToLogfile('VIDIVIC-LOG: function result: manager.CreateCall('+conf_id+') = ' + callId);
	if ( ! callId )
	{
		alert('Could not create the call');
		return false;
	}
  
  PrintToLogfile('VIDIVIC-LOG: function call: manager.VideoInputs()');
  var variantList = new VBArray ( manager.VideoInputs () );
  PrintToLogfile('VIDIVIC-LOG: function result: manager.VideoInputs() = '+ variantList, true);
  var videoinputs = variantList.toArray ();

  if ( videoinputs.length ) // We have a camera
  {
  	PrintToLogfile('VIDIVIC-LOG: function call: manager.StartAudioVideoForCall('+callId+')');
  	var result = manager.StartAudioVideoForCall ( callId );
  	PrintToLogfile('VIDIVIC-LOG: function result: manager.StartAudioVideoForCall('+callId+') = '+ result, true);
  }
  else // We don't have a camera
  {
  	PrintToLogfile('VIDIVIC-LOG: function call: manager.StartAudioForCall('+callId+')');
  	var result = manager.StartAudioForCall ( callId );
  	PrintToLogfile('VIDIVIC-LOG: function result: manager.StartAudioForCall('+callId+') = '+ result, true);  
  }  
  
	if ( result != ISDKERROR_NONE )
	{
		alert ( 'Failed to start call. Error: ' + showErrorMsg (result) );
		return false;
	}

	isdkSetCookie ( g_IdCookieName, callId, 1 );
	return true;
}
//-----------------------------------------------------

//-----------------------------------------------------
// Show the ui components for an audio and video call
// @param callId id of the call
//-----------------------------------------------------
function ShowAudioVideoCall ( callId )
{
	var manager = GetManager();
	if ( ! manager )
		return;

	// show video stream
	var videoWindow = GetVideoWindow();
	if ( ! videoWindow )
		return;
	var videoWindowId = videoWindow.WindowId ();
	PrintToLogfile('VIDIVIC-LOG: function call: manager.SetVideoWindowIdForCall('+callId+','+videoWindowId+')');
	var result = manager.SetVideoWindowIdForCall ( callId, videoWindowId );
	PrintToLogfile('VIDIVIC-LOG: function result: manager.SetVideoWindowIdForCall('+callId+','+videoWindowId+') = ' + result, true);
	if ( result != ISDKERROR_NONE )
	{
		alert ( 'Failed to set video window. Error: ' + showErrorMsg ( result ) ) ;
	  CleanupCall ( callId );
	  return false;
	}
	return true;
}
//-----------------------------------------------------

//-----------------------------------------------------
// Destroys call
//-----------------------------------------------------
function CleanupCall ( callId )
{
 	var manager = GetManager ();
	if ( ! manager )
		return;

  PrintToLogfile('VIDIVIC-LOG: function call: manager.DestroyCall('+callId+')');
	var result = manager.DestroyCall ( callId );
	PrintToLogfile('VIDIVIC-LOG: function result: manager.DestroyCall('+callId+') = ' + result, true);
  
  isdkDeleteCookie( g_IdCookieName );
	if( ( result != ISDKERROR_NONE ) && ( result != ISDKERROR_UNKNOWN_ID) )
	{
	
  	alert ( 'Failed to destroy call with ' + callId );
		return;
	}
}
//-----------------------------------------------------


//-----------------------------------------------------
// Check if the user is busy with another contact
//-----------------------------------------------------
function CheckBusy ( contactURI )
{
	var callId = isdkGetCookie ( g_IdCookieName );
	if (( ! callId ) || ( callId.length== 0 ) )
		return false;

	if ( callId == contactURI ) // same contact, we're not busy for him/her
		return false;

	// callid differs
	return true;
}
//-----------------------------------------------------



//-----------------------------------------------------
// Load a call if there is an active call
//-----------------------------------------------------
function LoadCall ( bLoadCall )
{
	var manager = GetManager ();
	if ( ! manager )
		return;

	// check cookie for call
	var callId  = isdkGetCookie ( g_IdCookieName );
	var hascall = false;

	if (( ! callId ) || ( callId.length == 0 ) )
	{
		// check if there is a connected call...
		var callArray = Calls ( manager );
		for ( i = 0; i < callArray.length; i++ )
		{
		  PrintToLogfile('VIDIVIC-LOG: function call: manager.AvStatusForCall('+callArray[i]+')');
			var callStatus = manager.AvStatusForCall( callArray[i] );
			PrintToLogfile('VIDIVIC-LOG: function result: manager.AvStatusForCall('+callArray[i]+') = ' + callStatus);

			if ( callStatus == ISDKCALLSTATUS_CONNECTED )
			{
				callId  = callArray[i];
				hascall = true;
				break;
			}
		}
	}
  else
  {
    PrintToLogfile('VIDIVIC-LOG: function call: manager.AvStatusForCall('+callId+')');
    hascall = ( manager.AvStatusForCall( callId ) == ISDKCALLSTATUS_CONNECTED ) ;
    PrintToLogfile('VIDIVIC-LOG: function result: manager.AvStatusForCall('+callId+') = ' + manager.AvStatusForCall( callId ));
  }

  if ( hascall )
  {
    if ( bLoadCall )
    {
	    if( ShowAudioVideoCall( callId ) )
	    {
    		if( !checkVideoInputDevices() )
    		{
      	  if(myGetElementById('showNoVideoMsg'))
      	  {
            myGetElementById('showNoVideoMsg').style.display = 'block';
          }
        }
        else
        {
          setVideoWindowSize();
          if(myGetElementById('videoWindow'))
            myGetElementById('videoWindow').style.display = 'block';
        }

        return callId;
	    }
	    else
	    {
	      alert('could not show call');
	      return false;
	    }
	  }
	}
	else
  {
    isdkDeleteCookie ( g_IdCookieName );
    return false;
  }

}
//-----------------------------------------------------


//-----------------------------------------------------
// Load test conference if there it is active
//-----------------------------------------------------
function LoadTestCall ( callId )
{
	var manager = GetManager ();
	if ( ! manager )
		return false;

  PrintToLogfile('VIDIVIC-LOG: function call: manager.AvStatusForCall('+callId+')');
  hascall = ( manager.AvStatusForCall( callId ) == ISDKCALLSTATUS_CONNECTED ) ;
  PrintToLogfile('VIDIVIC-LOG: function result: manager.AvStatusForCall('+callId+') = ' + manager.AvStatusForCall( callId ));

  if ( hascall )
  {
    if( ShowAudioVideoCall( callId ) )
    {
      if(myGetElementById('videoWindow'))
        myGetElementById('videoWindow').style.display = 'block';
      return callId;
    }
    else
    {
      alert('could not show call');
      return false;
    }
	}
	else
  {
    isdkDeleteCookie('Test_Conference');
    window.location = 'index.php?page='+_PAGE_PREFERENCES+'&action=testconference';
    return false;
  }

}
//-----------------------------------------------------

//-----------------------------------------------------
// Gets the javascript SDK 
// (can be replaced in a future release by intellivic_api.sdk.get_instance()
//-----------------------------------------------------
function GetJavascriptSdk()
{
  try 
  {
    var manager = GetIntellivicJavascriptManager();
    var sdk = intellivic_manager.newsdk( manager );
    var sdk_id = manager.GetValueForKey('sdk_id');
    
    if( sdk_id == '')
    {
      sdk._create();
      manager.SetValueForKey('sdk_id',sdk.id);
    }
    else
      sdk.id = sdk_id;
    
    return sdk;
    
  } catch(exc )
  {//manager not yet created!
    return;
  }
}
//-----------------------------------------------------

function getHttpsEnabled()
{
  var httpsEnabled = isdkGetCookie('https_enabled');
  
  if( httpsEnabled == "true")
    return true;
  
  /*if(isdkGetCookie('https_enabled_automatic') == "true")
    return true;*/
  
  return false;
}

function getHttpsEnabledAutomaticaly()
{
  /*var httpsEnabledAutomatic = isdkGetCookie('https_enabled_automatic');
  
  if( httpsEnabledAutomatic == "true")
    return true;*/
  
  return false;
}
