/*
* This function makes sure that the users are putting numeric values
* in the appropriate boxes.
*/
	function validateInput3(textInput)
	{

		var val = checkNumber(textInput.value);

		if ( isNaN(parseInt(val)) || (parseInt(val) < 0) )
		{
		    alert("Please enter your total annual income");
		    textInput.value = 0;
		    return false;
		}
		else
		{
			textInput.value = val;
			return true;
		}
	}



/*
* This function makes sure that the users are putting numeric values
* in the appropriate boxes.
*/
	function validateInput4 (textInput)
	{

		var val = checkNumber(textInput.value);

		if ( isNaN(parseInt(val)) || (parseInt(val) < 0) )
		{
		    alert("Please enter your monthly commitments");
		    textInput.value = 0;
		    return false;
		}
		else
		{
			textInput.value = val;
			return true;
		}
	}




/*
* This function makes sure that the users are putting numeric values
* in the appropriate boxes.
*/
	function validateInput1(textInput)
	{

		var val = checkNumber(textInput.value);

		if ( isNaN(parseInt(val)) || (parseInt(val) < 0) )
		{
		    alert("Please enter a value for the amount you would like to borrow ie. £50,000");
		    textInput.value = 0;
		    return false;
		}
		else
		{
			textInput.value = val;
			return true;
		}
	}



/*
* This function makes sure that the users are putting numeric values
* in the appropriate boxes.
*/
	function validateInput2(textInput)
	{

		var val = checkNumber(textInput.value);

		if ( isNaN(parseInt(val)) || (parseInt(val) < 0) )
		{
		    alert("Please enter a value for the property value ie. £75,000");
		    textInput.value = 0;
		    return false;
		}
		else
		{
			textInput.value = val;
			return true;
		}
	}



/*
* This function makes sure that the users are putting numeric values
* in the appropriate boxes.
*/
	function validateInput(textInput)
	{

		var val = checkNumber(textInput.value);

		if ( isNaN(parseInt(val)) || (parseInt(val) < 0) )
		{
		    alert("You should put positive numeric amounts in the boxes!");
		    textInput.value = 0;
		    return false;
		}
		else
		{
			textInput.value = val;
			return true;
		}
	}


/*
* This function deals with numbers entered with commas
* e.g. 1,000 or 21,000, etc.
*/
	function checkNumber(input)
	{
		var num = "";
		var str = input;
		var totalStr = ""

		for (var i = 0; i < str.length; i++)
		{
			num = str.substring(i, i + 1);
			if (num == ",")
			{
				continue;
			}
			else
			{
				totalStr = totalStr + str.substring(i, i + 1);
			}
		}

 //now remove leading zero's
		while (totalStr.substring(0, 1) == "0")
		{
			totalStr = totalStr.substring(1, totalStr.length);
		}

	 	return totalStr;
	}
    
    
	function escapeText(text)
	{

// call the standard escape code to convert to friendly text
		escapedText = escape(text);
		newText = "";
		singleChar = "";
		escapedAddChar = "%2B";
		unescapedAddChar = "+";

// check for + chars, these do not get escaped for some reason
		for (var i = 0; i < escapedText.length; i++)
		{
			singleChar = escapedText.charAt(i);

			if (singleChar == unescapedAddChar)
			{
				newText = newText + escapedAddChar;
			}
			else
			{
				newText = newText + singleChar;
			}
		}

		return newText;
	}

// format a float properly so that 1dp is displayed as 2dp - e.g. 3.1 = 3.10
	function floatToString(numToConvert)
	{
		formattedNumber = numToConvert.toString();

// get the position of the decimal place
		pos = formattedNumber.indexOf(".");

// work out if there is a dp
		if (pos == -1)
		{
// no dp so add it
			formattedNumber += ".00";
		}
		else
		{
// work out if there is only 1 dp
			if (pos == (formattedNumber.length - 2))
			{
// only one dp so add the final 0
				formattedNumber += "0";
			}
		}
		return formattedNumber;
	}


	function validateLoanRequired(loanRequired)
	{
		if (parseInt(loanRequired.value) < 15000)
		{
			alert("Currently we only offer mortgages over £15000, please enter a value of 15000 or more.");
			return false;
		}
		else
		{
			return true;
		}
	}


