/* Copyright (c) 2011 Smileweb co., Ltd.  All rights reserved.  www.smileweb.co.kr */

var ajaxRun = false;
var ajax = {};
ajax.xhr = {};

ajax.xhr.Request = function(url, params, callback, method) {
	this.url = url;
	this.params = params;
	this.callback = callback;
	this.method = method;
	this.sendCnt=0;
	this.send();
}


ajax.xhr.Request.prototype = {
	getXMLHttpRequest: function() {
		if (window.ActiveXObject) {
			try {
				return new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					return  new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e1) { return null; }
			}
		} else if (window.XMLHttpRequest) {
			return new XMLHttpRequest();
		} else {
			return null;
		}
	},
	send: function() {
		ajaxRun = true;
		this.sendCnt++;
		this.req = this.getXMLHttpRequest();
		if(this.req == null){
			alert("이 브라우저는 AJAX를 지원하지 않습니다.");
			return;
		}
		var httpMethod = this.method ? this.method : 'GET';
		if (httpMethod != 'GET' && httpMethod != 'POST') {
			httpMethod = 'GET';
		}
		var httpParams = (this.params == null || this.params == '') ?
		                 null : this.params;
		var httpUrl = this.url;
		if (httpMethod == 'GET' && httpParams != null) {
			httpUrl = httpUrl + "?" + httpParams;
		}

		this.req.open(httpMethod, httpUrl, true);
		this.req.setRequestHeader(
			'Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
		this.req.setRequestHeader("Cache-Control","no-cache, must-revalidate");
		this.req.setRequestHeader("Pargma","no-cache");
		var request = this;
		this.req.onreadystatechange = function() {
			request.onStateChange.call(request);
		}

		this.req.send(httpMethod == 'POST' ? this.params : null);
	},
	onStateChange: function() {
		var xml;
			if (this.req.readyState == 4) {
			if (this.req.status == 200) {
				this.callback(this.getXmlDom());
				ajaxRun = false;
			} else {
				if(this.sendCnt == 1) this.send();
				else	alert("오류가 발생했습니다.");
			}
		}
	},
	getXmlDom : function(){
		var xmlDoc = null;
		if (window.DOMParser) {
			var parser = new DOMParser();
			xmlDoc = parser.parseFromString(this.req.responseText, "text/xml");
		} else if (window.ActiveXObject) {
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async = false;
			xmlDoc.loadXML(this.req.responseText);
		} else {
			alert("XML 문자열로부터 XML DOM을 만들 수 없습니다");
			return null;
		}
		if(isvalidate(xmlDoc)) return xmlDoc;
		return '';
	}
}

function isvalidate(xmlDoc)
{
	var nodes = xmlDoc.getElementsByTagName ( 'sfail' );
	for (i=0; i<nodes.length; i++)
	{
		try {
			alert('세션이 종료 되었거나 정상적인 접근이 아닙니다.\n문제가 지속될시 관리자에게 문의하세요.\n페이지를 새로고침 합니다.');
			document.location.reload();
			return false;
		} catch(e) {
			return true;
		}
	}
	return true;
}

function getFirstChild (node)
{
	var fChild = node.firstChild;
		while (fChild.nodeType != 1)
			fChild = fChild.nextSibling;

	return fChild;
}

function getNodeValue (nodes)
{
	var nodeValue = "";
	for (i=0; i<nodes.length; i++)
	{
		try {
			nodeValue = nodes[i].childNodes[0].nodeValue;
		} catch(e) {
			nodeValue = "찾을수 없습니다.";
		}
	}

	return nodeValue;
}

function getNodeAt(nodes, aname)
{
	var nodeValue = "";
	for (i=0; i < nodes.length; i++)
	{
		for ( j = 0; j < nodes[i].attributes.length ; j++ )
		{
			nodeValue = nodes[i].attributes[j];
			if (nodeValue.name == aname) break;
		}
	}

	return nodeValue.value;
}

