// JavaScript Document
var debugAllowed = true;
var Go;
if (!Go) Go = {};

Go.Maker = function()
{
	this.newPlow = 'proppa';
};

Go.Maker.prototype.plow = function()
{
   return this.newPlow;	
};

Go.Maker.prototype.make3 = function(divId)
{
	var divVal = document.getElementById(divId);
	
	var newHtml = this.plow();
	
	divVal.innerHTML = newHtml;
};

//======================================
// Browser Tools
//======================================

Go.BrowserTools = function()
{
	this.xmlhttp = undefined;
	this.init();
};

Go.BrowserTools.prototype.init = function()
{
   try
   {
      // Firefox, Opera, Safari
      this.xmlhttp = new XMLHttpRequest();
   }
   catch (e)
   {
      // Internet Explorer
      try
      {
          this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e)
      {
         try
         {
            this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch (e)
         {
            alert("Your browser does not support AJAX!");
            return false;
         }
      }
   }
};

Go.BrowserTools.prototype.createCookie = function(name,value,days) 
{
	if (days) 
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
};

Go.BrowserTools.prototype.eraseCookie = function(name) 
{
	this.createCookie(name,"",-1);
};

Go.BrowserTools.prototype.readCookie = function(name) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) 
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
};

Go.BrowserTools.prototype.createXmlRequest = function()
{
   return this.xmlhttp;	
};

//====================================== 
// MUTEX
//======================================
if (!Go.Widget) Go.Widget = {};

Go.Widget.Mutex = function(values, files, initial)
{
	var bTools = new Go.BrowserTools();
	this.xmlhttp = bTools.createXmlRequest();
	this.choices = values;
	this.selected = "";
	this.fileMap = [];
	
	var len = files.length;
	for (var iter = 0; iter < len; iter++)
	{
	   this.fileMap[this.choices[iter]] = files[iter];	
	}
	
	if (initial != undefined)
	{
	   this.loadFile(this.fileMap[initial]);
	}
	
	this.clearAll();
};

Go.Widget.Mutex.prototype.clearAll = function()
{
	var numChoices = this.choices.length;
	var iter = 0;
	for (iter = 0; iter < numChoices; iter++)
	{
	   var tag = document.getElementById(this.choices[iter]);
	   tag.innerHTML = "";
	}
	
};

Go.Widget.Mutex.prototype.setSelected = function(selection)
{
	this.clearAll();
	
	if (selection != this.selected)
	{
//		document.getElementById("debug").innerHTML = "not selected";
		this.selected = selection; 
        this.loadFile(this, selection, this.fileMap[selection]);
	}
	else
	{
		this.selected = "";
//	    document.getElementById("debug").innerHTML = "already selected";	
	}
};

Go.Widget.Mutex.prototype.loadFile = function(self, selection, fileName)
{
	var myFile = fileName;
    if (this.xmlhttp != undefined)
	{
	   this.xmlhttp.onreadystatechange = function() { self.pageLoadCallback(selection); }
       this.xmlhttp.open('GET', myFile, true);
	   this.xmlhttp.send(null);
	}
};

Go.Widget.Mutex.prototype.pageLoadCallback = function(selection)
{
	if (this.xmlhttp.readyState == 4) 
	{
		if (this.xmlhttp.status == 200) 
		{
           document.getElementById(selection).innerHTML = this.xmlhttp.responseText;
        } 
		else 
		{
           document.getElementById(selection).innerHTML=" Error:\n"+ this.xmlhttp.status + "\n" + this.xmlhttp.statusText;
        }
	}
};


//=========================================
// DEBUG
//=========================================
function debug(text, append)
{
	if (debugAllowed)
	{
		if (append)
		{
        	text = '<br>' + text + '</br>'; 
    	    document.getElementById('debug').innerHTML += text;   	
		}
	    else
    	{
	    	document.getElementById('debug').innerHTML = text;   	
    	}
	}
}

//=========================================
// Styler
//=========================================
Go.Widget.Styler = function()
{
    this.bTools = new Go.BrowserTools();
	this.xmlhttp = this.bTools.createXmlRequest();
};

Go.Widget.Styler.prototype.styleTag = function(property, value, divtag)
{
	try
	{
//	   debug('stylin');
 	   var chged = document.getElementById(divtag);
	   if (chged.style[property] == undefined)
	   {
		   var err = 'Property: ' + property + ' undefined or does not exist.';   
		   throw err;
	   }
	   chged.style[property] = value;
	}
	catch (e)
	{
	   debug('Something Failed: ' + e, 1);
	}
};

Go.Widget.Styler.prototype.styleChild = function(property, value, parent, child)
{
	try
	{
       if (child == undefined || child == '')
	   {
	      throw "Child tag or class undefined";	
	   }
	
 	   var chged = document.getElementById(parent);
	   if (chged.style[property] == undefined)
	   {
		   var err = 'Property: ' + property + ' undefined or does not exist.';   
		   throw err;
	   }
	   
	   var childs = chged.childNodes;
	   for (var i = 0; i < childs.length; i++)
	   {
		  var childNode = childs[i];
		  if (childNode.tagName == undefined)
		  {
			  continue;
		  }
		  if (childNode.tagName.toLowerCase() == child.toLowerCase())
		  {
//			 debug('old value', 1);
//			 debug(childNode.style[property], 1);

             childNode.style[property] = value;
			 
//			 debug('new value', 1);
//			 debug(childNode.style[property], 1);
		  }
	   }
	}
	catch (e)
	{
	   debug('Something Failed: ' + e, 1);
	}
};
