function openWindow(page,width,height)
{
	var oOptions = "height="+height+",width="+width+",status=no,toolbar=no,resizable=no,scrollbars=yes,dependend=yes,menubar=no,location=no";
	window.open(page,null,oOptions);
}

function doLimitCheck(oTextArea,iLimit,strCharCouterID,strSubmitID) 
{
	var oCouterDisplay = document.getElementById(strCharCouterID);
	var iSize = oTextArea.value.length;
	var oSubmit = document.getElementById(strSubmitID);
	oCouterDisplay.value=iSize;
				
	if (iSize > iLimit) 
	{
		oSubmit.disabled=true;
		oCouterDisplay.style.color="red";
	}
	else 
	{
		oCouterDisplay.style.color="black";
		oSubmit.disabled=false;
	}
}

function doPostcodeFocus()
{
	doFocus('postcode');
}

function doFocus(elementId)
{
    var inputfield = document.getElementById(elementId);
    if (inputfield != null)
    {
    	var iLength = inputfield.value.length;
    	inputfield.focus();
    	setSelectionRange(inputfield, iLength, iLength);
    }
}
    		
function setSelectionRange(input, selectionStart, selectionEnd)
{
    if (input.setSelectionRange)
    {
        input.focus();
        input.setSelectionRange(selectionStart, selectionEnd);
    }
    else if (input.createTextRange)
    {
        var range = input.createTextRange();
        range.collapse(true);
        range.moveEnd('character', selectionEnd);
        range.moveStart('character', selectionStart);
        range.select();
    }
}

function checkLessonType()
{
	var iVal = document.getElementById('type').value;
	
	if(iVal == 6)
	{
		document.getElementById('number').disabled=true;
	}
	else 
	{
		document.getElementById('number').disabled=false;
	}
}

function validateQuote()
{
	
	/*var bFound = false;
	if(document.getElementById('mon').checked)bFound = true;
	if(document.getElementById('tue').checked)bFound = true;
	if(document.getElementById('wed').checked)bFound = true;
	if(document.getElementById('thu').checked)bFound = true;
	if(document.getElementById('fri').checked)bFound = true;
	if(document.getElementById('sat').checked)bFound = true;
	if(document.getElementById('sun').checked)bFound = true;
	
	if(bFound)
	{
		document.forms[0].submit();
	}
	else
	{
		alert("Please select at least one day");
		return false;
	}
	*/
	
	document.forms[0].submit();
}

function validateBooking()
{
	var bValid = true;
	var strErrorMsg = "The following fields are required : \n--------------------------------------\n";
	var strFName = document.getElementById('forename').value;
	strFName = trim(strFName);
	if(stringCheckName(strFName) == false)
	{
		strErrorMsg = strErrorMsg + "Forename\n";
		bValid = false;
	}
	var strSName = document.getElementById('surname').value;
	strSName = trim(strSName);
	if(stringCheckName(strSName) == false)
	{
		strErrorMsg = strErrorMsg + "Surname\n";
		bValid = false;
	}
	var strLine1 = document.getElementById('line1').value;
	if(strLine1 == null || strLine1.length == 0)
	{
		strErrorMsg = strErrorMsg + "Address Line 1\n";
		bValid = false;
	}
	
	var strCity = document.getElementById('city').value;
	if(strCity == null || strCity.length == 0)
	{
		strErrorMsg = strErrorMsg + "City\n";
		bValid = false;
	}
	var strPostCode = document.getElementById('postcode').value;
	strPostCode = trim(strPostCode);
	if(strPostCode == null || strPostCode.length == 0)
	{
		strErrorMsg = strErrorMsg + "Post Code\n";
		bValid = false;
	}
	var strMob = document.getElementById('mobtel').value;
	var strTel = document.getElementById('tel').value;
	strMob = trim(strMob);
	strTel = trim(strTel);
	if((telephoneCheck(strTel) == false) && (telephoneCheck(strMob) == false))
	{
		strErrorMsg = strErrorMsg + "Telephone\n";
		bValid = false;
	}
	
	if(bValid)
	{
		document.forms[0].submit();
	}
	else
	{
		alert(strErrorMsg);
		return false;
	}
}

function validateVoucher1()
{
	var bValid = true;
	var strErrorMsg = "The following fields are required : \n--------------------------------------\n";
	var strFName = document.getElementById('full_name').value;
	strFName = trim(strFName);
	if(stringCheckName(strFName) == false)
	{
		strErrorMsg = strErrorMsg + "Name\n";
		bValid = false;
	}
	var strMob = document.getElementById('mobtel').value;
	var strTel = document.getElementById('tel').value;
	strMob = trim(strMob);
	strTel = trim(strTel);
	if((telephoneCheck(strTel) == false) && (telephoneCheck(strMob) == false))
	{
		strErrorMsg = strErrorMsg + "Telephone\n";
		bValid = false;
	}
	
	if(bValid)
	{
		document.forms[0].submit();
	}
	else
	{
		alert(strErrorMsg);
	}
}

