var ModelData = new URI();
var ModelID = ModelData.getData('ModelID');

var W = ModelData.getData('W');
var L = ModelData.getData('L');

function DynamicLength(mode){
	if($('BuildingLength')){
		var selL = $('BuildingLength').getSelected().getProperty('value');
		var GetSizes = new Request({
			url: '/products_getsizes.cfm',
			onSuccess: function(txt) {
				var SizeArray = txt.split("|");	
				
				for(var i=0; i<SizeArray.length; i++){
					$('BuildingLength').options[i] = new Option(SizeArray[i],SizeArray[i]);				
				}
				//add some functionality for special modes
				if(mode != undefined){
					if(mode == 'init'){
						SetDefaultLength(L);
					}
					else{
						SetDefaultLength(selL);
					}
					//call the building quote function if we have sizes available
					if(SizeArray.length>1){
						GetBuildingQuote();
					}
					//out of area error if there are no sizes
					else{
						$('QuoteSteps').set('opacity',0.2);
						$('SaveQuote').dispose();
						//$('QuoteSteps').empty();
						var Message = 'You have entered a Delivery Zip Code outside the TUFF SHED service area or this specific Model is unavailable in your area. If you have entered your Delivery Zip Code incorrectly, please try again.';			
						/*var Messenger = new StickyWin({
						  draggable: true,
						  content: StickyWin.ui('TUFF SHED Service Areas', Message, {
							width: '300px',
							height: '300px',
							cornerHandle: false				
						  })
						});*/
						alert(Message);
					}
				}
				//set the default length to the previous selected value
				else{
					SetDefaultLength(selL);
				};
			},
			onFailure: function() {
				alert('No Sizes Available');
			}
		});
		
		function GetLengths(Wdth){
			var SendQuery = 'ModelID=' + ModelID + '&W=' + Wdth;
			GetSizes.send(SendQuery);
		};
		var DefaultWidth = $('BuildingWidth').getSelected().getProperty('value');
		GetLengths(DefaultWidth);		
	};
};
		
function SetDefaultLength(Len){
	var BuildingLengths = $('BuildingLength').getChildren('option');
	for(var i=0; i<BuildingLengths.length; i++){
		if(BuildingLengths[i].getProperty('value') == Len){
			BuildingLengths[i].setProperty('selected',false);
			BuildingLengths[i].setProperty('selected',true);
		}
	}
};
window.addEvent('domready', function() {	
	if($('CustomSize')){
		//get the initial lengths for the custom form
		DynamicLength();
		//add an event for when the user changes the width
		$('BuildingWidth').addEvent('change',function(e){
			DynamicLength();	
		});
	}
});