// JavaScript Document

function blogEntry(place,width,height){
	if(!width){
		width = 500;	
	}
	if(!height){
		height = 300;	
	}
	//Set Up Text Area
	area = document.createElement('textarea');
	area.style.width = width + "px";
	area.style.height = height + "px";

	//Set Up Formatting Bar
	surDiv = document.createElement('div');
	surDiv.style.width = width + 6 + "px";
	surDiv.align = "center";
	surDiv.style.border = "3px solid black";
	surDiv.style.position = 'relative';


	optDiv = document.createElement('div');
	optDiv.style.height = '25px';
	optDiv.style.width = width + "px";
	optDiv.style.background = "lightgray";
	optDiv.style.borderBottom = "3px outset";
	optDiv.style.borderRight = "3px outset";
	optDiv.style.borderLeft = "3px outset";
	
	function textDiv(letter,tag){
		if(tag){
			endTag = "</" + tag.substr(1);
		}
		this.div = document.createElement('div');
		this.div.innerHTML = letter;
		this.div.style.width = '25px';
		this.div.style.cssFloat = 'left';
		this.div.style.border = 'thin outset';
		this.div.style.cursor = 'pointer';
		this.div.style.marginLeft = '2px';
		this.div.onmousedown = function(){
			this.style.border = 'thin inset';	
		}
		this.div.onmouseup = function(){
			this.style.border = 'thin outset';	
		}
		this.div.onclick = function (){
		//	alert(area.value.substr(area.selectionStart, (area.selectionEnd - area.selectionStart)));	
			toReplace = area.value.substr(area.selectionStart, (area.selectionEnd - area.selectionStart));	
			replacement = tag + toReplace + endTag;
			area.value = area.value.replace(toReplace,replacement);
		}
	}
	
	bDiv = new textDiv('<b>b</b>','<b>');
	
	iDiv = new textDiv('<em>i</em>','<em>');
		
	nDiv = new textDiv('n');	

	optDiv.appendChild(nDiv.div);
	optDiv.appendChild(bDiv.div);
	optDiv.appendChild(iDiv.div);

/*	preview = document.createElement('div');
	preview.style.width = width + "px";
	preview.style.height = height + "px";
	preview.style.position = 'absolute';
//	preview.style.border = 'thin solid red';
	preview.style.top = '29px';
	preview.style.cursor = 'text';*/
	
	surDiv.appendChild(optDiv);
	surDiv.appendChild(area);
//	surDiv.appendChild(preview);
	place.appendChild(surDiv);
}

function Poll(PHPpage,question,aArray,id){
	//Create the Surrounding Div
	var surDiv = document.createElement("div");
	surDiv.className = "poll";
	
	//Create the Question
	var questionTag = document.createElement("h1");
	questionTag.innerHTML = question;
	
	//Create the answers and their div
	var ansDiv = document.createElement("div");
	ansDiv.className = "ansDiv";
	var form = document.createElement("form");
	form.method = "post";
	form.action = '#';
	for (var i in aArray){
		var label = document.createElement("label");
		label.style.display = "block";
		var input = document.createElement("input");
		input.type = "radio";
		input.name = "answer";
		input.value = i;
		label.appendChild(input);
		label.innerHTML = label.innerHTML + aArray[i];
		form.appendChild(label);
	}
	var submitButton = document.createElement("input");
	submitButton.type = "submit";
	submitButton.value = "Vote!";
	submitButton.style.display = "block";
	submitButton.style.margin = "10px auto 5px"
	form.appendChild(submitButton);
	
	form.onsubmit = function(){
		var ready = false;
		var radios = document.getElementsByTagName("input");
		var value = null;

		for(var i in radios){
			if(radios[i].type == "radio"){
				if(radios[i].checked){
					ready = true;
					value = radios[i].value;
					break;
				}
			}
		}
	
		if(ready){
			ajax = getAjaxObject();
			PHPpage += "?answer=" + value;
			PHPpage += "&id=" + id;
			
			ajax.open("GET",PHPpage, true);
			ajax.send(null);
			ajax.onreadystatechange = function(){
				if(ajax.readyState == 4){
					var data = parseData(ajax.responseText);
					displayGraph(data)
				}
			}
		}
		return false;
	}

	
	function displayGraph(data){
		ansDiv.innerHTML = '';
		for (var i in data){
			if(aArray[i] != null){
				ansDiv.innerHTML += data[i] + "% <label>" + aArray[i]+ "</label><br/>";	
			}
		}
		
	}
	
	function parseData(data){
		return data.split(",");
	}
	
	//Methods
	this.display = function(obj){
		ansDiv.appendChild(form);
		surDiv.appendChild(questionTag);
		surDiv.appendChild(ansDiv);
		obj.appendChild(surDiv);	
	}
	
	
	
}

function getAjaxObject(){
	var xmlHttp;
	try{
  		xmlHttp=new XMLHttpRequest();
  	}catch (e){
  // Internet Explorer
		try{
	    	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}catch (e){
		    try{
		      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		}catch (e){
				alert("Your browser does not support AJAX!");
		    	return false;
      		}
    	}
  	}
	return xmlHttp;
}

function Divider(height, margin){
	var div = document.createElement('div');
	div.style.height = height;
	div.style.width = '2px';
	div.style.margin = margin ? margin : '0';
	div.className = 'divider';
	
	this.display = function(obj){
		obj.appendChild(div);	
	}
	
	this.position = function(type,top,left){
		div.style.position = type;
		div.style.top = top ? top : 0;
		div.style.left = left ? left : 0;
	}
}