
//do the JS date functions to set the date of the today/tomorrow option fields
var todayDate = new Date();
var tomorrowDate = new Date();
tomorrowDate.setDate(tomorrowDate.getDate()+1);
var todDay = todayDate.getDate();
var todMonth = todayDate.getMonth() + 1;
var tomDay = tomorrowDate.getDate();
var tomMonth = tomorrowDate.getMonth() + 1;

if (todDay < 10) todDay= "0" + todDay;
if (todMonth < 10) todMonth= "0" + todMonth;
if (tomDay < 10) tomDay= "0" + tomDay;
if (tomMonth < 10) tomMonth= "0" + tomMonth;

//active the fields, incase of a back click
//if (document.forms[0].Delay[4].checked) activate(); 

//set the today and tomorrow field dates as defined above

//document.forms[0].dates.options[0].value = todDay + "-" + todMonth + "-" + todayDate.getFullYear();
//document.forms[0].dates.options[1].value = tomDay + "-" + tomMonth + "-" + tomorrowDate.getFullYear();


//if the callMe parameter exists
if (getURLParam("callMe").length > 0) {
	var thisMsg = URLDecode(getURLParam("MSG"));

	//if there is a time defined
	if (getURLParam("time").length > 0) {
		// Scheduled Time/Date
		var calltime = getURLParam("time");
		calltime = calltime.replace(/[+]/g," ").replace("%3a",":").replace("%2c","");
		var hour = calltime.slice(0,2);
		var minute = calltime.slice(3,5);
		var date2call = calltime.slice(6);
		var year = date2call.slice(0,4);
		var month = date2call.slice(5,7);
		var day = date2call.slice(8,10);

		switch(month) {
			case("01"):
				var monthname = ", January";
				break;
			case("02"):
				var monthname = ", February";
				break;
			case("03"):
				var monthname = ", March";
				break;
			case("04"):
				var monthname = ", April"
				break;
			case("05"):
				var monthname = ", May"
				break;
			case("06"):
				var monthname = ", June"
				break;
			case("07"):
				var monthname = ", July"
				break;
			case("08"):
				var monthname = ", August"
				break;
			case("09"):
				var monthname = ", September"
				break;
			case("10"):
				var monthname = ", October"
				break;
			case("11"):
				var monthname = ", November"
				break;
			case("12"):
				var monthname = ", December"
				break;
			default : var monthname = "*";
		}

		thisMsg += " at ";

		if (hour > 12) {
			hour -= 12;
			tag = "pm";
		} else {
			tag = "am";
		}

		thisMsg = thisMsg.replace("in united kingdom (not london).", " at " + hour + ":"+ minute + tag + monthname + " " + day + " " + year);
	} else {
		thisMsg = thisMsg.replace("in united kingdom (not london).", "");
	}

	thisMsg = thisMsg.replace("thank you for requesting further information via national learning advice line.<\/p>", "Thank you, one of our advisers will contact you ");
	thisMsg = thisMsg.replace("  <p>", "");
	thisMsg = thisMsg.replace("<\/p>", "");
	thisMsg = thisMsg.replace("telephone number ", "");
	thisMsg = thisMsg.replace("click here to return.","");
	thisMsg = thisMsg.replace("<a href=\"javascript:history.back(1)\">click here<\/a> to re-enter your details","");
	thisMsg = thisMsg.replace("you will shortly be called by national learning advice line", " shortly");
	thisMsg = thisMsg.replace("you will be called by national learning advice line ", "");

	if (getURLParam("time").length > 0) thisMsg = thisMsg.replace("shortly ", "");

	//replace the main content area with the new submitted message
	document.getElementById('callMeArea').innerHTML = thisMsg;
}

//gets the msg parameters etc from the url
function getURLParam(strParamName){
	strParamName = strParamName.toLowerCase();
  var strReturn = "";
  var strHref = window.location.href;
	strHref = strHref.toLowerCase();

	if ( strHref.indexOf("?") > -1 ) {
		var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
		var aQueryString = strQueryString.split("&");

		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ) {
			if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ) {
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}

	return strReturn;
}

function URLDecode(txt) {
  // Replace + with ' '
  // Replace %xx with equivalent character
  // Put [ERROR] in output if %xx is invalid.
  var HEXCHARS = "0123456789ABCDEFabcdef"; 
  var encoded = txt;
  var plaintext = "";
  var i = 0;

  while (i < encoded.length) {
	  var ch = encoded.charAt(i);
	  if (ch == "+") {
		  plaintext += " ";
		  i++;
	  } else if (ch == "%") {
			if (i < (encoded.length-2) && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				//alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		  plaintext += ch;
		  i++;
		}
	}

  return plaintext;
}





function checkForm() {
	var ukNumber = new RegExp("^0[1-9]");
	var validPhone = new RegExp("^[0-9 ]*$");

	if (!document.getElementById("termsAgreed").checked) {
		alert("You must agree to the terms and conditions to continue");
		return false;
	} else if (document.forms[0].TelNumber.value.length == 0) {
		//ensure the telephone number has been entered
		alert("Please enter a \"Telephone Number\".");
		document.forms[0].TelNumber.focus();
		return false;
	} else if (document.forms[0].TelNumber.value.length < 6) {
		//and thats it is at least 6 digits
		alert("Telephone number cannot be less than 6 Characters");
		document.forms[0].TelNumber.focus();
		return false;
	} else if (!validPhone.test(document.forms[0].TelNumber.value)) {
		// only allow numbers to be entered
		alert("Please enter only numeric characters for the \"Telephone Number\".");
		document.forms[0].TelNumber.focus();
		return false;
	} else if (!ukNumber.test(document.forms[0].TelNumber.value)) {
		// Check for numbers prefixed with 00 (UK only Services)
		alert("Unfortunately we can only contact UK-based telephone numbers.");
		document.forms[0].TelNumber.focus();
		return false;
	}

	//if we've made it this far then there are no errors!
	return true;
}
