/*--------Search Functions ------------*/

function simpleSearch(form){
	url = form.action;
	newurl=url;
	var simpleValue = form['simple'].value;
	var simpleArray = new Array();
	simpleArray = simpleValue.split(' ');
	var simpleString;
	for(i=0; i<simpleArray.length; i++){
		if(i == 0){
			simpleString = '&q=' + simpleArray[0];
		} else {
			simpleString = simpleString + '+' + simpleArray[i];
		}
	}
	newurl=newurl + simpleString;
	location.href=newurl;
	return false;
}

function doSearch(form){
	url = form.action;
	newurl=url;
	var urlAdd = getCriteria(form);
	newurl=newurl + urlAdd + getSort(form);
	location.href=newurl;
	return false;
}

function getSort(form){
var sortClause='';
var sortItem = document.getElementById('sortkey').value;
if(sortItem!='score') {sortClause='&sort=' + sortItem + ' ' + document.getElementById('sortorder').value}
return(sortClause);
}

function getCriteria(form){
	//Final Strings for each criteria.
	var testString = '';
	var subjectString = '';
	var authorString = '';
	var titleString = '';
	var descriptionString = '';
	var relationString = '';
	var languageString = '';
	var identifierString = '';
	var genreString = '';
	var dateString = '';
	var collectionString = '';
	var fullQueryString = '';
	//Arrays containing values for each criteria type.
	var fullTextArray = new Array;
	var creatorArray = new Array;
	var subjectArray = new Array;
	var titleArray = new Array;
	var descriptionArray = new Array;
	var relationArray = new Array;
	var languageArray = new Array;
	var identifierArray = new Array;
	var genreArray = new Array;
	var dateArray = new Array;
	var collectionArray = new Array;
	//Gets values and adds to corresponding array.
	for(i=0; i<document.getElementById('searchAttributes').childNodes.length; i++){
		var temp = document.getElementById('searchAttributes').childNodes[i].id;
		if(typeof temp != 'undefined'){
			if(temp != undefined){
				if(temp != null){
					if(temp != ''){
						if(document.getElementById(temp).childNodes.length>1){
							var cellID = document.getElementById(temp).childNodes[1].id;
							var criteriaType = document.getElementById(temp).childNodes[0].childNodes[0].value;
							var boxID = document.getElementById(cellID).childNodes[0].id;
							var theValue = document.getElementById(boxID).value;
							if(theValue != '') {
							if(criteriaType == 'text'){fullTextArray.push(' +text:' + theValue);
							}
							if(criteriaType == 'creator'){creatorArray.push(' +name_t:' + theValue);
							}
							if(criteriaType == 'subject'){subjectArray.push(' +subject_t:' + theValue);
							}
							if(criteriaType == 'title'){titleArray.push(' +title_t:' + theValue);
							}
							if(criteriaType == 'description'){descriptionArray.push(' +note_t:' + theValue);
							}
							if(criteriaType == 'identifier'){identifierArray.push(' +id:' + theValue);
							}
							if(criteriaType == 'genre'){genreArray.push(' +genre_t:' + theValue);
							}
							if(criteriaType == 'date'){dateArray.push(' +raw_date:' + theValue);
							}
							if(criteriaType == 'collection'){dateArray.push(' +collection_t:' + theValue);
							}
							else {
							}
							}
						}
					}
				} else {}
			} else {}
		} else {
		}
	}
	textString = buildCriteriaString(fullTextArray);
	creatorString = buildCriteriaString(creatorArray);
	subjectString = buildCriteriaString(subjectArray);
	titleString = buildCriteriaString(titleArray);
	descriptionString = buildCriteriaString(descriptionArray);
	identifierString = buildCriteriaString(identifierArray);
	dateString = buildCriteriaString(dateArray);
	genreString = buildCriteriaString(genreArray);
	collectionString = buildCriteriaString(collectionArray);
	if(textString != '') {fullQueryString='q=' + textString;}
	if(titleString != '') {if(fullQueryString=='') {fullQueryString='q=' + titleString;} else {fullQueryString=fullQueryString + ' AND ' + titleString;}}
	if(dateString != '') {if(fullQueryString=='') {fullQueryString='q=' + dateString;} else {fullQueryString=fullQueryString + ' AND ' + dateString;}}

	if(creatorString != '') {if(fullQueryString=='') {fullQueryString='q=' + creatorString;} else {fullQueryString=fullQueryString + ' AND ' + creatorString;}}
	if(subjectString != '') {if(fullQueryString=='') {fullQueryString='q=' + subjectString;} else {fullQueryString=fullQueryString + ' AND ' + subjectString;}}
	if(descriptionString != '') {if(fullQueryString=='') {fullQueryString='q=' + descriptionString;} else {fullQueryString=fullQueryString + ' AND ' + descriptionString;}}
	if(identifierString != '') {if(fullQueryString=='') {fullQueryString='q=' + identifierString;} else {fullQueryString=fullQueryString + ' AND ' + identifierString;}}
	if(genreString != '') {if(fullQueryString=='') {fullQueryString='q=' + genreString;} else {fullQueryString=fullQueryString + ' AND ' + genreString;}}
	if(collectionString != '') {if(fullQueryString=='') {fullQueryString='q=' + collectionString;} else {fullQueryString=fullQueryString + ' AND ' + collectionString;}}

	return(fullQueryString);
}

