<?php
/*
  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
  
  Freeway eCommerce from Zac
http://www.zac-ware.com/freeway

Copyright 2007 ZacWare Pty. Ltd

*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
require('includes/application_top.php');
  require(DIR_WS_LANGUAGES.$language.'/'.FILENAME_SERVICES_COPY_SURVEYS);
  	$copy_command=isset($HTTP_GET_VARS['copy_command'])?$HTTP_GET_VARS['copy_command']:'';
	if($copy_command!=''){
		switch($copy_command){
		case 'copy_message':
			echo 'message^^^^copy_message^^^^';
			echo tep_service_survey_copy_message();
			break;
		case 'save_message':
			echo 'message^^^^save_message^^^^';
			echo update();
			break;
		}
	exit;
	}
	tep_get_last_access_file();
	require('includes/functions/services_tree.php');	
    $survey_service=array('RV'=>TEXT_RV,'WV'=>TEXT_WV,'FV'=>TEXT_FV,'CD'=>TEXT_CD);
	$choice_array=array('1'=>TEXT_MULTIPLE_RADIO,
				 '2'=>TEXT_MULTIPLE_PULLDOWN,
				 '3'=>TEXT_MULTIPLE_CHECKBOX,
				 '4'=>TEXT_INPUT_ONELINE,
				 '5'=>TEXT_INPUT_THREELINES,
				 '6'=>TEXT_INPUT_HEADING,
				 '7'=>TEXT_INPUT_DATE);

	$command = (isset($HTTP_GET_VARS['command']) ? $HTTP_GET_VARS['command'] : '');
	if ($command!=''){
		$service_id = (isset($HTTP_GET_VARS['service_id']) ?(int)$HTTP_GET_VARS['service_id'] : 0);
		$type=$HTTP_GET_VARS['type'];
	  	 echo 'command||'. $command ;
		 if($type!='')
		   echo '{}type||' . $type;
		   $commands[]=$command;
		   for ($icnt=0;$icnt<count($commands);$icnt++){
			 switch($commands[$icnt]){
			 case 'ajx_service_survey':
				echo '{}ajx_service_survey||';
				tep_services_survey_list($service_id);
				break;
			 case 'ajx_survey_details':
				echo '{}result_content||';
				tep_get_survey_details($service_id,$type);
				break;
			 case 'ajx_update':
				echo '{}result_content||';
				tep_get_survey_details($service_id,$type,true);
				break;
			 case 'ajx_update_survey':
				echo '{}result_content||';
				tep_get_survey_edit($service_id,$type,true);
				break;
			 case 'ajx_save':
				update_service_survey($HTTP_GET_VARS['suID']);
				$commands[]="ajx_survey_details";
				break;
			 case 'ajx_ques_save':
			 	survey_question_save($HTTP_GET_VARS['suID']);
				if($HTTP_GET_VARS['ques_id']!='')
					$commands[]="ajx_question_details";
				else
					$commands[]="ajx_survey_details";
			 	break;
			 case 'ajx_delete': 
	 		 	 echo '{}delete_survey||';
	  		 	 delete_service_survey($HTTP_GET_VARS['suID']);
				 break;
			 case 'ajx_update_ques':
			 	echo '{}question_id||' . $HTTP_GET_VARS['ques_id'] ;
				echo '{}ques_result_content||';
				tep_get_question_details($service_id,$type,$HTTP_GET_VARS['suID'],$HTTP_GET_VARS['ques_id'],true);
				break;
			case 'ajx_question_details':
			 	echo '{}question_id||' . $HTTP_GET_VARS['ques_id'] ;
				echo '{}ques_result_content||';
				tep_get_question_details($service_id,$type,$HTTP_GET_VARS['suID'],$HTTP_GET_VARS['ques_id'],false);
				break;				
			case 'ajx_delete_choice':
			 	echo '{}question_id||' . $HTTP_GET_VARS['ques_id'] ;
				echo '{}choice_id||' .$HTTP_GET_VARS['chID'];
				echo '{}remove_choice';
			    $chID=(isset($HTTP_GET_VARS['chID'])?(int)$HTTP_GET_VARS['chID']:0);
				tep_db_query("DELETE FROM " . TABLE_SERVICES_SURVEYS_CHOICE . " where choice_id='" . (int)$chID . "'");
				tep_db_query("DELETE FROM " . TABLE_SERVICES_SURVEYS_ANSWER . " where choice_id='" . (int)$chID . "'");
				break;
			case 'ajx_new_choice':
				$quID=(isset($HTTP_GET_VARS['ques_id'])?(int)$HTTP_GET_VARS['ques_id']:0);
			 	echo '{}question_id||' . $HTTP_GET_VARS['ques_id'] ;
				if ($quID>0){
       				 $sql_array=array('choice_text'=>tep_db_prepare_input($HTTP_POST_VARS['choice_text']),
	                 'question_id'=>tep_db_prepare_input($quID));
        			tep_db_perform(TABLE_SERVICES_SURVEYS_CHOICE,$sql_array);
				}
				$chid=tep_db_insert_id();
				echo '{}choice_id||' .$chid;
				echo '{}add_choice||<table><tr><td class="SmallText">' .  $HTTP_POST_VARS['choice_text'] . '</td><td>';
				echo '<a href="javascript:do_page_fetch(\'ajx_delete_choice\',\'' . $service_id . '\',\'' . $type. '\',\'' . $HTTP_GET_VARS['ques_id'] . '\',\'' . $chid . '\');">' . tep_image('images/icons/cross.gif','') . "</a>&nbsp;&nbsp;&nbsp;";
				echo '</td></tr></table>';
				break;
			 case 'ajx_setflag': //set the status of a template active.
				change_service_survey_status($HTTP_GET_VARS['suID']);
				$commands[]="ajx_survey_details";
				break;
			 case 'ajx_new':
				insert_service_survey($service_id,$type);
				$commands[]="ajx_survey_details";
				break;
			case 'ajx_delete_ques':
		        $quID=(isset($HTTP_GET_VARS['ques_id'])?(int)$HTTP_GET_VARS['ques_id']:0);
				tep_db_query("DELETE FROM " . TABLE_SERVICES_SURVEYS_CHOICE . " where question_id='" . (int)$quID . "'");
				tep_db_query("DELETE FROM " . TABLE_SERVICES_SURVEYS_QUESTION . " where question_id='" . (int)$quID . "'");
				tep_db_query("DELETE FROM " . TABLE_SERVICES_SURVEYS_ANSWER . " where question_id='" . (int)$quID . "'");
				tep_db_query("DELETE FROM " . TABLE_SERVICES_SURVEYS_TEXT . " where question_id='" . (int)$quID . "'");
			 	echo '{}question_id||' . $HTTP_GET_VARS['ques_id'] ;
				echo '{}remove_question';
				//$commands[]="ajx_update";
				break;
			case 'ajx_pos_up':
			case 'ajx_pos_down':
		        $change_id=(isset($HTTP_GET_VARS['suID'])?(int)$HTTP_GET_VARS['suID']:0);
			    $pos_query=tep_db_query("SELECT sort_order from " . TABLE_SERVICES_SURVEYS_QUESTION . " where question_id='" . (int)$HTTP_GET_VARS['ques_id'] . "'");
				$res=tep_db_fetch_array($pos_query);
				$pos=$res['sort_order'];
				if ($pos>0){
					if ($commands[$icnt]=='ajx_pos_up')
					  $question_query=tep_db_query("SELECT sort_order,question_id from " . TABLE_SERVICES_SURVEYS_QUESTION . " where survey_id='" . (int)$change_id . "' and sort_order<" . $pos . " order by sort_order desc limit 1");
				 	else
				  		$question_query=tep_db_query("SELECT sort_order,question_id from " . TABLE_SERVICES_SURVEYS_QUESTION . " where survey_id='" . (int)$change_id . "' and sort_order>" . $pos . " order by sort_order limit 1");
					if (tep_db_num_rows($question_query)>0){
						  $question_result=tep_db_fetch_array($question_query);
						  $prev_order=$question_result['sort_order'];
						  tep_db_query("UPDATE " . TABLE_SERVICES_SURVEYS_QUESTION . " set sort_order='" . $pos ."' where question_id='" . (int)$question_result['question_id'] . "'");
						  tep_db_query("UPDATE " . TABLE_SERVICES_SURVEYS_QUESTION . " set sort_order='" . $prev_order . "' where question_id='" . (int)$HTTP_GET_VARS['ques_id'] ."'");
					 }
				 }
				 //$commands[]="ajx_update";
				break;
			}
	   }
	  exit;
	}
	
	$service_array=tep_get_services_instructor_array_single();
	$service_found=true;
	if (sizeof($service_array)<=0){
		$service_found=false;
	} else {
		if ($select_id!=0 && $select_id!=$service_id)
			$service_id=$select_id;
		else if ($select_id==0 && $service_id==0)
			$service_id=$service_array[0]['id'];
	}
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<Style>
.dataTableRowOdd1 { background-color: #E4E9F9; }
.dataTableRowEven1 { background-color: #EDF1FE; }
</Style>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
<script language="JavaScript" src="includes/date-picker.js"></script>
<script language="javascript" src="includes/http.js"></script>
<script language="JavaScript1.3" type="text/javascript">
<!--
	var img_src="<?php echo HTTP_SERVER.DIR_WS_ADMIN.'images/';?>";
	function validateFormSettings( theForm ) {
	   tt=theForm.survey_name.value;
		if ( tt == "" || str_trim(tt) ==""){
			alert( "<?php echo ERR_SURVEY_NAME; ?>");
			theForm.survey_name.focus(); 
			return ( false );
		}    
		if ( theForm.survey_time_period.value < 1 || theForm.survey_time_period.value > 12){
			alert( "<?php echo ERR_SURVEY_LIMIT; ?>");
			theForm.survey_time_period.focus(); 
			return ( false );
		}
		return ( true );
	}
	function validateFormQuestion( theForm ) {
	obj=theForm.question_text.value;
	if (obj=="" || str_trim(obj) == "") {
		alert( "<?php echo tep_db_input(ERR_QUESTION_TEXT); ?>" );
		theForm.question_text.focus();
		return ( false );
	}
	return ( true );
}
function validateFormChoice( theForm ) {
     obj=theForm.choice_text.value;
	if ( obj=="" ||  str_trim(obj) == "" ) {
		alert( "<?php echo tep_db_input(ERR_CHOICE_TEXT); ?>" );
		theForm.choice_text.focus();
		return ( false );
	}
	return ( true );
}
function rowOverEffect1(object) {
 if (object.className == 'dataTableRowOdd1') object.className = 'dataTableRowOddOver';
  else if (object.className == 'dataTableRowEven1') object.className = 'dataTableRowEvenOver';
}

function rowOutEffect1(object) {
  if (object.className == 'dataTableRowOver') object.className = 'dataTableRow';
  else if (object.className == 'dataTableRowOddOver') object.className = 'dataTableRowOdd1';
  else if (object.className == 'dataTableRowEvenOver') object.className = 'dataTableRowEven1';
  else if (object.className == 'tableMenuItemOver') object.className = 'tableMenuItem';
}

//--></script>
<script language="JavaScript">
		function resize(){
			document.getElementById('service_list').style.visibility="hidden";
			document.select_service.select_text.blur();
		}
		var iex=(document.all);//ie
		var na6=(window.sidebar);//netscape 6.2
		var lastposition=0;
		var currentField="";
		var intervalId;
			function checkdropDown(){
				if (currentField==""){
					clearInterval(intervalId);
					hideList();
					intervalId="";
				}
			}
			function toggleText(mode){
				if (mode==1) {
					currentField="Text";
					showList();
					if (intervalId) clearInterval(intervalId);
					intervalId=setInterval("checkdropDown()",300);
				} else {
					currentField="";
				}
			}
			function toggleList(mode){
				if (mode==1){
					currentField="drop";
				} else {
					currentField="";
				}
			}
			function showList(){
				var listbox=document.getElementById("service_list");
				var textbox=document.select_service.select_text;
				listbox.style.visibility="visible";
				listbox.style.display="";
				var left = getPageOffsetLeft(textbox);
				var top =getPageOffsetTop(textbox);

				if(window.scrollMaxX >0)listbox.style.left=left;
				else listbox.style.left=left;
				listbox.style.top=top+17;
			}
			function hideList(){
				var listbox=document.getElementById("service_list");
				var textbox=document.select_service.select_text;
				setListboxText(1);
				listbox.style.display="none";
			}
			function scrollList(e){
				var listbox=document.select_service.select_id;

				if (iex) keyCode=window.service.keyCode;
				if (na6) keyCode=e.which;

				if (keyCode==40 || keyCode==38){
					if (keyCode==40 && listbox.selectedIndex>=0 && listbox.selectedIndex<listbox.options.length-1){
						listbox.selectedIndex+=1;
					} else if (keyCode==38 && listbox.selectedIndex>0 && listbox.selectedIndex<=listbox.options.length-1){
						listbox.selectedIndex-=1;
					}

					setListboxText(0);
					if (iex) window.service.returnValue=0;
					if (na6) e.returnValue=0;
					return;

				}
			}
			function selectList(e){
				var listbox=document.select_service.select_id;
				var search_word=document.select_service.select_text.value;
				var text_length;
				var found=-1;
				var listbox_word;
				var serviceObject;
				var keyCode=0;
				var fromPos=0;
				var toPos=0;
	
				if (!listbox.options || listbox.options.length<=0) return;

				if (search_word!="") {
					search_word=search_word.toLowerCase();
					found=binaryFind(listbox,search_word,0,listbox.options.length-1);
					if (found>=0)
						listbox.selectedIndex=found;
				}

			}
			function setListboxText(mode){
				var listbox=document.select_service.select_id;
				if (!listbox.options || listbox.options.length<=0 || listbox.selectedIndex<=-1) return;
				var textbox_name=document.select_service.select_text;
				if (textbox_name.value=="" && mode==1) return "";
				textbox_name.value=listbox.options[listbox.selectedIndex].innerHTML;
				//document.select_service.submit();
			}
			function binaryFind(listBox,fWord,sPos,ePos){
				var listElement;
				var tWord;
				var mPos;
				if (sPos>ePos) return -1;
				mPos=sPos+Math.ceil((ePos-sPos)/2);
				
				if (mPos<0 || mPos>=listBox.options.length) return -1;
				listElement=listBox.options[mPos].innerHTML.toLowerCase();
				tWord=listElement.substr(0,fWord.length);

				if (tWord==fWord) return mPos;
				if (mPos<=0 || mPos>=listBox.options.length) return -1;
				
				if (fWord<tWord) return binaryFind(listBox,fWord,mPos+1,ePos);
				else return binaryFind(listBox,fWord,sPos,mPos-1);
			}
/*	function do_hover_change(element,mode){
		if (element.mode && mode==element.mode) return;
		if (mode==2)
			element.firstChild.src=element.firstChild.src.replace(".","_hover.");
		else
			element.firstChild.src=element.firstChild.src.replace("_hover.",".");
		element.mode=mode;
	}
*/

	 function do_hover_change(element,eve){ 
		 var span_edit=document.getElementById("edit_survey");
		 var span_delete=document.getElementById("delete_survey");
		 if(element){		   		    		    
			var img_get_src=element.childNodes[0];
			if(img_get_src){
				if(img_get_src.src==img_src+'template/img_edit.gif') {
					if(span_edit && span_edit.style.display=="" && eve==1 || eve==2){
						img_get_src.src=img_src+'template/img_edit_hover.gif';
					}  		   		 	 			 	 			
				}else if(eve==3 && img_get_src.src==img_src+'template/img_edit_hover.gif' && span_edit  && span_edit.style.display=="none") img_get_src.src=img_src+'template/img_edit.gif';
				if(img_get_src.src==img_src+'template/img_trash.gif') {
					if(span_delete && span_delete.style.display=="" && eve==1 || eve==2){
						img_get_src.src=img_src+'template/img_trash_hover.gif';
					}  		   		 	 			 	 			
				}else if(eve==3 && img_get_src.src==img_src+'template/img_trash_hover.gif' && (span_delete)  && (span_delete.style.display=="none")) img_get_src.src=img_src+'template/img_trash.gif';
		  }
		 }		 
	 }  
	 
	 function do_hover_change1(element,eve){ 
		 var span_edit=document.getElementById("edit_question");
		 var span_delete=document.getElementById("delete_question");
		 if(element){		   		    		    
			var img_get_src=element.childNodes[0];
			if(img_get_src){
				if(img_get_src.src==img_src+'template/img_edit.gif') {
					if(span_edit && span_edit.style.display=="" && eve==1 || eve==2){
						img_get_src.src=img_src+'template/img_edit_hover.gif';
					}  		   		 	 			 	 			
				}else if(eve==3 && img_get_src.src==img_src+'template/img_edit_hover.gif' && span_edit  && span_edit.style.display=="none") img_get_src.src=img_src+'template/img_edit.gif';
				if(img_get_src.src==img_src+'template/img_trash.gif') {
					if(span_delete && span_delete.style.display=="" && eve==1 || eve==2){
						img_get_src.src=img_src+'template/img_trash_hover.gif';
					}  		   		 	 			 	 			
				}else if(eve==3 && img_get_src.src==img_src+'template/img_trash_hover.gif' && (span_delete)  && (span_delete.style.display=="none")) img_get_src.src=img_src+'template/img_trash.gif';
		  }
		 }		 
	 }  	
