Event.observe(window, 'load', SetListboxes, false);
Event.observe(window, 'load', SetRadioButtons, false);
//Event.observe(window, 'load', SetCheckboxes, false);

curType = "";
curEmail = "";
curFirstName = "";
curLastName = "";
curOrganisation = "";

function showFindDups(type) {
	params = "action=ShowFindDups&Type="+type;
	curType = type;
	var ajax = new Ajax.Updater(
		{success: 'findDups'},
		"/admin/Duplicates.php",
		{
			parameters: params
		}
	);
}

function showMatch(email) {
	curEmail = email;
	params = "action=ShowMatch&Email="+escape(email);
	var ajax = new Ajax.Updater(
		{success: 'duplicateMatch'},
		"/admin/Duplicates.php",
		{
			parameters: params
		}
	);
}

function showMatchByName(firstName, lastName) {
	curFirstName = firstName;
	curLastName = lastName;
	params = "action=ShowMatch&FirstName="+escape(firstName)+"&LastName="+escape(lastName);
	var ajax = new Ajax.Updater(
		{success: 'duplicateMatch'},
		"/admin/Duplicates.php",
		{
			parameters: params
		}
	);
}

function showMatchByOrganisation(organisation) {
	curOrganisation = organisation;
	params = "action=ShowMatch&Employer="+escape(organisation);
	var ajax = new Ajax.Updater(
		{success: 'duplicateMatch'},
		"/admin/Duplicates.php",
		{
			parameters: params
		}
	);
}

function mergeDuplicates() {
	params = "action=MergeDuplicates";
	if (curType == "email")
		params += "&Email="+escape(curEmail);
	else
		params += "&FirstName="+escape(curFirstName)+"&LastName="+escape(curLastName);
	var ajax = new Ajax.Updater(
		'duplicateMatch',
		"/admin/Duplicates.php",
		{
			parameters: params,
			onComplete: function(request) {
				/*if (curType == "email")
					showMatch(curEmail);
				else
					showMatchByName(curFirstName, curLastName);*/
			}
		}
	);
}

function SetListboxes() {
	Listboxes = document.getElementsBySelector('select');
	$A(Listboxes).each(function(Listbox) {
		if( (Listbox.getAttribute('originalvalue') != null) && (Listbox.getAttribute('originalvalue') != '') )
			SetListbox(Listbox,Listbox.getAttribute('originalvalue'));
	});
}

function SetListbox( Listbox, Value ) {        
	var Options = Listbox.getElementsByTagName("option");
			
    if( !Options )
        return false;
        
	for(var x = 0; x < Options.length; x++)
		if(Options.item(x).value == Value || Value == null) {
			Options.item(x).selected = true;
			return true;
		}
	return false;
}

function SetRadioButtons() {
	RadioButtons = document.getElementsBySelector('input[type="radio"]');
	$A(RadioButtons).each(function(RadioButton) {
		if(RadioButton.getAttribute('originalvalue'))
			SetRadioButton(RadioButton,RadioButton.getAttribute('originalvalue'));
	});
}

function SetRadioButton( RadioButton, Value ) {
	if( RadioButton.value == Value ) {
		RadioButton.checked = true;
	} else {
		RadioButton.checked = false;
	}
}

function SetCheckboxes() {
	Checkboxes = document.getElementsBySelector('input[type="checkbox"]');
	$A(Checkboxes).each(function(Checkbox) {
		SetCheckbox(Checkbox,Checkbox.getAttribute('originalvalue'));
	});
}

function SetCheckbox( Checkbox, Value ) {
	if(Checkbox.value = Value) {
		Checkbox.checked = true;
	} else {
		Checkbox.checked = false;
	}
}

PPValidated = true;
function ValidateProfessionalPractices() {
	PPValidated = true;
	Inputs = document.getElementsBySelector('input.IAgree');
	$A(Inputs).each(function(Input) {
		if( Input.checked == false ) {
			alert('You must first check I agree at every employer');
			PPValidated = false;
		}
	});
	return PPValidated;
}

function updateStatus(field, fieldCount, recert) {
	//alert(field);
	//alert(document.cfreForm[field+'[DateConferredMonth]'])
	// create date from available fields Degree[{$Degree->getDegreeID()}][DateConferredMonth]
	var monthField = document.cfreForm[field+'['+fieldCount+'][DateConferredMonth]'].options;
	var monthValue = monthField[monthField.selectedIndex].value;
	var yearField = document.cfreForm[field+'['+fieldCount+'][DateConferredYear]'].options;
	var yearValue = yearField[yearField.selectedIndex].value;
	var conferredDate=new Date()
	conferredDate.setFullYear(yearValue,monthValue-1);
	var today = new Date()
	var cutOffDate = new Date();
	if (recert)
		cutOffDate.setFullYear(today.getFullYear()-3, today.getMonth());
	else
		cutOffDate.setFullYear(today.getFullYear()-5, today.getMonth());
	//alert("cutoff: "+cutOffDate.getFullYear()+"-"+(cutOffDate.getMonth()+1)+"-"+cutOffDate.getDate());
	//alert("confferedDate: "+conferredDate.getFullYear()+"-"+(conferredDate.getMonth()+1)+"-"+conferredDate.getDate());
	//alert(parseInt(conferredDate.getTime())+" > "+parseInt(cutOffDate.getTime()) + " = "+(parseInt(conferredDate.getTime()) > parseInt(cutOffDate.getTime())));
	if (parseInt(conferredDate.getTime()) >= parseInt(cutOffDate.getTime())) {
		$(field+'Status'+fieldCount).className = "infoStatus";
		$(field+'['+fieldCount+']').className = "info";
	} else {
		$(field+'Status'+fieldCount).className = "infoStatusOld";
		$(field+'['+fieldCount+']').className = "oldInfo";
	}
	//alert(monthValue);
}
