/**************************************************
 * Mixonic Duplication 
 * Pricer (Quick Quote) Tool
 **************************************************/

// Create button class
var Button = Class.create();

Button.prototype = {

	// initialize 
	initialize: function(el) {
		// attach element (el) to the button
		this.el = $(el);
		
		// attach radio to the button
		this.radio  = $(el+'-radio');				

		// an array for other buttons that set the same variable
		this.others = Array();

		// setup an action function 
		this.action = function() {};
		
		// enable button
		this.enable();
		
		// if the radio is already checked, activate the button
		if(this.radio.checked) {
			this.activate();
		}
		
		// setup hover for element
		this.el.onmouseover = this.onMouseOver.bindAsEventListener(this);
		this.el.onmouseout = this.onMouseOut.bindAsEventListener(this);
		
		// setup onclick for element
		this.el.onmousedown = this.onClick.bindAsEventListener(this);
		
		// hide radio button and label
		this.radio.style.visibility = 'hidden';
		this.el.getElementsByTagName('label')[0].style.visibility = 'hidden';
	},
	
	// disable the button
	disable: function() {
		this.enabled = false;
		this.active = false;
		Element.addClassName(this.el, 'disabled');
		Element.removeClassName(this.el, 'option');
		Element.removeClassName(this.el, 'active');
		this.radio.checked = false;
	},
	
	// enable the button (can be activated)
	enable: function() {
		if(!this.active) {
			this.enabled = true;
			Element.addClassName(this.el, 'option');
			Element.removeClassName(this.el, 'disabled');
			Element.removeClassName(this.el, 'active');
		}
	},
	
	// activate the button
	activate: function() {
		this.enabled = false;
		this.active = true;
		Element.addClassName(this.el, 'active');
		Element.removeClassName(this.el, 'option');
		Element.removeClassName(this.el, 'disabled');
		Element.removeClassName(this.el, 'hover');
		this.radio.checked = true;
		
		// deactivate others
		for(var i = 0; i < this.others.length; i++) {
			this.others[i].deactivate();
		}
	},
	
	// deactivate the button
	deactivate: function() {
		if(this.active) {
			this.active = false;
			this.enable();
			this.radio.checked = false;
		}
	},
	
	// mouseover event
	onMouseOver: function() {
		if(this.enabled) {
			Element.addClassName(this.el, 'hover');
		}
	},
	
	// mouseout event
	onMouseOut: function() {
		Element.removeClassName(this.el, 'hover');
	},
	
	// onclick event
	onClick: function() {
		if(this.enabled) {
			this.activate();	// activate button
			this.action();		// perform any actions
		}
	},
	
	// specify which other buttons control the same variable
	setOthers: function() {
		var elements = [];
		$c(arguments).each(function(el){
			elements.push($(el));
		});
		this.others = elements;
	}
};

