/* File: opt_api.js
**
** This script is a custom API
**
*/

// Initialize Local Variables
// Broswer Detection
var isNav, isIE;
var coll = "";
var styleObj = "";
// Event Location
var name = null;
// Error handler
var error_count = 0;
var email = "webmaster@optdesign.com";

if (parseInt(navigator.appVersion) >= 4) {
  if (navigator.appName == "Netscape") {
    isNav= true;
  } else { 
    isIE = true
    coll = "all.";
    styleObj = ".style";
  }
}

// Image Preloading
if (document.images) {
  var onImgArray = new Array();
  var offImgArray = new Array();

  // create the image objects for image arrays
  for ( var i = 0; i <= 8; i++) {
    onImgArray[i] = new Image();
    offImgArray[i] = new Image();
  }

  // Navbar buttons 
  onImgArray[0].src = "/images/bnr_logo2_on.gif";
  offImgArray[0].src = "/images/bnr_logo2.gif";
  onImgArray[1].src = "/images/btn_corp2_on.gif";
  offImgArray[1].src = "/images/btn_corp2.gif";
  onImgArray[2].src = "/images/btn_prod2_on.gif";
  offImgArray[2].src = "/images/btn_prod2.gif";
  onImgArray[3].src = "/images/btn_educ2_on.gif";
  offImgArray[3].src = "/images/btn_educ2.gif";
  onImgArray[4].src = "/images/btn_supp2_on.gif";
  offImgArray[4].src = "/images/btn_supp2.gif";
  onImgArray[5].src = "/images/btn_cont2_on.gif";
  offImgArray[5].src = "/images/btn_cont2.gif";
  //onImgArray[6].src = "/images/btn_memb2_on.gif";
  //offImgArray[6].src = "/images/btn_memb2.gif";
  onImgArray[7].src = "/images/btn_srch2_on.gif";
  offImgArray[7].src = "/images/btn_srch2.gif";
  onImgArray[8].src = "/images/btn_intl2_on.gif";
  offImgArray[8].src = "/images/btn_intl2.gif";
  /* Product Buttons */
  //onImgArray[9].src = "/images/btn_sleep_off.jpg";
  //offImgArray[9].src = "/images/btn_sleep_off.jpg";
  //onImgArray[10].src = "/images/btn_asthm_off.jpg";
  //offImgArray[10].src = "/images/btn_asthm_off.jpg";
  //onImgArray[11].src = "/images/btn_respi_off.jpg";
  //offImgArray[11].src = "/images/btn_respi_off.jpg";
  //onImgArray[12].src = "/images/btn_press_off.jpg";
  //offImgArray[12].src = "/images/btn_press_off.jpg";
  //onImgArray[13].src = "/images/btn_pulse_off.jpg";
  //offImgArray[13].src = "/images/btn_pulse_off.jpg";
  //onImgArray[14].src = "/images/btn_infan_off.jpg";
  //offImgArray[14].src = "/images/btn_infan_off.jpg";
  //onImgArray[15].src = "/images/btn_patie_off.jpg";
  //offImgArray[15].src = "/images/btn_patie_off.jpg";
}


// Functions

function convertToUpper(field) {
  field.value = field.value.toUpperCase()
}

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 = "";

  // Loop through the elements on the form 
  for(var i = 0; i < f.length; i++) {
    var e = f.elements[i];
    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 + " ";
//      debug = "Elem : " + e.name + " : " + value + "\n";
//      alert(debug);

      // Verify drop down lists
      if (((value == null) || (value == "") || isblank(value) || 
          (value == "0 "))) {
//        empty_fields += "\n     " + e.name;
        oerrors += "- Please select a mailbox to deliver your comments to.\n";
        continue;
      }

    }
  }

  // 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);
} 

function getObject(obj) {
  var theObj;
  if ( typeof obj == "string") {
    theObj = eval("document." + coll + obj)
  } else {
    theObj = obj;
  }
  return theObj;
}

function checkDOM(obj) {
  if (isNav||isIE||isNew) { 
    var theObj = getObject(obj);
    alert('This browser uses the ' + theObj + ' Document Object Model.');
    theObj.left = 120;
    theObj.top = 120;
  }
}

// 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;
}

// Image Swapping
function imgOn (obj, j) {
  if (document.images) {
    document[obj].src = onImgArray[j].src;
  }
}

function imgOff (obj, j) {
  if (document.images) {
    document[obj].src = offImgArray[j].src;
  }
}

