/**** 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';
	}
}

//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";
	}
}

function checkEventInNC() {
	document.getElementById("eventInNCMessage").value = "";
	value = document.getElementById("eventInNC").value;
	if (value == 0) {
		return false;
	} else {
		return true;
	}

}

function validateContactForm(obj) {

	eventReturn = checkEventInNC();
	checkFormReturn = checkform(obj);
	
	returnFalse = false;
	if (eventReturn == false) {
		document.getElementById("eventInNCMessage").innerHTML = "<img src='img/alertIcon.gif' alt='We were unable to process your form. Please review the highlighted fields and resubmit.'/> We were unable to process your form. American Party Rentals only works with clients whose events are taking place in the state of North Carolina.";
		returnFalse = true;
	} else if (checkFormReturn == false) {
		returnFalse = true;
	}
	
	if (returnFalse == true) {
		return false;
	} else {
		return true;
	}
}	

function getObject(objectId) {
        if (document.getElementById && document.getElementById(objectId)) {
                return document.getElementById(objectId);
        } else if (document.all && document.all(objectId)) {
                return document.all(objectId);
        } else {
                return false;
        }
}


function calcSubTotal(input){
	if (input.value.match(/[^0-9]/g)) alert('Ticket quantities must be numeric.');
	var ticketPrice = input.parentNode.getElementsByTagName('span')[0].innerHTML.replace(/[^0-9.]/g, "");
	var subTotal = ticketPrice * input.value;
	input.parentNode.getElementsByTagName('span')[1].innerHTML = ' = ' + formatCurrency(subTotal);
	calcTotal();
}

function calcTotal(){
	var orchestraGeneral =	parseFloat(document.getElementById('orchestraGeneral').parentNode.getElementsByTagName('span')[1].innerHTML.replace(/[^0-9.]/g, ""));
	var orchestraSenior =	parseFloat(document.getElementById('orchestraSenior').parentNode.getElementsByTagName('span')[1].innerHTML.replace(/[^0-9.]/g, "")); 
	var orchestraStudent =	parseFloat(document.getElementById('orchestraStudent').parentNode.getElementsByTagName('span')[1].innerHTML.replace(/[^0-9.]/g, ""));
	var orchestraChild =	parseFloat(document.getElementById('orchestraChild').parentNode.getElementsByTagName('span')[1].innerHTML.replace(/[^0-9.]/g, "")); 
	var mezzanineGeneral =	parseFloat(document.getElementById('mezzanineGeneral').parentNode.getElementsByTagName('span')[1].innerHTML.replace(/[^0-9.]/g, ""));
	var mezzanineSenior =	parseFloat(document.getElementById('mezzanineSenior').parentNode.getElementsByTagName('span')[1].innerHTML.replace(/[^0-9.]/g, "")); 
	var mezzanineStudent =	parseFloat(document.getElementById('mezzanineStudent').parentNode.getElementsByTagName('span')[1].innerHTML.replace(/[^0-9.]/g, ""));
	var mezzanineChild =	parseFloat(document.getElementById('mezzanineChild').parentNode.getElementsByTagName('span')[1].innerHTML.replace(/[^0-9.]/g, ""));
	var shippingCharge =	(document.getElementById('mailtickets').checked) ? 2 : 0;
	var creditCharge =		(document.getElementById('paybycredit').checked) ? 3 : 0;
	var subtotal = orchestraGeneral + orchestraSenior + orchestraStudent + orchestraChild + mezzanineGeneral + mezzanineSenior + mezzanineStudent + mezzanineChild;
	var total = (subtotal != 0) ? orchestraGeneral + orchestraSenior + orchestraStudent + orchestraChild + mezzanineGeneral + mezzanineSenior + mezzanineStudent + mezzanineChild + shippingCharge + creditCharge : 0;
	document.getElementById('subtotalDisplay').innerHTML = (subtotal != 0) ? formatCurrency(subtotal) : "";
	document.getElementById('subtotal').value = subtotal;
	document.getElementById('totalDisplay').innerHTML = (subtotal != 0) ? formatCurrency(total) : "";
	document.getElementById('total').value = total;
}

function formatCurrency(num) {
	var num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) { 
		num = "0";
	}
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10) {
		cents = "0" + cents;
	}
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
		num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	}
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function getMonth(year, month) {
	var url = location.protocol + "//" + document.domain + "/event.php?mode=getmonthdisplay&month=" + month + "&year=" + year;
	var fnObj = document.getElementById("calendarEvents");
	loadXMLDoc(url, "displayMonth", fnObj);
}
addLoadEvent(function() {
	if (!document.getElementsByTagName) return; 
	var anchors = document.getElementsByTagName("a"); 
	for (var i=0; i<anchors.length; i++) { 
		var anchor = anchors[i]; 
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") 
			anchor.target = "_blank"; 
	} 
})
addLoadEvent( function () { 
	if (document.getElementById('sameasabove')) {
		document.getElementById('sameasabove').onclick = function() {
			checkboxDisplayToggle(this,document.getElementById('shipping'));
		}
		checkboxDisplayToggle(document.getElementById('sameasabove'),document.getElementById('shipping'));
	} 
})
function checkboxDisplayToggle(checkbox,target) {
	var required = document.getElementById('required');
	if (checkbox.checked == true) {
		target.style.display = "none";
		required.value = required.value.replace(/,shippingstate/g,"");
		required.value = required.value.replace(/,sName/g,"");
		required.value = required.value.replace(/,saddress/g,"");
		required.value = required.value.replace(/,scity/g,"");
		required.value = required.value.replace(/,szip/g,"");
	} else {
		target.style.display = "block";
		required.value += ",shippingstate,sName,saddress,scity,szip";
	}
}