var intTotal=0; //Global variable used in calculateUsedCharacters & SMSCharsRemaining.
function calculateUsedCharacters()
{
	var strName = document.getElementById('IDName').value.length;
	var strPhone = document.getElementById('IDPhone').value.length;
	var strSuburb = document.getElementById('IDSuburb').options[document.getElementById('IDSuburb').selectedIndex].value.length;
	var strProblem = document.getElementById('IDProblem').value.length;
	intTotal = strName + strPhone + strSuburb + strProblem;
}
 
function SMSCharsRemaining()
{ 
	calculateUsedCharacters(); // Calculate the total of characters used in all fields.
	if (intTotal > 157) //In the event, we have exceeded 160 total characters (160 is the SMS limit), shorten the problem field.
	{
		alert("You have entered too many characters. The problem field will now be truncated.");
		document.getElementById('IDProblem').value = document.getElementById('IDProblem').value.substring(0, 157 - document.getElementById('IDName').value.length - document.getElementById('IDPhone').value.length - document.getElementById('IDSuburb').options[document.getElementById('IDSuburb').selectedIndex].value.length);
		calculateUsedCharacters();
	}
	document.getElementById('IDcharsremain').innerHTML = ("157" - intTotal + " Characters Remaining");	

}

function processSMS()
{
	// Error Checking - Code Start:
	// Here, we ensure all fields have been completed. If not, an error
	// message is displayed. I'm confident in the error checking of the
	// character limit as this is done 'on the fly' as the text boxes
	// items are changed.
	if (changeCase(frmBookOnline.txtName.value, 'IDName') == true)
	{
		if (checkPhoneNumber(frmBookOnline.txtPhone.value, 'IDPhone') == true)
		{
			if (document.getElementById('IDProblem').value.length < 10)
			{
				alert("Please enter a detailed description of the problem.");
			}
			else
			{
				// Error Check is finally complete. Here, you can now begin to send the SMS.
				if (confirm("Your booking is about to be sent directly to beeconnected via SMS.\nPlease wait a moment while the SMS Gateway is contacted...") == "1")
				{
					sendSMS(document.getElementById('IDName').value + "," + document.getElementById('IDPhone').value + "," + document.getElementById('IDSuburb').options[document.getElementById('IDSuburb').selectedIndex].value + "," + document.getElementById('IDProblem').value);
				}
				else
				{
					alert("Ooops! Don't stop now, we want to hear from you!\nPlease feel free to call us to discuss your needs.");
				}
			}
		}
	}
}

var isWorking = false;

var http = getHTTPObject();


function handleHttpResponse() {
  if (http.readyState == 4)
  {
    isWorking = false;
    if (http.responseText.indexOf('invalid') == -1){
	alert(http.responseText);
	}
  }
}


function sendSMS(data)
{
  if (!isWorking) {
	http.open("GET", "smsprocess.php?data=" + data, true);
	isWorking = true;
	http.onreadystatechange = handleHttpResponse;
	http.send(null);
  }
}
