var RemoteHTML = function() {
}

//-------------------------------------------------------------------------------
RemoteHTML.prototype.ERROR_BAD_PAGE_URL = (RemoteHTML.ERROR_BAD_PAGE_URL = -100);
RemoteHTML.prototype.ERROR_BAD_PAGE_URL_MESSAGE = (RemoteHTML.ERROR_BAD_PAGE_URL_MESSAGE = "Bad page URL...");
RemoteHTML.prototype.ERROR_CAN_NOT_PARSE = (RemoteHTML.ERROR_CAN_NOT_PARSE = -101);
RemoteHTML.prototype.ERROR_CAN_NOT_PARSE_MESSAGE = (RemoteHTML.ERROR_CAN_NOT_PARSE_MESSAGE = "Can not parse data...");

//-------------------------------------------------------------------------------
RemoteHTML.prototype.attempt = 0;
//-------------------------------------------------------------------------------
RemoteHTML.prototype.completionStatus = 0;
RemoteHTML.prototype.getCompletionStatus = function () {return this.completionStatus;};
RemoteHTML.prototype.setCompletionStatus = function (completionStatus) {this.completionStatus = completionStatus;};
//-------------------------------------------------------------------------------
RemoteHTML.prototype.debugging = false;
RemoteHTML.prototype.getDebugging = function () {return this.debugging;};
RemoteHTML.prototype.setDebugging = function (debugging) {this.debugging = debugging;};
//-------------------------------------------------------------------------------
RemoteHTML.prototype.doForObject;
RemoteHTML.prototype.doIfComplete;
RemoteHTML.prototype.doIfError;
RemoteHTML.prototype.error;
RemoteHTML.prototype.errorElementList;
//-------------------------------------------------------------------------------
RemoteHTML.prototype.html;
RemoteHTML.prototype.getHTML = function() {return this.html;}; 
RemoteHTML.prototype.setHTML = function(html) {this.html = html; return 1;};
//-------------------------------------------------------------------------------
RemoteHTML.prototype.httpHeaders;
RemoteHTML.prototype.getHTTPHeaders = function() {return this.httpHeaders;}; 
RemoteHTML.prototype.setHTTPHeaders = function(httpHeaders) {this.httpHeaders = httpHeaders; return 1;};
//-------------------------------------------------------------------------------
RemoteHTML.prototype.httpRequest
RemoteHTML.prototype.message;
RemoteHTML.prototype.proxyHeaders; 
//-------------------------------------------------------------------------------
RemoteHTML.prototype.proxyURL; 
RemoteHTML.prototype.getProxyURL = function() {return this.proxyURL;}; 
RemoteHTML.prototype.setProxyURL = function(proxyURL) {this.proxyURL = proxyURL; return 1;};
//-------------------------------------------------------------------------------
RemoteHTML.prototype.proxyPost = ""; 
RemoteHTML.prototype.getProxyPost = function() {return this.proxyPost;}; 
RemoteHTML.prototype.setProxyPost = function(proxyPost) {this.proxyPost = proxyPost; return 1;};
//-------------------------------------------------------------------------------
RemoteHTML.prototype.reallyPageURL;
//-------------------------------------------------------------------------------
RemoteHTML.prototype.remoteHTMLLog;
RemoteHTML.prototype.getLog = function () {return this.remoteHTMLLog;};
RemoteHTML.prototype.setLog = function (remoteHTMLLog) {this.remoteHTMLLog = remoteHTMLLog;};
//-------------------------------------------------------------------------------
RemoteHTML.prototype.responseXML;
RemoteHTML.prototype.server;
RemoteHTML.prototype.status;
RemoteHTML.prototype.statusElement;
//-------------------------------------------------------------------------------
RemoteHTML.prototype.pageURL;
RemoteHTML.prototype.getPageURL = function() {return this.pageURL;}; 
RemoteHTML.prototype.setPageURL = function(pageURL) {this.pageURL = pageURL; return 1;};

