<?php 

// Check to ensure this file is included in Freeway!
defined('_FEXEC') or die();
?>
<table border="0" width="100%" cellspacing="0" cellpadding="<?php echo CELLPADDING_SUB; ?>">
<?php if (MAIN_TABLE_BORDER == 'yes'){
table_image_border_top(false, false, SURVEY_HEADING_TITLE);
} 
?>
	<?php require(DIR_WS_JAVASCRIPT . "spiffyCal/spiffyCal_v2_1.js");?>
<script language="JavaScript1.3" type="text/javascript">
<!--
function validateForm(theForm) { //form validator
	var cnt,jcnt,value="",err_result="",con_value,con_length;
	if (theForm.question_id[0]){
		for(cnt=0;cnt<theForm.question_id.length;cnt++){
			value="";
			con_value="";
			if (theForm.question_required[cnt].value==1){
				switch(theForm.question_type[cnt].value){
					case '1':  // radio,check
					case '3':
						con_value=eval("theForm.choices_" + theForm.question_id[cnt].value+"[0]");
						if (con_value){
							con_length=eval("theForm.choices_" + theForm.question_id[cnt].value+".length");
							for(jcnt=0;jcnt<con_length;jcnt++){
								if (eval("theForm.choices_" + theForm.question_id[cnt].value+"["+jcnt+"].checked")==true) value="1";
							}
							if (value!="1")
								err_result+="<?php echo ERR_VALUE_REQUIRED; ?>"+(cnt+1)+"\n";
						} else {
							if (eval("theForm.choices_" + theForm.question_id[cnt].value+".checked")==false)
								err_result+="<?php echo ERR_VALUE_REQUIRED; ?>"+(cnt+1)+"\n";
						}
						break;
					case '2':  // pulldown
					case '4':
					case '5':
						value=eval("theForm.choices_" + theForm.question_id[cnt].value + ".value");
						if (value=='' || value=='0'){
							err_result+="<?php echo ERR_VALUE_REQUIRED; ?>"+(cnt+1)+"\n";
						}
						break;
					case '7': // date box
						value=eval("theForm.choices_" + theForm.question_id[cnt].value + ".value");
						if (value==''){
							err_result+="<?php echo ERR_VALUE_REQUIRED; ?>"+(cnt+1)+"\n";
						} else if (!checkDate(value)){
							err_result+="<?php echo sprintf(ERR_VALUE_CHECK_DATE,EVENTS_DATE_FORMAT); ?>"+(cnt+1)+"\n";
						}
						break;
				}
			}
		}
	} else {
		if (theForm.question_required.value==1){
			switch(theForm.question_type.value){
				case '1':  // radio,check
				case '3':
					con_value=eval("theForm.choices_" + theForm.question_id.value+"[0]");
					if (con_value){
						con_length=eval("theForm.choices_" + theForm.question_id.value+".length");
						for(jcnt=0;jcnt<con_length;jcnt++){
							if (eval("theForm.choices_" + theForm.question_id.value+"["+jcnt+"].checked")==true) value="1";
						}
						if (value!="1")
							err_result+="<?php echo ERR_VALUE_REQUIRED; ?>1\n";
						} else {
							if (eval("theForm.choices_" + theForm.question_id.value+".checked")==false)
							err_result+="<?php echo ERR_VALUE_REQUIRED; ?>1\n";
						}
						break;
				case '2':  // pulldown
				case '4':  // Single Line
				case '5':  // multiple lines
					value=eval("theForm.choices_" + theForm.question_id.value + ".value");
					if (value=='' || value=='0'){
						err_result+="<?php echo ERR_VALUE_REQUIRED; ?>1\n";
					}
					break;
				case '7': // date box
					value=eval("theForm.choices_" + theForm.question_id.value + ".value");
					if (value==''){
						err_result+="<?php echo ERR_VALUE_REQUIRED; ?>1\n";
					} else if (!checkDate(value)){
						err_result+="<?php echo sprintf(ERR_VALUE_CHECK_DATE,EVENTS_DATE_FORMAT); ?>1\n";
					}
					break;
			}
		}
	}
	if (err_result!=''){
		alert(err_result);
		return false;
	}
	return (true);
}
	function checkDate(date){
				var dateExp="^<?php echo str_replace(array("d","m","Y"),array("([0-9]{1,2})","([0-9]{1,2})","([0-9]{4})"),EVENTS_DATE_FORMAT);?>$"
				var regExp=new RegExp(dateExp);
				var dateFormat="<?php echo EVENTS_DATE_FORMAT;?>";
				var dateSplt;
				var day;
				var month;
				var year;
				
				var result=regExp.exec(date);
				if (result==null) return false;
				if (dateFormat=="") return false;
				dateSplt=dateFormat.split("-");

				for (var icnt=1;icnt<dateSplt.length;icnt++){
					if (result[icnt].substr(0,1)=="0") result[icnt]=result[icnt].substr(1);
					if (dateSplt[icnt-1]=="d") {  // check for day{
						day=parseInt(result[icnt]);
						if (day<1 || day>31) return false;
					} else if (dateSplt[icnt-1]=="m") {// check for month
						month=parseInt(result[icnt]);
						if (month<1 || month>12) return false;
					} else if (dateSplt[icnt-1]=="Y") { // check for year
						year=parseInt(result[icnt]);
						if (year<=1) return false;
					}
				}
				if ((month==4 || month==6 || month==9 || month==11) && day==31)	return false;

				if (month==2){
					var isleap=(year%4==0 && (year%100!=0 || year%400!=0));
					if (day>29 || ( day==29 && !isleap)){
						return false;
					}
				}
				return true;
	}
