<script type="text/javascript">

//<[CDATA[
	/* Make sure all credit card details have been provided and are valid */
	function check_cc_payment_form() {
		var cc_cctype = g("cc_cctype");
		var cc_ccno = g("cc_ccno");
		var cc_ccexpm = g("cc_ccexpm");
		var cc_ccexpy = g("cc_ccexpy");
		var cc_name = g("cc_name");
		var cc_cpf = g("cc_cpf");
		var cc_datanasc = g("cc_datanasc");
		var cc_tel = g("cc_tel" );
		var attcpf = '%%GLOBAL_lerfunccpf%%';
		var attdata = '%%GLOBAL_lerfuncnas%%';
		var atttel = '%%GLOBAL_lerfunctel%%';
		var cc_parcelas = g("cc_parcelas" );

	if(cc_cctype.selectedIndex == 0) {
			swal("%%LNG_CCManualChooseCardType%%");
			cc_cctype.focus();
			return false;
		}

		if(isNaN(cc_ccno.value) || cc_ccno.value == "" || !validateCreditCard(cc_ccno.value)) {
			swal("%%LNG_CCManualEnterCardNumber%%");
			cc_ccno.focus();
			cc_ccno.select();
			return false;
		}

		if(requiresCVV2() && ($('#cc_cvv2').val() == '' || isNaN($('#cc_cvv2').val()) || $('#cc_cvv2').val().length > 4)) {
			swal('%%LNG_CCManualEnterCVV2Number%%');
			$('#cc_cvv2').focus();
			$('#cc_cvv2').select();
			return false;
		}

		if(cc_ccexpm.selectedIndex == 0) {
			swal("%%LNG_CCManualEnterCreditCardMonth%%");
			cc_ccexpm.focus();
			return false;
		}

		if(cc_ccexpy.selectedIndex == 0) {
			swal("%%LNG_CCManualEnterCreditCardYear%%");
			cc_ccexpy.focus();
			return false;
		}

		if(cc_name.value == "") {
			swal("%%LNG_CCManualEnterName%%");
			cc_name.focus();
			return false;
		}
		if(cc_cpf.value == "" && attcpf == 1) {
			swal("%%LNG_CCManualEnterCPF%%");
			cc_cpf.focus();
			return false;
		}
		if(cc_datanasc.value == "" && attdata == 1) {
			swal("%%LNG_CCManualEnterDatanasc%%");
			cc_datanasc.focus();
			return false;
		}
		if(cc_tel.value == "" && atttel == 1) {
			swal("%%LNG_CCManualEnterTel%%");
			cc_tel.focus();
			return false;
		}
		if(cc_parcelas.value == "") {
			swal("Selecione a Quantidade de Parcelas");
			cc_parcelas.focus();
			return false;
		}
		if(hasIssueNo() && ($('#cc_issueno').val() == '' || isNaN($('#cc_issueno').val())) ) {
			if(hasIssueDate() && (g('cc_issuedatey').selectedIndex == 0  || g('cc_issuedatem').selectedIndex == 0)) {
				swal("%%LNG_CCManualEnterIssueDetails%%");
				$('#cc_issueno').focus();
				return false;
			}
		}

		return true;
	}

	function validateCreditCard(number) {
		var number=number.replace(/\D/g, '');
		var number_length=number.length;
		var parity=number_length % 2;
		var total=0;
		for (i=0; i < number_length; i++) {
			var digit=number.charAt(i);
			if (i % 2 == parity) {
				digit=digit * 2;
				if (digit > 9) {
					digit=digit - 9;
				}
			}
			total = total + parseInt(digit);
		}
		if (total % 10 == 0) {
			return true;
		} else {
			return false;
		}
	}

	function requiresCVV2() {
		var type = $('#cc_cctype').val();
		if(type) {
			if($('#CCType_'+type).hasClass('requiresCVV2')) {
				return true;
			}
			else {
				return false;
			}
		}
		else {
			return false;
		}
	}

	function hasIssueDate() {
		var type = $('#cc_cctype').val();
		if(type) {
			if($('#CCType_'+type).hasClass('hasIssueDate')) {
				return true;
			}else {
				return false;
			}
		}else {
			return false;
		}
	}

	function hasIssueNo() {
		var type = $('#cc_cctype').val();
		if(type) {
			if($('#CCType_'+type).hasClass('hasIssueNo')) {
				return true;
			}else {
				return false;
			}
		}else {
			return false;
		}
	}

	function updateCreditCardType() {
		if(requiresCVV2()) {
			$('.CVV2Input').show();
		}else {
			$('.CVV2Input').hide();
		}
		if(hasIssueNo()) {
			$('.CCIssueNo').show();
		}else {
			$('.CCIssueNo').hide();
		}
		if(hasIssueDate()) {
			$('.CCIssueDate').show();
		}else {
			$('.CCIssueDate').hide();
		}
	}

	$(document).ready(function() {
		updateCreditCardType();
	});
//]]>
</script>