//Form/ajax js

//checks to see what to do on Select update
function updateReviewForm(theValue){
	//alert(theValue);
	if(theValue == 'new'){
		showHostInputs();
	}else if(theValue == ''){
		
	}else{
		hideHostInputs();
	}
}

//Sets hostname and url to hidden if a host is chosen
function hideHostInputs(){
	//set host name to hidden
	var hostName = document.getElementById('name');
	var parentNode = hostName.parentNode;
	parentNode.style.display = 'none';
	
	//set host url to hidden
	var hostURL = document.getElementById('url');
	parentNode = hostURL.parentNode;
	parentNode.style.display = 'none';
	
}

//shows host name and url if Other is chosen from host
function showHostInputs(){
	//set host name to visible
	var hostName = document.getElementById('name');
	var parentNode = hostName.parentNode;
	parentNode.style.display = 'block';
	
	//set host url to visible
	var hostURL = document.getElementById('url');
	parentNode = hostURL.parentNode;
	parentNode.style.display = 'block';
}

//displays the value of all tags in body, based on the search body.getElementsByTagName("h1");

//example: https://developer.mozilla.org/En/DOM/Element.getElementsByTagName
function xGetElementsByTagName(tagname){
	var body = document.body; 
	var h1s = body.getElementsByTagName(tagname); 
	for (var i = 0; i < h1s.length; i++) { 
		alert(h1s[i].innerHTML);
	}
}

function xSetElementOnMouseOver(element){
	//element.onmouseover  = "this.className='over'";
	//element.onmouseout  = "this.className=''";
	//element.attachEvent("onmouseover", testAlert);
	//element.attachEvent("onmouseout", testAlert);
	//element.onmouseover=xChangeClass(element, true);
	//element.onmouseout=xChangeClass(element, false);
	element.addEventListener('mouseover', xChangeClass, true);
	element.addEventListener('mouseout', xChangeClass, true);
	alert(element.innerHTML);
}

function xChangeClass(e){
	//look at the eventtype to determine action taken
	if(e.type=='mouseover'){
		//e.target.style.color='green';
		e.target.className = 'over';
	}else{	
		//e.target.style.color='black';
		e.target.className = 'over2';
	}

}

function xSetAllElementsAttributes(){
	var body = document.body; 
	if (body.hasChildNodes()){
		//alert('listeners being added to body');
	}else{
		alert('fucked')
	}
	var children = body.childNodes; 
	for (var i = 0; i < children.length; i++) { 
		//alert(children[i].innerHTML);
		children[i].addEventListener('mouseover', xChangeClass, true);
		children[i].addEventListener('mouseout', xChangeClass, true);
		children[i].addEventListener('click', xSetValueAndPath, true);
	}
}

//stores the value of the element into the texbox
function xSetValueAndPath(e){
	//set value
	document.forms['functionCaller'].elements['innerHTMLBox'].value = e.target.innerHTML;
	
	//set path
	var element = e.target;
	//var element = document.getElementById('fuz');
	var hasParentNode = true;
	var tempElement = element;
	var pathToElement = element.tagName;
	while(hasParentNode){
		//alert('Parent = '+tempParent.parentNode.tagName);
		if(tempElement.parentNode.parentNode.tagName == null){
			hasParentNode = false;
		}
		tempElement = tempElement.parentNode;
		
		//PREVENT TBODY from being added
		if(tempElement.tagName != 'TBODY')
			pathToElement = tempElement.tagName +"/"+ pathToElement;
		//alert('path:'+pathToElement+", current element: "+tempElement.tagName);
	}
	pathToElement = "//"+ pathToElement;element
	document.forms['functionCaller'].elements['elementPathBox'].value = pathToElement;
	document.forms['functionCaller'].elements['elementPHPPathBox'].value = e.target.getAttribute('phpdom').toUpperCase();
	//document.forms['functionCaller'].elements['elementPHPPathBox'].value = pathToElement;
	//grab the PHP dom
	e.preventDefault();
}

