// ---------------------------------------------------------------------------------
// newacct.js
// Create New Online Account JavaScript library
// Copyright(c)2009 Paschall Truck Lines
// ---------------------------------------------------------------------------------

// declare some global variables

var requestText = new String();
var requestComplete = new Boolean();

// ---------------------------------------------------------------------------------
// checkBrowser()
// procedure to determine what type of client is in use
// ---------------------------------------------------------------------------------

function checkBrowser() {

  // return control to the calling procedure

  return;

}

// ---------------------------------------------------------------------------------
// checkWelcomeMessage()
// procedure to determine what type of client is in use
// ---------------------------------------------------------------------------------

function checkWelcomeMessage() {

  // return control to the calling procedure

  return;

}

// ---------------------------------------------------------------------------------
// submitRequest()
// procedure to validate the requested account information and send it to the
// server for processing
// ---------------------------------------------------------------------------------

function submitRequest() {

  // validate that all required data fields are present in the DOM

  if (!document.getElementById("fnam") ||
      !document.getElementById("lnam") ||
      !document.getElementById("cnam") ||
      !document.getElementById("eml") ||
      !document.getElementById("uid") ||
      !document.getElementById("pwd") ||
      !document.getElementById("submit")) {

	// display an error message in the
	// text status DIVS
	
    alert("An unhandled exception has occurred.\n\n" +
          "Problem:\n\n" +
          "Necessary resource was not found.\n\n" +
          "Possible Causes for Problem:\n\n" +
          "*Browser is not latest version\n" +
          "*Javascript is disabled\n" +
          "Problem Solutions:\n\n" +
          "*Update your browser to the latest version\n" +
		  "*Enable javescript for the ptl-inc.com domain\n\n" +
          "If this error occurs again, please contact the website support staff (see below).\n\n" +
          "Paschall Truck Lines\n" +
          "MIS Department\n" +
          "Telephone: (270) 753-1717\n" +
          "Email: mis@ptl-inc.com");
		
	// close the newly opened window
	
	window.location = "http://www.ptl-inc.com/index.htm";
	
	// return control to the calling procedure
	
	return;

  }

  // verify that the first name is not blanks
  
  if (isBlanks(document.getElementById("fnam").value)) {
  
    // display the required error message
	
    alert("An unhandled exception has occurred.\n\n" +
          "Problem:\n\n" +
          "The requested account first name is blanks.\n\n" +
          "Problem Solution:\n\n" +
		  "Enter the first name of the user that is requesting a new account.");
  
    // give the element focus
	
	document.getElementById("fnam").focus();
  
    // return control to the calling procedure
	
	return;
  
  }

  // verify that the first name does not contain any invalid characters
  
  if (!isAlphaNumeric(document.getElementById("fnam").value)) {
  
    // display the required error message
	
    alert("An unhandled exception has occurred.\n\n" +
          "Problem:\n\n" +
          "The requested account first name contains an invalid character.\n\n" +
          "Problem Solution:\n\n" +
		  "Remove any non-alphanumeric characters from the user's first name.");
  
    // give the element focus
	
	document.getElementById("fnam").focus();
  
    // return control to the calling procedure
	
	return;
  
  }

  // verify that the last name is not blanks
  
  if (isBlanks(document.getElementById("lnam").value)) {
  
    // display the required error message
	
    alert("An unhandled exception has occurred.\n\n" +
          "Problem:\n\n" +
          "The requested account last name is blanks.\n\n" +
          "Problem Solution:\n\n" +
		  "Enter the last name of the user that is requesting a new account.");
  
    // give the element focus
	
	document.getElementById("lnam").focus();
  
    // return control to the calling procedure
	
	return;
  
  }

  // verify that the last name does not contain any invalid characters
  
  if (!isAlphaNumeric(document.getElementById("lnam").value)) {
  
    // display the required error message
	
    alert("An unhandled exception has occurred.\n\n" +
          "Problem:\n\n" +
          "The requested account last name contains an invalid character.\n\n" +
          "Problem Solution:\n\n" +
		  "Remove any non-alphanumeric characters from the user's last name.");
  
    // give the element focus
	
	document.getElementById("lnam").focus();
  
    // return control to the calling procedure
	
	return;
  
  }

  // verify that the company name is not blanks
  
  if (isBlanks(document.getElementById("cnam").value)) {
  
    // display the required error message
	
    alert("An unhandled exception has occurred.\n\n" +
          "Problem:\n\n" +
          "The requested account company name is blanks.\n\n" +
          "Problem Solution:\n\n" +
		  "Enter the company name of the user that is requesting a new account.");
  
    // give the element focus
	
	document.getElementById("cnam").focus();
  
    // return control to the calling procedure
	
	return;
  
  }

  // verify that the company name does not contain any invalid characters
  
  if (!isAlphaNumeric(document.getElementById("cnam").value)) {
  
    // display the required error message
	
    alert("An unhandled exception has occurred.\n\n" +
          "Problem:\n\n" +
          "The requested account company name contains an invalid character.\n\n" +
          "Problem Solution:\n\n" +
		  "Remove any non-alphanumeric characters from the user's company name.");
  
    // give the element focus
	
	document.getElementById("cnam").focus();
  
    // return control to the calling procedure
	
	return;
  
  }

  // verify that the company email address is not blanks
  
  if (isBlanks(document.getElementById("eml").value)) {
  
    // display the required error message
	
    alert("An unhandled exception has occurred.\n\n" +
          "Problem:\n\n" +
          "The requested account email address is blanks.\n\n" +
          "Problem Solution:\n\n" +
		  "Enter the email address of the user that is requesting a new account.");
  
    // give the element focus
	
	document.getElementById("eml").focus();
  
    // return control to the calling procedure
	
	return;
  
  }

  // verify that the email address does not contain any invalid characters
  
  if (!isValidEmailAddress(document.getElementById("eml").value)) {
  
    // display the required error message
	
    alert("An unhandled exception has occurred.\n\n" +
          "Problem:\n\n" +
          "The requested account email address is not valid.\n\n" +
          "Problem Solution:\n\n" +
		  "Please enter a valid email address.");
  
    // give the element focus
	
	document.getElementById("eml").focus();
  
    // return control to the calling procedure
	
	return;
  
  }

  // verify that the requested user id is not blanks
  
  if (isBlanks(document.getElementById("uid").value)) {
  
    // display the required error message
	
    alert("An unhandled exception has occurred.\n\n" +
          "Problem:\n\n" +
          "The requested user id is blanks.\n\n" +
          "Problem Solution:\n\n" +
		  "Enter a user id value.");
  
    // give the element focus
	
	document.getElementById("uid").focus();
  
    // return control to the calling procedure
	
	return;
  
  }

  // verify that the requested user id contains a blank
  
  if (hasBlanks(document.getElementById("uid").value)) {
  
    // display the required error message
	
    alert("An unhandled exception has occurred.\n\n" +
          "Problem:\n\n" +
          "The requested user id contains a blank.\n\n" +
          "Problem Solution:\n\n" +
		  "Enter a user id value that does not contain any blank spaces.");
  
    // give the element focus
	
	document.getElementById("uid").focus();
  
    // return control to the calling procedure
	
	return;
  
  }

  // verify that the requested user id is not blanks
  
  if (!isAlphaNumeric(document.getElementById("uid").value)) {
  
    // display the required error message
	
    alert("An unhandled exception has occurred.\n\n" +
          "Problem:\n\n" +
          "The requested user id contains an invalid character.\n\n" +
          "Problem Solution:\n\n" +
		  "Remove any non-alphanumeric characters from the user id.");
  
    // give the element focus
	
	document.getElementById("uid").focus();
  
    // return control to the calling procedure
	
	return;
  
  }

  // verify that the first byte of the requested user id is valiod
  
  if (!isAlpha(document.getElementById("uid").value.substr(0,1))) {
  
    // display the required error message
	
    alert("An unhandled exception has occurred.\n\n" +
          "Problem:\n\n" +
          "The first character of the requested user id contains an invalid character.\n\n" +
          "Problem Solution:\n\n" +
		  "Change the first character of the requested user id to be an alphabetic character between A and Z.");
  
    // give the element focus
	
	document.getElementById("uid").focus();
  
    // return control to the calling procedure
	
	return;
  
  }

  // verify that requested user id is of the minimum length
  
  if (document.getElementById("uid").value.length < 6) {
  
    // display the required error message
	
    alert("An unhandled exception has occurred.\n\n" +
          "Problem:\n\n" +
          "The requested user id is too short.\n\n" +
          "Problem Solution:\n\n" +
		  "The requested user id must be at least 6 character in length.");
  
    // give the element focus
	
	document.getElementById("uid").focus();
  
    // return control to the calling procedure
	
	return;
  
  }

  // verify that requested user id is not already in use
  
  if (!userIdAvailable(document.getElementById("uid").value)) {
  
    // display the required error message
	
    alert("An unhandled exception has occurred.\n\n" +
          "Problem:\n\n" +
          "The requested user id is already in use by another customer.\n\n" +
          "Problem Solution:\n\n" +
		  "Modify the requested user id or enter a different user id.");
  
    // give the element focus
	
	document.getElementById("uid").focus();
  
    // return control to the calling procedure
	
	return;
  
  }

  // verify that the requested password is not blanks
  
  if (isBlanks(document.getElementById("pwd").value)) {
  
    // display the required error message
	
    alert("An unhandled exception has occurred.\n\n" +
          "Problem:\n\n" +
          "The requested password is blanks.\n\n" +
          "Problem Solution:\n\n" +
		  "Enter a password value.");
  
    // give the element focus
	
	document.getElementById("pwd").focus();
  
    // return control to the calling procedure
	
	return;
  
  }

  // verify that the requested password is not blanks
  
  if (!isAlphaNumeric(document.getElementById("pwd").value)) {
  
    // display the required error message
	
    alert("An unhandled exception has occurred.\n\n" +
          "Problem:\n\n" +
          "The requested password contains an invalid character.\n\n" +
          "Problem Solution:\n\n" +
		  "Remove any non-alphanumeric characters from the password.");
  
    // give the element focus
	
	document.getElementById("pwd").focus();
  
    // return control to the calling procedure
	
	return;
  
  }

  // verify that the first byte of the requested password is valiod
  
  if (!isAlpha(document.getElementById("pwd").value.substr(0,1))) {
  
    // display the required error message
	
    alert("An unhandled exception has occurred.\n\n" +
          "Problem:\n\n" +
          "The first character of the requested password contains an invalid character.\n\n" +
          "Problem Solution:\n\n" +
		  "Change the first character of the requested password to be an alphabetic character between A and Z.");
  
    // give the element focus
	
	document.getElementById("pwd").focus();
  
    // return control to the calling procedure
	
	return;
  
  }

  // verify that requested password is of the minimum length
  
  if (document.getElementById("pwd").value.length < 6) {
  
    // display the required error message
	
    alert("An unhandled exception has occurred.\n\n" +
          "Problem:\n\n" +
          "The requested password is too short.\n\n" +
          "Problem Solution:\n\n" +
		  "The requested password must be at least 6 character in length.");
  
    // give the element focus
	
	document.getElementById("pwd").focus();
  
    // return control to the calling procedure
	
	return;
  
  }
  
  // ask the user one last to confirm their new account settings before submitting to server
  
  if (confirm("Please confirm your new account request.\n\n" + 
              "Contact Information\n\n" + 
              "First name: " + document.getElementById("fnam").value.toUpperCase() + "\n" + 
              "Last name: " + document.getElementById("lnam").value.toUpperCase() + "\n" + 
              "Company name: " + document.getElementById("cnam").value.toUpperCase() + "\n" + 
              "Email address: " + document.getElementById("eml").value.toUpperCase() + "\n\n\n" + 
              "Account Information\n\n" + 
              "Requested user id: " + document.getElementById("uid").value.toUpperCase() + "\n" + 
			  "Requested password: " + document.getElementById("pwd").value.toUpperCase() + "\n\n\n" + 
			  "Is this information correct?")) {
			  
    if (createNewRequest()) {
  
      alert("Your new account request has been received by PTL and will be processed within the next 24 hours.\n\n" + 
            "You will receive an email confirmation when your account is ready for use.\n\n" + 
            "Thank you for using Paschall Truck Lines Online Tracking.");
			
      window.location = "http://www.ptl-inc.com/index.htm";

    }

    else {
	
      alert("An unhandled exception has occurred.\n\n" + 
            "Problem:\n\n" + 
            "The server did not process the new account request.\n\n" +
            "Possible Causes for Problem:\n\n" + 
			"*Server was contacted by did not complete request\n" + 
            "*Server was not contacted because it is not available\n" + 
            "*Server is busy, no open connections available\n\n" + 
            "Problem Solution:\n\n" + 
            "*Wait 30 seconds and try the request again\n\n" + 
            "If this error occurs again, please contact the website support staff (see below).\n\n" +
            "Paschall Truck Lines\n" + 
            "MIS Department\n" + 
            "Telephone: (270) 753-1717\n" + 
            "Email: mis@ptl-inc.com");

    }
	
  }
  
  // return control to the calling procedure

  return;

}

