
// Add a class name to the body as a global flag that the user has javascript.
document.observe('dom:loaded', function(){
	$(document.body).addClassName('js-enabled');
});
// end js-enabled

/*
 * EnterClick listens on a containing element for the enter/return key being pressed.
 * When the enter key is pressed within a child input element that will submit the form, 
 * it will prevent the default form submittal and fire the click event of a designated button.
 *  
 * Usage:
 * $('fieldset_forgot').onEnterClick($('btn_Forgot'));
 * currently used on /account/login.aspx
 */
var EnterClick = Class.create({
	relevantTypes: $w('text password checkbox radio'),
	initialize: function(element, button)
	{
		this.element = $(element).addClassName("enterclick");
		this.button = $(button);
		this.element.observe('keypress', this.__keyPress.bindAsEventListener(this));
	},
	__keyPress: function(e)
	{
		var elSrc = e.element();
		if((e.keyCode == Event.KEY_RETURN) 
			&& (elSrc.nodeName == 'INPUT') 
			&& (this.relevantTypes.include(elSrc.type)))
			{
				// prevent default form submittal
				e.stop(); 
				// fire associated button's click event
				this.button.click();
		}
	}
});
// Extend Element with enterclick
Element.addMethods({
	onEnterClick: function(element, button)
	{
		new EnterClick(element, button);
		return element;
	}
});
// end EnterClick

// start PrintPage
var PrintPage = Class.create ({
	initialize: function(link)
	{
		this.link = link;
		this.link.observe('click', this.__Click.bindAsEventListener(this));
	},
	__Click: function(e)
	{
		e.stop();
		window.print();
	}
});
// end PrintPage

// start PopupWindow
var PopupWindow = Class.create ({
    initialize: function(link, width, height, xtras)
    {
        this.popName = 'popup';
        this.link = link;
        this.url = this.link.href;
        this.altW = 640;
        this.altH = 480;
        this.altX = 'location=no,menubar=no,statusbar=no,toolbar=no,scrollbars=no,resizable=yes';
        this.popupW = width || this.altW;
        this.popupH = height || this.altH;
        this.popupX = xtras || this.altX;
        this.availH = screen.availHeight;
        this.availW = screen.availWidth;
        this.topPos = (this.availH - this.popupH)/2;
        this.leftPos = (this.availW - this.popupW)/2;
        this.features = 'width=' + this.popupW + ',height=' + this.popupH + ',' + this.popupX + ',top=' + this.topPos + ',left=' + this.leftPos;
        this.link.observe('click', this.__Click.bindAsEventListener(this));
    },
    __Click: function(e)
    {
        e.stop();
        var win = window.open(this.url, this.popName, this.features);
        win.focus();
    }
});
// end PopupWindow

// start ClosePopup
var ClosePopup = Class.create ({
    initialize: function(link)
    {
        this.link = link;
        this.link.observe('click', this.__Click.bindAsEventListener(this));
    },
    __Click: function(e)
    {
        e.stop();
        window.close();
    }
});
// end ClosePopup

// start HeightAdjuster
// set subItem to same height as mainItem
var HeightAdjuster = Class.create ({
	initialize: function(mainItem, subItem, options)
	{
		this.mainItem = mainItem;
		this.subItem = subItem;
		this.options = Object.extend({
			adjAmt: 0, // pass in an adjustment amount for standards compliant browsers
			adjIE: 0 // pass in an adjustment amount for ie6 / ie7
		}, options || {});
		this.adjustment = ie6 || ie7 ? this.options.adjIE : this.options.adjAmt;
		this.subTPad = parseInt(this.subItem.getStyle('padding-top')); // get sub items padding-top in pixels as a number
		this.subBPad = parseInt(this.subItem.getStyle('padding-bottom')); // get sub items padding-bottom in pixels as a number

		this.subItem.style.height = (this.mainItem.getHeight() - (this.subTPad + this.subBPad)) + this.adjustment + 'px';

	}
});
// end HeightAdjuster

