//	start of initialisation of key variables
//	end of initialisation

//	standard functions for site

function getQueryVariable (variable) {
	var queryString = window.location.search.substring(1);
	var queryParts = queryString.split("&");
	for (var i = 0; i < queryParts.length; i++) {
		var keyPair = queryParts[i].split("=");
		if (keyPair[0] == variable) {
			return keyPair[1];
		}
	}
	return "";
}

function openSpec (file) {
	var win_width = 360, win_height = 300;	
	var xPosition = 75;
	var yPosition = 50;
	if (document.layers) {
		temp = "screenx=" + xPosition + ",screeny=" + yPosition + ",height=" + win_height + ",width=" + win_width + ",resizeable=yes";
	}
	else {
		temp = "left=" + xPosition + ",top=" + yPosition + ",height=" + win_height + ",width=" + win_width + ",resizeable=yes";
	}
	new_win = window.open(file, 'NOTES', temp);
	new_win.focus();
}

function checkFormFields (mandatoryFields, confirm) {
	var d = document.forms[0];
	var msg = '';
	for (var i = 0; i < mandatoryFields.length; i++) {
		var formObject = d.elements[mandatoryFields[i][0]];
		if (mandatoryFields[i][0] == "bc_number") {
			if (formObject.value == "") {
				var affiliation = document.forms[0].bc[0].checked;
				if (affiliation) {
					msg += mandatoryFields[i][1] + ' not supplied\n';
				}
			}
		}
		else if ((formObject.value == '') || (formObject.value == "None") || (formObject.value == "00/00/0000") || (formObject.value == "Not Given")) {
			msg += mandatoryFields[i][1] + ' not supplied\n';
		}
		else if (formObject.type == "checkbox") {
			if (formObject.checked == 0) {
				msg += mandatoryFields[i][1] + ' not checked\n';
			}
		}
		else if ((formObject.type != "select-one") && (formObject.type != "select-multiple") && (formObject.type != "text") && (formObject.type != "textarea") && (formObject.type != "password") && (formObject.type != "hidden")) {
			var buttonsChecked = 0;
			for (var j = 0; j < formObject.length; j++) {
				if (formObject[j].checked) {
					buttonsChecked++;
				}
			}
			if (buttonsChecked == 0) {
				msg += mandatoryFields[i][1] + ' not selected\n';
			}
		}
		if ((mandatoryFields[i][0].indexOf('email') > -1) || (mandatoryFields[i][0].indexOf('Email') > -1)) {
			if (!(validEmail (formObject.value))) {
				msg += mandatoryFields[i][1] + ' appears to be invalid address\n';
			}
		}
		if (mandatoryFields[i][0] == "p_dob") {
			if (!(checkYear (formObject.value))) {
				msg += mandatoryFields[i][1] + ' suggests your are under 18 or over 100 years old (not allowed)\n';
			}
		}
	}
	if (msg) {
		alert (msg + '\n...please correct and try again\n');
		return false;
	}
	else if (confirm.indexOf('visit') > -1) {
		if (confirm == "visit_weekend") {
			if ((d.elements['tany'].checked == 0) && (d.elements['tnear'].checked == 0)) {
				msg += "Under the 'UK locations' section you must indicate that either you are prepared to travel anywhere, or that you prefer to visit near your place of study, or both\n";
				alert (msg + '\n...please correct and try again\n');
				return false;
			}
		}	
		var datespicked = 0;
		for (i = 0; i < d.vdate.length; i++) {
			if (d.vdate[i].checked) {
				datespicked ++;
			}
        }
		if (datespicked < 3) {
			msg += "You must supply at least three optional dates for your visit under the 'Visit dates' section\n";
			alert (msg + '\n...please correct and try again\n');
			return false;		
		}
		confirm = 1;
	}
	
	if (confirm == 0) {
		return true;
	}
	else {
		return confirmFormSubmit ();
	}
}

function validEmail (address) {
	var at = address.indexOf('@');
	var dot = address.indexOf('.');
	if (at == -1) return false;
	if (at != address.lastIndexOf('@')) return false;
	if (dot == -1) return false;
	if (dot == (address.length - 1)) return false;
	if (address.lastIndexOf('.') == (address.length - 1)) return false;
	return true;
}

function checkYear (yearStr) {
	var d = new Date();
	var y = d.getFullYear();
	var tooYoung = y - 18;
	var tooOld = y - 100;
	var fullDate = new Array();
	fullDate = yearStr.split ('/');
	var thisYear = parseInt (fullDate[2], 10);
	if (thisYear < tooOld) return false;
	if (thisYear > tooYoung) return false;
	return true;
}

