function navEvents(){
	$$('.parent').each(function(el){
		var theul = el.getElement('ul');
		var showEff = new Fx.Morph(theul, {duration: 'short', transition: Fx.Transitions.Sine.easeOut, link:'cancel'});
		el.addEvent('mouseenter', function(el){	
			this.addClass('navHover');		
			showEff.start({
			    'height': [0, 174],
				'opacity':[0, 1]
			});
			//theul.removeClass('hide');
			//theul.addClass('show');
		});
		el.addEvent('mouseleave', function(el){
			//alert(this.className)
			this.removeClass('navHover');
			showEff.start({
			    'height': [174, 0],
				'opacity':[1, 0]  
			});
		});
	});
}

function displayCount(el){
	el = $(el);
	
	if(el){
		el.addEvent('change',function(){
			selectedVal = el.getSelected();
			window.location = selectedVal[0].value;

		});
	}
}

function loadConfigPanel(section,mode){
	holder = $('configHolder');
	holder.empty();
	addon = "";
	tab= false;
	if($chk(mode)){
		addon = "?m=1";
	}
	
	if($('sampledoor')){
		$('sampledoor').removeEvents();
		$('sampledoor').addEvent('click',function(e){

			curval = e.target.get('checked');
			//alert(curval);
			CurrentConfig.styleID = TempStyle.styleID; 
			CurrentConfig.styleName = TempStyle.styleName; 
			//CurrentConfig.colourName = TempColour.colourName;
			//CurrentConfig.colourID = TempColour.colourID;
			CurrentConfig.sizeName = '400mm x 300mm';
			CurrentConfig.total = '8.00';
			CurrentConfig.type = 3;

			updateConfigStatusSample(1);
			addon = "?m=1";
		});
	}
	if($('sampleswatch')){
		$('sampleswatch').removeEvents();
		$('sampleswatch').addEvent('click',function(e){
			curval = e.target.get('checked');
			//alert(curval);
			
			CurrentConfig.styleID = null; 
			CurrentConfig.styleName = 'N/A for swatch'; 
			//CurrentConfig.colourName = TempColour.colourName;
			//CurrentConfig.colourID = TempColour.colourID;
			CurrentConfig.sizeName = 'N/A for swatch';
			CurrentConfig.total = '0.00';
			CurrentConfig.type = 4;
			
			updateConfigStatusSample(1);
			
		});
	}
	
	switch(section){
		case 1://color
			url ="colourselectorclassic.php" + addon;
			tab = $('tabColour');
			break;
		
		case 2://frame
			url ="frameselector.php" + addon;
			tab = $('tabFrame');
			break;
			
		case 3://drill
			url ="drillselector.php" + addon;
			tab = $('tabSize');
			break;
	}
	$$('.tabs div').each(function(el){
		el.removeClass('active');
	});
	if (tab) {
		tab.addClass('active');
		var myHTMLRequest = new Request.HTML({
			url: url,
			update: holder
		}).get();
	}

}

//Door config Page
function selectColourStep1(){
	$('qty').addEvent('change',function(e){
		CurrentConfig.qty = $('qty').get('value');
		updateConfigStatus(1);
	});
	$$('#slideshow a').each(function(item, index, array){
		item.addEvent('click',function(e){
			e.stop();
			switch(e.target.tagName.toLowerCase()){
				case 'img':
					newEl = $(e.target).getParent('a');
					rev = newEl.get('rev');
					break;
					
				case 'a':
					newEl = $(e.target);
					rev = $(e.target).get('rev');
					break;
				
				case 'div':
					newEl = $(e.target).getParent('a');
					rev = newEl.get('rev');
					break;
			}
			//console.log(rev)
			var jsonRequest = new Request.JSON({
				url: "inc/colour.php", 
				onRequest: function(){
					//$('load').addClass('loading');
					$$('.slideitem a').each(function(el){el.removeClass('selected');});
				},
				onSuccess: function(Colour){
					//$$('.slideitem a').each(function(el){el.removeClass('selected');});
					newEl.addClass('selected');
					TempColour.colourID = Colour[0].ID;
					selectColourStep2(Colour[0].ID,Colour[0].Title);
				}
			}).get({'ID': rev});

		});
	});
	if (CurrentConfig.colourID) {
		var jsonRequest = new Request.JSON({
			url: "inc/colour.php",
			onRequest: function(){
				//$('load').addClass('loading');	
			},
			onSuccess: function(Colour){
				TempColour.colourID = Colour[0].ID;
				selectColourStep2(Colour[0].ID, Colour[0].Title);
			}
		}).get({
			'ID': CurrentConfig.colourID
		});
	}
	getPrice();

}
function selectColourStep2(ID,Title){

	//$('ColourSelect').removeEvents();
	//$('ColourSelect').addEvent('click',function(e){
			//e.stop();
			
			CurrentConfig.colourName = Title;
			CurrentConfig.colourID = ID;
			getPrice();
			updateConfigStatus(1);
	//})
}

function selectFrameStep1(){
	//////////FRAME
	$('FrameSelect').addEvent('change',function(e){
		e.stop();
		var id = e.target.getSelected()[0].value;
		//console.log(rev)
		//alert(id);
		
		if (id != "None" && id != "") {
			var jsonRequest = new Request.JSON({
				url: "inc/frame.php",
				onRequest: function(){
					$('loadf').addClass('loading');	
				},
				onSuccess: function(product){
					//$('frameImage').set('html', '<img src="' + product[0].Image + '" />');
					getImg(product[0].Image,setframeImageMargins);
					$('loadf').removeClass('loading');	
					TempFrame.frameID = product[0].ID;
					TempFrame.frameName = product[0].Title;
					selectFrameStep2();
				}
			}).get({
				'FID': id
			});
		}else{
			$('frameImage').set('html', '');
			TempFrame.frameID = null;
			TempFrame.frameName = 'None';
			selectFrameStep2();
		}

	});
	
	
	//////////GLASS
	$('GlassSelect').addEvent('change',function(e){
		e.stop();
		var id = e.target.getSelected()[0].value;
		//console.log(rev)
		if (id != "None" && id != "") {
			var jsonRequest = new Request.JSON({
				url: "inc/frame.php", 
				onRequest: function(){
					$('loadg').addClass('loading');	
				},
				onSuccess: function(product){
					//$('glassImage').set('html','<img src="' + product[0].Image + '" />');
					$('loadg').removeClass('loading');
					getImg(product[0].Image,setglassImageMargins);
					TempFrame.glassID = product[0].ID;
					TempFrame.glassName = product[0].Title;
					selectFrameStep2();
				}
			}).get({'GID': id});
		}else{
			$('glassImage').set('html', '');
			TempFrame.glassID = null;
			TempFrame.glassName = 'None';
			selectFrameStep2();
		}
	});
	
	
	////AUTO POPULATE FRAME
	if (CurrentConfig.frameID) {
		var jsonRequest = new Request.JSON({
			url: "inc/frame.php",
			onRequest: function(){
				$('loadf').addClass('loading');	
			},
			onSuccess: function(product){
				//$('frameImage').set('html', '<img src="' + product[0].Image + '" />');
				getImg(product[0].Image,setframeImageMargins);
				$('loadf').removeClass('loading');	
				$$('#FrameSelect option').each(function(el){
					if (el.value == product[0].ID) {
						el.setProperty('selected', 'selected');
					}
				});
			}
		}).get({
			'FID': CurrentConfig.frameID
		});
	}
	
	////AUTO POPULATE GLASS
	if (CurrentConfig.glassID) {
		var jsonRequest = new Request.JSON({
			url: "inc/frame.php",
			onRequest: function(){
				$('loadg').addClass('loading');	
			},
			onSuccess: function(product){
				//$('glassImage').set('html', '<img src="' + product[0].Image + '" />');
				getImg(product[0].Image,setglassImageMargins);
				$('loadg').removeClass('loading');
				$$('#GlassSelect option').each(function(el){
					if (el.value == product[0].ID) {
						el.setProperty('selected', 'selected');
					}
				});
			}
		}).get({
			'GID': CurrentConfig.glassID
		});
	}
	
}
function selectFrameStep2(){
	//$('FrameSelectBut').removeEvents();
	//$('FrameSelectBut').addEvent('click',function(e){
			//e.stop();
		
			CurrentConfig.frameID = TempFrame.frameID;
			CurrentConfig.frameName = TempFrame.frameName;
			CurrentConfig.glassID = TempFrame.glassID;
			CurrentConfig.glassName = TempFrame.glassName;
			getPrice();
			updateConfigStatus(1);
		//})
}