// start UpdateSelectbox
// update secondary selectbox options per primary selectbox
// per the spec: The application will allow the patron to select up to 2 Half Price Child price tickets for each Standard price ticket to the same performance.
// note: for the sake of time, the UpdateSelectbox class was written only taking the spec reqs into account and is NOT configurable beyond passing in two unique selectbox IDs, all other options are hard-coded. CN.
var UpdateSelectbox = Class.create ({
	initialize: function(primeSB, secondSB)
	{
		this.primeSB = primeSB;
		this.secondSB = secondSB;
		this.secondSBOptions = this.secondSB.childElements();
			this.length = this.secondSBOptions.size();
			this.origLength = this.length;
		this.hideSecondSBOptions();

		this.primeSB.observe('change', this.__ChangePrimeSB.bindAsEventListener(this));

	},
	hideSecondSBOptions: function()
	{
		// remove all child options except first
        for (var i=1; i<this.length; i++)
        {
            this.secondSBOptions[i].remove();
        }
        this.length = 1;
	},
	__ChangePrimeSB: function()
	{
		var primeSBVal = $F(this.primeSB);
		if (primeSBVal == '--'){primeSBVal = 0}
		primeSBVal = parseInt(primeSBVal);
		this.updateSecondSBOptions(primeSBVal);
	},
	updateSecondSBOptions: function(primeSBVal)
	{
		this.hideSecondSBOptions();
		var numHPCtickets = primeSBVal * 2;
		this.newLength = numHPCtickets + 1;
        // insert child options
        for (var i=1; i<this.newLength; i++)
        {
            this.secondSB.insert({bottom: this.secondSBOptions[i]});
        }
        this.length = this.newLength;
        if (this.length > this.origLength){this.length = this.origLength}
	}
});
// end UpdateSelectbox



// start HandleCheckBoxes for register, update, and change account pages
// on click of checkbox: are any checked--> disable/enable interest checkbox accordingly
// if any interest checkboxes are checked, maintain enabled.
var HandleCheckBoxes = Class.create ({
	initialize: function(cbNoContact, cbsContactMethods, cbsUserInterests)
	{
		this.cbNoContact = cbNoContact;
		this.cbsContactMethods = cbsContactMethods;
		this.cbsUserInterests = cbsUserInterests;

		this.cbsContactMethods.invoke('observe', 'click', this.__ClickContactMethods.bindAsEventListener(this));

		var anyContactChecked = this.anythingChecked(this.cbsContactMethods);
		var anyInterestChecked = this.anythingChecked(this.cbsUserInterests);

		// set the initial enable/disable for data on load
		 this.enableCheckboxes(this.cbsUserInterests, anyContactChecked);

		this.cbNoContact.observe('click', this.__ClickNoContact.bindAsEventListener(this));
	},
	__ClickContactMethods: function(e)
	{
		var el = e.target || e.srcElement;
		var anyContactChecked = this.anythingChecked(this.cbsContactMethods);
		var anyInterestChecked = this.anythingChecked(this.cbsUserInterests);
		this.enableCheckboxes(this.cbsUserInterests, anyContactChecked);
		if (el.checked) {
			this.cbNoContact.checked = false;
		}
	},
	__ClickNoContact: function(e)
	{
	   var el = e.target || e.srcElement;
		if(el.checked){
			this.toggleCheckboxes( this.cbsUserInterests, false );
			this.toggleCheckboxes( this.cbsContactMethods, false );
			this.enableCheckboxes( this.cbsUserInterests, false )
		}
	},
	anythingChecked: function(arCheckboxes)
	{
		var anyChecked = false;
		for(var i=0; i < arCheckboxes.length; i++){
			if (arCheckboxes[i].checked){
				anyChecked = true
				continue;
			}
		}
		return anyChecked;
	},
	enableCheckboxes: function(arCheckboxes, bEnable)
	{
		arCheckboxes.each(
			function(chk){
				chk.disabled = (bEnable)? '' : 'disabled' ;
			}
		);
	},
	toggleCheckboxes: function(arCheckboxes, bChecked)
	{
		arCheckboxes.each(
			function(chk){
				chk.checked = (bChecked);
			}
		);
	}
});
// end HandleCheckBoxes
