//JavaScript File

/*
    Author: Codey Shriner
    Date:   January 24, 2008

    All content herin is the intellectual property of, and only of, the auther (Codey Shriner).
    No modifications may be made without the express written consent of said author.

    This script is used to verify the model application form.

    Copyright © 2007 - 2008 Codey Shriner
*/

function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.cellphone);
  reason += validateDate(theForm.dob);
  reason += validateEmpty(theForm.lname);
  reason += validateEmpty(theForm.fname);
  reason += validateEmpty(theForm.email);
  reason += validateEmpty(theForm.airport);
  reason += validateEmpty(theForm.city);
  reason += validateEmpty(theForm.state);
  reason += validateEmpty(theForm.pic1);
  reason += validateEmpty(theForm.pic2);
  reason += validateEmpty(theForm.pic3);
  reason += validateEmpty(theForm.pic4);
  reason += validateEmpty(theForm.pic5);


  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  //else return true;
  else return document.getElementById('submit').disabled=true;

  //alert("All fields are filled correctly");
  //return false;
}
function validateEmpty(fld) {
    var error = "";
    var fldName = "";

    fldName = fld.name;
    
    if (fld.name == "lname") {
        fldName = 'Last Name';
    }

    if (fld.name == "fname") {
        fldName = 'First Name';
    }

    if (fld.name == "pic1") {
        fldName = 'Face Picture';
    }

    if (fld.name == "pic2") {
        fldName = 'Face and Upper Body Picture';
    }

    if (fld.name == "pic3") {
        fldName = 'Full Body Nude with Face Picture';
    }

    if (fld.name == "pic4") {
        fldName = 'Nude from Backside Picture';
    }

    if (fld.name == "pic5") {
        fldName = 'Penis (Rigid or Erect) Picture';
    }

    if (fld.value.length == 0) {
        fld.style.background = 'Yellow';
        //error = "The required field has not been filled in.\n"
        error = "The required field (" + fldName + ") has not been filled in.\n";
    }

    else fld.style.background = 'White';

    return error;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}
function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    }
    else
        if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    }
    else
    if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    }
    else fld.style.background = 'White';
    return error;
}
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    }
    else
    if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    }
    else
    if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure it is just 10 numbers.\n";
        fld.style.background = 'Yellow';
    }
    else fld.style.background = 'White';
    return error;
}
function validateDate(fld) {
	var error = "";
	var validformat = /^\d{4}-\d{2}-\d{2}$/ ;//Basic check for format validity

	if (!validformat.test(fld.value)) {
		error = "Invalid Date Format. Please correct and submit again.\n";
		fld.style.background = 'Yellow';
	}
    else fld.style.background = 'White';
	return error;
}
