// JavaScript Document

// DEFINE VARIABLES

// whitespace characters
var whitespace = " \t\n\r";


function LimitTextLength(objTextArea, maxLength)
{
	if (objTextArea.value.length > maxLength) objTextArea.value = objTextArea.value.substring(0, maxLength);
}
/****************************************************************/

function ForcePostCode(objField, FieldName)
{
	var isValid = true;

	isValid = ForceEntry(objField, FieldName)
	
	if (isValid) isValid = ForceLength(objField, FieldName, 4, 4);		
    	
	if (isValid) isValid = ForceNumeric(objField, FieldName); 
	
	return isValid;

}

/****************************************************************/

function ForceCreditCardNumber(objField, FieldName)
{
	var isValid = true;

	isValid = ForceEntry(objField, FieldName)
	
	if (isValid) isValid = ForceLength(objField, FieldName, 16, 16);		
    	
	if (isValid) isValid = ForceNumeric(objField, FieldName); 
	
	return isValid;

}

/****************************************************************/

// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c ... in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))

    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}


function ShowError(theError, objField)
{
	alert(theError);
	//objField.focus(); causes problem for freight region select
}

/****************************************************************/

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

//****************************************************************/

// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
	// Check that current character isn't whitespace.
	var c = s.charAt(i);

	if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

//****************************************************************/

// Checks to see if a required field is blank.  If it is, a warning
// message is displayed...

function ForceEntry(objField, FieldName)
{
	var strField = new String(objField.value);
	if (isWhitespace(strField)) 
	{
		ShowError("Please enter your \"" + FieldName + "\".", objField);
		return false;
	}

	return true;
}


function ForceSelection(objDropDown, FieldName)
{
	if ((objDropDown.value == "") || (objDropDown.value =="0"))
	{
		ShowError("Please select \"" + FieldName + "\".", objDropDown);
		return false;
	}

	return true;
}
     

function ForcePhone (objField, FieldName) 
{
	var isValid = true;

	isValid = ForceEntry(objField, FieldName)
	
	/*if (isValid) isValid = ForceNumeric(objField, FieldName); */

	/* Not much validation here - just check it starts with 0 */
	if (isValid)
	{
		var strField = new String(objField.value);
		if ((strField.charAt(0) != '0') && (strField.charAt(0) != 'o'))
		{
			ShowError(FieldName + " must begin with a 0.  Please enter a valid \"" + FieldName + "\".", objField);
			isValid = false;
		}
	}

	return isValid;
}       


function ForceEmail (objField, FieldName) 
{
	var isValid = true;

	isValid = ForceEntry(objField, FieldName)
	
	if ((isValid) && (! isEmail(objField.value)))
	{
		ShowError("Please enter a valid \"" + FieldName + "\".", objField);
		isValid = false;
	}
	
	return isValid;
}       

//****************************************************************/

function ForceUsername (objField, FieldName) 
{
	var isValid = true;

	isValid = ForceEntry(objField, FieldName)
	
	if (isValid) isValid = ForceAlphanumeric(objField, FieldName); 
	
	if (isValid) isValid = ForceLength(objField, FieldName, 4, 20);		
    	
	return isValid;
}       

//****************************************************************/

function ForcePassword (objField, FieldName) 
{
	var isValid = true;

	isValid = ForceEntry(objField, FieldName)
	
	if (isValid) isValid = ForceAlphanumeric(objField, FieldName); 
	
	if (isValid) isValid = ForceLength(objField, FieldName, 4, 10);		
    	
	return isValid;
}       
//****************************************************************/

function ForceLength(objField, FieldName, nMinLength, nMaxLength)
{
	var strFieldValue = new String(objField.value);
	
	if ((strFieldValue.length < nMinLength) || (strFieldValue.length > nMaxLength)) 
	{
		if (nMinLength == nMaxLength)
		{
			ShowError("The \"" + FieldName + "\" is the wrong length.  Please enter a \"" + FieldName + "\" that is " + nMinLength + " characters long.\n", objField);
		}
		else
		{
			ShowError("The \"" + FieldName + "\" is the wrong length.  Please enter a \"" + FieldName + "\" between " + nMinLength + " and " + nMaxLength + " characters long.\n", objField);
		}
		
		return false;
	}
   	
	return true;
}

function ForceAlphanumeric(objField, FieldName)
{
	var strFieldValue = new String(objField.value);
	var illegalChars = /\W/; // allow letters, numbers, and underscores

	if (illegalChars.test(strFieldValue)) 
	{
		ShowError("The \"" + FieldName + "\" contains illegal characters.  Please enter a \"" + FieldName + "\" containing only letters, numbers and underscores. \n", objField);
		return false;
	} 
	
	return true;

}

function ForceNumeric(objField, FieldName)
{
	var strField = new String(objField.value);

	var i = 0;

	for (i = 0; i < strField.length; i++)
		if (strField.charAt(i) < '0' || strField.charAt(i) > '9') 
		{
			ShowError("The \"" + FieldName + "\" contains illegal characters.  Please enter a \"" + FieldName + "\" containing only numbers. \n", objField);
			return false;
		}

	
	return true;
}


/************/


function ValidateContactDetails(theForm)
{
	var CanSubmit = false;

	/* validate contact details */	
	CanSubmit = ForceEntry(theForm.company, "Name");
	CanSubmit = ForceEmail(theForm.email, "Email Address");
	if (CanSubmit) CanSubmit = ForcePhone(theForm.telephoneNumber, "Telephone number");
	/* Validate cellphone if one has been entered */
	if ((CanSubmit) && (theForm.cellphoneNumber.value != ""))
	{
		if (CanSubmit) CanSubmit = ForcePhone(theForm.cellphoneNumber, "Cellphone number");
	}
	/* Validate fax number if one has been entered */
	if ((CanSubmit) && (theForm.faxNumber.value != ""))
	{
		if (CanSubmit) CanSubmit = ForcePhone(theForm.faxNumber, "Fax number");
	}
	
	/* validate postal address */
	if (CanSubmit) CanSubmit = ForceEntry(theForm.street_address, "Street Address");
	if (CanSubmit) CanSubmit = ForceEntry(theForm.city, "City");
	//if (CanSubmit) CanSubmit = ForcePostCode(theForm.postcode, "Postcode"); 

	return CanSubmit;
}

/********************/
function ValidateLoginForm(theForm)
{
	var CanSubmit = false;

	CanSubmit = ForceEntry(theForm.UID,"Username");
    if (CanSubmit) CanSubmit = ForceEntry(theForm.PWD,"Password");

	return CanSubmit;
}