</script>
<Script>
		var is_fetching=false;
		var prev_type='';
		var prev_ques='';
		function do_page_ajax(command){
			if (is_fetching) return;
			is_fetching=true;
			do_get_command(command);
		}
		function close_new(type)
		{
			document.getElementById(type + "_row").style.display="";
			document.getElementById("panel_" + type + "_row").style.display="none";
			document.getElementById("panel_" + type + "_row").innerHTML=document.getElementById("ajx_load_content_img").innerHTML;
		}
		function close_question(qid)
		{
			if(document.getElementById("ques_" + qid))
			{
				document.getElementById("ques_" + qid).style.display="";
				document.getElementById("ques_" + qid + "_edit").style.display="none";
				document.getElementById("ques_" + qid + "_edit").innerHTML=document.getElementById("ajx_load_content_img").innerHTML;
			}
		}
		function close_ques_new(act,qid)
		{
			if(act=='update')
				close_question(qid);
			else
				document.getElementById("ques_new").style.display="none";
		}
		function do_page_fetch(cmd,service_id,type,col,qid,pos){
			command="<?php echo tep_href_link(FILENAME_SERVICES_SURVEYS,'');?>?command="+cmd;
			switch(cmd){
				case "ajx_service_survey":
					command=command+"&service_id="+service_id.value;
					//alert(command);
					break;
				case "ajx_survey_details":
					if(prev_type!='')
						close_new(prev_type);
					prev_ques='';
					document.getElementById("panel_" + type + "_row").style.display="";
					document.getElementById("copy_message_load").style.display="none";
					command=command+"&service_id=" + service_id + "&type=" +type;
					prev_type=type;
					break;
				case "ajx_new": //new survey
					if(validateFormSettings(document.forms["new_survey"]))
					{
						do_post_command("new_survey",command+"&service_id=" + service_id + "&type=" +type);
					}
					return;
				case "ajx_save": //save the new survey
					if(validateFormSettings(document.forms["new_survey"]))
					{
						do_post_command("new_survey",command+"&service_id=" + service_id + "&type=" +type + "&suID=" + col);
					}
					return;
				case "ajx_setflag":
					command=command+"&service_id=" + service_id + "&type=" +type + "&suID=" + col + "&edit=1";
					break;
				case "ajx_delete":  //delete survey
					if(document.getElementById("edit_survey"))document.getElementById("edit_survey").style.display='none';
					if(document.getElementById("delete_survey"))document.getElementById("delete_survey").style.display='';
					if(confirm("<?php echo TEXT_DELETE_SURVEY_INTRO;?>")){
						command=command+"&service_id=" + service_id + "&type=" +type + "&suID=" + col + "&edit=1";
					}else{
					var tr1=document.getElementById("img_ids");
					var imgsrc;
					var node;
					if(tr1)
					for(var j=0;j<tr1.cells.length;j++){
						td=tr1.cells[j];
						imgsrc='';
						for(var i=0;i<td.childNodes.length;i++){
							node=td.childNodes[i];
							if(node.tagName=='IMG'){
								imgsrc=node.src;
								if(imgsrc==img_src+'template/img_edit_hover.gif')
								   td.childNodes[i].src = img_src+'template/img_edit.gif';
								else if(imgsrc==img_src+'template/img_trash_hover.gif')
								   td.childNodes[i].src = img_src+'template/img_trash.gif';
								else if(imgsrc==img_src+'template/img_copy_hover.gif')
								   td.childNodes[i].src = img_src+'template/img_copy.gif';
								break;
							}
						}
					}
					document.getElementById("edit_survey").style.display='none';
					document.getElementById("delete_survey").style.display='none';
					return;
					}
					break;
				case "ajx_update":  //show update screen survey
				
					document.getElementById("show_edit_scr").innerHTML=document.getElementById("ajx_load_content_img").innerHTML;
					command=command+"&service_id=" + service_id + "&type=" +type + "&suID=" + col + "&edit=1";
					break;
				case "ajx_update_survey":
					document.getElementById("edit_survey").style.display='';
					document.getElementById("delete_survey").style.display='none';
					document.getElementById("show_edit_scr").innerHTML=document.getElementById("ajx_load_content_img").innerHTML;
					command=command+"&service_id=" + service_id + "&type=" +type + "&suID=" + col + "&edit=1";
					break;
				case "ajx_delete_ques":
					if(document.getElementById("edit_question"))document.getElementById("edit_question").style.display='none';
					if(document.getElementById("delete_question"))document.getElementById("delete_question").style.display='';
					if(confirm("<?php echo TEXT_DELETE_QUESTION_INTRO;?>")){
						command=command+"&service_id=" + service_id + "&type=" +type + "&ques_id=" + col;
					}else{
					var tr1=document.getElementById("image_ids");
					var imgsrc;
					var node;
					if(tr1)
					for(var j=0;j<tr1.cells.length;j++){
						td=tr1.cells[j];
						imgsrc='';
						for(var i=0;i<td.childNodes.length;i++){
							node=td.childNodes[i];
							if(node.tagName=='IMG'){
								imgsrc=node.src;
								if(imgsrc==img_src+'template/img_edit_hover.gif')
								   td.childNodes[i].src = img_src+'template/img_edit.gif';
								else if(imgsrc==img_src+'template/img_trash_hover.gif')
								   td.childNodes[i].src = img_src+'template/img_trash.gif';
								else if(imgsrc==img_src+'template/img_copy_hover.gif')
								   td.childNodes[i].src = img_src+'template/img_copy.gif';
								break;
							}
						}
					}
					 document.getElementById("edit_question").style.display='none';
					 document.getElementById("delete_question").style.display='none';
					 return;
					} 
					break;
				case "ajx_update_ques":
					if(document.getElementById("edit_question"))document.getElementById("edit_question").style.display='';
					if(document.getElementById("delete_question"))document.getElementById("delete_question").style.display='none';
					if(qid!='')
					{
						document.getElementById("ques_" + qid + "_edit").style.display="";
						document.getElementById("question_update_details").innerHTML=document.getElementById("ajx_load_content_img").innerHTML;
						//document.getElementById("ques_" + qid + "_edit").innerHTML=document.getElementById("ajx_load_content_img").innerHTML;
					}
					else
					{
						if(prev_ques!='')
							close_question(prev_ques);
						document.getElementById("ques_new").innerHTML=document.getElementById("ajx_load_content_img").innerHTML;
						document.getElementById("ques_new").style.display="";
					}
					command=command+"&service_id=" + service_id + "&type=" +type + "&suID=" +  col + "&ques_id=" + qid ;
					break;
				case "ajx_pos_up":
				case "ajx_pos_down":
					command=command+"&service_id=" + service_id + "&type=" +type + "&suID=" +  col + "&ques_id=" + qid + "&pos=" + pos;
					break;
				case "ajx_ques_save":
					if(validateFormQuestion(document.forms["add_question"]))
					{
						do_post_command("add_question",command+"&service_id=" + service_id + "&type=" +type + "&suID=" + col + "&ques_id=" + qid);
					}
					return;
				case "ajx_delete_choice":
					command=command+"&service_id=" + service_id + "&type=" +type + "&ques_id=" + col + "&chID=" + qid;
					break;
				case "ajx_question_details":
					if(prev_ques!='')
						close_question(prev_ques);
					document.getElementById("ques_new").innerHTML=document.getElementById("ajx_load_content_img").innerHTML;
					document.getElementById("ques_new").style.display="none";
					document.getElementById("ques_" + qid + "_edit").style.display="";
					command=command+"&service_id=" + service_id + "&type=" +type +  "&ques_id=" + qid + "&suID=" + col + "&pos=" + pos;
					prev_ques=qid;
					break;
				case "ajx_new_choice":
					if(validateFormChoice(document.forms["question_choice" + qid]))
					{
						do_post_command("question_choice" + qid,command+"&service_id=" + service_id + "&type=" +type + "&suID=" + col + "&ques_id=" + qid);
					}
					return;
			}
			do_page_ajax(command);
		}
		function move_updown(type,eve_id,e_type,sid,qid,sort_ord){
			var row=document.getElementById("ques_"+qid);
			var index=row.rowIndex;
			var count=row.parentNode.rows.length;
			if (type=="up" && index==0) return;
			if (type=="down" && index==count-2) return;

			if (type=="up"){
				row.parentNode.insertBefore(row,row.parentNode.rows[index-2]);
				var row=document.getElementById("ques_"+qid + "_row");
				var index=row.rowIndex;
				row.parentNode.insertBefore(row,row.parentNode.rows[index-2]);
			} else {
				if (index==row.parentNode.rows.length-4)
					row.parentNode.appendChild(row);
				else
					row.parentNode.insertBefore(row,row.parentNode.rows[index+4]);
				var row=document.getElementById("ques_"+qid + "_row");
				var index=row.rowIndex;
				if (index==row.parentNode.rows.length-4)
					row.parentNode.appendChild(row);
				else
					row.parentNode.insertBefore(row,row.parentNode.rows[index+4]);
			}
			realign_row_index(row.parentNode,'');
			realign_row_class(row.parentNode);
			do_page_fetch("ajx_pos_"+type,eve_id,e_type,sid,qid,sort_ord);
		}

		function do_result(result){ 
			var token=result.split('^^^^');
			if(token[0]=='message'){
				switch(token[1]){
					case 'copy_message':
			 			document.getElementById('copy_message_load').innerHTML=token[2];
						break;
					case 'save_message':
						document.getElementById('copy_message_load').innerHTML='';
						document.getElementById('copy_message_load').style.display='none';
						break;
				}
			
			}else{
			var cmd;
			var id;
			var icnt;
			var n;
			var sort_order;
			var cfg_key
			cmds_splt=result.split("{}");
			//alert(result);
			for (icnt=0,n=cmds_splt.length;icnt<n;icnt++){
				cmd_splt=cmds_splt[icnt].split("||");
				switch(cmd_splt[0]){
					case "type":
						type=cmd_splt[1];
						break;
					case "question_id":
						question_id=cmd_splt[1];
						break;
					case "choice_id":
						choice_id=cmd_splt[1];
						break;
					case "question_text":
						document.getElementById("ques_"+question_id + "_text").innerHTML=cmd_splt[1];
						break;
					case "ajx_service_survey":
						document.getElementById("survey_list").innerHTML=cmd_splt[1];
						break;
					case "delete_survey":
						close_new(type)
						break;
					case "result_new":
						document.getElementById("panel_new").innerHTML=cmd_splt[1];
						document.getElementById("panel_new").style.display="";
						document.getElementById("panel_new_action").style.display="";
						break;
					case "remove_question":
						table=document.getElementById("question_list");
						var row=document.getElementById("ques_"+question_id);
						var index=row.rowIndex;
						table.deleteRow(index);
						var row1=document.getElementById("ques_"+question_id + "_row");
						var index1=row1.rowIndex;
						table.deleteRow(index1);
						
						realign_row_index(table,'');
						realign_row_class(table);
						break;
					case "remove_choice":
						var row=document.getElementById("ques_choice_"+choice_id);
						var index=row.rowIndex;
						table=document.getElementById("question_choice_" + question_id);
						table.deleteRow(index);
						realign_row_index(table,'. ');
						break;
					case "add_choice":
						table=document.getElementById("question_choice_" + question_id);
						temp=cmd_splt[1]
						rowIndex=table.rows.length-1;
						var r=table.insertRow(rowIndex);
						var c0=r.insertCell(0);
						c0.className="smallText";
						var c1=r.insertCell(1);
						c1.innerHTML=temp;
						r.id="ques_choice_" + choice_id;
						realign_row_index(table,'. ');
						form_name=document.forms["question_choice" + question_id];
						form_name.choice_text.value='';
						break;
					case "result_content":
						document.getElementById(type + "_row").style.display="none";
						document.getElementById("panel_"+type+"_row").innerHTML=cmd_splt[1];
						break;
					case "ques_result_content":
						if(question_id!='')
						{
							document.getElementById("ques_" + question_id ).style.display="none";
							document.getElementById("ques_" + question_id + "_edit").innerHTML=cmd_splt[1];
						}
						else
							document.getElementById("ques_new").innerHTML=cmd_splt[1];
						break;

				}
			}
			is_fetching=false;
		  }	
		}
	function realign_row_index(table,delimit){
			var icnt,n,cnt;
			cnt=1;
			if(table.rows.length>1)
			{
				for (icnt=0,n=table.rows.length;icnt<n;icnt++)
				{
					var row_id=table.rows[icnt].id;
					if(row_id.indexOf("_row")<0)
					{
						table.rows[icnt].cells[0].innerHTML=cnt + delimit;
						cnt++;
					}
				}
			}
			else
			{
				for (icnt=0,n=table.rows.length;icnt<n;icnt++)
				{
				  if(table.rows.length==1)
					 table.rows[icnt].cells[0].innerHTML=1;
				} 
			}
		}
		function realign_row_class(table){
			var icnt,n;
			if(table.rows.length>1)
			{
				table.rows[0].className="dataTableRowOdd1";
				for (icnt=2,n=table.rows.length;icnt<n;icnt=icnt+2){
					if (table.rows[icnt].opened) continue;
					if (table.rows[icnt].className.indexOf("hover")!=-1) continue;
					if (table.rows[icnt-2].className=="dataTableRowEven1"){
						table.rows[icnt].className="dataTableRowOdd1";
					} else {
						table.rows[icnt].className="dataTableRowEven1";
					}
				}
			}
		}
