/*expect this file to be called by a direct access from within a page
* output the groups news in a nice little tabular form for inclusion in their site
*/

var lgxsl = '<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" indent="yes" encoding="utf-8"/><xsl:variable name="p1">_webname_</xsl:variable><xsl:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable><xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable><xsl:template match="items"><div style="width: _boxwidth_; border: solid 1px _bdcolour_; font-family: _font-family_; font-size: _font-size_;"><xsl:for-each select="item"><xsl:if test="starts-with(translate(./umbrellaauthorshortname,$ucletters,$lcletters),$p1) != false"><div style="background: _hdcolour_; padding: 2px 1px 1px 2px; color: _titlecolour_; text-align: left;"><xsl:value-of select="relatedlivestartdate" />  <b style="padding: 0px 0px 0px 2px"><xsl:value-of select="title" /></b></div><div style="padding: 2px 1px 8px 2px; text-align: left;"><xsl:value-of select="description" disable-output-escaping="yes" /></div></xsl:if></xsl:for-each></div></xsl:template></xsl:stylesheet>';
var PARAMS = new Object();
PARAMS['boxwidth'] = '310px';
PARAMS['webname'] = 'manchester';
PARAMS['hdcolour'] = 'green';
PARAMS['font-family'] = 'inherit';
PARAMS['font-size'] = 'inherit';
PARAMS['titlecolour'] = '#fff';
PARAMS['bdcolour'] = '#aaa';
PARAMS['noresults'] = 'No recent activity documents found. Visit <a href="http://community.foe.co.uk/grouplogin">this page</a> to add activities for your group';

var xmlString = '<?xml version="1.0" encoding="utf-8" ?><items><item><umbrellaauthorshortname>stour</umbrellaauthorshortname><umbrellaauthorname>Harwich Peninsula Friends of the Earth</umbrellaauthorname><umbrellaauthorid>11523</umbrellaauthorid><umbrellaauthorjs>getGroupDocsRunner(11523,document.docform,printGroupDocsS,Harwich Peninsula Friends of the Earth updates,/Templates/entities/foedocument/desc_date_title_html.html); return false;</umbrellaauthorjs><regionrcc>east of england</regionrcc><title>The Welcome Meeting</title><description>Our new group will be having its first public meeting where the future of the group will be shaped by the people of Harwich. Come along to The Salvation Army Hall on George Street at 07:00pm till 08:00pm for talk and tea.</description><relatedlivestartdate>06 Jan, 2010</relatedlivestartdate><documentidentifier>20091207161908Z</documentidentifier></item><item><umbrellaauthorshortname>telford</umbrellaauthorshortname><umbrellaauthorname>Telford Friends of the Earth</umbrellaauthorname><umbrellaauthorid>11531</umbrellaauthorid><umbrellaauthorjs>getGroupDocsRunner(11531,document.docform,printGroupDocsS,Telford Friends of the Earth updates,/Templates/entities/foedocument/desc_date_title_html.html); return false;</umbrellaauthorjs><regionrcc>west midlands</regionrcc><title>Incinerator Victory</title><description>Celebrations were in order on 7th December when plans for an incinerator in the  local borough of Telford &#38; Wrekin were unanimously rejected by the councils plans board.The result followed a long campaign by Telford group, which included lobbying local councillors, arranging public meetings &#38; campaign stalls, and helping local residents to submit planning objections. As part of the campaign, we worked with local campaign group Telford PAIN (People Against Incineration Now)and won the support of both local MPs.</description><relatedlivestartdate>07 Dec, 2009</relatedlivestartdate><documentidentifier>20100118164514Z</documentidentifier></item></items>';

