// email

function checkEmail (strng) {
var error="";
if (strng == "") {
   error = "You didn't enter an email address.\n\n";
}

    var emailFilter=/^.+@.+\..{2,4}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.\n\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The email address contains illegal characters.\n\n";
       }
    }
return error;    
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a phone number.\n\n";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters.\n\n";
  
    }
    if (!(stripped.length == 10)) {
	error = "Please enter 10 digit phone number including the area code.\n\n";
    } 
return error;
}

// mobile number - strip out delimiters and check for 10 digits

function checkMobile (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a mobile number.\n\n";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The mobile number contains illegal characters.\n\n";
  
    }
    if (!(stripped.length == 10)) {
	error = "The mobile number is the wrong length. It should be 10 digits\n\n";
    } 
return error;
}

// postcode - strip out delimiters and check for 4 digits

function checkPostcode (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a postcode.\n\n";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The postcode contains illegal characters.";
  
    }
    if (!(stripped.length == 4)) {
	error = "Please enter a 4 digit postcode.\n\n";
    } 
return error;
}

// barcode - strip out delimiters and check for 13 digits

function checkBarcode (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a barcode.\n\n";
}

    if (isNaN(parseInt(strng))) {
       error = "The barcode contains illegal characters - please enter the numbers only.\n\n";
  
    }
    if (!(strng.length == 13 || strng.length == 8)) {
	error = "The barcode is the wrong length. Please re-enter the numbers carefully.\n\n";
    } 
return error;
}


// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a password.\n\n";
}

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((strng.length < 6) || (strng.length > 8)) {
       error = "The password is the wrong length.\n\n";
    }
    else if (illegalChars.test(strng)) {
      error = "The password contains illegal characters.\n\n";
    } 
    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
       error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n\n";
    }  
return error;    
}    


// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a username.\n\n";
}


    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 4) || (strng.length > 10)) {
       error = "The username is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
    error = "The username contains illegal characters.\n\n";
    } 
return error;
}       


// non-empty textbox

function isEmpty(strng, fieldname) {
var error = "";
  if (strng.length == 0) {
     error = "Please fill in your "+fieldname+"\n\n";
  }
return error;	  
}

// was textbox altered

function isDifferent(strng) {
var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n\n";
  }
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue, fieldname) {
var error = "";
   if (!(checkvalue)) {
       error = "Please choose a "+fieldname+".\n\n";
    }
return error;
}

// valid selector from dropdown list

function checkDropdown(choice, fieldname) {
var error = "";
    if (choice == 0) {
    error = "Please choose a "+fieldname+" from the drop-down list.\n\n";
    }    
return error;
}

// wordcount 
function checkWordcount(strng) {
var error = "";
if (strng == "") {
   error = "Please tell us ‘Why you have selected an aluminium free anti-perspirant deodorant’.\n\n";
}

var words = strng.split(/\s/);
cnt = words.length;

    if (cnt > 25) {
       error = "You entered more than 25 words for your testimonial.\n\n";
    }
return error;
}

// validate checkbox has been selected
function checkCheckbox (bln) {
	var error = ""; 
  if (bln == false) {
     error = "Please read and accept the Terms and Conditions.\n\n";
  }
return error;
}

function checkmultiCheckbox (bln1, bln2, bln3, bln4, bln5, bln6, bln7, bln8, bln9, bln10) {
	var error = ""; 
  if (bln1 == false && bln2 == false && bln3 == false && bln4 == false && bln5 == false && bln6 == false && bln7 == false && bln8 == false && bln9 == false && bln10 == false) {
     error = "Please select ‘Why you use Redwin products’.\n\n";
  }
return error;
}

function checkDate (theday, themonth, theyear) {
var error = ""; 
//var timeFilter=/^\d{1,2}\/\d{1,2}\/\d{4}$/;
//var brokenstring = strng.split("/")
/* Using form values, create a new date object
using the setFullYear function */
var myDate = new Date();
myDate.setFullYear(parseFloat(theyear),parseFloat(themonth) - 1,parseFloat(theday));
var startDate=new Date();
startDate.setFullYear(2009,2,9);
var endDate=new Date();
endDate.setFullYear(2009,6,1);
var todaysDate = new Date();

if (startDate>myDate || endDate<myDate) {
   error = "You didn't enter a valid purchase date.\n - The product must have been purchased from 10th March 2009 to 30 June 2009\n\n";
} else if (myDate>todaysDate) {
	error = "This purchase date has not occured yet!\n\n";
} else if ( (myDate.getMonth() + 1) != parseFloat(themonth) ) {
  error = "Please enter a valid purchase date.\n\n";
} else {
  error = ""; 
}
return error;
}


// ^((((31\/(0?[13578]|1[02]))|((29|30)\/(0?[1,3-9]|1[0-2])))\/(1[6-9]|[2-9]\d)?\d{2})|(29\/0?2\/(((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))|(0?[1-9]|1\d|2[0-8])\/((0?[1-9])|(1[0-2]))\/((1[6-9]|[2-9]\d)?\d{2})) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$