

// Declare Global Variables

	var formAlertMessage='There were Errors!';
	var formSubmit=1;
	var defaultEmptyOK = true;		//	Order quantity can be empty
	var orderTotal = 0;			// 	Counts the number of items ordered				
	var orderValid = false;			//	Order becomes valid once orderTotal > 0
/*
	throwError gets called each time there is an error in the
	validation routines.
*/
function throwError (theField,theMessage) {
	formAlertMessage=formAlertMessage + "\n - " + theMessage;
	formSubmit=0;
	switchStyle(theField);
}

function switchStyle (theField) {
	theField.style.backgroundColor='#FFe4E1';
	theField.style.color='#000000';
}



/*
	formAlert displays the alert message and clears necessary variables to start
	the validation over.
*/

function formAlert() {
	alert(formAlertMessage);
	formAlertMessage='There were Errors!';
	formSubmit=1;
}


/*
	formRequired makes sure a field has been completed by the user.   In order
	for this function to work with select boxes the empty value must be set to 0
	in the form.
*/

function formRequired(theField,theMessage,theBG) {
	theField.value = theField.value.replace(/\s+$|^\s*/gi, "");
	if(theField.value == '' || theField.value == '-1' || theField.value == '0') {
		throwError(theField,theMessage);
	} else {
		theField.style.backgroundColor=theBG;
		theField.style.color='#000000';
	}
}


// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// form field must be composed only of numbers and letters.
function formIsAlphaNumeric(theField,theMessage,theBG) {
	theField.value = theField.value.replace(/\s+$|^\s*/gi, "");
	isAlphaNum = new RegExp ("[^0-9a-zA-Z \(\)\-\.\/]");
	if (isAlphaNum.test(theField.value)) {
		throwError(theField,theMessage);
	}
}

// form field must be composed only of numbers and letters.
function formIsSame(field1,field2,theMessage) {

	if(field1.value != field2.value) {
		throwError(field2,theMessage);
		switchStyle(field1);
	}
}

function formIsEmail (theField,theMessage) {
	var email = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
 	if(!email.test(theField.value)) {
		throwError(theField,theMessage);
	} else {
		theField.style.backgroundColor=theBG;
		theField.style.color='#000000';
	}
 }



function isSignedInteger (s) {   
	if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}

function isPositiveInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isPositiveInteger.arguments.length > 1)
        secondArg = isPositiveInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a positive, not negative, number

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) > 0) ) );
}

function isNonnegativeInteger (theField, theBG)
{   var secondArg = defaultEmptyOK;
	var s = theField.value;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

	validNumber = (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
	if (!validNumber)
		throwError(theField, "Please enter a valid order quantity.");
	else {
		// Valid! Change colour and update validity flag
		theField.style.backgroundColor=theBG;
		theField.style.color='#000000';
		
		// Check if a valid order quantity already entered. If not, check if this quantity is valid
		if (!orderValid)
			orderValid = (isPositiveInteger(s));
		}
    return validNumber;
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}


function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

function isValidOrder(theField) {
	// Once all quantities checked, check that minimum total quantity ordered
	if (!orderValid) {
		throwError(theField, "You need to order at least 1 item.");
		formSubmit=0;
		}
}