function checkTextLength (object, characters) {
	if (object.value.length > characters) {
		var msg = "You must reduce the length of the supplied text to ";
		msg += characters;
		msg += " or less characters";
		alert (msg);
		object.focus ();
		object.select ();
	}
}

function openWindow (windowname) {
	var windowTitle = windowname.replace (/\s/g, "_");
	var width = (windowname.indexOf('Notes') == 0) ? 600 : 750;
	if (document.layers) {
    	window.open ("", windowTitle, "screenx=25,screeny=25,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars,resizable=yes,copyhistory=yes,width=" + width + ",height=550");
	}
	else {
    	window.open ("", windowTitle, "left=25,top=25,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars,resizable=yes,copyhistory=yes,width=" + width + ",height=550");
	}
}

function openFlexiWindow (windowname, width, height) {
	var windowTitle = windowname.replace (/\s/g, "_");
	if (document.layers) {
    	temp = "screenx=25,screeny=25,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars,resizable=yes,copyhistory=yes,width=" + width + ",height=" + height;
	}
	else {
    	temp = "left=25,top=25,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars,resizable=yes,copyhistory=yes,width=" + width + ",height=" + height;
	}
	window.open ("", windowTitle, temp);
}

function confirmRecordDelete (object, identifier) {
	var msg = 'You are about to delete:\n\n\t' + object + ' ' + identifier;
	msg += '\n\nYou cannot undo this deletion.';
	msg += '\n\nOnly select \'OK\' if you wish to continue.';
	msg += ' If you do not wish to continue then select \'Cancel\'.';
	var proceed = confirm(msg);
	if (proceed) {
		return true;
	}
	else {	
		return false;
	}
}

function confirmFormSubmit () {
	var msg = 'You have now chosen to submit the information on this form.';
	msg += '\n\nOnly select \'OK\' if you are sure you wish to proceed.';
	msg += ' Otherwise you should select \'Cancel\' to continue editing the information.';
	var proceed = confirm(msg);
	if (proceed) {
		return true;
	}
	else {	
		return false;
	}
}

function confirmFixIt () {
	var msg = 'Warning: you have now chosen to change the status on this application.';
	msg += ' This could have a significant impact on workflow associated with this application in the HOST online system.';
	msg += '\n\nOnly select \'OK\' if you are sure you wish to proceed.';
	msg += ' Otherwise you should select \'Cancel\' to review, or consult further regarding, this application.';
	var proceed = confirm(msg);
	if (proceed) {
		return true;
	}
	else {	
		return false;
	}
}

function confirmUnselecthost () {
	var msg = 'If you proceed you will unassign the host from this application and ';
	msg += 'both the student guest and the host will be informed that a new host will be ';
	msg += 'selected and a suitable visit date agreed.';
	msg += '\n\nYou will not be able to reverse these actions once you select \'OK\'.';
	msg += '\n\nIf you are not sure that you wish to proceed, you should select \'Cancel\'.';
	var proceed = confirm(msg);
	if (proceed) {
		return true;
	}
	else {
		return false;
	}
}

function confirmVisitDetails () {
	var msg = 'If you proceed you will initiate messages to both the assigned host ';
	msg += 'and the student guest containing details of the visit and the respective ';
	msg += '\'friendlies\'.';
	msg += '\n\nYou will not be able to reverse this action once you select \'OK\'.';
	msg += '\n\nIf you are not sure that you wish to proceed, you should select \'Cancel\'.';
	var proceed = confirm(msg);
	if (proceed) {
		return true;
	}
	else {
		return false;
	}
}

function showDateSelected (container, month, year, date, formattedDate) {
	var qmonth = (month < 10) ? '0' + month.toString() : month.toString();
	var qdate = (date < 10) ? '0' + date.toString() : date.toString();
	if (document.getElementById || document.all) {
		var place = window.opener.document.getElementById ? window.opener.document.getElementById (container) : window.opener.document.all[container];
		if (place && typeof place.innerHTML != "undefined") {
			var selected = year.toString() + '-' + qmonth + '-' + qdate;
			var content = '<input type="hidden" name="' + container + '" value="' + selected + '">';
			content += (formattedDate != "") ? formattedDate : selected;
			place.innerHTML = content;
		}
	}
}

function checkAll (field, state) {
	for (i = 0; i < field.length; i++) {
		field[i].checked = state;
	}
}

function toggledisplay (id) {
	if (document.getElementById) {
		document.getElementById(id).style.display = (document.getElementById(id).style.display == 'block') ? 'none' : 'block';
	}
	else if (document.all) {
		document.all(id).style.display = (document.all(id).style.display == 'block') ? 'none' : 'block';
	}
}

//	end of functions for site

