///////////////////////////////////////////////////////////////////
//// 
//// website utilities - njscuba.net / A.R. Galiano ( argaliano@optonline.com )
////
///////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////
//// embed flash slideshow using xhtml object standard
//// width = pixels
//// height = pixels
//// where = name of standard images directory
//// which = name of slideshow file

/*
USAGE:

<script type=text/javascript> // <!--
ss_flash( 800, 360, "/volvonj/" , "ss_xc60.xml" ) ;
// --> </script>

*/

function ss_flash( width, height, where, which )
{
debug = 0 ;
if(debug) document.write( "<textarea rows='50' cols='100'>" ) ;
document.write( "<object type=\"application/x-shockwave-flash\" " ) ;
document.write( "data=\"" + where + "/monoslideshow.swf\" ") ;
document.write( "width=\"" + width + "\" ") ;
document.write( "height=\"" + height + "\" ") ;
document.write( "id=\"" + which + "\" >\n" ) ;
document.write( "<param name=\"movie\" value=\"" + where + "/monoslideshow.swf\" />\n" ) ;
document.write( "<param name=\"flashvars\" value=\"dataFile=" + where + "/" + which + "&showLogo=false&showVersionInfo=false\" />\n" ) ; 
document.write( "<param name=\"wmode\" value=\"transparent\" />\n" );
document.write( "<h1 style=\"text-align:center\"><a href=\"http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\" target=\"_blank\">Adobe Flash Plugin Required - Get It</a></h1>\n" ) ;
document.write( "</object>\n" ) ;
if(debug) document.write( "</textarea>" ) ; 
if(debug) alert("flash slideshow");
}

///////////////////////////////////////////////////////////////////
//// embed general flash object using xhtml object standard
//// width = pixels
//// height = pixels
//// swf = /path/name of flash file

/*
USAGE: make sure you get the path right:

<script type=text/javascript> // <!--
flash( 600, 300, "/path/ss_xc60.swf" ) ;
// --> </script>

*/

function flash( width, height, swf )
{
debug = 0 ;
if(debug) document.write( "<textarea rows='50' cols='100'>" ) ;
document.write( "<object type=\"application/x-shockwave-flash\" " ) ;
document.write( "data=\"" + swf + "\" ") ;
document.write( "width=\"" + width + "\" ") ;
document.write( "height=\"" + height + "\" ") ;
document.write( "id=\"" + swf + "\" >\n" ) ;
document.write( "<param name=\"movie\" value=\"" + swf + "\" />\n" ) ;
document.write( "<param name=\"wmode\" value=\"transparent\" />\n" );
document.write( "<h1 style=\"text-align:center\"><a href=\"http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\" target=\"_blank\">Adobe Flash Plugin Required - Get It</a></h1>\n" ) ;
document.write( "</object>\n" ) ;
if(debug) document.write( "</textarea>" ) ; 
if(debug) alert("flash slideshow");
}

///////////////////////////////////////////////////////////////////
//// embed any object using xhtml object standard
//// width = pixels
//// height = pixels
//// url = duh

/*
USAGE: make sure you get the path right:

<script type=text/javascript> // <!--
embed( 600, 300, "url" ) ;
// --> </script>

*/

function embed( width, height, url )
{
debug = 0 ;
if(debug) document.write( "<textarea rows='50' cols='100'>" ) ;
document.write( "<object type=\"text/html\" " ) ;
document.write( "data=\"" + url + "\" ") ;
document.write( "width=\"" + width + "\" ") ;
document.write( "height=\"" + height + "\" >\n") ;
document.write( "<h1 style=\"text-align:center\">Object Embedding not supported</h1>\n" ) ;
document.write( "</object>\n" ) ;
if(debug) document.write( "</textarea>" ) ; 
if(debug) alert("object embed");
}

///////////////////////////////////////////////////////////////////
//// pre-formatted embedded video player or placeholder
//// plays audio: mp3, wav, wma, mid
//// plays video: wmv, mpg, avi, mov

/*
USAGE:

<script type=text/javascript> // <!--
media_player( "/imagedir/ss_xc60.wmv" , "autostart,loop" ) ;
// --> </script>

*/

var PlayerCount = 1 ;