function selectSizeStep1(){
//		var TempSize = new Hash({
//			sizeMethod: null,
//			sizeID: null, 
//			sizeName: 'None',
//			sizeCustomW: null, 
//			sizeCustomH: null, 
//			drillOptions: null, 
//			drillMount: null,
//			drill1: null, 
//			drill2: null, 
//			drill3: null, 
//			drill4: null
//		});
		
		
	//////////SIZE
	$('SizeSelect').addEvent('change',function(e){
		//e.stop();
		var value = e.target.getSelected();
		
		TempSize.sizeName = value[0].get('text');
		
		value = value[0].value.split('-');
		TempSize.sizeID = value[0];
		TempSize.drillOptions = value[1];
		TempSize.Price = value[2];
		
		if(TempSize.drillOptions > 2){
			if(TempSize.drillMount != 3){
				$('addHoles').removeClass('hide');
			}
		}else{
			hideAddHoles();
		}
		selectSizeStep2();
	});
	
	//////////DRILL
	$('DrillMountu').addEvent('change',function(e){
		//e.stop();
		var id = $('DrillMountu').getSelected()[0].value;
		TempSize.drillMount = id;
		switch(id){
			case '1':
			case '2':
				updateDrillText(1);
				if (TempSize.sizeMethod == 1) {
					if (TempSize.drillOptions > 2) {
						$('addHoles').removeClass('hide');

					}
					else {
						hideAddHoles();
					}
				}else{
					if (TempSize.sizeCustomH > 900) {
						$('addHoles').removeClass('hide');
					}
					else {
						hideAddHoles();
					}
				}
				break;
				
			case '3':
				updateDrillText(2);
				hideAddHoles();
				validateDrill = false;
				break;
				
			case '0':
				TempSize.drill1 = null;
				TempSize.drill2 = null;
				TempSize.drill3 = null;
				TempSize.drill4 = null;
				TempSize.drillName = getDrillName();
				hideAddHoles();
				selectSizeStep2();
				break;
		}
		
		selectSizeStep2();
	});	
	$('Cw').addEvent('change',function(e){
		selectSizeStep2();
	});
	
	$('Ch').addEvent('change',function(e){
		selectSizeStep2();
	});
	
	$('Drill1u').addEvent('change', function(e){
		//top 1
		fcontinue = true;
		if (fcontinue) {
			if (e.target.value.match(/\D/)) {
				//has letters
				e.target.setProperty('value','');
				informUser.alert('Alert', 'Please use numbers only.');
				fcontinue = false;
			}
		}
		
		if (fcontinue) {
			TempSize.drillName = getDrillName();
			TempSize.drill1 = e.target.value;
		}
		
		if(e.target.value > 0 && $('DrillMountu').getSelected()[0].value == 0){
			$$('#DrillMountu option').each(function(el){
				if(el.value == 1){
					el.setProperty('selected', 'selected');
				}else{
					el.setProperty('selected', '');
				}
				
			});
			$('DrillMountu').fireEvent('change');
		}
		selectSizeStep2();
	});

	
	$('Drill2u').addEvent('change',function(e){
		//bottom 1
		fcontinue = true;
		if (fcontinue) {
			if (e.target.value.match(/\D/)) {
				//has letters
				e.target.setProperty('value','');
				informUser.alert('Alert', 'Please use numbers only.');
				fcontinue = false;
			}
		}
		
		if (fcontinue) {
			TempSize.drillName = getDrillName();
			TempSize.drill2 = e.target.value;
		}
		if(e.target.value > 0 && $('DrillMountu').getSelected()[0].value == 0){
			$$('#DrillMountu option').each(function(el){
				if(el.value == 1){
					el.setProperty('selected', 'selected');
				}else{
					el.setProperty('selected', '');
				}
			});
			$('DrillMountu').fireEvent('change');
		}
		selectSizeStep2();
	});
	$('Drill3u').addEvent('change',function(e){
		//top 2
		fcontinue = true;
		if (fcontinue) {
			if (e.target.value.match(/\D/)) {
				//has letters
				e.target.setProperty('value','');
				informUser.alert('Alert', 'Please use numbers only.');
				fcontinue = false;
			}
		}
		
		if (fcontinue) {
			TempSize.drillName = getDrillName();
			TempSize.drill3 = e.target.value;
		}
		if(e.target.value > 0 && $('DrillMountu').getSelected()[0].value == 0){
			$$('#DrillMountu option').each(function(el){
				if(el.value == 1){
					el.setProperty('selected', 'selected');
				}else{
					el.setProperty('selected', '');
				}
			});
		}
		selectSizeStep2();
	});
	
	$('Drill4u').addEvent('change',function(e){
		//bottom 2
		fcontinue = true;
		if (fcontinue) {
			if (e.target.value.match(/\D/)) {
				//has letters
				e.target.setProperty('value','');
				informUser.alert('Alert', 'Please use numbers only.');
				fcontinue = false;
			}
		}
		
		if (fcontinue) {
			TempSize.drillName = getDrillName();
			TempSize.drill4 = e.target.value;
		}
		if(e.target.value > 0 && $('DrillMountu').getSelected()[0].value == 0){
			$$('#DrillMountu option').each(function(el){
				if(el.value == 1){
					el.setProperty('selected', 'selected');
				}else{
					el.setProperty('selected', '');
				}
			});
			$('DrillMountu').fireEvent('change');
		}
		selectSizeStep2();
	});
	
	$('CDon').addEvent('click',function(e){
		e.stop();
		$('doorstandard').addClass('hide');
		$('doorcustom').removeClass('hide');
		TempSize.sizeMethod = 2;
		selectSizeStep2();
	});
	$('CDoff').addEvent('click',function(e){
		e.stop();
		$('doorcustom').addClass('hide');
		$('doorstandard').removeClass('hide');
		TempSize.sizeMethod = 1;
		selectSizeStep2();
	});
	//selectSizeStep2();
	
	////AUTO POPULATE 
	populateSizes();
}
function selectSizeStep2(){
//		var TempSize = new Hash({
//			sizeMethod: null,
//			sizeID: null, 
//			sizeName: 'None',
//			sizeCustomW: null, 
//			sizeCustomH: null, 
//			drillOptions: null, 
//			drillMount: null,
//			drill1: null, 
//			drill2: null, 
//			drill3: null, 
//			drill4: null
//		});

	//$('SizeSelectBut').removeEvents();
	//$('SizeSelectBut').addEvent('click',function(e){
			//e.stop();
			
			fcontinue = true;

			fcontinue = validateDrillHoles();
			
			if (TempSize.sizeID == null && (TempSize.sizeCustomW == null || TempSize.sizeCustomH == null) ) {
				informUser.alert('Error', 'Please select a door size.');
				fcontinue = false;
			}
			if(fcontinue){
				if (TempSize.sizeMethod == 1) {
					CurrentConfig.sizeMethod = TempSize.sizeMethod;
					CurrentConfig.sizeID = TempSize.sizeID;
					CurrentConfig.sizeName = TempSize.sizeName;
					CurrentConfig.drillOptions = TempSize.drillOptions;
					CurrentConfig.drillMount = TempSize.drillMount;
					CurrentConfig.drillName = TempSize.drillName;
					CurrentConfig.drill1 = TempSize.drill1;
					CurrentConfig.drill2 = TempSize.drill2;
					CurrentConfig.drill3 = TempSize.drill3;
					CurrentConfig.drill4 = TempSize.drill4;
				}
				else {
					CurrentConfig.sizeMethod = TempSize.sizeMethod;
					CurrentConfig.sizeCustomW = TempSize.sizeCustomW;
					CurrentConfig.sizeCustomH = TempSize.sizeCustomH;
					CurrentConfig.sizeName = TempSize.sizeName;
					CurrentConfig.drillOptions = TempSize.drillOptions;
					CurrentConfig.drillMount = TempSize.drillMount;
					CurrentConfig.drillName = TempSize.drillName;
					CurrentConfig.drill1 = TempSize.drill1;
					CurrentConfig.drill2 = TempSize.drill2;
					CurrentConfig.drill3 = TempSize.drill3;
					CurrentConfig.drill4 = TempSize.drill4;
				}
				updateConfigStatus(1);
				//loadConfigPanel(2);
			}
			getPrice();

		//})
}