</Script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<div id="spiffycalendar" class="text"></div>
	<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
	<!-- header_eof //-->
	<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2" >
	<tr> 
		<!-- body_text //-->	  
		<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10');?></td></tr>
			<tr>
				<td><?php echo tep_draw_form('select_service',FILENAME_SERVICES_SURVEYS); 
							$selected_name="";	?>
				<table border="0" cellspacing="0" cellpadding="2">
					<div id="service_list" style="position:absolute;left:242px;top:130px;display:none;width:400px">
						<?php echo tep_draw_service_select_menu('select_id',$services_array,$service_id,'size=10 onClick="javascript:do_page_fetch(\'ajx_service_survey\',this);setListboxText(0);toggleList(0);" onFocus="javascript:toggleList(1);" onBlur="javascript:toggleList(0);" onchange="" class="customerSelect" style="width:400px"');?>
					</div>
					<tr height=20>
						<td class="smalltext" width="100"><?php echo TEXT_SELECT_SERVICE;?></td>
						<td class="smalltext"><?php echo tep_draw_input_field('select_text',$selected_name,'onFocus="javascript:toggleText(1);" onBlur="javascript:toggleText(0);" style="width:400px"');?></td>
					</tr>
				</table>
				</form>
				<script language="JavaScript">
					if(na6) document.select_service.select_text.addEventListener("keyup",selectList,true);
					if(iex) document.select_service.select_text.onkeyup=selectList;
					if(na6) document.select_service.select_text.addEventListener("keydown",scrollList,true);
					if(iex) document.select_service.select_text.onkeydown=scrollList;
				</script>
				</td>
			</tr>
		<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10');?></td></tr>
		<tr><td id="survey_list"><?php tep_services_survey_list($service_id);?></td></tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
     </table>
	<div id="ajx_load_content_img" style="display:none"><div style="height:30px;margin-left:30px"><img src="images/24-1.gif"></div>
