function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}
var eGenderID = 0, eGender = 1, eIsCouple = 2, eGenderPlural = 3, eMale = 4, eFemale = 5, eBinaryValue = 6
var eQAGQuestionID = 0, eQAGGroupLabel = 1, eQAGAnswers = 2, eQAGSequence = 3, eQAGGroupID = 4
var eSectionID = 0, eSectionSearchLabel = 1, eQuestionID = 2, eIsRadio = 3, eIsTextSingle = 4, eIsTextMulti = 5, eIsDropdownSingle = 6, eIsDropdownMulti = 7, eIsCheckbox = 8, eSearchLabel = 9, eOptions = 10, eOnOwnLine = 11, eRows = 12, eIsMale = 13, eIsFemale = 14
var sPerson0 = '', sPerson1 = ''
var isTV
function showQuestions(g) {
	isTV = document.forms['frmSearch'].cbxTVTS.checked ? true : false
	if((g != lG || isTV != lTV) && g > 0 && aG.length) {
		sPerson0 = '', sPerson1 = ''
		for (var i = 0; i < aG.length; i++) {
			if(g == aG[i][eGenderID]) {
				$('tblQuestions').className = ''
				$('tdGender').innerHTML = ''	
				if(aG[i][eIsCouple]) {setCouple(true);} else {setCouple(false);}				
				if(aG[i][eMale]) { if(aG[i][eIsCouple] && aG[i][eFemale] || !aG[i][eIsCouple] && !aG[i][eFemale]) { sPerson0 = 'Male' } else { sPerson0 = 'Male 1' }; writeQuestions('0', 'M'); } 
				else { if(aG[i][eIsCouple]) {sPerson0 = 'Female 1'} else {sPerson0 = 'Female'}; writeQuestions('0', 'F');}
				$('tbl0').className = ''
				if(aG[i][eIsCouple]) {
					if(aG[i][eFemale]) { if(aG[i][eMale]) {sPerson1 = 'Female'} else {sPerson1 = 'Female 2'}; writeQuestions('1', 'F');} 
					else { sPerson1 = 'Male 2'; writeQuestions('1', 'M');}
					$('tbl1').className = ''
				}
				else { $('tbl1').className = 'Hidden'}
				break;
			}
		}
		lG = g; lTV = isTV			
		$('AgeLabel1').innerHTML = '(' + sPerson0 + ')'
		if(sPerson1.length > 0) {$('AgeLabel2').innerHTML = '('+ sPerson1 + ')' } else {$('AgeLabel2').innerHTML = ''}
		$('OrientationLabel1').innerHTML = sPerson0
		$('OrientationLabel2').innerHTML = sPerson1		 
	}
}
function writeQuestions(w, mf) {
	if(w==0) { var sPerson = sPerson0 } else { var sPerson = sPerson1 }
	if(isTV){ sPerson += '&nbsp;(<em><span title="The TV\/TS flag is currently ON. \nQuestions here are therefore not gender specific." class="ToolTip">TV\/TS Search</span></em>)' }
	var qS = '<table cellspacing="0" cellpadding="0" style="margin-left:12px;margin-right:12px;margin-bottom:20px;"><tr><td colspan="2"><div class="dotted-box" style="margin-top:10px;">' + sPerson + '</div></td></tr>'
	var j = 0
	var prevSec = 0
	for (var i = 0; i < aQ.length; i++) {			
		if((mf == 'M' && aQ[i][eIsMale]) || (mf == 'F' && aQ[i][eIsFemale]) || isTV ) {				
			if(aQ[i][eSectionID] != prevSec) { qS += '<tr><td colspan="2"><img border="0" src="images/1px.gif" WIDTH="1" HEIGHT="12"></td></tr><tr><td class="PaddedLabel" colspan="2">' + aQ[i][eSectionSearchLabel] + '</td></tr><tr><th colspan="2"><img border="0" src="images/1px.gif" WIDTH="1" HEIGHT="1"></th></tr>'; j = 0 }
			qS += '<tr' + altRow(j) + '><td class="PaddedLabel">' + aQ[i][eSearchLabel] + '</td><td class="Padded">' + writeGroupedQuestionAnswer(aQ[i][eQuestionID], w) + writeAnswerInput(i, w) + '</td></tr>'
			prevSec = aQ[i][eSectionID]
			j++
		}
	}
	qS += '</table>'
	$('td' + w).innerHTML = qS
}
function showNoGender() {
	$('tblQuestions').className = ''
	$('tdGender').innerHTML = '<div class="dotted-box" style="margin:10px;font-weight:normal;">To conduct a search using the Interview, you must select the gender you are looking for within the <a href="javascript:switchTab(\'Profile\')">Profile tab</a>.</div>'
}
function setCouple(isCouple) {
	f = document.forms['frmSearch']
	if(isCouple) {
		f.intAgeFrom2.readonly = false
		f.intAgeTo2.readonly = false	
		f.intAgeFrom2.disabled = false
		f.intAgeTo2.disabled = false
	//	f.intAgeFrom2.style.backgroundColor = '#FFFFFF'
	//	f.intAgeTo2.style.backgroundColor = '#FFFFFF'
		for (var j=0; j < f.cbxOrientationID2.length; j++) {
		   f.cbxOrientationID2[j].disabled = false
		   f.cbxOrientationID2[j].readonly = false
		}		
	}
	else {
		f.intAgeFrom2.readonly = true
		f.intAgeTo2.readonly = true	
		f.intAgeFrom2.disabled = true
		f.intAgeTo2.disabled = true	
	//	f.intAgeFrom2.style.backgroundColor = '#D4D0C8'
	//	f.intAgeTo2.style.backgroundColor = '#D4D0C8'
		for (var j=0; j < f.cbxOrientationID2.length; j++) {
		   f.cbxOrientationID2[j].disabled = true
		   f.cbxOrientationID2[j].readonly = true
		}	
	}
}
function writeAnswerInput(i, w) {
	var retStr = ''
	if(aQ[i][eOptions].length > 0) { var aO = aQ[i][eOptions].split('|'); var aO = aQ[i][eOptions].split('|') }
	if(aQ[i][eIsRadio] || aQ[i][eIsCheckbox]) {
		if(aO.length > 0) {
			for (var j = 0; j < aO.length; j++) {
				l = 'ql' + w + i + j
				retStr += '<label for="' + l + '"><input type="' + (aQ[i][eIsCheckbox] ? 'checkbox' : 'radio') + '" id="' + l + '" name="question_' + w + '_' + aQ[i][eQuestionID] + '" value="' + aO[j] + '"' + isSelectedOrChecked(w + '_' + aQ[i][eQuestionID], aO[j], 'checked') + '>' + aO[j] + '</label>' + (aQ[i][eOnOwnLine] ? '<br>' : '&nbsp;')
			}
		}
	}
	else if(aQ[i][eIsDropdownSingle] || aQ[i][eIsDropdownMulti]) {
		retStr = '<select ' + (aQ[i][eIsDropdownMulti] ? 'multiple ' : '') + ' name="question_' + w + '_' + aQ[i][eQuestionID] + '" size="' + aQ[i][eRows] + '">'
		if(aO.length > 0) {
			for (var j = 0; j < aO.length; j++) {
				retStr += '<option value="' + aO[j] + '"' + isSelectedOrChecked(w + '_' + aQ[i][eQuestionID], aO[j], 'selected') + '>' + aO[j] + '</option>'
			}
		}		
		retStr += '</select>'
	}
	else if(aQ[i][eIsTextSingle]) {
		retStr = '<input type="text" name="question_' + w + '_' + aQ[i][eQuestionID] + '" size="30" maxlength="50" value="' + getTextAnswer(w + '_' + aQ[i][eQuestionID]) + '">'
	}
	return retStr
}
function writeGroupedQuestionAnswer(q, w) {
	if(!aQAG.length) { return '' }
	var writeStart = true, writeEnd = false
	var retStr = ''
	for(var i = 0; i < aQAG.length; i++) {
		if(aQAG[i][eQAGQuestionID] == q) {
			if(writeStart) { retStr += '<select name="GC_' + w + '_' + q + '" size="1"><option value="(select)">(select)</option>'; writeStart = false; writeEnd = true }
			retStr += '<option value="' + aQAG[i][eQAGGroupID] + '"'
			retStr += isGroupSelected(w + '_' + q, aQAG[i][eQAGGroupID])
			retStr += '>' + aQAG[i][eQAGGroupLabel] + '</option>'
		}
		else if(aQAG[i][eQAGQuestionID] > q) {
			break
		}
	}
	if(writeEnd) { retStr += '</select> or exactly ' }
	return retStr
}
function isGroupSelected(wq, ov) {
	var retStr = ''
	if(sGA.length > 0) {
		for (var i = 0; i < sGA.length; i++) {
			if(sGA[i][0] == wq && sGA[i][1] == ov) { retStr = ' selected'; break }
		}
	}
	return retStr
}
function getTextAnswer(wq) {
	var answer = ''
	if(sA.length > 0) { for(var i = 0; i < sA.length; i++) { if(sA[i][0] == wq) { answer = sA[i][1]; break } } }
	return answer
}
function isSelectedOrChecked(wq, o, s) {
	var retStr = ''
	if(sA.length > 0) { for(var i = 0; i < sA.length; i++) { if(sA[i][0] == wq && sA[i][1] == o) { retStr = ' ' + s; break } } }
	return retStr
}
function altRow(i) { if(i%2==0) { return ' class="altRow"' }}
function setTVTS(f, c) {
	for (var i=0; i < f.cbxGenderID.length; i++) {
		if (f.cbxGenderID[i].checked) {
			var hasGender = true; 
			showQuestions(f.cbxGenderID[i].value);
			break;
		}
	}
	if(c.checked && !hasGender) { 
		alert('You must also select a gender if you wish to search using the Interview') 
	}
}
function saveSearch(s, u) {
	var searchName = '', searchSaved = false, url = 'XMLDataSources/SaveSearch.asp'
	searchName = prompt('Please enter a name for your search:', searchName)
	if(searchName != null && searchName.length > 0) {
		url += '?SearchID=' + s + '&UserID=' + u + '&SearchName=' + encodeURIComponent(searchName)
		var req = g_getHTTPObject(); if (req) { req.open("GET", url, false); req.send(null) } else { return }
		var values = req.responseXML.getElementsByTagName("VALUE"), vL = values.length
		if(vL > 0) { searchSaved = parseInt(values.item(0).childNodes[0].nodeValue, 10) == 1} ; req = null
		if(searchSaved) { alert('Search saved.  Access it via the \"Search - Saved Searches\" Menu.') }
	}
}
