// formChecks.js

// CONSTANT STRING DECLARATIONS
// (grouped for ease of translation and localization)

//******************************************************//
//This class is written by Philip! Feel free to email	//
//to phil@ng.tc                                      	//
//DATE: March 10,2001                                	//
//							//
//Modified: August 09, 2001 by Jiwoong Kim		//
//*******************************************************/


/******************  Regular Expressions for different field  *********************/
//
//	regular expressions need to be updated. currently not implemented
//
FirstName_Rexp=/^[a-zA-Z]+$/
LastName_Rexp=/^[a-zA-Z]+$/
Name_Rexp = /^[a-zA-Z\s]+$/
Email_Rexp=/^[\w.]+\@[\w]+\.[a-zA-Z.]+$/
Title_Rexp=/^.+$/
Address_Rexp=/^.+$/
City_Rexp=/^[a-zA-Z\s\-]+$/
State_Rexp=/^[a-zA-Z\s]+$/
Zip_Rexp=/^[0-9]+$/
Country_Rexp=/^[a-zA-Z\s]+$/
Phone_Rexp=/^[0-9]{3}\-[0-9]{3}\-[0-9]{4}$/
URL_Rexp=/^[\w.?#_\-\/~$%]+\.[\w.?#_\-\/~$%]+\.[\w.?#_\-\/~$%]+$/
UserName_Rexp=/^[a-zA-Z0-9]+$/
PassWord_Rexp=/^[a-zA-Z0-9]+$/
Age_Rexp=/^[0-9]+$/
Comments_Rexp=/^.+$/
/*********************************************************************************/


/******************    m is an abbreviation for "missing"   **********************/
var mPrefix = "You did not enter a value into the "
var mSuffix = " field. This is a required field. Please enter it now."
/*********************************************************************************/


/*****************    s is an abbreviation for "string"    ***********************/
var sLastName = "Last Name"
var sFirstName = "First Name"
var sName = "Name"
var sTitle = "Title"
var sEmail = "Email"
var sAddress = "Address"
var sCity = "City"
var sState = "State"
var sCountry = "Country"
var sZip = "ZIP Code"
var sPhone = "Phone Number"
var sUserName = "UserName"
var sPassWord = "Password"
var sURL = "URL"
var sAge = "Age"
var sComments = "Comments"
/*********************************************************************************/


/*************************    i is an abbreviation for "invalid"    ****************************************/
var iLastName = "Please enter a valid Last Name(space is not allowed)!"
var iFirstName= "Please enter a valid First Name(space is not allowed)!"
var iName     = "Invalid character/s is/are in the Name field!"
var iEmail    = "This field must be a valid email address\n(like 'something@something.something') \n @@ are not allowed!"
var iTitle    = "Invalid character/s is/are in the Title field!"
var iAddress  = "Invalid character/s is/are in the Address field!"
var iCity     = "Invalid character/s is/are in the City field! (digits are not allowed)!"
var iState    = "Invalid character/s is/are in the State field! (digits are not allowed)!"
var iZip      = "This field must be a 5 digit U.S. ZIP Code (like 94043)!"
var iCountry  = "Invalid character/s is/are in the Country field! (digits are not allowed)!"
var iPhone    = "This field must be a 12 digit U.S. phone number (like 415-555-1212)!"
var iURL      = "Invalid character/s is/are in the URL field!"
var iUserName = "Invalid character/s is/are in the UserName field!"
var iUserName2 = "UserName must have at least 6 characters!"
var iPassWord  = "Invalid character/s is/are in the Password field!"
var iPassWord2 = "Password must have at least 6 characters!"
var iPassWord3 = "Password does not match!"
var iD_Space   = "Double space or the field has just a space is not allowed!\nPlease delete the space!"
var iAge      = "Invalid character/s is/are in the Age field (only digits are allowed)!"
var iComments      = "Invalid character/s is/are in the Comments field!"
/*************************************************************************************************************/


// Force forward
function gogogo()
{
window.history.forward(1)
}


// Check whether string s is empty.
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// Check whether string s is double space.

function isDouble_Space(s)
{
	   if(s.charAt(0) == ' ')
                return true

           for(i=0; i<s.length ;i++)
                {
                if(s.charAt(i) ==' ' && s.charAt(i+1) ==' ')
                return true
                }
return false
}


// Notify user that required field theField is empty.
// String s describes expected contents of theField.value.
// Put focus in theField and return false.

function warnEmpty (theField, s)
{
    alert(mPrefix + s + mSuffix)
    theField.focus()
}


// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, pu focus in it, and return false.

function warnInvalid (theField, s)
{
    alert(s)
    theField.focus()
}

//-------------------------------- checkForm --------------------------------
//  Function: 
//  Precondition: 
//---------------------------------------------------------------------------
function checkForm(Name, theField, condition)
{
var check = 0;
if(condition == 1)
  	if(isEmpty(theField.value))
    		{
   		warnEmpty(theField, eval("s" + Name));
   		return false;
    		}
    	else
   		check = 1;
else
	if(!isEmpty(theField.value))
    		check = 1;

if(check)
	{
	if (Name == "Email")
		{
  		var newArray = theField.value.split(",");
  		var Arraylength = newArray.length;
		var regExp = /^[\s]*/;
  		for(i=0; i<Arraylength; i++)
			{
  			if(newArray[i])
				{
				trimdStr = newArray[i].replace(regExp, "");  // trim leading white space
  				result = Email_Rexp.exec(trimdStr);
				}
 			else
 				result = 1;
 			if(!result)
				{
 				warnInvalid (theField, iEmail);
 				return false;
                    		}
                	} // End for
		}
	else
		{
		if (Name == "PassWord" || Name == "UserName")
			{
  			if (theField.value.length < 6)
        			{
       				warnInvalid (theField, eval("i" + Name + "2"));
        			return false;
        			}
			}
  		result = (eval(Name + "_Rexp")).exec(theField.value);
 		if(!result)
			{
 			warnInvalid (theField, eval("i" + Name));
 			return false;
        		}
		}
        }
return true;
} // End of checkForm 


function checkComments(Name, theField, condition)
{
var check = 0;
if(condition == 1)
  	if(isEmpty(theField.value))
    		{
   		warnEmpty(theField, eval("s" + Name));
   		return false;
    		}
        
return true;
} // End of checkForm 


//This funciton is used to double check the PassWord!
function checkRetypePassWord(theField1,theField2,condition)
{
 var check=0
if(condition==1)
{
  if(isEmpty(theField1.value))
    {
   warnEmpty(theField1, sPassWord)
   return false
    }
    else
   {
   check=1
   }
}
else
{
if(!isEmpty(theField1.value))
    {check=1}
}

if(check) {
if(theField1.value != theField2.value){
                warnInvalid (theField2, iPassWord3)
                return false
                                       }
           }
return true
}//End function checkRetypePassWord

