/* Included by header widget - add any js you want to reuse in templates here */
/* addLoadEvent() so we do not need to attach to document onload() event - cool! */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != "function") {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

/* style swapper */
function swap(obj,new_style) {
    obj.className = new_style;
}

/* email address validation */
function validateemail(TheForm,TheField,TheValue) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) return (TheValue.indexOf(".") > 2) && (TheValue.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(TheValue) && r2.test(TheValue));
}

function getElem( szSrcID ) {
	return document.layers ? document.layers[szSrcID] :
	document.getElementById ?  document.getElementById(szSrcID) :
	document.all[szSrcID];
}


function shrink(targetID) {
	var t = getElem( targetID);
	t.className = "shrink";
}

function expand(targetID) {
	var t = getElem( targetID );
	t.className = "expand";
}

function toggleAnswer(szImgID, szTargetID, szVisibleImg, szHiddenImg) {
	var target = getElem( szTargetID );
	var img = getElem( szImgID );
	
	var visible = target.className == "expand" ? 1 : 0;
	
	if( visible ) {
		target.className = "shrink";
		img.src = szVisibleImg;
	} else {
		target.className = "expand";
		img.src = szHiddenImg;
	}
}

function switchAddress(selectedAddressIndex) {

   // This function will turn off all existing addresses with IDs 'less than' and 
   // 'greater than' the selectedAddressIndex. Once all other addresses are hidden,
   // it will show the selected address.


   var addressLabel = 'Address';
   var addressIndex;

   addressIndex = selectedAddressIndex - 1;

   while ((addressIndex < selectedAddressIndex) && (document.getElementById(addressLabel + addressIndex))) {
      document.getElementById(addressLabel + addressIndex).className = 'shrink';
      addressIndex  = addressIndex - 1;
   }

   addressIndex = parseInt(selectedAddressIndex) + 1;
   while ((addressIndex >= selectedAddressIndex) && (document.getElementById(addressLabel + addressIndex))) {
      document.getElementById(addressLabel + addressIndex).className = 'shrink';
      addressIndex  = addressIndex + 1;
   }

   document.getElementById(addressLabel + selectedAddressIndex).className = 'expand';

}
