//     Author: Iain Bell
//  Copyright: (c)2001-2004 EON Visual Media Ltd
//    Project: Email Form
//    Created: 2003-05-14
//      RCSId: $Id: ContactCheck.js,v 1.4 2004/10/25 11:50:45 itb Exp $

function EON_ValidatePhoneNumber(phoneNumber) {
    if (phoneNumber == "") {
	return false;
    }

    // Because we don't know for sure how people will type in a phone number
    // all over the world, just make sure that there is a digit in there
    // somewhere.
    return /.*[0-9].*/.test(phoneNumber);
}

function EON_ValidateEmailAddress(emailAddress) {
    // First, we check that there's one @ symbol, and that the lengths are
    // right
    if (!/[^@]{1,64}@[^@]{1,255}/.test(emailAddress)) {
	// Email invalid because wrong number of characters in one section,
	// or wrong number of @ symbols.
	return false;
    }

    // Split it into sections to make life easier
    var email_array = emailAddress.split("@");
    var local_array = email_array[0].split(".");

    var local_re = new RegExp("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$");
    for (var i = 0; i < local_array.length; i++) {
	if (!local_re.test(local_array[i])) {
	    return false;
	}
    }

    // Check if domain is IP. If not, it should be valid domain name
    if (!/^\[?[0-9\.]+\]?$/.test(email_array[1])) { 
	var domain_array = email_array[1].split(".");
	if (domain_array.length < 2) {
	    return false; // Not enough parts to domain
	}
	var domain_re = new RegExp("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$");
	for (var i = 0; i < domain_array.length; i++) {
	    if (!domain_re.test(domain_array[i])) {
		return false;
	    }
	}
    }
    
    return true;
}

function EON_Radio(formName, input) {

    for(i = 0; i < document.forms[formName][input].length; i++) {
	if (document.forms[formName][input][i].checked) {
	    return true;
	}
    }

    return false;
}

function EON_CheckBox(formName, input) {
    return document.forms[formName][input].checked;
}
