var testoVML="";

var featureid="";
var npoints=0;
var title = "";


/***************************************************************************************
                                        SAX EVENT HANDLER
***************************************************************************************/


xmlHandler = function() {
    /*************************************************************************************
    Function:       xmlHandler

    author:         xwisdom@yahoo.com

    description:
    	constructor for the xmlHandler object

    ************************************************************************************/
    this.m_strError='';
    this.m_treePath=[];
    this.m_xPath=[''] // stores current path info
    this.m_text=['']
    this.m_cdata=['']
    this.m_attr=['']
    this.m_pi=['']
    this.cdata=false

} // end function xmlHandler


xmlHandler.prototype.characters = function(data, start, length) {
    /*************************************************************************************
    Function:       object.characters(String data, Int start, Int length)
					-> data: xml data
					-> start of text/cdata entity
					-> length of text/cdata entity

    author:         xwisdom@yahoo.com

    description:
    	this event is triggered whenever a text/cdata entity is encounter by the sax2 parser

    ************************************************************************************/

    // capture characters from CDATA and Text entities
    var text=data.substr(start, length);
    if (text=='\n' ) return null // get ride of blank text lines
    if (this.m_treePath.length>0){
        if(this.cdata==false){
            if (!this.m_text[this.m_xPath.join('/')]) {
                this.m_text[this.m_xPath.join('/')]='';
            }
            this.m_text[this.m_xPath.join('/')]+=text;
        }
        else {
            if (!this.m_cdata[this.m_xPath.join('/')]) {
                this.m_cdata[this.m_xPath.join('/')]='';
            }
            this.m_cdata[this.m_xPath.join('/')]+=text;
        }
    }

} // end function characters


xmlHandler.prototype.comment = function(data, start, length) {
    /*************************************************************************************
    Function:       object.comment(String data, Int start, Int length)

    author:         xwisdom@yahoo.com

    description:
		triggered whenever a comment <!-- text --> is found. Same as the character event

    ************************************************************************************/

    var comment=data.substr(start, length)

} // end function comment


xmlHandler.prototype.endCDATA = function() {
    /*************************************************************************************
    Function:       object.endCDATA()

    author:         xwisdom@yahoo.com

    description:
    	triggered at the end of cdata entity

    ************************************************************************************/

    // end of CDATA entity
    this.cdata=false

} // end function endCDATA


xmlHandler.prototype.endDocument = function() {
    /*************************************************************************************
    Function:       object.endDocument()

    author:         xwisdom@yahoo.com

    description:
    	end of document parsing - last event triggered by the sax2 parser

    ************************************************************************************/
	testoVML += '</div>';		
	//alert("Geometria parsata, ["+npoints+"] punti trovati.");
} // end function end Document




xmlHandler.prototype.error = function(exception) {
    /*************************************************************************************
    Function:       object.error(String exception)

    author:         xwisdom@yahoo.com

    description:
		triggered whenever an error is encounter by the sax2 parser

    ************************************************************************************/

    this.m_strError+='Error:'+exception.getMessage()+'\n'

} // end function error


xmlHandler.prototype.fatalError = function(exception) {
    /*************************************************************************************
    Function:       object.fatalError(String exception)

    author:         xwisdom@yahoo.com

    description:
		triggered whenever an error is encounter by the sax2 parser

    ************************************************************************************/

    this.m_strError+='fata error:'+exception.getMessage()+'\n'

} // end function fatalError


xmlHandler.prototype.getAttr_Array= function() {
    /*************************************************************************************
    Function:       getAttr_Array

    author:         xwisdom@yahoo.com
    ************************************************************************************/

    return this.m_attr;

}   // end function getAttr_Array


xmlHandler.prototype.getCDATA_Array= function() {
    /*************************************************************************************
    Function:       getCDATA_Array

    author:         xwisdom@yahoo.com
    ************************************************************************************/
    return this.m_cdata;

}  // end function getCDATA_Array


xmlHandler.prototype.getError = function() {
    /*************************************************************************************
    Function:       getError

    author:         xwisdom@yahoo.com
    ************************************************************************************/

    return this.m_strError;

}  // end function getError


