//=================================================================================
//	101 Media Ltd - SmartWebb
//	(c) 2002 101 Media Ltd
//	http://www.101ltd.com
//=================================================================================

var objForm = document.forms[document.forms.length - 1];

//-----------------------------------------------------------------------------
// Validation functions
//-----------------------------------------------------------------------------

function ShortContent(objTest, intSize, strWarning)
{
	if (objTest.value.length < intSize)
	{
		window.alert(strWarning);
		return true;
	}
	return false;
}

function TooShort(objTest, intSize, strWarning)
{
	if (objTest.value.length < intSize)
	{
		window.alert(strWarning);
		objTest.focus();
		objTest.select();
		return true;
	}
	return false;
}

function TooLong(objTest, intSize, strWarning)
{
	if (objTest.value.length > intSize)
	{
		window.alert(strWarning);
		objTest.focus();
		objTest.select();
		return true;
	}
	return false;
}

function BadSQL(objTest)
{
	if (objTest.value != "")
	{
		if (objTest.value.indexOf("%") >= 0 || objTest.value.indexOf("#") >= 0)
		{
			window.alert("Please remove % and # characters");
			objTest.focus();
			objTest.select();
			return true;
		}
	}
	return false;
}

function BadQuotes(objTest)
{
	if (objTest.value != "")
	{
		if (objTest.value.indexOf('"') >= 0)
		{
			window.alert('Please remove " characters');
			objTest.focus();
			objTest.select();
			return true;
		}
	}
	return false;
}

function BadEmail(objTest)
{
	var ixAt = objTest.value.indexOf("@");
	var ixDot = objTest.value.indexOf(".");
	var lenObj = objTest.value.length;
	if (objTest.value == "" || ixAt < 1 || ixDot < 1 || lenObj < (ixDot + 2))
	{
		window.alert("Please enter a valid Email address");
		objTest.focus();
		objTest.select();
		return true;
	}
	return false;
}

function BadItem(objTest, strWarning)
{
	if (objTest.selectedIndex <= 0)
	{
		window.alert(strWarning);
		objTest.focus();
		return true;
	}
	return false;
}

function BadInteger(objTest, strWarning)
{
	var strMask = /^[ ]*[+-]?\d+[ ]*$/;

	if (strMask.test(objTest.value) == false || !isFinite(objTest.value))
	{
		window.alert(strWarning);
		objTest.focus();
		objTest.select();
		return true;
	}
	return false;
}

function initialCaps(strText)
{
	var strPunc = ",.?!:;('";
	strPunc += '"';
	var arrSplit = strText.split(" ");
	var strFinal = "";

	var lngPos = 0;
	var lngPunc = 0;

	var strWord = "";
	var strLetter = "";
	var strPre = "";
	var strTemp = "";
	var strCmp = "";

	for (var lngEl = 0; lngEl < arrSplit.length; lngEl++)
	{
		arrSplit[lngEl] += " ";
		arrSplit[lngEl] = arrSplit[lngEl].toLowerCase();
		strTemp = "";
		while ((arrSplit[lngEl].length > 0) && (arrSplit[lngEl].indexOf(" ") > -1))
		{
			lngPos = arrSplit[lngEl].indexOf(" ");
			strWord = arrSplit[lngEl].substring(0, lngPos);
			strPre = "";
			if (strPunc.indexOf(strWord.substring(0, 1)) > -1)
			{
				strPre = strWord.substring(0, 1);
				strWord = strWord.substring(1, strWord.length);
			}
			strCmp = " " + strWord + " ";
			for (var i = 0; i < 9; i++)
			{
				lngPunc = strWord.indexOf(strPunc.substring(i, i + 1));
				if (lngPunc == strWord.length - 1)
				{
					strCmp = " " + strWord.substring(0, strWord.length - 1) + " ";
					i = 9;
				}
			}
			
			if (strCmp.indexOf(strCmp) <= 0)
			{
				strLetter = strWord.substring(0, 1);
				strLetter = strLetter.toUpperCase();
				strWord = strLetter + strWord.substring(1, strWord.length);
			}
			strTemp += strPre + strWord + " "; 
			arrSplit[lngEl] = arrSplit[lngEl].substring((lngPos + 1), 
			arrSplit[lngEl].length);
		}
		strLetter = strTemp.substring(0, 1);
		strLetter = strLetter.toUpperCase();
		if (strLetter != " ")
		{
			strFinal += strLetter + strTemp.substring(1, strTemp.length - 1) + " ";
		}
	}
	return strFinal;
}