function xSetPHPDOMPath(e){
	//set value
	
	document.forms['functionCaller'].elements['elementPHPPathBox'].value = e.target.getAttribute('phpdom');
	//grab the PHP dom
	e.preventDefault();
}
function testAlert(){
	alert('test');
}

function simpleEvent(e){ //the e is the eventObject set by default if this was caused by an event
	var targ = e.target;
	var tname = targ.tagName;
	alert("You clicked on a " + tname + " element.");
}

function setTextBox(){
	document.forms['functionCaller'].elements['innerHTMLBox'].value = 'test';
}

//finds the path of an element, from the root
//DEPRECATED
function xSetPathBox(e){
	//this has been deprecated
	
	var element = e.target;
	//var element = document.getElementById('fuz');
	var hasParentNode = true;
	var tempElement = element;
	var pathToElement = element.tagName;
	while(hasParentNode){
		//alert('Parent = '+tempParent.parentNode.tagName);
		if(tempElement.parentNode.tagName == 'BODY'){
			hasParentNode = false;
		}
		tempElement = tempElement.parentNode;
		
		//PREVENT TBODY from being added
		if(tempElement.tagName != 'TBODY')
			pathToElement = tempElement.tagName +"/"+ pathToElement;
		//alert('path:'+pathToElement+", current element: "+tempElement.tagName);
	}
	pathToElement = "//"+ pathToElement;
	document.forms['functionCaller'].elements['elementPathBox'].value = pathToElement;
}

//sets all iframe attributes
function xSetAllIFrameElementsAttributes(){
	var body = frames['testiframe'].document.body;
	if (body.hasChildNodes()){
		//alert('listeners being added to body');
	}else{
		alert('fucked')
	}
	var children = body.childNodes; 
	for (var i = 0; i < children.length; i++) { 
		//alert(children[i].innerHTML);
		children[i].addEventListener('mouseover', xChangeClass, true);
		children[i].addEventListener('mouseout', xChangeClass, true);
		//children[i].addEventListener('click', xSetValue, true);
		//children[i].addEventListener('click', xSetPathBox, true);
	}
}

//copies data from the path input to the xquery input
function copyValueToXQuery(el){
	//document.forms['adminform'].elements['userXQuery'].value = document.forms['functionCaller'].elements['elementPathBox'].value;
	var xqueryArray = document.forms['adminform'].elements['userXQuery[]'];
	//xqueryArray[xqueryArray.length-1].value = document.forms['functionCaller'].elements['elementPathBox'].value;
	xqueryArray[xqueryArray.length-1].value = el.value;
}

//copies data from the regex select to the regex textarea
function copySelectToRegex(){
	document.forms['adminform'].elements['userRegex'].value = document.forms['adminform'].elements['userRegexChoice'].value;
}

//copies data from the xquery select to the xquery textarea
function copySelectToXQuery(){
	//delete all existing xqueries
	var container = document.getElementById('xQueryDiv');
	while ( container.childNodes.length >= 1 ){
		container.removeChild( container.firstChild );       
	} 

	
	//explode on @#2#
	var explodeMe = document.forms['adminform'].elements['userXQueryChoice'].value;
	//create new fields w/ values - steal this code from createXQueryInput
	//var xQueryArray = explodeMe.split(/(\(-\))/);
	//temp regex to split on /\(([0-9]+)-([A-Za-z])\)/
	var xQueryArray = explodeMe.split(/\(([0-9]+)-(xml|text)\)/);
	for(var i = 0; i < xQueryArray.length-1; i=i+3){
		createXQueryInputValueFull(xQueryArray[i], xQueryArray[i+1], xQueryArray[i+2]);	
	}
	
	
	//document.forms['adminform'].elements['userXQuery'].value = document.forms['adminform'].elements['userXQueryChoice'].value;
}



