function cardType (num)
{
	var num = num.replace (/\s/g, "");
	var regex = /^[0-9]{13,19}$/;
	var checksum = 0;
	var j = 1;
	
	if (!regex.exec(num))
		return 'Invalid';
	
	var calc;
	for (i = num.length - 1; i >= 0; i--) {
		
		calc = Number(num.charAt(i)) * j;
		if (calc > 9) {
			checksum = checksum + 1;
			calc = calc - 10;
		}
		checksum = checksum + calc;
		if (j ==1) {j = 2} else {j = 1};
	}
	
	if (checksum % 10 != 0)
		return 'Invalid';
	
	/* Visa */
	if ((num.length == 16) || (num.length == 13))
	{
		regex = /^4/;
		if (regex.exec(num))
			return 'Visa';
	}
	
	/* Maestro */
	/*if ((num.length == 16) || (num.length == 18))
	{
		regex = /^(5020|5038|56|67|63)/;
		if (regex.exec(num))
			return 'Maestro';
	}*/
	
	/* MasterCard */
	if (num.length == 16)
	{
		regex = /^(51|52|53|54|55)/;
		if (regex.exec(num))
			return 'MasterCard';
	}
	
	/* other */
	return 'Invalid2';
}
