function emailCheck (emailStr) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	alert("Email address seems incorrect (check @ and .'s)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    alert("The username doesn't seem to be valid.")
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Destination IP address is invalid!")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("The domain name doesn't seem to be valid.")
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   alert("The address must end in a three-letter domain, or two letter country.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!"
   alert(errStr)
   return false
}

  // If we've gotten this far, everything's valid!
  return true;
}

//list of all image names to be used, in any order
names = new Array("products_d", "products_b",
                  "media_d",    "media_b",
                  "credits_d",  "credits_b",
                  "bio_d",      "bio_b",
                  "join_d",     "join_b",
                  "request_d",  "request_b",
                  "about_d",   "about_b",
                  "plan_d",    "plan_b",
                  "partner_d", "partner_b",
                  "hire_d",    "hire_b");

//crap 'cause some browsers (MSIE) do not support image.src!
if (parseInt(navigator.appVersion) >= 3)
	Browser = true;
	else Browser = false;

//pre-load images
if (Browser) {
	button = new Array(names.length);
	for (i = 0; i < names.length; i++){
		b = names[i];
		button[b] = new Image();
		button[b].src = "/g/" + b + ".gif";
	} // end for
} // end if

function wiggle(n,s) {
	if (Browser) {
		index = n + "_" + s;
		document[n].src = button[index].src;
	} // end if
	return true;
 } // end wiggle

function getName() {
  var comic, curl, fname, mname, lname, prefix, suffix, name;
  var name_array = new Array();
  
  //use location.pathname to get the comic's name
  curl = location.pathname.replace("/comics/","");
  curl = curl.substr(0, curl.indexOf("/"));
  name_array = curl.split("_");

  if (name_array[4]) {
    prefix = name_array[0];
    fname  = name_array[1];
    mname  = name_array[2];
    lname  = name_array[3];
    suffix = name_array[4];
  }
  else if (name_array[3]) {
    if ((name_array[3] == 'jr') || (name_array[3] == 'sr') || (name_array[3] == 'III')) {
      prefix = '';
      fname  = name_array[0];
      mname  = name_array[1];
      lname  = name_array[2];
      suffix = name_array[3];
    }
    else {
      prefix = name_array[0];
      fname  = name_array[1];
      mname  = name_array[2];
      lname  = name_array[3];
      suffix = '';
    }
  }
  else if (name_array[2]) {
    if ((name_array[2] == 'jr') || (name_array[2] == 'sr') || (name_array[2] == 'III')) {
      prefix = '';
      fname  = name_array[0];
      mname  = '';
      lname  = name_array[1];
      suffix = name_array[2];
    }
    else if ((name_array[0] == 'Big') || (name_array[0] == 'The')) {
      prefix = name_array[0];
      fname  = name_array[1];
      mname  = '';
      lname  = name_array[2];
      suffix = '';
    }
    else {
      prefix = '';
      fname  = name_array[0];
      mname  = name_array[1];
      lname  = name_array[2];
      suffix = '';
    }
  }
  else if (name_array[1]) {
    prefix = '';
    fname  = name_array[0];
    mname  = '';
    lname  = name_array[1];
    suffix = '';
  }
  else if (name_array[0]) {
    prefix = '';
    fname  = name_array[0];
    mname  = '';
    lname  = '';
    suffix = '';
  }

  name = prefix + ' ' + fname + ' ' + mname + ' ' + lname + ' ' + suffix;
  return name;
}

function getTitle(type) {
  var captype, titletext;
  
  //capitalize the type for the alt attribute
  captype = type.substring(0, 1).toUpperCase() + type.substring(1, type.length);
  
  //build the title attribute with the comic's name
  titletext = 'Hire A Comic - ' + getName()+'\'s ' + captype + ' Page';

  return titletext;
}

function getItem(id)
 {
   var itm = false;
   if(document.getElementById)
     {itm = document.getElementById(id);}
   else if(document.all)
     {itm = document.all[id];}
   else if(document.layers)
     {itm = document.layers[id];}

   return itm;
 }

function writeComic(type) { 
  document.write('<h1 align="center" title="' +  getName() + '">' +  getName() + '</h1>');
}

function writeComicButtons(type) {

var req_url;
var join_url;
if (type == 'popup'){
  req_url  = 'javascript:opener.location.href=\'request.shtml\';window.close();';
  join_url = 'javascript:opener.location.href=\'join.shtml\';window.close();';
}
else{
  req_url  = 'request.shtml';
  join_url = 'join.shtml';
}

var reqbtn = '<div style="float:left;"><a href="'+req_url+'" onMouseOver="wiggle(\'request\',\'b\');" onMouseOut="wiggle(\'request\',\'d\');" title="Request ' + getName() + '"><img src="/g/request_d.gif" border="0" width="83" height="43" alt="Request ' + getName() + '" name="request" hspace="10" /></a></div>';

var joinbtn = '<div style="float:right;padding-right:20px;"><a href="'+join_url+'" onMouseOver="wiggle(\'join\',\'b\');" onMouseOut="wiggle(\'join\',\'d\');" title="Join The ' + getName() + ' Fan Club"><img src="/g/join_d.gif" border="0" width="55" height="43" alt="Join The ' + getName() + ' Fan Club" name="join" /></a></div>';
  
  document.write(reqbtn + joinbtn);
}

function writeJoinButton(){
  document.write('<input type="button" value="Join ' + getName() + '\'s fan club" onClick="checkForm(\'joincomic\');" /><br clear="all" />');
}