//validation in function below
var Stopped = 0;
function updateConfigStatus(show){
	if($chk(show)){
		var disPlayUpdate = 1;
	}else{
		var disPlayUpdate = 0;
	}
	
	if(CurrentConfig.sizeMethod == 2){
		vCustom = validateCustom();
	}
	var DesTitle = $('DesTitle');
	var DesColour = $('DesColour');
	var DesFrame = $('DesFrame');
	var DesSize = $('DesSize');
	var DesDrill = $('DesDrill');
	
	
	if(!$chk(CurrentConfig.frameName)){
		CurrentConfig.frameName = "none";
	}
	
	if(!$chk(CurrentConfig.glassName)){
		CurrentConfig.glassName = "none";
	}
	
	if(DesTitle){DesTitle.set('html',CurrentConfig.styleName);}
	if(DesColour){
		DesColour.set('html',CurrentConfig.colourName);
//		if(CurrentConfig.colourName != "Please Select"){
//			ColourTabText = $$('#tabColour span')[0];
//			ColourTabText.set('html',CurrentConfig.colourName);
//			ColourTabText.set('style','color:#51280C;');
//		}
	};

	
	if(DesFrame){DesFrame.set('html',CurrentConfig.frameName + ", " + CurrentConfig.glassName);}
	if(DesSize){DesSize.set('html',CurrentConfig.sizeName);}
	if(DesDrill){DesDrill.set('html',CurrentConfig.drillName);}
	
	$('butaddcart').removeEvents();
	
	//// DRILL
//	if(CurrentConfig.drillID != null){
//		var previousSibling = DesDrill.getPrevious('div');
//		previousSibling.addClass('true');
//
//	}else{
//		var previousSibling = DesDrill.getPrevious('div');
//		previousSibling.removeClass('true');
//	}
	
	//// FRAME
	if(DesFrame){
		if(CurrentConfig.frameID != null){

			if($('tabFrame')){$('tabFrame').addClass('complete')}

			var previousSibling = DesFrame.getPrevious('div');
			previousSibling.addClass('true');
		}else{
			if($('tabFrame')){$('tabFrame').addClass('complete')}
			if(CurrentConfig.glassID != null){
				if($('tabFrame')){$('tabFrame').removeClass('complete')}
				var previousSibling = DesFrame.getPrevious('div');
				previousSibling.removeClass('true');
				disPlayUpdate = 0;
				$('Addform').set('html', '');
				$('butaddcart').addClass('inactive');
				informUser.alert('Error', 'Please select a frame for your glazing option');
				Stopped = 1;
			}
		}
	}
	
	
	//// COLOUR
	if(DesColour){
		if(CurrentConfig.colourID != null){
			
			$('tabColour').addClass('complete');
			var previousSibling = DesColour.getPrevious('div');
			previousSibling.addClass('true');
		}	else{
			$('StatMessage').set('html','Please select a door color before adding to cart');
			$('tabColour').removeClass('complete');
			var previousSibling = DesColour.getPrevious('div');
			previousSibling.removeClass('true');
		}
	}
	
	//// STYLE
	if(CurrentConfig.styleID != null){
		
		var previousSibling = DesTitle.getPrevious('div');
		previousSibling.addClass('true');
	}else{
		$('StatMessage').set('html','Please select a door style before adding to cart');
		var previousSibling = DesTitle.getPrevious('div');
		previousSibling.removeClass('true');
	}
	
	//// SIZE
	if(DesSize){
		if(CurrentConfig.sizeID != 130 && CurrentConfig.sizeID != null && CurrentConfig.sizeID != "" || CurrentConfig.sizeCustomW != null){
			if ($('tabSize')) {
				$('tabSize').addClass('complete');
			}
			var previousSibling = DesSize.getPrevious('div');
			previousSibling.addClass('true');
		}else{
			$('StatMessage').set('html','Please select a door size before adding to cart');
			if ($('tabSize')) {
				$('tabSize').removeClass('complete');
			}
			var previousSibling = DesSize.getPrevious('div');
			previousSibling.removeClass('true');
		}
	}
	
	
	if(CurrentConfig.styleID == null || CurrentConfig.colourID == null || (CurrentConfig.sizeMethod == 2 && !vCustom) ||(CurrentConfig.sizeMethod == 1  && CurrentConfig.sizeID == 130 || CurrentConfig.sizeID == null) ){
		//$('StatMessage').set('html','Complete your selection before adding to cart');
		$('butaddcart').addClass('inactive');
		Stopped = 1;
	}else{
		if (disPlayUpdate == 1) {
			disPlayUpdate = 0;
			//get = CurrentConfig.toQueryString();
			$('StatMessage').set('html', '');
			var htmlcode = "";
			CurrentConfig.qty = $('qty').get('value');
			if(!CurrentConfig.has('url') || !$chk(CurrentConfig.url)){
				CurrentConfig.include('url', window.location.href);
			};

			CurrentConfig.each(function(value, key){
				htmlcode = htmlcode + "<input type='hidden' name='" + key + "' value='" + value + "' />";
			});
			
			 NewConfig = new Hash({
					styleID: CurrentConfig.styleID, 
					styleName: CurrentConfig.styleName, 
					styleSurcharge:CurrentConfig.styleSurcharge,
					colourID:CurrentConfig.colourID, 
					colourName: CurrentConfig.colourName,
					colourSurcharge: CurrentConfig.colourSurcharge,
					sizeMethod: 1,
					sizeID: 130, 
					sizeName: '0mm x0mm',
					sizePrice: null,
					qty:1,
					url:CurrentConfig.url,
                                        producttype:CurrentConfig.producttype
				});
			 
			htmlcode = htmlcode + "<input type='hidden' name='newConfig' value='" + JSON.encode(NewConfig) + "' />";
                        htmlcode = htmlcode + "<input type='hidden' name='currentConfig' value='" + JSON.encode(CurrentConfig) + "' />";

			$('Addform').set('html', htmlcode);
			$('butaddcart').addEvent('click',function(){
				$('Addform').submit();
			});
			$('butaddcart').removeClass('inactive');
			if(Stopped == 1){
				//informUser.alert('Complete', 'Your Selection is now complete. Click on \'Add to Cart\' to continue.');
				Stopped = 0;
			}
		}
	}
//	if(disPlayUpdate == 1){
//		informUser.alert('Update', 'Your Selection have been updated.');
//	}
}

function SelectionActionsSample(){
	$('tabColour').addEvent('click',function(){
		loadConfigPanel(1,1);
	});
	
	var DesTitle = $('DesTitle');
	var DesColour = $('DesColour');
	

	DesColour.addEvent('click',function(){loadConfigPanel(1,1);});
}
function SelectionActions(){
//	$('tabColour').addEvent('click',function(){
//		loadConfigPanel(1);
//	});
//	$('tabFrame').addEvent('click',function(){
//		loadConfigPanel(2);
//	});
//	$('tabSize').addEvent('click',function(){
//		loadConfigPanel(3);
//	});
	
	var DesTitle = $('DesTitle');
	var DesColour = $('DesColour');
	var DesFrame = $('DesFrame');
	var DesSize = $('DesSize');
	var DesDrill = $('DesDrill');
	

	//DesColour.addEvent('click',function(){loadConfigPanel(1);});
	//DesFrame.addEvent('click',function(){loadConfigPanel(2);});
	//DesSize.addEvent('click',function(){loadConfigPanel(3);});
	//DesDrill.addEvent('click',function(){loadConfigPanel(3);});
}


function getDrillName(){
	var temp = "";
	if (TempSize.drillMount == null && TempSize.drillMount == 0) {
		temp = "None";
	}else{
		switch (TempSize.drillMount) {
			case '1':
			case '2':
				//alert(TempSize.drillMount);
				if ($('Drill1u').value != "") {
					temp = temp + $('Drill1u').value + "mm from top,<br />";
				}
				if ($('Drill2u').value != "") {
					temp = temp + $('Drill2u').value + "mm from bottom,<br />";
				}
				break;
				
			case '3':
				temp = temp + $('Drill1u').value + "mm from left,<br />";
				temp = temp + $('Drill2u').value + "mm from right,<br />";
				break;
				
			case '0':
				temp = "None";
				break;
				
		}
		if ($('Drill3u').value != "") {
			temp = temp + $('Drill3u').value + "mm from top,<br />";
		}
		if ($('Drill4u').value != "") {
			temp = temp + $('Drill4u').value + "mm from bottom,<br />";
		}
	}
	
	return temp;
}
function hideAddHoles(){
	$('addHoles').addClass('hide');
	$('Drill3u').value = "";
	$('Drill4u').value = "";
	TempSize.drill3 = null;
	TempSize.drill4 = null;
	TempSize.drillName = getDrillName();
}

function updateDrillText(mode){
	switch(mode){
		case 1:
			text = new Array('top','bottom');
			break;
		case 2:
			text = new Array('left','right');
			break;
	}
	
	$('drill1Text').set('html',' mm from ' + text[0] + ' ');
	$('drill2Text').set('html',' mm from ' + text[1] + ' ');
}

function populateSizes(){
//		var CurrentConfig = new Hash({
//			sizeMethod: null,
//			sizeID: null, 
//			sizeName: 'None',
//			sizeCustomW: null, 
//			sizeCustomH: null, 
//			drillOptions: null, 
//			drillMount: null,
//			drill1: null, 
//			drill2: null, 
//			drill3: null, 
//			drill4: null
//		});

	if(CurrentConfig.sizeMethod == 1){
		//standard sizes
		$$('#SizeSelect option').each(function(el){
			if(el.get('text') == CurrentConfig.sizeName){
				el.setProperty('selected', 'selected');
			}
		});
	}else{
		$('doorstandard').addClass('hide');
		$('doorcustom').removeClass('hide');
		$('Cw').value = CurrentConfig.sizeCustomW;
		$('Ch').value = CurrentConfig.sizeCustomH;
	}
	
	if(CurrentConfig.drillMount != null){
		//alert('in here ' + CurrentConfig.drillMount + "<<" );
		$$('#DrillMountu option').each(function(el){
			if(el.value == CurrentConfig.drillMount){
				el.setProperty('selected', 'selected');
				$('DrillMountu').fireEvent('change');
			}
		});
		$('Drill1u').value = CurrentConfig.drill1;
		$('Drill2u').value = CurrentConfig.drill2;
		$('Drill3u').value = CurrentConfig.drill3;
		$('Drill4u').value = CurrentConfig.drill4;
	}
}

function cartActions(){
	$$('.section input').each(function(el){
		el.addEvent('change', function(){
			if($('butcontine') != null){
				$('butcontine').addClass('inactive');
				$('butcontine').set('onclick',"return false;");
				informUser.alert('Notice', 'Please click the refresh button to update prices before continuing.');
			}else{
				$('butrequest').removeEvents();
				$('butrequest').set('href',"javascript:void(0);");
				informUser.alert('Notice', 'Please click the refresh button to update prices before continuing.');
					
			}
		});
	});
}
function getPrice(){
	
	var newhash = new Hash({});

//	if(typeof(TempSize) != "undefined"){newhash.combine(TempSize);}
//	if(typeof(TempFrame) != "undefined"){newhash.combine(TempFrame);}
//	if(typeof(TempColour) != "undefined"){newhash.combine(TempColour);}
//	if(typeof(TempStyle) != "undefined"){newhash.combine(TempStyle);}
//	if(typeof(TempProduct) != "undefined"){newhash.combine(TempProduct);}
	if(CurrentConfig.sizeID != "" ){
            //|| CurrentConfig.carSizeID != ""
		var jsonRequest = new Request({
			url: "inc/cost.php", 
			onSuccess: function(Cost){
//                                if(Cost == " error"){
//                                    $('butaddcart').removeEvents();
//                                    $('butaddcart').addClass('inactive');
//                                    // this is probably due to og carcass size between 1200 and the user selection
//                                    informUser.alert('Error', 'There was an error');
//                                }
				CurrentConfig.total = Cost;
				$('money').set('html', "&pound;"+Cost);
			}
		}).post({'json': JSON.encode(CurrentConfig)});
	}
}

function getCarPrice(){

	var newhash = new Hash({});

//	if(typeof(TempSize) != "undefined"){newhash.combine(TempSize);}
//	if(typeof(TempFrame) != "undefined"){newhash.combine(TempFrame);}
//	if(typeof(TempColour) != "undefined"){newhash.combine(TempColour);}
//	if(typeof(TempStyle) != "undefined"){newhash.combine(TempStyle);}
//	if(typeof(TempProduct) != "undefined"){newhash.combine(TempProduct);}
	if(CurrentConfig.carSizeID != "" ){
		var jsonRequest = new Request({
			url: "inc/cost.php",
			onSuccess: function(Cost){
//                            if(Cost == " error"){
//                                $('butaddcart').removeEvents();
//                                $('butaddcart').addClass('inactive');
//                                // this is probably due to og carcass size between 1200 and the user selection
//				informUser.alert('Error', 'There was an error');
//                            }
				CurrentConfig.total = Cost;
				$('money').set('html', "&pound;"+Cost);
			}
		}).post({'json': JSON.encode(CurrentConfig)});
	}
}

