/*
   Title     :  Sample Template JavaScript Library
   Author    :  Virage Inc.
   Generator :  Virage Solution Server
   Copyright :  Copyright 2002 by Virage Inc.
   Date      :  2002-01-24T15:57:34
*/

function set_hidden_search_fields()
{
  var which = document.searchform.chooser_menu.selectedIndex;
  var chooser_menu = document.searchform.chooser_menu[which].value;
  if (chooser_menu == "AllFiles")
  {
    document.searchform.search_type.value = 'videos,docs';
    document.searchform.query1.value = '';
  }
  else if (chooser_menu == "AllClips")
  {
    document.searchform.search_type.value = 'clips';
    document.searchform.query1.value = '';
  }
  else if (chooser_menu == "AllFilesClips")
  {
    document.searchform.search_type.value = 'all';
    document.searchform.query1.value = '';
  }
  else if (chooser_menu == "VideoFiles")
  {
    document.searchform.search_type.value = 'videos,docs';
    document.searchform.query1.value = 'VIR_CAT_VIDEO';
  }
  else if (chooser_menu == "VideoClips")
  {
    document.searchform.search_type.value = 'clips';
    document.searchform.query1.value = 'VIR_CAT_VIDEO';
  }
  else if (chooser_menu == "AudioFiles")
  {
    document.searchform.search_type.value = 'videos,docs';
    document.searchform.query1.value = 'VIR_CAT_AUDIO';
  }
  else if (chooser_menu == "AudioClips")
  {
    document.searchform.search_type.value = 'clips';
    document.searchform.query1.value = 'VIR_CAT_AUDIO';
  }
  else if (chooser_menu == "WordProcessorFiles")
  {
    document.searchform.search_type.value = 'docs';
    document.searchform.query1.value = 'VIR_CAT_DOC';
  }
  else if (chooser_menu == "Presentations")
  {
    document.searchform.search_type.value = 'docs';
    document.searchform.query1.value = 'VIR_CAT_PRESENTATION';
  }
  else if (chooser_menu == "Spreadsheets")
  {
    document.searchform.search_type.value = 'docs';
    document.searchform.query1.value = 'VIR_CAT_SPREADSHEET';
  }
  else if (chooser_menu == "PDFs")
  {
    document.searchform.search_type.value = 'docs';
    document.searchform.query1.value = 'VIR_CAT_PDF';
  }
  else if (chooser_menu == "HTMLFiles")
  {
    document.searchform.search_type.value = 'docs';
    document.searchform.query1.value = 'VIR_CAT_HTML';
  }
  else if (chooser_menu == "TextFiles")
  {
    document.searchform.search_type.value = 'docs';
    document.searchform.query1.value = 'VIR_CAT_TEXT';
  }
  else if (chooser_menu == "Images")
  {
    document.searchform.search_type.value = 'docs';
    document.searchform.query1.value = 'VIR_CAT_IMAGE';
  }
  else if (chooser_menu == "Other")
  {
    document.searchform.search_type.value = 'docs';
    document.searchform.query1.value = 'VIR_CAT_OTHER';
  }
  else
  {
    document.searchform.search_type.value = 'all';
    document.searchform.query1.value = '';
  }
}

function viewer(URI)
{
  if (navigator.userAgent.indexOf("Mac") != -1)
  {
		//MAC CODE GOES HERE  
		 if (document.layers)
		{
		  var viewer = window.open(URI, 'viewer',
			'directories=no,height=471,location=no,menubar=no,resizable=no,' +
			'scrollbars=auto,status=no,toolbar=no,width=836,' +
			'screenX=150,screenY=150,left=150,top=150');
		  viewer.focus();
		}
		else
		{
		   var viewer = window.open(URI, 'viewer',
			'directories=no,height=452,location=no,menubar=no,resizable=no,' +
			'scrollbars=auto,status=no,toolbar=no,width=839,' +
			'screenX=150,screenY=150,left=150,top=150');
		  viewer.focus();
		}
  }
  
  if (navigator.userAgent.indexOf("Mac") == -1) 
  { 
    //PC CODE GOES HERE
	 var viewer = window.open(URI, 'viewer',
    'directories=no,height=468,location=no,menubar=no,resizable=no,' +
    'scrollbars=auto,status=no,toolbar=no,width=837,' +
    'screenX=150,screenY=150,left=150,top=150');
  	viewer.focus();
  }  
}

