//add to<INPUT onclick=countChoices(this) type=checkbox value=Yes name=q6ch38 id="CheckSurvey">
//add to <INPUT type=hidden value=2 name=q5 id="QuestionNumber">

function init(){
count=new Array(50);											//Arr from count of answers to each question

if(document.all){
	if(document.body.all.item("CheckSurvey")){
		var CheckboxNames=new Array();//Array from answers checkbox names
		var aInputs = document.all.item("CheckSurvey");
		for(i = 0; i<aInputs.length; i++){
		   CheckboxNames[i]=aInputs[i].name;
		}

		var QuestionsNames=new Array(); 							//Arr from inputs questions names
		var aQuestions = document.all.item("QuestionNumber");

		for(i = 0; i<aQuestions.length; i++){
		   QuestionsNames[i]=aQuestions[i].name;
		}

		for (i = 0; i < count.length; i++){
			count[i] = 0;
		}
		//start init accounting
		for (i=0; i<CheckboxNames.length; i++){
				if (eval("document.all." + CheckboxNames[i]) != null) {
					if (eval("document.all." + CheckboxNames[i] + ".checked") == true) {
						CurrentCheckboxName=CheckboxNames[i];
						posCH=eval("document.all." + CheckboxNames[i] + ".className")
						//CurrentQuestionName=CurrentCheckboxName.substring(1,posCH)
						count[posCH]+=1;
						//alert("count["+CurrentQuestionName + "]" + count[CurrentQuestionName]);
					}
				}
			}
		}
		
	}
	else {
		for (i = 0; i < count.length; i++){
			count[i] = 0;
		}
	}
}

//function countChoices(obj) {
//	var chName=obj.name;
//	var idx= chName.substring(chName.indexOf("ans")+3);
//			
//	chName =eval("obj.form.q"+idx+".value") ; 		// max. number allowed at a time
//	var max= chName.substr(0,chName.indexOf(",")); 
//	
//	idx=obj.className; //position of the Question within the survey	
//	
//	if (obj.checked){	
//		alert(obj.name + "==" + count[idx])	
//		count[idx]+=1;
//	}
//	else{
//		count[idx]-=1;
//	}
//	if (count[idx] > max) {
//		alert("You can only choose up to " + max + " choices! \nUncheck an option if you want to pick anoth.");
//		obj.checked = false;
//		count[idx]-=1;
//	}
//
//}
function countChoices(obj) {
	var chName=obj.name;
	var idx= chName.substring(chName.indexOf("ans")+3);
	var j=0;
		
	chName =eval("obj.form.q"+idx+".value") ; 		// max. number allowed at a time
	var max= chName.substr(0,chName.indexOf(",")); 
	
	var aChoices = new Array();
	if (document.all.item("ans" + idx)) aChoices=document.all.item("ans" + idx);
	for (i=0; i<aChoices.length; i++){
		if (aChoices[i].checked){
			j+=1;
		}	
	}
	if (j > max){
		alert("You can only choose up to " + max + " choices! \nUncheck an option if you want to pick anoth.");
		obj.checked = false;
	}	

}


function srvGoto(sCll){

	sURL="admin.asp?uri=" + sCll; 
	document.frmSurveyAnswer.action= sURL;
	document.frmSurveyAnswer.submit();
}
	
function CheckAdd(val){
	if (isNumFunction(val.value) == false) {
		alert('The value in the field must be number');//TODO Other VALIDATIONS!!!
		val.value='';
	}
}
function CheckEmail(val)
{
	if (val.value.indexOf ('@',0) == -1 || val.value.indexOf ('.',0) == -1)  {
            alert("Please enter a valid e-mail address.");
            val.value='';
    }
}

