// Javascript Functions Go Here

// Validation Functions
var submitted = 0;

function FormValidation(form)
{
	if (submitted)
	{
   		document.getElementById("Submitted").style.display="block";
   		return false;
	}
	
	// check if numbers field is blank
	if (form.TelNumber.value == "")
	{
		document.getElementById("TelephoneBlank").style.display="block";
		document.getElementById("TelNumberhelp").style.display="block";
		document.getElementById("TelNumberSection").style.borderColor="#FF0000";
		form.TelNumber.focus();
		return (false);
	}

	// only allow numbers to be entered
	var checkOK = "0123456789 ";
	var checkStr = form.TelNumber.value;
	var allValid = true;
	var allNum = "";
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
		break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
		if (ch != ",")
			allNum += ch;
		
		// Check for numbers prefixed with 00
		var pattern = new RegExp("^0[1-9]");
		if(!pattern.test(checkStr)) 
			allValid = false;
	}

	if (!allValid)
	{
		document.getElementById("TelephoneNumeric").style.display="block";
		document.getElementById("TelNumberhelp").style.display="block";
		document.getElementById("TelNumberSection").style.borderColor="#FF0000";
		form.TelNumber.focus();
		return (false);
	}

	if (!submitted)
	{
   		form.doSubmit.disabled=true;
   		submitted = 1;
   		form.submit();
	}
} 

function setElement(value, form)
{
	if (value == "enable")
	{
		form.ScheduleDay.disabled=false;
		form.ScheduleTime.disabled=false;
	}
	else
	{
		form.ScheduleDay.disabled=true;
		form.ScheduleTime.disabled=true;
	}

}

function AcceptTerms(form)
{
	if (form.Accept.checked == 1)
	{
		form.doSubmit.disabled=false;
	}
	else
	{
		form.doSubmit.disabled=true;
	}
}

// Custom CallOk Status
function getValue(varname)
{
	// First, we load the URL into a variable
  	var url = window.location.href;

  	// Next, split the url by the ?
  	var qparts = url.split("?");

  	// Check that there is a querystring, return "" if not
  	if (qparts.length == 0)
  	{
    		return "";
  	}

  	// Then find the querystring, everything after the ?
  	var query = qparts[1];

  	// Split the query string into variables (separates by &s)
  	var vars = query.split("&");

  	// Initialize the value with "" as default
  	var value = "";

  	// Iterate through vars, checking each one for varname
  	for (i=0;i<vars.length;i++)
  	{
    		// Split the variable by =, which splits name and value
    		var parts = vars[i].split("=");
    
    		// Check if the correct variable
    		if (parts[0] == varname)
    		{
      			// Load value into variable
      			value = parts[1];
      			// End the loop
      			break;
    		}
  	}
  
  	// Convert escape code
  	value = unescape(value);

  	// Convert "+"s to " "s
  	value.replace(/\+/g," ");

  	// Return the value
  	return value;
}
