
function fncSetRandomItems() {
  var intRandomTagline = Math.floor(Math.random()*astrTaglinesImage.length);
  var intRandomTip = Math.floor(Math.random()*astrTipImage.length);
  var strImagePath = "/images/";
  if (document.getElementById('idHomeTagline') && document.getElementById('idHomePhoto') && document.getElementById('idHomeTip')) {
    document.getElementById('idHomeTagline').src = strImagePath + astrTaglinesImage[intRandomTagline];
    document.getElementById('idHomeTagline').title = astrTaglinesText[intRandomTagline];
    document.getElementById('idHomePhoto').src = strImagePath + astrPhotoImage[intRandomTagline];
    document.getElementById('idHomePhoto').title = astrPhotoText[intRandomTagline];
    document.getElementById('idHomeTip').src = strImagePath + astrTipImage[intRandomTip];
    document.getElementById('idHomeTip').title = astrTipText[intRandomTip];
  }      
}

function fncOverOffSwap(oImage) {
  if (oImage.src) {
   var strImageSrc = oImage.src;
   if (strImageSrc.indexOf("-off") > 0) {
     strImageSrc = strImageSrc.replace(/\-off\.gif/, "-over.gif");
     oImage.src = strImageSrc;
   }
   else if (strImageSrc.indexOf("-over") > 0) {
     strImageSrc = strImageSrc.replace(/\-over\.gif/, "-off.gif");
     oImage.src = strImageSrc;
   }
  }
}

function fncPopupWin(strURL, intWidth, intHeight) {
  var winPopup = window.open(strURL, "winPopup", "width=" + intWidth + ",height=" + intHeight);
}

function fncGetAbsoluteX(oElement) {
  // Utility function to get the absolute X-coordinate of an object on the page
  if (typeof(oElement) == "string") {
    oElement = document.getElementById(oElement);
  }
  if (typeof(oElement) == "object") {
    var intCoords = {x: 0};
    while (oElement) {
      intCoords.x += oElement.offsetLeft;
      oElement = oElement.offsetParent;
    }
    return intCoords.x;
  }
}
function fncGetAbsoluteY(oElement) {
  // Utility function to get the absolute Y-coordinate of an object on the page
  if (typeof(oElement) == "string") {
    oElement = document.getElementById(oElement);
  }
  if (typeof(oElement) == "object") {
    var intCoords = {y: 0};
    while (oElement) {
      intCoords.y += oElement.offsetTop;
      oElement = oElement.offsetParent;
    }
    return intCoords.y;
  }
}

function fncGetWidth(oElement) {
  // Utility function to get the width of an object on the page
  if (typeof(oElement) == "string") {
    oElement = document.getElementById(oElement);
  }
  if (typeof(oElement) == "object") {
  	if (oElement.style.pixelWidth) {
  		return oElement.style.pixelWidth;
  	}
    if (oElement.style.width) {
        return oElement.style.width;
    } 
    return oElement.offsetWidth;
  }
}
function fncGetHeight(oElement) {
  // Utility function to get the height of an object on the page
  if (typeof(oElement) == "string") {
    oElement = document.getElementById(oElement);
  }
  if (typeof(oElement) == "object") {
  	if (oElement.style.pixelHeight) {
  		return oElement.style.pixelHeight;
  	}
    if (oElement.style.height) {
        return oElement.style.height;
    }
    return oElement.offsetHeight;
  }
}


function fncAddClass(oElement, strClassName) {
  if (typeof(oElement) == "object" && strClassName.length > 0) {
    if (oElement.className) {  
      var strCurrentClassName = oElement.className;
      if (strCurrentClassName.indexOf(strClassName) < 0) {
        if (strCurrentClassName.length > 1) {
          strClassName = " " + strClassName;
        }        
        oElement.className = strCurrentClassName + strClassName;
      }
    }
  }
}

function fncRemoveClass(oElement, strClassName) {
  if (typeof(oElement) == "object" && strClassName.length > 0) {
    if (oElement.className) {
      var strCurrentClassName = oElement.className;
      if (strCurrentClassName.indexOf(strClassName) >= 0) {
        strNewClassName = fncRemoveClassRecursive(" " + strCurrentClassName + " ", " " + strClassName + " ");
        if (strNewClassName.indexOf(" ") == 0) { strNewClassName = strNewClassName.substring(1); }
        if (strNewClassName.lastIndexOf(" ") == strNewClassName.length-1) { strNewClassName = strNewClassName.substring(0,strNewClassName.length-1); }
        oElement.className = strNewClassName;
      }
    }
  }
}

function fncRemoveClassRecursive(strSource, strSubstrToRemove) {
  var intIndex = strSource.indexOf(strSubstrToRemove);
  var strReturn = "";
  if (intIndex == -1) return strSource;
  strReturn += strSource.substring(0,intIndex) + " " + fncRemoveClassRecursive(strSource.substring(intIndex + strSubstrToRemove.length), strSubstrToRemove);
  return strReturn;
}



