
<!--
function WAtrimIt(theString,leaveLeft,leaveRight)  {
  if (!leaveLeft)  {
    while (theString.charAt(0) == " ")
      theString = theString.substring(1);
  }
  if (!leaveRight)  {
    while (theString.charAt(theString.length-1) == " ")
      theString = theString.substring(0,theString.length-1);
  }
  return theString;
}

function WAFV_GetValueFromInputType(formElement,inputType,trimWhite) {
  var value="";
  if (inputType == "select")  {
    if (formElement.selectedIndex != -1 && formElement.options[formElement.selectedIndex].value && formElement.options[formElement.selectedIndex].value != "") {
      value = formElement.options[formElement.selectedIndex].value;
    }
  }
  else if (inputType == "checkbox")  {
    if (formElement.length)  {
      for (var x=0; x<formElement.length ; x++)  {
        if (formElement[x].checked && formElement[x].value!="")  {
          value = formElement[x].value;
          break;
        }
      }
    }
    else if (formElement.checked)
      value = formElement.value;
  }
  else if (inputType == "radio")  {
    if (formElement.length)  {
      for (var x=0; x<formElement.length; x++)  {
        if (formElement[x].checked && formElement[x].value!="")  {
          value = formElement[x].value;
          break;
        }
      }
    }
    else if (formElement.checked)
      value = formElement.value;
  }
  else if (inputType == "radiogroup")  {
    for (var x=0; x<formElement.length; x++)  {
      if (formElement[x].checked && formElement[x].value!="")  {
        value = formElement[x].value;
        break;
      }
    }
  }
  else  {
    var value = formElement.value;
  }
  if (trimWhite)  {
    value = WAtrimIt(value);
  }
  return value;
}

function WAAddError(formElement,errorMsg,focusIt,stopIt)  {
  if (document.WAFV_Error)  {
	  document.WAFV_Error += "\n" + errorMsg;
  }
  else  {
    document.WAFV_Error = errorMsg;
  }
  if (!document.WAFV_InvalidArray)  {
    document.WAFV_InvalidArray = new Array();
  }
  document.WAFV_InvalidArray[document.WAFV_InvalidArray.length] = formElement;
  if (focusIt && !document.WAFV_Focus)  {
	document.WAFV_Focus = focusIt;
  }

  if (stopIt == 1)  {
	document.WAFV_Stop = true;
  }
  else if (stopIt == 2)  {
	formElement.WAFV_Continue = true;
  }
  else if (stopIt == 3)  {
	formElement.WAFV_Stop = true;
	formElement.WAFV_Continue = false;
  }
}

