function fIsTabEnabled(toTab)
{
	var llEnabled = false;

	var lcPageID = toTab.id.substr(3);
	
	var loPage = document.getElementById('page'+lcPageID);
	if(loPage == null)
		return false;
	
	var loItem;
	for(var i=0; i<loPage.childNodes.length; i++)
	{
		loItem = loPage.childNodes.item(i);
		if(loItem.tagName == null)
			continue;
		if(loItem.tagName.toLowerCase() != 'fieldset')
			continue;
		if(loItem.className.substr(0,11) != 'divQuestion')
			continue;
		
		if(loItem.className != 'divQuestionGrey')
		{
			llEnabled = true;
			break;
		}
	}
	return llEnabled;
}

function fShowPage(tcID)
{
	var loPTC = document.getElementById('pagetabcontainer');
	
	var loTabs = loPTC.childNodes;
	if(loTabs == null)
		return false;
	if(loTabs.length < 1)
		return false;
	
	var loTab;
	for(var i=0; i<loTabs.length; i++)
	{
		loTab = loTabs.item(i);
		
		if(loTab.tagName == null)
			continue;
		if(loTab.id.substr(0,3) != "tab")
			continue;
			
		if(fIsTabEnabled(loTab))
		{
			if(loTab.id == 'tab'+tcID)
				loTab.className = 'pagetabSel';
			else
				loTab.className = 'pagetab';
		}
		else
		{
//alert(loTab.id);
			if(loTab.id == 'tab'+tcID)
				loTab.className = 'pagetabGreySel';
			else
				loTab.className = 'pagetabGrey';
		}
	}
	
  
	var loPC = document.getElementById('pagecontainer');
	
	var loPages = loPC.childNodes;
	if(loPages == null)
		return false;
	if(loPages.length < 1)
		return false;
		
	var loPage;
	for(var i=0; i<loPages.length; i++)
	{
		loPage = loPages.item(i);
		
		if(loPage.tagName == null)
			continue;
		if(loPage.tagName.toLowerCase() != 'div')
			continue;
			
		if(loPage.id == 'page'+tcID)
			loPage.style.display = 'block';
		else
			loPage.style.display = 'none';
	}
	  
	return true;
}

function fGetInputValue(tcID)
{
	var loQuestions = document.getElementById('oQuestions');
	var loRoot = loQuestions.XMLDocument.selectSingleNode("/vragen");
	if(loRoot == null)
		return "";
		
	var loQuestion = loRoot.selectSingleNode("//vraag[@id='"+tcID+"']");
	if(loQuestion == null)
		return "";
	
	var loType = loQuestion.selectSingleNode("type");
	if(loType == null)
		return "";

	var lcHtmlVal = "";
//	alert(tcID + " : " + loType.text)
	switch(loType.text)
	{
		case "check":
			var loHtmlQ = document.getElementById('inp_'+tcID);
			if(loHtmlQ == null)
				return "";
			lcHtmlVal = loHtmlQ.checked ? 'on' : 'off';
			break;
		
		case "text":
			var loHtmlQ = document.getElementById('inp_'+tcID);
			if(loHtmlQ == null)
				return "";
			lcHtmlVal = loHtmlQ.value == "" ? 'filled' : 'empty';
			break;

		case "select":
			var loHtmlQ = document.getElementById('inp_'+tcID);
			if(loHtmlQ == null)
				return "";
			lcHtmlVal = loHtmlQ.options[loHtmlQ.selectedIndex].value;
			break;

		case "radio":
			var loOptions = loQuestion.selectNodes("choices/choice");
			for(var i=0; i<loOptions.length; i++)
			{
				var loHtmlQ = document.getElementById('inp_' + tcID + "_" +loOptions.item(i).getAttribute("value"));
				if(loHtmlQ != null)
				{
					if(loHtmlQ.checked)
					{
						lcHtmlVal = loHtmlQ.value;
						break;
					}
				}
			}
			break;
	}
	return lcHtmlVal;

/**
	var loHtmlQ = document.getElementById('inp_'+tcID);
	if(loHtmlQ == null)
		return "";

	var lcHtmlVal = loHtmlQ.value;
	switch(loHtmlQ.type)
	{
		case "checkbox":
			lcHtmlVal = loHtmlQ.checked ? 'on' : 'off';
			break;
		case "text":
			lcHtmlVal = loHtmlQ.value == "" ? 'filled' : 'empty';
			break;
	}
**/
	return lcHtmlVal;
}

