var xmlhttp = initXmlServletRequest();
var ajaxHost;

function initXmlServletRequest(){
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	}else {
		try {
            return new ActiveXObject("MSXML2.XMLHTTP.3.0");
        }catch(ex) {
            return null;
        }
	}
}

function getLegalText(host, shopId){
	ajaxHost = host+"/widget/widgetlt";
	xmlhttp.open("POST",ajaxHost,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlhttp.onreadystatechange = processAjaxResponse;
	var postQuery = "shopId="+shopId;
	xmlhttp.send(postQuery);
}

function getLegalTextByShopAndType(host, shopId, type){
	ajaxHost = host+"/widget/widgetlt";
	xmlhttp.open("POST",ajaxHost,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlhttp.onreadystatechange = processAjaxResponse_ltByType;
	var postQuery = "shopId="+shopId+"&t_t="+type;
	xmlhttp.send(postQuery);
}

function getLegalTextById(host, legalTextId){
	ajaxHost = host+"/widget/widgetlt";
	xmlhttp.open("POST",ajaxHost,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlhttp.onreadystatechange = processAjaxResponse_ltById;
	var postQuery = "t_l="+legalTextId;
	xmlhttp.send(postQuery);
}

function processAjaxResponse_ltByType(){
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200){
			var mainNode = xmlhttp.responseXML.getElementsByTagName("message")[0];
			for (i = 0; i < mainNode.childNodes.length; i++) {
				if (mainNode.childNodes[i].nodeType != 1)
					continue;
				if (mainNode.childNodes[i].nodeName == "lt_text") {
					var text = mainNode.childNodes[i].firstChild.nodeValue;
					var decodedText = decodeURIComponent(text);
					decodedText = decodedText.replace(/\+/g,' ');					
					$('ltText').innerHTML = decodedText;
				}
			}
		}
	}
}

function processAjaxResponse(){
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200){
			var mainNode = xmlhttp.responseXML.getElementsByTagName("message")[0];
			for (i = 0; i < mainNode.childNodes.length; i++) {
				if (mainNode.childNodes[i].nodeType != 1)
					continue;
				if (mainNode.childNodes[i].nodeName == "lt_id") {
					var id = mainNode.childNodes[i].firstChild.nodeValue;
					$('dpaId').value = id;
				}
				if (mainNode.childNodes[i].nodeName == "lt_text") {
					var text = mainNode.childNodes[i].firstChild.nodeValue;
					var decodedText = decodeURIComponent(text);
					decodedText = decodedText.replace(/\+/g,' ');					
					$('dpaText').innerHTML = decodedText;
				}
			}
		}
	}
}

function processAjaxResponse_ltById(){
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200){
			var mainNode = xmlhttp.responseXML.getElementsByTagName("message")[0];
			for (i = 0; i < mainNode.childNodes.length; i++) {
				if (mainNode.childNodes[i].nodeType != 1)
					continue;
				if (mainNode.childNodes[i].nodeName == "lt_text") {
					var text = mainNode.childNodes[i].firstChild.nodeValue;
					var decodedText = decodeURIComponent(text);
					decodedText = decodedText.replace(/\+/g,' ');					
					$('ltText').innerHTML = decodedText;
				}
			}
		}
	}
}

function openLTWindow(url){
	var width=800;
	var height=700;
	var left = Math.floor( (screen.width - width) / 2);
	var top = Math.floor( (screen.height - height) / 2);
	var winParms = "top=" + top + ",left=" + left + ",height="+height+",width="+width+",menubar=no,resizable=yes,toolbar=no,status=no,scrollbars=yes";
	var win = window.open(url, "ltWin", winParms);
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}
