<?php 
$form_name = "Survey Master";
$table_name = "custom_survey_master";
$primary_key = "sur_id";

$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : "";
	
include_once("includes/config.php");
include_once("includes/common_functions.php");

$common = new General_Functions();
$css_class = "x-gray";
// $common->SetCssClass($css_class);

$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : "";
$act_type = isset($_REQUEST['act_type']) ? $_REQUEST['act_type'] : "";
$cancel_link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
$action = $_SERVER['SCRIPT_NAME']."?page=".$_REQUEST['page']."&mode=submit";

switch($mode)
{
    case "submit":
        $_REQUEST['dor'] = $common->date_format($_REQUEST['dor'], DT_FMT);
        $_REQUEST['dob'] = $common->date_format($_REQUEST['dob'], DT_FMT);
        
        $qry = "SELECT * FROM ".$table_name." WHERE ".$primary_key." = '".$_REQUEST['id']."'";
        $common->sql->query($qry, SQL_ALL, SQL_ASSOC);
        $rec = $common->sql->record;
        $status = isset($_REQUEST['status']) ? $_REQUEST['status'] : 'N';
        
        if($_REQUEST['id'] == "")
        {
            $_query = sprintf("insert into ".$table_name." values(0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), '%s', 0, 0)",
                    mysql_escape_string($_REQUEST['survy_cde']),
                    mysql_escape_string($_REQUEST['survy_tle']),
                    mysql_escape_string($_REQUEST['survy_url']),
                    mysql_escape_string($_REQUEST['survy_ref']),
                    mysql_escape_string($_REQUEST['survy_prm_db']),
                    mysql_escape_string($_REQUEST['survy_prm_db_srver']),
                    mysql_escape_string($_REQUEST['survy_prm_db_usr']),
                    mysql_escape_string($_REQUEST['survy_prm_db_pwd']),
                    mysql_escape_string($_REQUEST['survy_sec_db']),
                    mysql_escape_string($_REQUEST['survy_sec_db_srver']),
                    mysql_escape_string($_REQUEST['survy_sec_db_usr']),
                    mysql_escape_string($_REQUEST['survy_sec_db_pwd']),
                    mysql_escape_string($status),
                    mysql_escape_string(USR)
            );
            $rec = $common->sql->query($_query);
            $_SESSION['msg'] = 'Survey has been saved Successfully';
            $last_id = mysql_insert_id();
			$common->UserLog($form_name, $table_name, $last_id, 'Insert');
        }
        else
        {
            $tab = $_REQUEST['tab_id'];
            
            if($tab == 1)
            {
                $query = "UPDATE ".$table_name."    SET     sur_code            = '".mysql_escape_string($_REQUEST['survy_cde'])."', 
                                                            sur_title           = '".mysql_escape_string($_REQUEST['survy_tle'])."', 
                                                            sur_url           	= '".mysql_escape_string($_REQUEST['survy_url'])."', 
                                                            sur_onlne_id        = '".mysql_escape_string($_REQUEST['survy_ref'])."', 
                                                            status              = '".mysql_escape_string($status)."', 
                                                            update_time         = now(), 
                                                            update_user         = '".USR."' 
                                                    WHERE   ".$primary_key."    = '".$_REQUEST['id']."'";
            }
            
            if($tab == 2)
            {
                $query = "UPDATE ".$table_name."    SET     sur_pri_db_name     = '".mysql_escape_string($_REQUEST['survy_prm_db'])."', 
                                                            sur_pri_db_server   = '".mysql_escape_string($_REQUEST['survy_prm_db_srver'])."', 
                                                            sur_pri_db_usrnme   = '".mysql_escape_string($_REQUEST['survy_prm_db_usr'])."', 
                                                            sur_pri_db_paswrd   = '".mysql_escape_string($_REQUEST['survy_prm_db_pwd'])."', 
                                                            update_time         = now(), 
                                                            update_user         = '".USR."' 
                                                    WHERE   ".$primary_key."    = '".$_REQUEST['id']."'";
            }
            
            if($tab == 3)
            {
                $query = "UPDATE ".$table_name."    SET     sur_sec_db_name     = '".mysql_escape_string($_REQUEST['survy_sec_db'])."', 
                                                            sur_sec_db_server   = '".mysql_escape_string($_REQUEST['survy_sec_db_srver'])."', 
                                                            sur_sec_db_usrnme   = '".mysql_escape_string($_REQUEST['survy_sec_db_usr'])."', 
                                                            sur_sec_db_paswrd   = '".mysql_escape_string($_REQUEST['survy_sec_db_pwd'])."', 
                                                            update_time         = now(), 
                                                            update_user         = '".USR."' 
                                                    WHERE   ".$primary_key."    = '".$_REQUEST['id']."'";
            }
            $rec = $common->sql->exeQuery($query);
            $_SESSION['msg'] = 'Survey has been updated Successfully';
			$common->UserLog($form_name, $table_name, $_REQUEST['id'], 'Update');
        }
        
        if($_REQUEST['id'] != '')
        {
            if($_REQUEST['survy_sec_db'] != "")
                $link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
            else
            {
                $last_id = $_REQUEST['id'];
                if($_REQUEST['tab_id'] == 1)
                    $link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'].'&id='.$last_id.'#tabs-2';
                elseif($_REQUEST['tab_id'] == 2)
                    $link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'].'&id='.$last_id.'#tabs-3';
            }
        }
        else
        {
            if($_REQUEST['tab_id'] == 1)
                $link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'].'&id='.$last_id.'#tabs-2';
            elseif($_REQUEST['tab_id'] == 2)
                $link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'].'&id='.$last_id.'#tabs-3';
            else
                $link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
        }
        
        /*if($_REQUEST['tab_id'] == 1)
            $link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'].'&id='.$last_id.'#tabs-2';
        elseif($_REQUEST['tab_id'] == 2)
            $link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'].'&id='.$last_id.'#tabs-3';
        else
            $link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];*/
        
        header("location:".$link);
    break;

    case "delete":
            if(isset($_REQUEST[$prefix.'id']))
                    $id = $_REQUEST[$prefix.'id'];
            else
                    $id = implode("', '", $_REQUEST[$primary_key]);

            $query = "DELETE FROM ".$table_name." WHERE ".$primary_key." IN ('".$id."')";

            $rec = $common->sql->exeQuery($query);

            if($rec)
            {
                    $_SESSION['msg'] = 'Selected record has been deleted successfully';
                    if(isset($_REQUEST['id']))
						$common->UserLog($form_name, $table_name, $_REQUEST['id'], 'Delete');
                    else
						for($i=0; $i<count($_REQUEST[$primary_key]); $i++)
							$common->UserLog($form_name, $table_name, $_REQUEST[$primary_key][$i], 'Delete');
            }

            $query_string = str_replace("&mode=delete","",$_SERVER['QUERY_STRING']);
            $link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
            header("location:".$link);
    break;
    
    default:
        $qry = "SELECT *, CASE WHEN status = 'Y' THEN 'checked' ELSE '' END AS status FROM ".$table_name." WHERE ".$primary_key." = '".$_REQUEST['id']."'";
        $common->sql->query($qry, SQL_ALL, SQL_ASSOC);
        $rec = $common->sql->record;
        
        $sur_code = $rec[0]['sur_code'];
        $survy_tle = $rec[0]['sur_title'];
        $survy_url = $rec[0]['sur_url'];
        
		if($survy_url == "")
			$survy_url = "http://";
        
		$survy_ref = $rec[0]['sur_onlne_id'];
        $status = $rec[0]['status'];
        
        $prm_db = $rec[0]['sur_pri_db_name'];
        $prm_db_srver = $rec[0]['sur_pri_db_server'];
        $prm_db_usr = $rec[0]['sur_pri_db_usrnme'];
        $prm_db_pwd = $rec[0]['sur_pri_db_paswrd'];
        
        $sec_db = $rec[0]['sur_sec_db_name'];
        $sec_db_srver = $rec[0]['sur_sec_db_server'];
        $sec_db_usr = $rec[0]['sur_sec_db_usrnme'];
        $sec_db_pwd = $rec[0]['sur_sec_db_paswrd'];
        
        echo '<div class="content_bg">';
            include("add.php");
        echo '</div>';
    break;
}
?>