// The type of the registration (Regular or Student).  Default: Regular
var regType = "";

// He needs hotel?
var wantsHotel = false;

// Prices
var early_student 	= 300.00;
var late_student  	= 350.00;
var early_regular 	= 400.00;
var late_regular  	= 450.00;
var tutorials 			=  60.00;
var proceedings			=  15.00;
var add_pages				=	100.00;
var excursion				=  30.00;
var conf_dinner			=  50.00;

// Accepted papers
var papers = new Array( 2, 9, 11, 15, 30, 33, 34, 35, 36, 37, 39, 43, 46, 47, 52, 55, 56, 59, 60, 65, 67, 71, 74, 75  );
var posters = new Array( 1, 6, 16, 22, 23, 26, 40, 41, 42, 48, 50, 54, 61, 68, 70, 72, 77, 79, 80 ); 

// The date when Early Registration ends.
var earlyDate=new Date()
earlyDate.setFullYear( 2009, 3, 17 )	// 2006 - 10 - 09
var today = new Date();

// Check elligibility for early registration today.
var early = today < earlyDate;		

// If selected country is Greece then it shows the VAT details section, if not then it hides it
function checkVAT() 
{
	if ( document.RegistrationForm.country.value == "Greece" )
		document.getElementById( 'greeks_only' ).style.display = "block";
	else
		document.getElementById( 'greeks_only' ).style.display = "none";
	
}

// Re-calculates the total and the subtotals of registration based on the information
// given by the user.
function calcTotal()
{
	var total = 0;
	
	// Subtotal - Registration Type
	if ( regType == "student" )
		if ( early ) {
			document.RegistrationForm.reg_total.value = early_student.toFixed(2);
			total += early_student;
		}
		else {
			document.RegistrationForm.reg_total.value = late_student.toFixed(2);
			total += late_student;
		}
	else if ( regType == "regular" )
		if ( early ) {
			document.RegistrationForm.reg_total.value = early_regular.toFixed(2);
			total += early_regular;
		}
		else {
			document.RegistrationForm.reg_total.value = late_regular.toFixed(2);
			total += late_regular;
		}

	// Subtotal - Proceedings
	if ( document.RegistrationForm.add_proceedings.value != "0" ) {
		document.RegistrationForm.add_proc_total.value = (document.RegistrationForm.add_proceedings.value * proceedings.toFixed(2)).toFixed(2);
		total += document.RegistrationForm.add_proceedings.value * proceedings;
	}
	else
		document.RegistrationForm.add_proc_total.value = "";
	
	// Subtotal - Conference Dinner
	if ( document.RegistrationForm.conf_dinner.value != "0" ) {
			document.RegistrationForm.conf_dinner_total.value = (document.RegistrationForm.conf_dinner.value * conf_dinner).toFixed(2);
			total += document.RegistrationForm.conf_dinner.value * conf_dinner;
	}
	else {
		document.RegistrationForm.conf_dinner_total.value = "";
	}
	
	// Subtotal - Extra Pages
	if ( document.RegistrationForm.add_pages.value != "0" ) {
		document.RegistrationForm.add_pages_total.value = (document.RegistrationForm.add_pages.value * add_pages).toFixed(2);
		total += document.RegistrationForm.add_pages.value * add_pages;
	}
	else {
		document.RegistrationForm.add_pages_total.value = "";
	}

	// Total
	if ( total != 0 )
		document.RegistrationForm.total.value = total.toFixed(2);
	else
		document.RegistrationForm.total.value = "";
		
	document.RegistrationForm.real_total.value = ( total * 100 ).toFixed(0);
}

// Validates if user filled correctly the registration form.
function validateForm()
{
	var missingFields = "";
	var hasErrors = false;
	var errorText = "";
	
	// Check for missing fields
	if ( document.RegistrationForm.first_name.value == "" ) {
		hasErrors = true;
		missingFields += "\f\t - First (personal) name\n";
	}
	
	if ( document.RegistrationForm.last_name.value == "" ) {
		hasErrors = true;
		missingFields += "\f\t - Last (family) name\n";
	}

	if ( document.RegistrationForm.email.value == "" ) {
		hasErrors = true;
		missingFields += "\f\t - Email\n";
	}
	
	if ( document.RegistrationForm.affiliation.value == "" ) {
		hasErrors = true;
		missingFields += "\f\t - Affiliation/Company\n";
	}

	if ( document.RegistrationForm.address_1.value == "" ) {
		hasErrors = true;
		missingFields += "\f\t - Address line 1\n";
	}
	
	if ( document.RegistrationForm.postal_code.value == "" ) {
		hasErrors = true;
		missingFields += "\f\t - Postal Code\n";
	}

	if ( document.RegistrationForm.city.value == "" ) {
		hasErrors = true;
		missingFields += "\f\t - City\n";
	}

	if ( document.RegistrationForm.country.value == "" ) {
		hasErrors = true;
		missingFields += "\f\t - Country\n";
	}

	if ( document.RegistrationForm.telephone.value == "" ) {
		hasErrors = true;
		missingFields += "\f\t - Telephone\n";
	}

	if ( regType == "" ) {
		hasErrors = true;
		missingFields += "\f\t - Registration Type\n";
	}
	
	// Check if he wants hotel
	if ( wantsHotel == true ) {
		if ( document.RegistrationForm.room_arrival.value == "" ) {
			hasErrors = true;
			missingFields += "\f\t - Date of arrival\n";
		}
		
		if ( document.RegistrationForm.room_nights.value == "" ) {
			hasErrors = true;
			missingFields += "\f\t - Number of nights\n";
		}
	}
	
	errorText = "The must fill the following fields before proceeding: \n" + missingFields + "\n";
	
	if ( hasErrors ) 
		alert ( errorText );
	else { 						// Check for invalid input
		// Check if papers are valid
		if ( document.RegistrationForm.paper_1.value != "" ) {
			if ( ! paperExists( document.RegistrationForm.paper_1.value ) ) {
				alert ( "Paper does not exist" );
			}
		}
	}
	
	return ! hasErrors;
}

// 
function paperExists( paperNumber )
{
	for ( i in papers ) {
		if ( paperNumber == papers[i] )
			return true;
	}
	
	for ( i in posters ) {
		if ( paperNumber == posters[i] )
			return true;
	}
	
	return false;
}

// Checks if the key pressed is a number or not.  Used to restrict the input on certain
// textboxes to only numbers.
function isNumberKey( evt )
{
 var charCode = ( evt.which ) ? evt.which : event.keyCode
 if ( charCode > 31 && ( charCode < 48 || charCode > 57 ) )
	return false;

 return true;
}

// Update the fields to show required fields
function updateHotel()
{
	if ( wantsHotel == true ) {
		document.getElementById( 'hotel_doa' ).style.visibility = "visible";
		document.getElementById( 'hotel_non' ).style.visibility = "visible";
		document.RegistrationForm.room_arrival.style.visibility = "visible";
		document.RegistrationForm.room_nights.style.visibility = "visible"
	}
	else {
		document.getElementById( 'hotel_doa' ).style.visibility = "hidden";
		document.getElementById( 'hotel_non' ).style.visibility = "hidden";
		document.RegistrationForm.room_arrival.style.visibility = "hidden";
		document.RegistrationForm.room_nights.style.visibility = "hidden"
	}
}
