
// JavaScript Document
function deleteEnrollment(id)
	{
		if (!confirm('Are you sure you want to delete this instructor course enrollment?'))
			return false;
	//	var enrollmentDetails = document.getElementById('EnrollmentDetails');
	//	enrollmentDetails.deleteRow(document.getElementById('enrollment' + id).rowIndex);
		window.open('delete_instructor_course_enrollment.php?id=' + id, 'deleteInstructor', 'width=640,height=480,scrollbars=yes');
	}

function validateForm()
	{
		messageObj = new DHTML_modalMessage();
		messageObj.setSize(400,150);
		errorString = '';
		// CC details all mandatory
		if (document.getElementById('cc_number').value.length < 13)
		{
			errorString += '<li>You must enter a valid credit card number<br />';
		}
		if (document.getElementById('cc_exp_yy').value.length != 2 || document.getElementById('cc_exp_mm').value.length != 2)
		{
			errorString += '<li>You must enter a valid credit card expiry date<br />';
		}
		if (document.getElementById('cc_cvv').value.length < 3)
		{
			errorString += '<li>You must enter a valid credit card CVV<br />';
		}
		if (errorString != '')
		{
			messageObj.setHtmlContent('The following data is missing from your order:<br /><ul>' + errorString + '</ul><br /><center><input type="button" value="Close" onclick="parent.messageObj.close();" /></center>');
			messageObj.display();
			return false;
		}
		return true;
	}


function showStatusDetails(status)
	{
		// If we're moving to cancelled invalidly, don't even show the details...
		statusSelect = $('status');
		statusValue = statusSelect.options[statusSelect.selectedIndex].value;
		origStatus = $('orig_status').value;
		if ((origStatus == 'prepare' || origStatus == 'dispatched' || origStatus == 'invoice_generated') && statusValue == 'cancelled')
		{
			alert('You cannot cancel an order which has progressed beyond the new stage');
			return false;
		}
		
		switch (status)
		{
			case 'new':
				document.getElementById('mail_tracking_code_div').style.display = 'none';
				document.getElementById('invoice_number_div').style.display = 'none';
				document.getElementById('cancellation_notes_div').style.display = 'none';
			case 'prepare':
				document.getElementById('mail_tracking_code_div').style.display = 'none';
				document.getElementById('invoice_number_div').style.display = 'none';
				document.getElementById('cancellation_notes_div').style.display = 'none';
				break;
			case 'dispatched':
				document.getElementById('mail_tracking_code_div').style.display = '';
				document.getElementById('invoice_number_div').style.display = 'none';
				document.getElementById('cancellation_notes_div').style.display = 'none';
				break;
			case 'invoice_generated':
				document.getElementById('mail_tracking_code_div').style.display = 'none';
				document.getElementById('invoice_number_div').style.display = '';
				document.getElementById('cancellation_notes_div').style.display = 'none';
				break;
			case 'cancelled':
				document.getElementById('mail_tracking_code_div').style.display = 'none';
                document.getElementById('invoice_number_div').style.display = 'none';
				document.getElementById('cancellation_notes_div').style.display = '';
		}
	}
	
	function validateUpdateStatus()
	{
		statusSelect = $('status');
		statusValue = statusSelect.options[statusSelect.selectedIndex].value;
		if (statusValue == 'dispatched' && $('mail_tracking_code').value == '')
		{
			alert('You must enter the mail tracking code to update the status to dispatched');
			return false;
		}
		if (statusValue == 'invoice_generated' && $('invoice_number').value == '')
		{
			alert('You must enter the invoice number to update the status to Completed');
			return false;
		}
		if (statusValue == 'cancelled' && $('transaction_code').value == '')
		{
			alert('You must enter the transaction code to update the status to Cancelled');
			return false;
		}
		// Only allow the status to move forward in the flow
		origStatus = $('orig_status').value;

		switch (origStatus)
		{
            case 'new':
                if (statusValue == 'dispatched')
                {
                    if (!confirm('This order has not yet been prepared. Do you want to proceed to dispatched?'))
                    {
                        return false;
                    }
                }
                if (statusValue == 'invoice_generated')
                {
                    if (!confirm('This order has not yet been prepared or dispatched. Do you want to proceed to completed?'))
                    {
                        return false;
                    }
                }
				break;
			case 'prepare':
				if (statusValue == 'new')
				{
					alert('You cannot move the status backwards from prepare to new');
					return false;
				}
                if (statusValue == 'invoice_generated')
                {
                    if (!confirm('This order has not been dispatched do you want to proceed to completed?'))
                    {
                        return false;
                    }
                }
				if (statusValue == 'cancelled')
				{
					alert('You cannot cancel an order which has progressed beyond the new stage');
					return false;
				}
				break;
			case 'dispatched':
				if (statusValue == 'prepare' || statusValue == 'new')
				{
					alert('You cannot move the status backwards from dispatched to ' + statusValue);
					return false;
				}
				if (statusValue == 'cancelled')
				{
					alert('You cannot cancel an order which has progressed beyond the new stage');
					return false;
				}
				break;
			case 'invoice_generated':
				if (statusValue == 'dispatched' || statusValue == 'prepare' || statusValue == 'new')
				{
					alert('You cannot move the status backwards from invoice generated to ' + statusValue);
					return false;
				}
				if (statusValue == 'cancelled')
				{
					alert('You cannot cancel an order which has progressed beyond the new stage');
					return false;
				}
				break;
			case 'cancelled':
				if (statusValue == 'invoice_generated' || statusValue == 'dispatched' || statusValue == 'prepare' || statusValue == 'new')
				{
					alert('You cannot move the status backwards from cancelled to ' + statusValue);
					return false;
				}
				break;
		}
		
		$('status_value').value = '1';
		return true;
	}

