var timeoutSecs = 300; 

function cRequest (url, callback, startRefresh) {
	this.url = url;
	this.callback = callback;
	this.startRefresh;
	this.req;
	this.send();
	this.tempTimerId;
}

cRequest.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 **************
cRequest.prototype.send = function() {
	if (window.widget) {
		globals.isNetwork = window.widget.sysInfo.network.getIsNetworkAvailable();
	}else {
		globals.isNetwork = true;
	}
	
	if(!globals.isNetwork) {
		//showAccessError();
		return;
	}	
	
	try {	
		var tReq = this.getXMLHttpRequest();
		this.req = tReq; 
		this.req.open('GET', this.url, true);
		var cRequest = this;
		this.req.onreadystatechange = function() { cRequest.onStateChange.call(cRequest); }
		this.req.send(null);
		
		if(globals.timerId == null){
			globals.timerId = setTimeout(function () {cRequest.timeOut(tReq);}, timeoutSecs*1000);
		}
		
	} catch(e) {
		this.tempTimerId = setTimeout(function () {cRequest.shotTimerId();},10);//running timer kill	
		//showNetworkError();
		//mustCallAfterNotify();
		return;	
	}	
		
}

cRequest.prototype.onStateChange = function() {		
	this.onReqChage(this);
}

cRequest.prototype.onReqChage = function(cReq) {
	
	   result = new RequestResult();

		if(cReq.req.readyState <= 0) {
			// response speed >> very High >> abnormal timeOut timer = null >> setTimeout 10   
			this.tempTimerId = setTimeout(function () {cReq.shotTimerId();},10);//running timer kill
			//showNetworkError();
			//mustCallAfterNotify();
			return;	
		}else if(cReq.req.readyState == 4) {
			this.tempTimerId = setTimeout(function () {cReq.shotTimerId();},10);//running timer kill
			if (cReq.req.status == 200) {
				result.status ="ok";
				result.resText = cReq.req.responseText;
				result.xmldom = this.getXmlDOM(cReq.req.responseText);
				result.cReq = cReq;
				this.callback(result);
				
			}else if (cReq.req.status > 200 || cReq.req.status <= 0) {
				//showNetworkError();
				//mustCallAfterNotify();
				return;	
				
			}
		}
}

cRequest.prototype.getXmlDOM = function (xmlval) {
	
	var xmlDoc = null;
	try{
		if(window.DOMParser) {
			var parser = new DOMParser();
			xmlDoc= parser.parseFromString(xmlval,"text/xml");
		} else if(window.ActiveXObject) {
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async = "false";
			xmlDoc.loadXML(xmlval);
		} 
	} catch(e){
	}
	return xmlDoc;
}

cRequest.prototype.shotTimerId = function(){
	if(globals.timerId) {
		clearTimeout(globals.timerId);
		globals.timerId= null;
	}

	if(this.tempTimerId){
		clearTimeout(this.tempTimerId);
		this.tempTimerId= null;
	}	
}

cRequest.prototype.timeOut = function (req) {
	
	this.shotTimerId();//running timer kill
	if(req.readyStatus < 0) {
		//showAccessError();
		return;		
	}else  if(req.readyStatus != 4) {
		req.abort();
		//showNetworkError();
		return;		
	}				

	try	{
		//window.widget.error.notify(2);
	}catch(E){}
		
	//mustCallAfterNotify();

}

//Request Result
function RequestResult () {
	var status;//ok,error
	var resText;
	var xmldom;
	var cReq;
}

function  getTextToXmlDOM(xmlval) {
	
	var xmlDoc = null;
	try{
		if(window.DOMParser) {
			var parser = new DOMParser();
			xmlDoc= parser.parseFromString(xmlval,"text/xml");
		} else if(window.ActiveXObject) {
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async = "false";
			xmlDoc.loadXML(xmlval);
		} 
	} catch(e){
	}
	return xmlDoc;
}