function media_player( vurl, options )
  { 
  //// defaults  
  
  var autostart     = 0   ;
  var stretch       = 1   ;
  var showcontrols  = 1   ;
  var loop          = 0   ;
  var aspectratio   = 4/3 ;
  var vwidth        = 320 ; //// 320 native
  var awidth        = 450 ;
  var width               ;
  var height              ;
  var controlheight = 45  ;
  var type          = "unknown/unknown" ;
  var pid           = "" ;
  var fn            = new String( window.location ) ;
  var home          = "http://" + location.hostname ;


  plid = "Player" + PlayerCount++ ;
  ////alert( plid ) ;

  //// options override defaults
  if( typeof( options ) != "undefined" )
    {
    if ( options.indexOf( "loop"       ) >= 0 ) { loop         = 100000 ; }
    if ( options.indexOf( "autostart"  ) >= 0 ) { autostart    = 1 ; }
    if ( options.indexOf( "stretch"    ) >= 0 ) { autosize     = 1  ; }
    if ( options.indexOf( "widescreen" ) >= 0 ) { aspectratio  = 16/9  ; vwidth = 450 ;}
    if ( options.indexOf( "nocontrols" ) >= 0 ) { showcontrols = 0 ; controlheight = 0 ; }
    if ( options.indexOf( "slideshow"  ) >= 0 ) { showcontrols = 0 ; controlheight = 0 ; autostart = 1 ; loop = 100000 ; }
    if ( options.indexOf( "wide"       ) >= 0 ) { awidth       = 550 ; }
    }

  //// get mime type
  if     ( typeof( vurl ) == "undefined" ) { vurl = "" ; }
  
  if     ( vurl.indexOf( ".mp3"  ) >= 0 )  { type = "audio/mpeg"      ; width = awidth ; height = 0 ; }
  else if( vurl.indexOf( ".wav"  ) >= 0 )  { type = "audio/x-wav"     ; width = awidth ; height = 0 ; }
  else if( vurl.indexOf( ".wma"  ) >= 0 )  { type = "audio/x-ms-wmv"  ; width = awidth ; height = 0 ; }
  else if( vurl.indexOf( ".mid"  ) >= 0 )  { type = "audio/x-midi"    ; width = awidth ; height = 0 ; }

  else if( vurl.indexOf( ".wmv"  ) >= 0 )  { type = "video/x-ms-wmv"  ; width = vwidth ; height = width / aspectratio ; }

  else if( vurl.indexOf( ".mpg"  ) >= 0 )  { type = "video/mpeg"      ; width = vwidth ; height = width / aspectratio ; }
  else if( vurl.indexOf( ".mpeg" ) >= 0 )  { type = "video/mpeg"      ; width = vwidth ; height = width / aspectratio ; }
  else if( vurl.indexOf( ".mp4"  ) >= 0 )  { type = "video/mpeg"      ; width = vwidth ; height = width / aspectratio ; }
  else if( vurl.indexOf( ".m4v"  ) >= 0 )  { type = "video/mpeg"      ; width = vwidth ; height = width / aspectratio ; }

  else if( vurl.indexOf( ".avi"  ) >= 0 )  { type = "video/avi"       ; width = vwidth ; height = width / aspectratio ; }
  else if( vurl.indexOf( ".mov"  ) >= 0 )  { type = "video/quicktime" ; width = vwidth ; height = width / aspectratio ; }

  vurl = home + vurl ;
  height += controlheight ;

  debug = 0 ;
  if(debug) document.writeln( "<textarea rows='50' cols='75'>" ) ;
  document.writeln( '<object ' ) ;
  document.writeln( '  id="' + plid + '" ' ) ;
  document.writeln( '  type="' + type + '" ' ) ;
  //document.writeln( ' data="' + vurl + '" ' ) ; //// preload! performance impact on slow conxns
  if( navigator.userAgent.indexOf("Firefox") == -1 ) //// important !!!
    document.writeln( '  classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" ' ) ;
  document.writeln( '  width="' + width + '" height="' + height + '" ') ;
  document.writeln( '  style="position:relative; left:0; top:0; z-index:-10;"' ) ;
  document.writeln( '  >' ) ;
  document.writeln( '<param name="src"             value="' + vurl         + '" />' ) ;
  document.writeln( '<param name="autostart"       value="' + autostart    + '" />' ) ;
  document.writeln( '<param name="autoplay"        value="' + autostart    + '" />' ) ;
  document.writeln( '<param name="animationstart"  value="' + autostart    + '" />' ) ;
  document.writeln( '<param name="loop"            value="' + loop         + '" />' ) ;
  document.writeln( '<param name="showcontrols"    value="' + showcontrols + '" />' ) ;
  document.writeln( '<param name="controller"      value="' + showcontrols + '" />' ) ;
  document.writeln( '<param name="autosize"        value="' + stretch      + '" />' ) ;
  document.writeln( '<param name="visible"         value="1"                    />' ) ;
  document.writeln( '<param name="showdisplay"     value="0"                    />' ) ;
  document.writeln( '<param name="showstatusbar"   value="0"                    />' ) ;
  document.writeln( '<param name="wmode"           value="transparent"          />' ) ;
  document.writeln( '<param name="windowlessvideo" value="1"                    />' ) ;
  document.writeln( '</object>' ) ;
  if(debug) document.writeln( "<p><a href='" + vurl + "'>" + vurl + "</a></p>" ) ; //// debug
  if(debug) document.writeln( "</textarea>" ) ; 
  if(debug) alert("media player: " + navigator.userAgent );
  
  }