<!-- body_eof //-->
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php');
function update_service_survey($update_id)
{
	global $HTTP_POST_VARS;
	$status = $HTTP_POST_VARS["survey_visibility_flag"];
	if(empty($status)) $status='0';
    $sql_array=array('survey_name'=>tep_db_prepare_input($HTTP_POST_VARS['survey_name']),
	         'survey_desc'=>tep_db_prepare_input($HTTP_POST_VARS['survey_desc']),
			 'survey_time_period'=>tep_db_prepare_input($HTTP_POST_VARS['survey_time_period']),
			 'survey_expiration_date'=>tep_db_prepare_input(tep_convert_date_raw($HTTP_POST_VARS['survey_expiration_date'])),
			 'survey_visibility_flag'=>tep_db_prepare_input($status));
	tep_db_perform(TABLE_SERVICES_SURVEYS,$sql_array,'update','survey_id="' . $update_id . '"');
}
function insert_service_survey($service_id,$type)
{
	global $HTTP_POST_VARS;
	$status = $HTTP_POST_VARS["survey_visibility_flag"];
	if(empty($status)) $status='0';
	$sql_array=array('survey_type'=>tep_db_prepare_input($type),
						'service_id'=>$service_id,
						'survey_name'=>tep_db_prepare_input($HTTP_POST_VARS['survey_name']),
						'survey_desc'=>tep_db_prepare_input($HTTP_POST_VARS['survey_desc']),
						'survey_time_period'=>tep_db_prepare_input($HTTP_POST_VARS['survey_time_period']),
						'survey_expiration_date'=>tep_db_prepare_input(tep_convert_date_raw($HTTP_POST_VARS['survey_expiration_date'])),
						'survey_visibility_flag'=>tep_db_prepare_input($status)
					);
	tep_db_perform(TABLE_SERVICES_SURVEYS,$sql_array);
}
function delete_service_survey($delete_id)
{
	tep_delete_service_survey($delete_id);
}
function change_service_survey_status($suid)
{
	tep_db_query("UPDATE " . TABLE_SERVICES_SURVEYS . " set survey_visibility_flag=not survey_visibility_flag where survey_id='" . (int)$suid . "'");
}
function survey_question_save($suID)
{
 global $HTTP_POST_VARS;
	$required = $HTTP_POST_VARS["question_required"];
	$question_type=$HTTP_POST_VARS['question_type'];
	$question_required=($question_type!=6?$required:0);
     $sql_array=array('question_text'=>tep_db_prepare_input($HTTP_POST_VARS['question_text']),
	                 'question_type'=>tep_db_prepare_input($question_type),
					 'question_required'=>tep_db_prepare_input($question_required),
					 'survey_id'=>tep_db_prepare_input($suID));
	if ($HTTP_POST_VARS['question_id']!=''){
	    tep_db_perform(TABLE_SERVICES_SURVEYS_QUESTION,$sql_array,'update','question_id="' . (int)$HTTP_POST_VARS['question_id'] . '"');
	    if (!($question_type>=1 && $question_type<=3)){
	       tep_db_query('DELETE from ' . TABLE_SERVICES_SURVEYS_CHOICE . ' where question_id="' . (int)$HTTP_POST_VARS['question_id'] . '"');
	    }
	}    
	else
	{
   	    $order_check=tep_db_query("SELECT max(sort_order) as last_id from " . TABLE_SERVICES_SURVEYS_QUESTION . " where survey_id='" . (int)$suID ."'");
	    $order_result=tep_db_fetch_array($order_check);
	    $sort_order=((int)$order_result['last_id'])+1;
	    $sql_array['sort_order']=tep_db_prepare_input($sort_order);
	    tep_db_perform(TABLE_SERVICES_SURVEYS_QUESTION,$sql_array);
	}
}
function tep_services_survey_list($service_id)
{
global $survey_service;?>
	<table border="0" width="100%" cellspacing="0" cellpadding="0" valign="top">
		<tr>
			<td class="pageHeading"><?php echo sprintf(TEXT_HEADING_TITLE,tep_get_service_name($service_id)); ?></td>  
		</tr>
		<tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','10'); ?></td></tr>
		<tr class="dataTableHeadingRow"><td class="main"><?php echo TEXT_SURVEY_TYPE ;?></td></tr>
		<tr><td></td></tr>
<?php
   while(list($type,$value)=each($survey_service)){
        $sql="SELECT survey_name,survey_id,survey_visibility_flag,survey_expiration_date,survey_type from " . TABLE_SERVICES_SURVEYS . " where service_id='" . (int)$service_id . "' and survey_type='" . $type . "'";
        $survey_query = tep_db_query($sql);
		$cnt=tep_db_num_rows($survey_query);
		$survey_name="";
		if ($cnt>0){
			  $surveys = tep_db_fetch_array($survey_query); 
			  $survey_name=$surveys['survey_name'];
		}
		$disp_class=($disp_class=="dataTableRowOdd")?"dataTableRowEven":"dataTableRowOdd";?>
		<tr id="<?php echo $type;?>_row" height=25 onMouseOver="rowOverEffect(this)" onMouseOut="rowOutEffect(this)" class="<?php echo $disp_class;?>" style="cursor:hand" onClick="javascript:do_page_fetch('ajx_survey_details','<?php echo $service_id; ?>','<?php echo $type; ?>');">
	   <td class="dataTableContent">
	   <?php if ($cnt>0){
			  /*if ($surveys['survey_visibility_flag'] == '1') {
					echo tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', IMAGE_INSTALLED, '', '','align=absmiddle');
			  } else {
					echo tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', IMAGE_REMOVED, '', '','align=absmiddle');
			  } */
			  echo '&nbsp;' . $value;
			}
			else
			{
				//echo tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', IMAGE_REMOVED, '', '','align=absmiddle');
				echo '&nbsp;' . $value;
			}?>
			 </td>
  		</tr>
		<tr  class="openContent1">
			<td height=25 id="panel_<?php echo $type; ?>_row" style="display:none">
				<?php echo tep_image(DIR_WS_IMAGES . '24-1.gif') ;?>
			</td>
		</tr>
<?php }?>
	 <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
  		 <tr>
			<td><?php //echo '<a href="' . tep_href_link(FILENAME_SERVICES_COPY_SURVEYS,'suID=' . $service_id) . '">' . tep_image_button("button_copy_surveys.gif",IMAGE_COPY_SURVEYS) . '</a>';
				$button_text='<div style="padding-top:10px;"><a href="javascript: copy_message('.$service_id.');">' . tep_image_button('button_copy_messages.gif',IMAGE_COPY_MESSAGES) . "</a></div>";
				echo $button_text; 
			?>
			</td>		
		 </tr>
		
		<tr><td><br></td></tr>
		<tr>
		<td class="openContent" id="copy_message_load"></td>
		</tr>
	</table>
<?php } 