// Upon load, create buttons and set up dependencies
if(document.getElementById) {
	attachOnload( function() {
		
		/***** set up transitions *****/
		
		// define transition duration
		var transitionDuration = 250;
		
		// submit button
		var getPrice = $('getPrice');
		
		var populateProject = $('populateProject');
		var populateProject1 = $('populateProject1');
		

		
		// extend the fx.Height class to allow expand() and collapse() functions
		Object.extend(fx.Height.prototype, {
			expand: function() {
				if (this.el.offsetHeight == 0) this.custom(0, this.el.scrollHeight);
			},
			
			collapse: function() {
				if (this.el.offsetHeight > 0) this.custom(this.el.offsetHeight, 0);
			}
		});
		
		// create transition for disc surface selection 
		var toggleSurface = new fx.Height('disc-surface', { duration: transitionDuration });
		toggleSurface.hide();
		
		// create transition for cellphone wrap selection
		var toggleNofinishing = new fx.Height('no-finishing', { duration: transitionDuration });
		toggleNofinishing.hide();
		
		// create transition for cellphone wrap selection
		var toggleCello = new fx.Height('cellophane', { duration: transitionDuration });
		toggleCello.hide();
		
		// create transition for shrink wrap selection
		var toggleShrinkWrap = new fx.Height('shrink-wrap', { duration: transitionDuration });
		toggleShrinkWrap.hide();
		
		// create transition for wafer seal selection
		var toggleWaferseal = new fx.Height('wafer-seal', { duration: transitionDuration });
		toggleWaferseal.hide();
		
		// create transitions for tray card and dvd inside insert 2-sided checkboxes
		var toggleTrayCard2Sided = new fx.Height('insertType-traycard-2sided', { duration: transitionDuration });
		var toggleDvdInside2Sided = new fx.Height('insertType-dvdInside-2sided', { duration: transitionDuration });
		toggleTrayCard2Sided.hide();
		toggleDvdInside2Sided.hide();
		
		// transition for digipak
		var toggleDigipak = new fx.Height('caseType-digipak-inner', { duration: transitionDuration });
		toggleDigipak.hide();
		
		// create transitions for cd blank and dvd blank checkboxes
		var toggleCDBlank = new fx.Height('contentType-cd-blank', { duration: transitionDuration });
		var toggleDVDBlank = new fx.Height('contentType-dvd-blank', { duration: transitionDuration });
		toggleCDBlank.hide();
		toggleDVDBlank.hide();
		
		//var toggleSelect99 =  new fx.Height('jacket-qty', { duration: transitionDuration });
		/*
		// create transition for production type and quantity
		var toggleProduction = new fx.Height('production', { duration: transitionDuration });
		toggleProduction.hide();
		*/
		
		// function to check toggled items status
		function checkToggles() {
			
			var anyFinishing = false;
			// if disc type is CD or DVD
			if(discTypeCd.active || discTypeDvd.active) {
				// expand disc surface options
				toggleSurface.expand();
				
				// allow cellophane if case is Standard Jewel and disc is not blank
				if(caseTypeStandard.active) {
					if(discTypeCd.active && document.getElementById('cdBlank').checked == false){
						toggleCello.expand();
						toggleNofinishing.expand();
						if(!justLoaded)
							$('nofinishing').checked = 'checked';
						anyFinishing = true;
					} else if(discTypeDvd.active && document.getElementById('dvdBlank').checked == false){
						toggleCello.expand();
						toggleNofinishing.expand();
						if(!justLoaded)
							$('nofinishing').checked = 'checked';
						anyFinishing = true;
					}else{
						if(!justLoaded)
							$('cellophaneWrap').checked = '';
						toggleCello.collapse();
					}
				}
				// allow cellophane if case is DVD and insert is DVD slip and disc is not blank
				else if(caseTypeDvd.active && insertTypeDvd.active) {
					if(discTypeCd.active && document.getElementById('cdBlank').checked == false){
						toggleCello.expand();
						toggleNofinishing.expand();
						if(!justLoaded)
							$('nofinishing').checked = 'checked';
						anyFinishing = true;
					} else if(discTypeDvd.active && document.getElementById('dvdBlank').checked == false){
						toggleCello.expand();
						toggleNofinishing.expand();
						if(!justLoaded)
							$('nofinishing').checked = 'checked';
						anyFinishing = true;
					}else{
						if(!justLoaded)
							$('cellophaneWrap').checked = '';
						toggleCello.collapse();
					}
				}
				else {
					if(!justLoaded)
						$('cellophaneWrap').checked = '';
					toggleCello.collapse();
				}
				
				
				//allow shrinkwrap if case is printed jacket, printed wallet, or thin jewel and disc is not blank
				if(caseTypeJacket.active || caseTypeWallet.active || caseTypeDigipak.active || caseTypeThin.active) {
					if(discTypeCd.active && document.getElementById('cdBlank').checked == false){
						toggleShrinkWrap.expand();
						toggleNofinishing.expand();
						if(!justLoaded)
							$('nofinishing').checked = 'checked';
						anyFinishing = true;
					} else if(discTypeDvd.active && document.getElementById('dvdBlank').checked == false){
						toggleShrinkWrap.expand();
						toggleNofinishing.expand();
						anyFinishing = true;
					
					}else{
						if(!justLoaded)
							$('shrinkWrap').checked = '';
						toggleShrinkWrap.collapse();
					}
				} else {
					if(!justLoaded)
						$('shrinkWrap').checked = '';
					toggleShrinkWrap.collapse();
				}
				
				//allow wafer seal if case is printed jacket or printed wallet and disc is not blank
				if(caseTypeJacket.active || caseTypeWallet.active || caseTypeDigipak.active) {
					if(discTypeCd.active && document.getElementById('cdBlank').checked == false){
						toggleWaferseal.expand();
						toggleNofinishing.expand();
						if(!justLoaded)
							$('waferseal').checked = 'checked';
						anyFinishing = true;
					} else if(discTypeDvd.active && document.getElementById('dvdBlank').checked == false){
						toggleWaferseal.expand();
						toggleNofinishing.expand();
						if(!justLoaded)
							$('waferseal').checked = 'checked';
						anyFinishing = true;
					}else{
						if(!justLoaded)
							$('waferseal').checked = '';
						toggleWaferseal.collapse();
					}
				} else {
					if(!justLoaded)
						$('waferseal').checked = '';
					toggleWaferseal.collapse();
				}
			}
			else {
				toggleSurface.collapse();
				toggleCello.collapse();
				toggleShrinkWrap.collapse();
				toggleWaferseal.collapse();
				toggleNofinishing.collapse();
			}
			
			// cd blank option
			if(discTypeCd.active) {
				toggleCDBlank.expand();
				//toggleQtySelect('cdBlank');
			}
			else {
				toggleCDBlank.collapse();
			}
			
			// dvd blank option
			if(discTypeDvd.active) {
				toggleDVDBlank.expand();
				//toggleQtySelect('dvdBlank');
			}
			else {
				toggleDVDBlank.collapse();
			}
			// tray card 2-sided option
			if(insertTypeTrayCard.active) {
				toggleTrayCard2Sided.expand();
			}
			else {
				toggleTrayCard2Sided.collapse();
			}
			
			// dvd inside insert 2-sided option
			if(insertTypeDvdInside.active) {
				toggleDvdInside2Sided.expand();
			}
			else {
				toggleDvdInside2Sided.collapse();
			}
			
			// digipak option
			if(caseTypeDigipak.active) {
				toggleDigipak.expand();
			}
			else {
				toggleDigipak.collapse();
			}

			// disable submit button unless something is selected
			if(discTypeNone.active && caseTypeNone.active && insertTypeNone.active) {
				 getPrice.disabled = true;
				 populateProject.disabled = true;
				 populateProject1.disabled = true;
			} 
			else {
				  getPrice.disabled = false;
				  populateProject.disabled = false;
				  populateProject1.disabled = false;
			}
			
			if (!anyFinishing) {
				toggleNofinishing.collapse();
			}
			checkTogglePasses++;
			
			if(checkTogglePasses==3 && !insertTypeTrayCard.active && !insertTypeDvdInside.active)
				justLoaded=false;
			if(checkTogglePasses==4 && (insertTypeTrayCard.active || insertTypeDvdInside.active))
				justLoaded=false;
		
		}
		
		var quantityTextInput = $('quantity-text-input'); 
	    var quantitySelectSelect = $('quantity-select-select'); 
	    var stateSelect = $('stateSelectId');
		
		function disableCaseSelection(){
			caseTypeThin.disable();
			caseTypeStandard.disable();
			caseTypeFlexible.disable();
			caseTypeDvd.disable();
			caseTypeSleeve.disable();
			caseTypeJacket.disable();
			caseTypeWallet.disable();
		}
		
		function enableCaseSelection(){
			caseTypeThin.enable();
			caseTypeStandard.enable();
			caseTypeFlexible.enable();
			caseTypeDvd.enable();
			caseTypeSleeve.enable();
			caseTypeJacket.enable();
			caseTypeWallet.enable();
		}
		
		function enableConditionalCaseSelection(){
			if(insertTypeTwoPanel.active){
				caseTypeThin.enable();
				caseTypeStandard.enable();
			}
			if(insertTypeFourPanel.active){
				caseTypeThin.enable();
				caseTypeStandard.enable();
			}
			if(insertTypeTrayCard.active){
				caseTypeStandard.enable();
			}
			if(insertTypeDvd.active){
				caseTypeDvd.enable();
			}
			if(insertTypeDvdInside.active){
				caseTypeDvd.enable();
			}
			if(insertTypeNone.active){
				caseTypeThin.enable();
				caseTypeStandard.enable();
				caseTypeFlexible.enable();
				caseTypeDvd.enable();
				caseTypeSleeve.enable();
				caseTypeJacket.enable();
				caseTypeWallet.enable();
			}
		
			
		}
		
		function enable99qty(select){
				// when the first element is not 50 (for printed wallet/jacket)
				if (select.options[0].value >= 100)
				{
					insertOptionAt(select,new Option('1 to 99','1'),0);
				}	
				// for printed wallet/jacket
				else if(select.options[0].value == 50)
				{
					//change first element to 1 to 99
					select.options[0].innerHTML = '1 to 99';
					select.options[0].value = '1';

					// remove quantity 75
					removeOptionFrom(select,1);
					// show textfield if the first or second item is selected
					document.getElementById('quantity-text-input').value='50';
					if(select.selectedIndex==0 || select.selectedIndex==1)
					{
						select.selectedIndex=0;
						document.getElementById('quantity-text').style.display = '';
						
					}
				}
		}
		
		
		/***** instantiate buttons *****/
		
		var discTypeCd = new Button('discType-cd');
		var discTypeDvd = new Button('discType-dvd');
		var discTypeNone = new Button('discType-none');
		
		var discSurfaceColor = new Button('discSurface-color');
		var discSurfaceColorSilver = new Button('discSurface-colorSilver');
		var discSurfaceBlackSilver = new Button('discSurface-blackSilver');
		var discSurfaceBlackWhite = new Button('discSurface-blackWhite');
		
		var caseTypeThin = new Button('caseType-thin');
		var caseTypeStandard = new Button('caseType-standard');
		var caseTypeFlexible = new Button('caseType-flexible');
		var caseTypeDvd = new Button('caseType-dvd');
		var caseTypeSleeve = new Button('caseType-sleeve');
		var caseTypeJacket = new Button('caseType-jacket');		
		var caseTypeWallet = new Button('caseType-wallet');		
		var caseTypeDigipak = new Button('caseType-digipak');		
		var caseTypeNone = new Button('caseType-none');
		
		var insertTypeTwoPanel = new Button('insertType-twopanel');
		var insertTypeFourPanel = new Button('insertType-fourpanel');
		var insertTypeSixPanel = new Button('insertType-sixpanel');
		var insertTypeTrayCard = new Button('insertType-traycard');
		var insertTypeDvd = new Button('insertType-dvd');
		var insertTypeDvdInside = new Button('insertType-dvdInside');
		var insertTypeNone = new Button('insertType-none');
		
		/***** assign actions to each button *****/
		
		// Disc Type: CD
		discTypeCd.setOthers(discTypeDvd, discTypeNone);
		discTypeCd.action = function() {
			checkToggles();
		};
		
		// Disc Type: DVD
		discTypeDvd.setOthers(discTypeCd, discTypeNone);
		discTypeDvd.action = function() {
			checkToggles();
		};
		
		// Disc Type: None
		discTypeNone.setOthers(discTypeDvd, discTypeCd);
		discTypeNone.action = function() {
			checkToggles();
		};
				
		// Disc Surface: Full Color
		discSurfaceColor.setOthers(discSurfaceColorSilver,discSurfaceBlackSilver,discSurfaceBlackWhite);
		
		// Disc Surface: Color on Silver
		discSurfaceColorSilver.setOthers(discSurfaceColor,discSurfaceBlackSilver,discSurfaceBlackWhite);
		
		// Disc Surface: Black on Silver
		discSurfaceBlackSilver.setOthers(discSurfaceColor,discSurfaceColorSilver,discSurfaceBlackWhite);
		
		// Disc Surface: Black on White
		discSurfaceBlackWhite.setOthers(discSurfaceColor,discSurfaceColorSilver,discSurfaceBlackSilver);
		
		// Case Type: Thin Jewel
		caseTypeThin.setOthers(caseTypeStandard,caseTypeFlexible,caseTypeDvd,caseTypeSleeve,caseTypeJacket,caseTypeWallet,caseTypeDigipak,caseTypeNone);
		caseTypeThin.action = function() {
			checkToggles();
			insertTypeTwoPanel.enable();
			insertTypeFourPanel.enable();
			insertTypeSixPanel.enable();
			insertTypeNone.enable();
			insertTypeTrayCard.disable();
			insertTypeDvd.disable();
			insertTypeDvdInside.disable();
			
			if(!caseTypeJacket.active){
			enable99qty(document.getElementById('quantity-select-select'));
			enable99qty(document.getElementById('quantity-select-blank'));
			}
		};
		
		// Case Type: Standard Jewel
		caseTypeStandard.setOthers(caseTypeThin,caseTypeFlexible,caseTypeDvd,caseTypeSleeve,caseTypeJacket,caseTypeWallet,caseTypeDigipak,caseTypeNone);
		caseTypeStandard.action = function() {
			checkToggles();
			insertTypeTwoPanel.enable();
			insertTypeFourPanel.enable();
			insertTypeSixPanel.enable();
			insertTypeNone.enable();
			insertTypeTrayCard.enable();
			insertTypeDvd.disable();
			insertTypeDvdInside.disable();
			
			if(!caseTypeJacket.active){
			enable99qty(document.getElementById('quantity-select-select'));
			enable99qty(document.getElementById('quantity-select-blank'));
			}
		};
		
		// Case Type: Flexible Plastic
		caseTypeFlexible.setOthers(caseTypeThin,caseTypeStandard,caseTypeDvd,caseTypeSleeve,caseTypeJacket,caseTypeWallet,caseTypeDigipak,caseTypeNone);
		caseTypeFlexible.action = function() {
			checkToggles();
			insertTypeTwoPanel.disable();
			insertTypeFourPanel.disable();
			insertTypeSixPanel.disable();
			insertTypeNone.enable();
			insertTypeTrayCard.disable();
			insertTypeDvd.disable();
			insertTypeDvdInside.disable();
			
			if(!caseTypeJacket.active){
			enable99qty(document.getElementById('quantity-select-select'));
			enable99qty(document.getElementById('quantity-select-blank'));
			}
		};
		
		// Case Type: Dvd Style
		caseTypeDvd.setOthers(caseTypeThin,caseTypeStandard,caseTypeFlexible,caseTypeSleeve,caseTypeJacket,caseTypeWallet,caseTypeDigipak,caseTypeNone);
		caseTypeDvd.action = function() {
			checkToggles();
			insertTypeTwoPanel.disable();
			insertTypeFourPanel.disable();
			insertTypeSixPanel.disable();
			insertTypeNone.enable();
			insertTypeTrayCard.disable();
			insertTypeDvd.enable();
			insertTypeDvdInside.enable();
			
			if(!caseTypeJacket.active){
			enable99qty(document.getElementById('quantity-select-select'));
			enable99qty(document.getElementById('quantity-select-blank'));
			}
		};
		
		// Case Type: Paper Sleeve
		caseTypeSleeve.setOthers(caseTypeThin,caseTypeStandard,caseTypeFlexible,caseTypeDvd,caseTypeJacket,caseTypeWallet,caseTypeDigipak,caseTypeNone);
		caseTypeSleeve.action = function() {
			checkToggles();
			insertTypeTwoPanel.disable();
			insertTypeFourPanel.disable();
			insertTypeSixPanel.disable();
			insertTypeNone.enable();
			insertTypeTrayCard.disable();
			insertTypeDvd.disable();
			insertTypeDvdInside.disable();
			
			if(!caseTypeJacket.active){
			enable99qty(document.getElementById('quantity-select-select'));
			enable99qty(document.getElementById('quantity-select-blank'));
			}
		};
		
		// Case Type: Printed Jacket
		caseTypeJacket.setOthers(caseTypeThin,caseTypeStandard,caseTypeFlexible,caseTypeDvd,caseTypeSleeve,caseTypeWallet,caseTypeDigipak,caseTypeNone);
		caseTypeJacket.action = function() {
			checkToggles();
			insertTypeTwoPanel.disable();
			insertTypeFourPanel.disable();
			insertTypeSixPanel.disable();
			insertTypeNone.enable();
			insertTypeTrayCard.disable();
			insertTypeDvd.disable();
			insertTypeDvdInside.disable();
			
			enable99qty(document.getElementById('quantity-select-select'));
			enable99qty(document.getElementById('quantity-select-blank'));
			if(caseTypeJacket.active){
				correctDropdownForJacketAndWallet(document.getElementById('quantity-select-select'));
				correctDropdownForJacketAndWallet(document.getElementById('quantity-select-blank'));
				document.getElementById('quantity-text').style.display = 'none';
			}
		};
		
		// Case Type: Printed Wallet
		caseTypeWallet.setOthers(caseTypeThin,caseTypeStandard,caseTypeFlexible,caseTypeDvd,caseTypeSleeve,caseTypeJacket,caseTypeDigipak,caseTypeNone);
		caseTypeWallet.action = function() {
			checkToggles();
			insertTypeTwoPanel.disable();
			insertTypeFourPanel.disable();
			insertTypeSixPanel.disable();
			insertTypeNone.enable();
			insertTypeTrayCard.disable();
			insertTypeDvd.disable();
			insertTypeDvdInside.disable();
			if(caseTypeWallet.active){
				correctDropdownForJacketAndWallet(document.getElementById('quantity-select-select'));
				correctDropdownForJacketAndWallet(document.getElementById('quantity-select-blank'));
				document.getElementById('quantity-text').style.display = 'none';
			}
		};
		
		// Case Type: Digipak
		caseTypeDigipak.setOthers(caseTypeThin,caseTypeStandard,caseTypeFlexible,caseTypeDvd,caseTypeSleeve,caseTypeJacket,caseTypeWallet,caseTypeNone);
		caseTypeDigipak.action = function() {
			checkToggles();
			insertTypeTwoPanel.disable();
			insertTypeFourPanel.disable();
			insertTypeSixPanel.disable();
			insertTypeNone.enable();
			insertTypeTrayCard.disable();
			insertTypeDvd.disable();
			insertTypeDvdInside.disable();
			if(caseTypeDigipak.active){
				correctDropdownForJacketAndWallet(document.getElementById('quantity-select-select'));
				correctDropdownForJacketAndWallet(document.getElementById('quantity-select-blank'));
				document.getElementById('quantity-text').style.display = 'none';
			}
		};

		// Case Type: None
		caseTypeNone.setOthers(caseTypeThin,caseTypeStandard,caseTypeFlexible,caseTypeDvd,caseTypeSleeve,caseTypeJacket,caseTypeWallet,caseTypeDigipak);
		caseTypeNone.action = function() {
			checkToggles();
			insertTypeTwoPanel.enable();
			insertTypeFourPanel.enable();
			insertTypeSixPanel.enable();
			insertTypeNone.enable();
			insertTypeTrayCard.enable();
			insertTypeDvd.enable();
			insertTypeDvdInside.enable();

			if(!caseTypeJacket.active){
			enable99qty(document.getElementById('quantity-select-select'));
			enable99qty(document.getElementById('quantity-select-blank'));
			}
		};
		
		// Insert Type: TwoPanel
		insertTypeTwoPanel.setOthers(insertTypeFourPanel,insertTypeDvdInside,insertTypeDvd,insertTypeNone,insertTypeSixPanel);
		insertTypeTwoPanel.action = function() {
			checkToggles();
			
		};
		
		// Insert Type: FourPanel
		insertTypeFourPanel.setOthers(insertTypeTwoPanel,insertTypeDvdInside,insertTypeDvd,insertTypeNone,insertTypeSixPanel);
		insertTypeFourPanel.action = function() {
			checkToggles();
			
		};
		
		// Insert Type: SixPanel
		insertTypeSixPanel.setOthers(insertTypeTwoPanel,insertTypeDvdInside,insertTypeDvd,insertTypeNone,insertTypeFourPanel);
		insertTypeSixPanel.action = function() {
			checkToggles();
			
		};
		
		// Insert Type: TrayCard
		insertTypeTrayCard.setOthers(insertTypeDvdInside,insertTypeDvd,insertTypeNone);
		insertTypeTrayCard.action = function() {
			checkToggles();
			
		};
		
		// Insert Type: Dvd Inside
		insertTypeDvdInside.setOthers(insertTypeTwoPanel,insertTypeFourPanel,insertTypeSixPanel,insertTypeTrayCard,insertTypeNone);
		insertTypeDvdInside.action = function() {
			checkToggles();
		
		};
		
		// Insert Type: Dvd
		insertTypeDvd.setOthers(insertTypeTwoPanel,insertTypeFourPanel,insertTypeSixPanel,insertTypeTrayCard,insertTypeNone);
		insertTypeDvd.action = function() {
			checkToggles();
			
		};
					
		// Insert Type: None
		insertTypeNone.setOthers(insertTypeTwoPanel,insertTypeFourPanel,insertTypeSixPanel,insertTypeTrayCard,insertTypeDvd,insertTypeDvdInside);
		insertTypeNone.action = function() {
			checkToggles();
			
		};
		
		/***** Set up reset button *****/
		var reset = $('reset');
		reset.onclick = function() {
			if(confirm('Are you sure you want to reset all of your selections?')) {
				discTypeNone.activate();
				discTypeNone.action();
				discSurfaceColor.activate();
				discSurfaceColor.action();
				insertTypeNone.activate();
				insertTypeNone.action();
				caseTypeNone.activate();
				caseTypeNone.action();
				enableCaseSelection();
				
				stateSelect.selectedIndex = 0;
			
				//setQuantityControl('selectValue');
				quantityTextInput.value = '';
				document.getElementById('quantity-text').style.display = 'none';
				quantitySelectSelect.value = '300';
				document.getElementById('quantity-select-value-blank').style.display = 'none';
				document.getElementById('quantity-select-value').style.display = '';
				$('cellophaneWrap').checked = '';
				$('shrinkWrap').checked = '';
				$('waferseal').checked = '';
				$('traycardFlavor').checked = '';
				$('dvdInsideFlavor').checked = '';
				$('cdBlank').checked = '';
				$('dvdBlank').checked = '';
				document.getElementById('digipak_cd_radio').checked = true;
				document.getElementById('digipak_dvd_radio').checked = false;
				if(quoteDetails = $('quote-details')) {
					quoteDetails.parentNode.removeChild(quoteDetails);
				}
			}
		};
		
		/***** Upon load, perform activation actions for any pre-selected buttons *****/
		
		// array of buttons
		buttons = Array(
			discTypeCd,
			discTypeDvd,
			discTypeNone,
			caseTypeThin,
			caseTypeStandard,
			caseTypeFlexible,
			caseTypeDvd,
			caseTypeSleeve,
			caseTypeJacket,
			caseTypeWallet,
			caseTypeDigipak,
			caseTypeNone,
			insertTypeTwoPanel,
			insertTypeFourPanel,
			insertTypeSixPanel,
			insertTypeTrayCard,
			insertTypeDvd,
			insertTypeDvdInside,
			insertTypeNone
		);
		
		// perform activation routines for selected buttons
		for(var i = 0; i < buttons.length; i++) {
			if(buttons[i].active) {
				buttons[i].activate();
				buttons[i].action();
			}
		}
		
		checkQty('quantity-select-select');
		
	});
}