function MakeSelectBox(elementId, loaddata)
{
	var obj = document.getElementById(elementId);

	SelectBoxChildNodesDel(elementId);

	for(var i =0; i < obj.length; i++)
	{
		var option = document.createElement("option");
		var text = document.createTextNode(loaddata[i].catename);

		option.appendChild(text);
		option.setAttribute("value",loaddata[i].cate);
		obj.appendChild(option);
	}
}

function MakeSelectBox2(elementId, arrayText, arrayValue) {
	if (arrayText.length != arrayValue.length) {
		alert("셀렉트박스를 생성할수 없습니다.");
		return;
	}

	var obj = document.getElementById(elementId);
	if (obj == null) return;
	SelectBoxChildNodesDel(elementId);

	for(var i =0; i < arrayText.length; i++)
	{
		var option = document.createElement("option");
		var text = document.createTextNode(arrayText[i]);
		option.appendChild(text);
		option.setAttribute("value",arrayValue[i]);
		obj.appendChild(option);
	}
}

function MakeSelectBox3(elementId, arrayText, arrayValue, value) {
	if (arrayText.length != arrayValue.length) {
		alert("셀렉트박스를 생성할수 없습니다.");
		return;
	}

	var obj = document.getElementById(elementId);
	if (obj == null) return;
	SelectBoxChildNodesDel(elementId);

	for (var i = 0; i < arrayText.length; i++) {
		var option = document.createElement("option");
		var text = document.createTextNode(arrayText[i]);
		option.appendChild(text);
		option.setAttribute("value",arrayValue[i]);
		if (arrayValue[i] == value) option.setAttribute("selected", true);
		obj.appendChild(option);
	}
}

function SelectBoxChildNodesDel(elementId) {
	var obj = document.getElementById(elementId);
	if (obj == null) return;
	for (var o = obj.length - 1; o >= 0; o--) {
		obj.options[o] = null;
	}
}

function SelectBoxChildNodesDel2(elementId) {
	var obj = document.getElementById(elementId);
	if (obj == null) return;
	for (var i = obj.length - 1; i >= 1; i--) {
		obj.options[i] = null;
	}
}

function ChildNodesDel(elementId) {
	var obj = document.getElementById(elementId);
	if (obj == null) return;
	for(var i = obj.childNodes.length - 1; i >= 0; i--) {
		obj.removeChild(obj.childNodes.item(i));
	}
}

function MakeParamStr(theForm)
{
	var param = "";

	var radios = new Array();
	var selects = new Array();

	for(var i=0; i < theForm.length; i++) {

		obj = theForm[i];

		switch(obj.type) {

			case "text":
			case "textarea":
			case "hidden":
			case "password":
				param += encodeURIComponent(obj.name) + "=" + encodeURIComponent(obj.value) + "&";
				break;

			case "checkbox":
				param += encodeURIComponent(obj.name) + "=";
				if(obj.checked) param += encodeURIComponent(obj.value);
				param += "&";
				break;

			case "radio":

				if(obj.checked)
				{
					param += encodeURIComponent(obj.name) + "=" + encodeURIComponent(obj.value) + "&";
					radios[obj.name] = true;
				}
				else if(!radios[obj.name]) { radios[obj.name] = false; }
				break;

			case "select-one":
				param += encodeURIComponent(obj.name) + "=";

				for(var x=0; x < obj.options.length; x++)
				{
					if(obj.options[x].selected)  param += obj.options[x].value;
				}
				param += "&";
				break;

			case "select-multiple":
				break;

			case "file":
				break;
		}
	}

	var objname;
	for(objname in radios)
	{
		if(!radios[objname])  param += encodeURIComponent(objname) + "=" + "&";
	}

	return param;
}

function sleep(msecs)
{
	var start = new Date().getTime();
	var cur = start

	while(cur - start < msecs)
	{
		cur = new Date().getTime();
	}
}
