var currentTab = 1 
var qsSubmitted = false
function submitQS(f, b, c) {
	if(f.cboGenderID.selectedIndex > 0) { 
		f.cbxGenderID.value = f.cboGenderID.options[f.cboGenderID.selectedIndex].value
	}
	else {
		alert('You must select a gender to use the Quick Search feature.')
		return
	}
	if(qsSubmitted) { return }
	if(!vQS(f, c)) { return }
	qsSubmitted = true; b.value = 'Searching...'; b.disabled = true; f.submit()
}
function vQS(f, c) {
	if(c == 158) {
		if(vMi(f.intMiles) && vPo(f.strSelPostCode)) {
			if(f.intMiles.value != '(miles)' && f.strSelPostCode.value == '(post code)' || f.intMiles.value == '(miles)' && f.strSelPostCode.value != '(post code)') {
				alert('You must specify both \'miles\' and \'post code\' or neither.')
				if(f.intMiles.value == '(miles)') { var t = f.intMiles } else {	var t = f.strSelPostCode }
				t.select(); t.focus(); return false
			}
			else {
				if(f.intMiles.value == '(miles)') { f.intMiles.value = '' }
				if(f.strSelPostCode.value == '(post code)') { f.strSelPostCode.value = '' }
				return true
			}
		}
		else {
			return false
		}
	}
	else if(c == 196) {
		if(vMi(f.intMilesUSA) && vZ(f.strSelZipCode)) {
			if(f.intMilesUSA.value != '(miles)' && f.strSelZipCode.value == '(zip code)' || f.intMilesUSA.value == '(miles)' && f.strSelZipCode.value != '(zip code)') {
				alert('You must specify both \'miles\' and \'zip code\' or neither.')
				if(f.intMilesUSA.value == '(miles)') { var t = f.intMilesUSA } else {	var t = f.strSelZipCode }
				t.select(); t.focus(); return false
			}
			else {
				if(f.intMilesUSA.value == '(miles)') { f.intMilesUSA.value = '' }
				if(f.strSelZipCode.value == '(zip code)') { f.strSelZipCode.value = '' }
				return true
			}
		}
		else {
			return false
		}	
	}
	else {
		return true
	}
}
function fMi(t) { if(t.value == '(miles)') { t.value='' } else { t.select() } }
function vMi(t) {
	if(t.value != '(miles)') { 
		if(t.value.length == 0) {
			t.value = '(miles)'
		}
		else {
			var miles = parseInt(t.value, 10)
			if(isNaN(miles)) { 
				alert('You have not specified a valid \'miles\' value.'); t.select(); t.focus(); return false
			}
			else {
				t.value = miles
				if(miles < 1 || miles > 99) { alert('Miles must be between 1 and 99'); t.select(); t.focus(); return false }
			}
		}
	}
	return true
}
function fPo(t) { if(t.value == '(post code)') { t.value='' }	else { t.select() } }
function fZ(t) { if(t.value == '(zip code)') { t.value='' }	else { t.select() } }
function vPo(t) {
	if(t.value != '(post code)') { 
		if(t.value.length == 0) {
			t.value = '(post code)'
		}
		else {
			var postCode = t.value.toUpperCase()
			if(postCode.indexOf(' ') == 0) {
				postCode = postCode.substring(1, postCode.length)
				if(postCode.length == 0) { t.value = '(post code)'; return true }
			}
			t.value = postCode
			if(!isNaN(parseInt(postCode.substr(0, 1), 10))) { alert('You have not specified a valid post code.'); t.select(); t.focus(); return false }
			if(postCode.length < 2) { alert('You have not specified a valid post code.'); t.select(); t.focus(); return false }
			if(isNaN(parseInt(postCode.substr(1, 1), 10)) && isNaN(parseInt(postCode.substr(2, 1), 10))) { alert('You have not specified a valid post code.'); t.select(); t.focus(); return false }
		}
	}
	return true
}
function vZ(t) {
	if(t.value != '(zip code)') { 
		if(t.value.length == 0) { t.value = '(zip code)' }
		else if(t.value.length != 5) { alert('You have not specified a valid zip code, (5 digits only).'); t.select(); t.focus(); return false }
		else {
			var zipCode = t.value
			for (var i = 0; i < 5; i++) {if(isNaN(parseInt(zipCode.substr(i, 1), 10))) { alert('You have not specified a valid zip code.'); t.select(); t.focus(); return false }}
		}
	}
	return true
}
function switchTab(showTab) {
	if(showTab == currentTab) { return }
	var hideTab = currentTab
	var doIt = false
	if(document.all) {
		var dShow = document.all['tbl' + showTab + 'Content']
		var dShowLine = document.all['tbl' + hideTab + 'Line']
		var dShowLabel = document.all['td' + showTab + 'Label']
		var dHide = document.all['tbl' + hideTab + 'Content']
		var dHideLine = document.all['tbl' + showTab + 'Line']
		var dHideLabel = document.all['td' + hideTab + 'Label']
		doIt = true
	}
	
	else if (document.getElementById) {
		var dShow = document.getElementById('tbl' + showTab + 'Content')
		var dShowLine = document.getElementById('tbl' + hideTab + 'Line')
		var dShowLabel = document.getElementById('td' + showTab + 'Label')
		var dHide = document.getElementById('tbl' + hideTab + 'Content')
		var dHideLine = document.getElementById('tbl' + showTab + 'Line')
		var dHideLabel = document.getElementById('td' + hideTab + 'Label')
		doIt = true	
	}
	if(doIt) {
		dShow.className = 'Visible'
		dShowLine.className = 'Visible'
		dShowLabel.bgColor = '#FFFFFF'
		dHideLabel.bgColor = '#FEECE7'
		dHide.className = 'Hidden'
		dHideLine.className = 'Hidden'
		dShowLabel.className = 'Label'
		dHideLabel.className = ''
		dShowLabel.style.backgroundImage = 'url(facelift/tab_light_mid.gif)'; dHideLabel.style.backgroundImage = 'url(facelift/tab_dark_mid.gif)';
		if(document.images) {
			var hideImageChar = currentTab
			var transImageChar = showTab
			if(hideTab == '1') { var stopper1 = '_stopper' } else { var stopper1 = '' }; 
			if(showTab == '1') { var stopper2 = '_stopper' } else { var stopper2 = '' }; 
			document.images['LT' + transImageChar].src = 'facelift/tab_light_left' + stopper2 + '.gif'; 
			document.images['RT' + transImageChar].src = 'facelift/tab_light_right.gif'; 
			document.images['LT' + hideImageChar].src = 'facelift/tab_dark_left' + stopper1 + '.gif'; 
			document.images['RT' + hideImageChar].src = 'facelift/tab_dark_right.gif';
		}
	}
	else {
		alert('Your browser is too old to use this function.')
	}
	currentTab = showTab
}