//splits a custom array from PHP so javascript can use it
function jsrsArrayFromString(s){
	// rebuild an array returned from server as stringn
	// optional delimiter defaults to ~
	var d = '~-!~34~-!';
	return s.split(d);
}  

// populates select list from array of items given as objects: { name: 'text', value: 'value' }
function populateSelect(el, items, firstElementMessage) {
    el.options.length = 0;
    if (items.length > 0)
        el.options[0] = new Option(firstElementMessage, 'nothing');

    //for items.length
	for (var index=0; index < items.length; index++)	{
		if(items[index] != ''){ // non empty
			if(items[index].length > 45){ // not too long
				el[el.length] = new Option(items[index].substring(0, 45)+"...", items[index]);
			}else{
				el[el.length] = new Option(items[index]);
			}
		}
	} 
	
}

//START AJAX FUNCTIONS
//ajax Populate Files Select
function populateFiles(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	var userHost = document.getElementById('userHost').value;
	var queryString = "?hostname=" + userHost;
	if(userHost == 'nothing'){		
		var fileSelect = document.getElementById('userFile');
		
		fileSelect.options.length = 0;
        fileSelect.options[0] = new Option('Select a File...', 'nothing');
		return false;
	}
	
	ajaxRequest = new XMLHttpRequest();
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			//populate the select with the array generated on the PHP script. 
			//Do some R&D and look up Javascript arrays when making php script
			
			//var ajaxDisplay = document.getElementById('ajaxDiv');
			//ajaxDisplay.innerHTML = ajaxRequest.responseText;
			//first test case is to dump all the result into the regex textarea
			var tempArray = jsrsArrayFromString(ajaxRequest.responseText);
			//Delete current select text, add new items
			var fileSelect = document.getElementById('userFile');
			populateSelect(fileSelect, tempArray, "Select a File...");
		}
	}
	ajaxRequest.open("GET", "scraper-gui-ajax-host.php" + queryString, true);
	ajaxRequest.send(null); 
}


//ajax Populate List of Links drop down
function populateListOfLinks(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	var userHost = document.getElementById('userHost').value;
	var queryString = "?hostname=" + userHost;
	if(userHost == 'nothing'){		
		var fileSelect = document.getElementById('userListOfLinks');
		
		fileSelect.options.length = 0;
        fileSelect.options[0] = new Option('Select a List...', 'nothing');
		return false;
	}
	
	ajaxRequest = new XMLHttpRequest();
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			//populate the select with the array generated on the PHP script. 
			//Do some R&D and look up Javascript arrays when making php script
			
			//var ajaxDisplay = document.getElementById('ajaxDiv');
			//ajaxDisplay.innerHTML = ajaxRequest.responseText;
			//first test case is to dump all the result into the regex textarea
			var tempArray = jsrsArrayFromString(ajaxRequest.responseText);
			//Delete current select text, add new items
			var fileSelect = document.getElementById('userListOfLinks');
			populateSelect(fileSelect, tempArray, "Select a List...");
		}
	}
	ajaxRequest.open("GET", "scraper-gui-ajax-get-listolinks-db.php" + queryString, true);
	ajaxRequest.send(null); 
}


