function trimText(text)
{
	var regexp = /^\s*(.*?)\s*$/;
	var strings = text.value.match(regexp);
	if (strings!=null)
		text.value = strings[1];
	return;
}

function checkDate(date,msg)
{
	trimText(date);
    var monthlen = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
    var regexp = /^(\d{1,2})\/(\d{1,2})\/(\d{2})$/;
	var datesplit = date.value.match(regexp);
    if (datesplit==null || parseInt(datesplit[2])>12 ||
	    parseInt(datesplit[1])>monthlen[parseInt(datesplit[2])-1]+(parseInt(datesplit[3])%4==0))
    {
        alert((msg==null ? "Invalid date" : msg) + ": '" + date.value + "'");
		date.focus();
        return 1;
    }
    return 0;
}
function checkDateOpt(date,msg)
{
	trimText(date);
	return date.value.length>0 ? checkDate(date,msg) : 0;
}
function checkTime(time,msg)
{
	trimText(time);
	var str = time.value;
	str = str.replace(/a\./, "a");
	str = str.replace(/p\./, "p");
	str = str.replace(/m\./, "m");
	str = str.replace(/\./, ":");

    var regexp = /^[01]?\d(:\d{2})?\s*(am|pm)$/;
	if (str.match(regexp)==null)
	{
        alert((msg==null ? "Invalid time" : msg) + ": '" + time.value + "'");
		time.focus();
		return 1;
	}
	time.value = str; //write back to form field
    return 0;
}
function checkTimeOpt(time,msg)
{
	trimText(time);
	return time.value.length>0 ? checkTime(time,msg) : 0;
}

function checkPostcode(postcode,msg)
{
    return 0;
}

function checkEmail(email,msg)
{
	trimText(email);
    var regex = /^([\w-_\.]*)@([\w-\.]*\.[A-Za-z]{2,4})$/;
	if (email.value.match(regex) == null)
	{
        alert((msg==null ? "Invalid email" : msg) + ": '" + email.value + "'");
		email.focus();
		return 1;
	}
	
    return 0;
}
function checkEmailOpt(email,msg)
{
	trimText(email);
	return email.value.length>0 ? checkEmail(email,msg) : 0;
}

function checkFloat(f,msg)
{
	trimText(f);
    var regex = /^(\d+(\.\d*)?)$/;
	if (f.value.match(regex) == null)
	{
        alert((msg==null ? "Invalid number" : msg) + ": '" + f.value + "'");
		f.focus();
		return 1;
	}
	
    return 0;
}
function checkFloatOpt(f,msg)
{
	trimText(f);
	return f.value.length>0 ? checkFloat(f,msg) : 0;
}

function checkInteger(myInt,msg)
{
	trimText(myInt);
    var regex = /^(\d+)$/;
	if (myInt.value.match(regex) == null)
	{
        alert((msg==null ? "Invalid number" : msg) + ": '" + myInt.value + "'");
		myInt.focus();
		return 1;
	}
	
    return 0;
}
function checkIntegerOpt(myInt,msg)
{
	trimText(myInt);
	return myInt.value.length>0 ? checkInteger(myInt,msg) : 0;
}
function checkInt0(myInt,msg)
{
	trimText(myInt);
	if (myInt.value.length==0) myInt.value='0';
	return checkInteger(myInt,msg);
}

function checkRequired(text,msg)
{
	trimText(text);
	if (text.value.length==0)
	{
        alert(msg==null ? "Missing required field" : msg);
		text.focus();
		return 1;
	}
    return 0;
}

function checkSelect(select,msg)
{
	for (var i=0; i<select.options.length; i++)
	{
		if (select.options[i].selected)
		{
			var val = select.options[i].value;
			if (val=='')
			{
				alert(msg==null ? "Please make a selection" : msg);
				return 1;
			}
			else
				return 0;
		}
	}
	alert(msg==null ? "Please make a selection" : msg);
	return 1;
}

function checkPassword(pwd1,pwd2)
{
	if (pwd1.value.length < 6)
	{
        alert("Passwords must be at least 6 characters");
		pwd1.focus();
		return 1;
	}
	if (pwd1.value != pwd2.value)
	{
        alert("Password is not the same in both boxes!");
		pwd1.focus();
		return 1;
	}
    return 0;
}

function thumbName(name,size)
{
    i = name.lastIndexOf('/');
    j = name.lastIndexOf('.');

    if (i<0 || j<0) return "images/Library/thumbs/thumbnail.jpg";

    return name.substring(0,i) + "/thumbs" +
           name.substring(i,j) + '_' + size + name.substring(j);

    //return name;
}

