// email
function checkEmail (strng, name) {
	var error="";
	if (strng == "") {
	   error = "You didn't enter an "+name;
	}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid "+name;
    }
    else {
		//test email for illegal characters
		var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/;
		if (strng.match(illegalChars)) {
			error = name+" contains illegal characters.";
		}
    }
	return error;    
}


// phone number - strip out delimiters and check for 10 digits
function checkPhone (strng, name) {
	var error = "";
	if (strng == "") {
	   error = "You didn't enter a "+name;
	}

	var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The "+name+" contains illegal characters.";
    }
    if (!(stripped.length == 10)) {
	error = "The "+name+" is the wrong length. Make sure you included an area code.";
    } 
	return error;
}


function checkZip (strng, name) {
	var error = "";
	if (strng == "") {
	   error = "You didn't enter a "+name;
	}

	var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The "+name+" contains illegal characters.";
    }
    if (!(stripped.length == 5) && !(stripped.length == 9)) {
		error = "The "+name+" is the wrong length.";
    } 
	return error;
}

// non-empty textbox
function isEmpty(strng, name) {
	var error = "";
	if (strng.length == 0) {
		error = "You didn't enter a "+name;
	}
	return error;	  
}

// exactly one radio button is chosen

function checkRadio(checkvalue, name) {
	var error = "";
	if (!(checkvalue)) {
		error = "Please select a "+name;
	}
	return error;
}

// valid selector from dropdown list

function checkDropdown(choice, name) {
	var error = "";
    if (choice == 0) {
		error = "You didn't choose an option for "+name;
    }    
	return error;
}    

function checkCreditCard(number, name) {
	var is_valid = validateCreditCard(number);
	var error = "";
	if (is_valid == false) {
		error = "You entered an invalid credit card number";
	}
	return error;
}

// Credit Card Validation Javascript
// copyright 12th May 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function validateCreditCard(s) {
	var v = "0123456789";
	var w = "";
	for (var i=0; i < s.length; i++) {
		x = s.charAt(i);
		if (v.indexOf(x,0) != -1)
			w += x;
	}
	var j = w.length / 2;
	if (j < 6.5 || j > 8 || j == 7) return false;
	var k = Math.floor(j);
	var m = Math.ceil(j) - k;
	var c = 0;
	for (var i=0; i<k; i++) {
		a = w.charAt(i*2+m) * 2;
		c += a > 9 ? Math.floor(a/10 + a%10) : a;
	}
	for (var i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
	return (c%10 == 0);
}
