
function emailCheck(email,notify) {
	var 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\-]+))$/i;
	var address = document.getElementById(email).value;
        if (regex.test(address) == false) {
		if (notify === true) alert('Invalid Email Address');	
		return false;
	} 
	else return true;	
}


function zipCheck(zip,notify) {
	var regex= /(^\d{5}$)|(^\d{5}-\d{4}$)/;
	var zipCode = document.getElementById(zip).value;
	if (regex.test(zipCode) == false) {
		if (notify === true) alert('Please enter a valid ZIP Code');
		return false;
	}	
}


function ccCheck(cc,notify) {
	var regexAlpha = /[A-Za-z]/;
	var ccNum = document.getElementById(cc).value;
	var alertText = 'Please enter a valid Credit Card Number';
	if (regexAlpha.test(ccNum) === false) {
	    var regexNum = /\D/g;
	    ccNum = ccNum.replace(regexNum,'');
	    var firstDigit = ccNum.charAt(0);
	    var cardTypes = [];
	        cardTypes[4] = 'Visa';
	    	cardTypes[5] = 'MasterCard';
	    	cardTypes[6] = 'Discover';

	    if ((firstDigit >= 3) & (firstDigit <= 6)) {
	    	if (firstDigit == 3) {
			if (ccNum.length == 15) {
				cardTypes[3] = 'AmEx';
			} else if (ccNum.length == 14) {
				cardTypes[3] = 'Diners Club';
			} else {
				if (notify === true) alert(alertText);
				return false;
			}	
	        }
		if ((firstDigit != 3) & (ccNum.length != 16)) {
		     if (notify === true) alert(alertText);
		     return false;	
	 	}	
		return cardTypes[firstDigit];
	    } else {
		if (notify === true) alert(alertText);
		return false;
	    }	
	} else {
		if (notify === true) alert(alertText);
		return false;
	}	
}


function htmlSafe(text,notify) {
	var valid = Boolean(true);
	var html = document.getElementById(text).value;
	this.badChars=[];
	for (var i=0;i<html.length;i++) {
	    if ((html.charAt(i) == '"') | (html.charAt(i) == '&') | (html.charAt(i) == '<') | (html.charAt(i) == '>')) {
		    this.badChars[i]=html.charAt(i);
		    valid = false;
	    }	
	}
	if (valid === false) {
	    if (notify === true) {
		var message = 'Please correct the following invalid characters: ' + this.badChars.join('');
		alert(message);
	    }	
	    
	    return false;		
	} else return true;
	
}	