// Event Location
function whichElement (evt) {
  if (isNav) {
    var testElem;
    var xPos = evt.pageX;
    var yPos = evt.pageY;
    // Cycle through layers in Netscape
    for ( var i = document.layers.length - 1; i >= 0; i--) {
      testElem = document.layers[i];
      if ((xPos > testElem.left) && 
        (xPos < testElem.left + testElem.clip.width) &&
        (yPos > testElem.top) &&
        (yPos < testElem.top + testElem.clip.height)) {
        name = testElem.name;
        alert('You are on ' + name + '.');
        return;
      }
    }
  }
  else {
    // access the element that generated the event and get the id
    name = event.srcElement.name;
    alert('You are on ' + name + '.');
    return;
  }
  return;
}
 
function verify_app(f) {
  var msg;
  var filemsg = "";
  var worktime = 0;
  var timemsg = "";
  var empty_fields = "";
  var errors = "";
  var oerrors = "";
  
  // Loop through the elements on the form 
  for(var i = 0; i < f.length; i++) {
    var e = f.elements[i];

    if (e.type == "checkbox"){
	//timemsg += " "+i+") "+worktime+", ";  this was for debugging
	switch( e.name ) {
		case 'work_day' : if (e.checked) worktime++; break;
		case 'work_eve' : if (e.checked) worktime++; break;
		case 'work_week': if (e.checked) worktime++; break;
  		case 'work_any' : if (e.checked) worktime++; break;
		default:
			break;
	}
    if (worktime == 0) timemsg = "\n\n * Please select one (or more) time(s) that you are available to work. \n";
    }
    

    if (e.type == "file") {      //this is for the 'file attachment' field
	// check if the 'resume file' is empty
      if ((e.value == null) || (e.value == "") || isblank(e.value)) {
        filemsg = "\n\n\n Unfortunately we are unable to review candidates for employment without a Resume:\n\n                      --->        ** Please attach your Resume **        <---";
        continue;
      }
    }
    if (((e.type == "text") || (e.type == "textarea")) && !e.optional) {
      // check if field is empty
      if ((e.value == null) || (e.value == "") || isblank(e.value) || (e.value == "()")) {
	  switch( e.name ) {

		case 'f_name' : 
			empty_fields += "\n		First Name"; 
			break;
		case 'l_name' : 
			empty_fields += "\n		Last Name";	
			break;
		case 'address_1':
			empty_fields += "\n		Address (in the 1st textbox)";
			break;
		case 'city':
			empty_fields += "\n		City";
			break;
		case 'state':
			empty_fields += "\n     		State";
			break;
		case 'zipcode':
			empty_fields += "\n     		Zipcode";
			break;
		case 'country':
			empty_fields += "\n     		Country";
			break;
		case 'phone':
			empty_fields += "\n     		Phone number";
			break;
		case 'prev_emp':
			empty_fields += "\n     		Previous Employer";
			break;
		case 'prev_pos':
			empty_fields += "\n     		Previous Position";
			break;
		default: empty_fields += "\n     		The <e.name> field"; break;
	    }	
	  /*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 + " ";
//      debug = "Elem : " + e.name + " : " + value + "\n";
//      alert(debug);

      // Verify drop down lists
      if (((value == null) || (value == "") || isblank(value) || (value == "0 "))) {
//        empty_fields += "\n     " + e.name;
	    switch( e.name ) {
		case 'educ_high':
			oerrors += "\n     		High School Education";
			break;
		case 'educ_coll':
			oerrors += "\n     		College Education";
			break;
		case 'educ_tech':
			oerrors+= "\n     		Technical School Education";
			break;
		case 'prev_length':
			oerrors+= "\n     		Length of Previous Position";
			break;
		case 'prev_reas':
			oerrors+= "\n     		Reason for Leaving Previous Position";
			break;
		default: empty_fields += "\n     		the <e.name> pull-down"; break;
	    }
        /*oerrors += "- Please choose a value for " + e.name + ".\n";
        continue;*/
      }

    }
  }

  // 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 += " Please fill in the following text fields: \n" + empty_fields + "\n";
  }
  if (errors) msg += "\n";
  msg += errors;
  if (timemsg) msg += timemsg;
  if (oerrors) msg += "\n\n Please choose a value for the following 'pull-down' options:\n";
  msg += oerrors;
  msg += filemsg;
  alert(msg);
  return(false);
} 


// For Error Handler:
// Before the event handler can take effect, we have to register it
// for a particular window.
self.onerror = report_error;

