function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

function expand_section(i) {
	var v = document.getElementById(i);
	if(v.style.display == "none") v.style.display = "table-row";
	else v.style.display = "none";
}

function show_method(m) {
	var t = document.getElementsByTagName('tbody');
	for(o = 5; o < t.length; o++) {
		if(t[o].style.display == "table-row-group") t[o].style.display = "none";
	}
	if(m != 0) {
		var p = document.getElementById(m);
		p.style.display = "table-row-group";
	} // v-----v  lazy, but my brain is turning to MUSH  v-----v  \\
	if(m != "card"){
		var rb = document.booking.card_type;
		for(i = 0; i < rb.length; i++) {
			if(rb[i].checked == true) rb[i].checked = false;
		}
		document.getElementById("card_number").value = "";
		document.getElementById("exp_month").selectedIndex = 0;
		document.getElementById("exp_year").selectedIndex = 0;
		document.getElementById("card_name").value = "";
	} 
	if(m != "invoice"){
		document.getElementById("invoice_name").value = "";
		document.getElementById("invoice_address").value = "";
		document.getElementById("invoice_contact").value = "";
		document.getElementById("invoice_phone").value = "";
		document.getElementById("invoice_email").value = "";
	}
	// ^-----^  end of lazy section - will fix 'one day'  ^-----^  \\
}

function show_core(w) {
	if(w == 2.1 || w == 1.1){
		var f = document.getElementById("course").options[document.getElementById("course").selectedIndex].value;
		var q = document.getElementById("question");
		if(f == 1.1) {
			q.innerHTML = "<P>You have selected Certificate IV in Training and Assessment (Session 1).<br />Tick if you would also like to book Session 2:&nbsp;&nbsp;<input type='checkbox' id='elective_check' name='elective_check' onClick='show_elect(\"s2\"); return get_date_list(\"session2\");'</p>";
		}
		if(f == 2.1) {
			q.innerHTML = "<P>You have selected Workplace Health and Safety Officer core units.<br />Tick if you would also like to book an elective:&nbsp;&nbsp;<input type='checkbox' id='elective_check' name='elective_check' onClick='show_elect(\"elective\")'</p>";
		}
		document.getElementById("core").style.display = "table-row-group";
	} else {
		document.getElementById("core").style.display = "none";
		document.getElementById("elective").style.display = "none";
		document.getElementById("s2").style.display = "none";
	}
	clear_dates();
}

function show_elect(thingo) {
	var r = document.getElementById(thingo);
	if(r.style.display == "none") r.style.display = "table-row-group";
	else {
		r.style.display = "none";
		clear_dates();
	}
	return;
}

function clear_dates() {
	document.getElementById("elective").style.display = "none";
	document.getElementById("whso_elective").selectedIndex = 0;
	document.getElementById("whso_elective_date").selectedIndex = 0;
	document.getElementById("whso_elective_date").disabled = true;
	if(document.getElementById("elective_check")) document.getElementById("elective_check").checked = false;
	optionItem = new Option("Select an elective first", 0, true, true);
	document.getElementById("whso_elective_date").options[0] = optionItem;
}

function add_hyphen() {
	var x = document.getElementById('card_number').value;
	if(x.length == 4 || x.length == 9 || x.length == 14) x += '-';
	document.getElementById('card_number').value = x;
}

function fill_month() {
	for(i = 1; i < 13; i++) {
		document.write("<option value='"+i+"'>"+i+"</option>");
	}
}

function fill_year() {
	var d = new Date();
	var y = d.getFullYear();
	for(i = y; i < (y+5); i++) {
		document.write("<option value='"+i+"'>"+i+"</option>");
	}
}

function check_course_url() {
	var addr = window.location.toString();
	if(addr.indexOf("?") > 0) {
		show_core(document.getElementById('course').options[document.getElementById('course').selectedIndex].value);
		get_date_list('course');
	}
}

function sendRequest() {
	getHidden();
	if(document.getElementById('errors').value == 'false') {	
		document.booking.action = 'confirm.php';
		document.booking.onsubmit = 'return true';
		$('pm_session2').disabled = false;
		$('pm_session3').disabled = false;
		document.booking.submit();
	} else {
		new Ajax.Request("includes/validate.php",{
			parameters: $('booking').serialize(true),
			onComplete: showResponse
		});
	}
}

function showResponse(req) {
	$('show').innerHTML= req.responseText;
	if(!req.responseText) { 
		document.getElementById('errors').value = 'false';
		sendRequest();
	}
}

function pmRequest(q) {
	if($('course').options[$('course').selectedIndex].value != 11) return;
	var dateList = $('course_date');
	var date2 = $('pm_session2');
	var date3 = $('pm_session3');
	dateList.disabled = false;
	var url = 'includes/pm_data_provider.php';
	if(q == 'c') {
		for(var count = dateList.options.length-1; count >-1; count--) {
			dateList.options[count] = null;
			date2.options[count] = null;
			date3.options[count] = null;
		}
	} else {
		for(var count = dateList.options.length-1; count >-1; count--) {
			date2.options[count] = null;
			date3.options[count] = null;
		}	
	}
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(transport) {
// alert(transport.responseText);
			var xmlDoc = transport.responseXML.documentElement;
			$("cohort").style.display = "table-row-group";
			var courseDates = xmlDoc.getElementsByTagName('dates');
			var sessionses = xmlDoc.getElementsByTagName('session');
			var idValue;
			var textValue;
			var optionItem;
			var youareell = window.location.toString();
			if(youareell.indexOf("&") > 0) {
				var sesh = youareell.split("&");
				var sidarr = sesh[1].split("=");
				var sid = sidarr[1];
			}
			if(dateList.options.length == 0) {
				for (var count = 0; count < sessionses.length; count++) {
					textValue = GetInnerText(courseDates[count]);
					idValue = courseDates[count].getAttribute("id");
					sessId = sessionses[count].getAttribute("id");
					var mod = sessId.substr(sessId.length-1,1);
					if(mod == 1) {
						if(sid && (idValue == sid)) optionItem = new Option(textValue, idValue, true, true);
						else optionItem = new Option(textValue, idValue, false, false);
						dateList.options[dateList.length] = optionItem;
					}
				}
			}
			findXMLdates(xmlDoc);
		}	
	});
	return;
}