function dismissShippingPrices()
	{
		messageObj.close();
	}
	
	function showShippingRates(product)
	{
		// Get data from AJAX call
		new Ajax.Request('get_shipping_rates.php?product=' + product, 
		{
			method: 'get',
			onSuccess: function(transport) {
				contentString = transport.responseText;
				messageObj.setSize(400, 320);
				messageObj.setHtmlContent(contentString);
				messageObj.display();
			}
		});
	}
	
	function validateProductSelection()
	{
		// All quantities must be numeric
		qtys = $$('input[id$="[Qty]"]');
		for (i = 0; i < qtys.length; i++)
		{
			if (qtys[i].value != '' && qtys[i].value != parseInt(qtys[i].value))
			{
				alert('The quantity you entered "' + qtys[i].value + '" is not numeric. All quantities must be numeric.');
				return false;
			}
		}
		
		// Special case for cards, ID sensitive...
		cardsQty = document.getElementById('Products[18][Qty]').value;
		if (parseInt(cardsQty))
		{
			if (parseInt(cardsQty) % 50 != 0)
			{
				alert('You must order a multiple of 50 MHFA Action Plan Cards');
				return false;
			}
		}
		
		// Must provide size for polo shirts
		mhfaShirtSize = document.getElementById('Products[3][Options][Size]');
		mhfaShirtSizeValue = mhfaShirtSize.options[mhfaShirtSize.selectedIndex].value;
		if (mhfaShirtSizeValue == '' && document.getElementById('Products[3][Qty]').value != '')
		{
			alert('You must select a size for the product \'Polo Shirt MHFA White\'');
			return false;
		}

		// Must provide a quantity if a size is selected
		if (mhfaShirtSizeValue != '' && document.getElementById('Products[3][Qty]').value == '')
		{
			alert('You selected a size but not a quantity for the product \'Polo Shirt MHFA White\'');
			return false;
		}

		// AMHFA polo shirt may or may not be shown...
		mhfaShirtSize = document.getElementById('Products[37][Options][Size]');
		if (mhfaShirtSize != null)
		{
			mhfaShirtSizeValue = mhfaShirtSize.options[mhfaShirtSize.selectedIndex].value;
			if (mhfaShirtSizeValue == '' && document.getElementById('Products[37][Qty]').value != '')
			{
				alert('You must select a size for the product \'Polo Shirt AMHFA Black\'');
				return false;
			}
			// Must provide a quantity if a size is selected
			if (mhfaShirtSizeValue != '' && document.getElementById('Products[37][Qty]').value == '')
			{
				alert('You selected a size but not a quantity for the product \'Polo Shirt AMHFA Black\'');
				return false;
			}
		}
		return true;
	}
	
	

	function validateShipping()
	{
		if (document.getElementById('prev_shipping') != null)
		{
			return validateBilling();
		}
		// All fields + one phone required
		if (document.getElementById('name_org').value == '')
		{
			alert('You must enter a Name/Organisation in the Shipping Details section');
			return false;
		}
		if (document.getElementById('delivery_address').value == '')
		{
			alert('You must enter a Delivery Address in the Shipping Details section');
			return false;
		}
		if (document.getElementById('postcode').value == '')
		{
			alert('You must enter a Postal Code in the Shipping Details section');
			return false;
		}
		if (document.getElementById('country').selectedIndex == 0)
		{
			alert('You must select a Country in the Shipping Details section');
			return false;
		}
		if (document.getElementById('telephone').value == '' && document.getElementById('mobile').value == '')
		{
			alert('You must enter a Telephone or Mobile number in the Shipping Details section');
			return false;
		}
		if (document.getElementById('email').value == '')
		{
			alert('You must enter an Email in the Shipping Details section');
			return false;
		}
		// Call validateBilling if shipping is all OK...
		return validateBilling();
	}
	
	function validateBilling()
	{
		if (document.getElementById('prev_billing') != null)
		{
			return true;
		}
		// All fields + one phone required
		if (document.getElementById('billing_name_org').value == '')
		{
			alert('You must enter a Name/Organisation in the Billing Details section');
			return false;
		}
		if (document.getElementById('billing_address').value == '')
		{
			alert('You must enter a Billing Address in the Shipping Details section');
			return false;
		}
		if (document.getElementById('billing_postcode').value == '')
		{
			alert('You must enter a Postal Code in the Billing Details section');
			return false;
		}
		if (document.getElementById('billing_country').selectedIndex == 0)
		{
			alert('You must select a Country in the Shipping Details section');
			return false;
		}
		if (document.getElementById('billing_telephone').value == '' && document.getElementById('billing_mobile').value == '')
		{
			alert('You must enter a Telephone or Mobile number in the Billing Details section');
			return false;
		}
		if (document.getElementById('billing_email').value == '')
		{
			alert('You must enter an Email in the Billing Details section');
			return false;
		}
		return true;
	}
	
	function pageUnderConstruction() {
		alert('This page is still under construction and is thus currently unavailable. Please try again later.');
		return false;
		}
		
	function enroll(instructorCourseId, type, location, date)
	{
		var doc = window.opener.document;	
		doc.getElementById('CourseEnrollments').value += instructorCourseId;
		doc.getElementById('CourseEnrollments').value += '-';

		var enrollmentDetails = doc.getElementById('EnrollmentDetails');
		var row = enrollmentDetails.insertRow(enrollmentDetails.rows.length);
		var typeCell = row.insertCell(0);
		var textNode = doc.createTextNode(type);
		typeCell.appendChild(textNode);
		var locationCell = row.insertCell(1);
		var textNode = doc.createTextNode(location);
		locationCell.appendChild(textNode);
		var dateCell = row.insertCell(2);
		var textNode = doc.createTextNode(date);
		dateCell.appendChild(textNode);
		window.close();
	}
	
	function selectCourse(id, title)
	{
		//alert(title);
		window.opener.document.getElementById('course').value = title;
		window.opener.document.getElementById('courseId').value = id;
		window.close();
	}
	
	
