// ----------------------------------------------------------------------
// Javascript form validation routines.
// Author: Stephen Poley
//
// Simple routines to quickly pick up obvious typos.
// All validation routines return true if executed by an older browser:
// in this case validation must be left to the server.
//
// Update Aug 2004: have tested that IE 5.0 and IE 5.5 both support DOM model
// sufficiently well, so innerHTML option removed (redundant).
// NOV 2006 edited by matt lovell so it works how i like it adding loads of extra validations!
// ----------------------------------------------------------------------

var nbsp = 160;    // non-breaking space char
var node_text = 3; // DOM text node-type
emptyString = /^\s*$/

// -----------------------------------------
//                  trim
// Trim leading/trailing whitespace off string
// -----------------------------------------

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '')
};

// -----------------------------------------
//                  msg
// Display warn/error message in HTML element
// commonCheck routine must have previously been called
// -----------------------------------------

function msg(fld,     // id of element to display message in
             msgtype, // class to give element ("warn" or "error")
             message) // string to display
{
  // setting an empty string can give problems if later set to a 
  // non-empty string, so ensure a space present. (For Mozilla and Opera one could 
  // simply use a space, but IE demands something more, like a non-breaking space.)
  var dispmessage;
  if (emptyString.test(message)) {
	 // alert(fld+" has passed");
    dispmessage = String.fromCharCode(nbsp);  }  
  else  {
	 // alert(fld+" has failed");
    dispmessage = message;
  }
  var elem = document.getElementById(fld);
  elem.firstChild.nodeValue = dispmessage;  
  
  elem.className = msgtype;
};

// -----------------------------------------
//            commonCheck
// Common code for all validation routines to:
// (a) check for older / less-equipped browsers
// (b) check if empty fields are required
// Returns true (validation passed), 
//         false (validation failed) or 
//         proceed (don't know yet)
// -----------------------------------------

var proceed = 2;  

function commonCheck    (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd, fldf)   // true if required
{
  if (!document.getElementById) 
    return true;  // not available on this browser - leave validation to the server
  var elem = document.getElementById(ifld);
  if (!elem.firstChild)
    return true;  // not available on this browser 
  if (elem.firstChild.nodeType != node_text)
    return true;  // ifld is wrong type of node  

  if (emptyString.test(vfld.value)) {
    if (reqd) {
      msg (ifld, "error", "** this field required **");  
		if (fldf){vfld.focus();}
      return false;
    }
    else {
      msg (ifld, "warn", "");   // OK
      return true;  
    }
  }
  return proceed;
}

// -----------------------------------------
//            validatePresent
// Validate if something has been entered
// Returns true if so 
// -----------------------------------------

function validatePresent(vfld,   // element to be validated
                         ifld,fldf )  // id of element to receive info/error msg
{
  var stat = commonCheck (vfld, ifld, true,fldf);
  if (stat != proceed) return stat;

  msg (ifld, "warn", "");  
  return true;
};

// -----------------------------------------
//               validateEmail
// Validate if e-mail address
// Returns true if so (and also if could not be executed because of old browser)
// -----------------------------------------

function validateEmail  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd,fldf)   // true if required
{
  var stat = commonCheck (vfld, ifld, reqd,fldf);
  if (stat != proceed) return stat;

  var tfld = trim(vfld.value);  // value of field with whitespace trimmed off
  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/
  if (!email.test(tfld)) {
    msg (ifld, "error", "** not a valid e-mail address **");
				if (fldf){vfld.focus();}
    return false;
  }

  var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/
  if (!email2.test(tfld)) 
    msg (ifld, "warn", "** unusual e-mail address - please check **");
  else
    msg (ifld, "warn", "");
  return true;
};


// -----------------------------------------
//            validateTelnr
// Validate telephone number
// Returns true if so (and also if could not be executed because of old browser)
// Permits spaces, hyphens, brackets and leading +
// -----------------------------------------