//ajax Populate Regex and XQuery
function populateRegexAndXquery(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	var userFile = document.getElementById('userFile');
	var tempIDArray = userFile.value.split('-');
	var tempID = tempIDArray[0];
	var queryString = "?id=" + tempID;
	if(userFile.value == 'nothing'){
		var regexSelect = document.getElementById('userRegexChoice');
		var xquerySelect = document.getElementById('userXQueryChoice');
		
		regexSelect.options.length = 0;
        regexSelect.options[0] = new Option('Select a Regex...', 'nothing');
		xquerySelect.options.length = 0;
        xquerySelect.options[0] = new Option('Select an XQuery...', 'nothing');
		return false;
	}
	
	ajaxRequest = new XMLHttpRequest();
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			//populate the select with the array generated on the PHP script. 
			var tempResponseText = ajaxRequest.responseText;
			var tempArray = tempResponseText.split('###8934043073247023asdsaffd###');
			var tempArrayRegex = jsrsArrayFromString(tempArray[0]);
			var tempArrayXQuery = jsrsArrayFromString(tempArray[1]);
			//Delete current select text, add new items
			//split on    ###8934043073247023asdsaffd###             [0] is regex   [1] is xquery
			var regexSelect = document.getElementById('userRegexChoice');
			var XQuerySelect = document.getElementById('userXQueryChoice');
			populateSelect(regexSelect, tempArrayRegex, "Select a Regex...");
			populateSelect(XQuerySelect, tempArrayXQuery, "Select an XQuery...");
		}
	}
	ajaxRequest.open("GET", "scraper-gui-ajax-patterns.php" + queryString, true);
	ajaxRequest.send(null); 
}


//saves the Regex and/or XQuery
function saveRegexAndOrXQuery(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	var userFile = document.getElementById('userFile');
	if(userFile.value == 'nothing'){
		alert('Please Select a File to Associate With');
		return;
	}
	var tempIDArray = userFile.value.split('-');
	var tempID = tempIDArray[0];
	
	var regexInput = document.getElementById('userRegex');
	//var XQueryInput = document.getElementById('userXQuery');
	var XQueryArray = document.forms['adminform'].elements['userXQuery[]'];
	var XQueryString = '';
	var tempOptionsArray = '';
	var tempXQueryOption = '';
	//build the XQuery querystring array
	if(XQueryArray.type == 'text'){
		//there was 1 xquery
		//get position and option  @ 1
		tempOptionsArray = document.forms['adminform'].elements['userXQueryOptions-1'];
		for (var i=0; i < tempOptionsArray.length; i++) {
			if (tempOptionsArray[i].checked){
				tempXQueryOption = tempOptionsArray[i].value;
				break;
			}
		}
		XQueryString = XQueryArray.value+'('+document.forms['adminform'].elements['userXQueryPosition-1'].value+'-'+tempXQueryOption+')';
	}else{	
		//there were multiple xqueries
		//get option position 1
		tempOptionsArray = document.forms['adminform'].elements['userXQueryOptions-1'];
		for (var i=0; i < tempOptionsArray.length; i++) {
			if (tempOptionsArray[i].checked){
				tempXQueryOption = tempOptionsArray[i].value;
				break;
			}
		}
		//XQueryString = XQueryArray[0].value;
		XQueryString = XQueryArray[0].value+'('+document.forms['adminform'].elements['userXQueryPosition-1'].value+'-'+tempXQueryOption+')';
		
		//set position and option @ 1-?
		for(var i = 1; i < XQueryArray.length; i++){			
			
			var tempOptionsArray = document.forms['adminform'].elements['userXQueryOptions-'+(i+1)];
			for (var j=0; j < tempOptionsArray.length; j++) {
				if (tempOptionsArray[j].checked){
					tempXQueryOption = tempOptionsArray[j].value;
					break;
				}
			}
			XQueryString = XQueryString + XQueryArray[i].value + '('+document.forms['adminform'].elements['userXQueryPosition-'+(i+1)].value+'-'+tempXQueryOption+')';
		}
	}
	
	
	ajaxRequest = new XMLHttpRequest();
	
	var url = "scraper-gui-ajax-post-save-patterns.php";
	//var params = "regex="+regexInput.value+"&xquery="+XQueryInput.value+"&id="+tempID;
	var params = "regex="+regexInput.value+"&xquery="+XQueryString+"&id="+tempID;
	//alert('Params - '+ params);
	//using post - http://www.openjs.com/articles/ajax_xmlhttp_using_post.php
	ajaxRequest.open("POST", url, true);
	//Send the proper header information along with the request
	ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajaxRequest.setRequestHeader("Content-length", params.length);
	ajaxRequest.setRequestHeader("Connection", "close");

	//alert('headers sent');
	ajaxRequest.onreadystatechange = function() {//Call a function when the state changes.
		if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
			//alert('respnse:'+ajaxRequest.responseText);
			var saveStatusSpan = document.getElementById('patternSaveStatus');
			saveStatusSpan.innerHTML = "<b style='color:green'>Saved</b>";
		}else{
			var saveStatusSpan = document.getElementById('patternSaveStatus');
			saveStatusSpan.innerHTML = "<b style='color:red'>Saving...</b>";
		}
	}
	ajaxRequest.send(params);
}

