function surveyValidator(form,scrollbars, nopopup) {
    var TYPE_RADIO = 0;
    var TYPE_LIST = 1;

    var j_width = 770;
    var j_height = 500;

    var scroll = "no";
    if (scrollbars == "yes")
        scroll = "yes";

    theForm = form;

    if (theForm.popupWidth != null) {
        j_width = theForm.popupWidth.value;
    }

    if (theForm.popupHeight != null) {
        j_height = theForm.popupHeight.value;
    }

    var questions = theForm.mandatoryQuestions.value;
    var temp = questions.substring(1);
    var questionArray = temp.split(",");
    for (i = 0; i < questionArray.length; i++) {
        qtype = "questionType_" + questionArray[i];
        if (theForm.elements[qtype].value == "0") {
            question = "questionAnswer_" + questionArray[i];
            if (!radioElementValidator(theForm, question)) {
                return false;
            }
        } else if (theForm.elements[qtype].value == "1") {
            question = "questionAnswer_" + questionArray[i];
            if (!listElementValidator(theForm, question)) {
                return false;
            }
        } else if (theForm.elements[qtype].value == "3") {
            question = "questionAnswer_" + questionArray[i];
            if (!emailElementValidator(theForm, question)) {
                return false;
            }
        }
    }
                
    if (theForm.onLastPage.value == "yes" && theForm.resultTemplate.value.length > 0) {
        //submit the form from the current window.
        if ('undefined' == nopopup || null == nopopup || 'yes' != nopopup) {        
        	popUp = window.open(theForm.resultTemplate.value,"resultPage","scrollbars=" + scroll + ",resizable=1,width="+j_width+",height="+j_height+",left=0,top=0");
        	theForm.target = "resultPage";	
	}	
        theForm.submit();
        return true;
    }

    if (theForm.onLastPage.value == "yes") {
    	if ('undefined' == nopopup || null == nopopup || 'yes' != nopopup) {	    	
        	popUp = window.open("/survey/processing.jsp","popUp","scrollbars=" + scroll + ",resizable=1,width=330,height=300,left=0,top=0");
        	setInterval("popUp.close();", 5000);        	
        }
        setInterval("theForm.submit();",5000)
        return false;
    }
}

function surveyVotes(url) {
    votes = window.open(url,"surveyVotes","width=770,height=450,scrollbars=no,left=0,top=0");
    votes.focus();
}

function radioElementValidator(surveyForm, elementName) {
    var answerId = "";
    for(j = 0; j < surveyForm.elements[elementName].length; j++) {
        if (surveyForm.elements[elementName][j].checked) {
            answerId = surveyForm.elements[elementName][j].value;
            break;
        }
    }
    if (answerId == "" || answerId == null) {
        alert("One or more of the quiz question was not answered.");
        return false;
    } else {
        return true;
    }
}

function listElementValidator(surveyForm, elementName) {
    if (surveyForm.elements[elementName].selectedIndex == "" || surveyForm.elements[elementName].selectedIndex == null) {
        alert("One or more of the survey question was not answered.");
        return false;
    } else {
        return true;
    }
}

function emailElementValidator(surveyForm, elementName) {
    if (surveyForm.elements[elementName].value == "") {
            alert("You have not entered an Email Address.\nPlease enter it now.");
        surveyForm.elements[elementName].focus();
        return false;
    }

    if (surveyForm.elements[elementName].value.length > 0) {
        //Return false if e-mail field does not contain a '@' and '.'.
        if (surveyForm.elements[elementName].value.indexOf('@',0) == -1 || surveyForm.elements[elementName].value.indexOf('.',0) == -1) {
            alert("You have entered an invalid email address, \nPlease enter a correct email address now.");
            surveyForm.elements[elementName].focus();
            surveyForm.elements[elementName].select();
            return false;
        }
    }
    return true
}


function showLiveSurvey(expiration, workFlowState, liveMode) {
	var now = Date.parse(new Date());
	var endDate = Date.parse(expiration);

	if(document.layers){ //  netscape version < 6
		if (endDate > now && workFlowState == liveMode) {
 			document.divId.visibility="show";
			document.divId.visibility="visible";
		}
	} else {
		if (endDate > now && workFlowState == liveMode) {
			document.getElementById("divId").style.visibility="visible";
 			document.getElementById("divTable").style.display="inline";
		}
	}
}