xmlHandler.prototype.getPath_Array = function() {
    /*************************************************************************************
    Function:       getError

    author:         xwisdom@yahoo.com
    ************************************************************************************/
	return this.m_treePath;
}  // end function getPath_Array


xmlHandler.prototype.getText_Array = function() {
    /*************************************************************************************
    Function:       getText_Array

    author:         xwisdom@yahoo.com
    ************************************************************************************/
    return this.m_text;

} // getTextArray


xmlHandler.prototype.processingInstruction = function(target, data) {
    /*************************************************************************************
    Function:       object.processingInstruction(String target, String data)
						-> target: is tagname of the pi
						-> data: is the content of the pi

    author:         xwisdom@yahoo.com

    description:
    	capture PI data here

    ************************************************************************************/

} // end function processingInstruction


xmlHandler.prototype.setDocumentLocator = function(locator) {
    /*************************************************************************************
    Function:       object.setDocumentLocator(SAXDriver locator)

    author:         xwisdom@yahoo.com

    description:
		passes an instance of the SAXDriver to the handler

    ************************************************************************************/

    this.m_locator = locator;

}  // end function setDocumentLocator


xmlHandler.prototype.startCDATA = function() {
    /*************************************************************************************
    Function:       object.startCDATA()

    author:         xwisdom@yahoo.com

    description:
    	triggered whenever a cdata entity is encounter by the sax2 parser

    ************************************************************************************/


    // start of CDATA entity
    this.cdata=true

} // end function startCDATA


xmlHandler.prototype.startDocument = function() {
    /*************************************************************************************
    Function:       object.startDocument()

    author:         xwisdom@yahoo.com

    description:
    	start of document - first event triggered by the sax2 parser

    ************************************************************************************/
	testoVML = '<div id="areaSelezionata" STYLE="overflow:hidden">';		
  title = "";

} // end function startDocument


