var currentConfiguration = new Object;

function LeasingLoadModelSelect(elCategory){
		$('butonSubmit').style.display = 'none';
		$('waitForForm').style.display = '';
		$('leasingProductID').disabled = true;
		var baseHref = document.getElementsByTagName('base')[0].href;
		var categoryID = elCategory.options[elCategory.selectedIndex].value;
		new Ajax.Request(baseHref+'index.html/showroom|getOptions',  {
		    method:'post',    
		    parameters: {'categoryID' : categoryID},
		    onSuccess: function(transport){
		    		var response = transport.responseText;
		    		productID = 0;
						var aProducts = new Array(); thisProduct = new Array();
						response = response.substr(0,response.length-2);
						aProducts = response.split('||');
						$('leasingProductID').update();
						for(i=0; i < aProducts.length; i++){
								thisProduct	= aProducts[i].split('|');
								var newOptionEl = new Element('option', {'value': thisProduct[0]}).update(thisProduct[1]);
								$('leasingProductID').insert(newOptionEl);
								
						}
//					alert($('leasingProductID').innerHTML);	
					$('leasingProductID').disabled = false;
					LeasingLoadPrice($('leasingProductID'));
		    },
		    onFailure: function(){  }
		  });
	}


function LeasingLoadPrice(elProduct){
		$('butonSubmit').style.display = 'none';
		$('waitForForm').style.display = '';
		var baseHref = document.getElementsByTagName('base')[0].href;
		var productID = elProduct.options[elProduct.selectedIndex].value;
		new Ajax.Request(baseHref+'index.html/showroom|getOptions',  {
		    method:'post',    
		    parameters: {'productID' : productID},
		    onSuccess: function(transport){
		    		var response = transport.responseText;
		    		$('leasingPriceCIP').value = response;
		    		$('butonSubmit').style.display = '';
						$('waitForForm').style.display = 'none';
		    },
		    onFailure: function(){  }
		  });
	}

var lastCategoryID = 0;
var lastCategoryYear = 0;
var lastVersion = 0;
function loadConfiguratorCategory(el){
		el =$(el);
		
		var baseHref = document.getElementsByTagName('base')[0].href;
		el.toggleClassName('selected');
		
		var categoryID = lastCategoryID = el.id.replace('categoryID_', '');
		var categories = $('configurator').select('[class="categorii"]');
		
		if(categories.length)
			categories.each(
				function(category) {
					if(category.id == el.id){
//						alert('here');
						Effect.Fade(category.up(), { 'queue': 'start', 'duration':'0.2' });
					}
					else
						Effect.Fade(category.up(), { 'duration':'0.5'});
			});
			
			Effect.Appear(el.up(), { 'queue': 'end' });
		
		currentConfiguration.categoryID = categoryID;
		
		new Ajax.Request(baseHref+'index.html/showroom|getOptions',  {
		    method:'post',    
		    parameters: {'categoryID' : categoryID, 'XML':1},
		    onSuccess: function(transport){
		    		var response = transport.responseText;
		    		aYears = response.split('|');
		    		Effect.Appear($('year'), { 'queue': 'end' });
		    		for(i=0; i < aYears.length; i++){
		    			oneYear = aYears[i].split('_');
//		    			alert(oneYear);
		    			var h3 = new Element('h3', {'style':'display:none;'});	
		    			var a  = new Element('a', {'href':'products|categoryListing?categoryID='+categoryID,'class':'years'}).update(oneYear[1]);
		    			a.writeAttribute('id', 'year_'+oneYear[0]);
		    			a.onclick = new Function('loadConfiguratorYear(\''+oneYear[0]+'\');return false;');
		    			h3.insert(a);
		    			$('yearReceiver').insert(h3);
		    			Effect.Appear($('year_'+oneYear[0]).up(), { 'queue': 'end' });
		    		}
		    },
		    onFailure: function(){  }
		  });
}