function tep_new_survey_service($service_id,$type)
{
global $survey_service;?>
	<table border="0" width="100%" cellspacing="0" cellpadding="5">
		<tr class="contentTitle" >
			<td class="smallText" style="color:#000000;margin-left:20px;" onClick="javascript:close_new('<?php echo $type; ?>');"><b><?php echo $survey_service[$type]; ?></b></td>
			<td align=right>
			<?php
				echo '<a href="javascript:do_page_fetch(\'ajx_new\',\'' . $service_id . '\',\''  . $type. '\')">' . tep_image('images/icons/edit_green.gif') . "</a>&nbsp;";
				echo '<a href="javascript:close_new(\''  . $type. '\');">' . tep_image('images/icons/cancel.gif') . "</a>";
			?>
			</td>
		 </tr>
		<?php 
				$parameters=array('survey_name'=>'',
				'survey_desc'=>'',
				'survey_time_period'=>'',
				'survey_expiration_date'=>date('Y-m-d',time()),
				'survey_visibility_flag'=>'0');
				$suInfo=new objectInfo($parameters);
				$survey_service=array('RV'=>TEXT_RV,'WV'=>TEXT_WV,'FV'=>TEXT_FV,'CD'=>TEXT_CD);
		?>
			<tr>
				<td><?php echo tep_draw_form('new_survey',FILENAME_SERVICES_SURVEYS,'','post'); ?>
					<table border="0" cellspacing="2" cellpadding="5" width=90%>
						<tr>
							<td class="smalltext" width=100><?php echo TEXT_SURVEY_SERVICE; ?></td>
							<td class="smalltext"><?php echo tep_get_service_name($service_id); ?></td>
						</tr>
						<tr>
							<td class="smalltext" width=100><?php echo TEXT_SURVEY_TITLE; ?></td>
							<td class="smalltext"><?php echo tep_draw_input_field('survey_name','','size=40'); ?></td>
						</tr>
						<tr valign="top">
							<td class="smalltext"><?php echo TEXT_SURVEY_DESCRIPTION; ?></td>
							<td class="smalltext"><?php echo tep_draw_textarea_field('survey_desc','','40','4'); ?></td>
						</tr>
						<tr><?php $_array=array('d','m','Y');  $replace_array=array('DD','MM','YYYY'); 	$date_format=str_replace($_array,$replace_array,serviceS_DATE_FORMAT);?>
							<td class="smalltext"><?php echo TEXT_SURVEY_EXPIRES; ?></td>
							<td class="smalltext"><?php echo tep_draw_input_field("survey_expiration_date",format_date($suInfo->survey_expiration_date),"size=10",false,'text',false);?>
							<a href="javascript:show_calendar('new_survey.survey_expiration_date',null,null,'<?php echo $date_format;?>');"
						      onmouseover="window.status='Date Picker';return true;"
						      onmouseout="window.status='';return true;"><img border="none" src="images/icon_calendar.gif"/>  
						    </a><small>(YYYY-MM-DD)</small>
							&nbsp;&nbsp;<?php echo TEXT_SURVEY_LIMIT; ?>
							<?php echo tep_draw_input_field('survey_time_period','','size=6'); ?>
							<?php echo tep_draw_checkbox_field('survey_visibility_flag','1') . '&nbsp;' . TEXT_SURVEY_VISIBLE;?></td>
						</tr>
					</table></form>
					</td>
				</tr>
				<tr  >
			<td colspan=2 align=right>
			<?php
				echo '<a href="javascript:do_page_fetch(\'ajx_new\',\'' . $service_id . '\',\''  . $type. '\')">' . tep_image('images/icons/edit_green.gif') . "</a>&nbsp;";
				echo '<a href="javascript:close_new(\''  . $type. '\');">' . tep_image('images/icons/cancel.gif') . "</a>";
			?>
			</td>
		 </tr>
		<tr><td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif','1','5'); ?></td></tr>
	</table>
<?php }
function tep_get_survey_edit($service_id,$type,$is_update=false)
{
	$survey_type_query=tep_db_query("select * from " . TABLE_SERVICES_SURVEYS . " where service_id='" . (int)$service_id . "' and survey_type='" . $type . "'");
    $result=tep_db_fetch_array($survey_type_query);
	tep_get_survey_header($service_id,$type,$result,true);
	echo tep_draw_form('new_survey',FILENAME_SERVICES_SURVEYS,'','post'); ?>
		<table border="0" cellspacing="2" cellpadding="5" width=100%>
			<tr><td class="smallText" colspan="2"><b><?php echo TEXT_SURVEY_SETTINGS;?></b></td></tr>
			<tr>
				<td class="smalltext" width=100><?php echo TEXT_SURVEY_SERVICE; ?></td>
				<td class="smalltext"><?php echo tep_get_service_name($service_id); ?></td>
			</tr>
			<tr>
				<td class="smalltext" width=100><?php echo TEXT_SURVEY_TITLE; ?></td>
				<td class="smalltext"><?php echo tep_draw_input_field('survey_name',$result['survey_name'],'size=40'); ?></td>
			</tr>
			<tr valign="top">
				<td class="smalltext"><?php echo TEXT_SURVEY_DESCRIPTION; ?></td>
				<td class="smalltext"><?php 
				echo tep_draw_textarea_field('survey_desc','soft','40','4',$result['survey_desc']); ?></td>
			</tr>
			<tr><?php $_array=array('d','m','Y');  $replace_array=array('DD','MM','YYYY'); 	$date_format=str_replace($_array,$replace_array,serviceS_DATE_FORMAT);?>
				<td class="smalltext"><?php echo TEXT_SURVEY_EXPIRES; ?></td>
				<td class="smalltext"><?php echo tep_draw_input_field("survey_expiration_date",format_date($result['survey_expiration_date']),"size=10",false,'text',false);?>
				<a href="javascript:show_calendar('new_survey.survey_expiration_date',null,null,'<?php echo $date_format;?>');"
				  onmouseover="window.status='Date Picker';return true;"
				  onmouseout="window.status='';return true;"><img border="none" src="images/icon_calendar.gif"/>  
				</a><small>(YYYY-MM-DD)</small></td></tr>
			<tr><td></td><td class="smallText">
				&nbsp;&nbsp;<?php echo TEXT_SURVEY_LIMIT; ?>
				<?php echo tep_draw_input_field('survey_time_period',$result['survey_time_period'],'size=6'); ?>
				<?php echo tep_draw_checkbox_field('survey_visibility_flag','1',$result['survey_visibility_flag']==1?true:false) . '&nbsp;' . TEXT_SURVEY_VISIBLE;?></td>
			</tr></table>
			</form>
	<?php tep_get_survey_footer($service_id,$type,$result,true);
  }
 
 function tep_get_survey_header($service_id,$type,$result,$is_update)
 {
 global $survey_service;?>
 	<Table width="100%" cellpadding="0" cellspacing="0">
	<tr><td >
	<Table width=100% align="center" cellpadding="0" cellspacing="0" border="0">
		<tr class="contentTitle">
			<td class=smallText style="color:#000000" width="1%">
			<?php if ($result['survey_visibility_flag'] == '1') {
					echo '<a href="javascript:do_page_fetch(\'ajx_setflag\',\''.$service_id . '\',\'' . $type. '\',\'' .$result['survey_id'] . '\');">' . tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', 'Set Inactive', '', '','align=absmiddle') . '</a>';
			  } else {
					echo '<a href="javascript:do_page_fetch(\'ajx_setflag\',\''.$service_id . '\',\'' . $type. '\',\'' .$result['survey_id'] . '\');">' . tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', 'Set Active', '', '','align=absmiddle') .'</a>';
			  } ?>
			<td width="50%" valign="top" onClick="javascript:close_new('<?php echo $type; ?>')"><b ><?php echo '&nbsp;&nbsp;'.$survey_service[$type];?></b></td>
		<td align="center" width="10%">
		<?php  echo ' 
		<table border="0" cellpadding="0" cellspacing="0" style="width:100;height:30;display:inline;position:relative;top:5px;">
			<tr id="img_ids">
				<td width="10" class="cell_bg_popm_left">&nbsp;</td>' .
				'<td width="20%" style="background:#DEE4F5;"><a href="' .tep_href_link(FILENAME_SERVICES_REPORTS_SURVEYS_SUMMARY,'service_id=' . $service_id . '&survey_type=' . $result['survey_type'] . '&survey_in=1') . '">' . tep_image('images/icons/copy_blue.gif','','','','') . "</a></td>" . 
				(($is_update)? '<td width="10%" style="background:#DEE4F5;" OnClick="javascript:do_hover_change(this,1);do_page_fetch(\'ajx_update_survey\',\''.$service_id . '\',\'' . $type. '\',\'' .$result['survey_id'] . '\')";>' . tep_image(DIR_WS_IMAGES . 'template/img_edit_hover.gif',IMAGE_MODIFY_SURVEY,'','','id="img_edit"') . '</td>':'<td width="10%" style="background:#DEE4F5;" OnClick="javascript:do_hover_change(this,1);do_page_fetch(\'ajx_update_survey\',\''.$service_id . '\',\'' . $type. '\',\'' .$result['survey_id'] . '\')" onMouseOver="javascript:do_hover_change(this,2);" onMouseOut="javascript:do_hover_change(this,3);">' . tep_image(DIR_WS_IMAGES . 'template/img_edit.gif',IMAGE_EDIT,'','','id="img_edit"') . '</td>') .  
				//'<a href="javascript:do_page_fetch(\'ajx_update_survey\',\''.$service_id . '\',\'' . $type. '\',\'' .$result['survey_id'] . '\');" onMouseOver="javascript:do_hover_change(this,2);" onMouseOut="javascript:do_hover_change(this,1);">' . tep_image(DIR_WS_IMAGES . 'template/img_edit.gif',IMAGE_MODIFY_SURVEY) . "</a>&nbsp;";
				'<td  width="10%" style="background:#DEE4F5;" OnClick="javascript:do_hover_change(this,1);do_page_fetch(\'ajx_delete\',\'' . $service_id . '\',\'' . $type. '\',\'' . $result['survey_id'] . '\');" onMouseOver="javascript:do_hover_change(this,2);" onMouseOut="javascript:do_hover_change(this,3);">' . tep_image(DIR_WS_IMAGES . 'template/img_trash.gif',IMAGE_DELETE_SURVEY) . "</td>" . 
				'<td width="10" class="cell_bg_popm_right">&nbsp;</td>
			</tr>
		</table>';?>
		</td>
		<td width="350" onClick="javascript:close_new('<?php echo $type; ?>')">&nbsp;</td>
		<td id="show_edit" align="right" width="75">
			<?php
				if($is_update)
				{
				echo '<a href="javascript:do_page_fetch(\'ajx_save\',\'' . $service_id . '\',\''  . $type. '\',\'' . $result['survey_id']. '\')">' . tep_image('images/icons/edit_green.gif') . "</a>&nbsp;";
				echo '<a href="javascript:do_page_fetch(\'ajx_survey_details\',\'' . $service_id . '\',\''  . $type. '\');">' . tep_image('images/icons/cancel.gif') . "</a>";
				}?>
		 </td>
		 </tr>
		 <div class="main" id="edit_survey" style="display:none"></div>
		 <div  class="main" id="delete_survey" style="display:none"></div>
		 <tr><td colspan="4"  <?php if(!$is_update) echo "id='show_edit_scr'"?>>
 <?php }
 
 function tep_get_survey_footer($service_id,$type,$result,$is_update)
 {?>
 </td></tr>
			<tr>
			<td width="350">&nbsp;</td>		
			<td colspan=4 width="985" align="right" <?php if(!$is_update) echo "style='display:none;'"?>>
			<?php
				echo '<a href="javascript:do_page_fetch(\'ajx_save\',\'' . $service_id . '\',\''  . $type. '\',\'' . $result['survey_id']. '\')">' . tep_image('images/icons/edit_green.gif') . "</a>&nbsp;";
				echo '<a href="javascript:close_new(\''  . $type. '\');">' . tep_image('images/icons/cancel.gif') . "</a>";
			?>
			</td>
		</tr>
		</table>
	</td></tr></table>
 <?php }