//rewrites the regex temp file
function rewriteRegexFile(){
	var arrInput = document.forms['regexMatchResults'].elements['regexColumns[]'];
	var columnString = arrInput[0].value;
	//var columnString = document.forms['regexMatchResults'].elements['regexColumns-1'].value;
	for (var i = 1; i < arrInput.length; i++) { 
	//for (var i = 2; i <= inputCount; i++) { 
		//columnString = columnString + ','+ document.forms['regexMatchResults'].elements['regexColumns-'+ i].value;
		columnString = columnString + ','+ arrInput[i].value;
	}
	
	var ajaxRequest;  // The variable that makes Ajax possible!
	var regexFilename = document.getElementById('tempRegexFilename').value;
	
	// Opera 8.0+, Firefox, Safari
	ajaxRequest = new XMLHttpRequest();
	var queryString = "?filename=" + regexFilename + "&columns=" +columnString;
	//alert('querystring - '+ queryString);
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			//populate the select with the array generated on the PHP script. 
			//alert('Response - '+ ajaxRequest.responseText);
			var spanBox = document.getElementById('regexRewriteSpan');
			spanBox.innerHTML = " <span style='color:green'>File Updated</span>";
		}else{
			var spanBox = document.getElementById('regexRewriteSpan');
			spanBox.innerHTML = " <span style='color:red'>Updating File...</span>";
		}
	}
	ajaxRequest.open("GET", "scraper-gui-ajax-regex-rewrite.php" + queryString, true);
	ajaxRequest.send(null); 
}

//rewrites the xquery temp file
function rewriteXQueryFile(){
	var arrInput = document.forms['xqueryMatchResults'].elements['xqueryColumns[]'];
	var columnString = arrInput[0].value;
	//var columnString = document.forms['regexMatchResults'].elements['regexColumns-1'].value;
	for (var i = 1; i < arrInput.length; i++) { 
	//for (var i = 2; i <= inputCount; i++) { 
		//columnString = columnString + ','+ document.forms['regexMatchResults'].elements['regexColumns-'+ i].value;
		columnString = columnString + ','+ arrInput[i].value;
	}
	
	var ajaxRequest;  // The variable that makes Ajax possible!
	var xqueryFilename = document.getElementById('tempXQueryFilename').value;
	
	// Opera 8.0+, Firefox, Safari
	ajaxRequest = new XMLHttpRequest();
	var queryString = "?filename=" + xqueryFilename + "&columns=" +columnString;
	//alert('querystring - '+ queryString);
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			//populate the select with the array generated on the PHP script. 
			//alert('Response - '+ ajaxRequest.responseText);
			var spanBox = document.getElementById('xqueryRewriteSpan');
			spanBox.innerHTML = " <span style='color:green'>File Updated</span>";
		}else{
			var spanBox = document.getElementById('xqueryRewriteSpan');
			spanBox.innerHTML = " <span style='color:red'>Updating File...</span>";
		}
	}
	ajaxRequest.open("GET", "scraper-gui-ajax-xquery-rewrite.php" + queryString, true);
	ajaxRequest.send(null); 
}