// --------------------------------------------------------------------------------
// function isBlanks()
// if a string of text is all blanks, return true
// if a string of text contains non-blank characters, return false
// --------------------------------------------------------------------------------

function isBlanks(pParm) {

  // delcare some local variables

  var pIndex = new Number();
  var pFoundNonWhiteSpaceChar = new Boolean();

  // initialize the local variables

  pFoundNonWhiteSpaceChar = false;

  // read through the provided bytes looking for non-blank characters

  for(pIndex = 0; pIndex < pParm.length; pIndex++) {

    if(pParm.substr(pIndex,1) != " ") {

      pFoundNonWhiteSpaceChar = true;

      break;

    }

  }

  // clean up any allocated memory

  delete pIndex;

  if(pFoundNonWhiteSpaceChar) {

    // clean up any allocated memory

    delete pFoundNoneWhiteSpaceChar;

    return false;

  }

  // clean up any allocated memory

  delete pFoundNoneWhiteSpaceChar;

  return true;

}

// --------------------------------------------------------------------------------
// function hasBlanks()
// if a string of text has a blank space, return true
// if a string of text contains non-blank characters, return false
// --------------------------------------------------------------------------------

function hasBlanks(pParm) {

  // delcare some local variables

  var pIndex = new Number();
  var pFoundNonWhiteSpaceChar = new Boolean();

  // initialize the local variables

  pFoundNonWhiteSpaceChar = false;

  // read through the provided bytes looking for non-blank characters

  for(pIndex = 0; pIndex < pParm.length; pIndex++) {

    if(pParm.substr(pIndex,1) == " ") {

      pFoundNonWhiteSpaceChar = true;

      break;

    }

  }

  // clean up any allocated memory

  delete pIndex;

  if(pFoundNonWhiteSpaceChar) {

    // clean up any allocated memory

    delete pFoundNoneWhiteSpaceChar;

    return true;

  }

  // clean up any allocated memory

  delete pFoundNoneWhiteSpaceChar;

  return false;

}

