/**
* @version $Id: wallet.card.js 24423 2009-03-11 15:16:26Z martin $
*/

///////////////////////////////////////////////////////////////////////////////

/**
* Toggles the "CVV" inputs when changing the credit card.
*
* @param Array arr - array with all cc types
* @param String cc_type - selected credit card type
*/
function toggle_cvv(arr, cc_type){
	
	// hide both the element identified by id="show_cvv", as
	// well as the elements that have the "show_cvv" class
	//
	jQuery('#show_cvv').css('visibility','hidden').css('display','none');
	jQuery('.show_cvv').css('visibility','hidden').css('display','none');

	// CVV restriction exists ?
	//
	if(arr[cc_type]) {

		// show the id="show_cvv" and class="show_cvv" elements
		//
		jQuery('#show_cvv').css('visibility','visible').css('display','');
		jQuery('.show_cvv').css('visibility','visible').css('display','');

		// set the name of the CVV field
		//
		jQuery('#cvv_text').html(arr[cc_type] + ':*');
		}
	return;
	}

///////////////////////////////////////////////////////////////////////////////

/**
* An idiot-proof solution for choosing the correct credit card type
*
* @param String number - the credit card number
* @param HTMLInput card_type - the text input that stores the credit card number
*/
function guess_card_type(number, card_type) {

	var new_type = null;
	switch (number.substr(0, 1)) {
		case '4':
			// '4([0-9]{12}|[0-9]{15})'
			new_type = 'V';
			break;

		case '5':
			// '5[1-5][0-9]{14}'
			new_type = 'MC';
			break;

		case '6':
			// '6011[0-9]{12}'
			new_type = 'D';
			break;

		case '3':
			switch (number.substr(0, 1)) {
				case '6':
				case '8':
					// DINERS/CARTEBLANCHE '3(0[0-5][0-9]{11}|[68][0-9]{12})'
					new_type = 'DC';
					break;

/*					case 4:
				case 7:
					// '3[47][0-9]{13}'
					new_type = 'AE';
					break;

				case 0:
					// JCB '(3[0-9]{15}|(2131|1800)[0-9]{11})'
*/

			}
			break;

		case '2':
		case '1':
			// '(3[0-9]{15}|(2131|1800)[0-9]{11})'
			new_type = 'JCB';
			break;

		// ENROUTE '2(014|149)[0-9]{11}'
		}

	if (new_type && card_type.value != new_type) {
		for (var i = 0; i < card_type.options.length; i++) {
			if (card_type.options[i].value == new_type) {
				card_type.options[i].selected = true;
				alert('Card type automatically changed to ' + card_type.options[i].text);
				break;
				}
			}
		}
	}

///////////////////////////////////////////////////////////////////////////////