/*

	RetailerInterface.js
	
*/
var RetailerInterface = new Class({
	options: { 
		PlanA: 'addPackageA', 
		PlanB: 'addPackageB',
		PlanC: 'addPackageC',
		CarteContainer: 'ALaCarte',
		SubmitBtn: 'upgrade_submit'
	},
	
	initialize: function(options) {
		this.setOptions(options);	
		
		this.SetupVariables();
		
		if( this.PlanA )
			this.PlanA.addEvent( 'click', this.PlanClick.bindWithEvent( this, this.PlanA ) );
		if( this.PlanB )
			this.PlanB.addEvent( 'click', this.PlanClick.bindWithEvent( this, this.PlanB ) );
		if( this.PlanC )
			this.PlanC.addEvent( 'click', this.PlanClick.bindWithEvent( this, this.PlanC ) );
		if( this.Carte.length > 0 )
		{
			for( var i = 0; i < this.Carte.length; i++ )
				this.Carte[i].addEvent( 'click', this.CarteClick.bindWithEvent( this, this.Carte[i] ) );
		}
		if( this.SubmitBtn )
			this.SubmitBtn.addEvent( 'click', this.SubmitUpgrades.bindWithEvent( this ) );
		
	}, 
	
	SetupVariables: function() {
		this.PlanA     = $(this.options.PlanA);
		this.PlanB     = $(this.options.PlanB);
		//this.PlanC = $(this.options.PlanC);
		this.Carte     = $$( '#' + this.options.CarteContainer + ' input' );
		this.SubmitBtn = $(this.options.SubmitBtn);
		this.Form      = $(this.options.retail_upgrade);
		
	}, 
	
	PlanClick: function( event, theEle ) {
		
		if( (theEle.name == this.options.PlanA) && ( theEle.checked ))
		{
			this.PlanB.checked = false; 
			//this.PlanC.checked = false; 
			this.ClearCarte();
		}
		else if( (theEle.name == this.options.PlanB) && ( theEle.checked ) )
		{
			this.PlanA.checked = false; 
			//this.PlanC.checked = false; 
			this.ClearCarte();
		}
		else if( (theEle.name == this.options.PlanC) && ( theEle.checked ) )
		{
			this.PlanA.checked = false; 
			this.PlanB.checked = false; 
			this.ClearCarte();
		}
	}, 
	
	CarteClick: function( event, theEle ) {
		
		this.PlanA.checked = false; 
		this.PlanB.checked = false; 
		//this.PlanC.checked = false; 
			
		if( theEle.checked )
			$(theEle.name + '_Sel').style.display = 'block';
		else
			$(theEle.name + '_Sel').style.display = 'none';
		
	}, 
	
	ClearCarte: function() {
		this.Carte.each( function( element, i ) {
			element.checked = false; 
			$(element.name + '_Sel').style.display = 'none';
		});	
	},
	
	
	SubmitUpgrades: function( event ) {
		
		//event.stop();
		
		var isReady = true; 
		
		this.Carte.each( function( element, i ) {
			if( element.checked && $(element.name + '_length').value == "" )
			{
				alert( 'Please Select a length of time for your item! ' );
				isReady = false; 
			}
		});
				
		this.CheckCard(event);	
	},
	
	CheckCard: function( event ) {
		// Over-ride of the CheckPayment in cc.js
		var form     = $('parent_register');
		var tmpyear  = '';
		var tmpmonth = '';
		var SubmitMe = true; 
		
		if (form.ccname.value.length == 0) {
			this.showHint( event, $('ccname') );
			form.ccname.focus();
			SubmitMe = false;
		}
		if (form.ccnum.value.length == 0) {
			this.showHint( event, $('ccnum') );
			form.ccnum.focus();
			SubmitMe = false;
		}
		if (form.ccmo.options[form.ccmo.selectedIndex].value == 0) {
			this.showHint( event, $('ccmo') );
			form.ccmo.focus();
			SubmitMe = false;
		}
		if (form.ccyr.value.length == 0) {
			this.showHint( event, $('ccyr') );
			form.ccyr.focus();
			SubmitMe = false;
		}
		if(form.cvv2.value.length == 0) {
			this.showHint( event, $('cvv2') );
			form.cvv2.focus();
			SubmitMe = false;
		}
		if(!isNum(form.cvv2.value)) {
			this.showHint( event, $('cvv2') );
			form.cvv2.focus();
			SubmitMe = false;
		}
		
		if( SubmitMe ) {
			tmpyear  = form.ccyr.value;
			tmpmonth = form.ccmo.options[form.ccmo.selectedIndex].value;
			
			if (!(new CardType()).isExpiryDate(tmpyear, tmpmonth)) {
				$('error_ccmo').innerHTML = "<p>This card has already expired</p>";
				this.showHint( event, $('ccmo') );
				SubmitMe = false;
			}
			
			card = form.CardType.options[form.CardType.selectedIndex].value;
			var retval = eval(card + ".checkCardNumber(\"" + form.ccnum.value + "\", " + tmpyear + ", " + tmpmonth + ");");
			cardname = "";
			if (retval) {
				
			}
			else
			{
				// The cardnumber has the valid luhn checksum, but we want to know which
				// cardtype it belongs to.
				for (var n = 0; n < Cards.size; n++) 
				{
					if (Cards[n].checkCardNumber(form.ccnum.value, tmpyear, tmpmonth)) {
						cardname = Cards[n].getCardType();
						break;
					}
				}
				if (cardname.length > 0) {
					$('error_CardType').innerHTML = "<p>This looks like a " + cardname + " number, not a " + card + " number.</p>"
					this.showHint( event, $('CardType') );
					SubmitMe = false;
				}
				else {
					$('error_ccnum').innerHTML = "<p>This card number is not valid</p>"
					this.showHint( event, $('ccnum') );
					SubmitMe = false;
				} 
			}
		}
		
		if( !SubmitMe ){
			event.stop();
		} else {
		}
	},
	
	showHint: function( event, _id ) {
		if( _id ) {
			var newID     = 'error_' + _id.id;
			if( $( newID ) ) {
				$( newID ).setStyles({'display': 'block' });
				return true;
			}
			return false; 
		}
		return false; 
	}
	
});

RetailerInterface.implement(new Options);

window.addEvent('load', function() {
	var Retailer = new RetailerInterface(); 
});
	
	
