////////////////////////
///// Popup Window /////
////////////////////////
function PopupWindow(url){
	var popupWindow = window.open(url,'Popup_Window');
	popupWindow.opener = window;
	popupWindow.focus();
	return false;
}

//////////////////////////////////////////
///// Get Specified Distributor List /////
//////////////////////////////////////////
function GetDistributorList(url){

	//// Initialize AJAX ////
	var request = false;
	if(window.XMLHttpRequest){
		request = new XMLHttpRequest();
	}else if(window.ActiveXObject){
		request = new ActiveXObject('Microsoft.XMLHTTP');
	}else{
		return true;
	}

	//// Hide the Distributor Locator DIV ////
	var list = '';
	var div = document.getElementById('dist_list');
	div.style.display = 'none';
	div.style.visibility = 'hidden';
	div.innerHTML = list;

	//// Retrieve Distributor Locator Data ////
	request.open("GET", url + '&simple=true',false);
	request.send(null);
	var response = request.responseText;
	//// Parse the Distributor Locator Data ////
	if((response != '') && (response.indexOf('<!DOCTYPE') == -1)){
		var list = [];
		var i = 0;
		var j = 0;		
		var entries = eval('(' + response + ')');
		/// Convert Entries into HTML ///
		var dtype = '';
		switch(entries[0]['dtype']) {
			case 'Industrial':
				var listtype = 'industrial';
				break;
			case 'Heavy Duty':
				var listtype = 'hdd';
				break;
                        case 'OEM':
                                var listtype = 'oem';
                                break;
		}


		list['hdd'] = '';
		list['industrial'] = '';
		list['oem'] = '';

		var atypes = [];
		for(i = 0; i < entries.length; i++){
			var entry = entries[i];
			if(dtype != entry['dtype']) {
				if(dtype != '') {
				  listtype = entry['dtype'];// == 'Industrial Market' ? 'plastics' : 'industrial';
                                  if(entry['dtype'] == 'OEM') listtype = 'oem';
                                  if(entry['dtype'] == 'Industrial') listtype = 'industrial';
                                  if(entry['dtype'] == 'Heavy Duty') listtype = 'hdd';
				  // = dtype;
				  var atypes = [];
				}
				if(entry['dtype'] == 'OEM') {
				 list[listtype] = list[listtype] + '<h2>' + entry['dtype'] + ' Sales Representatives</h2>';
				}
				else {
				 list[listtype] = list[listtype] + '<h2>' + entry['dtype'] + ' Aftermarket</h2>';
				}
			}
			if(atypes[entry['company_name']] != entry['company_name']) {
				dtype = entry['dtype'];
				list[listtype] = list[listtype] + '<ul><li class="first">' + entry['company_name'] + '</li>';
	                        list[listtype] = list[listtype] + '<li>' + entry['address'] + (entry['address2'] != '' ? '<br />' + entry['address2'] : '') + '</li>';
				list[listtype] = list[listtype] + '<li>' + entry['city'] + ', ' + entry['state_display'] + ' ' + entry['zip'] + '</li>';
				if(entry['phone'] != '') list[listtype] = list[listtype] + '<li>' + entry['phone'] + '</li>';
				if(entry['fax'] != '') list[listtype] = list[listtype] + '<li>' + entry['fax'] + '</li>';
	                        if(entry['information'] != '') list[listtype] = list[listtype] + '<li>' + entry['information'] + '</li>';
	                        if(entry['contact'] != '') list[listtype] = list[listtype] + '<li>' + entry['contact'] + '</li>';
				if(entry['email'] != '') list[listtype] = list[listtype] + '<li><a href="mailto:' + entry['email'] + '">' + entry['email'] + '</a></li>';
				if(entry['website'] != '') list[listtype] = list[listtype] + '<li><a href="http://' + entry['website'] + '" onclick="return PopupWindow(this.href);">' + entry['website'] + '</a></li>';
				list[listtype] = list[listtype] + '</ul>';
			}
			atypes[entry['company_name']] = entry['company_name'];
		}

		var listdata = '';
		if(list['oem'] != '') {
			listdata += '<div class="halign" id="oem">' + list['oem'] + '</div>';
		}
		if(list['industrial'] != '') {
			listdata += '<div class="halign" id="industrial">' + list['industrial'] + '</div>';
		}
		if(list['hdd'] != '') {
			listdata += '<div class="halign" id="hdd">' + list['hdd'] + '</div>';
		}	
		/// Display the Distributor Locator DIV ///
		if(listdata != ''){
			div.innerHTML = listdata;
			div.style.display = '';
			div.style.visibility = 'visible';
		}
	}
        else {
          div.innerHTML = 'There were 0 results for your search';
          div.style.display = '';
          div.style.visibility = 'visible';
        }

	//// Override the Default Click ////
	return false;
}

