// This file contains functions that are used by all pages.
// If you need page specific functions then please create a seperate js file for that page.\
// You can use call_center_scripts.js as a template for how to do this.

// Browser specific objects
var isNav = false;
var isIE = false;
var isWin = false;
var isMac = false;
var isOther = false;
var isSupported = false;
var userAgentNum = null;

function Resize()
{
	//Dummy function that should be overwritten by other js include files
}

function BrowserDetection()
{
	//Dectects the Browser version and Platform
	var version = parseInt( navigator.appVersion );
	var userAgentVersion = null;
	var ie = /MSIE \d*\.\d*/i;
	var netscape = /Netscape\/\d*\.\d*/i;
	var numCheck = /\d*\.\d*/;
	
	if( version >= 4)
	{
		if( navigator.appName == "Microsoft Internet Explorer" )
		{
			userAgentVersion = navigator.userAgent.match( ie );
			
			if( userAgentVersion != null )
			{
				userAgentNum = userAgentVersion[0].match( numCheck );
				
				if( userAgentNum >= 5.5 && navigator.platform == "Win32" )
				{
					isSupported = true;
				}
				
				isIE = true;
			}
		}
		else if( navigator.appName == "Netscape" )
		{
			userAgentVersion = navigator.userAgent.match( netscape );
			
			if( userAgentVersion != null )
			{
				userAgentNum = userAgentVersion[0].match( numCheck );
				
				if( userAgentNum >= 7.1 && ( navigator.platform == "Win32" || navigator.platform == "MacPPC" ) )
				{
					isSupported = true;
				}
				
				isNav = true;
			}
		}
		else
		{
			isOther = true;
		}
		
		if( navigator.platform == "Win32" )
		{
			isWin = true;
		}
		else if( navigator.platform == "MacPPC" )
		{
			isMac = true;
		}
	}
}

function GetObj(name)
{
	// Fucntion to get an Object regardless of Browser
	if ( document.getElementById )
	{
		return document.getElementById( name )
	}
	else if ( document.all )
	{
		return document.all[name]
	}
}

function Init()
{
	// Initialize any objects and set up the page in the proper state
	// Overwrite this for page specific Init
}

function OnPageLoad()
{
	// Function called when Body OnLoad Event Occurs
	BrowserDetection();
	Init();
}

function OnPageResize()
{
	// Functions called when page is resized by users
	// Overwrite this for page specific Init
}

function ImageSwap( img, src )
{
	// Swaps image source for a img element
	var obj1 = GetObj( img );
	
	if( obj1.src != null )
		obj1.src = src;
}

function ButtonClick(valueId, value, formId, action)
{  
	ValueSet( valueId, value);
	FormSubmit( formId, action );
}



function ConfirmButtonClick(valueId, value, formId, action, confirmMessage)
{   
	   if( Confirm(confirmMessage) )
	   {
	  		  ButtonClick(valueId, value, formId, action);
	   }
}

function FormSubmit( formID, action )
{
	// Submits the form specified by formID
	var form = GetObj( formID )
	
	if( action != null )
	{
		form.action = action;
	}
	
	form.submit()
}

function ValueSet( inputID, value )
{
	// Sets the form input specified by inputID to value
	var input = GetObj( inputID );
	input.value = value;
}

function Confirm( message )
{
	// sends user a confirm message
	// returns false if user selects no
	return window.confirm( message )
}

function transferElements(sourceList, destinationList, hiddenSourceList, hiddenDestinationList)
{
	var namesToMove = new Array();
    var namesToKeep = new Array();
    var count =0; 
    var keepCount = 0;
  
    var totalElements = sourceList.options.length;
    
    if (transferElements.arguments.length == 5 && transferElements.arguments[4]=='all')
    {
      for (var i = 0; i < totalElements; i++) {
               namesToMove[count] = sourceList.options[i].text;				   	                                 
               ++count;
      }
      for (var i = totalElements; i > -1; i--) {
                sourceList.options[i] = null;				   	                                              
      }
    }
    else
    {
	   for (var i = 0; i < totalElements; i++) {
          if (sourceList.options[i].selected)
			{
               namesToMove[count] = sourceList.options[i].text;				   	                                 
               ++count;
            }
           else 
			{
				namesToKeep[keepCount] = sourceList.options[i].text;
				++keepCount;
			}
	   }// end for each element in the source list
	
	    for (var i = 0; i < totalElements; i++) {       	           
          sourceList.options[i] = null;
       }
       
       namesToKeep.sort();
       for (var i = 0; i < namesToKeep.length; i++) {     
         sourceList.options[i] = new Option(namesToKeep[i], i);              
       }      
       	   
    } // end else transfer all selected
    
    
    var otherListNames = new Array();
	for (var i = 0; i < destinationList.options.length; i++) {
		otherListNames[i] = destinationList.options[i].text;
    }

   total = otherListNames.length;
   for (var i = 0; i < namesToMove.length; i++) {
        otherListNames[total + i] = namesToMove[i];   
        hiddenDestinationList.value +=  namesToMove[i] + ",";
        var index = hiddenSourceList.value.indexOf(namesToMove[i] + ",");
        if (index > -1)
        {
             var sub1 = hiddenSourceList.value.substring(0, index-1);
             var sub2 = hiddenSourceList.value.substring(index+namesToMove[i].length, hiddenSourceList.value.length);
             hiddenSourceList.value = sub1 + sub2;
        }
   } 

	otherListNames.sort();
    for (var i = 0; i < otherListNames.length; i++) {
       destinationList.options[i] = new Option(otherListNames[i], i);
    }
    
}