//sends of a grab all links like this request
function getAllLinksLikeThis(usePHP){
	var ajaxRequest;  // The variable that makes Ajax possible!
	if(usePHP)
		var xpathToLink = document.forms['functionCaller'].elements['elementPHPPathBox'].value;
	else
		var xpathToLink = document.forms['functionCaller'].elements['elementPathBox'].value;
	
	var userFile = document.getElementById('userFile');
	if(userFile.value == 'nothing'){
		alert('Please Select a File to Associate With');
		return;
	}
	var tempIDArray = userFile.value.split('-');
	var tempID = tempIDArray[0];
	
	// Opera 8.0+, Firefox, Safari
	ajaxRequest = new XMLHttpRequest();
	var queryString = "?linkxpath=" + xpathToLink + "&id=" + tempID;
	alert(xpathToLink);
	//alert('querystring - '+ queryString);
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			//populate the select with the array generated on the PHP script. 
			//alert('Response - '+ ajaxRequest.responseText);
			var spanBox = document.getElementById('xpathStatusSpan');
			spanBox.innerHTML = " <span style='color:green'>Links Fetched</span>"+ ajaxRequest.responseText;
		}else{
			var spanBox = document.getElementById('xpathStatusSpan');
			spanBox.innerHTML = " <span style='color:red'>Fetching Links...</span>";
		}
	}
	ajaxRequest.open("GET", "scraper-gui-ajax-get-like-links.php" + queryString, true);
	ajaxRequest.send(null); 
}
//END AJAX FUNCTIONS

//clears the regex and xquery fields
function clearRegexAndXQuerySelects(){
	var userRegex = document.getElementById('userRegexChoice');
	var userXQuery = document.getElementById('userXQueryChoice');
	
	userRegex.options.length = 0;
	userRegex.options[0] = new Option('Select a Regex...', 'nothing');
	userXQuery.options.length = 0;
	userXQuery.options[0] = new Option('Select an XQuery...', 'nothing');
}

//creates a new xquery input
function createXQueryInput(){
	var newIn = document.createElement('input');
	var container = document.getElementById('xQueryDiv');
	var newLI = document.createElement('li');
	var newLabel = document.createElement('label');
	var xqueryArray = document.forms['adminform'].elements['userXQuery[]'];
	var xqueryCount = 0;
	var newButton = document.createElement('button');
	var newInputO = document.createElement('input');
	var newSpan = document.createElement('span');
	newButton.innerHTML = 'X';
	newButton.addEventListener('click', deleteXQueryInput, false);
	newButton.type = 'button';
	//get current number of xqueries
	if(xqueryArray == undefined)
		xqueryCount = 0;
	else if(xqueryArray.type == 'text')
		xqueryCount = 1;
	else
		xqueryCount = xqueryArray.length;
	xqueryCount++;
	//make label
	newLabel.htmlFor = 'userXQuery[]';
	newLabel.innerHTML = 'XQuery '+ xqueryCount;
	//make main xquery input
	newIn.type = 'text';
	newIn.size = '60';
	newIn.name = 'userXQuery[]';
	//setup radio buttons and options
	newInputO.type = 'text';
	newInputO.size = 1;
	newInputO.value = 1;
	newInputO.name = 'userXQueryPosition-'+xqueryCount;
	//hack in some text
	var tempHTML = "<span class='formElementBox' onclick='this.childNodes[0].checked=true'><input type='radio' value='text' name='userXQueryOptions-"+xqueryCount+"' />Text</span> <span class='formElementBox' onclick='this.childNodes[0].checked=true'><input type='radio' value='xml' name='userXQueryOptions-"+xqueryCount+"' checked/>XML</span>";
	//var tempHTML = 'test';
	//add stuff to dom
	newLI.appendChild(newLabel);
	newLI.appendChild(newIn);
	newLI.appendChild(newButton);
	newLI.appendChild(newInputO);
	//newLI.innerHTML = newLI.innerHTML + tempHTML;
	newSpan.innerHTML = tempHTML;
	newLI.appendChild(newSpan);
	
	
	container.appendChild(newLI);
	//alert('didntbreak');	
}

