// JavaScript Document
function initForm()
{
	document.getElementById("sector_of_activity_select").onchange = updateSector;
	//document.getElementById("days_of_attendance_select").onchange = updateSector;
}
//fc_registerOnLoad ( new Array( initForm, null ) );

function deleteForm()
{
	document.forms['booking_form'].innerHTML = "";
	document.getElementById('submitBtn').innerHTML = "";
}

function paymentOption( action )
{
	switch (action)
	{
		default:
		case 'delete':		
			
			var paymentDivCnt = '<h3>Cancellation policy:</h3><div class="formRow"><p>If you become unable to attend the conference, please inform us by email to info@itinthecommunity.com.  If you fail to inform us by email that you will not be attending, you will be subject to a &pound;25 admin charge</p></div>';
			/*
			if ( document.getElementById("paymentSection") )
				document.getElementById('paymentSection').innerHTML = "";
			*/
			break;
			
		case 'add':
			var paymentDiv;
			var paymentDivCnt = '<h3 id="payment_choice">Payment options</h3>' +  
			'<div class="formRow">' + 
				'<div class="column formLeft">' + 
					'<p><input type="radio" class="radioBtn" name="payment_choice-mandatory" value="by_creditcard"/> By Credit Card<br />(Via PayPal)' + 
					'</p>' + 
				'</div>' + 
				'<div class="column formRight">' + 
					'<p><input type="radio" class="radioBtn" name="payment_choice-mandatory" value="by_invoice" checked="checked"/> By Invoice<br />(Invoice will be sent to the above address)' + 
					'</p>' + 
				'</div>' + 
			'</div>';		
			
			
			var attendace = '';
			var fee = '';
			var daySelection = document.getElementById("days_of_attendance_select").value;
			if ( daySelection != '2_Day' )
			{
				attendace = '1 Day Conference';
				fee = '90';
			}else{
				attendace = '2 Day Conference';
				fee = '145';
			}
			var payementTxt = 'Total fee payable: ' + attendace + ' - &pound;' + fee + ' + VAT';			
			paymentDivCnt += '<div class="formRow"><p>' + payementTxt + '</p>';
			paymentDivCnt += '<input type="hidden" name="payable_fee" value="'+fee+' pounds + VAT"/></div>';
			
			
			var cancelPol = '<h3>Cancellation policy:</h3><div class="formRow"><p>Cancellations made before or on Friday 7th November will be refunded the total fee minus a &pound;25 admin charge. Cancellations made on or after 8th November will not be entitled to a refund.  All Cancellations must be made in writing to the Conference Office by email to melissa@itinthecommunity.com.</p></div>';
			paymentDivCnt += cancelPol;
			
			break;
	}
	
	if ( document.getElementById("paymentSection") )
	{
		paymentDiv = document.getElementById("paymentSection");
		paymentDiv.innerHTML = paymentDivCnt;
	}else{
		paymentDiv = document.createElement('div');
		paymentDiv.setAttribute('id', 'paymentSection');
		paymentDiv.innerHTML = paymentDivCnt;
		document.forms['booking_form'].appendChild(paymentDiv);
	}
	
}

function updateSector()
{
	var container = document.getElementById("sector_of_activity_select").parentNode;	
	
	//-----------------------------------------------------------------
	
	//alert( document.getElementById("newFormElmt") );	
	var newFormElmt = document.getElementById("newFormElmtSector");
	if ( newFormElmt == null )
	{
		newFormElmt = document.createElement('div');
		newFormElmt.setAttribute('id', 'newFormElmtSector');		
		container.appendChild(newFormElmt);
	}else{
		document.getElementById("newFormElmtSector").innerHTML = "";
	}
	
	//-----------------------------------------------------------------
	
	var newText;
	var newField;
	
	//-----------------------------------------------------------------
	
	var selection = document.getElementById("sector_of_activity_select").value;
	
	switch (selection)
	{
		/*
		case "Government_and_Public_Sector":
		case "Commercial_and_Private_sector":
			//- add payment
			paymentOption( 'add' );			
			reserveListDisclaimer( 'delete' );
			break;		
		*/
		case "SME_-_IT_Works":
			document.getElementById("days_of_attendance_select").value = 'Tuesday_only';
			break;
		case "SME_-_Whole_Conference":
			document.getElementById("days_of_attendance_select").value = '2_Day';
			break;
		default:
			//document.getElementById("newFormElmtSector").innerHTML = "";
			//- remove payment
			/*
			paymentOption( 'delete' );	
			checkBookingCounts();
			*/
			break;
	}
}


/*
 *********************************************** */

function reserveListDisclaimer( action )
{
	switch (action)
	{
		default:
		case 'delete':		
			var disclaimerDivCnt = '';
			break;
			
		case 'add':
			var disclaimerDivCnt = '<h3>Reserve list:</h3><div class="formRow"><p>The limit of free delegate places has now been reached. You can still proceed with your registration, but you will be put on the reserve list. If any places become available, we will contact you to finalise your booking.</p><input type="hidden" name="reserve_list" value="on_reserve_list"/></div>';
			break;
	}
	
	if ( document.getElementById("reserveListSection") )
	{
		disclaimerDiv = document.getElementById("reserveListSection");
		disclaimerDiv.innerHTML = disclaimerDivCnt;
	}else{
		disclaimerDiv = document.createElement('div');
		disclaimerDiv.setAttribute('id', 'reserveListSection');
		disclaimerDiv.innerHTML = disclaimerDivCnt;
		document.forms['booking_form'].appendChild(disclaimerDiv);
	}	
}

function updateBookingLimit ( bookingCounts )
{
	bookingCounts = bookingCounts.split(',');
	freeCount = parseInt( bookingCounts[0] );
	freeLimit = parseInt( bookingCounts[1] );
	payCount = parseInt( bookingCounts[2] );
	
	//alert('check: ' + freeCount + ' - ' + freeLimit);

	if ( freeCount >= freeLimit )
	{
		reserveListDisclaimer ('add');	
	}else{
		reserveListDisclaimer ('delete');	
	}
}


// Get the HTTP Object
function getHTTPObject ()
{
	var xmlHttp;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}catch (e){
		// Internet Explorer
		try{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			try{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch (e){				
				xmlHttp = null;
			}
		}
	}	
	return xmlHttp;
}	

// received data
function fc_phpHttpObjState ()
{
	//alert (this);
	if ( this.readyState == 4 || this.readyState == "complete" )
	{
		updateBookingLimit( this.responseText ); 
		delete this; 
	}
}

// request data
function checkBookingCounts ()
{
	phpHttpObject = getHTTPObject();
		
	if (phpHttpObject != null)
	{
		phpHttpObject.open("GET", "./php/functions.php?AJAX_getBookingCount=1", true);
			
		phpHttpObject.onreadystatechange = new Function();
		phpHttpObject.onreadystatechange = fc_phpHttpObjState;
		
		phpHttpObject.send(null);
	}
}