// Attach an onload event 
function attachOnload(handler)
{
	if(window.addEventListener) {
		window.addEventListener("load",handler,false);
	}
	else if(window.attachEvent) {
		window.attachEvent("onload",handler);
	}
	else if(window.onload) {
		var oldHandler = window.onload;
		window.onload = function piggyback() {
			oldHandler();
			handler();
		};
	}
	else {
		window.onload = handler;
	}
}

function checkQty(selectbox){

	var s = document.getElementById(selectbox);
	var div  = 'quantity-text';
	var textField = 'quantity-text-input';
	// display textfiled for quantities smaller than 100 but not for 50 and 75 which are for printed jacket/wallet
	if( s.options[s.selectedIndex].value  < 100 && s.options[s.selectedIndex].innerHTML!='50' && s.options[s.selectedIndex].innerHTML!='75') {
		if (document.getElementById(textField).value == ''){ 
			document.getElementById(textField).value='50';
		}
		document.getElementById(div).style.display = '';
	}
	else{
		document.getElementById(textField).value='';
		document.getElementById(div).style.display = 'none';
	}
	
}

function disableEnterKey(e)
{
     var key;      
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox      

     return (key != 13);
}


function toggleQtySelect(blankcheckbox){
	
	if (document.getElementById(blankcheckbox).checked == true){
		if (document.getElementById('quantity-select-value-blank').style.display == 'none'){
			var selectValue = document.getElementById('quantity-select-select').value;
			document.getElementById('quantity-select-value').style.display = 'none';
			document.getElementById('quantity-select-value-blank').style.display = '';
			if (selectValue <= 5000){
				document.getElementById('quantity-select-blank').value = selectValue;
			}else{
				document.getElementById('quantity-select-blank').value = 5000;
			}
		}
	}else{
		if (document.getElementById('quantity-select-value').style.display == 'none'){
			var selectValue = document.getElementById('quantity-select-blank').value;
			document.getElementById('quantity-select-value-blank').style.display = 'none';
			document.getElementById('quantity-select-value').style.display = '';
			document.getElementById('quantity-select-select').value = selectValue;
		}
	}
}

