// JavaScript Document

var numQualifications = 0;
var numQualificationsUna = 0;
var undefined;

function check() {
	var theForm = $('new_candidate_form');
	if (theForm) {
		$('Submit').disabled = true;
		var isError = false;
		var errorMessage = '';
		if ($F('first_name').length < 1) {
			isError = true;
			errorMessage += '<li class="error">Please provide your <strong>first name</strong>.</li>';
		}
		if ($F('last_name').length < 1) {
			isError = true;
			errorMessage += '<li class="error">Please provide your <strong>last name</strong>.</li>';
		}
		if ($F('address1').length < 1) {
			isError = true;
			errorMessage += '<li class="error">Please provide your <strong>address</strong>.</li>';
		}
		if ($F('town').length < 1) {
			isError = true;
			errorMessage += '<li class="error">Please provide your <strong>town</strong>.</li>';
		}
		if ($F('postcode').length < 1) {
			isError = true;
			errorMessage += '<li class="error">Please provide a <strong>postcode</strong>.</li>';
		}
		if ($F('telephone').length < 1) {
			isError = true;
			errorMessage += '<li class="error">Please provide a <strong>telephone number</strong>.</li>';
		}
		
		if (isError) {
			var messageDiv = $('errorMargin');
			if (messageDiv) {
				errorMessage = '<p class="error">The following error or errors occurred while processing the form. Please correct them and try again.</p><ul>' + errorMessage + '</ul>';
				messageDiv.innerHTML = errorMessage;
			} else {
				alert(errorMessage);
			}
			$('Submit').disabled = false;
			return false;
		}
	}
	return true;
}

function checkHistory() {
	var theForm = $('new_candidate_form');
	if (theForm) {
		var isError = false;
		var errorMessage = '';
		if ($F('detail').length < 1) {
			isError = true;
			errorMessage += '<li class="error">Please provide the employer\'s details.</li>';
		}
		if ($F('from').length < 1) {
			isError = true;
			errorMessage += '<li class="error">Please provide the month and year the employment was from.</li>';
		}
		if ($F('to').length < 1) {
			isError = true;
			errorMessage += '<li class="error">Please provide the month and year the employment was to.</li>';
		}
		if ($F('role').length < 1) {
			isError = true;
			errorMessage += '<li class="error">Please give the your role.</li>';
		}
		if ($F('duties').length < 1) {
			isError = true;
			errorMessage += '<li class="error">Please give details for your duties during this employment.</li>';
		}
		if ($F('reason').length < 1) {
			isError = true;
			errorMessage += '<li class="error">Please detail why you left this employment - or if this is your current employment, please state that this is the case.</li>';
		}
		
		if (isError) {
			var messageDiv = $('errorMargin');
			if (messageDiv) {
				errorMessage = '<p class="error">The following error or errors occurred while processing the form. Please correct them and try again.</p><ul>' + errorMessage + '</ul>';
				messageDiv.innerHTML = errorMessage;
			} else {
				alert(errorMessage);
			}
			return false;
		}
	}
	return true;
}

function checkFinal() {
	var theForm = $('new_candidate_form');
	if (theForm) {
		if ($('tac').checked) {
			return true;
		} else {
			var messageDiv = $('errorMargin');
			if (messageDiv) {
				errorMessage = 'Please tick the box to indicate that you\'ve read the terms and conditions.';
				messageDiv.innerHTML = '<p class="error">' + errorMessage + '</p>';
			} else {
				alert(errorMessage);
			}
			return false;
		}
	}
	return true;
}

function cancelButton() {
	location.href='/';
	return false
}

function injectCancelButton() {
	var button;
	try {
		button = $('cancel');
	} catch(ex) {}
	if (button) {
		addEvent(button, 'click', cancelButton);
	}
}

function injectBehaviours() {
	injectCancelButton();
	setupExistingQualifications();
	addQualificationInput();
	addQualificationInput(true);
}

function setupExistingQualifications() {
	if (qualifications != undefined && qualificationIds != undefined) {
		for (var n = 0; n < aQualLvl.length; n++) {
			addQualificationInput(false, aQualLvl[n], aQualSubject[n], aQualPlace[n], aQualYear[n]);
		}
		for (var n = 0; n < uQualLvl.length; n++) {
			addQualificationInput(true, uQualLvl[n], uQualSubject[n], uQualPlace[n], uQualYear[n]);
		}
	}
}