// --------------------------------------------------------------------------------
// function isAlphaNumeric()
// if a string of text contains a character other than a-Z or 0-9, return false
// otherwise, return true
// --------------------------------------------------------------------------------

function isAlphaNumeric(pParm) {

  // if the variable sent to this function is not a string
  
  if (typeof pParm != "string") {
  
    return false;
	
  }

  // delcare some local variables

  var pIndex = new Number();
  var pFoundInvalidChar = new Boolean();
  var pTestChar = new String();
  var pValidChars = new String();

  // initialize the local variables
  
  pFoundInvalidChar = false;
  pValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ";

  // read through the provided bytes looking for non-blank characters
  
  for(pIndex = 0; pIndex < pParm.length; pIndex++) {
  
    pTestChar = pParm.substr(pIndex,1);

    if(pValidChars.indexOf(pTestChar) == -1) {
   
      pFoundInvalidChar = true;

      break;

    }

  }

  if(pFoundInvalidChar) {

    // clean up any allocated memory

	delete pIndex;
    delete pFoundInvalidChar;
	delete pTestChar;
	delete pValidChars;

	// return control to the calling procedure
	
    return false;

  }

  // clean up any allocated memory

  delete pIndex;
  delete pFoundInvalidChar;
  delete pTestChar;
  delete pValidChars;

  // return control to the calling procedure
	
  return true;

}