//-------------------------------------------------------------------------------
RemoteHTML.prototype.init = function() {
 if(this.debugging && this.getLog()) {this.getLog().println("RemoteHTML.init is running...");};
 var url = this.getProxyURL() + escape(this.pageURL) + this.getProxyPost();
 if(this.debugging && this.getLog()) {this.remoteHTMLLog.println("RemoteHTML.init: "+"url="+url);};
 
 this.server = new Server(); 
 this.server.serverLog = this.remoteHTMLLog;
 this.server.makeRequest("GET", url, "", this.responseAnalyserFromEvent, this);
}

//-------------------------------------------------------------------------------
RemoteHTML.prototype.responseAnalyserFromEvent = function(httpRequest, obj) {obj.responseAnalyser(httpRequest);}
RemoteHTML.prototype.responseAnalyser = function(httpRequest) {
 if(this.debugging && this.getLog()) {this.getLog().println("RemoteHTML.responseAnalyser is running...");};
 this.httpRequest = httpRequest;
 if(this.debugging && this.getLog()) {this.getLog().println("RemoteHTML.responseAnalyser: "+"httpRequest.readyState="+httpRequest.readyState);};
 if (httpRequest.readyState == 4) {
  if (httpRequest.status == 200) {// || httpRequest.status == 301
   if(this.debugging && this.getLog()) {this.getLog().println("RemoteHTML.responseAnalyser: "+"httpRequest.responseXML: ok");};  
   
   try {
    if(httpRequest.getAllResponseHeaders && httpRequest.getAllResponseHeaders()) {this.proxyHeaders = httpRequest.getAllResponseHeaders();};     
   } catch (e) {}
 
   this.setFromResponseXML(httpRequest.responseXML);
  } else if ( (httpRequest.status == 301 || httpRequest.status == 0)  && this.attempt == 0) {
   if(this.debugging && this.getLog()) {this.getLog().println("RemoteHTML.responseAnalyser: "+"Oops... 301 Moved Permanently");};
   this.attempt++;
   this.pageURL += "/";
   this.init();
  } else {
   if(this.debugging && this.getLog()) {this.getLog().println("RemoteHTML.responseAnalyser: "+"There was a problem with the request. httpRequest.status="+httpRequest.status);};

   try {
    if(this.debugging && this.getLog()) {this.getLog().println("RemoteHTML.responseAnalyser: "+"Get Proxy headers");};
    if(httpRequest.getAllResponseHeaders && httpRequest.getAllResponseHeaders()) {this.proxyHeaders = httpRequest.getAllResponseHeaders();};
    if(this.debugging && this.getLog()) {this.getLog().println("RemoteHTML.responseAnalyser: "+"this.proxyHeaders = "+this.proxyHeaders);};    
   } catch (e) {}  
   
   this.completionStatus = -10;
   this.doIfError(this.doForObject, this.completionStatus);
  }
 } else {
 };
}


