window.addEvent('domready', function(){ 
	$$('.noenter').each(function(elm){
		elm.addEvent('keydown',function(e){
			if(e.key=="enter") e.stop();
		});
	});
	if($chk($('slider-stage'))){
		var test = new ClickRotate();
	}
	updatepayments();
	if($chk($('payment_type'))){
		
		$('payment_type').addEvent("change",function(){
			if(this.value==2){
				$('card').setStyle('display','block');
				$('debit').setStyle('display','none');
				if($chk($('winter_payment_period'))) $('winter_payment_period').set('value',3);
				if($chk($('winter_payment_period'))) $('winter_payment_period').set('disabled',true);
				if($chk($('summer_payment_period'))) $('summer_payment_period').set('value',3);
				if($chk($('summer_payment_period'))) $('summer_payment_period').set('disabled',true);
				
				if($chk($('winter_payment_day'))) $('winter_payment_day').set('value',20);
				if($chk($('winter_payment_day'))) $('winter_payment_day').set('disabled',true);
				
				if($chk($('summer_payment_day'))) $('summer_payment_day').set('value',20);
				if($chk($('summer_payment_day'))) $('summer_payment_day').set('disabled',true);
			}else{
				if(this.value==1){
					$('card').setStyle('display','none');
					$('debit').setStyle('display','block');
				}else{
					$('card').setStyle('display','none');
					$('debit').setStyle('display','none');
				}
				if($chk($('winter_payment_day'))) $('winter_payment_day').set('disabled',false);
				if($chk($('summer_payment_day'))) $('summer_payment_day').set('disabled',false);
				if($chk($('winter_payment_period'))) $('winter_payment_period').set('disabled',false);
				if($chk($('summer_payment_period'))) $('summer_payment_period').set('disabled',false);
			}
			updatepayments();
		});
		if($chk($('winter_payment_period'))) paymentdates('winter_payment_month','winter_payment_year','winter_payment_day');
		if($chk($('summer_payment_period'))) paymentdates('summer_payment_month','summer_payment_year','summer_payment_day');
	}
	if($chk($('winter_payment_period'))){$('winter_payment_period').addEvent('change',function(){updatepayments();});}
	if($chk($('winter_payment_period'))){$('winter_payment_month').addEvent('change',function(){updatepayments();});}
	if($chk($('winter_payment_period'))){$('winter_payment_year').addEvent('change',function(){updatepayments();});}
	if($chk($('winter_payment_period'))){$('winter_payment_day').addEvent('change',function(){updatepayments();});}
	if($chk($('summer_payment_period'))){$('summer_payment_period').addEvent('change',function(){updatepayments();});}
	if($chk($('summer_payment_period'))){$('summer_payment_month').addEvent('change',function(){updatepayments();});}
	if($chk($('summer_payment_period'))){$('summer_payment_year').addEvent('change',function(){updatepayments();});}
	if($chk($('summer_payment_period'))){$('summer_payment_day').addEvent('change',function(){updatepayments();});}
	if($chk($('card_month'))) $('card_month').addEvent('click',function(){if(this.value="MM")this.value='';});
	if($chk($('card_year'))) $('card_year').addEvent('click',function(){if(this.value="MM")this.value='';});
	if($chk($('copydetails'))){
		$('copydetails').addEvent('click',function(e){
			e.stop();
			$('card_unit').set('value',unit);
			$('card_address').set('value',address);
			$('card_city').set('value',city);
			$('card_province').set('value',province);
			$('card_postcode').set('value',postcode);
		});
	}
	
	if($chk($('year'))) paymentdates('month','year','day');
	if($chk($('period'))){$('period').addEvent('change',function(){updatepayments();});}
	if($chk($('year'))){$('year').addEvent('change',function(){updatepayments();});}
	if($chk($('month'))){$('month').addEvent('change',function(){updatepayments();});}
	if($chk($('day'))){$('day').addEvent('change',function(){updatepayments();});}
	if($chk($('amount'))){$('amount').addEvent('blur',function(){updatepayments();});}
	$$('.radiochange').each(function(elm){
		elm.addEvent('click',function(){
			if(this.value.toInt()==3 && this.checked){
				$('newpayment').setStyle('display','block');
			}else{
				$('newpayment').setStyle('display','none');
			}
		});
	});
	
});

function updatepayments(){
	if($chk($('summer_payment_details'))){
		var query = "?summer=1&period="+escape($('summer_payment_period').value)+'&date='+escape($('summer_payment_year').value+'-'+$('summer_payment_month').value+'-'+$('summer_payment_day').value)
		$('summer_payment_details').load('/payment_months.asp'+query);
	}
	if($chk($('winter_payment_details'))){
		var query = "?winter=1&period="+escape($('winter_payment_period').value)+'&date='+escape($('winter_payment_year').value+'-'+$('winter_payment_month').value+'-'+$('winter_payment_day').value)
		$('winter_payment_details').load('/payment_months.asp'+query);
	}
	if($chk($('payment_details'))){
		var query = "?winter=1&period="+escape($('period').value)+'&date='+escape($('year').value+'-'+$('month').value+'-'+$('day').value)+'&amount='+escape($('amount').value)
		$('payment_details').load('/payment_months.asp'+query);
	}
	
	
}