function valRequired(){ //Required check
	var sRtn='';
	var aQuestions = new Array();
	if (document.all.item("QuestionNumber")) aQuestions=document.all.item("QuestionNumber");

	for(var i = 0; i<aQuestions.length; i++){
		var qVal=aQuestions[i].value;
		var qName=aQuestions[i].name;
		var qTitle=aQuestions[i].className;
		var row = aQuestions[i].row;
		
		var req= qVal.substring(qVal.indexOf(",")+1); 
		if (req=='True'){ //required answer
			var idx=qName.substring(qName.indexOf("q")+1);
			chName =eval("document.all.ans"+idx); //undefined when no Qtype/choices are defined
			if (typeof(chName)!='undefined'){
				if (typeof(chName.length)!="undefined"){//check for checked/selected items
					var sEmpty='true';
					for (var j=0; j<chName.length; j++ ){
						if (chName[j].checked || chName[j].selected){ sEmpty=''; break; }
					}
					if (sEmpty!=''){
						alert ('Please, select an answer for ' + qTitle); sRtn='break';
						if (chName.type!=undefined) chName.focus(); else chName[0].focus(); 
						break; 
					}
				}
				else { //text/Textarea
					if (chName.value==''){ 
						if (IsAnswerEntry(row)!=''){
						alert ('Please, enter ' + qTitle); chName =eval("document.all.ans"+idx); chName.focus(); sRtn='break'; break;
						} 
					}
				}
			}
		}
	}
	return sRtn;
}

function IsAnswerEntry(chkrow)
	{
	var aQst = new Array();
	var aChoice = new Array();
	var ans='';
	
	if (document.all.item("QuestionNumber")) aQst=document.all.item("QuestionNumber");	
	
	for(var i = 0; i<aQst.length; i++){	
		var row = aQst[i].row;
		var qName=aQst[i].name;
		var qtype =aQst[i].qtype;
		
		var idx=qName.substring(qName.indexOf("q")+1);				
		chName =eval("document.all.ans"+idx); //undefined when no Qtype/choices are defined
				
		if (chkrow==row){					
			if (qtype == 3)	{					
				for (var j=0; j<chName.length; j++ ){										
				if (chName[j].checked || chName[j].selected){ans='exit';}
				}	
			}
			else{
				if (chName.value!='' || chName.value!=null) {
				if (chName.value!=0 || chName.value!=null) {ans='exist';}
				}
			}			
		}
	}
	return  ans;
}

function submitSrv(frmAction){
	//Requiered check
	if (valRequired()==''){
		document.frmSurveyAnswer.action=frmAction;
		document.frmSurveyAnswer.submit();
	}
}


function CheckChoice(cbo, maxAns, sQuestion){
	var ans=0;
	var ind=new Array();	
	if(typeof(sQuestion)=='undefined') sQuestion='';
	else sQuestion= ' for "' + sQuestion + '"';
	for (var i=0;i<cbo.length;i++){
		if (cbo[i].selected) {ans++; ind[ans]=i; }
	}
	if(ans>maxAns){ alert('You can only choose up to ' + maxAns + ' choices' + sQuestion + '! \nUncheck an option if you want to select another.'); 
		var j=0; //unselect the choices > maxAnswers
		for(var i=maxAns; i<ans; i++){
			cbo[ind[ans-j]].selected=false;
			j++; }
		}
}


//-----------------------------
function showHidePayMethods(val, DBvalue){
	onChangeElement();

	if (val.value!='choice' && DBvalue=='choice'){
		var msgConfirm=confirm("You are about to change question type. Existing choices will be deleted.");
		if (msgConfirm!=true) { 
			
		for(i=0; i<document.frmManage.QuestType.length;i++){
			if (document.frmManage.QuestType(i).value=='choice')
				{
				document.frmManage.QuestType(i).checked=true;
				return;
				}
			}
		}
	}

	showHideLayers('Predifined','hide');
 	if (val.value==1){
 		showHideLayers('TxtOption','show');
  		showHideLayers('ChoiceOption','hide');
  		showHideLayers('SubOption','hide');
	}	
	if (val.value==3){
		showHideLayers('ChoiceOption','show')
 		showHideLayers('TxtOption','hide');
 		showHideLayers('SubOption','hide');
	}	
	if (val.value==2){
		showHideLayers('ChoiceOption','hide');
 		showHideLayers('TxtOption','hide');
 		showHideLayers('SubOption','hide');
	}
	if (val.value==4){
		showHideLayers('ChoiceOption','hide');
 		showHideLayers('TxtOption','hide');
  		showHideLayers('Predifined','show')
  		showHideLayers('SubOption','hide');
	}
	if (val.value==5){
		showHideLayers('ChoiceOption','hide');
 		showHideLayers('TxtOption','hide');
  		showHideLayers('Predifined','hide')
  		showHideLayers('SubOption','show');
	}
	if (val.value==6){
		showHideLayers('ChoiceOption','hide');
 		showHideLayers('TxtOption','hide');
 		showHideLayers('SubOption','hide');
	}
}
	