function validateTelnr  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd, fldf)   // true if required
{
  var stat = commonCheck (vfld, ifld, reqd, fldf);
  if (stat != proceed) return stat;

  var tfld = trim(vfld.value);  // value of field with whitespace trimmed off
  var telnr = /^\+?[0-9 ()-]+[0-9]$/
  if (!telnr.test(tfld)) {
    msg (ifld, "error", "** not a valid telephone number. Characters permitted are digits, space ()- and leading + **");
				if (fldf){vfld.focus();}
    return false;
  }

  var numdigits = 0;
  for (var j=0; j<tfld.length; j++)
    if (tfld.charAt(j)>='0' && tfld.charAt(j)<='9') numdigits++;

  if (numdigits<7) {
    msg (ifld, "error", "** " + numdigits + " digits - Please use full number including area code i.e. 01981 251 123 or a mobile i.e. 07941 123 456. **");
				if (fldf){vfld.focus();}
    return false;
  }

  if (numdigits>11)
    msg (ifld, "warn", "** Warning too long - UK numbers are 11 digits please amend");
  else { 
    if (numdigits<11)
      msg (ifld, "warn", "** Only " + numdigits + " digits - UK numbers are 11 digits, please amend **");
    else
      msg (ifld, "warn", "");
  }
  return true;
};


// added to check if name comprises of two parts

function validateFullName(vfld,   // element to be validated
                        ifld, fldf )  // id of element to receive info/error msg
{
 var stat = commonCheck (vfld, ifld, true, fldf);
 if (stat != proceed) return stat;

//code addition

if (vfld.value.split(" ").length < 2) {

msg (ifld, "error", "** Full Name should comprise of at least 2 parts **");
				if (fldf){vfld.focus();}
return false;
}

//

 msg (ifld, "warn", "");
 return true;
};


function validateSelfCertify(ifld)
{
    if (document.getElementById('selfcert-yes').checked == false && document.getElementById('selfcert-no').checked == false) {
        msg (ifld, "error", "** please select one of the above **");
        return false;
    }
    else
    {
        msg (ifld, "warn", "");
        return true;
    };
};

//added to check if anything been selected
function validateMortgageTypeOptions(vfld,   // element to be validated
                        ifld )  // id of element to receive info/error msg
{
//alert(vfld.selectedIndex);
//if (1 == 2) {
if (vfld.selectedIndex == 0) {
    msg (ifld, "error", "** the above field is required **");
    //alert("arse");
    return false;
    }
else {
    //alert("apparently 1 does equal 2");
    msg (ifld, "warn", "");
    return true;
    };
// }
};

function validateAmount(vfld,   // element to be validated
                         ifld, fldf )  // id of element to receive info/error msg
{
  var stat = commonCheck (vfld, ifld, true, fldf);
  if (stat != proceed)
  return stat;

  var tfld = trim(vfld.value);  // value of field with whitespace trimmed off
  if (tfld=="£0") { // make sure we don't have 0 pounds
    msg (ifld, "error", "** please enter an amount **");
				if (fldf){vfld.focus();}
    return false;
  }
  
  msg (ifld, "warn", ""); 
  return true;
}
function validateRadio(oRadGrp, ifld)
{
var checked=false;
for (var i=0;i<oRadGrp.length;i++){
if(oRadGrp[i].checked)
{
  msg (ifld, "error", "");
  checked=true;
  return true;
}
}
if (!checked) {
  msg (ifld, "error", "** the field is required **");
  return false; 
}
}
function validateCheck(vfld,   // element to be validated
                         ifld )  // id of element to receive info/error msg
{
  var elem = document.getElementById(vfld);
  if (!elem.checked) {
  msg (ifld, "error", "** this must be checked **");
  return false;  
  } else {
  msg (ifld, "error", ""); 
  return true;
  }
}

function validateCompare(vfld,   // element to be validated
                         sfld,   // element to be validated
                         ifld )  // id of element to receive info/error msg
{
  var elem = document.getElementById(vfld);
  var elem2 = document.getElementById(sfld);
  if (elem.value==elem2.value) {
  msg (ifld, "error", "");
  return true;  
  } else {
  msg (ifld, "error", "** this must match "+sfld+" **"); 
  return false;
  }
}



function valButton(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return btn[cnt].value;
    else return false;
}