// --------------------------------------------------------------------------------
// function isAlpha()
// if a string of text contains a character other than a-Z, return false
// otherwise, return true
// --------------------------------------------------------------------------------

function isAlpha(pParm) {

  // if the variable sent to this function is not a string
  
  if (typeof pParm != "string") {
  
    return false;
	
  }

  // delcare some local variables

  var pIndex = new Number();
  var pFoundInvalidChar = new Boolean();
  var pTestChar = new String();
  var pValidChars = new String();

  // initialize the local variables
  
  pFoundInvalidChar = false;
  pValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

  // read through the provided bytes looking for non-blank characters
  
  for(pIndex = 0; pIndex < pParm.length; pIndex++) {
  
    pTestChar = pParm.substr(pIndex,1);

    if(pValidChars.indexOf(pTestChar) == -1) {
   
      pFoundInvalidChar = true;

      break;

    }

  }

  if(pFoundInvalidChar) {

    // clean up any allocated memory

	delete pIndex;
    delete pFoundInvalidChar;
	delete pTestChar;
	delete pValidChars;

	// return control to the calling procedure
	
    return false;

  }

  // clean up any allocated memory

  delete pIndex;
  delete pFoundInvalidChar;
  delete pTestChar;
  delete pValidChars;

  // return control to the calling procedure
	
  return true;

}