var xmlhttp;


function instructors_by_state(state, label) {
	//alert(state);
	/*
	if (str.length==0)
	  {
	  document.getElementById("txtHint").innerHTML="";
	  return;
	  }
	  */
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){
	  alert ("Your browser does not support XMLHTTP!");
	  return;
	 }
	
	var url="get_instructor_by_state.php";
	url=url+"?state="+state;
	url=url+"&sid="+Math.random();
	//alert (url);
	if (label == 'CoFacilitator') {xmlhttp.onreadystatechange=stateChanged1;}
	if (label == 'Instructor') {xmlhttp.onreadystatechange=stateChanged2;}
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}


function trainers_by_state(state, label) {
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){
	  alert ("Your browser does not support XMLHTTP!");
	  return;
	 }
	
	var url="get_trainer_by_state.php";
	url=url+"?state="+state;
	url=url+"&sid="+Math.random();
	//alert (url);
	if (label == 'CoTrainer') {xmlhttp.onreadystatechange=stateChanged3;}
	if (label == 'Trainer') {xmlhttp.onreadystatechange=stateChanged4;}
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function stateChanged1() {if (xmlhttp.readyState==4) { document.getElementById('CoFacilitator').innerHTML=xmlhttp.responseText;}}	
function stateChanged2() {if (xmlhttp.readyState==4){document.getElementById('Instructor').innerHTML=xmlhttp.responseText;}}
function stateChanged3() {if (xmlhttp.readyState==4){document.getElementById('CoTrainer').innerHTML=xmlhttp.responseText;}}
function stateChanged4() {if (xmlhttp.readyState==4){document.getElementById('Trainer').innerHTML=xmlhttp.responseText;}}
	