function addColourID(el){
	url = el.getNext('div').getChildren('a').get('href');
	pos = url[0].indexOf("&");
	url =  url[0].substr(0,pos);
	id = el.getSelected()[0].value;
	newurl = url + "&CID="+id;
	el.getNext('div').getChildren('a').set('href',newurl);
}

// samples page
function getPriceSample(){
	$('money').set('html',"&pound; " + CurrentConfig.total);
}
function selectColourStep1sample(){
$$('#slideshow a').each(function(item, index, array){
	item.addEvent('click',function(e){
		e.stop();
		if(e.target.get('tag') == 'img'){
			newEl = e.target.getParent('a');
			rev = newEl.get('rev');
		}else{
			rev = e.target.get('rev');
		}
		//console.log(rev)
		var jsonRequest = new Request.JSON({
			url: "inc/colour.php", 
			onRequest: function(){
				$('load').addClass('loading');	
			},
			onSuccess: function(Colour){
				TempColour.colourID = Colour[0].ID;
				$('ColourPreview').set('html','<img src="' + Colour[0].Image + '" />');
				$('ColourNameText').set('html',Colour[0].Title);
				//$('ColourSelect').set('href',Colour[0].ID);
				$('load').removeClass('loading');	
				selectColourStep2sample(Colour[0].ID,Colour[0].Title);
			}
		}).get({'ID': rev});

	});
});
if (CurrentConfig.colourID) {
	var jsonRequest = new Request.JSON({
		url: "inc/colour.php",
		onRequest: function(){
			$('load').addClass('loading');	
		},
		onSuccess: function(Colour){
			TempColour.colourID = Colour[0].ID;
			$('ColourPreview').set('html', '<img src="' + Colour[0].Image + '" />');
			$('ColourNameText').set('html', Colour[0].Title);
			//$('ColourSelect').set('href', Colour[0].ID);
			$('load').removeClass('loading');
			selectColourStep2sample(Colour[0].ID, Colour[0].Title);
		}
	}).get({
		'ID': CurrentConfig.colourID
	});
}
getPriceSample();
}
function selectColourStep2sample(ID,Title){

//$('ColourSelect').removeEvents();
//$('ColourSelect').addEvent('click',function(e){
//		e.stop();
		TempColour.colourName = Title;
		TempColour.colourID = ID;
		CurrentConfig.colourName = Title;
		CurrentConfig.colourID = ID;
		updateConfigStatusSample(1);
//});
}

//validation in function below
function updateConfigStatusSample(show){
	if($chk(show)){
		var disPlayUpdate = 1;
	}else{
		var disPlayUpdate = 0;
	}
	var DesTitle = $('DesTitle');
	var DesColour = $('DesColour');
	var DesSize = $('DesSize');
		
	DesTitle.set('html',CurrentConfig.styleName);
	DesColour.set('html',CurrentConfig.colourName);
	DesSize.set('html',CurrentConfig.sizeName);
	
	$('butaddcart').removeEvents();
	
	complete = true;

		if(CurrentConfig.type == 3){	
			//// STYLE
			if(CurrentConfig.styleID != null){
				
				var previousSibling = DesTitle.getPrevious('div');
				previousSibling.addClass('true');
			}else{
				complete = false;
				$('StatMessage').set('html','Please select a door style before adding to cart');
				var previousSibling = DesTitle.getPrevious('div');
				previousSibling.removeClass('true');
			}
			
		}
		
		//// COLOUR
		if(CurrentConfig.colourID != null){
			
			$('tabColour').addClass('complete');
			var previousSibling = DesColour.getPrevious('div');
			previousSibling.addClass('true');
		}	else{
			complete = false;
			$('StatMessage').set('html','Please select a door color before adding to cart');
			$('tabColour').removeClass('complete');
			var previousSibling = DesColour.getPrevious('div');
			previousSibling.removeClass('true');
		}
	


	
	getPriceSample();

	if(!complete){
		$('StatMessage').set('html','Complete your selection before adding to cart');
		$('butaddcart').addClass('inactive');
		Stopped = 1;
	}else{
		if (disPlayUpdate == 1) {
			disPlayUpdate = 0;
			$('StatMessage').set('html', '');
			var htmlcode = "";
			CurrentConfig.each(function(value, key){
				htmlcode = htmlcode + "<input type='hidden' name='" + key + "' value='" + value + "' />";
			});
			
			$('Addform').set('html', htmlcode);
			$('butaddcart').addEvent('click',function(){
				$('Addform').submit();
			});
			$('butaddcart').removeClass('inactive');
			if(Stopped == 1){
				informUser.alert('Complete', 'Your Selection is now complete. Click on \'Add to Cart\' to continue.');
				Stopped = 0;
			}
		}
	}
		
	if(disPlayUpdate == 1){
		informUser.alert('Update', 'Your Selection have been updated.');
	}
}



// Accessories config page
function selectStyleStep1(){
	$$('#slideshow a').each(function(item, index, array){
		item.addEvent('click',function(e){
			e.stop();
			el= $(e.target);
			switch(el.get('tag')){
				case 'img':
					newEl = el.getParent('a');
					rev = newEl.get('rev');
					break;
					
				case 'a':
					rev = el.get('rev');
					break;
				
				case 'div':
					newEl = el.getParent('a');
					rev = newEl.get('rev');
					break;
			}
			//console.log(rev)
			var jsonRequest = new Request.JSON({
				url: "inc/style.php", 
				onRequest: function(){
					$('load').addClass('loading');	
				},
				onSuccess: function(Colour){
					TempStyle.styleID = Colour[0].ID;
					
					//window.NewMarginCSS = "";
					getImg(Colour[0].LargeImage,setColourPreviewMargins);
					$('ColourName').set('html',Colour[0].Title);
					$('ColourSelect').set('href',Colour[0].ID);
					$('load').removeClass('loading');
					selectStyleStep2(Colour[0].ID,Colour[0].Title);
				}
			}).get({'ID': rev});
		});
	});
	if (CurrentConfig.colourID) {
		var jsonRequest = new Request.JSON({
			url: "inc/style.php",
			onRequest: function(){
				$('load').addClass('loading');	
			},
			onSuccess: function(Colour){
				TempStyle.styleID = Colour[0].ID;				
				
				window.NewMarginCSS = "";
				getImg(Colour[0].LargeImage,setColourPreviewMargins);
				
				$('ColourName').set('html', Colour[0].Title);
				$('ColourSelect').set('href', Colour[0].ID);
				$('load').removeClass('loading');
				selectStyleStep2(Colour[0].ID, Colour[0].Title);
			}
		}).get({
			'ID': CurrentConfig.styleID
		});
	}
	getPrice();
}
function selectStyleStep2(ID,Title){
	$('ColourSelect').removeEvents();
	$('ColourSelect').addEvent('click',function(e){
			e.stop();
			getPrice();
			CurrentConfig.styleName = Title;
			CurrentConfig.styleID = ID;
			updateAccConfigStatus(1);
			loadAccConfigPanel(2,2);
	});
}

function selectAccColourStep1(){
	$('qty').addEvent('change',function(e){
		CurrentConfig.qty = $('qty').get('value');
		updateAccConfigStatus(1);
	});
	$$('#slideshow a').each(function(item, index, array){
		item.addEvent('click',function(e){
			e.stop();
			switch($(e.target).get('tag')){
				case 'img':
					newEl = $(e.target).getParent('a');
					rev = newEl.get('rev');
					break;
					
				case 'a':
					newEl = $(e.target);
					rev = $(e.target).get('rev');
					break;
				
				case 'div':
					newEl = $(e.target).getParent('a');
					rev = newEl.get('rev');
					break;
			}
			//console.log(rev)
			var jsonRequest = new Request.JSON({
				url: "inc/colour.php", 
				onRequest: function(){
					$('load').addClass('loading');	
					$$('.slideitem a').each(function(el){el.removeClass('selected');});
				},
				onSuccess: function(Colour){
					newEl.addClass('selected');
					TempColour.colourID = Colour[0].ID;
					//$('ColourPreview').set('html','<img src="' + Colour[0].Image + '" />');
					getImg(Colour[0].Image,setColourPreviewMargins);
					$('ColourNameText').set('html',Colour[0].Title);
					//$('ColourSelect').set('href',Colour[0].ID);
					$('load').removeClass('loading');	
					selectAccColourStep2(Colour[0].ID,Colour[0].Title);
				}
			}).get({'ID': rev});

		});
	});
	if (CurrentConfig.colourID) {
		var jsonRequest = new Request.JSON({
			url: "inc/colour.php",
			onRequest: function(){
				$('load').addClass('loading');	
			},
			onSuccess: function(Colour){
				TempColour.colourID = Colour[0].ID;
				//$('ColourPreview').set('html', '<img src="' + Colour[0].Image + '" />');
				getImg(Colour[0].Image,setColourPreviewMargins);
				$('ColourNameText').set('html', Colour[0].Title);
				//$('ColourSelect').set('href', Colour[0].ID);
				$('load').removeClass('loading');
				selectAccColourStep2(Colour[0].ID, Colour[0].Title);
			}
		}).get({
			'ID': CurrentConfig.colourID
		});
	}
	getPrice();
}
function selectAccColourStep2(ID,Title){
	//$('ColourSelect').removeEvents();
	//$('ColourSelect').addEvent('click',function(e){
	//		e.stop();
			
			CurrentConfig.colourName = Title;
			CurrentConfig.colourID = ID;
			getPrice();
			updateAccConfigStatus(1);
			//loadConfigPanel(3);
	//});
}

