// EMK VALIDATOR v2.10

// EMK VALIDATOR now uses more up-to-date and efficent methods to validate forms - Craig

// DO NOT EDIT -- All validation routines go into validation.js

// v2.10 - Email validation now accounts for common mistakes with mainly .co.uk domains!

// for forms that do not require validating
function EMKnocheck(n){
	return true
}

// Remove single and double quotes from a string
function cleanString(formnum,fieldname){
	var val 	= eval("document.forms[formnum]."+fieldname+".value");
	var newVal	= val.replace(/[\n]+/g,'\\n');				// The eval() was crashing because it was taking LITERAL new-lines. This replaces newlines with an escaped char, this can then be parsed by the eval().
	newVal		= newVal.replace(/[\r]+/g,'\\r');
	newVal		= newVal.replace(/\'/g,"");
	newVal 		= newVal.replace(/\"/g,'');
	val		= eval("document.forms[formnum]."+fieldname+".value='" + newVal +"'");
}

// Remove all blanks from a string
function blankout(strname){
  var regEx = new RegExp(/\s/g);
  strname = strname.replace(regEx,'');
  return strname ;
}

// Process telephone number for checking
function formatTel(text){
	var regEx = new RegExp(/\(/g);
  	text = text.replace(regEx,'');
	var regEx = new RegExp(/\)/g);
  	text = text.replace(regEx,'');
	var regEx = new RegExp(/\-/g);
  	text = text.replace(regEx,'');
	text = blankout(text);
	return text;
}

// Validate telephone/fax number
function vt(formnum,fieldname,needed,inputtext) {
  var vble = eval("document.forms[formnum]."+fieldname+".value");
  if (needed.toUpperCase() == "N" && vble == ""){ ; // Allow blank telephone/fax number
  	t_or_f = true;
  }else{
	  var regEx = new RegExp(/^((\+)|0)+([0-9]{10,15})+$/);
	  var t_or_f = true;
	  vble = formatTel(vble);
	  if(!regEx.exec(vble)){
	  	t_or_f = false;
		alert(inputtext + ' is not a valid phone number.');
	  }
  }
  return t_or_f ;
}

// Validate e-mail address
function ve(formnum,fieldname,needed) {
  var t_or_f = true;
  var val    = eval("document.forms[formnum]."+fieldname+".value");
  var regEx  = new RegExp(/,/g);
  var newVal = val.replace(regEx,'.'); // Replace commas with dots
  var val    = eval("document.forms[formnum]."+fieldname+'.value="' + newVal +'"');
  var email  = eval("document.forms[formnum]."+fieldname+".value");
  
  email = blankout(email);
  email = email.toLowerCase();
  
  if(needed.toUpperCase() == 'N' && email == ''){
  	t_or_f = true;
  }else{
  	// Step One (Does it look like an email?)
  	 var regEx  = new RegExp(/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([\.a-zA-Z0-9_-])+\.[a-z\.]{2,6}$/);
	 if(!regEx.exec(email)){
	  	t_or_f = false;
		if(email == '' || email == null){
			alert('Please enter an email address.');
		}else{
			alert(email + ' is an invalid email address.');
		}
	 }
	// Step Two (Check for common mistakes)
	if(t_or_f){
		// Dual dot domains (co.uk, org.uk, me.uk) not beginning with a '.' (Still valid email format but we know it's wrong)
		// Example: admin@emarketers-co.uk, admin@emarketers_org.uk, admin@emarketersco.uk
		// Check 3rd character from the left, is it a dot and does it end in 'uk' 
		// (There are no single dot domains ending in .uk so we can safley validate)
		if(email.charAt(email.length-3) == '.' && (email.charAt(email.length-2) == 'u' && email.charAt(email.length-1) == 'k')){
			// Now two character domain extentions do exist so as far as we are concerned, the email is still valid
			// So are the next two characters to the left 'co'?
			if((email.charAt(email.length-5) == 'c' && email.charAt(email.length-4) == 'o')){
				// We now know that the domain ends in co.uk 
				// So is the next character to the left a dot?
				if(email.charAt(email.length-6) != '.'){
					t_or_f = false;
					alert(email + ' is invalid, there is no dot detected before co.uk');
				}
			}
			if((email.charAt(email.length-5) == 'm' && email.charAt(email.length-4) == 'e')){
				// We now know that the domain ends in me.uk 
				// So is the next character to the left a dot?
				if(email.charAt(email.length-6) != '.'){
					t_or_f = false;
					alert(email + ' is invalid, there is no dot detected before me.uk');
				}
			}
			// Alright are the next three characters to the left 'org'?
			if(email.charAt(email.length-6) == 'o' && email.charAt(email.length-5) == 'r' && email.charAt(email.length-4) == 'g'){
				// We now know that the domain ends in org.uk
				// So is the next character to the left a dot?
				if(email.charAt(email.length-7) != '.'){
					t_or_f = false;
					alert(email + ' is invalid, there is no dot detected before org.uk');
				}
			}
		}
		// Note: There are many more that we could try to solve but the more we do the more danger some valid domains
		// 	 Will not pass either! (for example if we try to fix me.com, legitimate domains like somthing@some.com would be rejected!) 
	}
  }
  return t_or_f;
}

// Ensure string has minimum length
function vl(formnum,fieldname,minlength,inputtext) {
  var valid = true;
  var vble = eval("document.forms[formnum]."+fieldname+".value");
//window.alert("Value is >"+vble+"<   Length is >"+vble.length+"<");
  
  cleanString(formnum,fieldname);
  
  if(vble.length < minlength) {
    valid=false;
    var char1 = (minlength == 1) ? " character" : " characters";
    window.alert(inputtext+" must be at least "+minlength+char1+" long");
  }
  if(!valid) val = eval("document.forms[formnum]."+fieldname+".focus()");
  return valid;
}

// Ensure string does not exceed maximum length
function vm(formnum,fieldname,maxlength,inputtext) {
  var t_or_f = true ;
  var vble = eval("document.forms[formnum]."+fieldname+".value");

  cleanString(formnum,fieldname);
  
  if(vble.length > maxlength){
    t_or_f=false;
    var char1 = (maxlength == 1) ? " character" : " characters";
    window.alert(inputtext+" must be at no more than "+maxlength+char1+" long");
  }
  if(!t_or_f) val = eval("document.forms[formnum]."+fieldname+".focus()");
  return t_or_f ;
}

// Ensure variable has minimum value
function vv(formnum,fieldname,minvalue,inputtext){
  var t_or_f = true;
  var vble  = eval("document.forms[formnum]."+fieldname+".value");

  if(vble < minvalue){
    t_or_f=false;
    window.alert(inputtext+" must be at least "+minvalue);
  }
  if(!t_or_f) val = eval("document.forms[formnum]."+fieldname+".focus()");
  return t_or_f;
}


// Validate drop-down list
function vd(formnum,fieldname,defaultval,inputtext){
  var t_or_f = true;
  var vble  = eval("document.forms[formnum]."+fieldname+".value");

  if(vble == defaultval){
    t_or_f=false;
    window.alert("Please choose an option from the "+inputtext+" list");
  }
  return t_or_f;
}
  
// Validate radio buttons
function vr(formnum,fieldname,buttons,inputtext){
  var t_or_f = false ;

  for(var i=0 ; i<buttons ; i++){
    var vble  = eval("document.forms[formnum]."+fieldname+"["+i+"].checked");
    if(vble) t_or_f = true;
  }
  
  if(!t_or_f) window.alert("Please check one of the "+inputtext+" buttons");
  
  return t_or_f;
}

// Validate checkboxes
function vc(formnum,fieldname,totboxes,minboxes,maxboxes,inputtext){
  var check = 0;

  for(var i=0;i<totboxes;i++){
    var vble  = eval("document.forms[formnum]."+fieldname+"["+i+"].checked");
    if(vble) check++;
//window.alert("Value is >"+vble+"<") ;
  }
  if(check<minboxes) window.alert("Please check at least "+minboxes+" of the "+inputtext+" boxes");
  if(check>maxboxes) window.alert("Please check no more than "+maxboxes+" of the "+inputtext+" boxes");
  valid = (check<minboxes || check>maxboxes) ? false : true;
  return valid ;
}

// Confirm Web Site address begins with 'www.' or 'http://www.'
function vx(formnum,fieldname,needed,inputtext) {
  var valid = true;

  var vble = eval("document.forms[formnum]."+fieldname+".value");
  vble = blankout(vble);
  vble = vble.toLowerCase();
  if(needed.toUpperCase() == "N" && vble == "") return true;  // Allow blank web site address

  var valid = (vble.substring(0,4)  == "www.") ? true : false;
  var valid = (vble.substring(0,11) == "http://www.") ? true : valid;
  if(!valid){
    window.alert("please enter a valid "+inputtext);
    val = eval("document.forms[formnum]."+fieldname+".focus()");
  }
  return valid;
}

// Validate and format postcode
function vp(formnum,fieldname,needed,inputtext) {
  var t_or_f = true;
  var frm1 = eval("document.forms[formnum]."+fieldname)
  var frm2 = eval("document.forms[formnum]."+fieldname)
  frm1.value = blankout(eval("document.forms[formnum]."+fieldname+".value"));
  frm2.value = eval("document.forms[formnum]."+fieldname+".value").toUpperCase();
  var pcode = eval("document.forms[formnum]."+fieldname+".value");
  if (needed.toUpperCase() == "N" && pcode == "") return true;  // Allow blank postcode
  var regEx  = new RegExp(/^[A-Za-z]{1,2}[0-9A-Za-z]{1,2}[ ]?[0-9]{0,1}[A-Za-z]{2}$/);
  if(!regEx.exec(pcode)){
  	t_or_f = false;
	alert('You have not entered a valid UK postcode.');
  }
  return t_or_f;
}


