function validateEmail(email) {
    var email_regex = /^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/
    return email_regex.test(email);
}

function autoValidate(formId, doAutoSubmit) {
    var pleaseEnterStr = 'Please enter: ';
    
    if(typeof elementsToValidate != "undefined") {
        for (i=0; i<elementsToValidate.length; i++) {
            element = document.getElementById(elementsToValidate[i]);
            if(element && (element.value == '' || (element.value.charAt(0) == '-' && element.value.charAt(1) == ' '))) {
                alert (pleaseEnterStr + elementsNames[i]);
                element.focus();
                return false;
            }
        }
    }
    
    var pleaseChooseStr = 'Please choose: ';
    if(typeof selectsToValidate != "undefined") {
        for (i=0; i<selectsToValidate.length; i++) {
            element = document.getElementById(selectsToValidate[i]);
            if(element && (element.value == '' || element.value == -1)) {
                alert (pleaseChooseStr + selectsNames[i]);
                element.focus();
                return false;
            }
        }
    }
    // add any other validation below, eg email, phone, etc
    if(document.getElementById('product_details_colours')) {
        if(!validatePipedLength('product_details_colours', 20)) {
            alert('Each Colour is limited to 20 characters');
            document.getElementById('product_details_colours').focus();
            return false;
        }
    }
    
    if(document.getElementById('product_details_sizes')) {
        if(!validatePipedLength('product_details_sizes', 20)) {
            alert('Each Size is limited to 20 characters');
            document.getElementById('product_details_sizes').focus();
            return false;
        }
    }
    
    form = document.getElementById(formId);
    if(doAutoSubmit && form) {
        form.submit();
    } else {
        return true;
    }
}

function validatePipedLength(fieldName, maxLen) {    
    var field = document.getElementById(fieldName);
    var values = field.value.split('|');
    var numValues = values.length;
    for(var i=0; i < numValues; i++) {
        if(values[i].length > maxLen) {
            return false;
        }
    }
    return true;
    
}

function filterPostCode(str) {
	re = /[^0-9a-zA-Z]/g;
	// remove "$" and ","
	str=str.replace(re, "");
	return str.toUpperCase();
}
function filterNum(str) {
	re = /[^0-9]/g;
	// remove "$" and ","
	return str.replace(re, "");
}

function clearField(field) {
    if (field.value.charAt(0) == '-') {
        field.value = '';
    }
}

function validateEmail(addr,man,db) {
	if (addr == '' && man) {
		if (db) alert('email address is mandatory');
		return false;
	}
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
		if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
			if (db) alert('email address contains invalid characters');
			return false;
		}
	}
	for (i=0; i<addr.length; i++) {
		if (addr.charCodeAt(i)>127) {
			if (db) alert("email address contains non ascii characters.");
			return false;
		}
	}

	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
		if (db) alert('email address must contain an @');
		return false;
	}
	if (atPos == 0) {
		if (db) alert('email address must not start with @');
		return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
		if (db) alert('email address must contain only one @');
		return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
		if (db) alert('email address must contain a period in the domain name');
		return false;
	}
	if (addr.indexOf('@.',0) != -1) {
		if (db) alert('period must not immediately follow @ in email address');
		return false;
	}
	if (addr.indexOf('.@',0) != -1){
		if (db) alert('period must not immediately precede @ in email address');
		return false;
	}
	if (addr.indexOf('..',0) != -1) {
		if (db) alert('two periods must not be adjacent in email address');
		return false;
	}
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length != 2 && suffix != 'com' && suffix != 'net' &&
suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil'
&& suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix !=
'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' &&
suffix != 'pro' && suffix != 'museum') {
		if (db) alert('invalid primary domain in email address');
		return false;
	}
	return true;
}