function paymentdates(month,year,day){
	$(month).addEvent('change',function(){
		switch(this.value.toInt()){
			case 9:
			case 4:
			case 11:
			case 6:
				if($(day).value.toInt()>30) $(day).value=30;
				break;
			case 2:
				if(isLeapYear($(year).value.toInt())){
					if($(day).value.toInt()>29){
						$(day).value=29;
					}
				}else{
					if($(day).value.toInt()>28){
						$(day).value=28;
					}
				}
				break;
		}	
	});
	$(day).addEvent('change',function(){
		switch($(month).value.toInt()){
			case 9:
			case 4:
			case 11:
			case 6:
				if(this.value.toInt()>30){
					this.value=30;
					alert("Day must be 30 or smaller");
				}
				break;
			case 2:
				if(isLeapYear($(year).value.toInt())){
					if(this.value.toInt()>29){
						this.value=29;
						alert("Day must be 29 or smaller");
					}
				}else{
					if(this.value.toInt()>28){
						this.value=28;
						alert("Day must be 28 or smaller");
					}
				}
				break;
		}	
	});
}


var ClickRotate = new Class({
	Implements: [Options],
	options: {
		stage: 'slider-stage',
		sliders: 'slider-list',
		gotoclass: 'gotolink',
		gotoevent:'click',
		next:'next',
		nextevent:'click',
		transition:'fade',
		transitionduration:2000,
		clickduration:250,
		previous:'previous',
		previousevent:'click',
		autorotate:true,
		waittime:6000,
		returnrotate:true,
		returnafter:5000,
		returnid:null,
		intervalID:null,
		useactivebuttons:true,
		onbutton:'/gfx/splash_on.gif',
		offbutton:'/gfx/splash_off.gif',
		buttonsinject:'splashbuttons'
	},
	initialize: function(options) {
		this.setOptions(options);
		var op = this.options;
		var f = this;
		op.children = $(op.sliders).getChildren('li');
		if (op.transition == "fade"){
			var startloop = 1000+op.children.length;
			for(i=0;i<op.children.length;i++){
				op.children[i].setStyle("z-index",startloop);
				startloop--;
			}
		}
		if(op.useactivebuttons){
			op.clickbuttons = Array();
			op.currentbuttonid=0;
			for(i=0;i<op.children.length;i++){
				if(i==0){
					op.clickbuttons[i] = new Element('img', {'src': op.onbutton});
				}else{
					op.clickbuttons[i] = new Element('img', {'src': op.offbutton});
				}
				op.clickbuttons[i].inject($(op.buttonsinject));
			}
		}
		
		if(op.autorotate) op.intervalID = this.autorotate.periodical(op.waittime,this);
		if($type($(op.next))=='element'){
			$(op.next).addEvent(op.nextevent,function(e){
				e.stop();
				if($type(op.returnid)=='number') $clear(op.returnid);
				if($type(op.intervalID)=='number'){
					$clear(op.intervalID);
					if(op.returnrotate) op.returnid = f.autorotate.delay(op.returnafter,f);
				}
				var children = $(op.sliders).getChildren('li');
				if (op.transition == "fade"){
					children[0].set('tween',{duration:op.clickduration,onComplete:function(){
						children[0].setStyle("z-index",0).inject($(op.sliders)).setStyle('opacity',1);
						var children2 = $(op.sliders).getChildren('li');
						var startloop = 1000+op.children.length;
						for(i=0;i<children2.length;i++){
							children2[i].setStyle("z-index",startloop);
							startloop--;
						}
					}});
					children[0].tween('opacity', 0);
				}else{
					children[0].set('tween',{duration:op.clickduration,onComplete:function(){children[0].inject($(op.sliders)).setStyle('margin-left',0);}});
					children[0].tween('margin-left', -children[0].getSize().x);
				}
			});
		}
		if($type($(op.previous))=='element'){
			$(op.previous).addEvent(op.previousevent,function(e){
				e.stop();
				if($type(op.returnid)=='number') $clear(op.returnid);
				if($type(op.intervalID)=='number'){
					$clear(op.intervalID);
					if(op.returnrotate) op.returnid = f.autorotate.delay(op.returnafter,f);
				}
				var children = $(op.sliders).getChildren('li');
				var last = children.length-1;
				if (op.transition == "fade"){
					children[last].set('tween',{duration:op.clickduration,onComplete:function(){children[last].setStyle('opacity',1);op.infade=false;}});
					children[last].setStyle('opacity',0).inject($(op.sliders),'top');
					var children2 = $(op.sliders).getChildren('li');
					var startloop = 1000+op.children.length;
					for(i=0;i<children2.length;i++){
						children2[i].setStyle("z-index",startloop);
						startloop--;
					}
					children[last].tween('opacity', 1);
				}else{
					children[last].set('tween',{duration:op.clickduration,onComplete:function(){children[last].set('style','');}});
					children[last].setStyle('margin-left',-children[last].getSize().x).inject($(op.sliders),'top');
					children[last].tween('margin-left', 0);
				}
			});
		}
		$$("a."+op.gotoclass).each(function(elm){
			var slideid = elm.getProperty('rel');
			if ($type(slideid)=="string"){
				slideid=slideid.toInt();
				elm.addEvent(op.gotoevent,function(e){
					e.stop();
					if($type(op.returnid)=='number') $clear(op.returnid);
					if($type(op.intervalID)=='number'){
						$clear(op.intervalID);
						if(op.returnrotate) op.returnid = f.autorotate.delay(op.returnafter,f);
					}
					var children = $(op.sliders).getChildren('li');
					if (op.transition == "fade"){
						op.children[slideid].set('tween',{duration:op.clickduration,onComplete:function(){
								op.children[slideid].setStyle('opacity',1);
								for(i=slideid+1;i<op.children.length;i++) op.children[i].inject($(op.sliders),'bottom');
								for(i=0;i<slideid;i++) op.children[i].inject($(op.sliders),'bottom');
								var children2 = $(op.sliders).getChildren('li');
								var startloop = 1000+op.children.length;
								for(i=0;i<children2.length;i++){
									children2[i].setStyle("z-index",startloop);
									startloop--;
								}
								op.infade=false;
						},onStart:function(){op.infade=true;}});
						op.children[slideid].setStyle('opacity',0).inject($(op.sliders),'top');
						var children2 = $(op.sliders).getChildren('li');
						var startloop = 1000+op.children.length;
						for(i=0;i<children2.length;i++){
							children2[i].setStyle("z-index",startloop);
							startloop--;
						}
						op.children[slideid].tween('opacity', 1);
					}else{
						op.children[slideid].set('tween',{duration:op.clickduration,onComplete:function(){
							op.children[slideid].set('style','');
							for(i=slideid+1;i<op.children.length;i++) op.children[i].inject($(op.sliders),'bottom');
							for(i=0;i<slideid;i++) op.children[i].inject($(op.sliders),'bottom');
						}});
						op.children[slideid].setStyle('margin-left',-op.children[slideid].getSize().x).inject($(op.sliders),'top');
						op.children[slideid].tween('margin-left', 0);
					}
				});
			}

		
		});
		
	},
			
	autorotate:function(){
		var op = this.options;
		if($type(op.returnid)=='number'){
			$clear(op.returnid);
			op.returnid=null;
			op.intervalID = this.autorotate.periodical(op.waittime,this);
		}
		var children = $(op.sliders).getChildren('li');
		
		var toupdate = op.currentbuttonid+1;
			var lasupdate = op.currentbuttonid;
			if(toupdate>children.length-1){
					lasupdate = children.length-1;
					toupdate=0;
					op.currentbuttonid=0;
			}else{
				op.currentbuttonid++;
			}
			op.clickbuttons[toupdate].src = op.onbutton;
			op.clickbuttons[lasupdate].src = op.offbutton;
		
		if (op.transition == "fade"){
			children[0].set('tween',{duration:op.transitionduration,onComplete:function(){
				children[0].setStyle("z-index",0).inject($(op.sliders)).setStyle('opacity',1);
				var children2 = $(op.sliders).getChildren('li');
				var startloop = 1000+op.children.length;
				for(i=0;i<children2.length;i++){
					children2[i].setStyle("z-index",startloop);
					startloop--;
				}
			}});
			children[0].tween('opacity', 0);
		}else{
			children[0].set('tween',{duration:op.transitionduration,onComplete:function(){
				children[0].inject($(op.sliders)).setStyle('margin-left',0);
			}});
			children[0].tween('margin-left', -children[0].getSize().x);
		}
	}
		
	
});

