/**** ONLOAD FUNCTION ***/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function createPopup(master, slave) {
	var masterObj = document.getElementById(master);
	if (masterObj) { 
		var slaveObj = document.getElementById(slave);
		masterObj.onmouseover=function() {
			slaveObj.className="on";
		}
		masterObj.onmouseout=function(e) {
			var relTarg;
			if (!e) var e = window.event;
			if (!e) {
				slaveObj.className="off";
			}
			if (e.relatedTarget) relTarg = e.relatedTarget;
			else if (e.toElement) relTarg = e.toElement;
			if (relTarg == slaveObj) {
			}
			else {
				slaveObj.className="off";
			}
		}
		slaveObj.onmouseover=function() {
			this.className="on";
		}
		slaveObj.onmouseout=function(e) {
			var relTarg;
			if (!e) var e = window.event;
			if (!e) {
				this.className="off";
			}
			if (e.relatedTarget) relTarg = e.relatedTarget;
			else if (e.toElement) relTarg = e.toElement;
			if (relTarg.parentNode.id == this.id || relTarg.parentNode.parentNode.id == this.id ) {
			}
			else {
				this.className="off";
			}
		}
	}
}

function str_replace(inputString, fromString, toString) {
	   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function


function toggleMemoryHonor(input){
	if (input.checked){
		showContent('honoreeText');
		showContent('sourceInfo');
		showContent('personalMessageDiv');
	}
	else{
		hideContent('honoreeText');
		hideContent('sourceInfo');
		hideContent('personalMessageDiv');
	}
}

function toggleHonoreeNotify(input)
{
	if (input.checked){
		showContent('honoreeInformation');
	}
	else{
		hideContent('honoreeInformation');
	}
}

function toggleMemoryHonorDiv(input){
	//var toDisplay = document.getElementById(input);
	if (input){
		var index = input.selectedIndex;
		var value = input[index].value;
		if (value == 'honor'){
			showContent('honorSelector');
			hideContent('memorySelector');
		}
		else if (value = 'memory'){
			showContent('memorySelector');
			hideContent('honorSelector');
		}
	}
}

function toggleSourceAddress(input){
	if (input){
		var ischecked = input.checked;
		if (ischecked){
			showContent('sourceAddressDiv');
		}
		else{
			hideContent('sourceAddressDiv');
		}
	}
}

function toggleHonoreeAddress(input) {
	if (input){
		var isChecked = input.checked;
		if (isChecked){
			showContent('honoreeAddressInfo');
		}
		else{
			hideContent('honoreeAddressInfo');
		}
	}
}

function limitTextArea(input,maxLength){
	var inputLength = input.value.length;
	if (inputLength > maxLength){
		input.value=input.value.substring(0,maxLength);
	}
}

function updateRequired(value){
	switch(value){
		
		case "donateByMail":
			document.getElementById('required').value = "donationMethod,fname,lname,email,address,city,state,zip,phone,";
		break;
		
		case "donateOnline":
			document.getElementById('required').value = "donationMethod,fname,lname,email,address,city,state,zip,phone,credit,cvv";
		break;
	}
}
function makeMultibox(itemClass){
	
	var modalBox = new MultiBox(itemClass, {
		container: document.body,
		contentColor:'#000',
		openFromLink: true,
		useOverlay:true,
		offset: {x:0, y:-50},
		showNumbers: true});
}
function toggleContent(span) {

	var spanClassName = document.getElementById(span).className;
	position = spanClassName.search("hidden");

	//The classname contains 'hidden'. Remove it.
	if (position != -1) {
		document.getElementById(span).className = str_replace(spanClassName, "hidden", "");
	
	//The classname does not contain 'hidden'. Add it.
	} else {
		document.getElementById(span).className += " hidden";
	}
}

function showContent(span) {

	var spanClassName = document.getElementById(span).className;
	position = spanClassName.search("hidden");
	if (position != -1) {
		document.getElementById(span).className = str_replace(spanClassName, "hidden", "");
	}
}

function hideContent(span) {

	var spanClassName = document.getElementById(span).className;
	position = spanClassName.search("hidden");
	if (position == -1) {
		document.getElementById(span).className += " hidden";
	}
}