//      RCSId: $Id: FormSupport.js,v 1.1 2009/04/27 16:29:35 ibell Exp $

function Touch_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 Touch_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;
}