function email_viewer(URI)
{
  var email_viewer = window.open(URI, 'email_viewer',
    'directories=no,height=580,location=no,menubar=no,resizable=yes,' +
    'scrollbars=auto,status=yes,toolbar=no,width=470,' +
    'screenX=10,screenY=0,left=10,top=0');
  email_viewer.focus();
}

function label_editor(URI)
{
  var label_editor = window.open(URI, 'label_editor',
    'directories=no,height=480,location=no,menubar=no,resizable=yes,' +
    'scrollbars=yes,status=yes,toolbar=no,width=640,' +
    'screenX=10,screenY=10,left=10,top=10');
  label_editor.focus();
}

function editor_storyboard(URI)
{
  var editor_storyboard = window.open(URI, 'editor_storyboard',
    'directories=no,height=480,location=no,menubar=no,resizable=yes,' +
    'scrollbars=yes,status=yes,toolbar=no,width=640,' +
    'screenX=30,screenY=30,left=30,top=30');
  editor_storyboard.focus();
}

function text_editor(URI)
{
  var text_editor = window.open(URI, 'text_editor',
    'directories=no,height=650,location=no,menubar=no,resizable=yes,' +
    'scrollbars=yes,status=yes,toolbar=no,width=640,' +
    'screenX=10,screenY=0,left=10,top=0');
  text_editor.focus();
}

function help(URI)
{
  var help_offset = screen.width - 470;
  var help = window.open(URI, 'help',
    'directories=no,height=600,location=yes,menubar=yes,resizable=yes,' +
    'scrollbars=yes,status=yes,toolbar=yes,width=450,' +
    'screenX=' + help_offset + ',screenY=5,' +
    'left='    + help_offset + ',top=5');
  help.focus();
}

function ms2HMS(ms)
{
  var SS = ms / 1000;
  var MM = Math.floor(SS / 60);
  var HH = Math.floor(MM / 60);

  SS = SS - ( MM * 60 );
  MM = MM - ( HH * 60 );
  SS = Math.round(SS * 100) / 100;

  if ( HH < 10 ) HH = '0' + HH;
  if ( MM < 10 ) MM = '0' + MM;
  if ( SS < 10 ) SS = '0' + SS;

  return HH + ':' + MM + ':' + SS;
}

function URI_authority()
{
  var from = document.URL.indexOf("://") + 3;
  var to   = document.URL.indexOf("/", from);
  return document.URL.substring(from, to);
}


function validate_entry_field (form, value, fieldname, fieldtype, maxchars)
{
  var orig_name = 'original_'+fieldname;

  if (value.length > maxchars)
  {
    alert('String has ' + value.length + ' chars, max is ' + maxchars + ', resetting value');
    form.elements[fieldname].value = form.elements[orig_name].value;
    return false;
  }

  if (fieldtype == "int" && !isSignedInteger(value, true) )
  {
    alert(value + " is not a valid integer");
    form.elements[fieldname].value = form.elements[orig_name].value;
    return false;
  }

  if (fieldtype == "float" && !isSignedFloat(value, true) )
  {
    alert(value + " is not a valid floating point number");
    form.elements[fieldname].value = form.elements[orig_name].value;    
    return false;
  }

  if (fieldtype == "date" && !isDateString(value, true))
  {
    var msg = value + " is not a valid date";
    if (value.length != 10)
    {
      msg += ", please use format YYYY-MM-DD or MM-DD-YYYY";
    }
    alert(msg);
    form.elements[fieldname].value = form.elements[orig_name].value;
    return false;
  }

  setLabelChanged( form, fieldname );
  return true;
}

function isDateString(value, eok)
{
  var result = true;

  if (value.length == 0)
  {
    result = eok;
  }
  else if (value.length == 10)
  {
    var year  = "";
    var month = "";
    var day   = "";

    if ( (value.charAt(4) == "-")  &&
         (value.charAt(7) == "-")  )
    {
      result = isDate(value.substring(0, 4), value.substring(5, 7), value.substring(8, 10));
    }
    else if ( (value.charAt(2) == "-")  &&
              (value.charAt(5) == "-")  )
    {
      result = isDate(value.substring(6, 10), value.substring(0, 2), value.substring(3, 5));
    }
    else  // not YYYY-MM-DD or MM-DD-YYYY
    {
      result = false;
    }
  }
  else // string is not of length 10 or empty
  {
    result = false;
  }
  
  return result;
}

function move_menu_selection_to_entry_field(form, index, fieldname)
{
    var menu_fieldname = fieldname+"__select";
    var value = form.elements[menu_fieldname].options[index].text;
    form.elements[fieldname].value = value;
    setLabelChanged( form, fieldname );
}