function AccSelectionActions(){
	if($('tabStyle')){
		$('tabStyle').addEvent('click',function(){
			loadAccConfigPanel(1,2);
		});
	}
	if($('tabColour')){
		$('tabColour').addEvent('click',function(){
			loadAccConfigPanel(2,2);
		});
	}
	
	var DesStyle = $('DesStyle');
	var DesColour = $('DesColour');
	var DesTitle = $('DesTitle');
	
	if(DesStyle){DesStyle.addEvent('click',function(){loadAccConfigPanel(1,2);});};
	if(DesColour){DesColour.addEvent('click',function(){loadAccConfigPanel(2,2);});};
}

// validation in function below
function updateAccConfigStatus(show){
	if($chk(show)){
		var disPlayUpdate = 1;
	}else{
		var disPlayUpdate = 0;
	}
	var DesTitle = $('DesTitle');
	var DesStyle = $('DesStyle');
	var DesColour = $('DesColour');
	
	if(DesTitle){DesTitle.set('html',CurrentConfig.productName);};	
	if(DesStyle){DesStyle.set('html',CurrentConfig.styleName);};
	if(DesColour){DesColour.set('html',CurrentConfig.colourName);};
	
	$('butaddcart').removeEvents();
	
	
	
	//// COLOUR
	if(DesColour){
		if(CurrentConfig.colourID != null){	
			$('tabColour').addClass('complete');
			var previousSibling = DesColour.getPrevious('div');
			previousSibling.addClass('true');
		}	else{
			$('StatMessage').set('html','Please select a door color before adding to cart');
			$('tabColour').removeClass('complete');
			var previousSibling = DesColour.getPrevious('div');
			previousSibling.removeClass('true');
		}
	}
	
	//// STYLE
	//alert(CurrentConfig.styleID);
	if(DesStyle){
		if(CurrentConfig.styleID != null){
			$('tabStyle').addClass('complete');
			var previousSibling = DesStyle.getPrevious('div');
			previousSibling.addClass('true');
		}else{
			$('StatMessage').set('html','Please select a door style before adding to cart');
			$('tabStyle').removeClass('complete');
			var previousSibling = DesStyle.getPrevious('div');
			previousSibling.removeClass('true');
		}
	}
	
	
	
	if((DesStyle && CurrentConfig.styleID == null) || (DesColour && CurrentConfig.colourID == null)){
		//$('StatMessage').set('html','Complete your selection before adding to cart');
		$('butaddcart').addClass('inactive');
		Stopped = 1;
	}else{
		if (disPlayUpdate == 1) {
			disPlayUpdate = 0;
			//get = CurrentConfig.toQueryString();
			$('StatMessage').set('html', '');
			var htmlcode = "";
			CurrentConfig.qty = $('qty').get('value');
			if(!CurrentConfig.has('url') || !$chk(CurrentConfig.url)){
				CurrentConfig.include('url', window.location.href);
			};
			
			CurrentConfig.each(function(value, key){
				htmlcode = htmlcode + "<input type='hidden' name='" + key + "' value='" + value + "' />";
			});
			htmlcode = htmlcode + "<input type='hidden' name='currentConfig' value='" + JSON.encode(CurrentConfig) + "' />";
			
			$('Addform').set('html', htmlcode);
			$('butaddcart').addEvent('click',function(){
				$('Addform').submit();
			});
			$('butaddcart').removeClass('inactive');
			if(Stopped == 1){
				informUser.alert('Complete', 'Your Selection is now complete. Click on \'Add to Cart\' to continue.');
				Stopped = 0;
			}
		}
	}
	if(disPlayUpdate == 1){
		informUser.alert('Update', 'Your Selection have been updated.');
	}
}

function loadAccConfigPanel(section,mode){
	holder = $('configHolder');
	addon = "";
	tab = "";
	if($chk(mode)){
		addon = "?m="+mode;
	}
	
	switch(section){
		case 1://style
			url ="styleselector.php" + addon;
			tab = $('tabStyle');
			break;
		
		case 2://color
			url ="colourselectorclassic.php" + addon;
			tab = $('tabColour');
			break;
			
//		case 3://size
//			url ="drillselector.php" + addon;
//			tab = $('tabSize');
//			break;
	}
	if(tab){
		holder.empty();
		$$('.tabs div').each(function(el){
			el.removeClass('active');
		});
		tab.addClass('active');
		var myHTMLRequest = new Request.HTML({url:url,update:holder}).get();
	}

}



// Get image size for src 
function getImg(imgSrc,funcName){
	var newImg = new Image();
	newImg.src = imgSrc;
	newImg.onload = funcName;
}
function setColourPreviewMargins(){
	var height = this.height;
	var width = this.width;
	var src = this.src;
	var newheight = (189-height)/2;
	var newwidth = (189-width)/2;
	var NewMarginCSS = "style=\"margin-top:"+newheight+"px;margin-left:"+newwidth+"px;\"";
	
	$('ColourPreview').set('html', '<img src="' + src + '" '+NewMarginCSS+' />');
}
function setframeImageMargins(){
	var height = this.height;
	var width = this.width;
	var src = this.src;
	var newheight = (240-height)/2;
	var newwidth = (240-width)/2;
	var NewMarginCSS = "style=\"margin-top:"+newheight+"px;margin-left:"+newwidth+"px;\"";
	
	$('frameImage').set('html', '<img src="' + src + '" '+NewMarginCSS+' />');
}
function setglassImageMargins(){
	var height = this.height;
	var width = this.width;
	var src = this.src;
	var newheight = (240-height)/2;
	var newwidth = (240-width)/2;
	var NewMarginCSS = "style=\"margin-top:"+newheight+"px;margin-left:"+newwidth+"px;\"";
	
	$('glassImage').set('html', '<img src="' + src + '" '+NewMarginCSS+' />');
}
function setcarframeImageMargins(){
	var height = this.height;
	var width = this.width;
	var src = this.src;
	var newheight = (127-height)/2;
	var newwidth = (127-width)/2;
	var NewMarginCSS = "style=\"margin-top:"+newheight+"px;margin-left:"+newwidth+"px;\"";

        $('frameImage').set('html', '<img src="' + src + '" '+NewMarginCSS+' />');
}
function setcarglassImageMargins(){
	var height = this.height;
	var width = this.width;
	var src = this.src;
	var newheight = (127-height)/2;
	var newwidth = (127-width)/2;
	var NewMarginCSS = "style=\"margin-top:"+newheight+"px;margin-left:"+newwidth+"px;\"";

        $('glassImage').set('html', '<img src="' + src + '" '+NewMarginCSS+' />');
}
function setcarstyleImageMargins(){
	var height = this.height;
	var width = this.width;
	var src = this.src;
	var newheight = (127-height)/2;
	var newwidth = (127-width)/2;
	var NewMarginCSS = "style=\"margin-top:"+newheight+"px;margin-left:"+newwidth+"px;\"";

	//$('styleImage').set('html', '<img src="' + src + '" '+NewMarginCSS+' />');
        $('styleImage').set('html', '<img src="' + src + '" '+NewMarginCSS+' />');
}
function setcardoorcolourImageMargins(){
	var height = this.height;
	var width = this.width;
	var src = this.src;
	var newheight = (127-height)/2;
	var newwidth = (127-width)/2;
	var NewMarginCSS = "style=\"margin-top:"+newheight+"px;margin-left:"+newwidth+"px;\"";

	$('colourImage').set('html', '<img src="' + src + '" '+NewMarginCSS+' />');
}
function setcarcolourImageMargins(){
	var height = this.height;
	var width = this.width;
	var src = this.src;
	var newheight = (127-height)/2;
	var newwidth = (127-width)/2;
	var NewMarginCSS = "style=\"margin-top:"+newheight+"px;margin-left:"+newwidth+"px;\"";

	$('carColourImage').set('html', '<img src="' + src + '" '+NewMarginCSS+' />');
}

function validateDrillHoles(){
	fcontinue = true;
	var id = $('DrillMountu').getSelected()[0].value;
	drill1 = $('Drill1u').get('value');
	drill2 = $('Drill2u').get('value');
	drill3 = $('Drill3u').get('value');
	drill4 = $('Drill4u').get('value');
	
	shown = 0;
	switch(id){
	case '1':
	case '2':
		updateDrillText(1);
		if (TempSize.sizeMethod == 1) {
			if (TempSize.drillOptions > 2) {
				//all four
				if(drill1 == ""){
					if(shown == 0){
						informUser.alert('Alert', 'Please enter the first Drill position.');
						shown =1;
					}
					fcontinue = false;
				}
				if(drill2 == ""){
					if(shown == 0){
					informUser.alert('Alert', 'Please enter the second Drill position.');
					shown =1;
					}
					fcontinue = false;
				}
				if(drill3 == ""){
					if(shown == 0){
					informUser.alert('Alert', 'Please enter the third Drill position.');
					shown =1;
					}
					fcontinue = false;
				}
				if(drill4 == ""){
					if(shown == 0){
					informUser.alert('Alert', 'Please enter the fourth Drill position.');
					shown =1;
					}
					fcontinue = false;
				}
			}else{
				//top two only
				if(drill1 == ""){
					if(shown == 0){
					informUser.alert('Alert', 'Please enter the first Drill position.');
					shown =1;
					}
					fcontinue = false;
				}
				if(drill2 == ""){
					if(shown == 0){
					informUser.alert('Alert', 'Please enter the second Drill position.');
					shown =1;
					}
					fcontinue = false;
				}
			}
		}else{
			if (TempSize.sizeCustomH > 900) {
				//all four
				if(drill1 == ""){
					if(shown == 0){
					informUser.alert('Alert', 'Please enter the first Drill position.');
					shown = 1;
					}
					fcontinue = false;
				}
				if(drill2 == ""){
					if(shown == 0){
					informUser.alert('Alert', 'Please enter the second Drill position.');
					shown = 1;
					}
					fcontinue = false;
				}
				if(drill3 == ""){
					if(shown == 0){
					informUser.alert('Alert', 'Please enter the third Drill position.');
					shown = 1;
					}
					fcontinue = false;
				}
				if(drill4 == ""){
					if(shown == 0){
					informUser.alert('Alert', 'Please enter the fourth Drill position.');
					shown = 1;
					}
					fcontinue = false;
				}
			}else{
				//top two only
				if(drill1 == ""){
					if(shown == 0){
					informUser.alert('Alert', 'Please enter the first Drill position.');
					shown = 1;
					}
					fcontinue = false;
				}
				if(drill2 == ""){
					if(shown == 0){
					informUser.alert('Alert', 'Please enter the second Drill position.');
					shown = 1;
					}
					fcontinue = false;
				}
			}
		}
		break;
		
	case '3':
		//top two only
		if(drill1 == ""){
			informUser.alert('Alert', 'Please enter the first Drill position.');
			fcontinue = false;
		}
		if(drill2 == ""){
			informUser.alert('Alert', 'Please enter the second Drill position.');
			fcontinue = false;
		}
		break;
		
	}
	return fcontinue;
	
}