function loadConfiguratorYear(elID){
		var baseHref = document.getElementsByTagName('base')[0].href;
		var el = $('year_'+elID);
		el.toggleClassName('selected');
		
		var categoryYear = elID;
		var allyears = $('configurator').select('[class="years"]');
		if(allyears.length)
			allyears.each(
				function(year) {
					if(year.id == el.id){
						Effect.Fade(year.up(), { 'queue': 'start' });
					}
					else
						Effect.Fade(year.up());
			});
			
			Effect.Appear(el.up(), { 'queue': 'end' });
		
//		alert(categoryYear);
		currentConfiguration.bodyTypeID = categoryYear;
		new Ajax.Request(baseHref+'index.html/showroom|getOptions',  {
		    method:'post',    
		    parameters: {'categoryID' : lastCategoryID, 'bodyTypeID': categoryYear, 'XML':1},
		    onSuccess: function(transport){
		    		var rXML = transport.responseXML;
		    		
		    		if(document.all)
							root = rXML.childNodes[1];
						else
							root = rXML.childNodes[0];
							
		    		Effect.Appear($('motorcycle'), { 'queue': 'end' });
		    		var items = $A(root.getElementsByTagName('item'));
		    		var counter = 0;
						if(items.length) {
							items.each( function(item) {
								var productName = item.getElementsByTagName('name')[0].firstChild.nodeValue;
								var productURL = item.getElementsByTagName('url')[0].firstChild.nodeValue;

								var productID = item.getElementsByTagName('productID')[0].firstChild.nodeValue;
								var aTag = new Element('a', {'id':'productID_'+productID, 'href': productURL, 'class':'products', 'style':'display:none;'});
								aTag.onclick = new Function('loadConfiguratorProductVersions(\''+productID+'\');return false;');
								var spanTag = new Element('span').update(productName);
								aTag.insert(spanTag);
								$('motorcyclesReceiver').insert(aTag);
								Effect.Appear($('productID_'+productID), { 'queue': 'end' });
								counter++;
								if(counter % 4 == 0)
									var divTag = new Element('div', {'class': 'cleaner'}).update('&nbsp;');
									$('motorcyclesReceiver').insert(divTag);
							});
						}
		    },
		    onFailure: function(){  }
		  });
}

function loadConfiguratorProductVersions(productID){
		var baseHref = document.getElementsByTagName('base')[0].href;
		var el = $('productID_'+productID);
		el.toggleClassName('selected');		
		
		var queue = Effect.Queues.get('global');
		queue.each(function(effect) { effect.cancel(); });

		var allProducts = $('configurator').select('.products');
		
		if(allProducts.length)
			allProducts.each(
				function(tmpProduct) {
					if(tmpProduct.id != el.id)
						Effect.Fade(tmpProduct);
			});
			Effect.Appear(el, { 'queue': 'end' });

		currentConfiguration.productID = productID;		
		new Ajax.Request(baseHref+'index.html/showroom|getOptions',  {
		    method:'post',    
		    parameters: {'productID' : productID, 'XML':1},
		    onSuccess: function(transport){
		    		var rXML = transport.responseXML;
		    		
		    		if(document.all)
							root = rXML.childNodes[1];
						else
							root = rXML.childNodes[0];
							
		    		Effect.Appear($('optionsLevel'), { 'queue': 'end' });
		    		var items = $A(root.getElementsByTagName('item'));
		    		var counter = 0;
						if(items.length) {
							items.each( function(item) {
								var featureName = item.getElementsByTagName('name')[0].firstChild.nodeValue;
								var featurePrice = item.getElementsByTagName('price')[0].firstChild.nodeValue;
								var featureID = item.getElementsByTagName('featureID')[0].firstChild.nodeValue;
								
								var aTag = new Element('a', {'id':'featureID_'+featureID, 'href': '#', 'class':'features', 'style':'display:none;'});
								aTag.onclick = new Function('loadShowColors(\''+featureID+'\');return false;');
								var spanTag = new Element('span').update(featureName);
								aTag.insert(spanTag);
								$('optionsLevelReceiver').insert(aTag);
								Effect.Appear($('featureID_'+featureID), { 'queue': 'end' });
								counter++;
								if(counter % 4 == 0)
									var divTag = new Element('div', {'class': 'cleaner'}).update('&nbsp;');
									
								$('optionsLevelReceiver').insert(divTag);
							});
						}
		    },
		    onFailure: function(){  }
		  });
}

var aColorCodes = new Array();

