/* Implementations of library functions for My English CV */


// Function to display an error message for the user using a designated area of the page
function DisplayError(msg, field)
{
	if (document.getElementById("JSErrorMessagesTop"))
	{
		var el = GetMyObjectByID("JSErrorMessagesTop");
		el.innerHTML = msg;
		el.style.display = "block";
	}
	
	if (document.getElementById("JSErrorMessagesBottom"))
	{
		var el2 = GetMyObjectByID("JSErrorMessagesBottom");
		el2.innerHTML = msg;
		el2.style.display = "block";
	}
	
	if (field!="") GetMyObjectByID(field).focus();
	return false;
}

// Function to clear designated error message area
function ClearError()
{
	if (document.getElementById("JSErrorMessagesTop"))
	{
		var el = GetMyObjectByID("JSErrorMessagesTop");
		el.innerHTML = "";
		el.style.display = "none";
	}
	
	if (document.getElementById("JSErrorMessagesBottom"))
	{
		var el2 = GetMyObjectByID("JSErrorMessagesBottom");
		el2.innerHTML = "";
		el2.style.display = "none";
	}
}		


/* Standard library functions */


// verifies that a value is specified for a text field
function CheckField(id)
{
	return (GetMyObjectByID(id).value.length>0)
}

// verifies that a field is greater than or less than a specified length
function CheckFieldLength(id, comparison, len)
{
	if (comparison == ">")
	{
		return (GetMyObjectByID(id).value.length >= len)
	}
	else if (comparison == "=")
	{
		return (GetMyObjectByID(id).value.length == len)
	}	
	else
	{
		return (GetMyObjectByID(id).value.length <= len)
	}	
}

// verifies that a drop-down is selected
function CheckDD(id)
{
	var o = GetMyObjectByID(id);	
	if (o.selectedIndex < 0) return false;
		
	var selected_option = o.options[o.selectedIndex].value;	
	return  !(selected_option.length<1 || selected_option == "0")
}

// verifies that a checkbox is checked
function CheckCheckBox(id)
{
	return (GetMyObjectByID(id).checked);
}

// verify that a radio button in a named group is selected
function CheckRadio(name)
{	
	var o = document.forms[0][name];	
	for (var i=0, n=o.length; i < n; i++) 
	{
		if (o[i].checked) return true;
	}
	return false;
}

// verify that an email address is the correct format
function CheckEmail(id) 
{
	var o = GetMyObjectByID(id);	
	// Allow an email address that is empty
	if (o.value.length < 1) return true;

	var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;		
	return (regex.test(o.value))
}

// verify that two text fields are identical
function CheckComparison(id1, id2)
{		
	return (GetMyObjectByID(id1).value == GetMyObjectByID(id2).value)
}

// verify that a numeric field is between two values
function CheckBetween(id, lbound, ubound)
{	
	var val = GetMyObjectByID(id).value; 	
	return ( val>=lbound && val<=ubound)
}

// verifies that a text field value is numeric
function CheckNumeric(id)
{
	return (!isNaN(GetMyObjectByID(id).value));
}

// gets an object by id (supports IE4+ and all standards-compliant browsers)
function GetMyObjectByID(id)
{
	if (document.getElementById)
		return document.getElementById(id)
	else if (document.all)
		return document.all[id];
}