///////////////////////////////////////////////
///// Get Specified Distributor List INTL /////
///////////////////////////////////////////////
function GetDistributorListINTL(url){

	//// Initialize AJAX ////
	var request = false;
	if(window.XMLHttpRequest){
		request = new XMLHttpRequest();
	}else if(window.ActiveXObject){
		request = new ActiveXObject('Microsoft.XMLHTTP');
	}else{
		return true;
	}

	//// Hide the Distributor Locator DIV ////
	var list = '';
	var div = document.getElementById('dist_list');
	div.style.display = 'none';
	div.style.visibility = 'hidden';
	div.innerHTML = list;

	//// Retrieve Distributor Locator Data ////
	request.open("GET", url + '&simple=true',false);
	request.send(null);
	var response = request.responseText;
	//// Parse the Distributor Locator Data ////
	if((response != '') && (response.indexOf('<!DOCTYPE') == -1)){
		var list = '';
		var i = 0;
		var j = 0;		
		var entries = eval('(' + response + ')');
		/// Convert Entries into HTML ///
		var region_groupby = '~';
		var company = '~';
		var atypes = [];

		for(i = 0; i < entries.length; i++){
			var entry = entries[i];
			if(region_groupby != entry['region_groupby']) {
				var atypes = [];
				var region = entry['region_groupby'] == '' ? "International" : entry['region_groupby'];
                                if(region == null) region = 'International Aftermarket';
				list += '<h2>' + region + '</h2>';
			}
			if(company != entry['company_name']) {
				dtype = entry['dtype'];
				list = list + '<ul><li class="first">' + entry['company_name'] + '</li>';
	                        list = list + '<li>' + entry['address'] + (entry['address2'] != '' ? '<br />' + entry['address2'] : '') + '</li>';
				list = list + '<li>' + entry['city'] + ', ' + entry['state_display'] + ' ' + entry['zip'] + '</li>';
				if(entry['phone'] != '') list = list + '<li>' + entry['phone'] + '</li>';
				if(entry['fax'] != '') list = list + '<li>' + entry['fax'] + '</li>';
	                        if(entry['information'] != '') list = list + '<li>' + entry['information'] + '</li>';
	                        if(entry['contact'] != '') list = list + '<li>' + entry['contact'] + '</li>';
				if(entry['email'] != '') list = list + '<li><a href="mailto:' + entry['email'] + '">' + entry['email'] + '</a></li>';
				if(entry['website'] != '') list = list + '<li><a href="http://' + entry['website'] + '" onclick="return PopupWindow(this.href);">' + entry['website'] + '</a></li>';
				list = list + '</ul>';
			}
			region_groupby = entry['region_groupby'];
			company = entry['company_name'];
		}

		var listdata = '';
                if(list == null) list = 'International';
		listdata = '<div id="plastics">' + list + '</div>';

		/// Display the Distributor Locator DIV ///
		if(listdata != ''){
			div.innerHTML = listdata;
			div.style.display = '';
			div.style.visibility = 'visible';
		}
	}

	//// Override the Default Click ////
	return false;
}