function setLabelChanged( this_form, field_name )
{
    this_form.elements[ "changed_"+field_name ].value="1";
}

/*****************************************************************************
 *  Function:  vss_isEmailAddress
 *  Description:  This function is used in form field validation to 
 *     test whether or not a given string is an email address.  
 *  Arguments: a string
 *  Return Value: true or false
 *****************************************************************************/
function vss_isEmailAddress(addrStr) 
{
    var addrPattern = /^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+\.[a-zA-Z0-9_\-]+$/;
    return( addrPattern.test(addrStr) );
}

/*****************************************************************************
 *  Function:  non_Alph_Num_Space_Dash_Underscore
 *  Description:  This function is used in form field validation to 
 *     test whether or not a given string contains any characters that are
 *     not letters, numbers, dashes, spaces, or underscores.  
 *  Arguments: a string
 *  Return Value: true or false
 *****************************************************************************/
function non_Alph_Num_Space_Dash_Underscore(inStr) 
{
	var thePattern = /[^a-zA-Z0-9_\-\s]/;
    if (inStr)
    {
	    return( thePattern.test(inStr) );
    }
    return true;
}

/*****************************************************************************
 *  Function:  non_Alph_Num_Dash_Underscore
 *  Description:  This function is used in form field validation to 
 *     test whether or not a given string contains any characters that are
 *     not letters, numbers, dashes, or underscores.  
 *  Arguments: a string
 *  Return Value: true or false
 *****************************************************************************/
function non_Alph_Num_Dash_Underscore(inStr) 
{
	var thePattern = /[^a-zA-Z0-9_\-]/;
    if (inStr)
    {
	    return( thePattern.test(inStr) );
    }
    return true;
}

/*****************************************************************************
 *  Function:		checkPlayCookie
 *
 *  Description:
 *	Searches  through document.cookie for the playback cookie. If it finds 
 *      Cookie then send you to correct player page or else to preference page  
 *  
 *  Arguments:
 *				url - Url of the page to send to after appending 
 *				      Correct template name	
 *				cookieName - this is the account name
 *
 *  Return Value: none
 	
 *****************************************************************************/

function checkPlayCookie(player_url, pref_url, cookieName)
{
	var allcookies = document.cookie;
	var prefName = "playprefs";
	var pos = allcookies.indexOf(cookieName+"=");
	var today, expires, player ;
	var str_value;
	var url = pref_url;

	
	// Default template for player preferences
	var pref_tmpl = 'playprefs.tmpl';
		
	if ( pos == -1 )
	{
		// the cookie was not set - goto preferences page

		// create anempyt cookie
		today = new Date;
		today.setFullYear(2020);
		expires = today.toGMTString();
		document.cookie = cookieName + "=" + prefName + ";expires=" + expires;
	
		if ( ( str_value = extractValue(player_url, "pref_tmpl=" ) )  ) {
			player = str_value;
		}		
		else {
			player = pref_tmpl;
		}
			        
	} 
	else 
	{
		// the cookie was set - see if the value is valid
		player = getCookie(cookieName);
		
		/* Get a handle on the cookie value, and test if it is the default playprefs.
		   If a user launches the player, and fails to choose a format/bitrate, the cookie value
		   is set to playprefs.*/
		   
		if(player == "playprefs")
		{
			if ( ( str_value = extractValue(player_url, "pref_tmpl=" ) )  ) {
				player = str_value;
		        }
			else {
				player = pref_tmpl;
			}
		}
		else
		{	
			var text_part, number_part;
		        var asf_pattern = /asf/i;

			// reverse player string i.e from 56asf to asf56
			var player_length = player.length;
			number_part = player.substr( 0 , ( player_length -  3 ) );
			text_part = player.substr( ( player_length - 3 ) , 3 ); 
			player = text_part + number_part ;

			// Use exact player template if specified
		        if ( ( str_value = extractValue(player_url, player + "_tmpl=" ) )  ) {
				player = str_value;
			}	
			else if ( asf_pattern.test(player) )	{
			     //  use asf template if specified
			     url = player_url;
			     if ( ( str_value = extractValue(player_url,  "asf_tmpl=" ) )  ) 
			     {
				  player = str_value;

			     }
                             // if nothing specified default to windows media template
		             else {
			     	player =  "viewer_mplayer.tmpl";
			     }
		        }
			else {
			     //  use ram template if specified
			     url = player_url;
			     if ( ( str_value = extractValue(url,  "ram_tmpl=" ) )  ) 
			     { 
				  player = str_value;
			     }
                             // if nothing specified default to real media template
		             else {
			     	player =  "viewer_realplayer.tmpl";
			     }
			}
 
		}
	
	}
	//alert(url + ";template=" + player );
	self.location.href = url + ";template=" + player;
}