function WAValidateRQ(formElement,errorMsg,focusIt,stopIt,trimWhite,inputType)  {
  var isValid = true;
  if (!document.WAFV_Stop && !formElement.WAFV_Stop)  {
    var value=WAFV_GetValueFromInputType(formElement,inputType,trimWhite);
    if (value == "")  {
	    isValid = false;
    }
  }
  if (!isValid)  {
    WAAddError(formElement,errorMsg,focusIt,stopIt);
  }
}
function WAValidateEM(formElement,value,errorMsg,focusIt,stopIt,required) {
  var isValid = true;
  if ((!document.WAFV_Stop && !formElement.WAFV_Stop) && !(!required && value==""))  {
    var knownDomsPat = /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
    var emailPat = /^(.+)@(.+)$/;
    var accepted = "\[^\\s\\(\\)><@,;:\\\\\\\"\\.\\[\\]\]+";
    var quotedUser = "(\"[^\"]*\")";
    var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
    var section = "(" + accepted + "|" + quotedUser + ")";
    var userPat = new RegExp("^" + section + "(\\." + section + ")*$");
    var domainPat = new RegExp("^" + accepted + "(\\." + accepted +")*$");
    var theMatch = value.match(emailPat);
    var acceptedPat = new RegExp("^" + accepted + "$");
    var userName = "";
    var domainName = "";
    if (theMatch==null) {
      isValid = false;
    }
    else  {
      userName = theMatch[1];
      domainName = theMatch[2];
	  var domArr = domainName.split(".");
	  var IPArray = domainName.match(ipDomainPat);
      for (x=0; x < userName.length; x++) {
        if ((userName.charCodeAt(x) > 127 && userName.charCodeAt(x) < 192) || userName.charCodeAt(x) > 255) {
          isValid = false;

        }
      }
      for (x=0; x < domainName.length; x++) {
        if ((domainName.charCodeAt(x) > 127 && domainName.charCodeAt(x) < 192) || domainName.charCodeAt(x) > 255) {
          isValid = false;
        }
      }
      if (userName.match(userPat) == null) {
        isValid = false;
      }
      if (IPArray != null) {
        for (var x=1; x<=4; x++) {
          if (IPArray[x] > 255) {
            isValid = false;
          }
        }
      }
      for (x=0; x < domArr.length; x++) {
        if (domArr[x].search(acceptedPat) == -1 || domArr[x].length == 0 || (domArr[x].length < 2 && x >= domArr.length-2)) {
          isValid = false;
        }
      }
      if (domArr[domArr.length-1].length !=2 && domArr[domArr.length-1].search(knownDomsPat) == -1) {
        isValid = false;
      }
      if (domArr.length < 2) {
        isValid = false;
      }
    }
  }
  if (!isValid)  {
    WAAddError(formElement,errorMsg,focusIt,stopIt);
  }
}
function WAAlertErrors(errorHead,errorFoot,setFocus,submitForm)  { 
  if (!document.WAFV_StopAlert)  { 
	  document.WAFV_StopAlert = true;
	  if (document.WAFV_InvalidArray)  {  
	    document.WAFV_Stop = true;
        var errorMsg = document.WAFV_Error;
	    if (errorHead!="")
		  errorMsg = errorHead + "\n" + errorMsg;
		if (errorFoot!="")
		  errorMsg += "\n" + errorFoot;
		document.MM_returnValue = false;
		if (document.WAFV_Error!="")
		  alert(errorMsg.replace(/&quot;/g,'"'));
		else if (submitForm)
		  submitForm.submit();
	    if (setFocus && document.WAFV_Focus)  {
		  document.tempFocus = document.WAFV_Focus;
          setTimeout("document.tempFocus.focus();setTimeout('document.WAFV_Stop = false;document.WAFV_StopAlert = false;',1)",1); 
        }
        else {
          document.WAFV_Stop = false;
          document.WAFV_StopAlert = false;
        }
        for (var x=0; x<document.WAFV_InvalidArray.length; x++)  {
	      document.WAFV_InvalidArray[x].WAFV_Stop = false;
	    }
	  }
	  else  {
        document.WAFV_Stop = false;
        document.WAFV_StopAlert = false;
	    if (submitForm)  {
	      submitForm.submit();
	    }
	    document.MM_returnValue = true;
	  }
      document.WAFV_Focus = false;
	  document.WAFV_Error = false;
	  document.WAFV_InvalidArray = false;
  }
}
//-->