function EventKill( e )
{
	// Kills Propagation of events in IE and Netscape
	e.cancelBubble = true;
	e.returnValue = false;
	return false;
}

function insert_line_breaks( stringObj, lineBreakPos )
{
	// Function that inserts line breaks "<br>" into a string at the given line break position.
	// This is helpful for breaking lines on non-IE browsers.
	// For IE use the style word-wrap:break-word; and make sure to set the width
	// of the element the string appears in.
	
	var stringLength = string.length;
	var stringWithBreaks = "";
	
	for( i = 0; i + lineBreakPos <= stringLength; i += lineBreakPos )
	{
		stringWithBreaks +=	stringObj.substr( $i, $lineBreakPos) + "<br>";
	}
	
	stringWithBreaks +=	stringObj.substr(lineBreakPos*Math.floor(stringLength/lineBreakPos));
	
	return stringWithBreaks;
}

function expandDescription(callId, fullDescription)
{ 	
	var spanObj = GetObj("descSpan" + callId);
 	var aObj = GetObj("descA" + callId);
 	var html = ""; 
 	
 	var imgObj = GetObj("descImg" + callId);
 	var source = imgObj.src;
 	if (source.indexOf("up") != -1)
 	{
 		html=fullDescription.substring(0, 240) + "... ";
 		arrow = "/images/down.gif";	
    }
    else
    {
    	html=fullDescription;	
	    arrow = "/images/up.gif";
    }    
	
	spanObj.innerHTML = html;
	aObj.onclick = "expandDescription('" + callId + "','" +fullDescription+ "')";
	imgObj.src = arrow;
}

function reloadCatalog(pageNumber)
{
	window.location.href='catalog_content.php?page=' + pageNumber;
}

function submitViewStreamForm(callId, owner, mcastOrFilename, player, audio_only, needsPreface, isLive)
{
	GetObj("callId").value = callId;
	GetObj("owner").value = owner;
	GetObj("mcastOrFilename").value = mcastOrFilename;
	GetObj("player").value = player;
	GetObj("audio_only").value = audio_only;
	GetObj("needsPreface").value = needsPreface;
	GetObj("isLive").value = isLive;
	GetObj("sourceLiveList").value = "yes";	
	FormSubmit("viewLiveConf");				
}

function viewStream(callId, owner, host, mcastOrFilename, player, audio_only, content_encoding_info_id, needsPreface, isLive)
{
         var heightAdj = 82;
		 if( navigator.platform == "MacPPC" ) heightAdj = 130;
         var newHeight = 768 - heightAdj;
		 var newWidth = 1024; 
				
		 if( window.screen.width <= newWidth )
		 {
				newWidth = window.screen.width - 20;
				newHeight = window.screen.height - heightAdj;
		 }
		 else
		 {
				newWidth -= 20;
		 }


   // if we need to query user for password or chat name open the preface window to do so 
   // otherwise open the call viewer directly 
   if(needsPreface == 1)
   {	
		if(isLive)
		{
      		var linkTarget = "live_2.php";
        }
        else
        {        	
        	 var target = "viewCallPreface.php?"; 
             var mcastOrFilenameLine =  "&owner="    + owner  + 
                                        "&filename=" + escape(mcastOrFilename) +
                                        "&content_encoding_info_id=" + content_encoding_info_id;
        
        	 var linkTarget = target +  "call_id="     + callId     +                      
                              "&player="     + player     + 
                              "&audio_only=" + audio_only +
                              mcastOrFilenameLine;
		}                                     
   }
   else 
   {      
        if(isLive)
		{
		    var target = "/call_viewer/index.php?";
	    	var mcastOrFilenameLine = "&mcast="  + mcastOrFilename;		    
		}
		else
		{
		    var target = "viewCallPreface.php?";
		    var mcastOrFilenameLine = "&filename="  + escape(mcastOrFilename) +
                                      "&content_encoding_info_id=" + content_encoding_info_id+
                                      "&authOnly=true";
		}
	    
        var linkTarget = target + "player="  + player + 
                         "&audio_only=" + audio_only +
                         "&call_id="    + callId     +                                   
                         mcastOrFilenameLine         + 
                         "&owner="      + owner;
   } 
   
   if( host != null )
       		linkTarget = "http://" + host + "/" + linkTarget;

   window.open(linkTarget, "_blank", "width=" + newWidth + ",height=" + newHeight + ",left=0,top=0,resizable=yes,status=yes,location=no");
 
  return true;
}

function inputFocus( id )
{
	var focusID = GetObj( id );
	if (focusID != null){
		if (focusID.elements.length > 0){
			focusID.elements[0].focus();
		}
	}
}