function deleteRow(aRow) {
	aRow.parentNode.removeChild(aRow);
}


function addQualificationInput() {
	var unattained;
	var level;
	var subject;
	var place;
	var year;
	
	if (addQualificationInput.arguments.length > 0) {
		unattained = addQualificationInput.arguments[0];
	} else {
		unattained = false;
	}
	
	if (addQualificationInput.arguments.length > 1) {
		level = addQualificationInput.arguments[1];
	} else {
		level = '';
	}
	
	if (addQualificationInput.arguments.length > 2) {
		subject = addQualificationInput.arguments[2];
	} else {
		subject = '';
	}
	
	if (addQualificationInput.arguments.length > 3) {
		place = addQualificationInput.arguments[3];
	} else {
		place = '';
	}
	
	if (addQualificationInput.arguments.length > 4) {
		year = addQualificationInput.arguments[4];
	} else {
		year = '';
	}
	
	if (qualifications != undefined && qualificationIds != undefined) {
		var qualTBody, diff, currId, years;
		var curYear = (new Date()).getYear();
		if (curYear < 1900) {
			curYear = curYear + 1900;
		}
		if (unattained) {
			qualTBody = $('qual2TBody');
			diff = '_un_';
			currId = numQualificationsUna;
			years = new Array(10);
			for (var i = 0; i < years.length; ++i) {
				years[i] = curYear+i;
			}
		} else {
			qualTBody = $('qualTBody');
			diff = '';
			currId = numQualifications;
			years = new Array(50);
			for (var i = 0; i < years.length; ++i) {
				years[i] = curYear-i;
			}
		}
		if (qualTBody) {
			var newLineHtml = '<tr id="qualline'+diff+currId+'"><td><select name="qualification'+diff+currId+'" id="qualification'+diff+currId+'" class="textarea">';
			for (var i = 0; i < qualifications.length; ++i) {
            	newLineHtml += '<option value="'+ qualificationIds[i] +'"'
				if (qualificationIds[i] == level) {
					newLineHtml += ' selected="selected" ';
				}
				newLineHtml += '>'+ qualifications[i] +'</option>';
			}
            newLineHtml += '</select></td>';
			newLineHtml += '<td><input name="subject'+diff+currId+'" type="text" id="subject'+diff+currId+'" size="12" maxlength="255" class="textarea" value="'+subject+'"></td>';
			newLineHtml += '<td><input name="place'+diff+currId+'" type="text" id="place'+diff+currId+'" size="12" maxlength="255" class="textarea" value="'+place+'"></td>';
			newLineHtml += '<td><select name="year'+diff+currId+'" type="text" id="year'+diff+currId+'" class="textarea">';
			for (var i = 0; i < years.length; ++i) {
            	newLineHtml += '<option value="'+ years[i] +'"'
				if (years[i] == year) {
					newLineHtml += ' selected="selected" ';
				}
				newLineHtml += '>'+ years[i] +'</option>';
			}
			newLineHtml += '</select></td>';
			//if (currId == 0) {
			//	newLineHtml += '<td><a href="javascript:void(0)" class="nu" onclick="addQualificationInput('+unattained+');">+</a></td>';
			//} else {
				newLineHtml += '<td><a href="javascript:void(0)" class="nu" onclick="removeQualificationInput('+ currId +', '+unattained+')">-</a></td>';
			//}
			
			'</tr>';
			
			new Insertion.Bottom(qualTBody, newLineHtml)
			
			if (unattained) {
				++numQualificationsUna;
				$('numQualsUna').value = numQualificationsUna;
			} else {
				++numQualifications;
				$('numQuals').value = numQualifications;
			}
		}
	}
}

function removeQualificationInput(inputId, unattained) {
	var ident;
	if (unattained) {
		ident = 'qualline_un_'+inputId;
	} else {
		ident = 'qualline'+inputId;
	}
	var qualLine = $(ident);
	if (qualLine) {
		deleteRow(qualLine);
	}
}

addEvent(window, 'load', injectBehaviours);
