
//Welcome to the wonky land of Javascript "OOP"
function Validation(){}
//This function is for validating credit card account numbers
//account: The account number to validate.
//type: The type of credit card to validate, supported values: mastercard, visa, amex, discover.
Validation.IsValidCreditCard = function(account, type){
	var value = false;
	//Is the account number numeric?
	var isNumeric = /[^\d]/;
	value = !isNumeric.test(account);
	
	if(value){
	 	//Is the account number the correct length?
		switch(type){
	    	case "mastercard":
		        value = (account.length == 16);
		        //Setup the regex to validate the account prefix.
		        isValidPrefix = /^5[1-5]/;
		        break;
	
	    	case "visa":
		        value = (account.length == 16 || account.length == 13);
		        //Setup the regex to validate the account prefix.
		        isValidPrefix = /^4/;
		        break;
	
	    	case "amex":
		        value = (account.length == 15);
		        //Setup the regex to validate the account prefix.
		        isValidPrefix = /^3(4|7)/;
		        break;
	
	    	case "discover":
		        value = (account.length == 16);
		        //Setup the regex to validate the account prefix.
		        isValidPrefix = /^6011/;
		        break;
	
	    	default:
		        value = false;
	    }
	    //If the length was valid, is the prefix?
	    if(value && isValidPrefix.test(account)){
	     	//Luhn anyone?
	    	var product;
		    var productDigitIndex;
		    var checkSumTotal = 0;
		
		    for (var i = account.length - 1; 
		      i >= 0; 
		      i--)
		    {
		      checkSumTotal += parseInt (account.charAt(i));
		      i--;
		      product = String((account.charAt(i) * 2));
		      for (var j = 0;
		        j < product.length; 
		        j++)
		      {
		        checkSumTotal += 
		          parseInt(product.charAt(j));
		      }
		    }
		
		    value = (checkSumTotal % 10 == 0);
		}else{
			value = false;
		}
	}
	return value;
}

//This function is for validation of credit card expiration values.
//month: The month the card expires.
//year: The year the card expires.
Validation.IsValidCreditCardExpiration = function(month, year){
 	var value = false;
 	var numMonth = parseInt(month); 
 	var numYear = parseInt(year); 
	if(numYear < 100){
		numYear += 2000;
	}
	date = new Date();
	if(numYear > date.getFullYear() && numMonth > 0 && numMonth <= 12){
		value = true;
	} else {
		if(numYear == date.getFullYear() && numMonth >= date.getMonth() && numMonth <= 12){
			value = true;
		}
	}
	return value;
}

//This function is for validation of an email address. (we should improve this at some point)
// The email address to validate.
Validation.IsValidEmail = function(email){
 	var emailValidator = /[\w]*[\@][\w]*[\.][a-zA-Z\.]{2,5}/;
 	return emailValidator.test(email);
}

//This function is for validation of a strings length.
//value: The string to validate.
//min: The minimum allowed length.
//max: The maximum allowed length.
Validation.IsValidLength = function(value, min, max){
	if(value.length >= min && value.length < max){
		return true;
	}
	return false;
}

//This function is for validation of string data.
//value: The string to validate.
//allowNumeric: A bool value indicating whether numeric chars are allowed.
//allowAlpha: A bool value indicating whether alpha chars are allowed.
//allowSpecialChars: A bool value indicating whether special chars are allowed.
//allowedSpecialChars: A string containing special chars allow or disallow.
Validation.IsValidChars = function(value, allowNumeric, allowAlpha, allowSpecialChars, specialChars){
	var regex = "^([" + 
			((allowNumeric)? "\\d" : "") + 
			((allowAlpha)? "a-zA-Z" : "") + 
			FormatSpecialCharsRegExString(specialChars, allowSpecialChars) + 
			"])*$";
	validator = new RegExp(regex, "");
	return validator.test(value);
}

//This function is for formating the special chars for the regex builder in Validation.IsValidChars
function FormatSpecialCharsRegExString(specialChars, allow){
 	var returnSpecialChars = "\\n\\~\\`\\'\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\_\\-\\+\\=\\{\\[\\}\\]\\|\\\\\\:\\;\\\"\\'\\<\\,\\>\\.\\?\\/\\ ";
 	if(specialChars.length > 0){
 	 	if(allow){returnSpecialChars = "";}
		for(var i = 0; i < specialChars.length; i++){
			if(allow){
				returnSpecialChars = returnSpecialChars.replace('\\' + specialChars[i], '');
			} else {
				returnSpecialChars += '\\' + specialChars[i];
			}
		}
	} else if (!allow){
		returnSpecialChars = "";
	}
	return returnSpecialChars;
}