function updateQty(){
	if(CurrentConfig.qty != null){
		$('qty').set('value',CurrentConfig.qty);
	}
}

function validateCarCustom(){
	//$('Cw').addEvent('change',function(e){
        message = "";
		cw = $('customSize');
                if(cw){
                    fwcontinue = true;
                    if (fwcontinue) {
                            if (cw.value.match(/\D/)) {
                                    //has letters
                                    cw.setProperty('value','');
                                    message =  'Your size must be larger than 150.';

                                    fwcontinue = false;
                            };
                    }
                    if (fwcontinue) {
                            if (cw.value < 150) {
                                    //less than 100
                                    cw.setProperty('value','');
                                    message =  'Your size must be larger than 150mm.';
                                    fwcontinue = false;
                            };
                    }
                    if (fwcontinue) {
                            if (cw.value > 1200) {
                                    cw.setProperty('value','');
                                    message =  'Our maximum width is 1200mm.';
                                    fwcontinue = false;
                            };
                    }

                    if (fwcontinue) {
                            CurrentConfig.sizeCustomW = cw.value;
                    }
                    return fwcontinue;
                }
	//});
}

function validateCustom(){
	//$('Cw').addEvent('change',function(e){
		cw = $('Cw');
		fwcontinue = true;
		if (fwcontinue) {
			if (cw.value.match(/\D/)) {
				//has letters
				cw.setProperty('value','');
				message =  'Your size must be larger than 100.';
				
				fwcontinue = false;
				selectSizeStep2();
			};
		}
		if (fwcontinue) {
			if (cw.value < 100) {
				//less than 100
				cw.setProperty('value','');
				message =  'Your size must be larger than 100mm.';
				fwcontinue = false;
			};
		}
		if (fwcontinue) {
			if (cw.value > 1200) {
				cw.setProperty('value','');
				message =  'Our maximum width is 1200mm.';
				fwcontinue = false;
			};
		}
		
		if (fwcontinue) {
			CurrentConfig.sizeName = $('Ch').value + "mm x " + cw.value + "mm";
			CurrentConfig.sizeCustomW = cw.value;
		}
	//});
	
	
	 ch = $('Ch');
		fhcontinue = true;
		if (fhcontinue) {
			if (ch.value.match(/\D/)) {
				//has letters
				ch.setProperty('value','');
				message =   'Your size must be larger than 100.';
				fhcontinue = false;
			};
		}
		if (fhcontinue) {
			if (ch.value < 100) {
				//less than 100
				ch.setProperty('value','');
				message =  'Your size must be larger than 100mm.';
				fhcontinue = false;
			};
		}
		if (fhcontinue) {
			if (ch.value > 2540) {
				ch.setProperty('value','');
				message = 'Our maximum height is 2540mm.';
				fhcontinue = false;
			};
		}
		
		if (fhcontinue) {
			CurrentConfig.sizeName = ch.value + "mm x " + $('Cw').value + "mm";
			CurrentConfig.sizeCustomH = ch.value;
			
			if (ch.value > 900) {
				$('addHoles').removeClass('hide');
			}else{
				hideAddHoles();
			};
		}else{
			informUser.alert('Alert', message);
		}
		
		if(fwcontinue && fhcontinue){
			return true;
		}else{
			return false;
		}
	//});
}



function selectCarFrameStep1(){
	//////////FRAME
	$('FrameSelect').addEvent('change',function(e){
		e.stop();
		var id = e.target.getSelected()[0].value;
		//console.log(rev)
		//alert(id);

		if (id != "None" && id != "") {
			var jsonRequest = new Request.JSON({
				url: "inc/frame.php",
				onRequest: function(){
					$('loadf').addClass('loading');
				},
				onSuccess: function(product){
					//$('frameImage').set('html', '<img src="' + product[0].Image + '" />');
                                        if(product.length > 0){
                                            id = product[0].ID;
                                            title = product[0].Title;
                                            getImg(product[0].Image,setcarframeImageMargins);
                                        }else{
                                            $('frameImage').set('html', '');
                                            id = null;
                                            title = "None";//
                                        }

					$('loadf').removeClass('loading');
					TempFrame.frameID = id;
					TempFrame.frameName = title;
					selectCarFrameStep2();
				}
			}).get({
				'FID': id
			});
		}else{
			$('frameImage').set('html', '');
			TempFrame.frameID = null;
			TempFrame.frameName = 'None';
			selectCarFrameStep2();
		}

	});


	//////////GLASS
	$('GlassSelect').addEvent('change',function(e){
		e.stop();
		var id = e.target.getSelected()[0].value;
		//console.log(rev)
		if (id != "None" && id != "") {
			var jsonRequest = new Request.JSON({
				url: "inc/frame.php",
				onRequest: function(){
					$('loadg').addClass('loading');
				},
				onSuccess: function(product){
					//$('glassImage').set('html','<img src="' + product[0].Image + '" />');
					$('loadg').removeClass('loading');
                                       // console.log(product);
                                        if(product.length > 0){
                                            id = product[0].ID;
                                            title = product[0].Title;
                                            getImg(product[0].Image,setcarglassImageMargins);
                                        }else{
                                            $('glassImage').set('html', '');
                                            id = null;
                                            title = "None";//
                                        }
                                        
                                            TempFrame.glassID = id;
                                            TempFrame.glassName = title;
                                            selectCarFrameStep2();

				}
			}).get({'GID': id});
		}else{
			$('glassImage').set('html', '');
			TempFrame.glassID = null;
			TempFrame.glassName = 'None';
			selectCarFrameStep2();
		}
	});


	////AUTO POPULATE FRAME
	if (CurrentConfig.frameID) {
		var jsonRequest = new Request.JSON({
			url: "inc/frame.php",
			onRequest: function(){
				$('loadf').addClass('loading');
			},
			onSuccess: function(product){
				//$('frameImage').set('html', '<img src="' + product[0].Image + '" />');
				getImg(product[0].Image,setcarframeImageMargins);
				$('loadf').removeClass('loading');
				$$('#FrameSelect option').each(function(el){
					if (el.value == product[0].ID) {
						el.setProperty('selected', 'selected');
					}
				});
			}
		}).get({
			'FID': CurrentConfig.frameID
		});
	}

	////AUTO POPULATE GLASS
	if (CurrentConfig.glassID) {
		var jsonRequest = new Request.JSON({
			url: "inc/frame.php",
			onRequest: function(){
				$('loadg').addClass('loading');
			},
			onSuccess: function(product){
				//$('glassImage').set('html', '<img src="' + product[0].Image + '" />');
				getImg(product[0].Image,setcarglassImageMargins);
				$('loadg').removeClass('loading');
				$$('#GlassSelect option').each(function(el){
					if (el.value == product[0].ID) {
						el.setProperty('selected', 'selected');
					}
				});
			}
		}).get({
			'GID': CurrentConfig.glassID
		});
	}

}
function selectCarFrameStep2(){
        CurrentConfig.frameID = TempFrame.frameID;
        CurrentConfig.frameName = TempFrame.frameName;
        CurrentConfig.glassID = TempFrame.glassID;
        CurrentConfig.glassName = TempFrame.glassName;
        //TODO: get these working
        //getPrice();
        updateCarConfigStatus(1);
}

