
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='';
    }
}

/* This is the old version of the function that doesn't support Other (Specify) answer types
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;
}
*/

//This is the new version of the function that does support Other (Specify) answer types
function valRequired(){ //Required check
	var sRtn='';
	var aQuestions = new Array();

//  Changed by Spas in April,2010 - document.all doesn't work in Netscape based browsers (FF, Chrome, Opera)
//	if (document.all.item("QuestionNumber")) aQuestions=document.all.item("QuestionNumber");
	if (document.all.item("QuestionNumber")) aQuestions=document.getElementsByName("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++ )
					{ 
						var aVal = chName[j].value;
						var objID	= "ans"+idx+"other"+aVal;
						var otherSpecify = document.getElementById(objID); 
						//alert("document.getElementsByName(\"ans"+idx+"other"+aVal+"\")");
						//alert(otherSpecify != null);
						if (chName[j].checked || chName[j].selected)
							{ 
								if (otherSpecify != null)
									{
										var osVl = otherSpecify.value;
										if (osVl == '' || osVl == null)
											{
												//alert('empty');				
												sEmpty='true';
											}
										else
											{
												//alert('not empty');				
												sEmpty='';
												//break;
											}
									}
								else
									{
										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();
}
}


function Raise_Events(){
	ClearAnswers("input");
	ClearAnswers("select");
	ClearAnswers("textarea");
	var selObj=document.getElementById("Existing");
	var selValue=selObj.options[selObj.selectedIndex].value; //the value of the selected option from <select>
	if(selValue==0) return;
	var hiddenCntrl=document.getElementById("DB_Ans"+ selValue); //the needed hidden control depending on the selected option
	var controls=hiddenCntrl.value.substring(0,hiddenCntrl.value.length-1).split(";"); //array of all ### sections from the hidden control
	var equalIndex; //index of '=' in each ### section
	var controlName; //the value between 'Q' and 'Data' => if we have Q71Data or Q71Choices the result is 71
	var controlObj; //array of elements with same controlName
	var controlValue; //the value after each 'Q[something here]Choices='
	for(var i=1; i<controls.length; i=i+2){
		equalIndex=controls[i].indexOf("=")
		controlName=controls[i].substring(4,equalIndex-7);
		controlValue=controls[i].substring(equalIndex+1,controls[i].length)
		if(controlValue==""){
			if(document.getElementsByName("ans" + controlName).length==1){
				controlObj=document.getElementsByName("ans" + controlName)[0];
				controlValue=controls[i-1].substring(controls[i-1].indexOf("=")+1,controls[i-1].length);
				
				switch(controlObj.type){
					case "text" :
						controlObj.value=controlValue;
						break;
					case "textarea" :
						controlObj.value=controlValue;
						break;
				}
			}
		}
		else{
			if(document.getElementsByName("ans" + controlName).length >0){
				controlObj=document.getElementsByName("ans" + controlName);
				for(var j=0; j<controlObj.length; j++){
					switch(controlObj[j].type){
						case "select-one" :
							for(var k=0; k<controlObj[j].length; k++){
								if(controlObj[j].options[k].value==controlValue*1) controlObj[j].options[k].selected=true;
							}
							break;
						case "radio" :
							if(controlObj[j].value==controlValue*1) controlObj[j].checked=true;
							break;
						case "checkbox" :
							var mulValues=controlValue.split(",");
							for(var singleVal in mulValues){
								if(controlObj[j].value==mulValues[singleVal]*1) controlObj[j].checked=true;
							}
							break;
					}
				}
			}
		}
	}
}


function ClearAnswers(tag){
	var answers=document.getElementsByTagName(tag);
	for(var i=0; i<answers.length; i++){
		if(answers[i].name.match("\ans")){
			switch(answers[i].type){
				case "text" :
					answers[i].value=""
					break;
				case "textarea" :
					answers[i].value=""
					break;
				case "select-one" :
					answers[i].selectedIndex=0;
					break;
				case "radio" :
					answers[i].checked=false;
					break;
				case "checkbox" :
					answers[i].checked=false;
					break;
			}
		}
	}
}

function _CopyQuestion(si,qi){
var sURL="";

sURL="admin.asp?uri=2020&action=4&si=" + si + "&qi=" + qi + "&copycalled=1";

frmManage.action = sURL;
frmManage.submit();
}