//-------------------------------------------------------------------------------
RemoteHTML.prototype.setFromResponseXML = function(responseXML) { 
 if(this.debugging && this.getLog()) {this.getLog().println("RemoteHTML.setFromResponseXML is running...");};
 
 this.responseXML = responseXML;
 if(this.debugging && this.getLog()) {this.getLog().println("RemoteHTML.setFromResponseXML: "+"this.responseXML="+this.responseXML);};
 
 var preStatusElement = this.responseXML.getElementsByTagName("status");
 if(this.debugging && this.getLog()) {this.getLog().println("RemoteHTML.setFromResponseXML: "+"preStatusElement.length="+preStatusElement.length+";");};
 if(preStatusElement.length < 1){
  this.completionStatus = this.ERROR_CAN_NOT_PARSE;
  if(this.debugging && this.getLog()) {this.getLog().println("RemoteHTML.setFromResponseXML: "+"Go to do if error...");};
  this.doIfError(this.doForObject, this.completionStatus);
  return this.completionStatus;
 }
 this.statusElement = preStatusElement.item(0);
 var preStatus = this.statusElement.firstChild;
 this.status = preStatus.data;
 var reallyPageURLElement = this.responseXML.getElementsByTagName("url");
 this.reallyPageURL = unescape(reallyPageURLElement.item(0).firstChild.data);
 if(this.debugging && this.getLog()) {this.getLog().println("RemoteHTML.setFromResponseXML: "+"status="+this.status+"; realyURL="+this.reallyPageURL+";");};

 this.errorElementList = this.responseXML.getElementsByTagName("error");
 if(this.errorElementList && this.errorElementList.item(0) && this.errorElementList.item(0).firstChild && this.errorElementList.item(0).firstChild.data) {
  this.error = this.errorElementList.item(0).firstChild.data;
  this.message = this.responseXML.getElementsByTagName("message").item(0).firstChild.data;
  if(this.debugging && this.getLog()) {this.getLog().println("RemoteHTML.setFromResponseXML: "+"error="+this.error+"; message="+this.message+";");};
  this.completionStatus = this.ERROR_BAD_PAGE_URL;
  if(this.debugging && this.getLog()) {this.getLog().println("RemoteHTML.setFromResponseXML: "+"RemoteHTML: "+"Go to do if error...");};
  this.doIfError(this.doForObject, this.completionStatus);
  return this.completionStatus;
 } else {
  var headersElement = this.responseXML.getElementsByTagName("headers").item(0);
  var headersElementNodeList = headersElement.childNodes;
  var headers = "";
  for(var i=0; i<headersElementNodeList.length; i++) {
   var iItem = headersElementNodeList.item(i);
   if(iItem.nodeType == 4) {
    headers = iItem.data;
   }
  }
  headers = headers.replace(/&CDATAEND;/g, "]]>");
  this.httpHeaders = new HTTPHeaders();
  this.httpHeaders.httpHeadersLog = this.remoteHTMLLog;
  this.httpHeaders.setHeaders(headers);
 
  if(this.debugging && this.getLog()) {
   this.createdElementList = this.responseXML.getElementsByTagName("created");
   if(this.createdElementList && this.createdElementList.item(0) && this.createdElementList.item(0).firstChild && this.createdElementList.item(0).firstChild.data) {
    var created = this.createdElementList.item(0).firstChild.data;
    if(this.debugging && this.getLog()) {this.getLog().println("RemoteHTML.setFromResponseXML: "+"created="+created);};
   }
   this.sourseElementList = this.responseXML.getElementsByTagName("sourse");
   if(this.sourseElementList && this.sourseElementList.item(0) && this.sourseElementList.item(0).firstChild && this.sourseElementList.item(0).firstChild.data) {
    var sourse = this.sourseElementList.item(0).firstChild.data;
    if(this.debugging && this.getLog()) {this.getLog().println("RemoteHTML.setFromResponseXML: "+"sourse="+sourse);};
   }
  }
 
  var contentElement = this.responseXML.getElementsByTagName("content").item(0);
  var contentElementNodeList = contentElement.childNodes;
  var html = "";
  for(var i=0; i<contentElementNodeList.length; i++) {
   var iItem = contentElementNodeList.item(i);
   if(iItem.nodeType == 4) {
    html = iItem.data;
   }
  }  
  html = html.replace(/&CDATAEND;/g, "]]>");
  this.html = new HTML();
  this.html.htmlLog = this.remoteHTMLLog;
  this.html.setHTML(html);
  this.completionStatus = 1;
  this.doIfComplete(this.doForObject, this.completionStatus);
 };
}

RemoteHTML.prototype.printDocument = function(xmlElement) {
 if(this.debugging && this.getLog()) {this.getLog().println("RemoteHTML.printDocument is running...");};
 if(xmlElement.nodeType == 3) {if(this.debugging && this.getLog()) {this.getLog().println("RemoteHTML.printDocument: "+xmlElement.nodeValue);};};
 var firstChild = xmlDocument.getFirstChild;
}