function tep_get_survey_details($service_id,$type,$is_update=false)
{
 global $survey_service,$choice_array;
	$survey_type_query=tep_db_query("select * from " . TABLE_SERVICES_SURVEYS . " where service_id='" . (int)$service_id . "' and survey_type='" . $type . "'");
	$result_cnt=tep_db_num_rows($survey_type_query);
	?>
	<?php 
		if ($result_cnt<=0){?>
		<Table width="100%" cellpadding="0" cellspacing="0">
		<tr><td >
		<Table width=100% align="center" cellpadding="0" cellspacing="0">
		  <tr>
		  	<td colspan="2">
				<?php tep_new_survey_service($service_id,$type);?>
			</td>
		  </tr>
		  </Table></td></tr>
		</table>
		<?php }else{
			$result=tep_db_fetch_array($survey_type_query);
			tep_get_survey_header($service_id,$type,$result,$is_update)?>
			<table width="100%" cellpadding="5">
				<tr><td width="50%" valign="top">
				  <table cellpadding="2" width="100%">
					<tr>
						<td class="smallText" valign="top"><b><?php echo TEXT_SURVEY_TITLE;?></b>  <?php echo $result['survey_name'];?></td>
						<td class="smallText" valign="top"><b><?php echo TEXT_SURVEY_DESCRIPTION;?></b>  <?php echo substr($result['survey_desc'],0,25) . ' ...';?></td>
						<td class="smallText" width="15%" nowrap><b><?php echo TEXT_SURVEY_EXPIRES;?></b>  <?php echo $result['survey_expiration_date'];?></td>
					</tr>
					<tr>
						<td colspan="2">&nbsp;</td>
						<td class="smallText"><b><?php echo TEXT_SURVEY_LIMIT;?></b>  <?php echo $result['survey_time_period'];?>&nbsp;<?php echo TEXT_MONTHS;?></td>
					</tr>
				  </table>
				</td></tr>
			<tr><td  valign="top" width="50%">
			<table border=0 cellspacing="0" cellpadding="2" valign=top width=100%>
			 <tr>
		    	  <td class="smallText"><b><?php echo TEXT_SURVEY_QUESTIONS; ?></b>&nbsp;&nbsp;
				  <?php	echo '<a href="javascript:do_page_fetch(\'ajx_update_ques\',\''.$service_id . '\',\'' . $type. '\',\'' .$result['survey_id'] . '\',\'\');">' . tep_image_button('button_insert.gif',IMAGE_QUESTION_ADD) . "</a>&nbsp;&nbsp;&nbsp;";?>
					</td>
				  </tr>
				  <tr >
					<td id="ques_new" style="display:none"></td>
				</tr>
				<Tr>
				 <td>
				 <Table width="100%" cellspacing=0 id="question_list">
				<?php $question_query=tep_db_query("SELECT * from " . TABLE_SERVICES_SURVEYS_QUESTION . " where survey_id='" . (int)$result['survey_id'] . "' order by sort_order,question_text");
				 $id=1;
			     $qu_count=tep_db_num_rows($question_query);
				 if ($qu_count>0){
				   	while($question_result=tep_db_fetch_array($question_query)){ 
						$disp_class=($disp_class=="dataTableRowOdd1")?"dataTableRowEven1":"dataTableRowOdd1";?>
						<tr id="ques_<?php echo $question_result['question_id'];?>" height=25 onMouseOver="rowOverEffect1(this)" onMouseOut="rowOutEffect1(this)" class="<?php echo $disp_class;?>" style="cursor:hand" onClick="javascript:do_page_fetch('ajx_question_details','<?php echo $service_id; ?>','<?php echo $type; ?>','<?php echo $question_result['survey_id'];?>','<?php echo $question_result['question_id'];?>','<?php echo $id?>');">
						<td class="SmallText" width="2%" nowrap><?php echo $id ;?></td>
						<td>
							<table border="0" cellpadding="2" cellspacing="0" width=100%>
							  <tr valign=top >
								<td class="SmallText" id="ques_<?php echo $question_result['question_id'];?>_text"><?php echo $question_result['question_text'] ;?></td>
							  </tr>
							 </table>
					 	 </td>
					  </tr>
  					  <tr  id="ques_<?php echo $question_result['question_id'];?>_row" ><td colspan="2" height=25 id="ques_<?php echo $question_result['question_id'];?>_edit" style="display:none;"><?php echo tep_image(DIR_WS_IMAGES . '24-1.gif') ;?></td></tr>
				   <?php $id++;}
	   			 } else { ?>
           			<tr id="no_questions"><td colspan="2">
		   				<table border="0" cellpadding="2" width="100%">
	           			<tr><td class="smallText"><?php echo TEXT_NO_QUESTIONS; ?></td></tr>
			   			 </table>
		    		</td></tr>
       			<?php }?> </Table>
				</td></Tr>
		     <tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','5'); ?></td></tr>
	   </table></td></tr></table>
	   <?php tep_get_survey_footer($service_id,$type,$result,$is_update);
		 } ?>
<?php }

