var nbsp = 160;		// non-breaking space char
var node_text = 3;	// DOM text node-type
var emptyString = /^\s*$/ ;
var msg="";
var global_valfield;	// retain valfield for timer thread
// Trim leading/trailing whitespace off string

function trim(str){
  return str.replace(/^\s+|\s+$/g, '');
}
// Delayed focus setting to get around IE bug
function setFocusDelayed(){
  global_valfield.focus();
}
function setfocus(valfield){
  // save valfield in global variable so value retained when routine exits
  global_valfield = valfield;
  setTimeout('setFocusDelayed()', 100 );
}
// Validate if something has been entered (element to be validated,id of element to receive info/error msg) || (emptyString.test(valfield))
function validateName(valfield){
//alert ("index: " + valfield.value.indexOf("enter") + " empty: " + emptyString.test(valfield.value));
 if ((!(valfield.value.indexOf("enter")==-1)) || (emptyString.test(valfield.value))){ 
    msg += "Enter your full name\n";
    return false;
	}else{return true;}
}
// Validate if e-mail address (element to be validated,id of element to receive info/error msg,true if required)
function validateEmail(valfield,required){
//alert("valfield: " + valfield + " required: " + required);
  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
  if (!email.test(tfld)) {
    msg += "Not a valid e-mail address\n";
    return false;
  }else{return true;}
}
//validate select list
function validateState(valfield,required){
msg="";
if (!valfield.selectedIndex >0 ){
    msg += "Select your preferred state\n";
    return false;
    }else{return true;}
}
//validate select list
function validateAMT(valfield,required){
if (!valfield.selectedIndex > 0 )
    {
    msg += "Subject to AMT?\n";
        return false;
    }else{return true;}
}

// Returns true if so (and also if could not be executed because of old browser)(element to be validated,id of element to receive info/error msg,true if required)
function validateTelnr(valfield,required){
  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var telnr = /^\+?[0-9 ()-]+[0-9]$/  ;
  if (!telnr.test(tfld)) {
    msg += "Not a valid telephone number. Characters permitted are digits, space ()- and leading +\n";
    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<6) {
    msg += "Not a valid telephone number - too short\n";
    return false;
  }
  return true;
}
//(checkbox to be validated,id of element to receive info/error msg)
function validateConfirm(vfld,ifld){
  var stat = commonCheck2(vfld, ifld);
  if (stat != proceed) return stat;
  if (vfld.checked) return true;
  // if we get here then the validation has failed
  var errorMsg = 'Please read the above message and confirm you agree to it';
  msg (ifld, "error", errorMsg);
  return false;
}

function validateCheckOfferings(vform){
msg="";
  if (!(vform.send_me_offerings.checked) && !(vform.send_me_strategies.checked))  {
    msg += "Please check at least one of the opt-in boxes for customized offerings and bond strategies.\n";
        return false;
    }else{return true;}
   }

  function validateOnSubmit(thisform){
    var elem;
    var errs=0;
    if (!validateCheckOfferings(thisform)){errs += 1; }
    //if (!validateState(thisform.infostate,true)){errs += 1; }
    //if (!validateAMT(thisform.amt,true)){errs += 1; }
    if (!validateEmail(thisform.email,true)){errs += 1; } 
    //if (!validateName(thisform.fullname)){errs += 1; }
    //if (!validateTelnr(thisform.phone,true)){errs += 1; } 
    if (!errs==0)  alert(msg);
	//alert("errorcount: " + errs)
    return (errs==0);
  };


//clear form boxes when clicked

function clear_leadform1()
{
		if (document.contact.email.value == " enter your email address")  {
		document.contact.email.value = "";
	}
}	


function clear_leadform2()
{
		if (document.contact.fullname.value == " enter your full name")  {
		document.contact.fullname.value = "";
	}
}	

function clear_leadform3()
{
		if (document.contact.phone.value == " enter phone number")  {
		document.contact.phone.value = "";
	}
}
