var get_dpt_url = 'http://www.nissan.fr/tools/test-it.-Par-44259-mlmttestitformrespstep3bxml.xml';
//var get_dpt_url = 'static_xml/test-it.-Par-44259-mlmttestitformrespstep3bxml.xml';
var get_dealer_url = 'http://www.nissan.fr/tools/test-it.-Par-44259-mlmttestitformrespstep3bxml.xml?dealerdepartment=';


var url_proxy="services/proxy/index.php?pmpurl=";
var default_dpt_options = [];

var spcCar = {
	"code": ["103857"],
	"xml": "static_xml/spe_car_dealers.xml",
	"isSpcCar": function (code) {
		return this.code.contains(code);
	},
	"wasSpcCar": false,
	"options": [],
	"response": null
};

window.addEvent('domready',function(){
	
	var changeCode = function (force){
		var isSpcCar = spcCar.isSpcCar($('code').value);
		
		if ((isSpcCar && spcCar.wasSpcCar || !isSpcCar && !spcCar.wasSpcCar) && !force) {
			return;
		}
		
		spcCar.wasSpcCar = isSpcCar;
		
		var arr = isSpcCar ? spcCar.options : default_dpt_options;
		
		$('department').set('html','');
		$('dealer').set('html','');
		new Element('option',{value:''}).set('html','-').inject($('dealer'));
		
		if (arr.length == 0) {
			return;
		}
		
		for(var i = 0; i < arr.length; i++){
			arr[i].inject($('department'));
		}
		arr[0].selected = true;
	};
	
	
	var myRequest = new Request({
		method: 'get', 
		url: url_proxy + encode_submit(get_dpt_url),
		onSuccess:function(responseText, responseXML){
			option_array = responseXML.getElementsByTagName('option');
			default_dpt_options.push(new Element('option',{value:''}).set('html','-'));
			for(var i = 0; i < option_array.length; i++){
				default_dpt_options.push(new Element('option',{value:option_array[i].getAttribute('value')}).set('html',option_array[i].firstChild.nodeValue));
			}
			changeCode(true);
		}
	});
	myRequest.send({});
	
	var myRequestSpcCar = new Request({
		method: 'get', 
		url: spcCar.xml,
		onSuccess:function(responseText, responseXML){
			option_array = responseXML.getElementsByTagName('deptOption');
			spcCar.response = responseXML;
			spcCar.options.push(new Element('option',{value:''}).set('html','-'));
			for(var i = 0; i < option_array.length; i++){
				spcCar.options.push(new Element('option',{value:option_array[i].getAttribute('value')}).set('html',option_array[i].firstChild.nodeValue));
			}
			changeCode(true);
		}
	});
	myRequestSpcCar.send({});
	
	$('code').addEvent('change',function(){
		changeCode();
	});
	
	
	$('department').addEvent('change',function(){
		if($('department').value){
			$('dealer').set('html','');
			new Element('option',{value:''}).set('html','-').inject($('dealer'));
			
			if (spcCar.isSpcCar($('code').value)) {
				var dept_array = spcCar.response.getElementsByTagName('dealer'),
					deal_array = [];
					
				for(var i = 0; i < dept_array.length; i++){
					if (dept_array[i].getAttribute('dept') == $('department').value) {
						deal_array = dept_array[i].getElementsByTagName('dealerOption');
						break;
					}
				}
				
				for(var i = 0; i < deal_array.length; i++){
					new Element('option',{value:formatDeealerCode(deal_array[i].getAttribute('value'))}).set('html',deal_array[i].firstChild.nodeValue).inject($('dealer'));
				}
			}
			else {
				var myRequest = new Request({
					method: 'get', 
					url: url_proxy + encode_submit(get_dealer_url+escape($('department').value)),
					onSuccess:function(responseText, responseXML){
						var fields_array = responseXML.getElementsByTagName('field');
						for(var i = 0; i < fields_array.length; i++){
							if (fields_array[i].getElementsByTagName('name')[0].getAttribute('value') == "dealercode"){
								option_array = fields_array[i].getElementsByTagName('option');
								break;
							}
						}
						for(var i = 0; i < option_array.length; i++){
							new Element('option',{value:formatDeealerCode(option_array[i].getAttribute('value'))}).set('html',option_array[i].firstChild.nodeValue).inject($('dealer'));
						}
					}
				});
				myRequest.send({});
			}
		}
	});
});

function formatDeealerCode(dealer_code){
	return dealer_code;
	/*completed_code = "00000000"+dealer_code;
	return completed_code.substr(completed_code.length-8);*/
}