function tep_get_question_details($service_id,$type,$suid,$qid,$is_update)
{
global $choice_array;
	$action='insert';?>
		<table border="0" cellpadding="2" cellspacing="0" width=100%>
		  <tr valign=top class="contentTitle">
		  <?php	if($qid!='')
			{
				$action='update';
				$question_query=tep_db_query("SELECT * from " . TABLE_SERVICES_SURVEYS_QUESTION . " where question_id='" . (int)$qid . "' order by sort_order,question_text");
				$question_result=tep_db_fetch_array($question_query);?>
				<td class="SmallText" style="color:#000000" width="50%"><b>
				<?php echo '<a href="javascript:move_updown(\'up\',\'' . $service_id  . '\',\'' . $type . '\',\'' . $question_result['survey_id'] . '\',\'' .  $question_result['question_id'] . '\',\'' . $question_result['sort_order'] . '\');">' . tep_image(DIR_WS_IMAGES . '/template/img_arrow_up.gif', IMAGE_ORDER_UP) . '</a>&nbsp;';
					  echo '<a href="javascript:move_updown(\'down\',\'' . $service_id  . '\',\'' . $type . '\',\'' . $question_result['survey_id'] . '\',\'' .  $question_result['question_id'] . '\',\'' . $question_result['sort_order'] . '\');">' . tep_image(DIR_WS_IMAGES . '/template/img_arrow_down.gif', IMAGE_ORDER_DOWN) . '</a>&nbsp;';
				      echo $question_result['question_text'] . '</b>';?>
				</td>
			
			<?php 	echo "<td width=10%>";
					echo '<table border="0" cellpadding="0" cellspacing="0" style="width:100;height:30;display:inline;position:relative;top:5px;">
						<tr id="image_ids">
						<td width="10" class="cell_bg_popm_left">&nbsp;</td>' .
						(($is_update)? '<td width="10%" style="background:#DEE4F5;" OnClick="javascript:do_hover_change1(this,1);do_page_fetch(\'ajx_update_ques\',\''.$service_id . '\',\'' . $type. '\',\'' .$question_result['survey_id'] . '\',\'' . $question_result['question_id'] . '\',\'' . $qno . '\')";>' . tep_image(DIR_WS_IMAGES . 'template/img_edit_hover.gif',IMAGE_QUESTION_EDIT,'','','id="img_edit"') . '</td>':'<td width="10%" style="background:#DEE4F5;" OnClick="javascript:do_hover_change1(this,1);do_page_fetch(\'ajx_update_ques\',\''.$service_id . '\',\'' . $type. '\',\'' .$question_result['survey_id'] . '\',\'' . $question_result['question_id'] . '\',\'' . $qno . '\')" onMouseOver="javascript:do_hover_change1(this,2);" onMouseOut="javascript:do_hover_change1(this,3);">' . tep_image(DIR_WS_IMAGES . 'template/img_edit.gif',IMAGE_QUESTION_EDIT,'','','id="img_edit"') . '</td>') .  
						//echo '<a href="javascript:do_page_fetch(\'ajx_update_ques\',\''.$service_id . '\',\'' . $type. '\',\'' .$question_result['survey_id'] . '\',\'' . $question_result['question_id'] . '\',\'' . $qno . '\');" onMouseOver="javascript:do_hover_change(this,2);" onMouseOut="javascript:do_hover_change(this,1);">' . tep_image(DIR_WS_IMAGES . 'template/img_edit.gif',IMAGE_QUESTION_EDIT) . "</a>&nbsp;";
					 	'<td style="background:#DEE4F5;" OnClick="javascript:do_hover_change1(this,1);javascript:do_page_fetch(\'ajx_delete_ques\',\'' . $service_id . '\',\'' . $type. '\',\'' . $question_result['question_id'] . '\');" onMouseOver="javascript:do_hover_change1(this,2);" onMouseOut="javascript:do_hover_change1(this,3);">' . tep_image(DIR_WS_IMAGES . 'template/img_trash.gif',IMAGE_QUESTION_DELETE) . "</td>".
						'<td width="10" class="cell_bg_popm_right">&nbsp;</td>
					</tr>
					</table>';
					echo "</td>";
				}?>
				<td class="smallText" colspan="1" align=right>
				 <?php if($is_update){?>
						<?php 
							echo '<a href="javascript:do_page_fetch(\'ajx_ques_save\',\'' . $service_id . '\',\''  . $type. '\',\'' . $suid. '\',\'' . $qid .'\')">' . tep_image('images/icons/edit_green.gif',IMAGE_QUESTION_UPDATE) . "</a>&nbsp;";
							echo '<a href="javascript:close_ques_new(\'' . $action .'\',\'' . $qid .'\');">' . tep_image('images/icons/cancel.gif') . "</a>";
						?>
				  <?php }?>
				 </td>
			   </tr>
				 
				 <div class="main" id="edit_question" style="display:none"></div>
				 <div  class="main" id="delete_question" style="display:none"></div>
			   
			   <Tr><td colspan="4" id="question_update_details" width="100%" >
			   	<Table width="99%" cellpadding="5" cellspacing="0" align="center" >
			   <?php if($is_update){?>
			   <tr><td >
			   	<?php update_question_form($service_id,$type,$question_result['survey_id'],$qid);?>
			   </td></tr>
			   <?php }
			   else
			   {
				  echo '<tr><TD class="SmallText" >' . $choice_array[$question_result['question_type']] . ($question_result['question_required']==1?', ' . TEXT_REQUIRED:'') . '</td></tr>';
				}?>
               <?php if ($question_result['question_type']>=1 && $question_result['question_type']<=3) { ?>
   			     <tr><td class="SmallText" ><u><?php echo TEXT_CHOICES; ?></u></td></tr>
		      	 <tr>
		         <td><table border="0" cellpadding="2" cellspacing="0" valign=top id="question_choice_<?php echo $question_result['question_id'];?>">
				 <?php $choice_query=tep_db_query("SELECT choice_id,choice_text from " . TABLE_SERVICES_SURVEYS_CHOICE . " where question_id='" . (int)$question_result['question_id'] . "'order by choice_id");
			       $qn_id=1;
				   if(tep_db_num_rows($choice_query)>0)
				   {
					   while($choice_result=tep_db_fetch_array($choice_query)){	?>
						  <tr valign=middle id="ques_choice_<?php echo $choice_result['choice_id'];?>">
						  <td class="SmallText"><?php echo $qn_id . '. ';?></td>
						  <td>
							 <table><tr>
							   <td class="SmallText"><?php echo $choice_result['choice_text'];?></td>
							   <?php if($is_update){
									echo "<td>";
									echo '<a href="javascript:do_page_fetch(\'ajx_delete_choice\',\'' . $service_id . '\',\'' . $type. '\',\'' . $question_result['question_id'] . '\',\'' . $choice_result['choice_id'] . '\');">' . tep_image('images/icons/cross.gif','') . "</a>&nbsp;&nbsp;&nbsp;";
									echo "</td>";
								}?>
							 </tr>
							</table>
						</td></tr>
					<?php $qn_id++;}
				} 
				elseif(!$is_update)
				{
					echo '<Tr><td class="smalltext">No choice found</td></tr>';
				}?>
				<?php if($is_update){
			         echo '<tr valign=middle><td class="smallText">' . $qn_id. '. </td><td class="smallText">';
					 echo tep_draw_form('question_choice' . $question_result['question_id'],FILENAME_SERVICES_SURVEYS,'','post','');
 					 echo tep_draw_input_field('choice_text','','size=30');
 					 //echo tep_draw_hidden_field('choice_text1','','size=30');
					 echo "</form>&nbsp;";
					 echo '<a href="javascript:do_page_fetch(\'ajx_new_choice\',\''.$service_id . '\',\'' . $type. '\',\'' .$question_result['survey_id'] . '\',\'' . $question_result['question_id'] . '\');">' . tep_image('images/icons/tick.gif',IMAGE_CHOICE_INSERT,'','','align=absmiddle') . "</a></td></tr>";
				}?>
				</table></td></tr>
			<?php }?>
			<tr><td class="smallText" colspan="4" align=right width="100%">
			<?php if($is_update){?>
						<?php 
							echo '<a href="javascript:do_page_fetch(\'ajx_ques_save\',\'' . $service_id . '\',\''  . $type. '\',\'' . $suid. '\',\'' . $qid .'\')">' . tep_image('images/icons/edit_green.gif',IMAGE_QUESTION_UPDATE) . "</a>&nbsp;";
							echo '<a href="javascript:close_ques_new(\''. $action .'\',\'' . $qid .'\');">' . tep_image('images/icons/cancel.gif') . "</a>";
							echo tep_draw_hidden_field('question_id',$qid);
						?>
				<?php }?>
				</td></tr>
				</Table>
			   </td></Tr>
			</table>
	<?php if($qid!='')
			echo "{}question_text||" . $question_result['question_text'];
	}
function update_question_form($service_id,$type,$suID,$ques_id)
{
$choice_array=array(array('id'=>'1','text'=>TEXT_MULTIPLE_RADIO),
                    array('id'=>'2','text'=>TEXT_MULTIPLE_PULLDOWN),
				   array('id'=>'3','text'=>TEXT_MULTIPLE_CHECKBOX),
				   array('id'=>'4','text'=>TEXT_INPUT_ONELINE),
				   array('id'=>'5','text'=>TEXT_INPUT_THREELINES),
				   array('id'=>'6','text'=>TEXT_INPUT_HEADING),
				   array('id'=>'7','text'=>TEXT_INPUT_DATE));
     $question_edit_array=array('question_text'=>'',
                               'question_type'=>'1',
							'question_required'=>'1');
	 $question_action='insert';
	 $qInfo=new objectInfo($question_edit_array);
	 if ($ques_id!=''){
	   $question_edit_query=tep_db_query("SELECT question_text,question_type,question_required,sort_order from " . TABLE_SERVICES_SURVEYS_QUESTION . " where question_id='" . (int)$ques_id ."'");
	   $question_edit_result=tep_db_fetch_array($question_edit_query);
	   $qInfo=new objectInfo($question_edit_result);
	   $question_action='update';
	 }
	 echo tep_draw_form('add_question',FILENAME_SERVICES_SURVEYS,'','post','onSubmit="return validateFormQuestion(this)"');
	 echo tep_draw_hidden_field('question_id',$ques_id);
    ?><table border="0" cellpadding="2" width="100%">
	<tr><td class="smalltext" nowrap><b><?php if($ques_id=='') echo TEXT_ADD_QUESTION;?></b></td></tr>
	 <tr><td class="smallText" width=100><?php echo TEXT_QUESTION;?></td>
	 <td class="smallText"><?php echo tep_draw_input_field('question_text',$qInfo->question_text,'size=40');?></td> 
	 </tr>
	 <tr>
		 <td class="smallText"><?php echo TEXT_QUESTION_TYPE;?></td>
		 <td class="smallText">
	<?php     echo tep_draw_pull_down_menu('question_type',$choice_array,$qInfo->question_type); ?></td>
      </tr>
       <tr>
           <td class="smallText"></td>
	       <td class="smallText"><?php echo tep_draw_checkbox_field('question_required',1,($qInfo->question_required==1?true:false)) . '&nbsp;' . TEXT_QUESTION_REQUIRE;?></td>
     </tr>
     <tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','5'); ?></td></tr>
     </table>
	   <?php echo '</form>';?>
<?php }?>
<script language=javascript>
var ajax_image_load='<?php echo tep_image(DIR_WS_IMAGES.'24-1.gif'); ?>';
	function message_validateForm()	{		
			var icnt;
			var listbox,error_message='';
			var error_result=Array();
			var listbox_name=Array("select_survey[]","select_service[]");
			error_result[0]="<?php echo stripslashes(addslashes(ERR_SELECT_SURVEY));?>";
			error_result[1]="<?php echo stripslashes(addslashes(ERR_SELECT_SERVICE));?>";
			for(ecnt=0;ecnt<listbox_name.length;ecnt++){
				listbox=document.copy_survey.elements[listbox_name[ecnt]];
				if (listbox==null) break;
				if (listbox.length){
					for(icnt=0;icnt<listbox.length;icnt++){
						if (listbox[icnt].checked){
							 error_result[ecnt]="";
							 break;
						}
					}
				} else {
					if (listbox.checked) error_result[ecnt]="";
				}
			}
			if (error_result[0]!="")
				error_message=error_result[0];

			if (error_result[1]!="")
				error_message+='\n'+error_result[1];
				
		if(error_message!='')
			alert(error_message);
		else {
		do_post_command('copy_survey','<?php echo tep_href_link(FILENAME_SERVICES_SURVEYS,'copy_command=save_message'); ?>');
		document.getElementById('copy_message_load').innerHTML=ajax_image_load;
		}
	}
	document.getElementById('copy_message_load').style.display='none';
	var type="";
	function copy_message(suID){
	document.getElementById('copy_message_load').style.display='';
	var show=document.getElementById("show_edit_scr");
	if(show)
	show.style.display='none';
	
	var ii=document.getElementById("img_ids");
	if(ii)
	ii.style.display='none';
	if(document.getElementById('copy_message_load'))document.getElementById('copy_message_load').style.display='';
	if(document.getElementById(type + "_row"))
	     document.getElementById(type + "_row").style.display="";
	var tt=document.getElementById("panel_" + type + "_row");
	if(tt){
	tt.style.display="none";
	tt.innerHTML="";
	}
	document.getElementById('copy_message_load').innerHTML=ajax_image_load;
	do_get_command('<?php echo tep_href_link(FILENAME_SERVICES_SURVEYS,'copy_command=copy_message&suID='); ?>'+suID);
	}
	var type="";
	function close_copy_message(){
	if(document.getElementById(type + "_row"))
		document.getElementById(type + "_row").style.display="";
	var tt=document.getElementById("panel_" + type + "_row");
	if(tt){
	tt.style.display="none";
	tt.innerHTML="";
	}
	document.getElementById('copy_message_load').style.display='none';
	document.getElementById('copy_message_load').innerHTML='';
	}
