/*
 * Library of javascript validation functions.
 */

/**
 * Returns true if the text is empty (i.e. does not have at least one alpanumeric character, not including spaces). 
 * Returns false otherwise.
 */
function isEmpty(text) {
	if (text.search(/[A-Za-z0-9]+/) != -1) {
        return false;
    } else {
        return true;
    }
}

/** 
 * Returns true if email is a valid email address, or false if it does not fit the format
 */
function isValidEmail(email) {
    // email usernames cannot contain "@" or whitespace and must be at least 1 char long --> [^@\s]+
    // Domains and subdomains can be between 1 and 63 characters long.
    // They can have alpha numeric chars and dashes but they cannot start or end with dashes. --> [a-zA-Z0-9](?:[-a-zA-Z0-9]{0,61}[a-zA-Z0-9])?
    // new TLDs are coming out all the time but they all seem to be alpha, between 2 and 10 (just to be safe) chars --> [a-zA-Z]{2,10}
    if (email.search(/^[^@\s]+@(?:[a-zA-Z0-9](?:[-a-zA-Z0-9]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,10}$/) != -1) {
        return true;
    } else {
        return false;
    }
}

/**
 * Returns true if the passed in text is numeric and false otherwise
 */
function IsNumeric(sText) {
	var ValidChars = "0123456789.";
	var IsNumber = true;
	var Char;
	
	for(i=0; i<sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}



/**
 * Returns true if the supplied phone is a valid north american phone.
 * Also optionally validates phone extensions (validates extensions by default)
 */
function isValidPhone(phone, allowExtensions) {
	 
	if(allowExtensions == null || allowExtensions == true) {
		// allow extensions
		if(phone.search(/^\(?[0-9]{3}\)? *\-? *[0-9]{3} *\-? *[0-9]{4}(x*[0-9]+)?$/) != -1) {
			return true;
		} else {
			return false;
		}
	 } else {
		// do not allow extensions
		if(phone.search(/^\(?[0-9]{3}\)? *\-? *[0-9]{3} *\-? *[0-9]{4}$/) != -1) {
			return true;
		} else {
			return false;
		}
	 }
}

/**
 * Returns true if the passed value is a valid US zip code eg. 00000-0000 
 * (last 4 numbers optional)
 */
function isValidZip(zip) {
     if(zip.search(/^[0-9]{5}( *\-? *[0-9]{4})?$/) != -1) {
     	return true;
     } else {
     	return false;
     }
}

/**
 * Returns true if the passed value is a valid canadian postal code.
 */
function isValidPostalCode(postalCode) {
     if(postalCode.search(/^[A-Z][0-9][A-Z] *[0-9][A-Z][0-9]$/i) != -1) {
     	return true;
     } else {
     	return false;
     }
}

/**
 * Returns a formatted north american phone number
 */
function formatPhone(phone) {
	// Return if this isn't a valid phone number
	if(!isValidPhone(phone)) {
		return phone;
	}

	var digits = stripNonDigits(phone);
	var formatedPhone = '(' + digits.substr(0, 3) + ') ' + digits.substr(3, 3) + '-' + digits.substr(6, 4);

	if(digits.length > 10) {
		formatedPhone += ' ext. ' + digits.substr(10);
	}
	return formatedPhone;
}

/**
 * Returns the supplied string with all digit characters removed.
 */
function stripNonDigits(text) {
	return text.replace(/[^0-9]+/g, '');
}

/**
 * Returns true if the supplied string is only comprised of digits
 * Doesn't allow negative numbers and exponential expressions
 */
function isDigits(text) {
     if(text.search(/^[0-9]+$/) != -1) {
     	return true;
     } else {
     	return false;
     }
}