function findXMLdates(xmlDoc) {
// $('pm_session2').disabled = false;
// $('pm_session3').disabled = false;
	var dateID = $('course_date').options[$('course_date').selectedIndex].value;
// alert(dateID);
	var searchdate = xmlDoc.selectSingleNode("/xml/course/session/dates[@id='"+dateID+"']");
	var sessionNodeID = searchdate.parentNode.getAttribute('id');
// alert(sessionNodeID);
	var secondmodID = parseInt(sessionNodeID) + 1;
	var thirdmodID = parseInt(sessionNodeID) + 2;
	var findsecondmod = xmlDoc.selectSingleNode("/xml/course/session[@id='"+secondmodID+"']");
	var secondmoddate = GetInnerText(findsecondmod.childNodes(0));
	var secondmoddateID = findsecondmod.childNodes(0).getAttribute('id');
	var findthirdmod = xmlDoc.selectSingleNode("/xml/course/session[@id='"+thirdmodID+"']");
	var thirdmoddate = GetInnerText(findthirdmod.childNodes(0));
	var thirdmoddateID = findthirdmod.childNodes(0).getAttribute('id');
	$('pm_session2').options[$('pm_session2').length] = new Option(secondmoddate, secondmoddateID, true, true);
	$('pm_session3').options[$('pm_session3').length] = new Option(thirdmoddate, thirdmoddateID, true, true);
	return;
}

function sel(f) {
	document.booking.elements[f].focus();
	document.booking.elements[f].select();
	return;
}

function getHidden() {
	var foo = new Array("course","course_date","whso_elective","whso_elective_date","session2","session2_date","payment_type","pm_session2","pm_session3");
	var bar = new Array();
	for(i = 0; i < foo.length; i++) { 
		bar.push(document.getElementById(foo[i]).options[document.getElementById(foo[i]).selectedIndex].text); 
	}
	for(j = 0; j < bar.length; j++) { 
		var wee = foo[j] + "_hidden"; 
		document.getElementById(wee).value = bar[j]; 
	}
	return;
}

function popup(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=800,height=600,left = 240,top = 100');");
}

function display_info(n) {
	var m;
	switch(n) {
		case "last_name": m = "LAST NAME\n\nYour last name is used in conjunction with your first name to identify you properly.\n\nWe use your last name in correspondence that we send to you.\n\nWe also use your name on any certificates you achieve from your course."; break;
		case "first_name": m = "FIRST NAME\n\nYour first name is used in conjunction with your last name to identify you properly.\n\nWe use your first and last names in correspondence that we send to you.\n\nWe also use your name on any certificates you achieve from your course."; break;
		case "dob":  m = "DATE OF BIRTH\n\nYour date of birth is used in conjunction with your name solely for identification purposes."; break;
		case "address": m = "ADDRESS\n\nYour address is used to forward correspondence to you.\n\nIn accordance with the Australian Safety & Learning Systems privacy policy,\nwe will never sell or give away your address to any third party, nor will we\nuse your address for anything other than official correspondence."; break;
		case "phone": m = "PHONE NUMBER\n\nWe use your telephone number to contact you to:\na) confirm course dates and requirements in the days before your course starts,\nb) advise you of any urgent changes (such as times or venues),\nc) contact you if you are missing to confirm your attendance."; break;
		case "email": m = "EMAIL ADDRESS\n\nYour email address will be used for regular day-to-day correspondence, including \nbooking confirmation.\n\nIn accordance with the Australian Safety & Learning Systems privacy policy,\nwe will never sell or give away your email address to any third party, nor will we\nuse your email address for anything other than official correspondence.\n\n** Please ensure your email address is correct as you will be sent an automated\nconfirmation email to the address you provide **"; break;
		case "employer": m = "EMPLOYER NAME\n\nEmployer details are used where an employer is to be invoiced, and for our own\nstatistical purposes.\n\nUnless your employer has arranged your course for you, we will never inform your\nemployer of you attendance at a course."; break;
		case "private": m = "PRIVATE/NO EMPLOYER\n\nIf you do not want to disclose who your employer is, or you are not currently\n employed, you may select this option to not enter an employer name."; break;
		case "stats": m = "STATISTICS\n\nWe use the information provided here to determine where our advertising is most effective.\n\nCompletion of this section is optional, but it assists us greatly if you choose to complete it."; break;
		}
	alert(m);
	return;
}

function open_employer() {
	var d = document.getElementById('employer');
	var e = document.getElementById('noemp');
	if(e.checked == true) {
		d.value = '';
		d.disabled = true;
	} else d.disabled = false;
}