//creates a new xquery input
function createXQueryInputValue(theValue){
	//var theVal = document.forms['functionCaller'].elements['elementPathBox'].value;
	var newIn = document.createElement('input');
	var container = document.getElementById('xQueryDiv');
	var newLI = document.createElement('li');
	var newLabel = document.createElement('label');
	var xqueryArray = document.forms['adminform'].elements['userXQuery[]'];
	var xqueryCount = 0;
	var newButton = document.createElement('button');
	var newInputO = document.createElement('input');
	var newSpan = document.createElement('span');
	newButton.innerHTML = 'X';
	newButton.addEventListener('click', deleteXQueryInput, false);
	newButton.type = 'button';
	if(xqueryArray == undefined)
		xqueryCount = 0;
	else if(xqueryArray.type == 'text')
		xqueryCount = 1;
	else
		xqueryCount = xqueryArray.length;
	xqueryCount++;
	newLabel.innerHTML = 'XQuery '+ xqueryCount;
	newLabel.htmlFor = 'userXQuery[]';
	newIn.type = 'text';
	newIn.size = '60';
	newIn.value = theValue;
	newIn.name = 'userXQuery[]';
	//alert('middle');
	//setup radio buttons and options
	newInputO.type = 'text';
	newInputO.size = 1;
	newInputO.value = 1;
	newInputO.name = 'userXQueryPosition-'+xqueryCount;
	//hack in some text
	var tempHTML = "<span class='formElementBox' onclick='this.childNodes[0].checked=true'><input type='radio' value='text' name='userXQueryOptions-"+xqueryCount+"' />Text</span> <span class='formElementBox' onclick='this.childNodes[0].checked=true'><input type='radio' value='xml' name='userXQueryOptions-"+xqueryCount+"' checked/>XML</span>";
	//add stuff to dom
	newLI.appendChild(newLabel);
	newLI.appendChild(newIn);
	newLI.appendChild(newButton);
	newLI.appendChild(newInputO);
	newSpan.innerHTML = tempHTML;
	newLI.appendChild(newSpan);
	
	container.appendChild(newLI);
	//alert('didntbreak');	
}


//creates a new xquery input
function createXQueryInputValueFull(theValue, thePosition, theOption){
	//var theVal = document.forms['functionCaller'].elements['elementPathBox'].value;
	var newIn = document.createElement('input');
	var container = document.getElementById('xQueryDiv');
	var newLI = document.createElement('li');
	var newLabel = document.createElement('label');
	var xqueryArray = document.forms['adminform'].elements['userXQuery[]'];
	var xqueryCount = 0;
	var newButton = document.createElement('button');
	var newInputO = document.createElement('input');
	var newSpan = document.createElement('span');
	newButton.innerHTML = 'X';
	newButton.addEventListener('click', deleteXQueryInput, false);
	newButton.type = 'button';
	if(xqueryArray == undefined)
		xqueryCount = 0;
	else if(xqueryArray.type == 'text')
		xqueryCount = 1;
	else
		xqueryCount = xqueryArray.length;
	xqueryCount++;
	newLabel.innerHTML = 'XQuery '+ xqueryCount;
	newLabel.htmlFor = 'userXQuery[]';
	newIn.type = 'text';
	newIn.size = '60';
	newIn.value = theValue;
	newIn.name = 'userXQuery[]';
	//alert('middle');
	//setup radio buttons and options
	newInputO.type = 'text';
	newInputO.size = 1;
	newInputO.value = thePosition;
	newInputO.name = 'userXQueryPosition-'+xqueryCount;
	//hack in some text
	if(theOption == 'text'){
		var tempHTML = "<span class='formElementBox' onclick='this.childNodes[0].checked=true'><input type='radio' value='text' name='userXQueryOptions-"+xqueryCount+"' checked/>Text</span> <span class='formElementBox' onclick='this.childNodes[0].checked=true'><input type='radio' value='xml' name='userXQueryOptions-"+xqueryCount+"'/>XML</span>";
	}else{
		var tempHTML = "<span class='formElementBox' onclick='this.childNodes[0].checked=true'><input type='radio' value='text' name='userXQueryOptions-"+xqueryCount+"' />Text</span> <span class='formElementBox' onclick='this.childNodes[0].checked=true'><input type='radio' value='xml' name='userXQueryOptions-"+xqueryCount+"' checked/>XML</span>";
	}
	//add stuff to dom
	newLI.appendChild(newLabel);
	newLI.appendChild(newIn);
	newLI.appendChild(newButton);
	newLI.appendChild(newInputO);
	newSpan.innerHTML = tempHTML;
	newLI.appendChild(newSpan);
	
	container.appendChild(newLI);
	//alert('didntbreak');	
}

