// nearly every page uses an ajax object so we'll just make one here so
// all the requests can use the same object
var ajaxRequestHandler = createAjaxObject();

//////////////////////////////////////////////////////////////////////////
// Function: createAjaxObject()
// Accepts: nothing
// Returns: object reference
//
// Creates an object to allow AJAX calls with XMLHttpRequest()
//
//////////////////////////////////////////////////////////////////////////
function createAjaxObject()
{
   var objAjax = null;
   try // for IE 6
   {
      objAjax = new ActiveXObject("Microsoft.XMLHTTP");
   }
   catch(e)
   {
      try // for IE <= 5?
      {
         objAjax = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch(ee)
      {
         try // for IE 7
	      {
            objAjax = new ActiveXObject("MSXML2.XMLHTTP.3.0");
	      }
	      catch(eee)
	      {
	         try // for everything else (don't let IE7 use this, IE7 is broken)
	         {
	            objAjax = new XMLHttpRequest();
	         }
	         catch(eeee)
	         {
               alert("Your browser does not support some of the features on this page. We recommend that you view this page with the latest version of either Mozilla(R) Firefox(R) or Microsoft(R) Internet Explorer(R). If you are already using one of the recommended browsers, please contact customer support by visiting screencast.com/answers and clicking on \"Contact Us\".");
               return null;
	         }
	      }
      }
   }
   return objAjax;
} // end function createAjaxObject()

// Begin help widget function
var helpWidgetQuestionShowing = "";
      
function onQuestionClick( id )
{         
   toShow = document.getElementById( "help_" + id );
   titleId = document.getElementById( "help" + id );
         
   if ( titleId.className != "helpSelected" ) 
   {
       toShow.style.display = "block";
       titleId.className = "helpSelected";
            
       if ( helpWidgetQuestionShowing != "" )
       {
           try
           {
              toHideQuestion = document.getElementById( "help_" + helpWidgetQuestionShowing );
              toHideTitle = document.getElementById( "help" + helpWidgetQuestionShowing ); 
              toHideQuestion.style.display = "none";
              toHideTitle.className = "";
           }
           catch (e) 
           {
             // Yes, empty catches are bad.  IE apparently doesn't like when you try to set the class
             // of items that are no longer displayed even when they still exist.  So... it's here.
           }
       }
            
       helpWidgetQuestionShowing = id;
   }
   else
   {
      toShow.style.display = "none";
      titleId.className = "";
      helpWidgetQuestionShowing = "";
   }
}
      
// Shows the help widget
function showHelp( helpName )
{
   document.getElementById( helpName + "Popup").style.display = "block";
   document.getElementById( helpName + "Label").className = "helpWidgetLabel helpSelected";
} // end function showHelp()

// Hides the help widget
function hideHelp( helpName )
{
   document.getElementById( helpName + "Popup").style.display = "none";
   document.getElementById( helpName + "Label").className = "helpWidgetLabel";
} // end function hideHelp()

// End help widget functions

//Keep Alive
//////////////////////////////////////////////////////////////////////////
// Function: EnableKeepAlive()
// Accepts: nothing
// Returns: nothing
//
// Designed to be called on page load.  Calls ping function at a set interval
// to keep sessions alive.
//
//////////////////////////////////////////////////////////////////////////
function EnableKeepAlive()
{
   setInterval("keepAlive()", 300000);
}

//////////////////////////////////////////////////////////////////////////
// Function: ping
// Accepts: nothing
// Returns: nothing on connection failure, true on everything else
//
// Send an empty ajax call to keep the current session alive.
//
//////////////////////////////////////////////////////////////////////////
function keepAlive()
{
// code to try http or https
// if you only try a relative path and the base href is set to http and this is
// called from https, the browser will block it because it doesn't see the two
// as the same site.
  
   curDomain = document.domain;
   try // try http first
   {
      url = "http://" + curDomain + "/handlers/keepalive.ashx";

      ajaxRequestHandler.open( "POST", url, false );
   }
   catch(e)
   {
      try // now try https
      {
         url = "https://" + curDomain + "/handlers/keepalive.ashx";

         ajaxRequestHandler.open( "POST", url, false );
      }
      catch(e) // return if both fail
      {
        // TODO: return an error here?
        return;
      }
   }
   ajaxRequestHandler.send( "" );
   return true;
} // end function keepAlive()
//End Keep Alive
//
// PLEASE DO NOT RE-ADD scOnLdASPX
// Use EnableKeepAlive

function getQueryStrParam( param ) 
{
   var query = window.location.search.substring(1).split( "&" );
   for ( i = 0 ; i < query.length ; i++ ) 
   {
      var ft = query[i].split( "=" );
      
      if ( ft[0] == param ) 
      {
         return ft[1];
      }
   }
   return "";
}

// Disables the enter key.
// Use by calling "return disableEnterKey( event );"
function disableEnterKey( e )
{
   var k = e.keyCode || e.which;
   return k != 13;
}

/*
* Copies the value in the given element to the clipboard via flash.
*/
function copyToClipboard(elt) 
{
    // Flash version 10 breaks this...do not allow it if that is the current version.
    if (checkFlash(9, 0, 125) != 'needupgrade') {
        return;
    }

    var urlSwf = "/inc/flash/_clipboard.swf";
    var eltNotify = document.getElementById("notifyTextCopied");

    if (eltNotify) {
        elt.onblur =
               function(e) {
                   Element.hide(eltNotify);
                   return true;
               }
        Element.show(eltNotify);

        var xEffect = Effect.Fade(eltNotify, { fps: 75, from: 1.9, to: 0.0, duration: 1.0, queue: 'front' });
        window.status = 'Copied to clipboard';
    }

    // Copy the text inside the text box to the user's clipboard
    var flashcopier = 'flashcopier';
    if (!document.getElementById(flashcopier)) {
        var divholder = document.createElement('div');
        divholder.id = flashcopier;
        document.body.appendChild(divholder);
    }

    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="' + urlSwf + '" FlashVars="clipboard=' + escape(elt.value) + '" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;

    elt.select();

    return true;
 }

/*
* Checks to see if the user is signed in. 
* Returns true if they are, otherwise returns false.
*/
function isUserSignedIn()
{
   var curDomain = document.domain;
   var handlerUrl = "/handlers/signincheck.ashx"; 
   
   try // try http first
   {
      var url = "http://" + curDomain + handlerUrl;

      ajaxRequestHandler.open( "POST", url, false );
   }
   catch(e)
   {
      try // now try https
      {
         var url = "https://" + curDomain + handlerUrl;

         ajaxRequestHandler.open( "POST", url, false );
      }
      catch(e) // return false if both fail.
      {
        return false;
      }
   }
   
   ajaxRequestHandler.send( "" );
   
   if (ajaxRequestHandler.responseText == "true")
   {
      return true;
   }
   else
   {
      return false;
   }
}

/*
* Checks to see if the user is signed in, and redirects them to the signin page if they are not.
* Returns true when signed in and false otherwise.
*/
function isUserSignedInOrRedirectToSignInPage()
{
   if ( !isUserSignedIn() )
   {
      var curDomain = document.domain;
      var signInUrl = "/signin.aspx";
      var redirectUrl = "https://" + curDomain + signInUrl;
      window.location = redirectUrl;
      return false;
   }
   return true;
}

// Safari work around
// Safari won't read CSS in the HEAD tag of lightboxes.
// We can force Safari to read the CSS after loading
// via javascript
function loadSafariStylesheet(stylesheet) 
{
   var ua = navigator.userAgent.toLowerCase();
   // Just detecting Safari for now
   if (ua.indexOf('safari/') != -1) 
   {
       loadStylesheet(stylesheet);
   }}

function loadStylesheet(stylesheet) 
{
    var server = 'http://' + location.host;
    var head = document.getElementsByTagName("head")[0];
    var cssNode = document.createElement('link');
    cssNode.type = 'text/css';
    cssNode.rel = 'stylesheet';
    cssNode.media = 'screen';
    cssNode.href = server + stylesheet;
    head.appendChild(cssNode);
}
