// JavaScript Document
var style="none";
var updateStyle = 0;

function urlencode(s) {
  s = encodeURIComponent(s);
  return s.replace(/~/g,'%7E').replace(/%20/g,'+');
 }

//First things first, set up our array that we are going to use.
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + //all caps
"abcdefghijklmnopqrstuvwxyz" + //all lowercase
"0123456789+/="; // all numbers plus +/=
//Heres the encode function
function base64_encode(inp)
{
var out = ""; //This is the output
var chr1, chr2, chr3 = ""; //These are the 3 bytes to be encoded
var enc1, enc2, enc3, enc4 = ""; //These are the 4 encoded bytes
var i = 0; //Position counter
do { //Set up the loop here
chr1 = inp.charCodeAt(i++); //Grab the first byte
chr2 = inp.charCodeAt(i++); //Grab the second byte
chr3 = inp.charCodeAt(i++); //Grab the third byte
//Here is the actual base64 encode part.
//There really is only one way to do it.
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;

if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
//Lets spit out the 4 encoded bytes
out = out + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) +
keyStr.charAt(enc4);
// OK, now clean out the variables used.
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < inp.length); //And finish off the loop
//Now return the encoded values.
return out;
}
//Heres the decode function
function base64_decode(inp)
{
var out = ""; //This is the output
var chr1, chr2, chr3 = ""; //These are the 3 decoded bytes
var enc1, enc2, enc3, enc4 = ""; //These are the 4 bytes to be decoded
var i = 0; //Position counter
// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
var base64test = /[^A-Za-z0-9\+\/\=]/g;
if (base64test.exec(inp)) { //Do some error checking
alert("There were invalid base64 characters in the input text.\n" +
"Valid base64 characters are A-Z, a-z, 0-9, ?+?, ?/?, and ?=?\n" +
"Expect errors in decoding.");
}
inp = inp.replace(/[^A-Za-z0-9\+\/\=]/g, "");
do { //Here’s the decode loop.
//Grab 4 bytes of encoded content.
enc1 = keyStr.indexOf(inp.charAt(i++));
enc2 = keyStr.indexOf(inp.charAt(i++));
enc3 = keyStr.indexOf(inp.charAt(i++));
enc4 = keyStr.indexOf(inp.charAt(i++));
//Heres the decode part. There’s really only one way to do it.
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
//Start to output decoded content
out = out + String.fromCharCode(chr1);
if (enc3 != 64) {
out = out + String.fromCharCode(chr2);
}
if (enc4 != 64) {
out = out + String.fromCharCode(chr3);
}
//now clean out the variables used
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < inp.length); //finish off the loop
//Now return the decoded values.
return out;
}

function loadXMLDoc(url,func) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.open("GET", url, true);
        req.onreadystatechange = func;
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.open("GET", url, true);
            req.onreadystatechange = func;
            req.send();
        }
    }
}

function loadXML_POST(url,params,func) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.open("POST", url, true);
        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.onreadystatechange = func;
        req.send(params);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.open("POST", url, true);
            req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            req.onreadystatechange = func;
            req.send(params);
        }
    }
}

function updateBody (name) {
	style=name;
	//alert(name);
	updateStyle = 1;	
}

function finish_page(paid)
{
  var s= new String(document.location);
  var p=s.lastIndexOf('/')+1;
  s=s.substr(p);
  p=s.lastIndexOf('.');
  s=s.substr(0,p);

  var bg=document.getElementById('_body').getAttribute('style');
//alert(document.getElementById('_body').getAttribute('style')); return;
  var t=document.getElementById('sh1').innerHTML;
  var cl=document.getElementById('sh1color').color;

  var tcode=document.getElementById('sh2_top').innerHTML;
  var scode=document.getElementById('sh2_side').innerHTML;

  var vc='';
  if (document.getElementById('sh3'))
  vc=encodeURIComponent(document.getElementById('sh3').innerHTML);
  
  var mc='';
  if (document.getElementById('sh4'))
	mc = encodeURIComponent($("#sh4").html());
  //mc=encodeURIComponent(document.getElementById('sh4').innerHTML);

  var l=document.getElementById('link_bottom').innerHTML;

  var cf=document.getElementById('sh6').innerHTML;
//  alert(cf);
//  return;

  var params="p="+s+"&t="+t+"&cl="+cl+
                    "&vc="+encodeURIComponent(vc)+"&mc="+mc+"&l="+l+"&cf="+cf+
					"&bg="+bg+"&paid="+paid;
	if (updateStyle == 1)
	{
		//alert(style);
		params = params+"&style="+style;
	}
                    //"&tc="+tcode+"&sc="+scode+
//  alert(params);
//  return;
  loadXML_POST('http://www.mybuzzbot.com/buzzboards/engine/finish.php',params,processReqChange_finish);
}

function processReqChange_finish() 
{
  if (req.readyState == 4)
  { // only if "OK"
    if (req.status == 200)
    { // ...processing statements go here...      
	  var page=req.responseText;
//	  alert(page); 
	  alert('The page was saved successfully. You can view your page by going to http://www.mybuzzbot.com/boards/'+page+'.php');
      window.location='http://www.mybuzzbot.com/boards/'+page+'.php';
    } 
  }
}

