
if( document.addEventListener ) document.addEventListener( 'DOMContentLoaded', cmxform, false );

/*
addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
*/
function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function removeEvent( obj, type, fn )
{
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}

function cmxform(){
 // Hide forms
 $( 'form.cmxform' ).hide().end();

 // Processing
 $( 'form.cmxform' ).find( 'li/label' ).not( '.nocmx' ).each( function( i ){
 var labelContent = this.innerHTML;
 var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
 var labelSpan = document.createElement( 'span' );
 labelSpan.style.display = 'block';
 labelSpan.style.width = labelWidth;
 labelSpan.innerHTML = labelContent;
 this.style.display = '-moz-inline-box';
 this.innerHTML = null;
 this.appendChild( labelSpan );
 } ).end();

 // Show forms
 $( 'form.cmxform' ).show().end();
}

function removeCurrency( strValue ) {
/************************************************
DESCRIPTION: Removes currency formatting from
  source string.

PARAMETERS:
  strValue - Source string from which currency formatting
     will be removed;

RETURNS: Source string with commas removed.
*************************************************/
  var objRegExp = /\(/;
  var strMinus = '';

  //check if negative
  if(objRegExp.test(strValue)){
    strMinus = '-';
  }

  objRegExp = /\)|\(|[,]/g;
  strValue = strValue.replace(objRegExp,'');
  if(strValue.indexOf('$') >= 0){
    strValue = strValue.substring(1, strValue.length);
  }
  returnVal = parseFloat(strMinus + strValue);
  if(!returnVal) returnVal = 0;
  return returnVal;
}

function updateFormTotals() {
	if(typeof(document.forms['ExhibitorForm']) != "undefined") {
		$totalPrice = 0;
		$discount = 0;
		switch(document.forms['ExhibitorForm'].ExhibitorPackages.selectedIndex) {
			case 0: $totalPrice = 0.00; break
			case 1: $totalPrice = 695.00; break
			case 2: $totalPrice = 1295.00; break
			case 3: $totalPrice = 1895.00; break
		}
		switch(document.forms['ExhibitorForm'].Discount.value.toUpperCase()) {
			case "NEWCUSTOMER":
			case "BABYSTEALS15":
				if(document.forms['ExhibitorForm'].ExhibitorPackages.selectedIndex) {
						$discount = Math.round($totalPrice*0.15*100)/100
						$totalPrice -= $discount;
						document.getElementById("discountSavings").innerHTML = "You save $" + $discount;
				}
				break
			case "RETURNINGCUSTOMER":
				if(document.forms['ExhibitorForm'].ExhibitorPackages.selectedIndex) {
						$discount = Math.round($totalPrice*0.20*100)/100
						$totalPrice -= $discount;
						document.getElementById("discountSavings").innerHTML = "You save $" + $discount;
				}
				break
				
			case "NONPROFIT":
				if(document.forms['ExhibitorForm'].ExhibitorPackages.selectedIndex) {
						$discount = 445
						$totalPrice -= $discount;
						document.getElementById("discountSavings").innerHTML = "You save $" + $discount;
				}
				break
			default:	
				document.getElementById("discountSavings").innerHTML = "";
				break
		}
		switch(document.forms['ExhibitorForm'].ProgramAd.selectedIndex) {
			case 0: $totalPrice += 0; break
			case 1: $totalPrice += 175; break
			case 2: $totalPrice += 90; break
			case 3: $totalPrice += 50; break
		}
		switch(document.forms['ExhibitorForm'].CornerEndLocation.selectedIndex) {
			case 0: $totalPrice += 0; break
			case 1: $totalPrice += 150; break
			case 2: $totalPrice += 300; break
		}
		if(document.forms['ExhibitorForm'].ElectricalOutlet.checked) $totalPrice += 75;
		$totalPrice += removeCurrency(document.forms['ExhibitorForm'].AdditionalFurniture.value);
		$totalPrice += removeCurrency(document.forms['ExhibitorForm'].ProgramAd.value);
		$totalPrice += removeCurrency(document.forms['ExhibitorForm'].Other.value);
		document.forms['ExhibitorForm'].ExhibitorTotal.value = "\$" + Math.round($totalPrice*100)/100;
	}
}

addEvent(window, 'load', updateFormTotals);

function attachUpdateEvents() {
	if (document.getElementsByTagName) {
		var inputs = document.getElementsByTagName('input');
		for (var i = 0; i < inputs.length; i++) {
			input = inputs[i];
			input.onblur = updateFormTotals;
			input.onchange = updateFormTotals;
		}
		var selects = document.getElementsByTagName('select');
		for (var i = 0; i < selects.length; i++) {
			select = selects[i];
			select.onblur = updateFormTotals;
			select.onchange = updateFormTotals;
		}
	}
}

addEvent(window, 'load', attachUpdateEvents)
