/* xfjs_view.js: view- and controller-related functions for VC Filter

   Copyright (C) 2009 Black Mesa Technologies LLC

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program, in the Licenses subdirectory, in file
   GNU_GPL.
   
   If the license was not provided, or is missing, see 
   <http://www.gnu.org/licenses/>.

*/

/* Revisions:
   2009-05-02 : CMSMcQ : made first version (empty) of this file
*/

var vcxsl;
var vcParams;
var verbosity = 0;
var gl = new Object();

function loadxfjs() {
  // Provide default styling (run apply-xslt('barbed').
  gl['barbed'] = -1;  
  gl['fair']   = -1;  
  gl['print']  = -1;  
  gl['XiH']    = -1;  
  gl['2col']   = -1;  
  gl.stylesheet = new Object();
  gl.stylesheet['barbed'] = 'hlbarbed.xsl';  
  gl.stylesheet['fair']   = 'hlfair.xsl';  
  gl.stylesheet['XiH']    = 'simple-xml2html.xsl';  
  gl.stylesheet['2col']   = 'two-column.xsl';  

  /* load the XML for HL 10305 once and for all */
  gl.xhr = new XMLHttpRequest();
  gl.xhr.open("GET","hl10305.xml",false);
  gl.xhr.send("");
  gl['hl10305'] = gl.xhr.responseXML;

  applyxslt('barbed');
}

function applyxslt(kw) {
  var xsl;
  if (gl[kw] == -1) {
    /* alert('loading fresh stylesheet ' + kw); */
    var xhr = new XMLHttpRequest();
    xhr.open("GET",gl.stylesheet[kw],false);
    xhr.send("");
    xsl = xhr.responseXML;
  } else {
    xsl = gl[kw];
  }

  vcParams = new Object();
  vcParams['target'] = 'div';


  // Process the document using the chosen stylesheet,
  // and display the results.

  var resultdoc = DomFromTransformXmlXsltParams(gl['hl10305'],xsl,vcParams);
  /* var resultdiv = resultdoc.getElementById('i_am_the_one'); */
  var serializer = new XMLSerializer();
  var resultstring = serializer.serializeToString(resultdoc);
  xsl_id('results').innerHTML = resultstring;
  // xsl_id('results').innerHTML = resultdoc;
  // optionally do XML(serializer.serializeToString(xsd1)).toXMLString();
  // alleged to pretty print with indent step of 2
}

function vcSetParam(nmParam,nmInput) {
  var v = xsl_id(nmInput).value;
  switch (nmParam) {
  case 'vc.Version':
    // check version number as decimal, strip whitespace, ...
    v = dec_clean_dec(v,1.1);
    break;
  case 'vc.AdditionalTypes':
    v = len_clean_len(v);

    break;
  case 'vc.AdditionalFacets':
    // check additional facets as expanded names ...
    v = len_clean_len(v);
    break;
  }
  vcParams[nmParam] = v;
}
  
function bam(s,desc,fallback) {
  alert("The string\n\n   "
	+ s
	+ "\n\nis not recognized as "
	+ desc
	+ ".\n\n"
	+ fallback);
}

function len_clean_len(v) {
  // take what purports to be a white-space-delimited sequence of
  // expanded names, verify it, dropping illegal entries
  vItems = v.split(/\s+/);
  v = "";
  for (i in vItems) {
    expname = vItems[i];

    if (expname === "") {
      // an artifact of our method of splitting ...
      ;
    } else {
      // expname not null
      enParts = expname.match(/^\{([^\}]*)\}(.+)$/);
      if (enParts === null) {
	bam(expname,"an expanded name","Ignoring it.");     
      } else {
	// expname has the pattern {...}...
	
	var nsname = enParts[1];
	var localname = enParts[2];
	
	urimatch = mp_NT_Regex['URI'].exec(nsname);
	lnmatch = mp_NT_Regex['NCName'].exec(localname);
	if ((urimatch === null) && (nsname !== "")) {
	  bam(nsname,
	      "a URI, and thus as a possible namespace name",
	      "Ignoring this entry in the list.");
	  /*
	    alert("The string\n\n    "
	    + nsname
	    + "\n\nis not recognized as a URI, and thus not as a possible namespace name.  "
	    + "Ignoring \""
	    + expname
	    + "\"."
	    );
	  */
	}
	if (lnmatch === null) {
	  bam(localname,
	      "an NCName, and thus as the local-name part of an expanded name",
	      "Ignoring this entry in the list");
	}
	if (urimatch === null || lnmatch === null) {
	  // error message already issued above
	  ;
	} else {
	  // nsname good, local name good
	  v += " " + expname;
	} // if expname is good
      } // if expname has the pattern {...}...
    } // if expname not null
  } // for expname in vItems
  return v;
} // function len_clean_len

function dec_clean_dec(s,decDefault) {
  v = s.replace(/^\s+/,'').replace(/\s+$/,'');
  v = v.replace(/^\+/,''); // strip leading plus sign
  if (v.match(/^-?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))$/)) {
    ;
  } else {
    bam(s,"a decimal number","Using 1.1 instead.");
    /*
      alert("The string \n\n    " 
      + xsl_id(nmInput).value 
      + "\n\nis not recognized as a decimal number.  Using 1.1 instead.");
    */
    v = decDefault;
  }
  return v;
}

