<%
//-------------------------------------
// upload.inc
// server-side function declarations to
// handle uploading of data
//
// Alex Szalay 2003-03-10
//-------------------------------------


function Form() {
	this.type  = "";
	this.value = "";
	this.names = "";
}

function getUploadFormat(f) {
	//----------------------------------
	// Figure out what is the format of the file
	//----------------------------------
	
	var s = body[0];
	f.value = "";
	f.type  = "";
	
	// check for Gator format
		if ( s.match(/^\\\ \w+/) ) {
			// need to scroll forward to the beginning of data
			while ( body[0].match(/^\\/) ) body.splice(0,1);
			if ( body[0].match(/^\s*\|/) && f.value == "" ) {
				f.value = String(body[0]).replace(/\|/g," ");
				body.splice(0,2);
			}
			f.type = "G";
			return "G";		
		}

	// is it a comma/whitespace?
		var c = s.split(reSplit);
		var n = c.length;
		if (n<2 || n>3) 
			return Error("Too many items in first line...");
	
	// we have 2 or 3 values, are they numbers or text
		if ( c[n-1].match(reSignedFloat) !=null ) {
			f.value = (n==2)? "ra dec": "name ra  dec";
			f.type  = "N"+n;
			return "N"+n;		
		}
		if ( c[n-2].match(/^ra$/i)!=null 
			&& c[n-1].match(/^dec$/i)!=null) {
			for (i in c)
				f.value += " "+c[i];
			body.splice(0,1);
			f.type = "H"+n;
			return "H"+n;
		}
	return Error("Error in header line \" "+s+"\"");;
}


function Error(msg) {
	Response.Write("<h2>Error: "+msg+"</h2>\n");
	return null;
}

//------------------------------------

//  define a few useful regular expression globals for the parsing
	var reSignedFloat = /^((((\+|-)?\d+(\.\d*)?)|((\+|-)?(\d*\.)?\d+))([eE](\+|-){1}\d+)?)$/
	var reSplit = /(\,|\s+)/

//======================= MAIN CODE ======================

	if (!debug) {
		var u		= new Form();
		var body	= paste.split("\n");
		getUploadFormat(u);
		
		// need one more thing -- scan through body
		// and eliminate blank lines
		
		var names	= u.value.split(reSplit);
		var nimages	= body.length-1;
		var npages	= Math.floor(nimages/(nrows*ncols)-0.01);
		if (nimages > npages*nrows*ncols+0.25) npages += 1;
	}
	
	if (debug) {
		Response.Write("<pre>\n");
		Response.Write("ppd:"+ppd+"\n");
		Response.Write("opt:"+opt+"\n");
		Response.Write("paste:\n");
		Response.Write(paste+"\n");
		Response.Write("</pre>\n");	
	}


//========================================================

%>
