﻿function emailCheck(emailStr) {
	emailStr = emailStr.toLowerCase();
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {return false}
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) {if (user.charCodeAt(i)>127) {return false}}
	for (i=0; i<domain.length; i++) {if (domain.charCodeAt(i)>127) {return false}}
	if (user.match(userPat)==null) {return false}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {for (var i=1;i<=4;i++) {if (IPArray[i]>255) {return false}}; return true}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {if (domArr[i].search(atomPat)==-1) {return false}}
	if (len<2) {return false}
	return true;
	}

function checkPostCode(toCheck) {
    var alpha1 = "[abcdefghijklmnoprstuwyz]";
    var alpha2 = "[abcdefghklmnopqrstuvwxy]";
    var alpha3 = "[abcdefghjkstuw]";
    var alpha4 = "[abehmnprvwxy]";
    var alpha5 = "[abdefghjlnpqrstuwxyz]";
    var pcexp = new Array ();
    pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
    pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
    pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
    pcexp.push (/^(GIR)(\s*)(0AA)$/i);
    pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
    pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);
    var postCode = toCheck;
    var valid = false;
    for (var i=0; i<pcexp.length; i++) {
        if (pcexp[i].test(postCode)) {
            pcexp[i].exec(postCode);
            postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
            postCode = postCode.replace (/C\/O\s*/,"c/o ");
            valid = true;
            break;
            }
        }
    if (valid) {return postCode} else {return false}
    }