function selectCarStyleStep1(){
	//////////Style
	$('StyleSelect').addEvent('change',function(e){
		e.stop();
		var id = e.target.getSelected()[0].value;

		if (id != "None" && id != "") {
			var jsonRequest = new Request.JSON({
				url: "inc/style.php",
				onRequest: function(){
					$('loads').addClass('loading');
				},
				onSuccess: function(product){
					getImg(product[0].Image,setcarstyleImageMargins);
					$('loads').removeClass('loading');
					TempStyle.styleID = product[0].ID;
					TempStyle.styleName = product[0].Title;
					selectCarStyleStep2();
                                        updateDoorOptions(CurrentConfig.carID,TempStyle.styleID);
				}
			}).get({
				'ID': id
			});
		}else{
			$('styleImage').set('html', '');
			TempStyle.styleID = null;
			TempStyle.styleName = 'None';
			selectCarFrameStep2();
                        updateDoorOptions(CurrentConfig.carID);
		}

	});


	//////////COLOUR
	$('ColourSelect').addEvent('change',function(e){
		e.stop();
		var id = e.target.getSelected()[0].value;
		//console.log(rev)
		if (id != "None" && id != "") {
			var jsonRequest = new Request.JSON({
				url: "inc/colour.php",
				onRequest: function(){
					$('loadc').addClass('loading');
				},
				onSuccess: function(product){
					//$('glassImage').set('html','<img src="' + product[0].Image + '" />');
					$('loadc').removeClass('loading');
					getImg(product[0].Thumbnail,setcardoorcolourImageMargins);
					TempColour.colourID = product[0].ID;
					TempColour.colourName = product[0].Title;
					selectCarStyleStep2();
				}
			}).get({'ID': id});
		}else{
			$('colourImage').set('html', '');
			TempFrame.glassID = null;
			TempFrame.glassName = 'None';
			selectCarStyleStep2();
		}
	});

            //////////car Door drill
            $('CarDoorDrillSelect').addEvent('change',function(e){
                    e.stop();
                    var id = e.target.getSelected()[0].value;

                    if (id != "None" && id != "") {
                            TempSize.doorDrillMount= id;
                            TempSize.doorDrillName = e.target.getSelected()[0].text;
                            selectCarStyleStep2();

                    }else{
                            TempSize.doorDrillMount = null;
                            TempSize.doorDrillName = 'None';
                            selectCarStyleStep2();
                    }

            });
	////AUTO POPULATE Style
	if (CurrentConfig.styleID) {
		var jsonRequest = new Request.JSON({
			url: "inc/style.php",
			onRequest: function(){
				$('loads').addClass('loading');
			},
			onSuccess: function(product){
				//$('frameImage').set('html', '<img src="' + product[0].Image + '" />');
				getImg(product[0].Image,setcarstyleImageMargins);
				$('loads').removeClass('loading');
				$$('#StyleSelect option').each(function(el){
					if (el.value == product[0].ID) {
						el.setProperty('selected', 'selected');
					}
				});
			}
		}).get({
			'ID': CurrentConfig.styleID
		});
	}

	////AUTO POPULATE COLOUR
	if (CurrentConfig.colourID) {
		var jsonRequest = new Request.JSON({
			url: "inc/colour.php",
			onRequest: function(){
				$('loadc').addClass('loading');
			},
			onSuccess: function(product){
				//$('glassImage').set('html', '<img src="' + product[0].Image + '" />');
				getImg(product[0].Thumbnail,setcardoorcolourImageMargins);
				$('loadc').removeClass('loading');
				$$('#ColourSelect option').each(function(el){
					if (el.value == product[0].ID) {
						el.setProperty('selected', 'selected');
					}
				});
			}
		}).get({
			'ID': CurrentConfig.colourID
		});
	}
}
function selectCarStyleStep2(){
        CurrentConfig.styleID = TempStyle.styleID;
        CurrentConfig.styleName = TempStyle.styleName;
        CurrentConfig.colourID = TempColour.colourID;
        CurrentConfig.colourName = TempColour.colourName;

        CurrentConfig.doorDrillMount = TempSize.doorDrillMount;
        CurrentConfig.doorDrillName = TempSize.doorDrillName;
        //TODO: get these working
        //getPrice();
        updateCarConfigStatus(1);
}

function checkdoors(){
    $('includedoors').addEvent('click',function(e){
        el = e.target;
        state = el.getProperty('checked');
        //alert(state);
        if(state){
           $('doorOptions').setStyle('display','block');
           CurrentConfig.includeDoors = true;
           updateCarConfigStatus(1);
        }else{
           $('doorOptions').setStyle('display','none');
           CurrentConfig.includeDoors = false;
           updateCarConfigStatus(1);
        }
    })
}

function selectCarColourStep1(){
	//////////car colour
        $('qty').addEvent('change',function(e){
		CurrentConfig.qty = $('qty').get('value');
		updateCarConfigStatus(1);
	});


	$('CarColourSelect').addEvent('change',function(e){
		e.stop();
		var id = e.target.getSelected()[0].value;

		if (id != "None" && id != "") {
			var jsonRequest = new Request.JSON({
				url: "inc/colour.php",
				onRequest: function(){
					$('loadcc').addClass('loading');
				},
				onSuccess: function(product){
					getImg(product[0].Thumbnail,setcarcolourImageMargins);
					$('loadcc').removeClass('loading');
					TempColour.carColourID= product[0].ID;
					TempColour.carColourName = product[0].Title;
					selectCarColourStep2();
				}
			}).get({
				'ID': id
			});
		}else{
			$('carColourImage').set('html', '');
			TempColour.carColourID = null;
			TempColour.carColourName = 'None';
			selectCarColourStep2();
		}

	});

        //////////car size
	$('carSizeSelect').addEvent('change',function(e){
            el = $('carSizeSelect');
            var id = el.getSelected()[0].value;


            if (id != "None" && id != "") {
                    TempSize.carSizeID= id;
                    TempSize.carSizeName = el.getSelected()[0].text;
                    selectCarColourStep2();

            }else{
                    TempSize.carSizeID = null;
                    TempSize.carSizeName = 'None';
                    selectCarColourStep2();
            }
            
	});

         //////////car drill
         if($('CarDrillSelect')){
            $('CarDrillSelect').addEvent('change',function(e){
                    e.stop();
                    var id = e.target.getSelected()[0].value;

                    if (id != "None" && id != "") {
                            TempSize.drillMount= id;
                            TempSize.drillName = e.target.getSelected()[0].text;
                            selectCarColourStep2();

                    }else{
                            TempSize.drillMount = null;
                            TempSize.drillName = 'None';
                            selectCarColourStep2();
                    }

            });
        }

        //////////car draw
         if($('CarDrawSelect')){
            $('CarDrawSelect').addEvent('change',function(e){
                    e.stop();
                    var id = e.target.getSelected()[0].value;

                    if (id != "None" && id != "") {
                            TempSize.carDrawType= id;
                            TempSize.carDrawTypeName = e.target.getSelected()[0].text;
                            selectCarColourStep2();

                    }else{
                            TempSize.carDrawType = 1;
                            TempSize.carDrawTypeName = 'Metabox';
                            selectCarColourStep2();
                    }

            });
        }

        //////////car custom size
         if($('customSize')){
            $('customSize').addEvent('blur',function(e){
                    el = $('customSize');
                    var val = el.value;

                    if (val != "") {
                            TempSize.carSizeCustomW = val.toInt();
                            selectCarColourStep2();

                    }else{
                            TempSize.carSizeCustomW = null;
                            selectCarColourStep2();
                    }

            });
        }
	////AUTO POPULATE custom size
	if (CurrentConfig.carSizeMethod == 2) {
             $('customSize').set('value',CurrentConfig.carSizeCustomW);
             $('sizeMethod_2').set('checked',true);
            // $('sizeMethod_1').set('checked',false);
             $('sizeMethod_2').fireEvent('click');

	}
	////AUTO POPULATE car colour
	if (CurrentConfig.carColourID) {
		var jsonRequest = new Request.JSON({
			url: "inc/colour.php",
			onRequest: function(){
				$('loadcc').addClass('loading');
			},
			onSuccess: function(product){
				//$('frameImage').set('html', '<img src="' + product[0].Image + '" />');
				getImg(product[0].Thumbnail,setcarcolourImageMargins);
				$('loadcc').removeClass('loading');
				$$('#CarColourSelect option').each(function(el){
					if (el.value == product[0].ID) {
						el.setProperty('selected', 'selected');
					}
				});
			}
		}).get({
			'ID': CurrentConfig.carColourID
		});
	}

        ////AUTO POPULATE car size
	if (CurrentConfig.carSizeID) {
                $$('#carSizeSelect option').each(function(el){
                        if (el.value == CurrentConfig.carSizeID) {
                                el.setProperty('selected', 'selected');
                        }
                });
	}
        ////AUTO POPULATE car drill
	if (CurrentConfig.drillMount) {
                $$('#CarDrillSelect option').each(function(el){
                        if (el.value == CurrentConfig.drillMount) {
                                el.setProperty('selected', 'selected');
                        }
                });
	}


}
function selectCarColourStep2(){
        CurrentConfig.carColourID = TempColour.carColourID;
        CurrentConfig.carColourName = TempColour.carColourName;
        CurrentConfig.carSizeID = TempSize.carSizeID;
        CurrentConfig.carSizeName = TempSize.carSizeName;
        CurrentConfig.drillMount = TempSize.drillMount;
        CurrentConfig.drillName = TempSize.drillName;
        CurrentConfig.carSizeCustomW = TempSize.carSizeCustomW;
        CurrentConfig.carDrawType = TempSize.carDrawType;
        CurrentConfig.carDrawTypeName = TempSize.carDrawTypeName;
        
        //TODO: get these working
        //getPrice();
        updateCarConfigStatus(1);
}

function updateDoorOptions(carID,StyleID){
    if(StyleID == undefined){
        StyleID = "";
    }
    var jsonRequest = new Request.JSON({
            url: "carcass_inc/updateDoorOptions.php",
            onSuccess: function(obj){
                //console.log(obj);
                $('ColourSelect').empty();
                $('FrameSelect').empty();
                $('GlassSelect').empty();
                //$('colourImage').empty();
                //$('frameImage').empty();
                //$('glassImage').empty();
                createSelectHTML(obj.doorcolours,$('ColourSelect'));
                createSelectHTML(obj.doorframes,$('FrameSelect'));
                createSelectHTML(obj.doorglass,$('GlassSelect'));

                if (CurrentConfig.colourID) {
                    $$('#ColourSelect option').each(function(el){
                        if (el.value == CurrentConfig.colourID) {
                                el.setProperty('selected', 'selected');
                        }
                    });
                }
                if (CurrentConfig.frameID) {
                    $$('#FrameSelect option').each(function(el){
                        if (el.value == CurrentConfig.frameID) {
                                el.setProperty('selected', 'selected');
                        }
                    });
                }
                if (CurrentConfig.glassID) {
                    $$('#GlassSelect option').each(function(el){
                        if (el.value == CurrentConfig.glassID) {
                                el.setProperty('selected', 'selected');
                        }
                    });
                }
                updateCarConfigStatus(1);
            }
    }).get({
            'ID': carID,
            'SID': StyleID
    });
}

