﻿var request = null;

function createRequest() {
	try {
       request = new XMLHttpRequest();
     } catch (trymicrosoft) {
       try {
         request = new ActiveXObject("Msxml2.XMLHTTP");
       } catch (othermicrosoft) {
         try {
           request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (failed) {
           request = null;
         }
       }
     }

     if (request == null)
       alert("Error creating request object!");
}

function countryDisp(country,lang) {
	var rec = document.getElementById("record-table");
	removeElement(rec);
	
	var countrydiv = document.createElement("div");
	countrydiv.setAttribute("id","countrydiv-" + country);
	countrydiv.className = "countrydiv-class";
	if (lang == "English") {
		if (country == "Global") {
			countrydiv.innerHTML = "<span class=\"countrydiv-name\">Top Global downloads:</span>";
		} else {
			countrydiv.innerHTML = "<span class=\"countrydiv-name\">Top downloads for " + country + ":</span>";
		}
	} else {
		if (country == "Global") {
			countrydiv.innerHTML = "<span class=\"countrydiv-name\">Top Global downloads:</span>";
		} else	if (country == "India") {
			countrydiv.innerHTML = "<span class=\"countrydiv-name\">Top downloads for インド:</span>";
		} else	if (country == "Japan") {
			countrydiv.innerHTML = "<span class=\"countrydiv-name\">Top downloads for 日本:</span>";
		} else	if (country == "China") {
			countrydiv.innerHTML = "<span class=\"countrydiv-name\">Top downloads for 中国:</span>";
		} else	if (country == "USA") {
			countrydiv.innerHTML = "<span class=\"countrydiv-name\">Top downloads for アメリカ:</span>";
		} else	if (country == "Mexico") {
			countrydiv.innerHTML = "<span class=\"countrydiv-name\">Top downloads for メキシコ:</span>";
		} else	if (country == "Russia") {
			countrydiv.innerHTML = "<span class=\"countrydiv-name\">Top downloads for アメリカ:</span>";
		}
		
	}
	
	rec.appendChild(countrydiv);
	
	if (lang == "English") {
		showLoader(countrydiv,"Please wait while we retrieve the pertinent records...");
	} else {
		showLoader(countrydiv,"読み込み中…");
	}
	
	createRequest();
	var url = "/ajaxfiles/downloads/fetch-mdownloaded-remote.php?lang=" + lang;
	url = url + "&country=" + country;
	url = url + "&dummy=" + new Date().getTime();
	request.open("POST",url,true);
	request.onreadystatechange = mDownloadedFetched;
	request.send(null);
}
function mDownloadedFetched() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			var xmlDOC = request.responseXML;
			removeElement(document.getElementById("fxid"));
			
			var country = xmlDOC.getElementsByTagName("country")[0].firstChild.nodeValue;
			var language = xmlDOC.getElementsByTagName("language")[0].firstChild.nodeValue;
			var cnt = xmlDOC.getElementsByTagName("cnt")[0].firstChild.nodeValue;
			var totalcnt = xmlDOC.getElementsByTagName("totalcnt")[0].firstChild.nodeValue;
			
			var rec = document.getElementById("countrydiv-" + country);
			
			if (cnt != 0) {
				var vareditors = document.createElement("ul");
				vareditors.className = "lister";
				var i = 0; 
				while (i < cnt) {
					var lister = document.createElement("li");
					lister.innerHTML = "<a href=\"?q=node/" + xmlDOC.getElementsByTagName("nid-" + i)[0].firstChild.nodeValue + "\">" + xmlDOC.getElementsByTagName("nodetitle-" + i)[0].firstChild.nodeValue + "</a>";
					vareditors.appendChild(lister);
					i = i + 1;
				}
				rec.appendChild(vareditors);
			} else {
				if(language == "English"){
				rec.innerHTML = "<div class=\"sorry-msg\">Bunseki.info is constantly being upgraded. Currently, we are in the process of uploading downloadable reports for this country. Please check back after sometime. In the meanwhile, you could browse through our reports based on other countries of categories.</div>";
				}
				else {
					rec.innerHTML = "<div class=\"sorry-msg\">現在、ご指定のデータについては企画・制作を行っているところです。申し訳ありませんが、現在ご利用になれません。bunseki.infoは継続的に改善改良を繰り返していますので、しばらく経ってからまたご確認いただければと思います。よろしければ、他の地域における人気コンテンツをご覧ください。</div>";
				}
			}
			
			if (totalcnt != 0) {
				var summary = document.createElement("div");
				summary.className = "summary";
				
				var linker = "products_master/" + country + "/" + language + "/*";
				
				if(language == "English") {
					if (country == "Global") {
						summary.innerHTML = "<a href=\"?q=en/" + linker + "\">Click here</a> to browse the other <strong>" + totalcnt + "</strong> downloadable reports available.";
					} else {
						summary.innerHTML = "<a href=\"?q=en/" + linker + "\">Click here</a> to browse the other <strong>" + totalcnt + "</strong> downloadable reports available for this country.";
					}
				} else {
					summary.innerHTML = "この国(地域に関する)その他<strong>" + totalcnt + "</strong>件のデータを確認するには、<a href=\"?q=ja/products_master/" + country + "/*/*\">こちら</a>をクリックしてください。";
				}
				
				rec.appendChild(summary);
			}
			
			var parent = document.getElementById("main-container");
			var conparent = document.getElementById("country-list");
			if (country == "Japan") {
				parent.style.height  = rec.offsetHeight + 120 + "px";
			} else {
				parent.style.height  = 191 + "px";
			}
		}
	}
}


//utilfunctions
function removeElement(parentEl) {
	var len = parentEl.childNodes.length;
	for (var i=0; i<len; i++) {
		var childNode = parentEl.childNodes[len-i-1];
		parentEl.removeChild(childNode);
	}
}
function replaceText(el, text) {
	if (el != null) {
		clearText(el);
		var newNode = document.createTextNode(text);
		el.appendChild(newNode);
	}	
}
function clearText (el) {
	if (el != null) {
		if (el.childNodes) {
			for (var i=0; i < el.childNodes.length; i++) {
				var childNode = el.childNodes[i];
				el.removeChild(childNode);
			}
		}
	}
}

//DOM FX--loading.gif
function showLoader(fxcontainer,msg) {
	var fxparent = document.createElement("div");
	fxparent.className = "specialfx";
	fxparent.setAttribute("id","fxid");
	var fximg = new Image(16,16);
	fximg.src = "/ajaxfiles/downloads/loading.gif";
	fximg.align = "middle";
	fxparent.appendChild(fximg);
	fxparent.appendChild(document.createTextNode(msg));
	fxcontainer.appendChild(fxparent);
}


function country_category(lang){
	var cntry = document.getElementById('selcountry').value;
	var cat = document.getElementById('selcategory').value;
	var ln = "";
	if(lang == "en"){
		ln = "English";
	}
	else if(lang == "ja"){
		ln = "*";
	}
	var url = "/?q=" + lang + "/products_master/" + cntry + "/" + ln + "/" + cat;
	document.location = url;
}