//put the querystring PARAMS into the PARAMS array
function collectQS (qs) {
 if(qs.indexOf('?')!=-1) {
  var qsa = qs.split('?');
  qs = qsa[1];
 }
 var pts = qs.split('&');
 var el; var i=0;
 for(i=0;i<pts.length;i++) {
  el=pts[i].split('=');
  //PARAMS['+unescape(el[0])+'] = unescape(el[1]);
  PARAMS[unescape(el[0])] = unescape(el[1]);
 }
}
function setNews (divid,webname,boxwidth,hdcolour,fontfamily,fontsize,titlecolour,bdcolour,nicename) {
 if(divid == undefined || divid == null) { divid = 'output'; }
 if(webname != undefined && webname != null) { PARAMS['webname'] = webname; }
 if(boxwidth != undefined && boxwidth != null) { PARAMS['boxwidth'] = boxwidth; }
 if(hdcolour != undefined && hdcolour != null) { PARAMS['hdcolour'] = hdcolour; }
 if(fontfamily != undefined && fontfamily != null) { PARAMS['font-family'] = fontfamily; }
 if(fontsize != undefined && fontsize != null) { PARAMS['font-size'] = fontsize; }
 if(titlecolour != undefined && titlecolour != null) { PARAMS['titlecolour'] = titlecolour; }
 if(bdcolour != undefined && bdcolour != null) { PARAMS['bdcolour'] = bdcolour; }
 if(nicename != undefined && nicename != null && (e = document.getElementById('nicename'))!=null) { e.innerHTML = nicename; } else if((e = document.getElementById('nicename'))!=null) { e.innerHTML = PARAMS['webname']; }
  if(nicename != undefined && nicename != null && (e = document.getElementById('groupname'))!=null) { e.innerHTML = nicename; } else if((e = document.getElementById('groupname'))!=null) { e.innerHTML = PARAMS['webname']; }
 
 if((e = document.getElementById('scripttext'))!=null) { e.value = '<script language="javascript" type="text/javascript" src="http://www.foe.co.uk/javascript/activities.js?webname='+PARAMS['webname']+'&boxwidth='+PARAMS['boxwidth']+'&hdcolour='+PARAMS['hdcolour']+'&font-family='+PARAMS['font-family']+'&font-size='+PARAMS['font-size']+'&titlecolour='+PARAMS['titlecolour']+'&bdcolour='+PARAMS['bdcolour']+'"></script>'; document.forms.mainform.scripttext.select(); }
 getGroupNews(divid);
}
function getNews (divid) {
 if(divid == undefined || divid == null) { divid = 'output'; }
 getGroupNews(divid);
}
function getGroupNews (oid,xslT,sortfield,sortorder) {
 var myXslProc,myXMLHTTPRequest,fragment,xmlDoc;
 var finishedHTML = ""; var i = 0;
 var myXslStylesheet = lgxsl;
 myXslStylesheet = myXslStylesheet.replace('_webname_',PARAMS['webname']);
 myXslStylesheet = myXslStylesheet.replace('_boxwidth_',PARAMS['boxwidth']);
 myXslStylesheet = myXslStylesheet.replace('_hdcolour_',PARAMS['hdcolour']);
 myXslStylesheet = myXslStylesheet.replace('_font-family_',PARAMS['font-family']);
 myXslStylesheet = myXslStylesheet.replace('_font-size_',PARAMS['font-size']);
 myXslStylesheet = myXslStylesheet.replace('_titlecolour_',PARAMS['titlecolour']);
  myXslStylesheet = myXslStylesheet.replace('_bdcolour_',PARAMS['bdcolour']);
if(document.implementation.createDocument) {   
   //xmlDoc = document.implementation.createDocument('','',xmlDoc2);
   // load the xsl text
   myXslProc = new XSLTProcessor();
   var parser = new DOMParser();
   myXslStylesheet = parser.parseFromString(myXslStylesheet, "text/xml");
   xmlDoc = parser.parseFromString(xmlString, "text/xml");
   myXslProc.importStylesheet(myXslStylesheet);
   try {
    fragment = myXslProc.transformToFragment(xmlDoc, document);
    } catch (e) {
     alert('caught exception in transformToFragment'+e.toString());
    }
    var tmpBox = document.createElement("div");
    tmpBox.appendChild(fragment);
    finishedHTML = tmpBox.innerHTML; 
 } else { 
   try {
    var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
    var xslTemplate = new ActiveXObject("Msxml2.XSLTemplate");
    xslDoc.async = false;
    xslDoc.loadXML(myXslStylesheet);
    xslTemplate.stylesheet = xslDoc;
    myXslProc = xslTemplate.createProcessor();
    //xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
 xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
    //xmlDoc.async = false;
    xmlDoc.loadXML(xmlString);
    //if (xmlDoc.parseError.errorCode != 0) {
      //var myErr = xmlDoc.parseError;
      //WScript.Echo("You have error " + myErr.reason);
    //} else {
      myXslProc.input = xmlDoc;
      myXslProc.transform();
      finishedHTML = myXslProc.output;  

   
 } catch (ie) {
     alert('caught iexception in ie block'+ie.toString());
    }
    //}
 }
 	  if(finishedHTML.indexOf('8pt;"></div>')!=-1){
		   finishedHTML = PARAMS['noresults'];
	  }
 var a;
 if((a = document.getElementById(oid))!=null) {
   
   a.innerHTML = finishedHTML;
 } else {
  document.write(finishedHTML);
 }
}
//heres the code that kicks off things when the js file is referenced directly from within a web page
var scripts = document.getElementsByTagName("script");
if(scripts!=undefined) {
 var i;
 for(i=0;i<scripts.length;i++) {
  if(scripts[i].src != undefined && scripts[i].src.indexOf('activities.js')!=-1) {
   //look for the group parameter
   if((wi = scripts[i].src.indexOf('?'))!=-1) {
    collectQS(scripts[i].src);
    if(PARAMS['webname']) { 
     getNews();
    }
   }
  }
 }
}

function g(v){
if(v!='' && v.indexOf('List')!=0) {
var upto = document.forms.mainform.scripttext.value.indexOf('webname=');
var from = document.forms.mainform.scripttext.value.indexOf('"',upto);
document.forms.mainform.scripttext.value = document.forms.mainform.scripttext.value.substring(0,upto)+'webname='+v+document.forms.mainform.scripttext.value.substring(from,document.forms.mainform.scripttext.value.length)
document.forms.mainform.scripttext.select();
}
//also set the other drop down
selectdd(document.forms.mainform.w2,v);
}
function selectdd (list,t) {
for(i=0;i<list.length;i++) {
if(list[i].text==t) {
list[i].selected = true;
break;
}
}
}
/*
function HighlightText(){
if (document.selection) {
var r1 = document.body.createTextRange();
r1.moveToElementText(document.getElementById('this1'));
r1.setEndPoint("EndToEnd", r1);
r1.moveStart('character', 4);
r1.moveEnd('character', 8);
r1.select();
} else {
s = window.getSelection();
var r1 = document.createRange();
r1.setStartBefore(document.getElementById('this1'));
r1.setEndAfter(document.getElementById('this1')) ;
s.addRange(r1);
}
}
*/
