startTime = 0;

function toggleMenu(cell) {
	if (cell.className.match("_on"))
	{
		cell.className = cell.className.replace("_on","_off");
	}
	else
	{
		cell.className = cell.className.replace("_off","_on");
	}
}

function showMenu(cell) {
	
	// close all menus

	for (x = 0; x < document.all.length; x++) {
		if(document.all[x].id.match("m_menu"))
		{
			document.all[x].className="m_hidden";
		}
	}

	// show selected menu

	var men_number = cell.id.charAt(cell.id.length-1);
	document.getElementById("m_menu_" + men_number).className="m_shown";
}

function submitLink(link)
{	
	parent.location=link;	
}

function convertDecToTime(dec)
{
	if (dec.toString().match(".5"))
	{
		return dec.toString().replace(".5",":30");
	}
	else
	{
		return dec.toString() + ":00";
	}

}

function disableBookingForm()
{

	// reset form fields
	document.getElementById("displaystarttime").value = "";
	document.getElementById("displayendtime").value = "";
	document.getElementById("starttime").value = "";
	document.getElementById("duration").value = "";
	document.getElementById("submit").disabled = true;
	
	// reset highlighted rows
	for (x = 0; x < document.all.length; x++) 
	{
		if(document.all[x].className == "schedule_free_set")
		{		
			document.all[x].className = "schedule_free_off";
		}
	}
}

function bookLesson(cell) 
{
	if (startTime == 0)
	{
		disableBookingForm();
		
		// start time is not set so set it
		startTime = parseFloat(cell.id.replace("free",""));
		
		// alert('set startTime to ' + startTime);
		
		document.getElementById("displaystarttime").value = convertDecToTime(startTime);
		cell.className = "schedule_free_set";


	}
	else
	{
		// start time is set so calculate lesson duration
		
		endTime = parseFloat(cell.id.replace("free","")) + 0.5;
		
		lessonDuration = endTime - startTime;
		// alert('start time is ' + startTime + ' and temp end time is ' + endTime + ' and lesson duration is ' + lessonDuration);
		
		// end time must be greater than start time
		if (endTime < startTime)
		{
			alert ('Lesson end time must be greater than lesson start time');
			
			disableBookingForm();
		
			startTime = 0;
			return;
		}
		
		// max lesson time is 2 hours
		if (lessonDuration < 1 || lessonDuration > 2)
		{
			 alert('Please note that lesson durations must be either 1 hour, 1 1/2 hours or 2 hours');
			
			disableBookingForm();
			
			startTime = 0;
			return;
		}
		
		// lesson must not cross busy period
		for (x = 0; x < document.all.length; x++) 
		{
			if(document.all[x].id.match("busy"))
			{
				
				busyTime = parseFloat(document.all[x].id.replace("busy",""));
				// alert('in loop - busy time is ' + busyTime);
				if (busyTime >= startTime && busyTime < endTime)
				{
					alert("lesson crosses busy period");
					
					disableBookingForm();
		
					startTime = 0;
					return;
				}
			}
		}
		
		// if we get through all these checks, booking can be made
		// document.getElementById("displayendtime").value = convertDecToTime(endTime);
		document.getElementById("displayendtime").value = convertDecToTime(endTime);
		
		// set hidden fields
		document.getElementById("starttime").value = startTime;
		document.getElementById("duration").value = (endTime - startTime) * 2;
		
		highlightSelectedLesson(startTime, endTime);
		
		// enable button
		document.getElementById("submit").disabled = false;
		
		// reset start time var
		startTime = 0;

	}

}

function highlightSelectedLesson(start, end)
{

	// highlight rows
	for (x = 0; x < document.all.length; x++) 
	{
		if(document.all[x].id.match("free"))
		{	
			time = parseFloat(document.all[x].id.replace("free",""));
			
			if (time >= start && time < end)
			{
				document.all[x].className = "schedule_free_set";
			}
		}
	}

}

function confirmAction()
{
	return confirm("Are you sure you wish to cancel this lesson?");
}


function isFieldAlphaNumeric(field, alphaallowed, numericallowed, spacesallowed, specialsallowed)
{
    alphachars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    numericchars = "0123456789";
    spaces = " ";
    specials = "-'+";
    
    searchstring = "";
    
    if (alphaallowed)
    	searchstring+=alphachars;
    	
    if (numericallowed)
    	searchstring+=numericchars;
    
    if (spacesallowed)
    	searchstring+=spaces;
    	
    if (specialsallowed)
    	searchstring+=specials;
    
    for (var i=0; i<field.value.length; i++) {
        if (searchstring.indexOf(field.value.charAt(i)) < 0) {
        	alert("The character " + field.value.charAt(i) + " is not valid for the field " + field.id);
        	return false;
        }
    }
    
    return true;

}


function isFieldValid(field, minlength, maxlength, alpha, numeric, spaces, specials)
{

	// check field is not null and length
	if (field.value == null || field.value == "" || field.value.length < minlength || field.value.length > maxlength)
	{
		alert ("The field " + field.id + " must be between " + minlength + " and " + maxlength + " characters long");
		return false;
	}
	
	// if required, check character content
	
	alpha_check = true;
		
	if (alpha || numeric)
	{
		alpha_check = isFieldAlphaNumeric(field, alpha, numeric, spaces, specials);
	}
	
	return alpha_check;
}

function validateAddUser()
{
	return isFieldValid(document.getElementById("userid"), 6, 12, true, true, false, false) 
		&& isFieldValid(document.getElementById("forename"), 2, 20, true, false, true, true)
		&& isFieldValid(document.getElementById("surname"), 2, 20, true, false, true, true)
		&& isFieldValid(document.getElementById("phone"), 2, 20, false, true, false, true)
		&& isFieldValid(document.getElementById("email"), 5, 50, false, false, false, false)
		&& isFieldValid(document.getElementById("postcode"), 5, 8, true, true, true, false);
}