<!--
var cp = new cpaint();
cp.set_transfer_mode('post');
cp.set_response_type('xml');
// lots of javascript alert boxes
// cp.set_debug(true);
// makes the ajax sychronous so one call has to finish before ANYTHING else can happen. cue browser hang...
// cp.set_async(false);

function logicSearchHandler(p_intQuestionNo){
	layerDisplay('processing_label','block');
	// can we get the first one done before we fire the rest?
	cp.set_async(false);
	reloadBox(p_intQuestionNo,true);
	cp.set_async(true);
	if(document.getElementById('step_by_step').value == 'True'){
		// show or hide question sets
		for(var q_cnt = 1; q_cnt <= p_intQuestionNo + 1; q_cnt++){
			layerDisplay('questionContainer'+(p_intQuestionNo+1),'block');
		}
		hideNext(q_cnt)
	}
	else{
	// need to refresh all the other boxes	
		reloadAllBoxes(p_intQuestionNo);
	}
	layerDisplay('processing_label','none');
}

function hideNext(p_idCount){
	if(document.getElementById('questionContainer' + p_idCount)){
		document.getElementById('QuestionAnswer' + p_idCount).selectedIndex = 0;
		layerDisplay('questionContainer' + p_idCount,'none');
		hideNext(++p_idCount);
	}
}

function reloadAllBoxes(p_intCurrentQuestion){
	//if the value is NULL then it is likely that a selection has been reset 
	//so we need to reload the current box
	var currentQuestion = document.getElementById('QuestionAnswer' + (p_intCurrentQuestion))
	var refreshCurrentQuestion = false;
	if(currentQuestion.options[currentQuestion.selectedIndex].text.indexOf('Select From') != -1)
	{
		refreshCurrentQuestion = true;
	}	
	for(var q_cnt = 1; q_cnt < document.getElementById('total_questions').value; q_cnt++){
		if(q_cnt != p_intCurrentQuestion && q_cnt != (p_intCurrentQuestion - 1) && !(p_intCurrentQuestion == 1 && q_cnt == document.getElementById('total_questions').value - 1)){
			if(document.getElementById('QuestionAnswer' + (q_cnt + 1))) document.getElementById('QuestionAnswer' + (q_cnt + 1)).disabled = true;
			reloadBox(q_cnt,false);
		}
	}
	if(refreshCurrentQuestion)
	{
		var boxToReload = (p_intCurrentQuestion == 1) ? document.getElementById('total_questions').value - 1: p_intCurrentQuestion - 1
		reloadBox(boxToReload, false);
	}
}

function reloadBox(p_intQuestionNo, p_blnSetCount){
	var collated_sql = '';
	var this_sql = '';
	
	try
	{
		// gather any sql collected so far
		for(var q_cnt = 1; q_cnt < document.getElementById('total_questions').value; q_cnt++){
			// showing one question at a time?
			this_sql = '';
			if(document.getElementById('step_by_step').value == 'True' && q_cnt < p_intQuestionNo){
				this_sql = document.getElementById('SQL'+q_cnt).value;
			}
			else if(q_cnt != p_intQuestionNo){
				this_sql = document.getElementById('SQL'+q_cnt).value;
			}
			if(!isBlank(this_sql)) collated_sql += this_sql += ' AND ';
			
		}
	//alert(p_intQuestionNo + collated_sql)
		if(document.getElementById('QuestionAnswer'+(p_intQuestionNo+1))){
			// call ajax function top populate the next box
			callPopulateDropdown(document.getElementById('DbField'+ p_intQuestionNo).value, document.getElementById('QuestionAnswer'+ p_intQuestionNo).value,document.getElementById('SelectionType'+ p_intQuestionNo).value, document.getElementById('CriteriaField'+ p_intQuestionNo).value, document.getElementById('CriteriaType'+ p_intQuestionNo).value, document.getElementById('CriteriaValue'+ p_intQuestionNo).value, document.getElementById('SQL'+ p_intQuestionNo).value, collated_sql, 'SQL'+p_intQuestionNo,document.getElementById('DbField'+ (p_intQuestionNo+1)).value,'QuestionAnswer'+(p_intQuestionNo+1),document.getElementById('QuestionAnswer'+ (p_intQuestionNo+1)).value,document.getElementById('SelectionType'+ (p_intQuestionNo+1)).value,document.getElementById('OptionLabelLookup'+ (p_intQuestionNo+1)).value,p_blnSetCount,document.getElementById('FixedOptions'+ (p_intQuestionNo+1)).value)
		}
		else if(document.getElementById('step_by_step').value != 'True'){
			// call ajax function to poulate the first box
			callPopulateDropdown(document.getElementById('DbField'+ p_intQuestionNo).value, document.getElementById('QuestionAnswer'+ p_intQuestionNo).value,document.getElementById('SelectionType'+ p_intQuestionNo).value, document.getElementById('CriteriaField'+ p_intQuestionNo).value, document.getElementById('CriteriaType'+ p_intQuestionNo).value, document.getElementById('CriteriaValue'+ p_intQuestionNo).value, document.getElementById('SQL'+ p_intQuestionNo).value, collated_sql, 'SQL'+p_intQuestionNo,document.getElementById('DbField1').value,'QuestionAnswer1',document.getElementById('QuestionAnswer1').value,document.getElementById('SelectionType1').value,document.getElementById('OptionLabelLookup1').value,p_blnSetCount,document.getElementById('FixedOptions').value)
		}
	}
	catch (exception) 
	{
		/*
		alert(exception);
		
		alert(p_intQuestionNo + 1);
		alert(document.getElementById('QuestionAnswer'+(p_intQuestionNo+1)));
		alert(!isNull(document.getElementById('QuestionAnswer'+(p_intQuestionNo+1))));
		alert(document.getElementById('QuestionAnswer'+(p_intQuestionNo+1)) == 'null')
		alert(document.getElementById('QuestionAnswer'+(p_intQuestionNo+1)) == null)
		*/
	}
}