function GetXmlHttpObject()
	{
	if (window.XMLHttpRequest)
	  {
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  return new XMLHttpRequest();
	  }
	if (window.ActiveXObject)
	  {
	  // code for IE6, IE5
	  return new ActiveXObject("Microsoft.XMLHTTP");
	  }
	return null;
}
	
function deleteParticipant(row_id, participant_id) {
	var xmlhttp;
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){
	  alert ("Your browser does not support XMLHTTP!");
	  return;
	}
	 
	var url="handle_particpants_ajax.php?action=delete_participant";
	url=url+"&participantId="+participant_id;
	url=url+"&sid="+Math.random();
	
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) { 
			//alert(xmlhttp.responseText);
			var tr_to_be_removed = 'participant_row_'+row_id;
			$("#"+tr_to_be_removed).remove();
		}
	}
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function saveParticipant(row_id, course_id, add_or_update) {
	
	var xmlhttp;
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){
	  alert ("Your browser does not support XMLHTTP!");
	  return;
	}
	
	var participant_id = $("#ParticipantId_"+row_id).val();
	var first_name = $("#FirstName_"+row_id).val();
	var last_name = $("#LastName_"+row_id).val();
	var email = $("#Email_"+row_id).val();
	var phone = $("#Phone_"+row_id).val();
	var passed;
	if($("#Passed_"+row_id).attr("checked")==true) {passed = 1;} else {passed = 0;}
	//alert(passed);
	
	
	var url="handle_particpants_ajax.php?action=save_participant";
	url=url+"&ParticipantId="+participant_id;
	url=url+"&FirstName="+first_name;
	url=url+"&LastName="+last_name;
	url=url+"&Email="+email;
	url=url+"&Phone="+phone;
	url=url+"&Passed="+passed;
	url=url+"&CourseId="+course_id;
	url=url+"&rowId="+row_id;
	url=url+"&add_or_update="+add_or_update;
	url=url+"&sid="+Math.random();
	
	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState==4 && xmlhttp.status==200) { 
			$('#participant_row_'+row_id).html(xmlhttp.responseText);
			$("#participant_row_"+row_id).css("background-color", "");
		}
	}
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function emailParticipant(row_id, participant_id) {
	//alert('this type = '+type);
	var xmlhttp;
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){
	  alert ("Your browser does not support XMLHTTP!");
	  return;
	}
	
	var url="handle_particpants_ajax.php?action=email_participant";
	url=url+"&participantId="+participant_id;
	url=url+"&sid="+Math.random();
	
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) { 
			//alert(xmlhttp.responseText);
			$("#EmailParticipant_"+row_id).val("send another email");
			$("#EmailSent_"+row_id).attr("checked", "checked");
		}
	}
	
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}


function emailInstructorParticipant(row_id, participant_id) {
	//alert('send email click');
	var xmlhttp;
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){
	  alert ("Your browser does not support XMLHTTP!");
	  return;
	}
	
	var url="handle_instructor_particpants_ajax.php?action=email_participant";
	url=url+"&participantId="+participant_id;
	url=url+"&sid="+Math.random();
	
	//alert(url);
	
	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState==4 && xmlhttp.status==200) { 
			alert(xmlhttp.responseText);
			$("#EmailParticipant_"+row_id).val("send another email");
			$("#EmailSent_"+row_id).attr("checked", "checked");
		}
	}
	
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function updateInstructorFailed(failed, participant_id) {
	//alert('send email click');
	var xmlhttp;
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){
	  alert ("Your browser does not support XMLHTTP!");
	  return;
	}
	
	//alert(failed);
	
	var url="handle_instructor_particpants_ajax.php?action=update_pass_fail";
	url=url+"&participantId="+participant_id;
	url=url+"&failed="+failed;
	url=url+"&sid="+Math.random();
	
	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState==4 && xmlhttp.status==200) { 
			//alert(xmlhttp.responseText);
		}
	}
	
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}


