﻿/*file reader via XMLHttpRequest object

	version 6  1/9/10 Added setRequestHeader("If-Modified-Since"... to stops caching

	version 5  10/4/08 Added the "numHeadingRows" property to allow skipping of any headers .....LATER
	version 4  9/5/08  Added ability to give an array of file URLs to getFileText, plus added the ParseCSVtext function
	version 3  4/21/08 added the semicolon at the end of getFileText (why it worked before ai don't know!)
	version 2	4/2/07 made overrideMimeType optional (line 37 and 74
	version 1  3/10/07 
*/ 

function parseCSVtext ( textToParse, fieldDelimiter  ,skipFirstLine){
	// text to parse assumed to be URL, Label, selected
		//var recordArray = new Array (   );
		var recordArray=textToParse.split ( "\n" );//lines are the record delimiters in csv
		
		
		if(skipFirstLine){recordArray.shift();}//remove first line
       
		number_of_records = recordArray.length;
		
      	for  ( var i=0; i<number_of_records;i++  ){

					recordArray[ i ]=recordArray[ i ].split ( fieldDelimiter );					

		 		}
		 return recordArray;
	}
	
	

function fileReader (   ) {
	this.async = true;
	//this.numHeadingRows = 1;//
	
	this.initialize = initialize;
	function initialize(){
		this.FileUrl="";//must be a text file
		this.fileContent="";	
		this.done = false;
		};
	
	this.initialize();

	this.getFileText = getFileText;

	function getFileText(aFileUrl){
		this.async=false;
		switch (typeof(aFileUrl))
			{
			case "string":
				this.loadTextFile(aFileUrl);
				return (this.fileContent);
			case "object":
				try{
					this.loadTextFile(aFileUrl[0]);
					var theText = this.fileContent;
					for (var i=1; i<aFileUrl.length;  i++)
						{
						this.loadTextFile(aFileUrl[i]);
						var thisText = this.fileContent;
						//remove forst line on all following
						//for (i=0;i<this.numHeadingRows;i++){
							thisText = (thisText.slice(1 + thisText.indexOf("\n")));
						//}
						theText = theText + thisText;//split off the first line
						}
				return (theText);
				}
				catch(err){
					//alert("Error: " + err);
					}
				default:
					return;
			}
	};

	this.start = start;
   		function start(){
    	this.loadTextFile ( this.FileUrl  );
    	};


	this.loadTextFile=loadTextFile;
	function loadTextFile ( theURL  ) {
		this.done = false;
		var _this = this;
		if  ( window.XMLHttpRequest  ) { // code for Mozilla, Safari, etc 
	      this.xmlhttpObj=new XMLHttpRequest (   );
	      try {this.xmlhttpObj.overrideMimeType('text/xml');} catch (er){}
	      try{
	      this.xmlhttpObj.onreadystatechange=function (   ) {_this.postFileReady (   )};
	      this.xmlhttpObj.open ( "GET", theURL, this.async  );
	      this.xmlhttpObj.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");//stops caching
	      this.xmlhttpObj.send ( null  );
	      this.postFileReady (   );// covers browsers that don't provide an onreadystatechange event for synchronous requests. Should execute harmlessly for async, since readystate should be valid by now
	      }
	      catch(er){}
		} 
	    else if  ( window.ActiveXObject  ) { 
	  		this.xmlhttpObj=false;
		 	try {
		 		this.xmlhttpObj = new ActiveXObject( "Msxml2.XMLHTTP");
		 	} catch (er) {
		 		try {
		    		this.xmlhttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		 		} catch (er) {
		   			this.xmlhttpObj = false;
		 			}
				}
			if (!this.xmlhttpObj && typeof XMLHttpRequest!='undefined') {
				try {
					this.xmlhttpObj = new XMLHttpRequest();
				} catch (er) {
					this.xmlhttpObj=false;
				}
			}
			if (!this.xmlhttpObj && window.createRequest) {
				try {
					this.xmlhttpObj = window.createRequest();
				} catch (er) {
					this.xmlhttpObj=false;
				}
			}
			
	
	      if  ( this.xmlhttpObj  ) {
	      	try{this.xmlhttpObj.overrideMimeType('text/xml');}catch (er){}
	       	try{this.xmlhttpObj.onreadystatechange=function (   ) {_this.postFileReady (   );};
	       	this.xmlhttpObj.open ( 'GET', theURL, this.async  );
	       	this.xmlhttpObj.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");//stops caching
		    this.xmlhttpObj.send (   );
		    this.postFileReady (   );}
		    catch (er){}
		   }
	   }
	  	  

	};

	this.postFileReady = postFileReady;
	function postFileReady (   ) {
	   if  ( this.xmlhttpObj.readyState==4  ) { 
	      if  ( this.xmlhttpObj.status==200  ) { 
	         this.fileContent = this.xmlhttpObj.responseText;
	         this.done = true;
	      }
	  }
	};

}