function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(function() {
	if(document.getElementById('discountCode')) {
		document.getElementById('discountCode').value = "";
		document.getElementById('discountCodeAmount').value = 0;
		document.getElementById('discountCode').onblur = checkDiscountCode;	
	}
	if(document.getElementById('businessopportunityreport')) {
		document.getElementById('businessopportunityreport').parentNode.parentNode.onclick = checkboxToggle;	
		if (document.getElementById('businessopportunityreport').checked == true) {
			document.getElementById('businessopportunityreport').parentNode.parentNode.parentNode.className = "highlight";
		}
	}
	if(document.getElementById('individualresponsereport')) {
		document.getElementById('individualresponsereport').parentNode.parentNode.onclick = checkboxToggle;	
		if (document.getElementById('individualresponsereport').checked == true) {
			document.getElementById('individualresponsereport').parentNode.parentNode.parentNode.className = "highlight";
		}
	}
	if(document.getElementById('segmentedreport')) {
		document.getElementById('segmentedreport').parentNode.parentNode.onclick = checkboxToggle;	
		if (document.getElementById('segmentedreport').checked == true) {
			document.getElementById('segmentedreport').parentNode.parentNode.parentNode.className = "highlight";
		}
	}
	if(document.getElementById('trackingReport')) {
		document.getElementById('trackingReport').parentNode.parentNode.onclick = checkboxToggle;	
		if (document.getElementById('trackingReport').checked == true) {
			document.getElementById('trackingReport').parentNode.parentNode.parentNode.className = "highlight";
		}		
	}
	if(document.getElementById('consultativePostSurveyReview')) {
		document.getElementById('consultativePostSurveyReview').parentNode.parentNode.onclick = checkboxToggle;	
		if (document.getElementById('consultativePostSurveyReview').checked == true) {
			document.getElementById('consultativePostSurveyReview').parentNode.parentNode.parentNode.className = "highlight";
		}		
	}
	if(document.getElementById('businessbenchmarking')) {
		document.getElementById('businessbenchmarking').parentNode.parentNode.onclick = checkboxToggle;	
		if (document.getElementById('businessbenchmarking').checked == true) {
			document.getElementById('businessbenchmarking').parentNode.parentNode.parentNode.className = "highlight";
		}		
	}
	if(document.getElementById('customsuggestioncard')) {
		document.getElementById('customsuggestioncard').parentNode.parentNode.onclick = checkboxToggle;	
		if (document.getElementById('customsuggestioncard').checked == true) {
			document.getElementById('customsuggestioncard').parentNode.parentNode.parentNode.className = "highlight";
		}		
	}
	if(document.getElementById('corebusiness')) {
		document.getElementById('corebusiness').parentNode.parentNode.onclick = checkboxToggle;	
		if (document.getElementById('corebusiness').checked == true) {
			document.getElementById('corebusiness').parentNode.parentNode.parentNode.className = "highlight";
		}		
	}
	if(document.getElementById('customstrategic')) {
		document.getElementById('customstrategic').parentNode.parentNode.onclick = checkboxToggle;	
		if (document.getElementById('customstrategic').checked == true) {
			document.getElementById('customstrategic').parentNode.parentNode.parentNode.className = "highlight";
		}		
	}
	if(document.getElementById('trendreports')) {
		document.getElementById('trendreports').parentNode.parentNode.onclick = checkboxToggle;	
		if (document.getElementById('trendreports').checked == true) {
			document.getElementById('trendreports').parentNode.parentNode.parentNode.className = "highlight";
		}		
	}
	if(document.getElementById('weeklyflash')) {
		document.getElementById('weeklyflash').parentNode.parentNode.onclick = checkboxToggle;	
		if (document.getElementById('weeklyflash').checked == true) {
			document.getElementById('weeklyflash').parentNode.parentNode.parentNode.className = "highlight";
		}		
	}
	recalculateTotalOrderAmount();
})

