function fncGenericOnload() {
  if (NiftyCheck()) {
    fncRoundEdges();
  }    
  if (typeof(fncPageOnload) == "function") {
    fncPageOnload();
  }
}

function fncTabSwitch(oImage, strFrom, strTo) {
  oImage.src = oImage.src.replace(strFrom,strTo);
}

function fncSubNavSwitch(oImage, blnBackground) {
  var strBackgroundImage = "/images/_dot.gif";
  if (blnBackground) {
    strBackgroundImage = "/images/nav-arrow.gif";
  }
  oImage.style.backgroundImage = "url(" + strBackgroundImage + ")";
}

function fncRoundEdges() {  
  Rounded("div#idSubNav","tr tl","#FFFFFF","transparent");
  Rounded("div#idQuoteBox","tr tl","#FFFFFF","transparent");
  Rounded("div#idQuotePhoto","br bl","#FFFFFF","transparent");
  Rounded("div#idPageHeading","tr","#FFFFFF","transparent");
  Rounded("div#idFooterLeft","all","#FFFFFF","transparent");
  Rounded("div#idFooterRight","all","#FFFFFF","transparent");
  Rounded("div.clsClickBoxBlue","all","#FFFFFF","transparent");
  
  if (document.getElementById('idBlueBox1')) {
    Rounded("div#idBlueBox1","all","#FFFFFF","transparent");
  }
  if (document.getElementById('idBlueBox2')) {
    Rounded("div#idBlueBox2","all","#FFFFFF","transparent");
  }
  if (document.getElementById('idBlueBox3')) {
    Rounded("div#idBlueBox3","all","#FFFFFF","transparent");
  }
  if (document.getElementById('idBlueBox4')) {
    Rounded("div#idBlueBox4","all","#FFFFFF","transparent");
  }
}


function fncPopupWindow(strName,strURL,intWidth,intHeight,intScroll,intResize) {
  if (strURL.length > 0) {
    var winPopup = window.open(strURL, strName, "width=" + intWidth + ",innerWidth=" + intWidth + ",height=" + intHeight + ",innerHeight=" + intHeight + ",directories=0, statusbar=0, menubar=0, resizable=" + intResize + ", scrollbars=" + intScroll + ", status=0, titlebar=0, toolbar=0");
  }
  return false;
}


//---------------------------------------------------------------------------
// POSITIONING FUNCTIONS

function fncGetAbsoluteX(oObjectToGetPosition) {
  // Utility function to get the absolute X-coordinate of an object on the page
  var intCoords = {x: 0};
  while (oObjectToGetPosition) {
    intCoords.x += oObjectToGetPosition.offsetLeft;
    oObjectToGetPosition = oObjectToGetPosition.offsetParent;
  }
  return intCoords.x;
}

function fncGetAbsoluteY(oObjectToGetPosition) {
  // Utility function to get the absolute Y-coordinate of an object on the page
  var intCoords = {y: 0};
  while (oObjectToGetPosition) {
    intCoords.y += oObjectToGetPosition.offsetTop;
    oObjectToGetPosition = oObjectToGetPosition.offsetParent;
  }
  return intCoords.y;
}
  
  

//---------------------------------------------------------------------------
// FORM FUNCTIONS

function fncValidateForm(oForm) {
  var strErrors = "";
  var oElements = oForm.elements;
  
  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 (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 (strElementError.length > 0) {
    strElementError = "\n - " + strElementError;
  }
  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*/)) {
		return true;
	}
	return false;
}



//---------------------------------------------------------------------------
// COOKIE FUNCTIONS

function fncSetCookie (name, value, exp, path) {
 /*
    INPUT
      name (string) - name of the cookie
      value (string) - value of the cookie
      exp (string) - default is "never"
                     if "never" it sets exp to "Thu, 7 Dec 2113 01:00:00 UTC"
                     if "exp" it sets exp to "Fri, 13 Apr 1970 01:00:00 UTC"
                     if int (ie "5", "20", "60000") it translates to int hours from now
                     if valid GMT date then it uses that as exp
                     else it defaults to 840 hours (5 weeks)
      path (string)
 */
  if(exp == "") {
    exp = "never";
  }
  if (typeof(exp) == 'string') {
    if (exp == 'never') { 
      var strExp = "Thu, 7 Dec 2113 01:00:00 UTC";
    }
    else if (exp == 'exp' || exp == 'now') { 
      var strExp = "Fri, 13 Apr 1970 01:00:00 UTC";
    }
    else if (Date.parse(exp)) {
     var strExp = exp;
    }
  }
  else {
    exp = exp*1;
    if (isNaN(exp)) {
     var strExp = (new Date((new Date()).getTime() + 840*3600000)).toGMTString();
    }
    else {
     var strExp = (new Date((new Date()).getTime() + exp*3600000)).toGMTString();
    }
  }
  if (typeof(strExp) == "undefined") {
    var strExp = (new Date((new Date()).getTime() + 840*3600000)).toGMTString();
  }
  document.cookie = name + '=' + escape(value) + ((strExp)?(';expires=' + strExp):'') + ((path)?';path=' + path:'');
}


function fncReadCookie(name) {
  var firstChar, lastChar;
  var theBigCookie = document.cookie;
  firstChar = theBigCookie.indexOf(name);
  if(firstChar != -1) {
    firstChar += name.length + 1;
    lastChar = theBigCookie.indexOf(';', firstChar);
    if(lastChar == -1) lastChar = theBigCookie.length;
    return unescape(theBigCookie.substring(firstChar, lastChar));
  }
  else {
    return false;
  }
}


function fncKillCookie(name, path) {
 /*
    REQUIRED MODULES
      "fncReadCookie" function
      "fncSetCookie" function
 */
  var value = fncReadCookie(name);
  if(value) {
    fncSetCookie(name, '', 'exp')    
  }
}

// opacity fading functions

function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	
	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	
	//make image transparent
	changeOpac(0, imageid);
	
	//make new image
	document.getElementById(imageid).src = imagefile;

	//fade in image
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}

function currentOpac(id, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;
	
	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}