</script>
<?php
function tep_service_survey_copy_message(){
	global $HTTP_GET_VARS;
	require(DIR_WS_CLASSES . '/currencies.php');
  	$currencies=new currencies;
  
  // action - course of action to move or display page contents,services_id - source service where the survey is moved
  // template -type indicates email templates or sms templates

	$action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
	$service_id = (isset($HTTP_GET_VARS['suID']) ?(int)$HTTP_GET_VARS['suID'] : 0);

	$templates=getTemplates($service_id,$template_type);
	// get the message list
	
	$type_params="";
	reset($templates);
	while(list($key,)=each($templates)){
		if ($type_params=="") $type_params.=" and survey_type in(";
		$type_params.="'" . $key . "',";
	}
	$type_params=substr($type_params,0,-1) . ")";
	$survey_query=tep_db_query("SELECT count(*) as total,survey_type from " . TABLE_SERVICES_SURVEYS . " where service_id='" . (int)$service_id . "' " . $type_params . " group by survey_type");
	while($survey=tep_db_fetch_array($survey_query)){
		if ((int)$survey["total"]>0 && isset($templates[$survey['survey_type']])){
			$templates[$survey['survey_type']]['status']=1;
			$templates[$survey['survey_type']]['color']='#0000FF';
		}
	}
	$service_array=array();
	$service_query=tep_db_query("SELECT se.service_id,sed.service_name from " . TABLE_SERVICES . " se, " . TABLE_SERVICES_DESCRIPTION . " sed where se.service_id=sed.service_id " . SERVICES_CONDITION_STATUS . " order by sed.service_name");
	while($service_result=tep_db_fetch_array($service_query)){
		if ($service_id!=$service_result["service_id"])
			$service_array[$service_result['service_id']]=array('text'=>$service_result['service_name'],'color'=>"#000000");
	}
	echo tep_draw_form("copy_survey",FILENAME_SERVICES_COPY_SURVEYS,'action=update&suID=' . $service_id,"post",'onSubmit="javascript:return validateForm();"');
	echo tep_draw_hidden_field('suID',$service_id);
	?>
	
	<table border="0" width="100%" cellspacing="0" cellpadding="2">
	<tr>
		<td class="openContent_top"><table width="100%" cellpadding="0" cellspacing="0" border="0">
		<tr>
			<td class="pageHeading"><?php echo tep_image(DIR_WS_IMAGES.'template/icon_active.gif'); ?>&nbsp;Copy Messages - <?php echo tep_get_service_name($service_id); ?></td>
			<td align="right"><?php echo '<a href="javascript: message_validateForm();">'.tep_image(DIR_WS_IMAGES.'template/img_savel.gif').'</a>&nbsp;&nbsp;<a href="javascript: close_copy_message();">'.tep_image(DIR_WS_IMAGES.'template/img_closel.gif').'</a>'; ?>&nbsp;</td>
		</tr>
		</table></td>
	</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif','1','5');?></td>
		</tr>
			<tr>
				<td>
				<table border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td>
							<table border="0" cellpadding="2" cellspacing="5">
								<tr class="attributes-even">
									<td class="tableHeading"><?php echo SELECT_MESSAGES;?></td>
									<td class="tableHeading"><?php echo SELECT_SERVICES;?></td>
								</tr>
								<tr>
									<td class="tableHeading"><?php echo customListBox("select_survey",$templates,0,300,200);?></td>
									<td class="tableHeading"><?php echo customListBox("select_service",$service_array,0,500,200);?></td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td><?php echo tep_draw_separator('pixel_trans.gif','1','5');?></td>
					</tr>
				</table>
				</td>
			</tr>
		</table>	
	</form>
  <?php }?>
 <?php  // function used in this page

	function update(){
		global $HTTP_POST_VARS;

		$survey_keys=&$HTTP_POST_VARS["select_survey"];
		$service_keys=&$HTTP_POST_VARS["select_service"];
	
		$service_id = (isset($HTTP_POST_VARS['suID']) ?(int)$HTTP_POST_VARS['suID'] : 0);
		//$template_type=(isset($HTTP_POST_VARS['type'])?strtolower($HTTP_POST_VARS['type']) : "su");
		$templates=getTemplates($subscription_id,$template_type);
	
		if (sizeof($survey_keys)<=0 || sizeof($service_keys)<=0 || $service_id<0)
			tep_redirect(tep_href_link(FILENAME_SERVICES_SURVEYS,'service_id=' . $service_id));

		// fetch services into total array to find and service has already survey
		$status=array();
		reset($service_keys);
		for ($icnt=0;$icnt<sizeof($service_keys);$icnt++){
			$status[$service_keys[$icnt]]=array();
		}
		//get the input survey_ids,service_ids
		$survey_string=join("','",$survey_keys);
		$service_string=join(",",$service_keys);

		$survey_query=tep_db_query("SELECT  * from " . TABLE_SERVICES_SURVEYS . " where service_id='" . (int)$service_id . "' and survey_type in('" . $survey_string . "')");
		$survey_details=array();
		while($survey_result=tep_db_fetch_array($survey_query)){
			$survey_id=$survey_result["survey_id"];
			unset($survey_result["survey_id"]);
			$survey_details[$survey_id]=array('details'=>$survey_result,'questions'=>array());
			$question_query=tep_db_query("SELECT * from " . TABLE_SERVICES_SURVEYS_QUESTION . " where survey_id='" . (int)$survey_id . "'");
			while($question_result=tep_db_fetch_array($question_query)){
				$question_id=$question_result["question_id"];
				unset($question_result["question_id"]);
				$survey_details[$survey_id]['questions'][$question_id]=array('details'=>$question_result,'choices'=>array());
				if ($question_result["question_type"]<=3){
					$choice_query=tep_db_query("SELECT * from " . TABLE_SERVICES_SURVEYS_CHOICE . " where question_id='" . (int)$question_id . "'");
					while($choice_result=tep_db_fetch_array($choice_query)){
						$choice_id=$choice_result["choice_id"];
						unset($choice_result["choice_id"]);
						$survey_details[$survey_id]['questions'][$question_id]["choices"][$choice_id]=$choice_result;
					}
				}
			}
		}
		// get the survey status for services we have to copy
		$service_status_query=tep_db_query("SELECT count(*) as total,survey_type,service_id,survey_id from " . TABLE_SERVICES_SURVEYS . " where service_id in(" . $service_string . ") and survey_type in('" . $survey_string . "') group by service_id,survey_type");
		
		while($service_status=tep_db_fetch_array($service_status_query)){
			if (isset($status[$service_status["service_id"]])){
				$status[$service_status["service_id"]][$service_status["survey_type"]]['count']=$service_status["total"];
				$status[$service_status["service_id"]][$service_status["survey_type"]]['survey_id']=$service_status["survey_id"];
			}
		}
		$copy_details=array();
		reset($status);
		//iterate with the services we have to find insert or update
		if (sizeof($status)>0 && sizeof($survey_details)>0){
			while(list($ekey,)=each($status)){
				reset($survey_details);
				while(list($survey_id,)=each($survey_details)){
					$survey=$survey_details[$survey_id]['details'];
					$count=isset($status[$ekey][$survey["survey_type"]]["count"])?$status[$ekey][$survey["survey_type"]]["count"]:0;
					if ($count>0){ // update operation
						tep_delete_survey($status[$ekey][$survey["survey_type"]]["survey_id"]);
						$status[$ekey][$survey["survey_type"]]["count"]=0;
					}
					$survey["service_id"]=$ekey;
					tep_db_perform(TABLE_SERVICES_SURVEYS,$survey);
					$new_survey_id=tep_db_insert_id();
					if (sizeof($survey_details[$survey_id]['questions'])>0){
						reset($survey_details[$survey_id]['questions']);
						while(list($question_id,)=each($survey_details[$survey_id]['questions'])){
							$question=$survey_details[$survey_id]['questions'][$question_id]['details'];
							$question["survey_id"]=$new_survey_id;
							tep_db_perform(TABLE_SERVICES_SURVEYS_QUESTION,$question);
							$new_question_id=tep_db_insert_id();
							if ($question["question_type"]<=3 && sizeof($survey_details[$survey_id]['questions'][$question_id]['choices'])>0){
								reset($survey_details[$survey_id]['questions'][$question_id]["choices"]);
								while(list($choice_id,)=each($survey_details[$survey_id]['questions'][$question_id]['choices'])){
									$choice=$survey_details[$survey_id]['questions'][$question_id]['choices'][$choice_id];
									$choice["question_id"]=$new_question_id;
									tep_db_perform(TABLE_SERVICES_SURVEYS_CHOICE,$choice);
								}
							}
						}
					}
				}
			}
		}
	
	//	tep_redirect(tep_href_link(FILENAME_SERVICES_SURVEYS,'service_id=' . $service_id));
		}
	
	function &getTemplates($events_id,$type){
		$templates=array(	'RV'=>array('text'=>TEXT_RV,'status'=>0,'color'=>'#FF0000'),
							'WV'=>array('text'=>TEXT_WV,'status'=>0,'color'=>'#FF0000'),
							'FV'=>array('text'=>TEXT_FV,'status'=>0,'color'=>'#FF0000'),
							);
		return $templates;
	}
	function customListBox($name,&$list,$selected="",$width=300,$height=100){
	$result="";
		$result.='<div id="' . $name . '_box" style="background-color: #FFFFFF;border-color: #7b9ebd; border-style: solid; border-width: 1px;overflow:auto;width:' . $width . ';height:' . $height . '">' . "\n";
		$result.='<table border="0" cellspacing="0" cellpadding="2" width="100%" valign="middle" align="center" class="BoxTable">' . "\n";
		reset($list);

		$selected=array();
		if (isset($HTTP_POST_VARS[$name]) && is_array($HTTP_POST_VARS[$name])){
			$selected=&$HTTP_POST_VARS[$name];
		}
		while(list($key,)=each($list)){
			$result.='<tr class="attributes-even"><td class="main" width="20">' . "\n";
			$result.='<input type="checkbox" name="' . $name . '[]" value="' . $key . '" ' . (in_array($key,$selected)?"checked":"") . ' >';
			$result.='<td class="main"><font color="' . $list[$key]['color'] . '">' . $list[$key]['text'] .'</font></td></tr>' ."\n";
		}
		$result.='</table></div>' . "\n";
		echo $result;
	}
?>