/* Copyright (c) 2009 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) {					
				xml = getXmlDom(this.req);
				this.callback(xml);
				ajaxRun = false;
			} else {
				if(this.sendCnt == 1) this.send();
				else	alert("오류가 발생했습니다.");
			}
		}
	}
}

function getXmlDom(xmlval)
{
	var xmlDoc;
	
	if (window.DOMParser)
	{	
		var parser = new DOMParser();
		xmlDoc = parser.parseFromString(xmlval.responseText, "text/xml");
		return xmlDoc;
	}
	else if ( window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = false;
		xmlDoc.loadXML(xmlval.responseText);
		return xmlDoc;
	}		
	else
	{		
		alert("XML 문자열로부터 XML DOM을 만들 수 없습니다");
		return null;
		
	}
	
}

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);

	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);

	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);

	for(var o=obj.length-1; o>=0; o--)
	{
		obj.options[o] = null;
	} 
}
		
function SelectBoxChildNodesDel2(elementId)
{
	var obj = document.getElementById(elementId);

	for(var i=obj.length-1; i>=1; i--)
	{
		obj.options[i] = null;
	} 
}		

function ChildNodesDel(elementId)
{
	var obj = document.getElementById(elementId);

	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();
	}
}