function fInList(tcList, tcVal)
{
	var llInList = false;
	
	if(tcList == "" && tcVal == "")
		return true;
	
	var taList = tcList.split(',');
	for(var i=0; i<taList.length; i++)
	{
		if(tcVal == taList[i])
		{
			llInList = true;
			break;
		}
	}

	return llInList;
}

function fCheckDeps(toQuestion)
{
	var llDepOK = true;
	
	var loDep;
	var lcDepID, lcDepVal, lcDepReqVals;
	
	var loDeps = toQuestion.selectNodes('depends/depend');
	for(var j=0; j<loDeps.length; j++)
	{
		loDep = loDeps.item(j);
//alert(loDep.xml);
		lcDepID = loDep.getAttribute('refid');
		lcDepVal = fGetInputValue(lcDepID);
		lcDepReqVals = loDep.text;
//alert(lcDepReqVal +'=?='+ lcDepVal );
	  
		if(!fInList(lcDepReqVals,lcDepVal))
		{
			llDepOK = false;
			break;
		}
	}

	return llDepOK;
}

function fEnableInput(tcID, tlEnable)
{
	var loQuestions = document.getElementById('oQuestions');
	var loRoot = loQuestions.XMLDocument.selectSingleNode("/vragen");
	if(loRoot == null)
		return "";
		
	var loQuestion = loRoot.selectSingleNode("//vraag[@id='"+tcID+"']");
	if(loQuestion == null)
		return "";
	
	var loType = loQuestion.selectSingleNode("type");
	if(loType == null)
		return "";

	switch(loType.text)
	{
		case "check":
			var loHtmlQ = document.getElementById('inp_'+tcID);
			if(loHtmlQ == null)
				return "";
			loHtmlQ.disabled = ! tlEnable;
			break;
		
		case "text":
			var loHtmlQ = document.getElementById('inp_'+tcID);
			if(loHtmlQ == null)
				return "";
			loHtmlQ.disabled = ! tlEnable;
			break;

		case "select":
			var loHtmlQ = document.getElementById('inp_'+tcID);
			if(loHtmlQ == null)
				return "";
			loHtmlQ.disabled = ! tlEnable;
			break;

		case "radio":
			var loOptions = loQuestion.selectNodes("choices/choice");
			for(var i=0; i<loOptions.length; i++)
			{
				var loHtmlQ = document.getElementById('inp_' + tcID + "_" +loOptions.item(i).getAttribute("value"));
				if(loHtmlQ != null)
				{
					loHtmlQ.disabled = ! tlEnable
				}
			}
			break;
	}




return true




//alert(tlEnable);
	var loInput = document.getElementById('inp_'+tcID);
	if(loInput == null)
		return false;
		
	loInput.disabled = tlEnable ? false : true;
//alert(loInput.id +' / '+tlEnable+' / '+loInput.disabled);
	
	return true;
}