function verifyTextInput() {
  if(g_submitFinalState == 'getQuote') {
  if(document.getElementById("quantity-text").style.display != 'none'){
	var qti = document.getElementById('quantity-text-input').value;
	if(parseInt(qti) < 1 || parseInt(qti) > 100 || !parseInt(qti)) {
	    document.getElementById('quantity-text-input').value = 50;
	    document.getElementById('quantity-text-input').focus();
		alert('Please enter a whole number between 1 and 99.');
		return false;
	} else {
	return true;
	}
  }
  }
}

function correctDropdownForJacketAndWallet(select)
{
				if (select.options[0].value < 100)
				{
					//changing first option to 50
					select.options[0].innerHTML = '50';
					select.options[0].value = '50';
					// adding 75 option
					var option=new Option('75','75');
					insertOptionAt(select,option,1);
					// correcting the selected value atfer page refresh
					var selectedQty=document.getElementById('choseQty').value;
					if(selectedQty == 75)
						select.selectedIndex=1;
					else if (selectedQty == 100)
						select.selectedIndex=2;
					//change hidden value to make sure quantity won't change after next refresh	
					document.getElementById('choseQty').value='0';
					
				}	

}

function insertOptionAt(select,option,position)
{
					//add element to the end
					select.options[select.length] = new Option('dummy','10000');
					//shift elements from position
					var j = select.options[position].value;
					var m = select.options[position].text;
					for (i=position;i<(select.length-1);i++){
						var k = select.options[i+1].value;
						select.options[i+1].value = j;
						j = k;
						var n = select.options[i+1].text;
						select.options[i+1].text = m;
						m = n;
					}
					//replace element
					select.options[position] = option;
					//increase selected index if>position
					if(select.selectedIndex>position)
						select.selectedIndex = select.selectedIndex + 1;
		
}

function removeOptionFrom(select,position)
{
					//shift elements up from position
					for (i=position;i<(select.length-1);i++){
						select.options[i].value = select.options[i+1].value;
						select.options[i].text = select.options[i+1].text;
					}
					//decrese selected index if>=position
					if(select.selectedIndex>=position)
						select.selectedIndex = select.selectedIndex - 1;
					//remove last element
					select.options[select.length-1] = null;
		
}


/*function toggleWrap(blankcheckbox){
	if (document.getElementById(blankcheckbox).checked == true){
		document.getElementById('shrinkWrap').checked = false;
		document.getElementById('shrink-wrap').style.display = 'none';
		
		document.getElementById('cellophaneWrap').checked = false;
		document.getElementById('cellophane').style.display = 'none';
	}else{
		document.getElementById('shrink-wrap').style.display = '';
		document.getElementById('cellophane').style.display = '';
	}

}*/