function validateContactForm(thisform) {
	var name = document.getElementsByName('name:first');
	if (name[0].value == "") {
		alert('Please enter your first name.');
		return false;
	}
	var surname = document.getElementsByName('name:last');
	if (surname[0].value == "") {
		alert('Please enter your surname.');
		return false;
	}
	var org_name = document.getElementsByName('org-name');
	if (org_name[0].value == "" && thisform.elements["org-type"].selectedIndex == 1) {
		alert('Please enter your organisation name.');
		return false;
	}
	var address = document.getElementById('address-input-0');
	if (address.value == "") {
		alert('Please enter your address.');
		return false;
	}
	var town = document.getElementsByName('address:city');
	if (town[0].value == "") {
		alert('Please enter your town.');
		return false;
	}
	var county = document.getElementsByName('address:county');
	if (county[0].value == "") {
		alert('Please enter your County.');
		return false;
	}
	var postcode = document.getElementsByName('address:postcode');
	if (postcode[0].value.indexOf(" ") <= 0) {
		alert('Please enter a postcode (including a space).');
		return false;
	}
	var phone = document.getElementsByName('phone:default');
	if (phone[0].value == "") {
		alert('Please enter your phone number.');
		return false;
	}
	var email = document.getElementById('email-input-0');
	if(email && validate_email(email, 'Please enter a valid e-mail address.') == false) {
		return false;
	}
	var pass = document.getElementById('password-input-0');
	var pass2 = document.getElementById('password-input-1');
	if(pass) {
		if(pass.value == "" || pass.value.length < 6) {
			alert('Please enter a passwords with a length of 6 or more characters.');
			return false;
		}
		if(pass2.value == "") {
			alert('Please confirm password.');
			return false;
		}
		if (pass.value != pass2.value) {
			alert('Password does not match.');
			return false;
		}
	}
	var birthYear = document.getElementById('dob-y-select-0');
	if(birthYear && birthYear.value == "2002") {
		alert('Please select your date of birth.');
		return false;
	}
	var sec_answer = document.getElementsByName('security-answer');
	if(sec_answer.length > 0) {
		if(sec_answer[0].value == "") {
			alert('Please enter your security answer.');
			return false;
		}
	}
	var source_sel = document.getElementById('source-select-1');
	if(source_sel) {
		if(source_sel.value == "Please select") {
			alert('Please select how you heard about us.');
			return false;
		}
	}
	return true;
}

/* ------------------------------ */
function checkDomaina() {
	var domain_val = document.getElementsByName('domain');
	if (domain_val[0].value.length == 0 || domain_val[0].value == 'Domain Name Search') {
		alert('Please enter a domain name to search for.');
		return false;
	}
	if(domain_val[0].value.substr(0, 7) == "http://" || domain_val[0].value.substr(0, 4) == "www.") {
		alert('Please remove http:// and www. (Domain Name Search)');
		return false;
	}
	return true;
}


function checkFakeDomain() {
	var domain_val = document.getElementsByName('fake-domain');
	if (domain_val[0].value.length == 0 || domain_val[0].value == 'Enter Domain Name') {
		alert('Please enter a domain name to search for.');
		return false;
	}
	if(domain_val[0].value.substr(0, 7) == "http://" || domain_val[0].value.substr(0, 4) == "www.") {
		alert('Please remove http:// and www. (Enter Domain Name)');
		return false;
	}
	return true;
}

function checkDomain() {
	var domain_val = document.getElementById('bum');
	if (domain_val[0].value.length == 0 || domain_val[0].value == 'Enter Domain Name') {
		alert('Please enter a domain name to search for.');
		return false;
	}
	if(domain_val[0].value.substr(0, 7) == "http://" || domain_val[0].value.substr(0, 4) == "www.") {
		alert('Please remove http:// and www. (Enter Domain Name)');
		return false;
	}
	return true;
}
/* ------------------------------ */
function validate_email(field, alerttxt) {
	with (field) {
		apos = value.indexOf("@");
		dotpos = value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) {
			alert(alerttxt);
			return false;
		} else {
			return true;
		}
	}
}

/* ------------ LOGIN form ----------- */
function validateLoginForm(thisform) {
	with (thisform) {
		if (validate_email(username, "Please enter a valid e-mail address.") == false) {
			username.focus();
			return false;
		}
	}
}

/* redirect to password reminder if empty login/pass (user_login.php page) */
function checkLoginFields() {
	var username = document.getElementById("username");
	var password = document.getElementById("password");
	if(username.value == "" && password.value == "") {
		window.location = "https://www.tudorhosting.co.uk/customer/password-reminder";
		return false;
	}
	return true;
}