// Trim whitespace from strings
// 
function trim(stringToTrim){
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim){
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim){
	return stringToTrim.replace(/\s+$/,"");
}
// Validate an email address
function validateEmail(s){
	emailPattern = new RegExp(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/);

	if (!emailPattern.test(s)) {
		//alert("Email Address Is Not Valid");
		return false;
	}
	return true;
}
//	Validate a US ZIP Code 
function isZip(s){
	//alert("In isZip");
	// Check for correct zip code
	reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
	
	if (!reZip.test(s)) {
		//alert("Zip Code Is Not Valid");
		return false;
	}
	return true;
}
//	Validate Credit Card Number
function isValidCreditCard(cardType, ccnum) {
	if (cardType == "VI") {
		// Visa: length 16, prefix 4, dashes optional.
		var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
	}else if (cardType == "MC") {
		// Mastercard: length 16, prefix 51-55, dashes optional.
		var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
	}else if (cardType == "DS") {
		// Discover: length 16, prefix 6011, dashes optional.
		var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
	}else if (cardType == "AX") {
		// American Express: length 15, prefix 34 or 37.
		var re = /^3[4,7]\d{13}$/;
	}else if (cardType == "DC") {
		// Diners: length 14, prefix 30, 36, or 38.
		var re = /^3[0,6,8]\d{12}$/;
	}
	if (!re.test(ccnum)) return false;
	// Remove all dashes for the checksum checks to eliminate negative numbers
	ccnum = ccnum.split("-").join("");
	// Checksum ("Mod 10")
	// Add even digits in even length strings or odd digits in odd length strings.
	var checksum = 0;
	for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
		checksum += parseInt(ccnum.charAt(i-1));
	}
	// Analyze odd digits in even length strings or even digits in odd length strings.
	for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
		var digit = parseInt(ccnum.charAt(i-1)) * 2;
		if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
	}
	if ((checksum % 10) == 0) return true; else return false;
}
//	Validate Credit Card CVC/CVV2 Code
function validateCvvCode(cardType, cvvCode){
	var digits = 0;
	switch (cardType.toUpperCase()){
		case 'MC':
		case 'VI':
		case 'DS':
		case 'DC':
			digits = 3;
			break;
		case 'AX':
			digits = 4;
			break;
		default:
			return false;
	}
	var regExp = new RegExp('[0-9]{' + digits + '}');
	return (cvvCode.length == digits && regExp.test(cvvCode))
}
//
// isPositiveNumeric
// 
// Returns true if passed-in string is a positive numeric value.
// It can have 1 decimal point.
//
function isPositiveNumeric(sTest)
{
    // Put the TEST value into a string object variable
    var sField = new String (trim (sTest));

    // Check for a length of 0 - if so, return false
    if (sField.length == 0)
        return false;

    // Check for a length of 1 - if character is a decimal point, return false
    if ((sField.length == 1) && (sField.charAt (0) == '.'))
        return false;

    // Loop through each character of the string
    for (var x = 0; x < sField.length; x++)
    {
        // if the character is < 0 or > 9, return false (not a number)
        if ((sField.charAt (x) < '0' || sField.charAt (x) > '9') &&
            (sField.charAt (x) != '.'))
            return false;
    }

    // made it through the loop - we have a number
    return true;
}
//
// isInteger
// 
// Returns true if passed-in string is an integer (non-zero positive, no dec.)
//
function isInteger (sTest)
{
    // Put the TEST value into a string object variable
    var sField = new String (trim (sTest));

    // Check for a length of 0 - if so, return false
    if (sField.length == 0)
        return false;

    // Loop through each character of the string
    for (var x = 0; x < sField.length; x++)
    {
        // if the character is not a number, return false 
        if ((sField.charAt (x) < '0' || sField.charAt (x) > '9'))
            return false;
    }

    // made it through the loop - we have a number
    return true;
}
//
// validatePhoneNumber
// 
// Returns true if passed-in string is 10 digits and an integer.
//
function validatePhoneNumber (sTest)
{
    // Put the TEST value into a string object variable
    var sField = new String (trim (sTest));

    // Check for a length of 10
    if (sField.length != 10)
        return false;

    return isInteger (sTest);
}
//
// Tooltip functions
// Show/hide tooltips using CSS-styled tool tips.
//
function findPosition (oElement)
{
  var posX, posY;
  if ( typeof( oElement.offsetParent ) != 'undefined' )
  {
    for (posX = 0, posY = 0; oElement; oElement = oElement.offsetParent )
    {
      posX += oElement.offsetLeft;
      posY += oElement.offsetTop;
    }
    return [ posX, posY ];
  }
  else
  {
    return [ oElement.x, oElement.y ];
  }
}
//
// forElementID is the id of the element that will get the tooltip.
// divID is the id of the div element that will be displayed as the tooltip.
// posX and posY are offsets from the element where the tooltip will be
//  displayed.
//
function showTooltip (forElementID, divID, posX, posY)
{
    var tooltip = document.getElementById(divID);
    var forElement;
    var xy, x, y;

    if ((tooltip.style.top == '' || tooltip.style.top == 0) 
        && (tooltip.style.left == '' || tooltip.style.left == 0))
    {
        // need to fixate default size (MSIE problem)
        tooltip.style.width = tooltip.offsetWidth + 'px';
        tooltip.style.height = tooltip.offsetHeight + 'px';

        forElement = document.getElementById(forElementID); 
        xy = findPosition (forElement);
        tooltip.style.top = xy[1] + posY + 'px';
        tooltip.style.left = xy[0] + posX + 'px';
    }

    tooltip.style.visibility = 'visible'; 
    return true;
}
function hideTooltip (divID)
{
    var coachPhone = document.getElementById(divID);
    coachPhone.style.visibility = 'hidden'; 
    return true;
}
//
// isLeapYear
// 
// Returns true if passed-in integer 4-digit year is a leap year.
//
function isLeapYear (intYear)
{
    if (intYear % 100 == 0)
    {
        if (intYear % 400 == 0)
            return true;
    }
    else
    {
        if ((intYear % 4) == 0)
            return true;
    }
    return false;
}
//
// validate_mmddyy
// 
// Validates that a string is in mm/dd/yy format
//
function validate_mmddyy (iString){
	var im, id, iy;
	
	if (iString.length != 8)
		return false;

	if ((iString.substr(2,1) != "/") || (iString.substr(5,1) != "/")){
		if ((iString.substr(2,1) != "-") || (iString.substr(5,1) != "-"))
			return false;
	}

	im = parseInt (iString.substr(0,2), 10);
	id = parseInt (iString.substr(3,2), 10);
	iy = parseInt (iString.substr(6,2), 10);
	if (isNaN(im) || isNaN(id) || isNaN(iy))
		return false;

	if ((im < 1) || (im > 12) || (id < 1) || (id > 31))
		return false;

	if ((im == 2) && (id > 28))
	{
		if (isLeapYear(iy) == false)
			return false;
		else if (sd > 29)
			return false;
	}

	if ((id == 31) && ((im == 4) || (im == 6) || (im == 9) || (im == 11)))
		return false;

	// At this point, the date is valid
	return true;
}
//
// externalLinks
// 
// Allows a tag to open external windows in XHTML strict
//
function externalForms() {  
	var forms = document.getElementsByTagName("form");
	for(var i = 0; i < forms.length; i++){ 
	   var form = forms[i];
	   if(form.getAttribute("id") == "dd")
	   { 
	      form.target = "_blank";
	   } 
	 }
	 return true;
}
function externalLinks() {  
	var anchors = document.getElementsByTagName("a");  
	for (var i=0; i < anchors.length; i++) {  
		var anchor = anchors[i];  
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external"){  
			anchor.target = "_blank";
		}
	}
	return true;
}  
window.onload = externalLinks;

