var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");}
 catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  xmlhttp = new XMLHttpRequest();
}

function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();

function init() {

	if(document.forms['form_wheretobuy'])
	{
		getCountries();
		
		//alert(document.getElementById('form_wheretobuy').country.options[document.getElementById('form_wheretobuy').country.selectedIndex].value);
	}
}

function getDealers(country_id)
{
	xmlhttp.open("GET", "http://www.ravo-fayat.com/index-ajax.php?q=assets/snippets/dealers/Dealers.php&action=getDealers&country_id=" + country_id, true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			document.getElementById('form_dealers').innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
}

function getCountries()
{
	xmlhttp.open("GET", "http://www.ravo-fayat.com/index-ajax.php?q=assets/snippets/dealers/Dealers.php&action=getCountries", true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			document.getElementById('form_countries').innerHTML = xmlhttp.responseText;
			var selectedCountry = document.getElementById('form_wheretobuy').country.options[document.getElementById('form_wheretobuy').country.selectedIndex].value;
			getDealers(selectedCountry);
			//alert(document.getElementById('form_wheretobuy').country.options[document.getElementById('form_wheretobuy').country.selectedIndex].value);
			//alert(document.getElementById('form_wheretobuy').country.selectedIndex);
		}
	}
	xmlhttp.send(null);
}

addEvent(window, 'load', init);
