function isEmpty(string) {
	return ((str == null) || (str.length == 0));
}

function isEmail(string) {
	var regex = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i;
	if(regex.test(string)) {
		return true;
	}
	return false;
}

function isName(string) {
	if(/[^a-z- ]/i.test(string)) {
		return false;
	}
	return true;
}

function isAlpha(string) {
	if(/[^a-z ]/i.test(string)) {
		return false;
	}
	return true;
}

function isNumeric(string) {
	if(/[\D]/.test(string)) {
		return false;
	}
	return true;
}

function isAlphanumeric(string) {	
	if(/[^a-z0-9- ]/i.test(string)) {
		return false;
	}
	return true;
}

function isLength(string, len) {
	return (string.length == len);
}

function isLengthBetween(string, min, max) {
	return ((string.length >= min) && (string.length <= max));
}

function isMatch(string1, string2) {
	return (string1 == string2);
}

function isWhitespace(string) {	
	if(/[\S]/.test(string)) {
		return false;
	}
	return true;
}

function stripWhitepsace(string, replacement) {
	if(replacement == null) {
		replacement = '';
	}
	
	var result = string;
	var regex = /\s/;
	
	if(string.search(regex) != -1) {
		result = string.replace(regex, replacement);
	}
	
	return result;
}

function checkCard(v) {
	cc = v;
	document.getElementById('cc_number_response').innerHTML = '<img src="/images/loading-small.gif" /> Pre-screening credit card...';
	alertTimerId = setTimeout('requestCard()', 1500);
}

function requestCard() {
	clearInterval(alertTimerId);
	request('/checkout/validate/', 'cc_number', 'ccnumber=' + cc + '&cctype=' + document.getElementById('cc_type').value);
}

function validate(element, type) {
	var bypass = false;
	var errorClass = 'error';
	
	if((element.name == 'cc_month') || (element.name == 'cc_year')) {
		var divResponse = 'cc_expiration_response';
	} else {
		var divResponse = element.name + '_response';
	}
	
	if(document.getElementById(divResponse)) {
		var div = document.getElementById(divResponse);
	}
	
	if(!element.value) {
		element.className = errorClass;
		div.innerHTML = 'Required';
		
		return false;
	} else {		
		if((type == 'alpha') && (!isAlpha(element.value))) {
			element.className = errorClass;
			div.innerHTML = 'Can only contain letters (A-Z)';
			
			return false;
		}
		
		if((type == 'numeric') && (!isNumeric(element.value))) {
			element.className = errorClass;
			div.innerHTML = 'Can only contain numbers (0-9)';
			
			return false;
		}
		
		if((type == 'alphanumeric') && (!isAlphanumeric(element.value))) {
			element.className = errorClass;
			div.innerHTML = 'Can only contain letters (A-Z) and numbers (0-9)';
			
			return false;
		}
		
		if((type == 'name') && (!isName(element.value))) {
			element.className = errorClass;
			div.innerHTML = 'Can only contain letters (A-Z)';
				
			return false;
		}
		
		if((type == 'email') && (!isEmail(element.value))) {
			element.className = errorClass;
			div.innerHTML = 'Invalid email address';
			
			return false;
		}
		
		if(type == 'cc') {
			bypass = true;
			checkCard(element.value);
		}
		
		if(type == 'cvv') {			
			if(!isNumeric(element.value)) {
				element.className = errorClass;
				div.innerHTML = 'Can only contain numbers (0-9)';
				
				return false;
			} else if(document.getElementById('cc_type').value == 'American Express') {
				if(!isLength(element.value, 4)) {
					element.className = errorClass;
					div.innerHTML = 'Card security code must be 4 characters long';
					
					return false;
				}
			} else {
				if(!isLength(element.value, 3)) {
					element.className = errorClass;
					div.innerHTML = 'Card security code must be 3 characters long';
					
					return false;
				}
			}
			
			element.className = '';
			div.innerHTML = '';
			
			return true;
		}
		
		if(!bypass){
			element.className = '';
			div.innerHTML = '';
		}
		
		return true;
	}
}