// JavaScript Document - AJAX
var xmlHttp;
var xmlDoc;
var thisLocation = location.protocol + "//" + location.hostname + "/AJAX";

function createXMLHttpRequest(){
	if (window.ActiveXObject){
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		xmlHttp = new XMLHttpRequest();
	}
	return;
}

function checkDates(startDateID,endDateID){
	createXMLHttpRequest();
	start = document.getElementById(startDateID).value;
	end = document.getElementById(endDateID).value;
	if (end == "" || !end){
		document.getElementById(endDateID).value = start;
		end = start;
	}
	url = thisLocation + "/dateCompare.cfm.cfm?startDate=" + start + "&endDate=" + end;
	xmlHttp.onreadystatechange = returnCheckDates;
	xmlHttp.open("GET",url, true);
	xmlHttp.send(null);
}

function returnCheckDates(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			result = xmlHttp.responseText;
			if (result == "false"){
				alert("The end date you entered is AFTER the start date.\r\n\r\nPlease enter or select an end date that is \r\neither the same as or after the start date.");
			}
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
}
function checkCourseName(course){
	if (!course || course == '') return;
	course_id = document.getElementById("course_id").value;
	if (isNaN(course_id) || course_id == ""){
		course_id = 0;
	}
	createXMLHttpRequest();
	url = thisLocation + "/checkCourseName.cfm?crs_name=" + course + "&course_id=" + course_id;
	if (!isNaN(course_id)) url += "&course_id=" + course_id;
	xmlHttp.onreadystatechange = courseNameResult;
	xmlHttp.open("GET",url, true);
	xmlHttp.send(null);
}
function courseNameResult(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			returnVal = xmlHttp.responseText;
			returnVal = returnVal.replace(/\D/g,"");
			if (returnVal > 0){
				document.getElementById("crs_name").value = '';
				alert("This name is already in use by another course.\r\n\r\nPlease enter a different course name.");
			}
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
	return;
}
function checkPrefix(prefix){
	if (!prefix || prefix == '') return;
	course_id = document.getElementById("course_id").value;
	createXMLHttpRequest();
	url = thisLocation + "/checkPrefix.cfm?prefix=" + prefix;
	if (!isNaN(course_id)) url += "&course_id=" + course_id;
	xmlHttp.onreadystatechange = prefixResult;
	xmlHttp.open("GET",url, true);
	xmlHttp.send(null);
}
function prefixResult(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			returnVal = xmlHttp.responseText;
			returnVal = returnVal.replace(/\D/g,"");
			if (returnVal > 0){
				document.getElementById("crs_prefix").value = '';
				alert("This prefix is already in use by another course.\r\n\r\nPlease enter a different prefix.");
			}
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
	return;
}
function getCourseInfo(course_id){
	if (isNaN(course_id)) return;
	createXMLHttpRequest();
	url = thisLocation + "/getCourseInfo.cfm?course_id=" + course_id
	xmlHttp.onreadystatechange = courseInfo_Result;
	xmlHttp.open("GET",url, true);
	xmlHttp.send(null);
}
function courseInfo_Result(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			returnVal = xmlHttp.responseText;
			val_parts = returnVal.split("^");
			if (!isNaN(val_parts[1]) && val_parts[0] != ''){
				useData = window.confirm("Do you wish to use the data specific to the course you've selected?\r\n\r\nClick 'Ok' to use this data, or 'Cancel' to cancel.","");
				if (useData){
					document.getElementById("cls_course_fee").value = val_parts[1];
					document.getElementById("cls_code").value = val_parts[0];
					setSelectMenu("cls_confirm_temp_id",val_parts[2]);
					setSelectMenu("cls_cert_temp_id",val_parts[3]);
					setSelectMenu("cls_fail1_temp_id",val_parts[4]);
					setSelectMenu("cls_fail2_temp_id",val_parts[5]);
					setSelectMenu("cls_pass_temp_id",val_parts[6]);
					setSelectMenu("cls_cpr_temp_id",val_parts[7]);
					document.getElementById("cls_online_url").value = val_parts[8];
					document.getElementById("cls_online_username").value = val_parts[9];
					document.getElementById("cls_online_password").value = val_parts[10];
				}
			}			
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
	return;
}
function setSelectMenu(menu,val){
	menuObj = document.getElementById(menu);
	for (var i=0; i < menuObj.options.length; i++){
		if (menuObj.options[i].value == val){
			menuObj.options[i].selected = true;
			return;
		}
	}
	return;
}
function showHideObj(obj,vis){
	if (!obj) return;
	thisObj = document.getElementById(obj);
	if (thisObj.style.visibility == 'hidden'){
		thisObj.style.visibility = 'visible';
		thisObj.style.display = 'inline';
	} else {
		thisObj.style.visibility = 'hidden';
		thisObj.style.display = 'none';
	}
	return;
}
function showHideObjVis(obj,vis){
	if (!obj) return;
	thisObj = document.getElementById(obj);
	if (vis == 'show'){
		thisObj.style.visibility = 'visible';
		thisObj.style.display = 'inline';
	} else {
		thisObj.style.visibility = 'hidden';
		thisObj.style.display = 'none';
	}
	return;
}
function setDaysBeforeStart(due_date){
	if (!due_date) return;
	start_date = document.getElementById("cls_start_date").value;
	createXMLHttpRequest();
	url = thisLocation + "/getDaysBeforeStart.cfm?cls_start_date=" + start_date + "&due_date=" + due_date;
	xmlHttp.onreadystatechange = daysBeforeStart_Result;
	xmlHttp.open("GET",url, true);
	xmlHttp.send(null);
}
function daysBeforeStart_Result(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			returnVal = xmlHttp.responseText;
			returnVal = returnVal.replace(/\D/g,"");
			if (!isNaN(returnVal)){
				document.getElementById("tsk_days_before").value = returnVal;
			}			
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
	return;
}
function checkGrade(grd_id){
	grdObj = document.getElementById(grd_id);
	grdVal = grdObj.value;
	if ((isNaN(grdVal) && grdVal != "P" && grdVal != "F") && (!isNaN(grdVal) && grdVal < 0 || grdVal > 100)){
		alert("You need to enter either a 'P' or an 'F', or a numeric value between 0 and 100.");
		if(gradeObj.id.indexOf("prac") > -1){
			gradeObj.value = "P/F";
		} else {
			grdObj.value = "0";
		}
	}
	return;
}
function setDueDate(days_before){
	if (isNaN(days_before)){
		alert("You need to enter a numeric value in the 'Days Before Start' field.");
		return;
	}
	start_date = document.getElementById("cls_start_date").value;
	createXMLHttpRequest();
	url = thisLocation + "/getDueDate.cfm?cls_start_date=" + start_date + "&days_before=" + days_before;
	xmlHttp.onreadystatechange = dueDate_Result;
	xmlHttp.open("GET",url, true);
	xmlHttp.send(null);
}
function dueDate_Result(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			returnVal = xmlHttp.responseText;
			returnVal = returnVal.replace(/\s/g,"");
			if (returnVal || returnVal != ''){
				document.getElementById("tsk_due_date").value = returnVal;
			}			
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
	return;
}
function setSalutation(){
	salutation = "Dear ";
	salutation += document.getElementById("cst_prefix").options[document.getElementById("cst_prefix").selectedIndex].value + " ";
	salutation += document.getElementById("cst_LastName").value;
	document.getElementById("cst_salutation").value = salutation;
	return;
}
function saveSOLCode(){
	var code = document.getElementById("sol_code").value;
	var name = document.getElementById("sol_name").value;
	createXMLHttpRequest();
	url = thisLocation + "/saveSOLCode.cfm?code=" + code + "&name=" + name;
	xmlHttp.onreadystatechange = saveSOL_Result;
	xmlHttp.open("GET",url, true);
	xmlHttp.send(null);
}
function saveSOL_Result(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			returnVal = xmlHttp.responseText;
			if (returnVal || returnVal != ''){
				setCodeList('gift_campaign_id',returnVal);
				showHideObj('solForm');
			}			
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
	return;
}
function saveGLCode(){
	var code = document.getElementById("gl_code").value;
	var name = document.getElementById("gl_name").value;
	createXMLHttpRequest();
	url = thisLocation + "/saveGLCode.cfm?code=" + code + "&name=" + name;
	xmlHttp.onreadystatechange = saveGL_Result;
	xmlHttp.open("GET",url, true);
	xmlHttp.send(null);
}
function saveGL_Result(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			returnVal = xmlHttp.responseText;
			if (returnVal || returnVal != ''){
				setCodeList('gift_glcode_id',returnVal);
				showHideObj('glForm');
			}			
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
	return;
}
function saveRefCode(){
	var code = document.getElementById("type_code").value;
	var name = document.getElementById("type_name").value;
	createXMLHttpRequest();
	url = thisLocation + "/saveRefCode.cfm?code=" + code + "&name=" + name;
	xmlHttp.onreadystatechange = saveRef_Result;
	xmlHttp.open("GET",url, true);
	xmlHttp.send(null);
}
function saveRef_Result(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			returnVal = xmlHttp.responseText;
			if (returnVal || returnVal != ''){
				setCodeList('gift_type_id',returnVal);
				showHideObj('giftListForm');
			}			
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
	return;
}
function savePCCode(){
	var code = document.getElementById("pc_code").value;
	var name = document.getElementById("pc_description").value;
	createXMLHttpRequest();
	url = thisLocation + "/savePCCode.cfm?code=" + code + "&name=" + name;
	xmlHttp.onreadystatechange = savePCCode_Result;
	xmlHttp.open("GET",url, true);
	xmlHttp.send(null);
}
function savePCCode_Result(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			returnVal = xmlHttp.responseText;
			if (returnVal == 'dupe'){
				alert("The code or description you entered is already in the list.");
				showForm('pcForm','pcFormAnchor');
				return;
			} else if (returnVal || returnVal != ''){
				setCodeList('gift_project_code_id',returnVal);
				showForm('pcForm','pcFormAnchor');
			} else {
			}
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
	return;
}
function setCodeList(code_id, code_list){
	codeObj = document.getElementById(code_id);
	codeObj.options.length = 1;
	code_array = new Array();
	if (code_list != ''){
		code_array = code_list.split("~");
		for (var i=0; i < code_array.length; i++){
			thisCodeItem = code_array[i];
			tci_array = thisCodeItem.split("^");
			if (tci_array.length == 2){
				codeObj.options[codeObj.options.length] = new Option(tci_array[1],tci_array[0]);
			}
		}
	}
}
function checkForDuplicate(field,value){
	if (!field || !value) return;
	createXMLHttpRequest();
	url = thisLocation + "/checkForDuplicate.cfm?field=" + field + "&value=" + value;
	xmlHttp.onreadystatechange = duplicate_Result;
	xmlHttp.open("GET",url, true);
	xmlHttp.send(null);
}
function checkForDuplicateProfile(field,value){
	if (!field || !value) return;
	createXMLHttpRequest();
	url = thisLocation + "/checkForDuplicateProfile.cfm?field=" + field + "&value=" + value;
	xmlHttp.onreadystatechange = duplicate_Result;
	xmlHttp.open("GET",url, true);
	xmlHttp.send(null);
}
function duplicate_Result(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			returnVal = xmlHttp.responseText;
			if (returnVal != ''){
				fields = returnVal.split("^");
				alert(fields[0]);
				if (fields.length == 1){
					document.getElementById("profileForm").style.visibility = "hidden";
					document.getElementById("profileForm").style.display = "none";
				}
			}			
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
	return;
}
function findStudents(keyval,keyfld){
	if (!keyval){
		keyword = document.getElementById("keyword").value;
	} else {
		keyword = keyval;
	}
	if (!keyfld){
		field = document.getElementById("field").options[document.getElementById("field").selectedIndex].value;
	} else {
		field = keyfld;
	}	
	createXMLHttpRequest();
	url = thisLocation + "/findStudents.cfm?field=" + field + "&keyword=" + keyword;
	xmlHttp.onreadystatechange = findStudents_Result;
	xmlHttp.open("GET",url, true);
	xmlHttp.send(null);
}
function findStudents_Result(){
	studentObj = document.getElementById("cst_ID");
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			returnVal = xmlHttp.responseText;
			ns = document.getElementById("no_student");
			studentObj.options.length = 0;
			if (returnVal != ''){
				ns.style.visibility = 'hidden';
				ns.style.display = 'none';
				records = returnVal.split("~");
				for (var i=0; i < records.length; i++){
					thisStudent = records[i].split("^");
					if (thisStudent.length == 2){
						studentObj.options[studentObj.options.length] = new Option(thisStudent[1],thisStudent[0]);
					}
				}
			} else {
				ns.style.visibility = 'visible';
				ns.style.display = 'inline';
				studentObj.options[studentObj.options.length] = new Option("No students found","");
			}			
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
	return;
}
function getClassCost(class_id){
	createXMLHttpRequest();
	url = thisLocation + "/getClassCost.cfm?class_id=" + class_id;
	xmlHttp.onreadystatechange = getClassCost_Result;
	xmlHttp.open("GET",url, true);
	xmlHttp.send(null);
}
function getClassCost_Result(){
	costObj = document.getElementById("class_payment");
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			returnVal = xmlHttp.responseText;
			if (!isNaN(returnVal)){
				costObj.value = returnVal;
			}			
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
	return;
}
function getEnrollment(class_id){
	createXMLHttpRequest();
	url = thisLocation + "/getEnrollment.cfm?class_id=" + class_id;
	xmlHttp.onreadystatechange = getEnrollment_Result;
	xmlHttp.open("GET",url, true);
	xmlHttp.send(null);
}
function getEnrollment_Result(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			returnVal = xmlHttp.responseText;
			if (returnVal != ""){
				class_values = returnVal.split("^");
				objID = "enr" + class_values[0];
				enrWinObj = document.getElementById(objID);
				enrWinObj.innerHTML = "Class Code: " + class_values[1] + "<br />Max: " + class_values[2] + ", Current: " + class_values[3];
				showEnrollment(objID,true);
			}			
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
	return;
}
function openHelp(file){
	window.open(file,"help","width=750,height=600,scrollbars,resizable");
	return;
}
function checkCNote(){
	cnote = document.getElementById("CNote").value;
	studentIndex = document.getElementById("cst_ID").selectedIndex;
	classIndex = document.getElementById("class_id").selectedIndex;
	class_id = document.getElementById("class_id").options[classIndex].value;
	customer_id = document.getElementById("cst_ID").options[studentIndex].value;
	createXMLHttpRequest();
	url = thisLocation + "/getCNote.cfm?class_id=" + class_id + "&cst_ID=" + customer_id + "&cnote_code=" + cnote;
	xmlHttp.onreadystatechange = getCNote_Result;
	xmlHttp.open("GET",url, true);
	xmlHttp.send(null);
	return;
}
function getCNote_Result(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			returnVal = xmlHttp.responseText;
			if (returnVal != ""){
				paymentObj = document.getElementById("class_payment");
				payment = paymentObj.value;
				payment = parseFloat(payment);
				cnote_array = returnVal.split("^");
				if (cnote_array[0] == "true"){
					cnote_value = parseFloat(cnote_array[2]);
					payment = payment - cnote_value;
				}
				paymentObj.value = payment;
			}			
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
	return;
}
function setNoShow(regid,val){
	createXMLHttpRequest();
	url = thisLocation + "/setNoShow.cfm?registration_id=" + regid + "&reg_no_show=" + val;
	xmlHttp.onreadystatechange = setNoShow_Result;
	xmlHttp.open("GET",url, true);
	xmlHttp.send(null);	
}
function setNoShow_Result(){	
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			returnVal = xmlHttp.responseText;
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
	return;
}
function searchStudents(type){
	if (!type) return;
	createXMLHttpRequest();
	kwObj = document.getElementById(type + "_keyword");
	fldObj = document.getElementById(type + "_field");
	kw = kwObj.value;
	kw = kw.replace(/[,\s]/g,",");
	fld = fldObj.options[fldObj.selectedIndex].value;
	url = thisLocation + "/searchStudents.cfm?type=" + type + "&keyword=" + kw + "&field=" + fld;
	xmlHttp.onreadystatechange = searchStudents_Result;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function searchStudents_Result(){	
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			returnVal = xmlHttp.responseText;
			if (returnVal.length > 4){
				rows = returnVal.split("~");
				type = rows[0];
				selectObj = document.getElementById(type + "_cst_ID");
				selectObj.options.length = 1;
				if (rows.length > 0){
					for (var i=1; i < rows.length; i++){
						thisRow = rows[i].split("^");
						selectObj.options[selectObj.options.length] = new Option(thisRow[1],thisRow[0]);
					}
				}
			} else {
				alert("No students were found matching your criteria");
			}
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
	return;
}
function getStudentInfo(type,student_id){
	if (!type || !student_id) return;
	createXMLHttpRequest();
	url = thisLocation + "/getStudentInfo.cfm?type=" + type + "&cst_ID=" + student_id;
	xmlHttp.onreadystatechange = getStudentInfo_Result;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function getStudentInfo_Result(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			returnVal = xmlHttp.responseText;
			fields = returnVal.split("~");
			profileObj = document.getElementById(fields[0] + "_profile");
			profileObj.innerHTML = fields[1];
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
	return;
}
function dateDiff(dateID){
	createXMLHttpRequest();
	from = document.getElementById(dateID).value;
	url = thisLocation + "/dateDiff.cfm?from=" + from;
	xmlHttp.onreadystatechange = returnDateDiff;
	xmlHttp.open("GET",url, true);
	xmlHttp.send(null);
}

function returnDateDiff(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			result = xmlHttp.responseText;
			if (result == "false"){
				alert("The date you entered is invalid.\r\n\r\nPlease enter or select a valid date.");
			} else {
				document.getElementById("resultVal").innerHTML = result;
			}
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
}