function checkDiscountCode() {
	if(document.getElementById('discountCode').value != ""){
		document.getElementById('discountCodeAmount').value = 0;
		document.getElementById('discountMessage').innerHTML = "Checking...";
		document.getElementById('discountMessage').className = "";
		document.getElementById('discountMessage').style.display = "inline";
		var url = SETTINGS.webRoot + "intranet/couponControl.php?mode=get_discount&code=" + document.getElementById('discountCode').value;
		loadXMLDoc(url, validateDiscountCode, this);
	}
	else{
		document.getElementById('discountCodeAmount').value = 0;
		document.getElementById('discountMessage').innerHTML = "";
		document.getElementById('discountMessage').className = "";
		document.getElementById('discountMessage').style.display = "none";
	}
}
function checkboxToggle() {
	if (this.getElementsByTagName('input')[0].checked == true) {
		this.parentNode.className = "highlight";
	} else {
		this.parentNode.className = "";
	}
	recalculateTotalOrderAmount();
}

function toggleAgreement(checkbox){
	if(checkbox.checked == true){
		document.getElementById("paypalNonRecurring").style.display = "none";
		document.getElementById("paypalRecurring").style.display = "block";
	}else{
		document.getElementById("paypalNonRecurring").style.display = "none";
		document.getElementById("paypalRecurring").style.display = "none";
	}
}