// --------------------------------------------------------------------------------
// function isNumeric()
// if a string of text contains a character other than 0-9, return false
// otherwise, return true
// --------------------------------------------------------------------------------

function isNumeric(pParm) {

  // if the variable sent to this function is not a string
  
  if (typeof pParm != "string") {
  
    return false;
	
  }

  // delcare some local variables

  var pIndex = new Number();
  var pFoundInvalidChar = new Boolean();
  var pTestChar = new String();
  var pValidChars = new String();

  // initialize the local variables
  
  pFoundInvalidChar = false;
  pValidChars = "0123456789";

  // read through the provided bytes looking for non-blank characters
  
  for(pIndex = 0; pIndex < pParm.length; pIndex++) {
  
    pTestChar = pParm.substr(pIndex,1);

    if(pValidChars.indexOf(pTestChar) == -1) {
   
      pFoundInvalidChar = true;

      break;

    }

  }

  if(pFoundInvalidChar) {

    // clean up any allocated memory

	delete pIndex;
    delete pFoundInvalidChar;
	delete pTestChar;
	delete pValidChars;

	// return control to the calling procedure
	
    return false;

  }

  // clean up any allocated memory

  delete pIndex;
  delete pFoundInvalidChar;
  delete pTestChar;
  delete pValidChars;

  // return control to the calling procedure
	
  return true;

}

// --------------------------------------------------------------------------------
// function isValidEmailAddress()
// if a string of text contains an invalid email address, return false
// otherwise, return true
// --------------------------------------------------------------------------------

function isValidEmailAddress(emailStringValue){

  // declare some local variables
  
  var emailTest = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  
  // validate the provided email address against our regular expression

  if (emailTest.test(emailStringValue)) {

    // free-up any allocated storage
  
    delete emailTest;
	
    // return control to our calling procedure

    return true;
	
  }

  // free-up any allocated storage
  
  delete emailTest;

  // return control to the calling procedure

  return false;
 
}

// --------------------------------------------------------------------------------
// function userIdAvailable()
// if the provided user id is not already being used, return true
// otherwise, return true
// --------------------------------------------------------------------------------

