

/*
    One required arguement and one optional arguement.
    Arg1 - The field which contains the email address
    Arg2 - The regexp format for the email (optional)
*/
function isValidEmail() {
    var emailField    = isValidEmail.arguments[0];
    var emailFormat   = /^.+@.+\..{2,4}$/;
    var emailBadChars = /[\(\)\<\>\,\|\;\:\\\/\"\[\]\ ]/;
    if (isValidEmail.arguments.length > 1){
      emailFormat = isValidEmail.arguments[1];
    }
    trim(emailField);
    return ((emailField.value.match(emailFormat))&&(!emailField.value.match(emailBadChars)));
  }

/*
    One required arguement and one optional arguement.
    Arg1 - The field which contains the phone number
    Arg2 - The regexp format for the phone number (optional)
*/
function isValidPhone()
{
    var phoneFormat = /^\(\d{3}\) \d{3} \d{4}$/;
    var phoneField = isValidPhone.arguments[0];
    if (isValidPhone.arguments.length > 1) {
        phoneFormat = isValidPhone.arguments[1];
    }

    trim(phoneField);
    return null!=phoneField.value.match(phoneFormat);
}

/*
    Tries to make the following string into a X digit phone number
    consisting only of digits.
    Arg1 - The field which contains the phone number
    Arg2 - The number of digits in the phone number - this
           is optional and defaults to 10
    Returns true if successfull.
*/
function makePhoneX()
{
    var phoneField = makePhoneX.arguments[0];
    var numberDigits = 10;
    var phoneNo = null;
    if (makePhoneX.arguments.length > 1) {
        numberDigits = makePhoneX.arguments[1];
    }

    trim(phoneField);
    phoneNo = phoneField.value.replace(/[^\d]/g, "");
    if (null==phoneNo) {
        return false;
    }

    if ( phoneNo.length== numberDigits ) {
        phoneField.value = phoneNo;
    } else {
        return false;
    }
    return true;
}


/*
    Get rid of preceding and trailing whitepace from the field.
*/
function trim(txtObj)
{
    //Skip select,radio,checkbox,hidden,reset,submit
    if (txtObj.type.match(/select|radio|checkbox|hidden|reset|submit/i)) {
        return;
    }

    if (txtObj!=null) {
        if (txtObj.value.length > 0) {
            txtObj.value=txtObj.value.replace(/^\s+/, "");
        }
        if (txtObj.value.length > 0) {
            txtObj.value=txtObj.value.replace(/\s+$/, "");
        }
    }
    return;
}


/*
    Gets rid of preceding and trailing whitepace from the field
    then returns whether the field has any size at all.

    Also returns woks for select boxes if the selected value is empty
    of the selectedIndex is -1
*/
function isEmpty( fieldVar )
{
    if (fieldVar.type.match(/select/i)) {
        if (fieldVar.selectedIndex==-1) {
            return true;
        }
        return fieldVar.options[fieldVar.selectedIndex].value.length==0;
    } else {
        trim( fieldVar )
        return (0==fieldVar.value.length);
    }
}


/*
    Gets rid of preceding and trailing whitepace from the field
    then returns whether the field is in the range.
    If the field cannot be converted to a number - then false is
    returned.
*/
function inNumericRange(field, low, high)
{
    if (field!=null) {
        while ( field.value.match(/\s+/)) {
            field.value=txtObj.value.replace(/\s+/, "");
        }
    }

    //Must be all numerics: 01234567890Ee+-.,
    //Check is done becuase if the field starts with a number
    //then only what is parsed gets returned.
    if (! field.value.match(/^[01234567890Ee\+\-\.\,]+$/)) {
        return false;
    }

    var n = parseFloat(field.value);
    if (isNaN(n)) {
        return false;
    }
    return (n >= low) && (n <= high);
}


/*
    Checks required fields. Returns true if all ok, false if not.
    arg1 - The field to check
    arg2 - The error message to display ( or null to not display a msg)
    arg(oddnumber) - The field to check
    arg(evennumber) - The error message to display ( or null to not display a msg)

    Each field will first be trimmed.
*/
function checkRequired() {
    var i;
    var loop = 0;
    for( i=0; i<checkRequired.arguments.length ; i+=2) {
        //isEmpty already trims for me
        if (isEmpty(checkRequired.arguments[i])) {
            if ( i+1 < checkRequired.arguments.length){
                if ( null != checkRequired.arguments[i+1]) {
                    alert(checkRequired.arguments[i+1]);
		            checkRequired.arguments[i].focus();
                }
            }
            return false;
        }
    }

    return true;
}



/*
    Validate Size
    arg1 - The field to check
    arg2 - The minimum length
    arg3 - The max length
    The field will first be trimmed.
*/
function isValidLength() {
    var loop = 0;
    trim(isValidLength.arguments[0]);
    return (isValidLength.arguments[0].value.length >= isValidLength.arguments[1] &&
           isValidLength.arguments[0].value.length <= isValidLength.arguments[2]);

    return true;
}


/*
    Populate a select list
    arg1 - The list to change
    arg2 - A list of Option objects
*/
function a_makeSelect(aSelect, values) {
	if (aSelect==null || value==null){
        return;
	}

	clearSelect(aSelect);

	var i;
	for (i=0;i<values.length;i++) {
		aSelect.options[i] = values[i];
	}
}


/*
    Clear a select list
    arg1 - The list to change
*/
function a_clearSelect(aSelect) {
	if (aSelect==null){
        return;
	}

	var i;
	for (i=0;i<aSelect.options.length;i++) {
		aSelect.options[i] = null;
	}
}