/* -- Interview Quick Search Starts */
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 sQuestionIDs = '', sGroupedQuestionIDs = ''
var sPerson0, sPerson1, isTV
function showQuestions(g) {
	isTV = document.forms['frmQuickSearch'].cbxTVTS.checked ? true : false
	if(g != lG && g > 0 && aG.length) {
		for (var i = 0; i < aG.length; i++) {
			if(g == aG[i][eGenderID]) {
				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
	}
	if(sQuestionIDs.length > 0) { sQuestionIDs = left(sQuestionIDs, sQuestionIDs.length-1); document.forms['frmQuickSearch'].QuestionIDs.value = sQuestionIDs }
	if(sGroupedQuestionIDs.length > 0) { sGroupedQuestionIDs = left(sGroupedQuestionIDs, sGroupedQuestionIDs.length-1); document.forms['frmQuickSearch'].GroupedQuestionIDs.value = sGroupedQuestionIDs }
}
function writeQuestions(w, mf) {
	if(w==0) { var sPerson = sPerson0 } else { var sPerson = sPerson1 }
	var qS = '<table cellspacing="5" cellpadding="0"><tr><td colspan="2"><div style="border-bottom:1px dashed black;padding-bottom:3px"><em>' + sPerson + '\'s Details</em></div></td></tr>'
	qS += '<tr><td align="right">Age:</td><td><select size="1" name="cboAge' + w + '" style="width: 115px"><option value="(all)">(select)</option><option value="18-24">18-24</option><option value="25-30">25-30</option><option value="31-35">31-35</option><option value="36-40">36-40</option><option value="41-45">41-45</option><option value="46-55">46-55</option><option value="56\+">56\+</option></select></td></tr>'
	for (var i = 0; i < aQ.length; i++) {		
		if((mf == 'M' && aQ[i][eIsMale]) || (mf == 'F' && aQ[i][eIsFemale]) || isTV) {			
			qS += '<tr><td align="right" nowrap>' + aQ[i][eSearchLabel] + '</td><td>' + writeGroupedQuestionAnswer(aQ[i][eQuestionID], w) + writeAnswerInput(i, w) + '</td></tr>'
			sQuestionIDs += aQ[i][eQuestionID] + ','
		}
	}
	qS += '</table>'
	$('td' + w).innerHTML = qS
}
function writeAnswerInput(i, w) {
	var retStr = ''
	if(aQ[i][eOptions].length > 0) { var aO = aQ[i][eOptions].split('|'); var aO = aQ[i][eOptions].split('|') }
	retStr = '<select style="width: 115px" ' + (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>'
	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 
				sGroupedQuestionIDs += aQAG[i][eQAGQuestionID] + ','
			}
			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 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 left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
var bCO = false
function vCO() {
	if(bCO){
		$('tblContactOptions').className = 'Hidden'
		$('spanCO').innerHTML = 'Show contact options \+'
		bCO = false
	}
	else{
		$('tblContactOptions').className = ''
		$('spanCO').innerHTML = 'Hide contact options \-'
		bCO = true
	}
}

/* -- Interview Quick Search Ends */