xmlHandler.prototype.startElement = function(name, atts) {
    /*************************************************************************************
    Function:       object.startElement(String tagname,Array content)
					-> tagname: name of tag
					-> content: [["attribute1", "value1"], ["attribute2", "value2"],....,n]

    author:         xwisdom@yahoo.com

    description:
    	First event trigger when a node is encounter by the sax2 parser
    	the name and attribute contents are passed to this event

    ************************************************************************************/

    // Note: the following code is used to store info about the node
    // into arrays for use the xpath layout
    var att_count = atts.getLength();
	if (name=="FEATURE") {
		featureid="todo";
		for (a=0; a<att_count; a++) {
			//alert(atts.getName(a));
			if (atts.getName(a)=="featureid") featureid=atts.getValue(a);
		}
	};

	if (name=="FIELD") {
		// due attributi name/value
		for (a=0; a<att_count; a++) {
			//alert(atts.getName(a));
			title += atts.getValue(a)+ (a>0 ? "" : ":");
		}
		title += "\n";
	};
	if (name=="POLYGON") {
		//alert(title);
		//title = "aaa";
	}
	if (name=="RING") {
		testoVML +='<v:shape style="filter:alpha(opacity=60);position:absolute;width:1000;height:1000" strokecolor="green" strokeweight="3" fillcolor="#FFFFFF" onmouseover="this.fillcolor=\'#ffff00\'" onmouseout="this.fillcolor=\'#aaaaaa\'" title="'+title+'">';
		testoVML +='<v:path v="';
		npoints = 0;
	}
	if (name=="PATH") {
		testoVML +='<v:shape style="filter:alpha(opacity=60);position:absolute;width:1000;height:1000" strokecolor="red" strokeweight="5" onmouseover="this.strokecolor=\'#ffff00\'" onmouseout="this.strokecolor=\'red\'" title="'+title+'">';
		testoVML +='<v:path v="';
		npoints = 0;
	}
	if (name=="HOLE") {
		testoVML +=' x ';
		npoints = 0;
	}
	if (name=="MULTIPOINT") {
		//testoVML +='<v:shape style="filter:alpha(opacity=60);position:absolute;width:1000;height:1000" strokecolor="red" strokeweight="5" onmouseover="this.strokecolor=\'#ffff00\'" onmouseout="this.strokecolor=\'red\'" title="'+title+'">';
		//testoVML +='<v:path v="';
		npoints = -1000;
	}
	if (name=="POINT") {
		var xReale,yReale;
		for (a=0; a<att_count; a++) {
			if (atts.getName(a)=="X" || atts.getName(a)=="x") xReale=atts.getValue(a).replace(",",".");
			if (atts.getName(a)=="Y" || atts.getName(a)=="y") yReale=atts.getValue(a).replace(",",".");
		}
		var coordinateMappa = converti(xReale, yReale);
/*
		coordinateMappa = new Array();
		coordinateMappa[0] = xReale.substring(0, xReale.indexOf("."));
		coordinateMappa[1] = yReale.substring(0, yReale.indexOf("."));
		coordinateMappa[0] = ""+(xReale.substring(0, xReale.indexOf("."))/1000-15);
		coordinateMappa[1] = ""+(yReale.substring(0, yReale.indexOf("."))/1000-49);
		coordinateMappa[0] = coordinateMappa[0].substring(0, coordinateMappa[0].indexOf("."));
		coordinateMappa[1] = coordinateMappa[1].substring(0, coordinateMappa[1].indexOf("."));
*/
		if (npoints == -1000) {
			// e' un singolo punto
			//testoVML += "<v:oval position='0 0' size='100 100' style='visibility: visible' opacity='1.0' fill='true' fillcolor='red' coordsize='1000,1000'  coordorigin='0 0'/>";
			testoVML += "<v:oval "+
							"style='filter:alpha(opacity=60);position:absolute;top:"+(coordinateMappa[1]-8/2)+"px;left:"+(coordinateMappa[0]-8/2)+"px;"+
							"width:8;height:8' fillcolor='red' strokecolor='blue' strokeweight='1pt' title='"+title+"'/>";
			//alert(testoVML); 

/*  chromakey="null"
  stroke="true"
  strokecolor="black"
  strokeweight="0.75pt"
  print="true"
  coordsize="1000,1000"
  coordorigin="0 0"
/>*/			
		
		} else if (npoints == 0) {
			//primo punto
			testoVML += " m ";
		} else if (npoints == 1)  {		
			//secondo punto
			testoVML += " l ";
		} else {
		  // punti successivi
		  testoVML += ", ";
		}
		if (npoints != -1000)  testoVML += " "+coordinateMappa[0] + ", " + coordinateMappa[1];
  	npoints++;
	}

    var cpath;
    this.m_xPath[this.m_xPath.length]=name
    cpath=this.m_xPath.join('/')
    this.m_treePath[this.m_treePath.length]=cpath

    if (att_count) {
        var attr=[]
        for (i=0;i<att_count;i++){
            attr[atts.getName(i)]=atts.getValue(i)
        }
        this.m_attr[this.m_xPath.join('/')]=attr;
    }

} // end function startElement

xmlHandler.prototype.endElement = function(name) {
    /*************************************************************************************
    Function:       object.endElement(String tagname)
					-> tagname: name of tag

    author:         xwisdom@yahoo.com

    description:
    	last event trigger when a node is encounter by the sax2 parser

    ************************************************************************************/

	if (name=="FEATURE") {
		featureid="";
		title="";
	};
	if (name=="POLYGON") {
	}
	if (name=="RING") {
		testoVML +=' x e" />';
		testoVML +='</v:shape>';
	}
	if (name=="PATH") {
		testoVML +=' " />';
		testoVML +='</v:shape>';
	}
	if (name=="MULTIPOINT") {
		//testoVML +=' " />';
		//testoVML +='</v:rect>';
		//alert(testoVML);
	}

    this.m_xPath=this.m_xPath.slice(0,-1)

} // end function endElement


xmlHandler.prototype.warning = function(exception) {
    /*************************************************************************************
    Function:       object.warninng(String exception)

    author:         xwisdom@yahoo.com

    description:
		triggered whenever an error is encounter by the sax2 parser

    ************************************************************************************/

    this.m_strError+='Warning:'+exception.getMessage()+'\n'

} // end function warning