function fncValidateForm(oForm) {
  blnDoFancyFields = false;
  var strErrors = "";
  var oElements = oForm.elements;
  if (arguments.length >= 2) {
    if (arguments[1] == true) {
      blnDoFancyFields = true;
    }
  }
  
  for (var i=0;i<oElements.length;i++) {
    // Validate each element using the fncValidFormField function which will
    //   return any errors found.  
    strErrors = strErrors + fncValidFormField(oElements[i]);
  }
  
  if (strErrors.length > 0) {
    // Report any errors and return false
    alert("Please correct these errors before continuing: \n" + strErrors);
    return false;
  }
  else {
    // All elements have been validated, return true
    return true;
  }
}

function fncValidFormField(oElement) {
  // Returns error message if invalid
  // Form Elements can have the following additional attributes (which may lead to invalid XHTML)
  //   required - makes sure there is a value provided or selected
  //   validphone - ensures valid US/Canadian phone number
  //   validemail - ensures valid email address
  // Does not validate checkbox lists or radio buttons
  var strElementError = "";
  var strNiceName = oElement.name;
  var blnAllRequired = false;
  
  if (oElement.form) {
    if (oElement.form.getAttribute("required")) {
      blnAllRequired = true;
    }
  }
  
  if (typeof(blnDoFancyFields) == "undefined") {
    blnDoFancyFields = false;
  }
  if (arguments.length >= 2) {
    if (arguments[1] == true) {
      blnDoFancyFields = true;
    }
  }
  
  if (oElement.getAttribute("nicename")) {
    strNiceName = oElement.getAttribute("nicename");
  }
  
  if (!oElement.disabled && oElement.getAttribute("type") != "hidden") {
    
    // Validate Required Elements
    if (blnAllRequired || oElement.getAttribute("required")) {
      
      // Check Required Text Elements (Text, Textbox, Textarea)
      if ((oElement.type.indexOf("text") >= 0 || oElement.type.indexOf("password") >= 0) && oElement.value.length <= 0) {
        strElementError = "Please provide a response for " + strNiceName;
      }
      
      // Check Required Drop-Down Boxes
      if (oElement.type.indexOf("select") >= 0) {
        var intNumSelected = 0;
        for (var j=0;j<oElement.length;j++) {
          if (oElement[j].selected && oElement[j].value.length > 0) {
            intNumSelected++;
          }
        }
          
        if (intNumSelected == 0) {
          strElementError = "Please select a response for " + strNiceName;
        }
      }
    }
    
    // Validate Phone Number
    if (oElement.getAttribute("validphone") && oElement.value.length > 0) {
      if (!fncValidUSCanadaPhone(oElement.value)) {
        strElementError = "Please provide a valid " + strNiceName;
      }
    }
    
    // Validate Email Addresses
    if (oElement.getAttribute("validemail") && oElement.value.length > 0) {
      if (!fncValidEmail(oElement.value)) {
        strElementError = "Please provide a valid " + strNiceName;
      }
    }
    if (oElement.getAttribute("validintlphone") && oElement.value.length > 0) {
      if (!fncValidIntlPhone(oElement.value)) {
        strElementError = "Please provide a valid " + strNiceName;
      }
    }
  }
  
  if (strElementError.length > 0) {
    strElementError = "\n - " + strElementError;
    if (blnDoFancyFields) { fncAddClass(oElement, "clsFormInputInvalid"); }
  }
  else {
    if (blnDoFancyFields) { fncRemoveClass(oElement, "clsFormInputInvalid"); }
  }
  return strElementError;
}

function fncValidEmail(strEmail) {
	strInvalidChars = " /:,;'";
	if (strEmail == "" ) {
		return false;
	}
	for ( i = 0; i<strInvalidChars.length;  i++) {
		chrBadChar=strInvalidChars.charAt(i);
		if (strEmail.indexOf(chrBadChar,0) > -1) {
			return false;
		}
	}
	intPos=strEmail.indexOf("@",1)
	if (intPos == -1 ) {
		return false;
	}
	if (strEmail.indexOf("@",intPos+1) > -1 ) {
		return false;
	}
	intPeriodPos=strEmail.indexOf(".",intPos)
	if (intPeriodPos == -1 ) {
		return false;
	}
	if (intPeriodPos+3 > strEmail.length ) {
		return false;
	}
	return true;
}

function fncValidUSCanadaPhone(strPhone) {
	if (strPhone.match(/\s*\(?\s*[2-9]\d{2}\s*\)?\s*([-.]?)\s*\d{3}\s*([-.]?)\s*\d{4}\s*/)) {
	  if (fncValidIntlPhone(strPhone)) {
		  return true;
	  }
	}
	return false;
}

function fncValidIntlPhone(strPhone) {
	strPhone = strPhone.replace(/[\s\(\)\-\.]/g,"");
	if (strPhone.length >= 10) {
	  return true;
	}
}