/* File: lewinter_api.js
**
** This script is a custom API
**
*/

var already = 0;
var subWindow;
var windowName = "subWindow";
var windowOptions = "width=380,height=440,scrollbars=yes,toolbar=no,menubar=no,resizable=no";

//function openSubWindow(url,redirect) {
//  if (already != 0) {
//    //has login for this page has been previously opened?
//    if (subWindow.closed) {
//      //If the window has been closed, open a new window
//      subWindow = window.open(url+"?redirect="+escape(redirect),windowName,windowOptions);
//    } else {
//      //otherwise bring it to the front
//      subWindow.focus();
//    }
//  } else {
//    //initial opening of window
//    subWindow = window.open(url+"?redirect="+escape(redirect),windowName,windowOptions);
//    already = 1;
//  }
//}

function openSubWindow(url) {
  if (already != 0) {
    //has login for this page has been previously opened?
    if (subWindow.closed) {
      //If the window has been closed, open a new window
      subWindow = window.open(url,windowName,windowOptions);
    } else {
      //otherwise bring it to the front
      subWindow.focus();
    }
  } else {
    //initial opening of window
    subWindow = window.open(url,windowName,windowOptions);
    already = 1;
  }
}

function isblank(s) {
  for(var i = 0; i < s.length; i++) {
    var c = s.charAt(i);
    if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
  }
  return true
}

function verify(f) {
  var msg;
  var empty_fields = "";
  var errors = "";
  var oerrors = "";
  var invalidChars = " /:,;";
  var interr = 0;

//debug = "Step 1 : " + invalidChars + " : " + invalidChars.length + "\n";
//alert(debug);

  // Loop through the elements on the form 
  for(var i = 0; i < f.length; i++) {
    var e = f.elements[i];

    if ( interr == 1) { 
      continue;
    }

    if (((e.type == "text") || (e.type == "textarea")) && !e.optional) {

      // check if field is empty
      if ((e.value == null) || (e.value == "") || isblank(e.value)) {
        empty_fields += "\n     " + e.name;
        continue;
      }

      // Check fields that were supposedto be numeric
      if (e.numeric || (e.min != null) || (e.max != null)) {
        var v = parseFloat(e.value);
        if (isNaN(v) ||
            ((e.min != null) && (v < e.min)) ||
            ((e.max != null) && (v > e.max))) {
          errors += "- The field " + e.name + " must be a number";
          if (e.min != null)
            errors += " that is greater than " + e.min;
          if (e.max != null && e.min != null)
            errors += " and less than " + e.max;
          else if (e.max != null)
            errors += " that is less than " + e.max;
        }
      }
    }
    else if ( e.type == "select-one" && !e.optional ) {
      value = "";
      for( var j = 0; j < e.options.length; j++)
        if (e.options[j].selected)
          value += e.options[j].value + " ";

      // Verify drop down lists
      if (((value == null) || (value == "") || isblank(value) || 
          (value == "0 "))) {
        empty_fields += "\n     " + e.name;
        oerrors += "- Please select a " + e.name + " to deliver your comments to.\n";
        continue;
      }
    }

    // Check email defined fields
    if (e.email) {

      for ( var k=0; k < invalidChars.length; k++) {

        badChar = invalidChars.charAt(k);
        if (e.value.indexOf(badChar,0) != -1) {
          errors += "- Email Address: " + e.name + " contains invalid characters.\n";
          interr = 1;
          break;
        }
        atPos = e.value.indexOf("@",1);
        if (atPos == -1) {
          errors += "- " + e.name + ": " + e.value + " is not a valid email address.\n";
          interr = 1;
          break;
        }
        if (e.value.indexOf("@",atPos+1) != -1) {
          errors += "- " + e.name + ": " + e.value + " is not a valid email address.\n";
          interr = 1;
          break;
        }
        periodPos = e.value.indexOf(".",atPos);
        if (periodPos == -1) { 
          errors += "- " + e.name + ": " + e.value + " is not a valid email address.\n";
          interr = 1;
          break;
        }
        if (periodPos+3 > e.length) {
          errors += "- " + e.name + ": " + e.value + " is not a valid email address.\n";
          interr = 1;
          break;
        }
      }
    }
  }

  // If errors, display error message
  if (!empty_fields && !errors && !oerrors) {
    return(true);
  }

  msg =  "____________________________________________________\n\n";
  msg += "The form was not submitted because of the following error(s).\n";
  msg += "Please correct these error(s) and re-submit.\n";
  msg += "____________________________________________________\n\n";

  if (empty_fields) {
    msg += "- The following required field(s) are empty:" + empty_fields + "\n";
  }
  if (errors) msg += "\n"
  msg += errors;
  if (oerrors) msg += "\n"
  msg += oerrors;
  alert(msg);
  return(false);
} 

// Define the error handler.  This will generate an HTML form so the user
// can report an error to the author.
function report_error( msg, url, line)
{
  var w = window.open("",  // URL (none specified)
    "error"+error_count++, // Name(Force it to be unique)
    "resizable,status,width=625,height=400"); // Features

  var d = w.document;	// We use this variable to save typing!

  // Output an HTML document, including a form, into the new window.
  d.write('<DIV align=center>');
  d.write('<FONT SIZE=7 FACE="helvetica"><B>');
  d.write('OOPS.... A JavaScript Error Has Occured!');
  d.write('</B></FONT><BR><HR SIZE=4 WIDTH="80%">');
  d.write('<FORM ACTION="mailto:' + email + '" METHOD=post');
  d.write(' ENCTYPE="text/plain">');
  d.write('<FONT SIZE=3>');
  d.write('<I>Click the "Report Error" button to send a bug report.</I><BR>');
  d.write('<INPUT TYPE="submit" VALUE="Report Error">&nbsp;&nbsp;');
  d.write('<INPUT TYPE="button" VALUE="Dismiss" onClick="self.close()">');
  d.write('</DIV><DIV align=right>');
  d.write('<BR>Your Name <I>(optional)</I>: ');
  d.write('<INPUT SIZE=42 NAME="name" VALUE="">');
  d.write('<BR>Error Message: ');
  d.write('<INPUT SIZE=42 NAME="message" VALUE="' + msg + '">');
  d.write('<BR>Line Number: <INPUT SIZE=42 NAME="line" VALUE="' + line + '">');
  d.write('<BR>Browser Version: ');
  d.write('<INPUT SIZE=42 NAME="version" VALUE="'+ navigator.userAgent + '">');
  d.write('</DIV></FONT>');
  d.write('</FORM>');
  // Remember to close the document when we're done.
  d.close();
  
  // Return true from this error handler, so that JavaScript does not
  // display its own error dialog.
  return true;
}

// For Error Handler:
// Before the event handler can take effect, we have to register it
// for a particular window.
self.onerror = report_error;
 