function new_page()
{
  var s= new String(document.location);
  var p=s.lastIndexOf('/')+1;
  s=s.substr(p);
  p=s.lastIndexOf('.');
  s=s.substr(0,p);

  var t=document.getElementById('sh1').innerHTML;
  var cl=document.getElementById('sh1color').color;

  var tcode=document.getElementById('sh2_top').innerHTML;
  var scode=document.getElementById('sh2_side').innerHTML;

  var vc='';
  if (document.getElementById('sh3'))
  vc=document.getElementById('sh3').innerHTML;
  
  var mc='';
  if (document.getElementById('sh4'))
  mc=document.getElementById('sh4').innerHTML;

  var l=document.getElementById('link_bottom').innerHTML;

  var cf=document.getElementById('sh6').innerHTML;

  var params="p="+s+"&t="+t+"&cl="+cl+
                    "&tc="+tcode+"&sc="+scode+
                    "&vc="+vc+"&mc="+mc+"&l="+l+"&cf="+cf;
  loadXML_POST('http://www.mybuzzbot.com/buzzboards/engine/new_page.php',params,processReqChange_newpage);
}

function processReqChange_newpage() 
{
  if (req.readyState == 4)
  { // only if "OK"
    if (req.status == 200)
    { // ...processing statements go here...      
//	  var msg=req.responseText;
//	  alert(msg);
      window.location='http://www.mybuzzbot.com/buzzboards/create.php';
    } 
  }
}


function saveView()
{
  var s= new String(document.location);
  var p=s.lastIndexOf('/')+1;
  s=s.substr(p);
  p=s.lastIndexOf('.');
  var page=s.substr(0,p);

  loadXMLDoc('http://www.mybuzzbot.com/buzzboards/engine/saveview.php?page='+page,processReqChange_saveView);
}


function processReqChange_saveView() 
{
  if (req.readyState == 4)
  { // only if "OK"
    if (req.status == 200)
    { // ...processing statements go here...
//      var n = req.responseText; 
//      alert('--->>>'+n+'<<<---');
    } 
  }
}

function showPages()
{
  loadXMLDoc('engine/getpages_table.php',processReqChange_showPages);
}

function processReqChange_showPages() 
{
  if (req.readyState == 4)
  { // only if "OK"
    if (req.status == 200)
    { // ...processing statements go here...      
	  var response = req.responseXML.documentElement;
      var result = response.getElementsByTagName('result');
	 
        var row;
		
		var kol_rec=result.length-2;

        if (kol_rec>0)
		{
            row='<table cellspacing="1" border="0" class="tablesorter" width="100%">'+
		      '<thead><tr>'+
  '<tr>'+
        '<td width="16%" align="center" bgcolor="#000000" nowrap><font face="Arial"'+
		'color="#FFFFFF"><small><strong>BuzzBoard'+
        'Name</strong></small></font></td>'+
        '<td width="16%" align="center" bgcolor="#000000"><font face="Arial" color="#FFFFFF"><small><strong>BuzzBoard'+
        'LINK URL</strong></small></font></td>'+
        '<td width="17%" align="center" bgcolor="#000000"><font face="Arial" color="#FFFFFF"><small><strong>Page'+
        'Views</strong></small></font></td>'+
        '<td width="17%" align="center" bgcolor="#000000"><font face="Arial" color="#FFFFFF"><small><strong>Form'+
        'Fills</strong></small></font></td>'+
        '<td width="17%" align="center" bgcolor="#000000"><font face="Arial" color="#FFFFFF"><small><strong>Edit</strong></small></font></td>'+
        '<td width="17%" align="center" bgcolor="#000000"><font face="Arial" color="#FFFFFF"><small><strong>Delete</strong></small></font></td>'+
      '</tr></thead><tbody>';
		 for (var i=2; i<kol_rec; i=i+6)
		  {
            var id_page=result[i].firstChild.data;
            var title=result[i+1].firstChild.data;
            var views=result[i+2].firstChild.data;
	        var fills=result[i+3].firstChild.data;
			var num = result[i+4].firstChild.data;
			var id_mem = result[i+5].firstChild.data;
	        
            row=row+
			      '<tr>'+
        '<td width="16%" align="center"><font face="Arial" color="#008000"><strong>'+title+'</strong></font></td>'+
        '<td width="16%" align="center"><a href="http://www.mybuzzbot.com/boards/'+id_mem+num+'.php" target="_blank"><small><small><font face="Arial">http://www.mybuzzbot.com/boards/'+id_mem+num+'.php</font></small></small></a></td>'+
        '<td width="17%" align="center">'+views+'</td>'+
        '<td width="17%" align="center"><a HREF="javascript:popUp(\'formfills.php?board='+id_mem+num+'\')">'+fills+'</a></td>'+
        '<td width="17%" align="center"><a href="http://www.mybuzzbot.com/boards/'+id_mem+num+'.php?ed=1" target="_blank"><small><font face="Arial">Edit</font></small></a></td>'+
        '<td width="17%" align="center"><a href="#" onClick="delPage('+id_page+')"><small><font face="Arial">Delete</font></small></a></td>'+
      '</tr>';
          } //for
		  
          row=row+'</tbody></table>';
		  document.getElementById('pages-table').innerHTML = row;
		  
		}
		else { document.getElementById('pages-table').innerHTML='<center><h1>You haven\'t created any boards yet.</h1></center>'; }	
      showElement('pages');
    } 
  }
}

function delPage(id_page)
{
var message = 'Are you sure you want to delete this page? This action cannot be undone.';	
var url = 'engine/delpage.php?page='+id_page;
if (confirm(message)) 
  loadXMLDoc(url,processReqChange_delPage());
}


function processReqChange_delPage() 
{
  if (req.readyState == 4)
  { // only if "OK"
    if (req.status == 200)
    { // ...processing statements go here...
      alert('Removal has passed successfully');
//      loadXMLDoc('engine/getpages_table.php',processReqChange_showPages);
//      showPages();
	  window.location='http://www.mybuzzbot.com/buzzboards/controlcenter.php';
    } 
  }
}

function showElement(id)
{
 document.getElementById(id).style.display = 'block';
}