//deletes the xquery that had an onclick event
function deleteXQueryInput(e){
	var xQueryElement = e.target.parentNode;
	//get position
	var tempPosition = parseInt(xQueryElement.lastChild.lastChild.firstChild.name.split('-')[1]);
	var XQueryArray = document.forms['adminform'].elements['userXQuery[]'];
	//grab all xquery inputs
	for(var i = tempPosition+1; i <= XQueryArray.length; i++){		
		//adjust all nodes that are larger than tempPosition
		var tempFormElement = document.forms['adminform'].elements['userXQueryPosition-'+i];
		
		//var tempLI = tempFormElement.parentNode;
		
		var tempFieldset =  document.forms['adminform'].getElementsByTagName('fieldset')[2];
		var tempLI = tempFieldset.getElementsByTagName('li')[i+1];
		//label
		tempLI.firstChild.innerHTML = 'XQuery ' + (i-1);
		//position
		tempLI.childNodes[3].name = 'userXQueryPosition-'+ (i-1);
		//radio buttons		
		tempFieldset.getElementsByTagName('li')[i+1].getElementsByTagName('input')[2].name = 'userXQueryOptions-'+ (i-1);
		tempFieldset.getElementsByTagName('li')[i+1].getElementsByTagName('input')[3].name = 'userXQueryOptions-'+ (i-1);
	}
	
	//get parent
	var queryParent = xQueryElement.parentNode;
	//remove xQueryElement from parent
	queryParent.removeChild(xQueryElement);
}


//deletes the passed xquery input
function deleteXQueryInputGhetto(xQueryElement){
	xQueryElement = xQueryElement.parentNode;
	//get position
	var tempPosition = parseInt(xQueryElement.firstChild.innerHTML.split(' ')[1]);
	var XQueryArray = document.forms['adminform'].elements['userXQuery[]'];
	//grab all xquery inputs
	for(var i = tempPosition+1; i <= XQueryArray.length; i++){		
		//adjust all nodes that are larger than tempPosition
		//alert('changing query - '+ i + ', element:' + document.forms['adminform'].elements['userXQueryPosition-'+i].name);
			
		var tempFieldset =  document.forms['adminform'].getElementsByTagName('fieldset')[2];
		var tempLI = tempFieldset.getElementsByTagName('li')[i+1];
		//label
		tempLI.firstChild.innerHTML = 'XQuery ' + (i-1);
		//position
		tempLI.childNodes[3].name = 'userXQueryPosition-'+ (i-1);
		//radio buttons		
		tempFieldset.getElementsByTagName('li')[i+1].getElementsByTagName('input')[2].name = 'userXQueryOptions-'+ (i-1);
		tempFieldset.getElementsByTagName('li')[i+1].getElementsByTagName('input')[3].name = 'userXQueryOptions-'+ (i-1);
	}
	//get parent
	var queryParent = xQueryElement.parentNode;
	//remove xQueryElement from parent
	queryParent.removeChild(xQueryElement);
}

//grabs the information position and radio from the first xquery
function demoGrab(){
	alert('test');
	//updateReviewForm('Other');
	//alert('switching');
	updateReviewForm(1);

}