function userIdAvailable(userIdValue){

  // declare some local variables

  var pCurrentTime = new Date();
  var pURL = new String();
  var pParms = new String();

  // format the AJAX request variables
  
  pURL = "/cgipubv3/chkUsrId.cgi";
  pParms = "userId=" + userIdValue.toUpperCase() + 
           "&requestId=" + pCurrentTime.getTime(); 
  
  // run the AJAX request (using Prototype framework v1.5.1)		   
		   
  new Ajax.Request(
    pURL, {
      asynchronous: false,
      method: "post",
      parameters: pParms,
      onSuccess: parseAJAXResponse,
      onFailure: parseAJAXFailure
    }
  );
  
  // parse the result returned from the web server
  
  if(requestComplete) {

    // save the value of the text returned from the server
  
    if (requestText == "TRUE") {

      // clean up any allocated memory
  
      delete pCurrentTime;
      delete pURL;
      delete pParms;
	
	  return true;
	  
	}

  }

  // clean up any allocated memory
  
  delete pCurrentTime;
  delete pURL;
  delete pParms;
				   
  // return control to the calling procedure
  
  return false;

}

// --------------------------------------------------------------------------------
// function createNewRequest()
// if the account request processed normally, return true
// otherwise, return true
// --------------------------------------------------------------------------------

function createNewRequest(){

  // if any required element does not exist in the DOM

  if (!document.getElementById("fnam") ||
      !document.getElementById("lnam") ||
      !document.getElementById("cnam") ||
      !document.getElementById("eml") ||
      !document.getElementById("uid") ||
      !document.getElementById("pwd")) {
	  
    // this function fails
	
	return false;
	
  }

  // declare some local variables

  var pCurrentTime = new Date();
  var pURL = new String();
  var pParms = new String();

  // format the AJAX request variables
  
  pURL = "/cgipubv3/crtNewReq.cgi";
  pParms = "first=" + document.getElementById("fnam").value.toUpperCase() + 
           "&last=" + document.getElementById("lnam").value.toUpperCase() + 
           "&company=" + document.getElementById("cnam").value.toUpperCase() + 
           "&email=" + document.getElementById("eml").value.toUpperCase() + 
		   "&userId=" + document.getElementById("uid").value.toUpperCase() + 
		   "&password=" + document.getElementById("pwd").value.toUpperCase() + 
           "&requestId=" + pCurrentTime.getTime(); 
  
  // run the AJAX request (using Prototype framework v1.5.1)		   
		   
  new Ajax.Request(
    pURL, {
      asynchronous: false,
      method: "post",
      parameters: pParms,
      onSuccess: parseAJAXResponse,
      onFailure: parseAJAXFailure
    }
  );
  
  // parse the result returned from the web server
  
  if(requestComplete) {

    // save the value of the text returned from the server
  
    if (requestText == "TRUE") {

      // clean up any allocated memory
  
      delete pCurrentTime;
      delete pURL;
      delete pParms;
	
	  return true;
	  
	}

  }

  // clean up any allocated memory
  
  delete pCurrentTime;
  delete pURL;
  delete pParms;
				   
  // return control to the calling procedure
  
  return false;
  
}

// --------------------------------------------------------------------------------
// function parseAJAXResponse()
// parse a successful response to a server AJAX request
// --------------------------------------------------------------------------------

function parseAJAXResponse(originalRequest) {

  // import the value of the AJAX response
  // into the global response variable

  requestText = originalRequest.responseText;

  // if the reponse was blanks, parse the 
  // result as a failure
  
  if(isBlanks(requestText)) {

    parseAJAXFailure();

  }

  // mark the global variables as successful
  
  else {

    requestComplete = true;

  }

  return;

}

// --------------------------------------------------------------------------------
// function parseAJAXFailure()
// parse a failed server response to an AJAX request
// --------------------------------------------------------------------------------

function parseAJAXFailure() {

  // set the global variables to failure conditions

  requestComplete = false;
  requestText = "";

  return;
 
}