// Check telephone numbers
 	function checkTelNumber(strNum)
 	{

// remove any spaces in the text box
		strVal = removeSpaces(strNum);
 		intVal = parseInt(strVal);

		if (strVal == "")
		{
// No number entered
			alert("Please enter a contact telephone number");
			return false;
		}
		else if (containsLetters(strVal))
		{
// Letters entered
			alert("Please only enter numbers");
			return false;
		}
		else if (intVal < 0)
		{
// Have entered a negative number
			alert("Please remove the - sign");
			return false;
		}
		else
		{
// OK
			return true;
		}
    }

    function containsLetters(val)
    {
		singleChar = "";
		letterFound = false;

// loop through each character and check if it is a letter
		for (i = 0; i < val.length; i++)
		{
			singleChar = val.charAt(i);
			intChar = parseInt(singleChar);

			if (isNaN(singleChar))
			{
				letterFound = true;
			}
		}

		if (letterFound)
		{
			return true;
		}
		else
		{
			return false;
		}
    	}

	function removeSpaces(strNum)
	{
		currentNumString = strNum;
		newNumString = "";
		singleChar = "";

// loop through each character and remove spaces
		for (i = 0; i < currentNumString.length; i++)
		{
			singleChar = currentNumString.charAt(i);

			if (singleChar == " ")
			{
				singleChar = "";
			}
			newNumString = newNumString + singleChar;
		}
		return newNumString;
	}

// Populate the Hours and Minutes selction lists based on the current time and day
	function populateHoursAndMinutes()
	{
		var time = new Date();
		var day = time.getDay();
		var hour = time.getHours();
		var minute = time.getMinutes();
		var startHour = 0;
		var closingHour = 0;

// check if the customer can call this hour or will have to wait till the next hour
		if (minute > 30)
		{
			startHour = hour + 1;
		}
		else
		{
			startHour = hour;
		}

// check whether it is Saturday and set the closing hour accodingly
		if (day == 6)
		{
			closingHour = 13;
		}
		else
		{
			closingHour = 20;
		}

// loop from the current hour until closing to populate
		for (var i = startHour; i <= closingHour; i++)
		{
			var hourValue = i.toString();

// if the hour is less than 10 then format the hour value so that it is 0x
			if (i < 10)
			{
				hourValue = "0" + hourValue;
			}

			addHourOption(hourValue, false);
		}

// set the hour and minutes selected index to the first elements
		document.call.callbackHour.options[0].selected = true;

// populate the minute options
		repopulateMinutes();

	}

	function addHourOption(text, selected)
	{
		var newIndex = document.call.callbackHour.options.length;
		document.call.callbackHour.options[newIndex] = new Option(text, text, selected);
	}

	function addMinuteOption(text, selected)
	{
		var newIndex = document.call.callbackMinute.options.length;

		document.call.callbackMinute.options[newIndex] = new Option(text, text, selected);
	}

// Repopulate the minutes selection list based on the selected hour
	function repopulateMinutes()
	{
		var selectedIndex = document.call.callbackHour.options.selectedIndex;
		var selectedValue = document.call.callbackHour.options[selectedIndex].value;
		var time = new Date();
		var hour = time.getHours();
		var minute = time.getMinutes();
		var addMinute00 = false;
		var addMinute30 = false;
		var firstHour = "";

// convert the selectedValue from a string to a Int.
// remove a leading zero - parseInt inteprets this as an Octal.
		if (selectedValue.charAt(0) == "0")
		{
			firstHour = parseInt(selectedValue.substring(1, selectedValue.length));
		}
		else
		{
			firstHour = parseInt(selectedValue);
		}

// clear down the Minutes options
		document.call.callbackMinute.options.length = 0;

		if (selectedIndex == 0) // check if the first hour has been selected
		{

// if the first hour is the current hour - check at what point in the hour we are
			if (firstHour == hour)
			{

// check if the minute is 00 then give 00 as an option
				if (minute == 0)
				{
					addMinute00 = true;
				}

// check we are before 30 then give 30 as an option
				if (minute <= 30)
				{
					addMinute30 = true;
				}
			}
			else	// the first hour is the next hour
			{
// if the first hour is the only hour then only give 00
				if (document.call.callbackHour.options.length == 1)
				{
					addMinute00 = true;
				}
				else // it is the next hour but not the final hour
				{
					addMinute00 = true;
					addMinute30 = true;
				}
			}
		}
		else if (selectedIndex == document.call.callbackHour.options.length - 1) // check if the last hour has been selected
		{
// only the exact hour can be selected because the call centre is closed on 30 mins
			addMinute00 = true;
		}
		else // one of the other hours was selected
		{
			addMinute00 = true;
			addMinute30 = true;
		}

		if (addMinute00)
		{
			addMinuteOption("00", false);
		}

		if (addMinute30)
		{
			addMinuteOption("30", false);
		}

		document.call.callbackMinute.options[0].selected = true;
	}

	function moveFocusToHour()
	{
// move the focus to the hour selection list
		document.call.callbackHour.focus();
	}