function createParticipantInputRow(row_id, course_id, add_or_update, participant_id, first_name, last_name, email, phone, passed) {
	
	var xmlhttp;
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){
	  alert ("Your browser does not support XMLHTTP!");
	  return;
	}
	$("#participant_row_"+row_id).css("background-color", "#F5F3FB");
	var url="handle_particpants_ajax.php?action=create_participant_input_row";
	url=url+"&ParticipantId="+participant_id;
	url=url+"&FirstName="+first_name;
	url=url+"&LastName="+last_name;
	url=url+"&Email="+email;
	url=url+"&Phone="+phone;
	url=url+"&Passed="+passed;
	url=url+"&CourseId="+course_id;
	url=url+"&rowId="+row_id;
	url=url+"&add_or_update="+add_or_update;
	url=url+"&sid="+Math.random();
	
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) { 			
			if(add_or_update=='add') {$('<tr id="participant_row_'+row_id+'" ></tr>').appendTo("#course_participants_table").html(xmlhttp.responseText);} 
			else if(add_or_update=='update') {$('#participant_row_'+row_id).html(xmlhttp.responseText);}
			else {alert('must specify add_or_update');}
		}
	}
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function incrementRowCountAndAddRow(courseId) {
	var count = $("#row_counter").val();
	count++;
	$("#row_counter").val(count);
	createParticipantInputRow(count, courseId, 'add', '', '','','','', '');
}
function removeParticipantRow(row_id) {
	var tr_to_be_removed = 'participant_row_'+row_id;
	$("#"+tr_to_be_removed).remove();
}

function selectAllParticipantRows() {
	var count = $("#row_counter").val();
	var i;
	for(i=0; i<count; i++) {
		$("#ParticipantId_"+i).attr("checked", "checked");
		$("#participant_row_"+i).css("background-color", "#F5F3FB");
	}
	
}

function saveAllParticipantRows(course_id) {
	var count = $("#row_counter").val();
	var i;
	var add_or_update;
	for(i=1; i<=count; i++) {
		//alert('row='+i+'add_or_update_'+$("#add_or_update_"+i).val());
		add_or_update = $("#add_or_update_"+i).val();
		if(add_or_update=='add') {//alert('yes add'); 
		add_or_update='add_many';}
		//alert(add_or_update);
		if(typeof(add_or_update)!="undefined") {saveParticipant(i, course_id, add_or_update);}
	}
	//incrementRowCountAndAddRow(course_id);
}

function deselectAllParticipantRows() {
	var count = $("#row_counter").val();
	var i;
	for(i=0; i<count; i++) {
		$("#ParticipantId_"+i).attr("checked", false);
		$("#participant_row_"+i).css("background-color", "");
	}
	
}
function deleteSelectedParticipantRows() {
	var count = $("#row_counter").val();
	var i;
	for(i=0; i<count; i++) {
		if($("#ParticipantId_"+i).attr("checked")) {
			deleteParticipant(i, $("#ParticipantId_"+i).val());
		}
	}
	
}
function emailSelectedParticipantRows() {
	var count = $("#row_counter").val();
	var i;
	for(i=0; i<count; i++) {
		if($("#ParticipantId_"+i).attr("checked")) {
			emailParticipant(i, $("#ParticipantId_"+i).val());
		}
	}
	
}
	
	function cp_bxo(){ if(!ebxstate){ $('#ebox').fadeIn(750); if(!olstate){ $('#ol').fadeIn(750); olstate=true; } if(ldstate){ cp_ldc(false); } ebxstate=true; } }
				function cp_bxc(){ if(ebxstate){ $('#ebox').fadeOut(750); if(olstate){ $('#ol').fadeOut(750); olstate=false; } if(ldstate){ cp_ldc(true); } ebxstate=false; } }
				function cp_hbxo(){ if(!hbxstate){ $('#help').fadeIn(750); if(!olstate){ $('#ol').fadeIn(750); olstate=true; } if(ldstate){ cp_ldc(false); } hbxstate=true; } }
				function cp_hbxc(o){ if(hbxstate){ $('#help').fadeOut(750); if(olstate&&!o){ $('#ol').fadeOut(750); olstate=false; } if(ldstate){ cp_ldc(true); } hbxstate=false; } }
				function cp_ldo(){ if(!ldstate){ $('#ld').fadeIn(750); if(!olstate){ $('#ol').fadeIn(750); olstate=true; } ldstate=true; } }
				function cp_ldc(o){ if(ldstate){ $('#ld').fadeOut(750); if(olstate&&o){ $('#ol').fadeOut(750); olstate=false; } ldstate=false; } }
				function __helper(id,o){ cp_ldo(); $('#help').html("<span id='ctrl'><a href='javascript:void(0);' onclick='cp_hbxc("+o+");'>Close [X]</a></span><br /><br />"+infoct(id)); cp_hbxo(); }
	var ebxstate = false;
	var hbxstate = false;
	var ldstate = false;
	var olstate = false;
	
	

function updateURLField(id) {
	//alert(id);
	var value = $('#pageType_'+id).attr('value');
	//(value);
	if(value=='CMS') {$('#nav_url_'+id).html($('#hidden_stuff1').html());}
	if(value=='Custom') {$('#nav_url_'+id).html($('#hidden_stuff2').html());}
}