function BadFives(objTest, strWarning)
{
	if (objTest.value % 5 > 0)
	{
		window.alert(strWarning);
		objTest.focus();
		objTest.select();
		return true;
	}
	return false;
}

function BadNumber(objTest, strWarning)
{
	var strMask = /^[ ]*[+-]?\d*\.?\d*([eE][+-]?\d+)?[ ]*$/;

	if (strMask.test(objTest.value) == false || !isFinite(objTest.value))
	{
		window.alert(strWarning);
		objTest.focus();
		objTest.select();
		return true;
	}
	return false;
}

function BadFullDate(objTest)
{
	var strDate = "", strTime = "";
	var arrTemp = objTest.value.split(" ");
	if (arrTemp.length > 1)
	{
		strDate = arrTemp[0];
		strTime = arrTemp[1]; 
	}
	else
	{
		strDate = objTest.value;
	}
	var vOut = strDate.split(new RegExp("[/]"));

	if (vOut.length == 3)
	{
		var vNewDate = new Date(vOut[2],vOut[1]-1,vOut[0])
		if (vNewDate.getFullYear() == parseInt(vOut[2],10) && vNewDate.getMonth() == (parseInt(vOut[1],10) - 1) && vNewDate.getDate() == parseInt(vOut[0],10))
		{
			if (vNewDate.getFullYear() > 1990 && vNewDate.getFullYear() < 2099)
			{
				return false;
			}
			else
			{
				window.alert("Please enter a date between 1990 and 2099");
			}	
		}
		else
		{
			window.alert("Please enter a valid date in the format dd/mm/yyyy");
		}
	}
	else
	{
		window.alert("Please enter a date in the format dd/mm/yyyy");
	}
	
	objTest.focus();
	objTest.select();
	return true;
}

function BadDate(objTest)
{
	if (objTest.value.length < 1)
	{
	    // Null entries are fine, it's a good date so return false
	    return false;
	}
	var strDate = objTest.value;
	var vOut = strDate.split(new RegExp("[/]"));

	if (vOut.length == 3)
	{
		var vNewDate = new Date(vOut[2],vOut[1]-1,vOut[0])
		if (vNewDate.getFullYear() == parseInt(vOut[2],10) && vNewDate.getMonth() == (parseInt(vOut[1],10) - 1) && vNewDate.getDate() == parseInt(vOut[0],10))
		{
			if (vNewDate.getFullYear() > 1990 && vNewDate.getFullYear() < 2099)
			{
				return false;
			}
			else
			{
				window.alert("Please enter a date between 1990 and 2099 or leave the box empty");
			}	
		}
		else
		{
			window.alert("Please enter a valid date in the format dd/mm/yyyy or leave the box empty");
		}
	}
	else
	{
		window.alert("Please enter a date in the format dd/mm/yyyy or leave the box empty");
	}

	objTest.focus();
	objTest.select();
	return true;
}

function isNAN(strCode)
{
		document.bookingForm.cDiscount.value = 0;
		var lngCode = strCode.length;
		if (lngCode < 6 || lngCode > 6 || isNaN(parseInt(strCode)))
		{
			alert("SORRY !!\n\nSpecial offer code: " + strCode + "\nIs not recognised.");
			document.bookingForm.cPromotion.focus();
			document.bookingForm.cDiscount.value = 0;
			return true;
		}

		var lngDiscount = (parseInt(strCode.charAt(0)) * 10) + parseInt(strCode.charAt(1));
		var lngTotal = parseInt(strCode.charAt(0)) + parseInt(strCode.charAt(1)) + parseInt(strCode.charAt(2)) + parseInt(strCode.charAt(3)) + parseInt(strCode.charAt(4)) + parseInt(strCode.charAt(5));
		if (lngTotal != 18 || lngDiscount < 0 || lngDiscount > 25)
		{
			alert("SORRY !!\n\nSpecial offer code: " + strCode + "\nIs not recognised.");
			document.bookingForm.cPromotion.focus();
			document.bookingForm.cDiscount.value = 0;
			return true;
		}
		// Looks like a good code 
		document.bookingForm.cDiscount.value = lngDiscount;
		return false;
}

function ConvertDate(strDate)
{
	var intOut = strDate.split(new RegExp("[/]"));
	if (intOut.length == 3)
	{
		return (new Date(intOut[2],intOut[1]-1,intOut[0]));
	}
	else	
	{
		return new Date();
	}
}