// Ajax call

function callPopulateDropdown(p_strThisQuestionDbField,p_strQuestionAnswerValue,p_strQuestionSelectionType,p_strCriteriaField,p_strCriteriaType,p_strCriteriaValue,p_strThisQuestionSql,p_strExistingSql,p_strThisQuestionSqlField,p_strNextQuestionDbField,p_strNextQuestionId,p_strNextQuestionAnswer,p_strNextQuestionSelectionType,p_strNextQuestionOptionLabelLookup,p_blnSetCount,p_strFixedOptions){
	var fileToUse = '/ajax/logic_search_controller.asp';
	var searchType = document.getElementById('search_type').value;
	//alert(p_strThisQuestionSqlField);
	//alert(p_strNextQuestionSelectionType);
	cp.call(fileToUse, 'PopulateDropdown', questionHandler, searchType, p_strThisQuestionDbField, p_strQuestionAnswerValue, p_strQuestionSelectionType, p_strCriteriaField, p_strCriteriaType, p_strCriteriaValue, p_strThisQuestionSql, p_strExistingSql, p_strThisQuestionSqlField, p_strNextQuestionDbField, p_strNextQuestionId, p_strNextQuestionAnswer,p_strNextQuestionSelectionType,p_strNextQuestionOptionLabelLookup,p_blnSetCount,p_strFixedOptions);
}

// Handle return xml
function questionHandler(p_xmlResult){
	try
	{
		
		var xmlNodeList = p_xmlResult.getElementsByTagName('sqlfield');
		var hfsql = xmlNodeList[0].childNodes.item(0).data;
		xmlNodeList = p_xmlResult.getElementsByTagName('sql');
		var returned_sql = xmlNodeList[0].childNodes.item(0).data
		returned_sql = decodeCpaint(returned_sql)
		document.getElementById(hfsql).value = returned_sql;
		if(document.getElementById(hfsql).value == 'NULL') document.getElementById(hfsql).value = '';
		xmlNodeList = p_xmlResult.getElementsByTagName('all_sql');
		
		xmlNodeList = p_xmlResult.getElementsByTagName('count');
		if(xmlNodeList[0].childNodes.item(0).data != -1) document.getElementById('count_products').value = xmlNodeList[0].childNodes.item(0).data;
		
		xmlNodeList = p_xmlResult.getElementsByTagName('formfield');
		var dd = document.getElementById(xmlNodeList[0].childNodes.item(0).data);
		dd.options.length = 0;
		dd.disabled = false;
		xmlNodeList = p_xmlResult.getElementsByTagName('option');
		populateDDWithXml(dd,xmlNodeList);
		if(dd.options.selectedIndex == -1) dd.options.selectedIndex = 0;
		/**/
	}
	catch (exception) 
	{
		alert(exception);
		/*
		if(!hfsql == 'undefined'){
		//alert("Error: " + exception.message);
		//alert(hfsql);
		//alert(document.getElementById(hfsql).value);
		//alert(document.getElementById('count_products').value);
		//alert(dd.id);
		}
		else{
		//alert('ahem')
		}
		*/
	}
}	
//-->
