// función para separar marca y modelo
if (document.getElementById) {
	splitSelect = {
		selects:[],
		current:null,
		firstName:'model',
		secondName:'brand',
		nextDiv:null,
		init:function(){
			var s = document.getElementById(splitSelect.firstName);
			if(s){
				var c = document.createElement('select');
				var selected = -1;
				c.className="brandCombo";
				c.onchange=function(){
					splitSelect.choose(this.selectedIndex-1);
				}
				s.parentNode.insertBefore(c, s);
				var g = s.getElementsByTagName('optgroup');
				var o_none = document.createElement('option');
				o_none.innerHTML="-- select --";
				c.appendChild(o_none);
				var labelNew=document.createElement('label');
				labelNew.setAttribute("for", splitSelect.secondName);
				labelNew.innerHTML="Select the model:";
				splitSelect.nextDiv=document.getElementById('nested_models');
				
				splitSelect.nextDiv.appendChild(labelNew);
				splitSelect.nextDiv.style.display='none';
				for(var i=0,j=g.length;i<j;i++){
					var o = g[i].getElementsByTagName('option');
					var news = document.createElement('select');
					//s.parentNode.insertBefore(news, s);
					news.className="brandCombo";
					splitSelect.nextDiv.appendChild(news);
					news.style.display='none';
					splitSelect.selects.push(news);
					var k=0;
					var l = g[i].getAttribute('label');
					var o_brand = document.createElement('option');
					o_brand.innerHTML=l;
					c.appendChild(o_brand);
					while(o[0]){
						if(o[0].getAttribute('selected')==='selected'){
							splitSelect.nextDiv.style.display='block';
							splitSelect.current = i;
							c.selectedIndex = splitSelect.current+1;
							selected=k;
							news.style.display = 'block';
						}
						news.appendChild(o[0]);
						k++;
					}
					
				}
				
				if(selected!=-1){
					splitSelect.selects[splitSelect.current].selectedIndex=selected;
					splitSelect.selects[splitSelect.current].name=splitSelect.firstName;
					selected=-1;
				}
					
				//c.selectedIndex = splitSelect.current;
				s.parentNode.removeChild(s);
				c.id = splitSelect.secondName;
				c.name = splitSelect.secondName;
			}
		},
		choose:function(o){
			if(splitSelect.current !== null){
				splitSelect.selects[splitSelect.current].style.display='none';
				splitSelect.selects[splitSelect.current].name='';
			}
			if(o!=-1){
				splitSelect.nextDiv.style.display='block';
				splitSelect.selects[o].style.display='block';			
				splitSelect.selects[o].name=splitSelect.firstName;	
				splitSelect.selects[o].selectedIndex = 0;				
				splitSelect.current = o;
			}else{
				splitSelect.current=null;
				splitSelect.nextDiv.style.display='none';
			}			
		}
	}
	
	Event.add( window, 'load', splitSelect.init );
}