//============================================================================
// This function determines if a string is null or empty. If empty or null,
// it returns true, otherwise false
function validate_empty_field(_cVal){
	var return_val = 0;
	if(_cVal == null || _cVal == '' || _cVal == void(0)){ return_val = 1; }
	return return_val;
}
//============================================================================
// This function tests a string to make sure that it is alphanumeric. Additional
// valid charcters are also checked. If valid, returns true, otherwise false.
function validate_extended_alphanum(_cStr){
	var return_val 	= false;
	var reg1 	= /^[a-zA-Z0-9\s\-\'\.\#\(\)\,]+$/;
	if(reg1.test(_cStr)){ return_val = true; }
	return return_val;
}
//============================================================================
// This function tests a string to make sure that it is alphanumeric. Only
// letters, numbers, and underscores are considered valid
function validate_simple_alphanum(_cStr){
	var return_val 	= false;
	var reg1 	= /^[a-zA-Z0-9_]+$/;
	if(reg1.test(_cStr)){ return_val = true; }
	return return_val;
}
//============================================================================
// This function tests a string to make sure that it is numeric. If valid, returns true, otherwise false.
function validate_numeric(_cStr){
	var return_val 	= false;
	var reg1 	= /^[0-9]+$/;
	if(reg1.test(_cStr)){ return_val = true; }
	return return_val;
}
//============================================================================
// This function takes a string, and removes everything but numbers. This number
// is then returned. This is mainly used for phone number validation. If the
// length of the number returned is less than 10 digits, then it's not a valid
// US phone number
function validate_get_just_nums(_str){
	var return_val 	= '';
	var reg1 	= /[^0-9]/g;
	return _str.replace(reg1,'');
}
//============================================================================
// This function determines if the string passed in is a valid email address. It
// looks complicated because I found it on the web. It works. If the string is valid, it returns true, else false.
function validate_email(_cEmail){
	var return_val 	= false;
	var reg1 	= /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
	var reg2 	= /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
	if(!reg1.test(_cEmail) && reg2.test(_cEmail)) { // if syntax is valid
		return_val = true;
	}
	return return_val;
}
//============================================================================
// This function determines if the string passed in is a valid US zip code.  It accepts 
// either ##### or #####-####.  If the string is valid, it returns true, else false.
function validate_zip_code(_cZipCode){
	var return_val 	= false;
	var reg1 	= /^[0-9]{5}$/; // valid 5 digit zipcode
	var reg2	= /^[0-9]{5}\-[0-9]{4}$/; // valid extended zipcode
	var reg3	= /^[0-9]{4}$/; // some lazy people leave off a leading 0
	var reg4	= /^[0-9]{4}\-[0-9]{4}$/; // since I can't kill them, I'll have to work with them 
	if(reg1.test(_cZipCode) || reg2.test(_cZipCode) || reg3.test(_cZipCode) || reg4.test(_cZipCode)) { // if syntax is valid
		return_val = true;
	}
	return return_val;
}
//============================================================================
function validate_card_expired(_cMonth, _cYear){
	var return_val	= false;
	var month 	= parseInt(_cMonth * 1, 10);
	var year 	= parseInt(_cYear * 1, 10);
	var now 	= new Date();
	var nowMonth 	= now.getMonth() + 1;
	var nowYear 	= now.getFullYear();

	if(_cYear.length == 2){ year += 2000; } // make sure that we're working with a 4 digit year
	if(month != null && year != null && month > 0 && month <= 12 && year > 2000){
		return_val = (nowYear > year) || ((nowYear == year ) && (nowMonth > month));
	}

	return return_val;
}
//============================================================================
function derive_card_type(card_number){
	var card_type 		= false;
	var card_length 	= card_number.length;	
	var first_dig 		= card_number.substring(0,1);
	var second_dig 		= card_number.substring(1,2);
	var first_4_digs 	= card_number.substring(0,4);
	var reg1 		= /^[0-9]+$/;
	var valid_nums;
	var i, c;

	// as a sanity check, make sure we have a valid number
	if(reg1.test(card_number)){ 
		if(((card_length == 16) || (card_length == 13)) && (first_dig == "4")){ card_type = "VISA"; }
		else{
			valid_nums = "47";
			if((card_length == 15) && (first_dig == "3") && (valid_nums.indexOf(second_dig) >= 0)){ card_type = "AMEX"; }
			else{
				valid_nums = "12345";
				if((card_length == 16) && (first_dig == "5") && (valid_nums.indexOf(second_dig) >= 0)){ card_type = "MASTERCARD"; }
				else{
					if((card_length == 16) && (first_4_digs == "6011")){ card_type = "DISCOVER"; }
					else{
						valid_nums = "068";
						if((card_length == 14) && (first_dig == "3") && (valid_nums.indexOf(second_dig) >= 0)){ card_type = "DINERS"; }
					}
				}
			}
		}
	}

	return card_type;
}
//============================================================================
function validate_card_number(card_type, card_number){
	card_type		= card_type.toUpperCase();
	var card_length 	= card_number.length;	
	var first_dig 		= card_number.substring(0,1);
	var second_dig 		= card_number.substring(1,2);
	var first_4_digs 	= card_number.substring(0,4);
	var return_val 		= false;
	var reg1 		= /^[0-9]+$/;
	var valid_nums;
	var i, c;

	// as a sanity check, make sure we have a valid number
	if(reg1.test(card_number)){ 
		switch(card_type){
			case "VISA":
				return_val = ((card_length == 16) || (card_length == 13)) && (first_dig == "4");
				break;
			case "AMEX":
				valid_nums = "47";
				return_val = (card_length == 15) && (first_dig == "3") && (valid_nums.indexOf(second_dig) >= 0);
				break;
			case "MASTERCARD":
				valid_nums = "12345";
				return_val = (card_length == 16) && (first_dig == "5") && (valid_nums.indexOf(second_dig) >= 0);
				break;
			case "DISCOVER":
				return_val = (card_length == 16) && (first_4_digs == "6011");
				break;
			case "DINERS":
				valid_nums = "068";
				return_val = (card_length == 14) && (first_dig == "3") && (valid_nums.indexOf(second_dig) >= 0);
				break;
			default: break;
		}
	}

	return return_val;
}
//============================================================================
function validate_luhn(str){
	var return_val	= false;
	var sum 	= 0; 
	var mul 	= 1; 
	var strLen 	= str.length;
	var digit, tproduct;

	for(i = 0; i < strLen; i++){
		digit = str.substring(strLen-i-1,strLen-i);
		tproduct = parseInt(digit ,10)*mul;
		if(tproduct >= 10){ sum += (tproduct % 10) + 1; }
		else{ sum += tproduct; }
		mul = (mul == 1) ? (mul + 1) : (mul - 1);
	}

	if((sum % 10) == 0){ return_val = true; }

	return return_val;
}
//============================================================================
function validate_ccv2_number(card_type, ccv2_number){
	card_type		= card_type.toUpperCase();
	var ccv2_length 	= ccv2_number.length;	
	var return_val 		= false;
	var reg1 		= /^[0-9]+$/;
	var i, c;

	// as a sanity check, make sure we have a valid number
	if(reg1.test(ccv2_number)){ 
		switch(card_type){
			case "VISA": return_val = (ccv2_length == 3); break;
			case "AMEX": return_val = (ccv2_length == 4); break;
			case "MASTERCARD": return_val = (ccv2_length == 3); break;
			case "DISCOVER": return_val = (ccv2_length == 3); break;
			case "DINERS": break; // don't know if it has a ccv2 number
			default: break;
		}
	}

	return return_val;
}
//============================================================================

