// JavaScript Document

var imgPass = "<img src=\"images/tick.gif\" height=\"13\" width=\"13\">";

var imgFail = "<img src=\"images/cross.gif\" height=\"13\" width=\"13\">";

var imgBlank = "<img src=\"images/blank.gif\" height=\"13\" width=\"13\">";

function isTab(e) {
	var code;
	//get keycode of button pressed
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if (code == "9") {
		return true;
	} else {
		return false;
	}
}



function isBackspace(e) {
	var code;
	//get keycode of button pressed
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if (code == "8") {
		return true;
	} else {
		return false;
	}
}

function isFieldEmpty(id) {
	field = document.getElementById(id).value;
	if (field == '') {
		return true;
	} else {
		return false;
	}
}

function gotError(id) {
	output = document.getElementById('form_'+id).innerHTML;
	//alert(output+"\n"+imgError);
	if (output == imgFail) {
		return true;
	} else {
		return false;
	}
}

function validateFieldFocus(id, field, e, req) {
	if ((!isTab(e) && !isFieldEmpty(id))) {
		//errorCheck(id, field_type, tooltip)
		errorCheck(id, field);
	} else if ((!req && isFieldEmpty(id))) {
		errorClear(id);
	} else if ((isBackspace(e) && isFieldEmpty(id))) {
		errorCheck(id, field);
	} else if ((gotError(id) && isFieldEmpty(id))) {
		errorCheck(id, field);
	}
}

function validateFieldBlur(id, field, req) {
	//if your leaving a required field or an unempty not required field then check it but dont display the tooltip
	if ((req || (!req && !isFieldEmpty(id)))) {
		errorCheck(id, field);
	//shold catch eveything else. if not required and empty then just remove all error related crap
	} else if (!req && isFieldEmpty(id)) {
		errorClear(id);
	}
 
}

function errorCheck(id, field) {
	var input, inputValue, output, regExp;
	//set variables
	input = document.getElementById(id);
	output = document.getElementById('form_'+id);
	inputValue = input.value;
	//based on which field type it is use a different expression
	switch(field)
	{
	case "txt":
	  regExp = "^[\-\&\,\.a-zA-Z ]{1,50}$";
	  break;    
	case "num":
	  regExp = "^[\-0-9 \+]{1,30}$";
	  break;
	case "adr":
	  regExp = "^[\-\&\,\.a-zA-Z0-9 ]{1,30}$";
	  break;
	case "email":
	  regExp = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	  break;
	}
	
	//do error checking
	var regExpMatch = new RegExp(regExp);
	if (inputValue.match(regExpMatch)) {
		//show pass image and hide any error messages
		output.innerHTML = imgPass;
		return true;
	} else {
		//show fail image
		output.innerHTML = imgFail;
		return false;
	}	
}

function errorClear(id) {
	output = document.getElementById('form_'+id);
	output.innerHTML=imgBlank;
}

