 function updateAns() {
 	var currAns = new Array();
 	var tmp, tmp1;
 	var re = /id_/;
 	var noAns = 0;
 	var validPrns = new Array();
 	var sw;
 	var el = document.forms.answer.elements;
 	for (var i=0;i<el.length;i++) {
 		if (el[i].checked && el[i].type.toLowerCase()=='checkbox') {
 			tmp = el[i].id;
 			tmp = tmp.replace(re, '');
 			currAns[tmp] = 1;
 			noAns++;
 		}
 	}

 	if (currAns.length == 0) {
 		for (id_ans in a) {
 			for (id_prn in a[id_ans]) {
 				validPrns[id_prn] = noAns;
 			}
 		}
 	} else {
 		for (id_ans in a) {
 			if (currAns[id_ans]) {
 				for (id_prn in a[id_ans]) {
 					if (!validPrns[id_prn]) {
 						validPrns[id_prn] = 0;
 					}
 					validPrns[id_prn]++;
 				}
 			}
 		}
 	}

 	for (var i=0;i<el.length;i++) {
 		if (!el[i].checked) {
 			tmp = el[i].id;
 			tmp = tmp.replace(re, '');
 			sw = 2;
 			if (parseInt(tmp) != tmp) {
 				continue;
 			}

 			for (id_prn in a[tmp]) {
 				if (validPrns[id_prn] >= noAns) {
 					sw = 1;
 					break;
 				}
 			}

 			el[i].sw = sw;
 			if (sw == 2) {
 		
 				el[i].disabled = true;
 			} else {
 
 				el[i].disabled = false;
 			}
 			if (document.getElementById) {
 				tmp1 = document.getElementById("label_" + tmp);
 				if(tmp1){
 					if (sw == 2) {
 						tmp1.className = 'disabled';
 					} else {
 						tmp1.className = 'normal';
 					}
 				}	
 			}
 		}
 	}
 }
 
  function absTop(el) {
     return (el.offsetParent)? 
     el.offsetTop+absTop(el.offsetParent) : el.offsetTop;
  }
  
  function absLeft(el) {
     return (el.offsetParent)? 
     el.offsetLeft+absLeft(el.offsetParent) : el.offsetLeft;
  }
 
 function showProductsInfo(el, name, desc)
 {
	img = new Image();
	img.src = 'img/arrowBox.gif';
	
	arrow = document.createElement('img');
	arrow.setAttribute('src', 'img/arrowBox.gif');
	arrow.id = 'arrowBox';
	
	nameTxt = document.createTextNode(name);
	descTxt = document.createTextNode(desc);
	
	strong = document.createElement('strong');
	strong.appendChild(nameTxt);

	head = document.createElement('p');
	head.className = 'head';
	head.appendChild(strong);
	
	body = document.createElement('p');
	body.className = 'body';
	body.appendChild(descTxt);

	elLeft = parseInt(absLeft(el));
	elTop = parseInt(absTop(el));

	infoLeft = (elLeft-230)+'px';
	infoTop = (elTop-10)+'px';

	$('productsInfo').style.position = 'absolute';
	$('productsInfo').style.left = infoLeft;
	$('productsInfo').style.top = infoTop;
	
	
	$('productsInfo').appendChild(arrow);
	$('productsInfo').appendChild(head);
	$('productsInfo').appendChild(body);
	$('productsInfo').show();
 }
 
 function hideProductsInfo()
 {
 	while($('productsInfo').firstChild) {
		$('productsInfo').removeChild($('productsInfo').firstChild);
	}
	$('productsInfo').hide();
 }
 
 function updateProducts()
 {
 	var answers = '';
 	var form = document.forms.answer.elements;
 	var parameter = {};
 	var currentStep = nextStep-2;
 	
	$('productsInfo').hide();
	
 	for (var i=0;i<form.length;i++) {
 		type = form[i].type.toLowerCase();
 		if(type == 'checkbox' || type == 'radio') {
 			answerId = form[i].id.replace('id_', '');
 			answerValue = form[i].value;
 			if(answerValue.indexOf(answerId+'|') == 0) {
 				answerValue = answerValue.replace(answerId+'|', '');
 			}
 			newAnswer = 'answers['+currentStep+']['+answerId+']='+answerValue;
 			if(oldAnswers.indexOf(newAnswer) >= 0) {
 				oldAnswers = oldAnswers.replace(newAnswer, '');
 			}
 			if(form[i].checked) {
 				answers = answers+newAnswer+'&';
 			}
 		}			
 	}
 	
 	answers = answers.substr(0, (answers.length-1));
 	if(oldAnswers.length > 0) {
 		if(answers.length > 0) {
 			answers = answers+'&'+oldAnswers;
 		} else {
 			answers = oldAnswers;
 		}
 	}
	Element.hide('products');
	Element.show('loading');
 	new Ajax.Request('get_products.php',
					 {
					  	method :'post',
						postBody : answers,
						onComplete : function(req) {
							Element.hide('loading');
							$('products').innerHTML = req.responseText;
							Element.show('products');
							
							var productsCount = 0;
							for(i=0; i<$('productsPreview').childNodes.length; i++) {
								if($('productsPreview').childNodes[i].nodeType == 1) {
									productsCount++;
								}
							}
							if(productsCount == 2) {
								$('nextLink').hide();
								$('finishLink').show();
							} else {
								$('finishLink').hide();
								$('nextLink').show();
							}
						}
					 });
 	
 }
 
 function canClick(o){
 	if (!o.checked) {
 		window.itemsChanged = true;
 		updateAns();
 		updateProducts();
 		return true;
 	}
 	
 	if (o.sw == 2 && errMsg) {
 		return false;
 	}

 	if (o.sw == 1) {
 		window.itemsChanged = true;
 		updateAns();
 		updateProducts();
 		return true;
 	} else {
 		if (checkChecked()>1) {
 			return false;
 		} else {
 			window.itemsChanged = true;
 			updateAns();
 			updateProducts();
 			return true;
 		}
 	}
 }
 
 function checkSubmit(){
 	if (document.forms.answer.elements.radioanswers) {
 		if(!document.forms.answer.elements.radioanswers.length) {
 		    if (document.forms.answer.elements.radioanswers.checked) {
 				return true;
 			}
 		} else {
 		  for (i=0;i<document.forms.answer.elements.radioanswers.length;i++) {
 			if (document.forms.answer.elements.radioanswers[i].checked) {
 				return true;
 			}
 		  }
 		}
 	} else {
 		for (i=0;i<document.forms.answer.elements.length;i++) {
 			if (document.forms.answer.elements[i].checked) {
 				return true;
 			}
 		}
 	}
 	alert(strLRI5);
 	return false;
 }
 
 function checkChecked(){
 	ret = 0;
 	if (document.forms.answer.elements.radioanswers) {
 		for (i=0;i<document.forms.answer.elements.radioanswers.length;i++) {
 			if (document.forms.answer.elements.radioanswers[i].checked) {
 				ret++;
 			}
 		}
 	} else {
 		for (i=0;i<document.forms.answer.elements.length;i++) {
 			if (document.forms.answer.elements[i].checked) {
 				ret++;
 			}
 		}
 	}
 	return ret;
 }
 
 function clickedNext() {
 	if (checkSubmit()){
 		return clickedNumber(nextStep, noQuestions);
 	} else {
 		return false;
 	}
 }
 
 function clickedFinish() {
 	if (checkSubmit()){
 		return clickedNumber('Finish', noQuestions);
 	} else {
 		return false;
 	}
 }
 
    var backSelected = 0;
 function clickedBack() {
     backSelected = 1;
 	return clickedNumber(backStep, noQuestions);
 }

 function clickedNumber(n, total) {
 	if(!skipTriangle){
 		if (n != total) {
 			if (!window.itemsChanged) {
 				if (n!='Finish') {
 					if(backSelected) {
 					  window.location="wizard2.php?step="+n+"&back=1";
 					} else {
 					  window.location="wizard2.php?step="+n;
 					}
 				} else {
 					window.location="result.php";
 				}
 				return false;
 			}
 			document.forms.answer.elements.submitButton.value = n;
 		} else {
 			if (window.itemsChanged) {
 				document.forms.answer.elements.submitButton.value = "last";
 			} else {
 				window.location="last2.php?step="+n;
 				return false;
 			}
 		}
 	}else{
 		if (!window.itemsChanged) {
 			if (n!='Finish') {
 				if(backSelected) {
 					  window.location="wizard2.php?step="+n+"&back=1";
 				} else {
 					  window.location="wizard2.php?step="+n;
 				}
 			} else {
 				window.location="result.php";
 			}
 			return false;
 		}
 		document.forms.answer.elements.submitButton.value = n;
 	}	
        if(backSelected) {
 		document.getElementById('backInput').value = 1;
 	}
 	document.forms.answer.submit();
 	return false;
 }
 