function recalculateTotalOrderAmount() {
	if (document.getElementById('surveypricingtable')){
		var amount = 995;
		var recurringAmount = 995;
		document.paypal_checkout.item_number.value = "Customersurvey,";
	
		var discountAmount = document.getElementById('discountCodeAmount').value;
		var discountType = document.getElementById('discountCodeType').value;
		if (document.getElementById('individualresponsereport').checked == true) {
			amount = amount + 295;
			recurringAmount += 295;
			document.paypal_checkout.item_number.value += "individualresponsereport,";
		}
		if (document.getElementById('consultativePostSurveyReview').checked == true) {
			amount = amount + 200;
			recurringAmount += 200;
			document.paypal_checkout.item_number.value += "Post-Survey,";
		}
		if(discountType == 'flat'){
			// Treat the discount as a flat rate
			amount = amount - discountAmount;
			recurringAmount -= discountAmount;
		}else{
			// Treat the discount like a percentage
			amount = amount - (amount * discountAmount);
			recurringAmount = recurringAmount - (recurringAmount * discountAmount);
		}
		document.getElementById('totalOrderAmount').innerHTML = formatCurrency(amount,true);
		document.paypal_checkout.amount.value = amount;
		if(document.paypal_checkout.item_number.value.length){

			if(document.getElementById('discountCode').value != null)
				document.paypal_checkout.item_number.value = document.paypal_checkout.item_number.value + "Discount:" + document.getElementById('discountCode').value + ",DiscountAmount" + document.getElementById('discountCodeAmount').value ;
			else
				document.paypal_checkout.item_number.value = document.paypal_checkout.item_number.value;		}
		updateRecurringForm(amount,recurringAmount);
	}
	if (document.getElementById('pricingtable')){
		if (document.getElementById('survey0')){
			var amount = 1995;
			var recurringAmount = 1995;
		}
		else if(document.getElementById('survey1')){
			var amount = 4500;
			var recurringAmount = 4500;
		}
		else if (document.getElementById('survey2')){
			var amount = 4995;
			var recurringAmount = 4995;
		}
		document.paypal_checkout.item_number.value = "Customersurvey,";
	
		var discountAmount = document.getElementById('discountCodeAmount').value;
		var discountType = document.getElementById('discountCodeType').value;
		if (document.getElementById('individualresponsereport').checked == true) {
			amount = amount + 295;
			recurringAmount += 295;
			document.paypal_checkout.item_number.value += "individualresponsereport,";
		}
		if (document.getElementById('consultativePostSurveyReview').checked == true) {
			amount = amount + 200;
			recurringAmount += 200;
			document.paypal_checkout.item_number.value += "Post-Survey,";
		}
		if(discountType == 'flat'){
			// Treat the discount as a flat rate
			amount = amount - discountAmount;
			recurringAmount -= discountAmount;
		}else{
			// Treat the discount like a percentage
			amount = amount - (amount * discountAmount);
			recurringAmount = recurringAmount - (recurringAmount * discountAmount);
		}
		document.getElementById('totalOrderAmount').innerHTML = formatCurrency(amount,true);
		document.paypal_checkout.amount.value = amount;
		if(document.paypal_checkout.item_number.value.length){

			if(document.getElementById('discountCode').value != null)
				document.paypal_checkout.item_number.value = document.paypal_checkout.item_number.value + "Discount:" + document.getElementById('discountCode').value + ",DiscountAmount" + document.getElementById('discountCodeAmount').value ;
			else
				document.paypal_checkout.item_number.value = document.paypal_checkout.item_number.value;		}
		updateRecurringForm(amount,recurringAmount);
	}
	else if (document.getElementById('suggestionboxtable')){
		var amount = 495;
		var recurringAmount = 495;
		var discountAmount = document.getElementById('discountCodeAmount').value;
		var discountType = document.getElementById('discountCodeType').value;
		document.paypal_checkout.item_number.value = "Suggestionbox,";
		if (document.getElementById('customsuggestioncard').checked == true) {
			amount = amount + 5490;
			// minus 1000 for initial setup
			recurringAmount += 495;
			document.paypal_checkout.item_number.value += "CustomSuggestionCard,";
		}
		if (document.getElementById('segmentedreport').checked == true) {
			amount = amount + 295;
			recurringAmount += 295;
			document.paypal_checkout.item_number.value += "SegmentedReport,";
		}
		if(discountType == 'flat'){
			// Treat the discount as a flat rate
			amount = amount - discountAmount;
			recurringAmount -= discountAmount;
		}else{
			// Treat the discount like a percentage
			amount = amount - (amount * discountAmount);
			recurringAmount = recurringAmount - (recurringAmount * discountAmount);
		}
		
		
		document.getElementById('totalOrderAmount').innerHTML = formatCurrency(amount,true);
		document.paypal_checkout.amount.value = amount;
		if(document.paypal_checkout.item_number.value.length){

			if(document.getElementById('discountCode').value != null)
				document.paypal_checkout.item_number.value = document.paypal_checkout.item_number.value + "Discount:" + document.getElementById('discountCode').value + ",DiscountAmount" + document.getElementById('discountCodeAmount').value ;
			else
				document.paypal_checkout.item_number.value = document.paypal_checkout.item_number.value;		}
		updateRecurringForm(amount,recurringAmount);
	}
	else if (document.getElementById('employeesurveytable')){
		var amount = 4950;
		var recurringAmount = 4950;
		document.paypal_checkout.item_number.value = "EmployeeSurvey,";
	
		var discountAmount = document.getElementById('discountCodeAmount').value;
		var discountType = document.getElementById('discountCodeType').value;
		if (document.getElementById('consultativePostSurveyReview').checked == true) {
			amount = amount + 500;
			recurringAmount += 500;
			document.paypal_checkout.item_number.value += "Individual,";
		}
		if (document.getElementById('trackingReport').checked == true) {
			amount = amount + 500;
			recurringAmount += 500;
			document.paypal_checkout.item_number.value += "Tracking,";
		}
		if(discountType == 'flat'){
			// Treat the discount as a flat rate
			amount = amount - discountAmount;
			recurringAmount -= discountAmount;
		}else{
			// Treat the discount like a percentage
			amount = amount - (amount * discountAmount);
			recurringAmount = recurringAmount - (recurringAmount * discountAmount);
		}
		document.getElementById('totalOrderAmount').innerHTML = formatCurrency(amount,true);
		document.paypal_checkout.amount.value = amount;
		if(document.paypal_checkout.item_number.value.length){
			if(document.getElementById('discountCode').value != null)
				document.paypal_checkout.item_number.value = document.paypal_checkout.item_number.value + "Discount:" + document.getElementById('discountCode').value + ",DiscountAmount" + document.getElementById('discountCodeAmount').value ;
			else
				document.paypal_checkout.item_number.value = document.paypal_checkout.item_number.value;
		}
		updateRecurringForm(amount,recurringAmount);
	}
}

function updateRecurringForm(amount,recurringAmount){
	document.paypalrecurringcheckout.a3.value=recurringAmount;
	document.paypalrecurringcheckout.a1.value=amount;
	document.paypalrecurringcheckout.custom.value=document.paypal_checkout.item_number.value
}

function checkAgreement(submitType){
	if (document.getElementById('clientagreement').checked == true){
//		document.paypalrecurringcheckout.submit();
		if(submitType == 'recurring')
			form = document.getElementById("paypalrecurringcheckout");
		else
			form = document.getElementById("paypal_checkout");
		if(form)
		{
			form.submit();
		}
	}
	else{
		alert("Please indicate that you've read & agree to our Terms & Conditions");
	}
}

function formatCurrency(num,signs) {
	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));
	}

	if (signs == true) {
		return (((sign)?'':'-') + '$' + num + '.' + cents);
	} else {
		return (num + '.' + cents);
	}
}