function writeRequestButton(){
  document.write('<input type="button" value="Request ' + getName() + '" onClick="checkForm(\'requestcomic\');" /><br clear="all" />');
}

function writeThanks(type) { 
  if (type == 'joincomic'){
    document.write('Thanks for joining ' + getName() + '\'s fans. You made a great choice and we will keep you updated on news, shows and comedy releases.');
  }
  else{
    document.write('Thanks for requesting ' + getName()+' <br /><br />A HireAComic representative will be in touch with you soon.');
  }
}

function writeTitle(type) {
  var titletext = getTitle(type);
  
  document.write('<title>' + titletext + '</title>');
}

function writeHiddenThanksInput(type){
  var retval;

  if (type == 'joincomic'){
    retval = location.pathname.replace("join.shtml","thanks_join.shtml"); 
  }
  else{
    retval = location.pathname.replace("request.shtml","thanks_request.shtml"); 
  }

  document.writeln('<input type="hidden" name="thanks" value="http://'+ location.hostname + retval+'" />');
}

function writeToInput(type) { 
  var curl; //comic url
  curl = location.pathname.replace("/comics/","");
  curl = curl.substr(0, curl.indexOf("/"));
  if (type == 'joincomic'){
    document.write('<input type="hidden" name="to" value="join_'+curl+'" />');
  }
  else{
    document.write('<input type="hidden" name="to" value="request_'+curl+'" />');
  }
}

function writeVideo() {

  var id = location.search.substring(1,location.search.length);

  document.writeln('<div align="center">');
  document.write('<object width="425" height="350" standby="Loading ..."><param name="movie" value="http://www.youtube.com/v/'+id+'" /><embed src="http://www.youtube.com/v/'+id+'&autoplay=1" type="application/x-shockwave-flash" width="425" height="350" /><noembed><a href="http://www.adobe.com/products/flashplayer/" target="_blank">Download and install Macromedia Flash</a> to view this movie.</noembed></object>');
  document.writeln('</div>');
}

function getTab(type, state) {
  var captype, name, titletext;
  var name_array = new Array();
  
  //capitalize the type for the alt attribute
  captype = type.substring(0, 1).toUpperCase() + type.substring(1, type.length);
  
  name = getName();

  //build the title attribute with the comic's name
  titletext = name + '\'s ' + captype;

  if (state == 'on') {
  	l_state = 'b';

    //write out the tabs
    document.write('<a href="'+type+'.shtml" title="'+titletext+'"><img src="/g/'+type+'_'+l_state+'.gif" name="'+type+'" alt="'+captype+'" width="144" height="49" border="0" onmouseover="wiggle(\''+type+'\',\'b\');" onmouseout="wiggle(\''+type+'\',\'b\');" /></a>');
  }
  else {
  	l_state = 'd';

    //write out the tabs
    document.write('<a href="'+type+'.shtml" title="'+titletext+'"><img src="/g/'+type+'_'+l_state+'.gif" name="'+type+'" alt="'+captype+'" width="144" height="49" border="0" onmouseover="wiggle(\''+type+'\',\'b\');" onmouseout="wiggle(\''+type+'\',\'d\');" /></a>');
  }
    
}

function newWin(url_path, win_width, win_height, win_name, win_scroll)
{
  newWindow =
window.open(url_path,win_name,config="resizable=yes,scrollbars="+
win_scroll+",screenX=50,screenY=50,left=50,top=50,width="+
win_width+",height="+win_height);
newWindow.focus();
}

function showEvents(action){
  l_events = getItem('events');
  if (action == 'hide') {
    l_events.style.visibility = 'hidden';
    l_events.style.height = '0px';
  }
  else {
    l_events.style.visibility = 'visible';
    l_events.style.height = '230px';
  }
}


function checkForm(type) {
  var ending;

  if (type == 'requestcomic') {
    var request_name = document.requestcomic.name.value;
    var request_phone = document.requestcomic.phone.value;
    var request_email = document.requestcomic.email.value;

	ending = ' to request ' + getName()+'.';

    if (request_name == '') {
      alert('You must enter your name' + ending);
    }
    else if (request_phone == '') {
      alert('You must enter your phone number' + ending);
    }
    else if (request_email == '') {
      alert('You must enter your email address' + ending);
    }
    else if (emailCheck(request_email)) {
      document.requestcomic.submit();
    }
  }
  else if (type == 'joincomic') {
  	var join_name  = document.joincomic.name.value;
	var join_email = document.joincomic.email.value;

	ending = ' to join ' + getName() + '\'s fan club.';

    if (join_name == '') {
      alert('You must enter your name' + ending);
    }
    else if (join_email == '') {
      alert('You must enter your email address' + ending);
    }
    else if (emailCheck(join_email)) {
      document.joincomic.submit();
    }
  }
}

function getItem(id)
 {
   var itm = false;
   if(document.getElementById)
     {itm = document.getElementById(id);}
   else if(document.all)
     {itm = document.all[id];}
   else if(document.layers)
     {itm = document.layers[id];}

   return itm;
 }

function showMenu(menuitem,action){
  l_events = getItem(menuitem);
  if (action == 'hide') {
    l_events.style.display = 'none';
    l_events.style.zIndex = '-1';
    l_events.style.clear = 'both';
  }
  else {
    l_events.style.display = 'block';
    l_events.style.zIndex = '100';
    l_events.style.clear = 'both';
  }
}

