// JavaScript Document
function createRequestObject() {
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		ro = new XMLHttpRequest();
	}
	return ro;
}


var http = createRequestObject();
var http1 = createRequestObject();
var testimonial = null;


function sendRegionRequest(regionId,thisPage) {
	http.open('get', 'ajax.php?regionId=' + regionId + '&thisPage=' + thisPage + "&timestamp=" + new Date().getTime());
	http.onreadystatechange = handleRegionResponse;
	http.send(null);
}

function handleRegionResponse() {
	if(http.readyState == 4) {
		var xmldoc = http.responseXML;
		var myCountries = xmldoc.getElementsByTagName("country");
		var myRegion = xmldoc.getElementsByTagName('name').item(0).firstChild.data;
		var thisPage = xmldoc.getElementsByTagName('thisPage').item(0).firstChild.data;
		var myCoords = "";
		var countryCoords = "";
		var message = "<p>Please select a country...</p>";
		if(myRegion == "Africa" & thisPage == "customers" | myRegion == "South America" & thisPage == "customers"){
			message = "";
		}
		var countryList = '<h2>'+myRegion+'</h2>' + message;
		countryList += "<ul class='locationList'>";
		for(var i=0; i < myCountries.length; i++) {
			node = myCountries[i];
			countryName = node.getElementsByTagName('name')[0].firstChild.nodeValue;
			linkname = countryName.toLowerCase();
			linkname = linkname.replace(/ /g,"_");
			linkname = linkname.replace(/-/g,"_");
			linkname = linkname.replace(/'/g,"_");
			linkname = linkname.replace(/&_/g,"");
			linkname = linkname.replace(/& /g,"");
			linkname = linkname.replace(/the_/g,"");
			linkname = linkname.replace(/___/g,"_"); 
			linkname = linkname.replace(/ /g,"_");
			countryName = countryName.replace(/ Europe/g,"");
			countryName = countryName.replace(/ Asia/g,"");
			countryId = node.getElementsByTagName('countryId')[0].firstChild.nodeValue;
			countryList += "<li><a onmouseover=\"showCountry('" + linkname + "');\" onmouseout=\"hideCountry('" + linkname + "');\" href=\"javascript:sendCountryRequest('" + countryId + "','" + thisPage + "');\">" + countryName + "</a></li>";
		}
		countryList += "</ul>";
		document.getElementById('listCountries').innerHTML = countryList;
		document.getElementById('currentRegion').innerHTML = '&nbsp;&gt;&gt; ' + myRegion;
		cursor_clear();	
	} 
	else {
		document.getElementById('listCountries').innerHTML = '<p>Loading Countries...</p>';
		cursor_wait();	
	} 
}	

// Request and Handler for Countries > Companies

function sendCountryRequest(countryId,thisPage) {
	http1.open('get', 'ajax.php?countryId=' + countryId + '&thisPage=' + thisPage + "&timestamp=" + new Date().getTime());
	http1.onreadystatechange = handleCountryResponse;
	http1.send(null);
}

function handleCountryResponse() {

	if(http1.readyState == 4) {
		var xmldoc = http1.responseXML;
		var countryName = xmldoc.getElementsByTagName('name').item(0).firstChild.data;
		var thisPage = xmldoc.getElementsByTagName('thisPage').item(0).firstChild.data;
		setBackground(countryName,'selectedCountry');
		countryName = countryName.replace(/ Europe/g,"");
		countryName = countryName.replace(/ Asia/g,"");
		var resultList = '<h2 class="country-name">'+countryName+'</h2>';
		if(thisPage == "customers") {
			var myCompanies = xmldoc.getElementsByTagName("company");
			
				for(var i=0; i < myCompanies.length; i++) {
					node = myCompanies[i];
					companyName = node.getElementsByTagName('name')[0].firstChild.nodeValue;
					companyId = node.getElementsByTagName('companyId')[0].firstChild.nodeValue;
					testimonialText = node.getElementsByTagName('testimonial')[0].firstChild.nodeValue;
					testimonialBy = node.getElementsByTagName('testimonialBy')[0].firstChild.nodeValue;
					caseStudyId = node.getElementsByTagName('caseStudyId')[0].firstChild.nodeValue;
					
					resultList += '<div class="company"><h2 class="companyName">' + companyName + '</h2>';
					
					if(testimonialText != "null") {
						resultList += '<div class="companyLinks" id="showTestimonial'+companyId+'"><a href="javascript:showTestimonial('+companyId+');">Show Testimonial</a></div>';
					}
					if(caseStudyId != "null" && caseStudyId != "0") {
						resultList += '<div class="companyLinks"><a href="javascript:newwindow(' + "'case-studies-detail-single.php?caseid=" + caseStudyId + "','popup','702','560','yes')" + '">View Case Study</a></div>';
					}
					if(testimonialText != "null") {
						resultList += '<p class="testimonial" id="testimonial' + companyId + '">&quot;' + testimonialText + '&quot;</p>';
					}
					if(testimonialBy != "null") {
						resultList += '<p class="testimonialBy" id="testimonialBy' + companyId + '">' + testimonialBy + '</p>';
					}
					resultList += "</div>";
				}
			resultList += "</ul>";
			document.getElementById('listCompanies').innerHTML = resultList;
			
			if(testimonial) {
//alert(testimonial);				
				waitTestimonial();
			}
		}
		else {
			
			//XML for Distributors
			var result = xmldoc.getElementsByTagName('result').item(0).firstChild.data;
			resultList += '<p>' + result + '</p>';
			var myDistributors = xmldoc.getElementsByTagName("distributor");
			for(var i=0; i < myDistributors.length; i++) {
				node = myDistributors[i];
				distributorId = node.getElementsByTagName('distributorId')[0].firstChild.nodeValue;
				distributorName = node.getElementsByTagName('distributorName')[0].firstChild.nodeValue;
				address = node.getElementsByTagName('address')[0].firstChild.nodeValue;
				
				resultList += '<div class="distributor">';
				resultList += '<div class="label">Distributor:</div><div class="detail name">' + distributorName + '</div>';
				if(node.getElementsByTagName('phone')[0].firstChild) {
					contact = node.getElementsByTagName('contact')[0].firstChild.nodeValue;
					resultList += '<div class="label">Contact Name:</div><div class="detail">' + contact + '</div>';
				}
				resultList += '<div class="label">Address:</div><div class="detail">' + address + '</div>';
				resultList += '<div class="label">Contact Details:</div><div class="detail">';
				
				if(node.getElementsByTagName('phone')[0].firstChild) {
					phone = node.getElementsByTagName('phone')[0].firstChild.nodeValue;
					resultList += 'Phone: ' + phone + '<br />';
				}
				if(node.getElementsByTagName('fax')[0].firstChild) {
					fax = node.getElementsByTagName('fax')[0].firstChild.nodeValue;
					resultList += 'Fax: ' + fax + '<br />';
				}
				if(node.getElementsByTagName('email')[0].firstChild) {
					email = node.getElementsByTagName('email')[0].firstChild.nodeValue;
					resultList += 'Email: ' + email + '<br />';
				}
				if(node.getElementsByTagName('web')[0].firstChild) {
					web = node.getElementsByTagName('web')[0].firstChild.nodeValue;
					resultList += 'Web: <a href="http://' + web + '" target="_blank">' + web + '</a>';
				}
				
				resultList += '</div>';
				resultList += '<div class="clearFix">&nbsp;</div>';
				resultList += '</div>';
			}
			
			document.getElementById('listDistributors').innerHTML = resultList;
		}
		cursor_clear();
	}
	else {
		if(typeof document.getElementById('listCompanies') == "undefined") {
			document.getElementById('listDistributors').innerHTML = '<p>Loading...</p>';
		}
		if(typeof document.getElementById('listDistributors') == "undefined") {
			document.getElementById('listCompanies').innerHTML = '<p>Loading...</p>';
		}
		cursor_wait();	
	} 
}

function waitTestimonial() {
	if(testimonial) {
		var elt = document.getElementById('testimonial'+testimonial);
		if(elt) {
			showTestimonial(testimonial);
			testimonial = null;
		} else {
			window.setTimeout('waitTestimonial()', 500);
		}
	}
}

//Show Testimonial 
function showTestimonial(companyId) {
	document.getElementById('testimonial'+companyId).style.display = 'block';
	document.getElementById('testimonialBy'+companyId).style.display = 'block';
	//Change link to hide
	var hideText = '<a href="javascript:hideTestimonial('+companyId+');">Hide Testimonial</a>';
	document.getElementById('showTestimonial'+companyId).innerHTML = hideText;
}

//Hide Testimonial 
function hideTestimonial(companyId) {
	document.getElementById('testimonial'+companyId).style.display = 'none';
	document.getElementById('testimonialBy'+companyId).style.display = 'none';
	//Change link to hide
	var showText = '<a href="javascript:showTestimonial('+companyId+');">Show Testimonial</a>';
	document.getElementById('showTestimonial'+companyId).innerHTML = showText;
}

function cursor_wait() {
  document.body.style.cursor = 'wait';
}

function cursor_clear() {
  document.body.style.cursor = 'default';
}

function cursor_pointer() {
	document.body.style.cursor = 'pointer';
}

function showRegion(region) {
	setBackground(region,'hoverRegions');
}

function hideRegion(region) {
	foo = document.getElementById('hoverRegions');
	foo.style.backgroundImage = 'none';
}		
	
function selectRegion(region, regionId, thisPage) {
	document.getElementById('countryMap').style.display = 'block';
	document.getElementById('regionMap').style.display = 'none';
	sendRegionRequest(regionId,thisPage);
	setBackground(region,'countryMap');
	// Set all transMaps to hide
	clearAreas(thisPage);
	// Show seleceted map
	document.getElementById(region + '_map').style.display = 'block';
}

function showCountry(country) {
	setBackground(country,'hoverCountries');
}

function hideCountry(country) {
	foo = document.getElementById('hoverCountries');
	foo.style.backgroundImage = 'none';	
}
	
function setBackground(area,obj) {
	area = area.toLowerCase();
	area = area.replace(/ /g,"_");
	area = area.replace(/-/g,"_");
	area = area.replace(/'/g,"_");
	area = area.replace(/&_/g,"");
	area = area.replace(/& /g,"");
	area = area.replace(/the_/g,"");
	area = area.replace(/___/g,"_"); 
	foo = document.getElementById(obj);
	if(obj == "hoverRegions") {
		area += "_region";
	}
	
	if(obj == "countryMap") {
		area += "_large";
	}
	
	var background = 'url(images/map/countries/' + area + '.gif)';
	
	foo.style.backgroundImage = background;
}

function clearAreas(thisPage){
	if(thisPage == "distributors") {
		document.getElementById('listDistributors').innerHTML = "";
		document.getElementById('australasia_map').style.display = 'none';
	}
	if(thisPage == "customers") {
		document.getElementById('listCompanies').innerHTML = "";
	}
	document.getElementById('selectedCountry').style.background = 'none';
	document.getElementById('africa_map').style.display = 'none';
	document.getElementById('asia_map').style.display = 'none';
	document.getElementById('europe_map').style.display = 'none';
	document.getElementById('north_america_map').style.display = 'none';
	document.getElementById('south_america_map').style.display = 'none';
	document.getElementById('central_america_map').style.display = 'none';
}