function validateForm() {
	var display_errors = document.getElementById('displayErrors');
	var display_errors_border = document.getElementById('displayErrorsBorder');
	
	//error message header
	var error_header = "<p>The follwing errors were detected when trying to submit your registration, please correct them and re-submit.</p><p>&nbsp;</p>";
	var error_msg = ""
	
	//variables collecting
	var firstname = document.getElementById('first_name');
	var lastname = document.getElementById('last_name');
	var position = document.getElementById('position');
	
	var organisation = document.getElementById('organisation');
	var address_1 = document.getElementById('address_1');
	var suburb = document.getElementById('suburb');
	var state = document.getElementById('state');
	var postcode = document.getElementById('postcode');
	var phone = document.getElementById('phone');
	var fax = document.getElementById('fax');
	var email = document.getElementById('email');
	var namebadge = document.getElementById('namebadge');
	
	var registration0 = document.getElementById('registration0');
	var registration1 = document.getElementById('registration1');
	var registration2 = document.getElementById('registration2');
	var registration3 = document.getElementById('registration3');
	var arrival = document.getElementById('arrival');
	var departure = document.getElementById('departure');
	
	var firstname2 = document.getElementById('firstname2');
	var lastname2 = document.getElementById('lastname2');
	
	var sharing = document.getElementById('sharing');
	
	var workshop1 = document.getElementById('workshop1');
	var workshop2 = document.getElementById('workshop2');
	var workshop3 = document.getElementById('workshop3');
	
	var terms = document.getElementById('terms');
	
	var payment0 = document.getElementById('payment0');
	var payment1 = document.getElementById('payment1');
	var payment2 = document.getElementById('payment2');
	
	var cardname = document.getElementById('cardname');
	var cardnumber = document.getElementById('cardnumber');
	var cvscode = document.getElementById('cvscode');
	
	if (isFieldEmpty(firstname.id)) {
		error_msg += "<p>- First name field must not be left blank.</p>";
	} else if (!errorCheck(firstname.id, 'txt')) {
		error_msg += "<p>- First name field contains invalid characters.</p>";
	}
	
	if (isFieldEmpty(lastname.id)) {
		error_msg += "<p>- Last name field must not be left blank.</p>";
	} else if (!errorCheck(lastname.id, 'txt')) {
		error_msg += "<p>- Last name field contains invalid characters.</p>";
	}
	
	if (!isFieldEmpty(position.id)) {
		if (!errorCheck(position.id, 'adr')) {
			error_msg += "<p>- Position field contains invalid characters.</p>";
		}
	}
	
	if (!isFieldEmpty(organisation.id)) {
		if (!errorCheck(organisation.id, 'adr')) {
			error_msg += "<p>- Organisation field contains invalid characters.</p>";
		}
	}
	
	if (isFieldEmpty(address_1.id)) {
		error_msg += "<p>- Address field must not be left blank.</p>";
	} else if (!errorCheck(address_1.id, 'adr')) {
		error_msg += "<p>- Address field contains invalid characters.</p>";
	}
	
	if (isFieldEmpty(suburb.id)) {
		error_msg += "<p>- Suburb field must not be left blank.</p>";
	} else if (!errorCheck(suburb.id, 'adr')) {
		error_msg += "<p>- Suburb field invalid characters.</p>";
	}
	
	if (isFieldEmpty(state.id)) {
		error_msg += "<p>- State field must not be left blank.</p>";
	} else if (!errorCheck(state.id, 'txt')) {
		error_msg += "<p>- State field contains invalid characters.</p>";
	}
	
	if (isFieldEmpty(postcode.id)) {
		error_msg += "<p>- Postcode field must not be left blank.</p>";
	} else if (!errorCheck(postcode.id, 'adr')) {
		error_msg += "<p>- Postcode field contains invalid characters.</p>";
	}
	
	if (isFieldEmpty(phone.id)) {
		error_msg += "<p>- Phone number field must not be left blank.</p>";
	} else if (!errorCheck(phone.id, 'num')) {
		error_msg += "<p>- Phone number field contains invalid characters.</p>";
	}
	
	if (!isFieldEmpty(fax.id)) {
		if (!errorCheck(fax.id, 'num')) {
			error_msg += "<p>- Fax number contains invalid characters.</p>";
		}
	}
	
	if (isFieldEmpty(email.id)) {
		error_msg += "<p>- Email address field must not be left blank.</p>";
	} else if (!errorCheck(email.id, 'email')) {
		error_msg += "<p>- Email address field contains invalid characters.</p>";
	}
	
	if (!isFieldEmpty(namebadge.id)) {
		if (!errorCheck(namebadge.id, 'txt')) {
			error_msg += "<p>- Name badge field contains invalid characters.</p>";
		}
	}

	if (!isFieldEmpty(sharing.id)) {
		if (!errorCheck(sharing.id, 'txt')) {
			error_msg += "<p>- The 'Sharing With' field contains invalid characters.</p>";
		}
	}
	
	if (!registration0.checked && !registration1.checked && !registration2.checked && !registration3.checked) {
		error_msg += "<p>- You must select a registration type.</p>";
	}
	
	if (arrival.value == departure.value) {
		error_msg += "<p>- Please select an arrival and departure date which are not the same.</p>";	
	}
	
	if ((workshop1.checked && workshop3.checked) || (workshop2.checked && workshop3.checked)) {
		error_msg += "<p>- Please indicate wether you wish to attend one workshop or both.</p>";
	}
	
	if (!payment0.checked && !payment1.checked && !payment2.checked) {
		error_msg += "<p>- Please select a payment type.</p>";
	}	
	
	if (payment2.checked) {
		if (isFieldEmpty(cardnumber.id)) {
			error_msg += "<p>- Card number field must not be blank if 'Credit Card' is selected.</p>";
		} else if (!errorCheck(cardnumber.id, 'num')) {
			error_msg += "<p>- Card number field contains invalid characters..</p>";
		}
		
		if (isFieldEmpty(cardname.id)) {
			error_msg += "<p>- Card name field must not be blank if 'Credit Card' is selected.</p>";
		} else if (!errorCheck(cardname.id, 'txt')) {
			error_msg += "<p>- Card name field contains invalid characters.</p>";
		}
		
		if (isFieldEmpty(cvscode.id)) {
			error_msg += "<p>- CVS code field must not be blank if 'Credit Card' is selected.</p>";
		} else if (!errorCheck(cvscode.id, 'num')) {
			error_msg += "<p>- CVS code field contains invalid characters.</p>";
		}
	}		
		

		
		if (!isFieldEmpty(sharing.id)) {
			if (!errorCheck(sharing.id, 'txt')) {
				error_msg += "<p>- The 'Sharing With' field contains invalid characters.</p>";
			}
		}

	if (!terms.checked) {
		error_msg += "<p>- Please agree to the <a href=\"terms.html\" onclick=\"return popupItem('terms.html');\">terms and conditions</a>.</p>";
	}
	
	//if errors displayand scroll to the top of the window
	if (error_msg != "") {
		window.scrollTo(0,0);
		display_errors.innerHTML = error_header+error_msg+"<p>&nbsp;</p>";
		display_errors_border.style.display = "block";
		display_errors.style.display = "block";
	} else {
		document.form_register.submit();
	}
}