function loadShowColors(featureID){
		var baseHref = document.getElementsByTagName('base')[0].href;
		var el = $('featureID_'+featureID);
		el.toggleClassName('selected');		
		
		var queue = Effect.Queues.get('global');
		queue.each(function(effect) { effect.cancel(); });

		var allFeatures = $('configurator').select('.features');
		
		if(allFeatures.length)
			allFeatures.each(
				function(tmpFeature) {
					if(tmpFeature.id != el.id)
						Effect.Fade(tmpFeature);
			});
			Effect.Appear(el, { 'queue': 'end' });
		
		currentConfiguration.featureID = featureID;
		new Ajax.Request(baseHref+'index.html/showroom|getOptions',  {
		    method:'post',    
		    parameters: {'showColorsProductID' : currentConfiguration.productID, 'XML':1},
		    onSuccess: function(transport){	
		    		Effect.Appear($('colors'));
		    		var rXML = transport.responseXML;
		    		
		    		if(document.all)
							root = rXML.childNodes[1];
						else
							root = rXML.childNodes[0];
						
		    		var items = $A(root.getElementsByTagName('item'));
		    		var counter = 0;
						if(items.length) {
							items.each( function(item) {
								var colorID = item.getElementsByTagName('colorID')[0].firstChild.nodeValue;
								var photo = item.getElementsByTagName('photo')[0].firstChild.nodeValue;
								var thisBgcolor = item.getElementsByTagName('bgcolor')[0].firstChild.nodeValue;
								var firstItem = item.getElementsByTagName('firstItem')[0].firstChild.nodeValue;
								
								var thisStyle = 'background-color:#' + thisBgcolor+';';
								var ImgTag = new Image();
								ImgTag.src = photo;
								ImgTag.id =  'colorID_'+colorID;
								var thisClass = '';
								if(firstItem != 1){
									ImgTag.style.display = 'none';
								}
								else
									thisClass = 'activ';
								
								$('culoriAuto').insert(ImgTag);
								var aTag = new Element('a', {'href': '#', 'class':thisClass, 'style':thisStyle});
								aTag.onclick = new Function('loadColor(\''+colorID+'\');return false;');
								var sTag = new Element('span');
								sTag.update('&nbsp;');
								aTag.insert(sTag);

								$('selectorCulori').insert(aTag);
								aColorCodes[colorID] = colorID;
							});
						}
						var cleaner = new Element('div', { 'class':'cleaner' });
						$('selectorCulori').insert(cleaner);
						
						Effect.Appear($('colorsReceiver'));
						// Effect.Appear($('lyteframeReceiver'));
						// setDetailsURL();
						Effect.Appear($('pasulUrmator'));
		    },
		    onFailure: function(){  }
		  });
		
	}

function showRequestLeasingOfferForm(elID){
		new Effect.Grow($('leasignForm'));
		new Effect.SwitchOff($(elID));
	}


function setDetailsURL(){
		
		baseHref = document.getElementsByTagName('base')[0].href;
		$('lyteframeReceiver').href = baseHref + 'products|showroomDetails?productID='+currentConfiguration.productID+'&featureID='+currentConfiguration.featureID+'&colorID='+currentConfiguration.colorID ;
	}

function checkRequestForm(){
		//checkin address setup
		
		
		var requiredFields = {
														'firstName':
															{
																'fullContainer':'firstName',
																'fullContainerMsg':'Nu ati completat campul Nume'
															},
														'lastName':
															{
																'fullContainer':'lastName',
																'fullContainerMsg':'Nu ati completat campul Prenume'
															},
														'email':
															{
																'fullContainer':'email',
																'fullContainerMsg':'Nu ati completat corect campul Email',
																'specialVerify' : 'email'
															}	
													}
		var error = false;
		requiredFields = $H(requiredFields);
		requiredFields.each(function(t) {
			var isChecked = 0;
			var m = document.requestLeasingFrm[t[0]];
			
			if(t[1].specialVerify == 'email'){
					var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
					if (!filter.test(m.value)) {
						error = true;
						showToolTipFader(t[1].fullContainerMsg, $(t[1].fullContainer));
						throw $break;
					}
				}
			else{
				if(!m.value) {
					error = true;
					showToolTipFader(t[1].fullContainerMsg, $(t[1].fullContainer));
					throw $break;
				}
			}
				
		});
		if(!error) {
			document.requestLeasingFrm.submit();
			return false;
		}
//		return false;
	}	


function loadColor(colorID){
		for(i in aColorCodes){
				var curObj = $('colorID_'+i);
				if(curObj)
					curObj.style.display = 'none';	
			}
		
		curObj = $('colorID_'+colorID);
		curObj.style.display = '';
		currentConfiguration.colorID = colorID;		
		//console.log(colorID);
		if($('colorID')){
			$('colorID').setValue(colorID);
		}
	}	
