var glbDivnum  = "";  // all divnum's must be calculated in PHP, which creates the the multiple JS links
var glbThisdiv = "";  // directly below the link clicked
var glbOpennum = "~";
var glbSubj    = "";
var glbComID;
var glbIndent;
function subFN() {
	tit = document.maxForm.tit.value;
	org = document.maxForm.org.value; 
	slo = document.maxForm.slo.value; 
	sug = document.maxForm.sug.value; 

    var dest = '/vote.php';
    var params = 't=' + escape(tit) + '&o=' + escape(org);
	  params += '&s=' + escape(slo) + '&g=' + escape(sug);
	document.getElementById("formMsg").innerHTML = 'Posting...';
    try { xmlhttp = window.XMLHttpRequest? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) { alert('browser doesn\'t support ajax'); }
    xmlhttp.onreadystatechange = votFN2; // same message
    xmlhttp.open("POST", dest, true);
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  // for POST
    xmlhttp.send(params);
}

function titFN() {
	glbOpennum = "~";
	var rng = "7";
	if (document.maxForm.range[0].checked) rng = 'all';
	if (document.maxForm.range[1].checked) rng = '31';
	if (document.maxForm.range[3].checked) rng = '2';
    var dest = '/list.php';  // sets up HTML and also returns the last divnum used
    var params = 'typ=t&rng=' + rng ;  // always d=0  from list.php
	document.getElementById("rightframe").innerHTML = 'Working...';
    try { xmlhttp = window.XMLHttpRequest? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) { alert('browser doesn\'t support ajax'); }
    xmlhttp.onreadystatechange = titFN2;
    xmlhttp.open("POST", dest, true);
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  // for POST
    xmlhttp.send(params);
}
function titFN2() {
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
		var tmp = xmlhttp.responseText;
		var aT = new Array(2);
		aT     = tmp.split("~");
		tmp       = aT[0];
		glbDivnum = aT[1];
		document.getElementById("rightframe").innerHTML = tmp;
	}
}
function namFN() {
	glbOpennum = "~";
	var rng = "7";
	if (document.maxForm.range[0].checked) rng = 'all';
	if (document.maxForm.range[1].checked) rng = '31';
	if (document.maxForm.range[3].checked) rng = '2';
    var dest = '/list.php';
    var params = 'typ=o&rng=' + rng;
	document.getElementById("rightframe").innerHTML = 'Working...';
    try { xmlhttp = window.XMLHttpRequest? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) { alert('browser doesn\'t support ajax'); }
    xmlhttp.onreadystatechange = namFN2;
    xmlhttp.open("POST", dest, true);
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  // for POST
    xmlhttp.send(params);
}
function namFN2() {
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
		var tmp = xmlhttp.responseText;
		var aT = new Array(2);
		aT     = tmp.split("~");
		tmp       = aT[0];
		glbDivnum = aT[1];
		document.getElementById("rightframe").innerHTML = tmp;
	}
}
function sloFN() {
	glbOpennum = "~";
	var rng = "7";
	if (document.maxForm.range[0].checked) rng = 'all';
	if (document.maxForm.range[1].checked) rng = '31';
	if (document.maxForm.range[3].checked) rng = '2';
    var dest = '/list.php';
    var params = 'typ=s&rng=' + rng;
	document.getElementById("rightframe").innerHTML = 'Working...';
    try { xmlhttp = window.XMLHttpRequest? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) { alert('browser doesn\'t support ajax'); }
    xmlhttp.onreadystatechange = sloFN2;
    xmlhttp.open("POST", dest, true);
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  // for POST
    xmlhttp.send(params);
}
function sloFN2() {
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
		var tmp = xmlhttp.responseText;
		var aT = new Array(2);
		aT     = tmp.split("~");
		tmp       = aT[0];
		glbDivnum = aT[1];
		document.getElementById("rightframe").innerHTML = tmp;
	}
}
function sugFN() {
	glbOpennum = "~";
	var rng = "7";
	if (document.maxForm.range[0].checked) rng = 'all';
	if (document.maxForm.range[1].checked) rng = '31';
	if (document.maxForm.range[3].checked) rng = '2';
    var dest = '/list.php';
    var params = 'typ=g&rng=' + rng;
	document.getElementById("rightframe").innerHTML = 'Working...';
    try { xmlhttp = window.XMLHttpRequest? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) { alert('browser doesn\'t support ajax'); }
    xmlhttp.onreadystatechange = sugFN2;
    xmlhttp.open("POST", dest, true);
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  // for POST
    xmlhttp.send(params);
}
function sugFN2() {
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
		var tmp = xmlhttp.responseText;
		var aT = new Array(2);
		aT     = tmp.split("~");
		tmp       = aT[0];
		glbDivnum = aT[1];
		document.getElementById("rightframe").innerHTML = tmp;
	}
}
function votFN(vid,sbj) {
    var dest = '/addvote.php';
    var params = 'v=' + vid + '&s=' + sbj;
	//document.getElementById("rightframe").innerHTML = 'Working...';
    try { xmlhttp = window.XMLHttpRequest? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) { alert('browser doesn\'t support ajax'); }
    xmlhttp.onreadystatechange = votFN2;
    xmlhttp.open("POST", dest, true);
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  // for POST
    xmlhttp.send(params);
}
function votFN2() {
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
		var tmp = xmlhttp.responseText;
		tmp = tmp.replace("~COK~","Thanks for contributing");
		tmp = tmp.replace("~VOK~","Thanks for voting");
		tmp = tmp.replace("~NOK~","Sorry, you have already voted for this topic today");
		document.getElementById("formMsg").innerHTML = '';
		document.getElementById("rightframe").innerHTML = '<blockquote><br />'+tmp+'</blockquote>';
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function comFN(divnum,vid) {
	// if divnum is already open, close it
//alert('divnum'+divnum+' glbOpennum='+glbOpennum);
	if (glbOpennum.indexOf("~"+divnum+"~") > -1) {
		glbOpennum = glbOpennum.replace("~"+divnum+"~","~");
//alert('*** glbOpennum='+glbOpennum);
		document.getElementById(divnum).innerHTML = '';
  } else {
    glbThisdiv = divnum;  // i.e. directly below the link clicked
	glbOpennum += divnum + "~"; 
    var dest = '/getcomments.php';
    var params = 'v=' + vid + '&t=' + glbThisdiv + '&d=' + glbDivnum; 
    try { xmlhttp = window.XMLHttpRequest? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) { alert('browser doesn\'t support ajax'); }
    xmlhttp.onreadystatechange = comFN2;
    xmlhttp.open("POST", dest, true);
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  // for POST
    xmlhttp.send(params);
  }
}
function comFN2() {
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
		var tmp = xmlhttp.responseText;
		var aT    = new Array(2);
		aT        = tmp.split("~");
		tmp       = aT[0];
		glbDivnum = aT[1];
		if (tmp != "") document.getElementById(glbThisdiv).innerHTML = tmp;
	}
}
function repFN(divnum,cid,indent) {
    glbThisdiv = divnum;  // i.e. directly below the link clicked
	glbIndent  = indent;  // indent replies
    var dest = '/getreplies.php';
    var params = 'c=' + cid + '&t=' + glbThisdiv + '&d=' + glbDivnum + '&i=' + glbIndent;
    xmlhttp = window.XMLHttpRequest? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.onreadystatechange = repFN2;
    xmlhttp.open("POST", dest, true);
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  // for POST
    xmlhttp.send(params);
}
function repFN2() {
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
		var tmp = xmlhttp.responseText;
		var aT    = new Array(2);
		aT        = tmp.split("~");
		tmp       = aT[0];
		glbDivnum = aT[1];
		if (tmp != "") document.getElementById(glbThisdiv).innerHTML = tmp;
	}
}
function addcomFN(divnum,vid,sbj) {
	glbThisdiv = divnum;  // oh-oh
	glbDivnum  = divnum;  // oh-oh CHECK THIS OUT
	glbSubj  = sbj
    var dest = '/getcomment.php';
    var params = 'v=' + vid;
    xmlhttp = window.XMLHttpRequest? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.onreadystatechange = addcomFN2;
    xmlhttp.open("POST", dest, true);
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  // for POST
    xmlhttp.send(params);
}
function addcomFN2() {
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
		var tmp = xmlhttp.responseText;
		document.getElementById("formMsg").innerHTML = '';
		document.getElementById(glbThisdiv).innerHTML = tmp;
	}
}
function addrepFN(divnum,cid) {
	glbDivnum = divnum;
	glbComID = cid;
    var dest = '/getreply.php';
    var params = 'v='; // nothing
    xmlhttp = window.XMLHttpRequest? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.onreadystatechange = addrepFN2;
    xmlhttp.open("POST", dest, true);
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  // for POST
    xmlhttp.send(params);

}
function addrepFN2() {
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
		var tmp = xmlhttp.responseText;
		document.getElementById(glbDivnum).innerHTML = tmp;
	}
}
///////////////////////////////////////////////////////////////////////////////
function putacomment(vid) {
   var dest = '/putcomment.php?vid='+vid+'&sbj='+glbSubj+'&txt='+escape(document.commentForm.comment.value);
   xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
   xmlhttp.onreadystatechange = putacommentC;
   xmlhttp.open("GET", dest);
   xmlhttp.send(null);
}
function putacommentC() {
   if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
	  if (glbOpennum.indexOf("~"+glbDivnum+"~") > -1) glbOpennum = glbOpennum.replace("~"+glbDivnum+"~","~");
      document.getElementById("rightframe").innerHTML = xmlhttp.responseText;
   }
}
function putNOcomment() {
	if (glbOpennum.indexOf("~"+glbDivnum+"~") > -1) glbOpennum = glbOpennum.replace("~"+glbDivnum+"~","~");
	document.getElementById(glbDivnum).innerHTML = "";
}
function putareply() {
   var dest = '/putreply.php?cid='+glbComID+'&txt='+escape(document.replyForm.reply.value);
   xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
   xmlhttp.onreadystatechange = putareplyC;
   xmlhttp.open("GET", dest);
   xmlhttp.send(null);
}
function putareplyC() {
   if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
	  if (glbOpennum.indexOf("~"+glbDivnum+"~") > -1) glbOpennum = glbOpennum.replace("~"+glbDivnum+"~","~");
      document.getElementById("rightframe").innerHTML = xmlhttp.responseText;
   }
}
function putNOreply() {
	if (glbOpennum.indexOf("~"+glbDivnum+"~") > -1) glbOpennum = glbOpennum.replace("~"+glbDivnum+"~","~");
    document.getElementById(glbDivnum).innerHTML = "";
}
function faqFN() {
//alert('eh?');
	var T  = "<blockquote><fieldset><h2>FAQ</h2>";
	T += "<p>Coming...</p>";
	T += "<p>Soon...</p></fieldset><br />";
	
T += '<object width="414" height="335"><param name="movie" value="http://www.youtube.com/v/DXnKWs01ITU&hl=en&fs=1"></param><param name="allowFullScreen" value="false"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/DXnKWs01ITU&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="false" width="414" height="335"></embed></object>';
	
	//T += "</blockquote>";
    document.getElementById("rightframe").innerHTML = T;
}
var glbT = '<object width="414" height="335"><param name="movie" value="http://www.youtube.com/v/VIDID"></param><param name="allowFullScreen" value="false"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/VIDID" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="false" width="414" height="335"></embed></object></blockquote>';
function whoFN(vid) {
var T = glbT;
T = T.replace('VIDID',vid);
T = T.replace('VIDID',vid);
//alert(T);
	    document.getElementById("rightframe").innerHTML = T;
}

///////////////////////////////////////////////////////////////////////////////
function addEventListener(instance, eventName, listener) {
    var listenerFn = listener;
    if (instance.addEventListener) {
        instance.addEventListener(eventName, listenerFn, false);
    } else if (instance.attachEvent) {
        listenerFn = function() {
            listener(window.event);
        }
        instance.attachEvent("on" + eventName, listenerFn);
    } else {
        throw new Error("Event registration not supported");
    }
    return {
        instance: instance,
        name:     eventName,
        listener: listenerFn
    };
}
function init() {
    var listener1  = addEventListener(document.getElementById("fTit"), "click", titFN);
    var listener2  = addEventListener(document.getElementById("fNam"), "click", namFN);
    var listener3  = addEventListener(document.getElementById("fSlo"), "click", sloFN);
    var listener4  = addEventListener(document.getElementById("fSug"), "click", sugFN);
    //var listener5  = addEventListener(document.getElementById("fVot"), "click", votFN);
    //var listener6  = addEventListener(document.getElementById("fCom"), "click", comFN);
}