function showCatering(value) {
	var box0 = document.getElementById("box0");
	var box1 = document.getElementById("box1");
	var box2 = document.getElementById("box2");
	var box3 = document.getElementById("box3");
	var currentbox = document.getElementById("box"+value);
	
	box0.className = 'hiddenPic';
	box1.className = 'hiddenPic';
	box2.className = 'hiddenPic';
	box3.className = 'hiddenPic';
	
	currentbox.className = 'showPic';

}

function showPayment(value) {
	var currentbox = document.getElementById("credit");
	if (value == "2") {
		currentbox.className = 'showPic';
	} else {
		currentbox.className = 'hiddenPic';
	}
}

var colour = "even";

function giveNewRow(name, price) {
	var data = "";
	
	if (colour == "even") {
		colour = "odd";
	} else {
		colour = "even";
	}
	
	data += " <tr>\n";
	data += "  <td width=\"434\" height=\"30\" class=\""+colour+"\"><h2>"+name+"</h2></td>\n"
	data += "  <td width=\"86\" height=\"30\" class=\""+colour+"\">"+price+"</td>\n"
	data += " </tr>";
	
	return data;
}

function showTotalCost() {
	//var declare
	var total = 0;
	var table_begin = "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"outerBorder\">\n";
	var table_end ="</table>\n";
	var table_data = "";
	var divOutput = document.getElementById('divTotalCost');

	var registration0 = document.getElementById('registration0');
	var registration1 = document.getElementById('registration1');
	var registration2 = document.getElementById('registration2');
	var registration3 = document.getElementById('registration3');
	
	var workshop1 = document.getElementById('workshop1');
	var workshop2 = document.getElementById('workshop2');
	var workshop3 = document.getElementById('workshop3');
	var friday_extra = document.getElementById('friday_extra');
	var saturday_extra = document.getElementById('saturday_extra');
	var sunday_extra = document.getElementById('sunday_extra');
	
	var arrival = document.getElementById('arrival');
	var departure = document.getElementById('departure');
	var bre_0 = document.getElementById('0_0');
	var emb_0 = document.getElementById('1_0');
	var emb_1 = document.getElementById('1_1');
	var hor_0 = document.getElementById('2_0');
	
	if (registration0.checked) {
		
		table_data += giveNewRow("Earlybird Delegate Registration", "$500.00");
		total += 500;
		
		//get vars under reg
		var sat_dinner = document.getElementById('sat_dinner0');
		var sunday_lunch = document.getElementById('sunday_lunch0');
		var cocktail_party = document.getElementById('cocktail_party0');
		
		if (sat_dinner.checked) {
			table_data += giveNewRow("Saturday Conference Dinner", "Free");
		}
		if (sunday_lunch.checked) {
			table_data += giveNewRow("Sunday Lunch", "Free");
		}
		if (cocktail_party.checked) {
			table_data += giveNewRow("Friday Cocktail Party", "Free");
		}
	}

	if (registration1.checked) {
		
		table_data += giveNewRow("Full Delegate Registration", "$600.00");
		total += 600;
		
		//get vars under reg
		var sat_dinner = document.getElementById('sat_dinner1');
		var sunday_lunch = document.getElementById('sunday_lunch1');
		var cocktail_party = document.getElementById('cocktail_party1');
		
		if (sat_dinner.checked) {
			table_data += giveNewRow("Saturday Conference Dinner", "Free");
		}
		if (sunday_lunch.checked) {
			table_data += giveNewRow("Sunday Lunch", "Free");
		}
		if (cocktail_party.checked) {
			table_data += giveNewRow("Friday Cocktail Party", "Free");
		}
	}

	if (registration2.checked) {
		
		table_data += giveNewRow("Saturday Delegate Registration", "$250.00");
		total += 250;
		
		//get vars under reg
		var sat_dinner = document.getElementById('sat_dinner2');
		
		if (sat_dinner.checked) {
			table_data += giveNewRow("Saturday Conference Dinner", "$100.00");
			total += 100;
		}
	}

	if (registration3.checked) {
		
		table_data += giveNewRow("Sunday Delegate Registration", "$250.00");
		total += 250;
		
		//get vars under reg
		var sat_dinner = document.getElementById('sat_dinner1');
		var sunday_lunch = document.getElementById('sunday_lunch1');
		
		if (sat_dinner.checked) {
			table_data += giveNewRow("Saturday Conference Dinner", "$100.00");
			total += 100;
		}
		if (sunday_lunch.checked) {
			table_data += giveNewRow("Sunday Lunch", "Free");
		}
	}
	
	//extra stuff
	if (workshop1.checked) {
		table_data += giveNewRow("Workshop 1 (Skin Workshop)", "$65.00");
		total += 65;
	}
	if (workshop2.checked) {
		table_data += giveNewRow("Workshop 2 (Immuno Workshop)", "$65.00");
		total += 65;
	}
	if (workshop3.checked) {
		table_data += giveNewRow("Workshops 1 & 2 (Skin & Immuno)", "$150.00");
		total += 150;
	}
	if (friday_extra.checked) {
		table_data += giveNewRow("Extra Ticket for Friday Cocktail Party", "$50.00");
		total += 50;
	}
	if (saturday_extra.checked) {
		table_data += giveNewRow("Extra Ticket for Saturday Conference Dinner", "$100.00");
		total += 100;
	}
	if (sunday_extra.checked) {
		table_data += giveNewRow("Extra Ticket for Sunday Lunch", "$20.00");
		total += 20;
	}

	
	//accomidation
	//get number of nights
	var nights = dateDiff(arrival.value, departure.value);
	
	if (bre_0.checked) {
		costs = nights*164;
		table_data += giveNewRow(nights+" Nights at the Break Free, 1 Bedroom Apartment", "$"+costs+".00");
		total += costs;
	}
	if (emb_0.checked) {
		costs = nights*174;
		table_data += giveNewRow(nights+" Nights at the Oaks Embassy, 1 Bedroom Executive Apartment", "$"+costs+".00");
		total += costs;
	}
	if (emb_1.checked) {
		costs = nights*197;
		table_data += giveNewRow(nights+" Nights at the Oaks Embassy, 2 Bedroom Apartment", "$"+costs+".00");
		total += costs;
	}
	if (hor_0.checked) {
		costs = nights*197;
		table_data += giveNewRow(nights+" Nights at the Oaks Horizon, 2 Bedroom Apartment", "$"+costs+".00");
		total += costs;
	}
	
	//total
	if (table_data) {
		table_data += giveNewRow("Total Cost", "<u>$"+total+".00</u>");
		//return table
		divOutput.innerHTML = table_begin+table_data+table_end;
	}
	colour = "even";
		
}

function dateDiff(strDate1,strDate2){
	//Set the two dates
	var date0 = strDate1.split("-");
	var date1 = strDate2.split("-");
	
	var newdate0 = new Date(date0[0], (date0[1]-1), (date0[2]-0));
	var newdate1 = new Date(date1[0], (date1[1]-1), (date1[2]-0));
	//Set 1 day in milliseconds
	var one_day = 1000*60*60*24;
	//Calculate difference btw the two dates, and convert to days
	return (Math.ceil((newdate1.getTime()-newdate0.getTime())/(one_day)));
}

function popupItem(url) {
	var newwindow = window.open(url,"mywindow","menubar=0,resizable=1,width=520,height=450,scrollbars=1");
	if (window.focus) {
		newwindow.focus();
	}
	return false;
}