function createSelectHTML($data,el){
    x = 0
    while(x < $data.length){
       temp = new Element('option', {
            'value': $data[x]['ID'],
            'text': $data[x]['Title']
        });
        temp.inject(el);
        x++;
    }
}


//validation in function below
var Stopped = 0;
function updateCarConfigStatus(show){
        getCarPrice();
	if($chk(show)){
		var disPlayUpdate = 1;
	}else{
		var disPlayUpdate = 0;
	}

	if(CurrentConfig.carSizeMethod == 2){
		vCustom = validateCarCustom();
                if(message != ""){
                    informUser.alert('Error', message);
                }
	}
	var CarColour = $('CarColour');
	var CarSize = $('CarSize');
	var CarDrill = $('CarDrill');
        var CarDraw = $('CarDraw');

        var CarIncludeDoor = $('CarDoorInclude');
        var CarDoorStyle = $('CarDoorStyle');
        var CarDoorColour = $('CarDoorColour');
        var CarDoorDrill = $('CarDoorDrill');
        var CarDoorFrame = $('CarDoorFrame');
        var CarDoorGlaze = $('CarDoorGlaze');


	if(!$chk(CurrentConfig.frameName)){
		CurrentConfig.frameName = "none";
	}

	if(!$chk(CurrentConfig.glassName)){
		CurrentConfig.glassName = "none";
	}

	if(CarColour){
		CarColour.set('html',CurrentConfig.carColourName);
	};
        if(CurrentConfig.carSizeMethod == 2 && CurrentConfig.carSizeCustomW != null){
            if(CarSize){CarSize.set('html',CurrentConfig.carSizeCustomW +'mm x '+CurrentConfig.carSizeCustomH + 'mm' );}
	}else{
            if(CarSize){CarSize.set('html',CurrentConfig.carSizeName);}
        }
        if(CurrentConfig.carType == 3 ){
            if(CarDrill){CarDrill.set('html','Blanker: <b>' + CurrentConfig.drillName+ '</b><br/>');}
	}else{
            if(CarDrill){CarDrill.set('html','Drill: <b>' + CurrentConfig.drillName+ '</b><br/>');}
        }
        if(CarDraw){CarDraw.set('html','Draw Type: <b>' + CurrentConfig.carDrawTypeName+ '</b>');}


        if(CurrentConfig.includeDoors){
            if($('tabDoor')){$('tabDoor').addClass('complete')};
            if(CarIncludeDoor){CarIncludeDoor.set('html','Yes');};
            if(CarDoorStyle){CarDoorStyle.set('html','Style: <b>' + CurrentConfig.styleName + '</b>');}
            if(CarDoorColour){CarDoorColour.set('html','Colour: <b>' + CurrentConfig.colourName + '</b>');}
            if(CarDoorFrame){CarDoorFrame.set('html','Frame: <b>' + CurrentConfig.frameName + ", " + CurrentConfig.glassName + '</b>');}
            if(CarDoorDrill){CarDoorDrill.set('html','Drill: <b>' + CurrentConfig.doorDrillName + '</b>');}
        }else{
            if($('tabDoor')){$('tabDoor').removeClass('complete')}
            if(CarIncludeDoor){CarIncludeDoor.set('html','No');}
            if(CarDoorStyle){CarDoorStyle.set('html','');}
            if(CarDoorColour){CarDoorColour.set('html','');}
            if(CarDoorFrame){CarDoorFrame.set('html','');}
            if(CarDoorDrill){CarDoorDrill.set('html','');}
        }


	$('butaddcart').removeEvents();

        //// CAR COLOUR
	if(CarColour){
		if(CurrentConfig.colourID != null){
			$('tabUnit').addClass('complete');
		}	else{
			$('StatMessage').set('html','Please select a carcass color before adding to cart');
			$('tabUnit').removeClass('complete');
		}
	}

        //// CAR SIZE
	if(CarSize){
		if(CurrentConfig.carSizeID != 130 && CurrentConfig.carSizeID != null && CurrentConfig.carSizeID != "" || CurrentConfig.carSizeCustomW != null){
			if ($('tabUnit')) {
				$('tabUnit').addClass('complete');
			};
		}else{
			$('StatMessage').set('html','Please select a carcass size before adding to cart');
			if ($('tabUnit')) {
				$('tabUnit').removeClass('complete');
			}
		}
	}


        //// FRAME
	if(CarDoorFrame){
		if(CurrentConfig.includeDoors && CurrentConfig.frameID == null){
			if(CurrentConfig.glassID != null){
				if($('tabDoor')){$('tabDoor').removeClass('complete')};
				disPlayUpdate = 0;
				$('Addform').set('html', '');
				$('butaddcart').addClass('inactive');
				informUser.alert('Error', 'Please select a frame for your glazing option');
				Stopped = 1;
			}
		}
	}

	//// Door Colour
	if(CarDoorColour){
		if(CurrentConfig.includeDoors && CurrentConfig.colourID == 0){
                        if($('tabDoor')){$('tabDoor').removeClass('complete')};
                        $('butaddcart').addClass('inactive');
                        informUser.alert('', 'Please select a colour for your door(s)');
			Stopped = 1;
		}
	}

        //// Door Style
	if(CarDoorStyle){
		if(CurrentConfig.includeDoors && CurrentConfig.styleID == 0){
                        if($('tabDoor')){$('tabDoor').removeClass('complete')};
                        $('butaddcart').addClass('inactive');
                        informUser.alert('', 'Please select a style for your door(s)');
			Stopped = 1;
		}
	}


	

	//// Door STYLE
	if(CurrentConfig.includeDoors && CurrentConfig.styleID == null){
		$('StatMessage').set('html','Please select a door style before adding to cart');
	}

	


	if(CurrentConfig.carID == null || CurrentConfig.carColourID == null || (CurrentConfig.carSizeMethod == 2 && !vCustom) ||(CurrentConfig.carSizeMethod == 1  && CurrentConfig.carSizeID == 130 || CurrentConfig.carSizeID == null) ){
		//$('StatMessage').set('html','hERE Complete your selection before adding to cart');
		$('butaddcart').addClass('inactive');
		Stopped = 1;
	}else{
		if (disPlayUpdate == 1) {
			disPlayUpdate = 0;
			//get = CurrentConfig.toQueryString();
			$('StatMessage').set('html', '');
			var htmlcode = "";
			CurrentConfig.qty = $('qty').get('value');
			if(!CurrentConfig.has('url') || !$chk(CurrentConfig.url)){
				CurrentConfig.include('url', window.location.href);
			};

			CurrentConfig.each(function(value, key){
				htmlcode = htmlcode + "<input type='hidden' name='" + key + "' value='" + value + "' />";
			});

			 NewConfig = new Hash({
                                        producttype: 3,
                                        styleID: CurrentConfig.styleID,
                                        styleName: CurrentConfig.styleName,
                                        colourID:CurrentConfig.colourID,
                                        colourName: CurrentConfig.colourName,
                                        frameID: null,
                                        frameName: 'None',
                                        glassID: null,
                                        glassName: 'None',
                                        carSizeMethod: 1,
                                        carSizeID: CurrentConfig.carSizeID,
                                        carSizeName: CurrentConfig.carSizeName,
                                        carSizeCustomW: null,
                                        carSizeCustomH: CurrentConfig.carSizeCustomH,
                                        drillMount: null,
                                        drillName: 'None',
                                        total:'0.00',
                                        type:CurrentConfig.type,
                                        includeDoors:false,
                                        qty:1,
                                        url:window.location.href,
                                        carColourID : CurrentConfig.carColourID,
                                        carColourName : CurrentConfig.carColourName,
                                        carID: CurrentConfig.carID,
                                        carName: CurrentConfig.carName,
                                        doorDrillMount : null,
                                        doorDrillName : 'None',
                                        carDrawType : 1,
                                        carDrawTypeName: 'Metabox'
				});

			htmlcode = htmlcode + "<input type='hidden' name='newConfig' value='" + JSON.encode(NewConfig) + "' />";
                        htmlcode = htmlcode + "<input type='hidden' name='currentConfig' value='" + JSON.encode(CurrentConfig) + "' />";
			$('Addform').set('html', htmlcode);
			$('butaddcart').addEvent('click',function(){
				$('Addform').submit();
			});
			$('butaddcart').removeClass('inactive');
			if(Stopped == 1){
				//informUser.alert('Complete', 'Your Selection is now complete. Click on \'Add to Cart\' to continue.');
				Stopped = 0;
			}
		}
	}
//	if(disPlayUpdate == 1){
//		informUser.alert('Update', 'Your Selection have been updated.');
//	}
}

function customSizeSwitch(radio,sizeSelect,customInput){
    if($(radio + '_1').get('checked')){
        $(sizeSelect).set('disabled',false);
        $(customInput).set('disabled',true);
    }else{
        $(sizeSelect).set('disabled',true);
        $(customInput).set('disabled',false);
    }


    $(radio + '_1').addEvent('click',function(e){
        el = e.target;
        if(el.get('checked')){
             $(sizeSelect).set('disabled',false);
             $(customInput).set('disabled',true);
             CurrentConfig.carSizeMethod = el.get('value').toInt();
             $('customSize').fireEvent('blur');
        }
    })

    $(radio + '_2').addEvent('click',function(e){
        el = e.target;
        if(el.get('checked')){
             $(sizeSelect).set('disabled',true);
             $(customInput).set('disabled',false);
             CurrentConfig.carSizeMethod = el.get('value').toInt();
             $('carSizeSelect').fireEvent('change');
        }
    })
}