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

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

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

<script type=text/javascript> // <!--
ss_flash( 600, 300, "imagedir" , "filename.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( "<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");
}

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

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

<script type=text/javascript> // <!--
media_player( "imagedir/filename.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          = "/" ;

  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( ".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 ;

  ////document.writeln( '<textarea style="width:500px;height:500px;">' ) ; //// sanity check
  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 ) document.writeln( ' classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" ' ) ;
  document.writeln( ' width="' + width + '" height="' + height + '" ') ;
  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( '</object>' ) ;
  //// sanity checks:
  //document.writeln( '</textarea>' ) ;
  //alert ( navigator.userAgent ) ;
  //document.writeln( "<p><a href='" + vurl + "'>" + vurl + "</a></p>" ) ; //// debug
  
  }

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

var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i ;

function checkmail(e){
  var returnval=emailfilter.test(e.value) ;
  if (returnval==false){
    alert("Please enter a valid email address.")
    e.select() ;
    e.style.background="yellow" ;
    }
  return returnval ;
  }
  
///////////////////////////////////////////////////////////////////
//// horrible form validator

/*

Codes:

"req"             - required - field should not be empty
"maxlen=x"        - checks the length entered to a maximum x 
"minlen=x"        - checks the length entered to a minimum x 
"alnum"           - field is alpha or numeric 
"alnum_s"         - field is alpha, numeric or white space 
"num"             - field is numeric 
"alpha"           - field is alpha 
"alpha_s"         - field is alpha with white space 
"email"           - validates email address 
"lt=x"            - value is less than x (only for numeric fields) 
"gt=x"            - value is greater than x (only for numeric fields) 
"regexp=x"        - enter any regular expression for x, and it will validate 
"dontselect=x"    - set x = 0 if you have a select box with the first option of "-Choose One-" or a value you don't want them to select 
"dontselectchk=x" - the user should not select this checkbox; here x is the value tied to the checkbox 
"shouldselchk=x"  - the user should select the checkbox; here x is the value tied to the checkbox 
"selone_radio"    - checks to make sure at least one radio button is checked

Usage:

<form id="form1" ... </form>

<script type="text/javascript">//<!--
validate( "form1" )
//--></script>

*/

function validateForm(thisform) 
{
	with (thisform) 
	{
		if(validateRequired(customerfirstname,"First name")==false)
		{
			customerfirstname.focus();
			return false;
		}
		if(IsAlpha(customerfirstname,"First name")==false)
		{
			customerfirstname.focus();
			return false;
		}
		if(validateRequired(customerlastname,"Last name")==false)
		{
			customerlastname.focus();
			return false;
		}
		if(IsAlpha(customerlastname,"Last name")==false)
		{
			customerlastname.focus();
			return false;
		}
		if(validateRequired(customeremail,"Email")==false)
		{
			customeremail.focus();
			return false;
		}
		if(validateEmail(customeremail)==false)
		{
			customeremail.focus();
			return false;
		}
		if(validateRequired(customercity,"City")==false)
		{
			customercity.focus();
			return false;
		}
		if(IsAlpha(customercity,"City")==false)
		{
			customercity.focus();
			return false;
		}
		if(validateRequired(customerstate,"State")==false)
		{
			customerstate.focus();
			return false;
		}
		if(IsAlpha(customerstate,"State")==false)
		{
			customerstate.focus();
			return false;
		}
		if(validateRequired(customerzip,"ZIP Code")==false)
		{
			customerzip.focus();
			return false;
		}
		if(IsNumeric(customerzip,"ZIP Code")==false)
		{
			customerzip.focus();
			return false;
		}

	}
}

function validateRequired(field,title)
{
	with(field)
	{
		if(value==null || value=="")
		{
			var isRequired = false;
		} else {
			var isRequired = true;
		}
	}
	if (isRequired==false)
	{
		alert(title + " is a required field.")
		return false;
	} else {
		return true;
	}
}
function validateEmail(field)
{
	with (field)
	{
    var splitted = value.match("^(.+)@(.+)$");
    if(splitted == null) 
		{
			alert("Please enter a valid email address.");
			return false;
		}
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null)
			{
				alert("Please enter a valid email address.");
				return false;
			}
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    	var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    	if(splitted[2].match(regexp_ip) == null)
				{
					alert("Please enter a valid email address.");
					return false;
      	}// if
      	return true;
			}
    }
	}
}

function IsPhone(field) {
	var validChars = "0123456789.()-";
	var IsNumber = true;
	var char;
	with (field)
	{
		for (i=0; i<value.length && IsNumber == true; i++)
		{
			char = value.charAt(i);
			if(validChars.indexOf(char) == -1)
			{
				IsNumber = false;
			}
		}
	}
	if(IsNumber == true) {
		return true;
	} else {
		alert("Please enter a valid telephone number");
		return false;
	}
}
	
function IsAlpha(field,title) {
	with (field)
	{
		var validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz. ";
		var IsAlpha = true;
		var char;
		for (i=0; i<value.length && IsAlpha == true; i++)
		{
			char = value.charAt(i);
			if(validChars.indexOf(char) == -1)
			{
				IsAlpha = false;
			}
		}
	}
	if(IsAlpha == true) {
		return true;
	} else {
		alert("Please only use alphabetical characters for " + title + ".");
		return false;
	}
}

function IsNumeric(field,title) {
	with (field)
	{
		var validChars = "0123456789- ";
		var IsNumeric = true;
		var char;
		for (i=0; i<value.length && IsNumeric == true; i++)
		{
			char = value.charAt(i);
			if(validChars.indexOf(char) == -1)
			{
				IsNumeric = false;
			}
		}
	}
	if(IsNumeric == true) {
		return true;
	} else {
		alert("Please only use numeric characters for " + title + ".");
		return false;
	}
}

function IsAlphaNumeric(field,title) {
	with (field)
	{
		var validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789. ";
		var IsAlphaNumeric = true;
		var char;
		for (i=0; i<value.length && IsAlphaNumeric == true; i++)
		{
			char = value.charAt(i);
			if(validChars.indexOf(char) == -1)
			{
				IsAlphaNumeric = false;
			}
		}
	}
	if(IsAlphaNumeric == true) 
	{
		return true;
	} else {
		alert("Please only use alphabetical and numeric characters" + title + ".")
		return false;
	}
} 