-->
</script>
    <tr>
    	<td width=100% align=left valign="top">
		<table border="0" width="100%" cellspacing="0" cellpadding="0">
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif','1','5'); ?></td>
		</tr>
		<tr>
			<td class="main"><?php echo $survey_result['survey_desc']; ?></td>  
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif','1','10'); ?></td>
		</tr>
		<?php 
			echo tep_draw_form('submit_survey',tep_href_link(FILENAME_EVENTS_BOOKING_PROCESS,tep_get_all_get_params(),'SSL'),'post','onSubmit=" return validateForm(this)"');
		?>
			<tr>
				<td>
				<table border="0" cellspacing="0" cellpadding="2" class="formArea" width=100%>
					<tr>
					<input type="hidden" name="survey_id" value="<?php echo $survey_result['survey_id']; ?>">
					<input type="hidden" name="login_id" value="<?php echo $cuID; ?>">
	    			<td valign="top">
					<table border="0" cellspacing="0" cellpadding="4">
					<?php 
						$counter=1;
						while($question_result = tep_db_fetch_array($question_query)){ 
							$choice_query=tep_db_query("SELECT choice_id,choice_text from " . TABLE_EVENTS_SURVEYS_CHOICE . " where question_id='" . $question_result['question_id'] . "' order by choice_id");
							if ($question_result['question_type']>3 || tep_db_num_rows($choice_query)>0){
					?>
					<tr>
						<td class="main" align=right><?php if ($question_result['question_type']!=6) echo $counter . ". ";?></td>
						<td class="main" align=left>
							<?php echo $question_result['question_text'];
									echo '&nbsp;<i>' . ($question_result['question_required']==1?'(' .TEXT_REQUIRED . ')':'') . '</i>' 
							?>
						</td>
					</tr>
					<?php 
					
					$choice = "";
					$choice_array=array();
					$choice_array[]=array('id'=>0,text=>'');
					while($choice_result = mysql_fetch_array ($choice_query)){
						switch ($question_result["question_type"]){
							case 1: //radio
								$choice .= "&nbsp;&nbsp;" . tep_draw_radio_field("choices_" . $question_result['question_id'] . "[]",$choice_result['choice_id'],false,'id="choices_' . $question_result['question_id'] . '"') . "&nbsp;" . $choice_result["choice_text"] . "<br>";
								break;
							case 2: //pulldown
								$choice_array[]=array('id'=>$choice_result['choice_id'],'text'=>$choice_result['choice_text']);
								break;
							case 3: //checkbox
								$choice .= "&nbsp;&nbsp;" . tep_draw_checkbox_field("choices_" . $question_result['question_id'] . "[]",$choice_result['choice_id'],false,'id="choices_' . $question_result['question_id'] . '"') . "&nbsp;" . $choice_result["choice_text"] . "<br>";
								break;
						}
					}
					?>
					<tr>
						<td class="main"></td>
						<td class="main" align=left>
						<?php 
							switch ($question_result["question_type"]){
								case 2:
									$choice=tep_draw_pull_down_menu("choices_" . $question_result['question_id'] . "[]",$choice_array,'','id="choices_' . $question_result['question_id'] . '"');
									break;
								case 4:
									$choice="&nbsp;&nbsp;" . tep_draw_input_field("choices_" . $question_result['question_id'] . "[]",'','size=90 id="choices_' . $question_result['question_id'] . '"');
									break;
								case 5:
									$choice="&nbsp;&nbsp;<table width=100% border='0'><tr><td>" . tep_draw_textarea_field("choices_" . $question_result['question_id'] . "[]" ,false,10,3,'','id="choices_' . $question_result['question_id'] . '"') . "</td></tr></table>";
									break;
								case 7:
									$choice=tep_draw_input_field("choices_" . $question_result['question_id'] . "[]",'','id="choices_' . $question_result['question_id'] . '" size=12 maxlength=10') . '&nbsp;<i>' .  EVENTS_DATE_FORMAT . '</i>';
									break;
							}
							echo $choice;
						?>
						</td>
					</tr>
					<tr>
						<td><?php echo tep_draw_separator('pixel_trans.gif','1','10'); ?></td>
					</tr>
					<?php 
							if ($question_result['question_type']!=6) {
								$counter++;
								echo tep_draw_hidden_field('question_required[]',$question_result['question_required'],'id="question_required"');
								echo tep_draw_hidden_field('question_type[]',$question_result['question_type'],'id="question_type"');
								echo tep_draw_hidden_field('question_id[]',$question_result['question_id'],'id="question_id"');
							}
						}
						}
					?>
				</table>
				</td>
			</tr>
		</table>
		</td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif','1','10'); ?></td>
		</tr>
		<tr>
			<td>
				<?php echo tep_template_image_submit('button_submit_survey.gif',IMAGE_SURVEY_SUBMIT); ?>
			</td>
	    </tr>
		<?php
			// draw the hidden field
			
			echo tep_draw_hidden("old_orders_id",$HTTP_POST_VARS["old_orders_id"]);
			echo tep_draw_hidden("events_id",$HTTP_POST_VARS["events_id"]);
			echo tep_draw_hidden("event_question_type",$HTTP_POST_VARS["event_question_type"]);
			echo tep_draw_hidden("event_question_value_data",$HTTP_POST_VARS["event_question_value_data"]);
			$session_id=tep_db_prepare_input($HTTP_POST_VARS["session_id"]);
			$session_type=tep_db_prepare_input($HTTP_POST_VARS["session_type"]);
			$session_group=tep_db_prepare_input($HTTP_POST_VARS["session_group"]);
			$session_date=tep_db_prepare_input($HTTP_POST_VARS["session_date"]);
			for ($icnt=0;$icnt<count($session_id);$icnt++){
				echo tep_draw_hidden("session_select_" . $session_id[$icnt],$HTTP_POST_VARS["session_select_" . $session_id[$icnt]]);
				echo tep_draw_hidden("session_id[]",$session_id[$icnt]);
				echo tep_draw_hidden("session_type[]",$session_type[$icnt]);
				echo tep_draw_hidden("session_group[]",$session_group[$icnt]);
				echo tep_draw_hidden("session_date[]",$session_date[$icnt]);
			}
		?>
		</form>
		<!-- body_eof //-->
	</table>
	</td>
</tr>
<?php 
if (MAIN_TABLE_BORDER == 'yes'){
	table_image_border_bottom();
} ?>       
</table>