/// <summary>
/// xmlLoader
/// </summary>
/// <author>T.Nagai</author>
/// <company>informatix</company>

//通信時エラーメッセージを派手に表示するか、上品にしまっておくかの設定。 
//debugやdevelopモード時は常に表示
var isShowHttpErr = true;  


//XMLHTTP通信用
//postParam付きならPost　無しならGet
function xmlLoader(url,cbo,postParam,cType){	
	var objXmlHttp;
	var objCallback = cbo;
	var filename = url;
	var param = postParam;
	var contentType = "application/x-www-form-urlencoded";
	if(cType != undefined && cType != null){
		contentType = cType;
	}

//	var myDebug = null;
//	try{
//		myDebug = log;
//	}catch(e){
//		//IFrame用
//		myDebug = parent.log;
//	}

	this.getObjXmlHttp = function(){
		try{
			return new ActiveXObject("Microsoft.XMLHTTP");
		}catch(e){
			try{
				return new XMLHttpRequest();
			}catch(e){
				alert("XmlHttpRequestを利用できないブラウザです");
//				new AttentionMessage().show("XmlHttpRequestを利用できないブラウザです");
//				new AttentionMessage().show(resMan.get("util_XH_XmlHttpCreateError"));  res:
				return ;
			}
		}
	}

	this.start =  function(){
		objXmlHttp = this.getObjXmlHttp();
		objXmlHttp.onreadystatechange = this.check;
		if(param != undefined && param != null){
//myDebug.write("XmlHttp#start():post",filename+"?"+param);
			objXmlHttp.open("POST",filename,true);
			//objXmlHttp.open("POST",filename,false);//同期
			objXmlHttp.setRequestHeader("Content-Type" , contentType);
			objXmlHttp.send(param);
		}else{
//myDebug.write("XmlHttp#start():get",filename);
			objXmlHttp.open("GET",filename,true);
			//objXmlHttp.open("GET",filename,false);//同期
			objXmlHttp.send(null);
		}
		
		//sessionKeeper待機経過時間をリセットする
		if(typeof(intervalTimerManager) == "object"){
		    intervalTimerManager.resetIntervalFunctionTimer("sessionKeeper");
		}
		
	}
	
	this.check = function(){
//		try{
			var http = objXmlHttp;
			if(http.readyState == 4){
				if(http.status == 200){
//					try{
						if(objCallback != null){	//IE5.5　2006/4/13 kuro

							var resConType = objXmlHttp.getResponseHeader("Content-Type");

							if(resConType.match(/html/)){
							    
								//myDebug.write("XmlHttp#check() contenttype is html !!!!!",objXmlHttp.responseText);
								
//								if(DEBUG || DEVELOP){
//								    if(typeof(IFrameHtmlTextDialog) == "function"){
//								        new IFrameHtmlTextDialog(objXmlHttp.responseText,500,400,"middle","center",0,0,true,false,false,true);
//								    }
//								}
								
    							objCallback = null;
								cbo = null;
								return;
							}

                            
							if(objCallback.callback2){
								objCallback.callback2(objXmlHttp.responseText);	
							}

							if(objCallback.callback){
								objCallback.callback(objXmlHttp.responseXML);
							}
						}
//					}catch(e){      
//						//myDebug.write("XmlHttp#check() "+url+"?"+postParam,"status=200 catch"+e.message+"\n"+objXmlHttp.responseText);

//						var resConType = objXmlHttp.getResponseHeader("Content-Type");
//						//new AttentionMessage().show(objXmlHttp.responseText);
//						if(resConType.match(/html/)){
//						    if(isShowHttpErr || DEBUG || DEVELOP){
//							    if(typeof(IFrameHtmlTextDialog) == "function"){
//							        new IFrameHtmlTextDialog(objXmlHttp.responseText,500,400,"middle","center",0,0,true,false,false,true);
//							    }
//							}
//						}else{
//							//new AttentionMessage().show(objXmlHttp.responseText);
//							//myDebug.write("XmlHttp#check()_status=200 catch_restext",objXmlHttp.responseText);
//						}

//						if(objCallback != null){
//							objCallback.exceptionThrown(url+"?"+postParam+" message:"+e.message);
//						}

//					}

					objCallback = null;
					cbo = null;
					
				} else {
//					try{
						var resConType = objXmlHttp.getResponseHeader("Content-Type");
						if(resConType.match(/html/)){
							//myDebug.write("XmlHttp#check()_status=Err",objXmlHttp.responseText);
//							if(isShowHttpErr || DEBUG || DEVELOP){
//							    if(typeof(IFrameHtmlTextDialog) == "function"){
//							        new IFrameHtmlTextDialog(objXmlHttp.responseText,500,400,"middle","center",0,0,true,false,false,true);
//							    }
//							}
							return;
						}

						var objXmlDom = objXmlHttp.responseXML;
						var timestamp = http.getResponseHeader("Date");
						//var msg = resMan.get("util_xmlhttpError")+"<BR/>Date:"+timestamp+ "<BR/>Name:"+filename+ "<BR/>Status:" + http.status;
						var msg = "XMLHTTP通信中またはサーバでエラーが発生しました"+"<BR/>Date:"+timestamp+ "<BR/>Name:"+filename+ "<BR/>Status:" + http.status;
                        
						if(objXmlDom){
							var objInElement = objXmlDom.documentElement;
							if (objInElement != null){
							    if (objInElement.hasChildNodes()) {
								    var objInChildNodes = objInElement.childNodes;
								    for (var i=0 ; i<objInChildNodes.length ; i++) {
									    var objCurrentNode = objInChildNodes.item(i);
									    if(objCurrentNode.nodeName == "Error"){
										    var nodeAttributeMessage = getNodeAttribute(objCurrentNode,"message","");
										    msg = msg + "<BR/>Message:" +nodeAttributeMessage;
									    }
								    }
							    }
							}
						}
						if(objCallback != null){
							objCallback.exceptionThrown(msg,objXmlDom);
						}

						objCallback = null;
						cbo = null;

//					}catch(e){
//						myDebug.write("XmlHttp#check()"+url+"?"+postParam,"status=Err catch"+e.message+"\n"+objXmlHttp.responseText);

//						var resConType = objXmlHttp.getResponseHeader("Content-Type");
//						if(resConType.match(/html/)){
//						    if(isShowHttpErr || DEBUG || DEVELOP){
//							    if(typeof(IFrameHtmlTextDialog) == "function"){
//							        new IFrameHtmlTextDialog(objXmlHttp.responseText,500,400,"middle","center",0,0,true,false,false,true);
//							    }
//							}
//						}else{
//							//new AttentionMessage().show(objXmlHttp.responseText);
//							myDebug.write("XmlHttp#check()_status=Err catch_restext",objXmlHttp.responseText);
//						}

//					}
				}
			} 
//		}catch(e){
//			if(e.number == -2147024891){
//				new AttentionMessage().show(resMan.get("util_Error"));
//			}else{
//			    if(DEBUG || DEVELOP){
//				    new AttentionMessage().show("xmlHttp#check\n"+filename+"\n"+e.message+"\n"+e.number);
//				}
//			}
//		}
	}
}


//jsLoadingFinished("js/common/util/XmlHttp.js","XmlHttp");