function fRecalcDepends(tcID)
{
	var loQuestions = document.getElementById('oQuestions');
	var loRoot = loQuestions.XMLDocument.selectSingleNode("/vragen");
//alert('oNode = ' + loRoot.xml);
	if(loRoot == null)
		return false;
//alert(tcID);

	var loQuestion;
	var lcQID;
	var loDivQ;
	var llIsSub;
	
	var loDepQs = loRoot.selectNodes('//vraag[depends/depend/@refid="'+tcID+'"]');
	for(var i=0; i<loDepQs.length; i++)
	{
		loQuestion = loDepQs.item(i);
//alert(loQuestion.xml);
		
		lcQID = loQuestion.getAttribute('id');
		loDivQ = document.getElementById('div_'+lcQID);
//alert(lcQID);

		llIsSub = loQuestion.parentNode.tagName == 'vraag';

		if(llIsSub)
		{
			loDivQ.style.display = fCheckDeps(loQuestion) ? 'block' : 'none';
		}
		else
		{
			if(fCheckDeps(loQuestion))
			{
				loDivQ.className = 'divQuestion';
//alert('enabling..');
				fEnableInput(lcQID,true);
			}
			else
			{
				loDivQ.className = 'divQuestionGrey';
//alert('disabling.. ' + lcQID);
				fEnableInput(lcQID,false);
			}
		}
	}

	return true;
}

function fSetPrice(tcID)
{
//alert(tcID);
	var loQuestions = document.getElementById('oQuestions');
	var lcType = fGetNodeValue(loQuestions,'//vraag[@id="'+tcID+'"]/type');
	
	if(lcType == "select")
	{
		var loPricePlace = document.getElementById('prc_'+tcID);
		if(loPricePlace == null)
			return false;
		var loInput = document.getElementById('inp_'+tcID);
		loPricePlace.innerHTML = fGetNodeValue(loQuestions,'//vraag[@id="'+tcID+'"]/choices/choice[.="'+loInput.value+'"]/@price');
	}
}

function fParseFloat(c)
{
	var f = parseFloat(c, 10);
	if(isNaN(f))
		return 0.0;
	return f;
}

