/**
 *@require jsregularajax.js
*/
/**
 * @constructor, build the XSLT Ajax instance
 * @param {String} i_id, xslt template name (filename)
 * @param {String} i_section, xslt section name (directory)
 */
function XSLTBuilder(i_id, i_section, i_includeFile){
	this.section = i_section;
	this.xmlId = i_id;
	this.genArgu = null;
	this.includeFile = i_includeFile;
//	this.ajaxXSLTInstance = null;
}
//default XSLT Instance
XSLTBuilder.ajaxXSLTInstance = null;
/**
 * get default XSLT Instance
 * @return Ajax XSLT instance
 */
XSLTBuilder.prototype.getDefaultInstance = function (){
	//if (XSLTBuilder.ajaxXSLTInstance == null){
		
	//}
    return this.setup();
}

/**
 * build the xslt argument
 * @private
 */
XSLTBuilder.prototype.setup = function (){
/*
	if (this.section == "" || this.section == null){
		this.genArgu = "xslId=" + this.xmlId;
	}else{
		this.genArgu = "xslId=" + this.xmlId + "&" + "section=" + this.section;
	}
*/
	var xsltAjaxObject = new Ajax();
   	xsltAjaxObject.action = "/ajaxlib/php/ajaxXSLT.php";
   	xsltAjaxObject.method = "GET";
	if (this.xmlId != "" && this.xmlId != null){
		xsltAjaxObject.setVar("xslId", this.xmlId);
	}
	if (this.section != "" && this.section != null)
		xsltAjaxObject.setVar("section", this.section);
	if (this.includeFile != "" && this.includeFile != null)
		xsltAjaxObject.setVar("includeFile", this.includeFile);


// 	xsltAjaxObject.generateURIArgu(this.genArgu);
	return xsltAjaxObject;
//	xsltAjaxObject.runAJAX();

   	
}


/**
 * global variable to save ajaxXSLT instance
 */
var tmpajaxXSLT;
AjaxXSLT.xsltInstance = null;
/**
 * @constructor, build AjaxXSLT instance
 * @param {Ajax} XML Ajax object
 * @param {Ajax} XSLT Ajax object
 */
function AjaxXSLT(ajaxXMLObject, ajaxXSLTObject){
   this.xmlAjaxObject = ajaxXMLObject;
   this.xsltAjaxObject = ajaxXSLTObject;
   this.xmlDoc = null;
   this.xslDoc = ajaxXSLTObject;
   this.objOutput = null;
   this.debug = false;
   this.uniqueId = null;
   this.async = true;
   
   this.finalize = function(){};
   this.setup();
   this.isIE = false;
}

/**
 * private function to set onCompletion of ajax object
 * @private
 */
AjaxXSLT.prototype.setup = function(){
	tmpajaxXSLT = this;
	this.xmlAjaxObject.onCompletion = this.onXMLLoad;
	this.xsltAjaxObject.onCompletion = this.onXSLLoad;
}

/**
 * function to fire xslt request
 */
AjaxXSLT.prototype.initialize = function() {
   if (AjaxXSLT.xsltInstance == null)
	   this.xsltAjaxObject.runAJAX();
   else
      this.xslDoc = AjaxXSLT.xsltInstance;
   this.xmlAjaxObject.debug = this.debug;
//   alert (this.xmlAjaxObject.uniqueId);
   this.xmlAjaxObject.runAJAX();
//  alert (this.uniqueId);      
}

/**
 * function to handle Ajax response for xml function
 * @private
 */
AjaxXSLT.prototype.onXMLLoad = function(){
   tmpajaxXSLT.xmlDoc=this.response.responseXML;
	if (this.uniqueIdentifier != null){
		var tmpTags = tmpajaxXSLT.xmlDoc.getElementsByTagName(this.uniqueIdentifier)[0];
		var tmpTagValue = tmpTags.childNodes[0].nodeValue;
		tmpajaxXSLT.uniqueId = tmpTagValue;
	}
    tmpajaxXSLT.doXSLT();
}


/**
 * function to handle Ajax response for xslt function 
 * @private
 */
AjaxXSLT.prototype.onXSLLoad = function(){
//   ajaxXSLT.xslDoc=this.response.responseXML;
   AjaxXSLT.xsltInstance = this.response.responseXML;
   tmpajaxXSLT.xslDoc = AjaxXSLT.xsltInstance;
   tmpajaxXSLT.doXSLT();
}
      
/**
 * function to transfrom xml into xhtml
 * @private
 */
AjaxXSLT.prototype.doXSLT = function(){
    if (this.xmlDoc==null || this.xslDoc == null){ 
  	   return false; 
    }
  if (window.ActiveXObject){
	try{
		this.objOutput = this.xmlDoc.transformNode(this.xslDoc);
//alert (this.objOutput);
	}catch (e){
		//alert ("trnsformNodeerror");
		//alert (e.message);
	}
	this.isIE = true;
  }
  else{
    var xsltProcessor = new XSLTProcessor();
    xsltProcessor.importStylesheet(this.xslDoc);
    var fragment =xsltProcessor.transformToFragment(
              		this.xmlDoc,document);
    this.objOutput = fragment;
    this.isIE = false;
  }
  ajaxXSLT = null;
//  alert ("this.uniqueId = " + this.uniqueId);
//  alert ("this.objOutput:\n" + this.objOutput);
  if (this.debug)
		alert ("this.objOutput:\n" + this.objOutput);
  this.finalize();
}
/**
 * getter, get transform xslt object
 * @param {DOMObject} transform xslt object
 */
AjaxXSLT.prototype.getTransformXSLT = function(){
	return this.objOutput;
}
/**
 * helper to function to replace dom object to the xslt object
 * @param {DOMObject} dom object to be replaced
 */
AjaxXSLT.prototype.appendDiv = function(div){
	if (this.isIE){
		div.innerHTML = this.objOutput;
	}else{
		div.innerHTML = "";
        div.appendChild(this.objOutput);
	}
}

