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

function toggleActive(span) {

	var spanClassName = document.getElementById(span).className;
	if (spanClassName == 'hidden') {
		document.getElementById(span).className = '';
	} else {
		document.getElementById(span).className = 'hidden';
	}
}

function alterRequiredFields(checkbox){
	var isChecked = checkbox.checked;
	if(isChecked){
		document.getElementById('required').value = "name,phone,organizationName,organizationAddress,organizationCity,organizationState,organizationZip,dateRequested,timeRequested,speakerRequestType,speakerLength,numAttendees";
	}else{
		document.getElementById('required').value = "name,phone";
	}
}

//string, search, replace
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 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";
	}
}

/*addLoadEvent(function() {
	var monthlyDonation = document.getElementById("monthlyDonationWrap");
	if(monthlyDonation){
		login = document.getElementById("login");
		email = document.getElementById("email");
		if(login){
			monthlyDonation.onclick = function(){
				if(login.value.length == 0){
					// Login field is empty so pull it from the email field
					login.value = email.value;
				}
			}
		}
	}
})*/

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

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 toggleFontSize() {
	if (document.body.className == "increaseFont") {
		document.body.className = "";
	} else {
		document.body.className = "increaseFont";
	}
}

function resizeText(type) {
	if (type == "larger") {
		var multiplier = "+1"; 
	} else if (type == "smaller") {
		var multiplier = "-1"; 
	} else {
		return false;	
	}
	
	if (document.body.style.fontSize == "") {  
		document.body.style.fontSize = "1em";  
	}  
	// need to multiply result by 10, round it, then divide by ten to make sure math doesn't get weird
	newFontSize = Math.round((parseFloat(document.body.style.fontSize) + (parseFloat(multiplier) * .2))*10)/10;
	if (newFontSize > 2) {  
		newFontSize = 2;
	} else if (newFontSize < 1.4) {
		newFontSize = 1.4;
	}
	if (newFontSize > 0) {
		newFontSize += "em";
		document.body.style.fontSize = newFontSize;  
		$.ajax({
			type: "POST",
			url: SETTINGS.webRoot+"page.php",
			data: "mode=setTextMagnification&bodyStyle="+newFontSize,
			processData: false
		});
	}
	return false;
} 

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/";
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function configurePrice(attendeeCountObj)
{
	var count = attendeeCountObj.value;
	var feeDisplay = $('#registrationFeeText');
	feeDisplay.html("$" + (parseFloat(feeDisplay.attr('rel')) * count));
}