/*
JavaScript Form routines
Date: 10/15/2003
Author: Greg Epley
*/

var tempobj;
var theField = fType = theValue = theType = sMsg = "";
var reqFlag = false;


/*
This function, passed a string value containing numeric digits, filters the passed string for any non-numerics and returns the filtered string.
*/
function NumStr(theString)
{ // start of function
var j = 0;
var ch = "";
var sReturn = "";

if (theString.length > 16)
{ // theString length > 16
return sReturn;
} // end of theString length > 16
else
{ // theString length <= 16
for (j=0;j<theString.length;j++)
{ // start of for loop
ch = theString.charAt(j);
if ((ch >= "0") && (ch <= "9"))
{ // start of valid numeric character handler
sReturn = sReturn + ch;
} // end of valid numeric character handler
} // end of for loop
} // end of theString length <=16
return sReturn;
} // end of function


function checkForm(which)
{ //start of function
var formDone = false;
if (document.images)
{ // document.images = true
for (i=0;i<which.length;i++)
{ // start of for loop
tempobj = which.elements[i];
var ok = true;
reqFlag = false; // assume the field is not required
theField = tempobj.name; // get the field name
if (tempobj.name.charAt(0) == "*") // if 1st char in name is asterisk, field is required
{ // start of 1st char = asterisk
reqFlag = true; // set required flag = true
theField = tempobj.name.substr(2,theField.length-2); // assume the rest of the name after the asterisk is the field name
} // end of 1st char = asterisk
fType = theField.toLowerCase(); // forces the field name to lowercase for easier matching
theValue = tempobj.value;
theType = tempobj.type;
if ( (fType.length == 0) || 
(fType == "form_submit") )
{
formDone = true;
break;
}
if ( (fType == "type of project") && (theValue.charAt(0) == "-") )
{
sMsg = "Please select a Type of Project from the combo box.";
ok = false;
break;
}
else if ( (fType == "purpose of contact") && (theValue.charAt(0) == "-") )
{
sMsg = "Please select a Purpose of Contact from the combo box.";
ok = false;
break;
}
else if ( (fType == "product name") && (theValue.charAt(0) == "-") )
{
sMsg = "Please select a Product Name from the combo box.";
ok = false;
break;
}
else if (fType == "email address")
{
if ( (theValue == "") && (reqFlag) )
{
sMsg = "Please enter your email address in the Email edit box.";
ok = false;
break;
}
ok = emailCheck(theValue);
if (ok == false) { break; }
document.theForm.email.value = theValue;
}
else if ( (fType == "name") && (theValue == "") && (reqFlag) )
{
sMsg = "Please enter your name in the Name edit box.";
ok = false;
break;
}
else if (fType == "phone")
{
ok = phoneCheck(theValue);
if (ok == false) { break; }
}
else if ( (fType == "version #") && (theValue == "") && (reqFlag) )
{
sMsg = "Please enter the product version number, located in the 'About' or 'Info' dialog within the software, in the Version # edit box.";
ok = false;
break;
}
else if ( (fType == "purpose") && (theValue == "") && (reqFlag) )
{
sMsg = "Please enter the project purpose in the Project Purpose edit box.";
ok = false;
break;
}
else if ( (fType == "look/feel") && (theValue == "") && (reqFlag) )
{
sMsg = "Please enter the project look/feel in the Project Look/Feel edit box.";
ok = false;
break;
}
else if ( (fType == "features/capabilities") && (theValue == "") && (reqFlag) )
{
sMsg = "Please enter the project features/capabilities in the Project Features/Capabilities edit box.";
ok = false;
break;
}
else if ( (fType == "timeframe") && (theValue == "") && (reqFlag) )
{
sMsg = "Please enter the project timeframe in the Project Timeframe edit box.";
ok = false;
break;
}
else if ( (fType == "budget") && (theValue == "") && (reqFlag) )
{
sMsg = "Please enter the project budget in the Project Budget edit box.";
ok = false;
break;
}
else if ( (fType == "title") && (theValue == "") && (reqFlag) )
{
sMsg = "Please enter a message title in the Title edit box.";
ok = false;
break;
}
else if ( (fType == "message") && (theValue == "") && (reqFlag) )
{
sMsg = "Please enter your message in the Message edit box.";
ok = false;
break;
}
} // end of for loop
if (ok == false)
{ // start of ok = false
alert(sMsg);
tempobj.focus();
} // end of ok = false
if (formDone)
{ // start of formDone
document.theForm.submit();
} // end of formDone
} // end of document.images = true block
else
{ // start of document.images = false
} // end of document.images = false
} // end of function


function reset()
{ // start of function
alert("reset");
if (document.images)
{ // document.images = true
for (i=0;i<which.length;i++)
{ // start of for loop
var tempobj = which.elements[i];
theField = tempobj.name;
fType = theField.toLowerCase();
theValue = tempobj.value;
if ( (fType.length == 0) || 
(fType == "form_submit") )
{
break;
}
if (tempobj.type == "text")
{
tempobj.value = "";
}
} // end of for loop
} // end of document.images = true;
} // end of function


function emailCheck (emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* 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. */

sMsg = "Email address seems incorrect.  Check to see it is in the format 'username@hostname.domain'.";
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
sMsg = "This username contains invalid characters.";
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
sMsg = "This domain name contains invalid characters.";
return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid

sMsg = "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) {
sMsg = "Destination IP address is invalid!";
return false;
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
sMsg = "The domain name does not seem to be valid.";
return false;
   }
}

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

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
sMsg = "The address must end in a well-known domain or two letter " + "country.";
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
sMsg = "This address is missing a hostname!";
return false;
}

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


function phoneCheck (phoneStr)
{ // start of function
var area = prefix = suffix = "";
var pos = 0;
var phoneNum = NumStr(phoneStr);

if ( ( (phoneNum.length < 10) && (reqFlag) ) || 
( (phoneNum.length > 0) && (phoneNum.length < 10) ) )
{
sMsg = "Please enter at least a 10 digit phone number in the Phone edit box.";
return false;
}
else
{
var pos = phoneNum.length - 4;
suffix = phoneNum.substr(pos,4);
phoneNum = phoneNum.substr(0,phoneNum.length-4);
pos = pos -3;
prefix = phoneNum.substr(pos,3);
phoneNum = phoneNum.substr(0,phoneNum.length-3);
pos = pos - 3;
area = phoneNum.substr(pos,3);
phoneNum = phoneNum.substr(0,phoneNum.length-3);
if ( (area.length == 0) && (prefix.length == 0) && (suffix.length == 0) )
{
phoneNum = "";
}
else
{
phoneNum = "(" + area + ") " + prefix + "-" + suffix;
}
tempobj.value = phoneNum;
return true;
}
} // end of function


function CloseWin()
{ // start of function
close();
return true;
} // end of function


function GoBackOnePage()
{ // start of function
history.go(-1);
return true;
} // end of function