function validateVoucher2()
{
	var bValid = true;
	var strErrorMsg = "The following fields are required : \n--------------------------------------\n";
	
	var strLine1 = document.getElementById('line1').value;
	if(strLine1 == null || strLine1.length == 0)
	{
		strErrorMsg = strErrorMsg + "Address Line 1\n";
		bValid = false;
	}
	
	var strCity = document.getElementById('city').value;
	if(strCity == null || strCity.length == 0)
	{
		strErrorMsg = strErrorMsg + "City\n";
		bValid = false;
	}
	var strPostCode = document.getElementById('postcode').value;
	strPostCode = trim(strPostCode);
	if(strPostCode == null || strPostCode.length == 0)
	{
		strErrorMsg = strErrorMsg + "Post Code\n";
		bValid = false;
	}
	
	
	var strRecipientName = document.getElementById('recipient').value;
	if(strRecipientName == null || strRecipientName.length == 0)
	{
		strErrorMsg = strErrorMsg + "Recipient Name\n";
		bValid = false;
	}
	
	var strVoucherValue = document.getElementById('value').value;
	if(strVoucherValue == null || strVoucherValue.length == 0)
	{
		strErrorMsg = strErrorMsg + "Voucher Value\n";
		bValid = false;
	}
	
	if(bValid)
	{
		document.forms[0].submit();
	}
	else
	{
		alert(strErrorMsg);
	}
}

function validateJobForm()
{
	var bValid = true;
	var strErrorMsg = "The following fields are required : \n--------------------------------------\n";
	var strFName = document.getElementById('first_name').value;
	strFName = trim(strFName);
	if(stringCheckName(strFName) == false)
	{
		strErrorMsg = strErrorMsg + "Forename\n";
		bValid = false;
	}
	var strSName = document.getElementById('surname').value;
	strSName = trim(strSName);
	if(stringCheckName(strSName) == false)
	{
		strErrorMsg = strErrorMsg + "Surname\n";
		bValid = false;
	}

	var strMob = document.getElementById('mob').value;
	var strTel = document.getElementById('tel').value;
	strMob = trim(strMob);
	strTel = trim(strTel);
	if((telephoneCheck(strTel) == false) && (telephoneCheck(strMob) == false))
	{
		strErrorMsg = strErrorMsg + "Telephone\n";
		bValid = false;
	}
	
	if(bValid)
	{
		document.forms[0].submit();
	}
	else
	{
		alert(strErrorMsg);
		return false;
	}
}

function validateEarningsForm()
{
	var bValid = true;
	var strErrorMsg = "The following fields are required : \n--------------------------------------\n";
	var strFName = document.getElementById('first_name').value;
	strFName = trim(strFName);
	if(stringCheckName(strFName) == false)
	{
		strErrorMsg = strErrorMsg + "Forename\n";
		bValid = false;
	}
	var strSName = document.getElementById('surname').value;
	strSName = trim(strSName);
	if(stringCheckName(strSName) == false)
	{
		strErrorMsg = strErrorMsg + "Surname\n";
		bValid = false;
	}
	var strPostCode = document.getElementById('postcode').value;
	strPostCode = trim(strPostCode);
	if(strPostCode == null || strPostCode.length == 0)
	{
		strErrorMsg = strErrorMsg + "Post Code\n";
		bValid = false;
	}

	var strMob = document.getElementById('mob').value;
	var strTel = document.getElementById('tel').value;
	strMob = trim(strMob);
	strTel = trim(strTel);
	if((telephoneCheck(strTel) == false) && (telephoneCheck(strMob) == false))
	{
		strErrorMsg = strErrorMsg + "Telephone\n";
		bValid = false;
	}
	
	if(bValid)
	{
		document.forms[0].submit();
	}
	else
	{
		alert(strErrorMsg);
		return false;
	}
}

function stringCheckName(strFName)
{
	if(strFName == null)return false;
	
	strFName = trim(strFName);
	
	var iLength = strFName.length;
	
	if(iLength < 2)return false;
	
	return true;
	
	/*
	var iVowel = 0;
	var iSameCharCount = 1;
	var oLastChar = '';
	
	for(var i=0; i<iLength; i++)
	{
		var TmpChar = strFName.charAt(i);
		if(TmpChar == oLastChar)iSameCharCount++;
		else iSameCharCount = 1;
		if(iSameCharCount > 2)return false;
		oLastChar = TmpChar;
		if(TmpChar == 'y' 
			|| TmpChar == 'a'
			|| TmpChar == 'i'
			|| TmpChar == 'o'
			|| TmpChar == 'u'
			|| TmpChar == 'e')iVowel++;
	}
	if(iLength == 0 || iVowel == 0 || iLength == iSameCharCount)return false;
	return true;
	*/
}

function telephoneCheck(strTel)
{
	if(strTel == null)return false;
	
	strTel = strTel.replace('(',' ');
	strTel = strTel.replace(')',' ');
	strTel = strTel.replace('+','0');
	strTel = trim(strTel);
	
	var iLength = strTel.length;
	if(iLength == 0)return false;
	var iDigitCount = 0;
	for(var i=0; i<iLength; i++)
	{
		var TmpChar = strTel.charAt(i);
		if(i == 0 && TmpChar != '0')return false;
		if(isDigit(TmpChar))
		{
			iDigitCount++;
		}
	}
	if(iDigitCount<6)return false;
	return true;
}

function isDigit(num) {
	if (num.length>1){return false;}
	var string="1234567890";
	if (string.indexOf(num)!=-1){return true;}
	return false;
}

function trim(strVal)
{
	var iLength = strVal.length;
	for(var i=0; i<iLength; i++)
	{
		var c = strVal.charAt(0);
		
		if(c == ' ')strVal = strVal.substring(i);
		else return strVal;
	}
	return strVal;
}


function leftTrim(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	return sString;
}

function rightTrim(sString)
{
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function trimAll(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function changeMade()
{
	document.getElementById('quotebutton').disabled=false;
}