function buildCriteriaString(contentArray){
	var textString ='';
	if(typeof contentArray != 'undefined'){
		if(contentArray != undefined){
			if(contentArray != null){
				if(contentArray != ''){
					for(i=0; i<contentArray.length; i++){
						if(i==0){
							textString = contentArray[i];
						} else {
							textString = textString + ' AND ' + contentArray[i];
						}
					}
				}
			}
		}
		
	}
	return textString;
}





/*------------ Display Functions ------------------*/

var criteriaArray = new Array();

function adjustForJS(id, newclass){//Progressive Enhancement.
	document.getElementById(id).className=newclass;
}

function addExpandLink(id){//Builds button to expand result.
	var buttonID = id+'button';
	var buttonText = 'view full result';
	var resultXName = id+'x';
	
	var idCode = "'"+buttonID+"' ";
	var onclickCode = "scaleResult("+"'"+id+"'"+")";
	var valueCode = "'"+buttonText+"' ";
	var codeStringX = "<input type='button' class='expander' id="+"'"+buttonID+"' value="+"'"+buttonText+"' "+onclickCode+"/>"
	var codeString = "<input type='button' class='expander' "+ "id="+idCode + "value="+valueCode + "onclick="+onclickCode;
	document.writeln(codeString)
}

function scaleResult(id){//Scales result when user clicks button.
	var buttonID = id+'button';
	var resultXName = id+'x';
	var currentClass = document.getElementById(resultXName).className;
	
	if(currentClass == 'resultsBoxExpanded'){
		document.getElementById(resultXName).className='resultsBoxExpanded_vis';
		document.getElementById(buttonID).value = 'view result summary';
	}
	else
	{
		document.getElementById(resultXName).className='resultsBoxExpanded';
		document.getElementById(buttonID).value = 'view full result';
	}
}

function addCriteria(){
	var newNumber = (document.getElementById('criteriaNumber').value -0)+ 1;//Number used in unique identification of elements
	criteriaArray.push(newNumber);
	
	document.getElementById('criteriaNumber').value = newNumber;//Sets number in hidden form.
	//Label Selection
	var label = 'Full Text';
	var newInputName = "text_" + newNumber;
	var changeParams = 'value, ' + '\'' + newInputName + '\'';
	var changeCode = '\"alertChange(' + changeParams +')\"';
	var optionsCode = '<option value="text">Anywhere</option><option value="title">Title</option><option value="date">Date</option><option value="subject">Subject</option><option value="genre">Genre</option><option value="description">Note</option><option value="collection">Collection</option><option value="identifier">ID</option></select>'
	//Label Code
	var labelPickCode = '<select id="criteriaType"' + 'onChange=' + changeCode+' class="labelPick">' + optionsCode;


	//Input Field
	var inputCode = '<input type="text" name=\"' + newInputName + '\" id=\"' + newInputName + '\" value=""/>';

	//Button that removes criteria from list
	if(newNumber == 0){
		var subtractButton = '&nbsp;'
	} else {
		var subtractButton = '<input type="button" onClick="removeCriteria(\''+newNumber+'\')" value="&ndash;"/>'
	}
	
	var codeArray = new Array(labelPickCode, inputCode, subtractButton);
	
	//Creates new TR and adds to DOM
	var rowID = 'row_' + newNumber;
	var newRow = document.createElement('tr');
	newRow.setAttribute('id', rowID);
	document.getElementById('searchAttributes').appendChild(newRow);
	
	for(var i=0; i < codeArray.length; i++){
		buildCriteriaRow(i, codeArray[i], rowID);
	}
	
}

function buildCriteriaRow(cellNumber, code, rowID){
	var styleArray = new Array("labelPick", "inputStyle", "removeButtonStyle");
	var newCellID = 'cell_'+rowID + '_' + cellNumber;
	var newCell = document.createElement('td');
	newCell.className = styleArray[cellNumber];
	newCell.innerHTML = code;
	newCell.setAttribute('id', newCellID);
	document.getElementById(rowID).appendChild(newCell);
}

function addCriteriaRow(cellNumber, code, rowID){
	var styleArray = new Array("labelPick", "inputStyle", "removeButtonStyle");
	var cellID = 'cell_'+rowID + '_' + cellNumber;
	document.getElementById(cellID).innerHTML = code;
}

function alertChange(selected, alter){
	var newStem = alter.split('_');
	var inputName = selected;
	var newName = inputName + '_' + newStem[1];
	document.getElementById(alter).name = newName;
}

function checkPlacement(id){
	for(i=0; i<criteriaArray.length; i++){
		if(id == criteriaArray[i]){
			return(i);
		} else {
		}
	}
}

function removeCriteria(id){
	var myPlace = checkPlacement(id);
	var criteriaLength = criteriaArray.length-1;
	var criteriaStart = criteriaArray[0];
	var andOrCode_invis = '&nbsp;';	

	criteriaArray.splice(id, '1');
	var parentElement = document.getElementById('searchAttributes');
	var goneElementName = 'row_' + id;
	var goneElement = document.getElementById(goneElementName);
	var goneElementSort = 'order_' + id;	

	parentElement.removeChild(goneElement);
}