function fRecalcPrices(tcID)
{
	var loQuestions = document.getElementById('oQuestions');
	var loRoot = loQuestions.XMLDocument.selectSingleNode("/vragen");
	if(loRoot == null)
		return false;
		
	var liTotalAmount, liPrice;
	var liTotalSubscription, liSubscription, liMinSubscription;
	var loQuestion;
	var lcQID, lcQType, lcInpVal, lcMS;
	
	var liBasePrice = fParseFloat(fGetNodeValue(loRoot,'/vragen/@baseprice'));
	liTotalAmount = liBasePrice;
	
	var liBaseSubscription = fParseFloat(fGetNodeValue(loRoot,'/vragen/@basesubscription'));
	liTotalSubscription = liBaseSubscription;

	var liMinSubscription = fParseFloat(fGetNodeValue(loRoot,'/vragen/@minsubscription'));
	
//alert("BaseSub="+liBaseSubscription+", MinSub="+liMinSubscription+", sub="+liTotalSubscription);	
	var loQs = loRoot.selectNodes('//vraag');
	for(var i=0; i<loQs.length; i++)
	{
		loQuestion = loQs.item(i);
		liPrice = "";
		liSubscription = "";
		
		if(fCheckDeps(loQuestion))
		{
			lcQID = fGetNodeValue(loQuestion,'@id');
			lcQType = fGetNodeValue(loQuestion,'type');
			
			switch(lcQType)
			{
				case "check":
					if(fGetInputValue(lcQID) == 'on')
					{
						liPrice = fGetNodeValue(loQuestion,'price');
						liSubscription = fGetNodeValue(loQuestion,'subscriptionprice');
//alert("1: BaseSub="+liBaseSubscription+", MinSub="+liMinSubscription+", sub="+liTotalSubscription);	
					}
					break;
					
				case "select":
					lcInpVal = fGetInputValue(lcQID);
					liPrice = fGetNodeValue(loQuestion,'choices/choice[.="'+lcInpVal+'"]/@price');
					liSubscription = fGetNodeValue(loQuestion,'choices/choice[.="'+lcInpVal+'"]/@subscriptionprice');
					if(fGetNodeValue(loQuestion,'pricetype') == 'base')
					{
						liBasePrice += parseFloat(liPrice);
						lcMS = fGetNodeValue(loQuestion,'choices/choice[.="'+lcInpVal+'"]/@subscriptionprice');
						if(lcMS != "")
							liMinSubscription = fParseFloat(lcMS);
//alert("2: BaseSub="+liBaseSubscription+", MinSub="+liMinSubscription+", sub="+liTotalSubscription);	
					}
					break;

				case "radio":
					lcInpVal = fGetInputValue(lcQID);
					liPrice = fGetNodeValue(loQuestion,'choices/choice[@value="'+lcInpVal+'"]/@price');
					liSubscription = fGetNodeValue(loQuestion,'choices/choice[@value="'+lcInpVal+'"]/@subscriptionprice');
					if(fGetNodeValue(loQuestion,'pricetype') == 'base')
					{
						liBasePrice += parseFloat(liPrice);
						lcMS = fGetNodeValue(loQuestion,'choices/choice[@value="'+lcInpVal+'"]/@minsubscriptionprice');
						if(lcMS != "")
							liMinSubscription = fParseFloat(lcMS);
//alert("3: lcQID = "+lcQID+", BaseSub="+liBaseSubscription+", MinSub="+liMinSubscription+", sub="+liTotalSubscription);	
					}
					break;
			}
		}
		
		if(liPrice != "")
		{
			liPrice = parseFloat(liPrice,10);
			liTotalAmount += liPrice;
		}
		
		if(liSubscription != "")
		{
//alert("4: BaseSub="+liBaseSubscription+", MinSub="+liMinSubscription+", sub="+liSubscription + ", total="+liTotalSubscription);	
			liSubscription = parseFloat(liSubscription,10);
			liTotalSubscription += liSubscription;
		}
	}
		
	if(!isNaN(liMinSubscription))
		if(liTotalSubscription < liMinSubscription)
			liTotalSubscription = liMinSubscription;
			
	//var loBasePrice = document.getElementById("BasePrice");
	//loBasePrice.innerHTML = String(liBasePrice)+',-';
	
	var loInpBasePrice = document.getElementById("inp_BasePrice");
	if(loInpBasePrice != null)
		loInpBasePrice.value = liBasePrice;

	var loTotalAmount = document.getElementById("PriceTag");
	loTotalAmount.innerHTML = String(liTotalAmount)+',-';
	
	var loInpTotalAmount = document.getElementById("inp_TotalAmount");
	if(loInpTotalAmount != null)
		loInpTotalAmount.value = liTotalAmount;
		
		
		/***/

	//var loBaseSubscription = document.getElementById("BaseSubscription");
	//loBaseSubscription.innerHTML = String(liBaseSubscription)+',-';
	
	var loInpBaseSubscription = document.getElementById("inp_BaseSubscription");
	if(loInpBaseSubscription != null)
		loInpBaseSubscription.value = liBaseSubscription;

	var loTotalSubscription = document.getElementById("Subscription");
	loTotalSubscription.innerHTML = String(liTotalSubscription)+',-';
	
	var loInpTotalSubscription = document.getElementById("inp_TotalSubscription");
	if(loInpTotalSubscription != null)
		loInpTotalSubscription.value = liTotalSubscription;
		/***/
}

function fRefreshTabs()
{
	var loPTC = document.getElementById('pagetabcontainer');
	if(loPTC == null)
		return false;
		
	var loTab, loTabs;
	var lcPageID;
	
	loTabs = loPTC.childNodes;
	for(var i=0; i<loTabs.length; i++)
	{
		loTab = loTabs.item(i);
		if(loTab.id.substr(0,3) != 'tab')
			continue;
		if(fIsTabEnabled(loTab))
		{
			if(loTab.className != 'pagetabSel')
			{
				loTab.className = 'pagetab';
			}
		}
		else
		{
			loTab.className = 'pagetabGrey';
		}
	}
}

function fRecalcAll(tcID)
{
	fRecalcDepends(tcID);
	fSetPrice(tcID);
	fRecalcPrices(tcID);
	fRefreshTabs();
}

function submitform(oField)
{
	oAction = oField.form.txtAction;
	if(oAction != null)
	{
		oAction.value = oField.name;
		oField.form.submit();
	}
}