function popup(w,h,page) {
var winl = (screen.width-w)/2;
var wint = (screen.height-h)/2;
if (winl < 0) winl = 0;
if (wint < 0) wint = 0;
windowprops = "height="+h+",width="+w+",top="+ wint +",left="+ winl +",location=no,"
+ "scrollbars=no,menubars=no,toolbars=no,resizable=no,status=no";
window.open(page, "Santas Choice", windowprops);
return false;
}

function popup2(w,h,page) {
var winl = (screen.width-w)/2;
var wint = (screen.height-h)/2;
if (winl < 0) winl = 0;
if (wint < 0) wint = 0;
windowprops = "height="+h+",width="+w+",top="+ wint +",left="+ winl +",location=no,"
+ "scrollbars=yes,menubars=no,toolbars=no,resizable=no,status=no";
window.open(page, "Santas Choice", windowprops);
return false;
}


function data_copy2(){
document.check.ship_unit.value=document.check.unit.value;
document.check.ship_address2.value=document.check.address2.value;
document.check.ship_town.value=document.check.town.value;
document.check.ship_county.value=document.check.county.value;
document.check.ship_postcode1.value=document.check.postcode1.value;
document.check.ship_postcode2.value=document.check.postcode2.value;
document.check.ship_title.value=document.check.title.value;
document.check.ship_first_name.value=document.check.first_name.value;
document.check.ship_last_name.value=document.check.last_name.value;
return false;
}
function isLeapYear(yr) {
  return new Date(yr,2-1,29).getDate()==29;
}