function showHideLayers(ID,v){
	if(document.getElementById(ID)!=null) document.getElementById(ID).style.display=(v=='show')?'':'none';
	}

function _onEnter(){	}

function _OnKeyUp(si,qi,cll, btn){
var sURL="";

sURL="admin.asp?uri=2020&action=4&si=" + si + "&qi=" + qi + "&goto=-10";
if (cll!='') {
sURL = sURL + "&cll=" + cll;
}

if(window.event.keyCode==13 || btn==1){
	frmManage.action = sURL;
	frmManage.submit();
	}
}

function checkSendEmail(rid, bSubForm, id){ //frmManage / forms(0)
	if (bSubForm!=-1) bSubForm=0;
	document.frmManage.action ="admin.asp?uri=2334&action=2&id=" + rid + "&email=yes&obj=" + bSubForm + "&eid=" + id;	
	document.frmManage.submit();
}	



function LookUp(id,rowUpd, bSts, GoToAction){

if(typeof(GoToAction)=='undefined') GoToAction=2;
if(typeof(step)=='undefined') step=0;
	
var gotoUrl='&state=goto&goto=2021|fi=1|cll=2334|action=' + GoToAction+ '|id=' + id + '|row=' + rowUpd;
if (GoToAction==1) gotoUrl= gotoUrl + "|st=4"; 

	
if (bSts!='True'){
	_onSave(document.frmManage,'2334&action=4&id=' + id + '&row=' + rowUpd + gotoUrl);
	}
else{
	frmManage.action=frmManage.action+ '&row=' + rowUpd + gotoUrl;
	frmManage.submit();
	}
}

// LookUp to Question ProductCode/ ProductName
function ProdSrch(id,QID, bSts, GoToAction){

if(typeof(GoToAction)=='undefined') GoToAction=2;
if(typeof(step)=='undefined') step=0;
	
var gotoUrl='&state=goto&goto=2021|fi=1|cll=2334|action=' + GoToAction+ '|id=' + id + '|qd=' + QID;
if (GoToAction==1) gotoUrl= gotoUrl + "|st=4"; 
	
if (bSts!='True'){
	_onSave(document.frmManage,'2334&action=4&id=' + id + '&qd=' + QID + gotoUrl);
	}
else{
	
	frmManage.action='admin.asp?uri=2334&action=4&id='+ id + '&qd=' + QID + gotoUrl;	
	frmManage.submit();
	}
}


//CurUrl - 2334 RequestMng/AdminEntry; CurUrl - 1083 RequestPublicEntry/RequestQuote 
function AddFormRow(CurUrl, bSts, Rqid, RTid, row,Act){
var URLgoto="";	
var UrlSave="";
var SaveAction=4;
	
URLgoto='&add='+ row + '&state=goto&goto='+ CurUrl + '|id=' + Rqid;
if (CurUrl==2334){URLgoto = URLgoto + '|action='+ Act;}
if (CurUrl==1083 && Act!=1){URLgoto = URLgoto + '|action='+ Act;}
if (CurUrl==2334){URLgoto = URLgoto + '|st=4';}
if (CurUrl==4001){SaveAction =44; URLgoto= URLgoto + '|st=11'; }
	
if (bSts!='True'){
				
	if(Rqid <= 0)		
		{
		UrlSave= CurUrl + '&action=' + SaveAction + '&rid=' + RTid + URLgoto;		
		}
	else
		{UrlSave= CurUrl + '&action=' + SaveAction + '&id=' + Rqid + URLgoto;}
		
	if (CurUrl==2334 || CurUrl==4001){UrlSave='admin.asp?uri=' + UrlSave + '&st=11';}	
	else {UrlSave='main.asp?uri=' + UrlSave;}

	frmManage.action=UrlSave;
	frmManage.submit();
}
else{
	frmManage.action=frmManage.action + URLgoto;
	frmManage.submit();
}
}