///////////////////////////////////////////////////////////////////
//// simple form validator

/*

USAGE:

This validator mis-uses the css class attribute to specify validation requirements.
The class attribute is both "safe" to use in WordPress, and easily accessible in JS.

Sample syntax:

--------------------------------------------------------

<form id="form-id" ... >
<input type="text"     class="valid-required" ... />
<input type="text"     class="valid-email" ... />
<input type="text"     class="valid-phone" ... />
<input type="textarea" class="valid-required" ... ></textarea>
<input type="select"   class="valid-required" ... >
<input type="submit"   onclick="validate('form-id')"... />
</form>

------------------------------------------------------------

The "classes" trigger validation as follows:

valid-required: non-null text value or selection (all inputs)
valid-email:    valid email address (text only)
valid-phone:    text min length 10 characters (text only)

If none of these classes is specified for a field, the validator simply skips it. These names
will hopefully not collide with any real css classes that may be in use.

Single selects ( drop-down lists ) require a choice other than the first one, which must be set up as the default. 
Multiple selects require any choice at all.

For radio buttons, just one button in a group needs to be marked required to trigger validation on the entire group.
It is best to mark the first one in the group, or all of them. Radio buttons are inherently messy.

The validate function takes the form id as a parameter, allowing multiple forms on a page. This is a simple
text string. The function knows nothing about the fields, nor does it need to. All set-up is done in the
html, no programming is required. Error messages are generic, the user is guided with color highlighting.

*/

function validate( frm )
{
emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i ; //// by Michael Cooper

theform = document.forms[frm] ;

if ( typeof theform  == "undefined" )
  {  
  alert( "Error - form not found" ) ;
  return false ;
  }

var radiosval = new Array();
var radiosreq = new Array();

//// collect all radio button states
for( i=0 ; i<theform.length ; i++ )
  {
  thefield = theform.elements[i] ;
  if( thefield.type == "radio" )
    {
    if( radiosval[thefield.name] == undefined ) radiosval[thefield.name] = 0 ;
    if( thefield.checked ) radiosval[thefield.name] = 1 ;
    if( thefield.className == "valid-required" && radiosreq[thefield.name] == undefined ) radiosreq[thefield.name] = 1 ;
    }
  }

//// validate fields
for( i=0 ; i<theform.length ; i++ )
  {
  thefield = theform.elements[i] ;

  //alert( thefield.name + " - " + thefield.type + " - " + thefield.className ) ;

  if ( thefield.type == "text" && thefield.className == "valid-required" )
  if ( thefield.value.length < 1 )
    {
    thefield.focus();
    thefield.style.background = "yellow" ;
    alert( "Please enter a value" ) ;
    return false ;
    } else { thefield.style.background = "white" ; } ;

  if ( thefield.type == "text" && thefield.className == "valid-phone" )
  if ( thefield.value.length < 10 )
    {
    thefield.focus();
    thefield.style.background = "yellow" ;
    alert( "Please enter a valid phone number" ) ;
    return false ;
    } else { thefield.style.background = "white" ; } ;

  if ( thefield.type == "text" && thefield.className == "valid-email" )
  if ( ! emailfilter.test(thefield.value) )
    {
    thefield.focus();
    thefield.style.background = "yellow" ;
    alert( "Please enter a valid email address" ) ;
    return false ;
    } else { thefield.style.background = "white" ; } ;

  if( thefield.type == "textarea" && thefield.className == "valid-required" )
  if( thefield.value.length < 1 )
    {
    thefield.focus();
    thefield.style.background = "yellow" ;
    alert( "Please enter a value" ) ;
    return false;
    } else { thefield.style.background = "white" ; } ;

  if( thefield.type == "select-one" && thefield.className == "valid-required" )
  if( thefield.selectedIndex == 0 )
    {
    thefield.focus();
    thefield.style.background = "yellow" ;
    alert( "Please make a selection" ) ;
    return false;
    } else { thefield.style.background = "white" ; } ;

  if( thefield.type == "select-multiple" && thefield.className == "valid-required" )
  if( thefield.selectedIndex == -1 )
    {
    thefield.focus();
    thefield.style.background = "yellow" ;
    alert( "Please make a selection" ) ;
    return false;
    } else { thefield.style.background = "white" ; }

  if ( radiosreq[thefield.name] != undefined ) 
    {
    if ( ! radiosval[thefield.name] ) 
      {
      for( j=0 ; j<theform.length ; j++ )
        { //// paint all elements
        tmp = theform.elements[j] ;
        if( tmp.name == thefield.name ) tmp.style.background = "yellow" ;
        }
      thefield.focus();
      alert( "Please make a selection" ) ;
      return false;
      } else { thefield.style.background = "white" ; }
    }  

  }

return true ;
}