/*****************************************************************************
 *  Function:		extractValue
 *
 *  Description:
 *	Gets the cookie value from the variable allcookies.
 *
 *  Arguments:
 *				Url - Url to look for the value
 *				Search String - String to look for		
 *
 *  Return Value: 
 *				the cookie value, which is equal to the bitrate + 
 *				format. e.g. 56asf		
 *
 *****************************************************************************/

function extractValue(url, search_str)
{
	var search_str_length, url_length;
	var end_pos, count , cur_char, str_value ;
	search_str_length = search_str.length;
	url_length = url.length;

	var pos = url.indexOf(search_str);
	if ( pos == -1 ) {
		return false;
	}
	else  {
		for ( count = ( pos + search_str_length ); count <  url_length; count++ ) {
			cur_char = url.charAt(count);
			//alert(cur_char);
                        if ( ( cur_char == ";" ) || ( cur_char == "&" ) ) {
			    break;
			}
		}
		if ( count > ( pos + search_str_length + 1 ) ) {
			str_value = url.substr( ( pos + search_str_length) , ( count - pos - search_str_length ) );
			alert ( "String : " + str_value );
			return ( str_value  );
		}
		else {
	          return false;
		}
		
	}

}


/*****************************************************************************
 *  Function:		getCookie
 *
 *  Description:
 *	Gets the cookie value from the variable allcookies.
 *
 *  Arguments:
 *				cookieName - the account name
 *
 *  Return Value: 
 *				the cookie value, which is equal to the bitrate + 
 *				format. e.g. 56asf		
 *
 *  Side Effects: 
 *  This function is called within the checkPlayCookie function, as well
 *  as in the setRadioPrefs function to retrieve the value of the
 *  previously chosen format and bitrate.
 *
 *****************************************************************************/
	
function getCookie(cookieName)
{
	var allcookies = document.cookie;
	var pos = allcookies.indexOf(cookieName+"=");
	var num, start, end, rawValue, value;
		
	num = parseInt(cookieName.length) + 1;
	start = pos + num; 
	end  = allcookies.indexOf(";", start);      
	if(end == -1) end = allcookies.length;     
	rawValue = allcookies.substring(start, end);
	value = unescape(rawValue); 
	
	return value;  
}
	
/*****************************************************************************
 *  Function:		setCookie
 *
 *  Description:
 *	This function is called to set the cookie value.
 *
 *  Arguments:
 *				cookieValue - the cookie value to set. e.g. 56asf, 56ram
 *				cookieName - this is the account name
 *
 *  Return Value: sets cookieName = value.
 *
 *  Side Effects:
 *  The result is a cookie set to the appropriate bitrate and format.
 *  The standard is to use this function exclusively in playprefs.tmpl.
 *
 *****************************************************************************/

function setCookie(cookieValue,cookieName) 
{
	var allcookies = window.document.cookie;
	var pos = allcookies.indexOf(cookieName + "=");
	var today;
	var expires;
	
	today = new Date;
	today.setFullYear(2020);
	expires = today.toGMTString();
	window.document.cookie = cookieName + "=" + cookieValue + ";expires=" + expires;
}
	

/*****************************************************************************
 *  Function:		setRadioPrefs
 *
 *  Description:
 *  Uses the getCookie function to retrieve the cookie value.
 *  and then loops through the radio button values and does a
 *  comparison to find the match.
 *
 *  Arguments:
 *				formName - name of the prefs form
 *				radioName - name of radio buttons
 *				cookieName - this is the account name
 *
 *  Return Value: the cookie value
 *
 *  Side Effects:
 *	The radio buttons on the preferences page will reflect their 
 *  previous choice in bitrate and/or format.
 *
 *****************************************************************************/	

function setRadioPrefs(formName,radioName,cookieName) 
{
	var format = getCookie(cookieName);
	var i_Radio;
	
	for ( i_Radio = 0; i_Radio < document[formName][radioName].length; i_Radio++ )
	{
		if (format == document[formName][radioName][i_Radio].value) 
		{
			document[formName][radioName][i_Radio].checked = true;
		}
	}
}

