<script language="javascript">
    var asInitVals = new Array();

    $(document).ready(function() {
        $("#tabs").tabs( {
            "show": function(event, ui) {
                var oTable = $('div.dataTables_scrollBody>table.display', ui.panel).dataTable();
                var tab1 = $("#tabs-1").is(":visible");
                var tab2 = $("#tabs-2").is(":visible");
                var tab3 = $("#tabs-3").is(":visible");

                if(tab1)
                        $("#tab_id").val("1");
                if(tab2)
                        $("#tab_id").val("2");
                if(tab3)
                        $("#tab_id").val("3");

                if ( oTable.length > 0 ) {
                        oTable.fnAdjustColumnSizing();
                }
            }
        } );
    } );
	
    function frm_validation()
    {
        if(navigator.appName != "Microsoft Internet Explorer")
            frmname = document.survey;

        var tab1 = $("#tabs-1").is(":visible");
        var tab2 = $("#tabs-2").is(":visible");
        var tab3 = $("#tabs-3").is(":visible");

        if(tab1)
            $("#tab_id").val("1");
        if(tab2)
            $("#tab_id").val("2");
        if(tab3)
            $("#tab_id").val("3");

        if(tab1)
        {
            if(frmname.survy_cde.value == "")
            {
                alert("Please Provide Survey Code");
                frmname.survy_cde.focus();
                return false;
            }
            if(frmname.survy_tle.value == "")
            {
                alert("Please Provide Survey Title");
                frmname.survy_tle.focus();
                return false;
            }
            if(frmname.survy_url.value == "http://")
            {
                /*alert("Please Provide Survey URL");
                frmname.survy_url.focus();
                return false;*/
            }
			else
			{
				if(!isValidURL(frmname.survy_url.value))
				{
					alert("Please Provide Valid Survey URL");
					frmname.survy_url.focus();
					return false;
				}
			}
            if(frmname.survy_ref.value == "")
            {
                alert("Please Provide Online Survey ID");
                frmname.survy_ref.focus();
                return false;
            }
        }
        if(tab2)
        {
            if(frmname.survy_prm_db.value == "")
            {
                alert("Please Provide Local DB Name");
                frmname.survy_prm_db.focus();
                return false;
            }
            if(frmname.survy_prm_db_srver.value == "")
            {
                alert("Please Provide Local DB Server");
                frmname.survy_prm_db_srver.focus();
                return false;
            }
            if(frmname.survy_prm_db_usr.value == "")
            {
                alert("Please Provide Local DB Username");
                frmname.survy_prm_db_usr.focus();
                return false;
            }
            if(frmname.survy_prm_db_pwd.value == "")
            {
                alert("Please Provide Local DB Password");
                frmname.survy_prm_db_pwd.focus();
                return false;
            }
        }
        if(tab3)
        {
            if(frmname.survy_sec_db.value == "")
            {
                alert("Please Provide Central DB Name");
                frmname.survy_sec_db.focus();
                return false;
            }
            if(frmname.survy_sec_db_srver.value == "")
            {
                alert("Please Provide Central DB Server");
                frmname.survy_sec_db_srver.focus();
                return false;
            }
            if(frmname.survy_sec_db_usr.value == "")
            {
                alert("Please Provide Central DB Username");
                frmname.survy_sec_db_usr.focus();
                return false;
            }
            if(frmname.survy_sec_db_pwd.value == "")
            {
                alert("Please Provide Central DB Password");
                frmname.survy_sec_db_pwd.focus();
                return false;
            }
        }
    }
	
    function test_prm_db()
    {
        var url = "";
        var loc = window.location;
        var pathName = loc.pathname.substring(0, loc.pathname.lastIndexOf('/') + 1);
        url = loc.href.substring(0, loc.href.length - ((loc.pathname + loc.search + loc.hash).length - pathName.length));

        server = $("#survy_prm_db_srver").val();
        username = $("#survy_prm_db_usr").val();
        password = $("#survy_prm_db_pwd").val();
        database = $("#survy_prm_db").val();

        if(server != "" && database != "" && username != "" && password != "")
        {
            $.ajax({
                type: "POST",
                data: "server="+server+"&username="+username+"&password="+password+"&database="+database+"&type=Local",
                url: url+"testdb.php",						
                error: function(msg){
                    alert(msg.status );
                },
                success: function(msg){
                    alert(msg);
                }
            });
        }
        else
        {
            if(navigator.appName != "Microsoft Internet Explorer")
                frmname = document.survey;

            if(frmname.survy_prm_db.value == "")
            {
                alert("Please Provide Local DB Name");
                frmname.survy_prm_db.focus();
                return false;
            }
            if(frmname.survy_prm_db_srver.value == "")
            {
                alert("Please Provide Local DB Server");
                frmname.survy_prm_db_srver.focus();
                return false;
            }
            if(frmname.survy_prm_db_usr.value == "")
            {
                alert("Please Provide Local DB Username");
                frmname.survy_prm_db_usr.focus();
                return false;
            }
            if(frmname.survy_prm_db_pwd.value == "")
            {
                alert("Please Provide Local DB Password");
                frmname.survy_prm_db_pwd.focus();
                return false;
            }
        }
    }
	
    function test_sec_db()
    {
        var url = "";
        var loc = window.location;
        var pathName = loc.pathname.substring(0, loc.pathname.lastIndexOf('/') + 1);
        url = loc.href.substring(0, loc.href.length - ((loc.pathname + loc.search + loc.hash).length - pathName.length));

        server = $("#survy_sec_db_srver").val();
        username = $("#survy_sec_db_usr").val();
        password = $("#survy_sec_db_pwd").val();
        database = $("#survy_sec_db").val();

        if(server != "" && database != "" && username != "" && password != "")
        {
            $.ajax({
                type: "POST",
                data: "server="+server+"&username="+username+"&password="+password+"&database="+database+"&type=Central",
                url: url+"testdb.php",						
                error: function(msg){
                    alert(msg.status );
                },
                success: function(msg){
                    alert(msg);
                }
            });
        }
        else
        {
            if(navigator.appName != "Microsoft Internet Explorer")
                frmname = document.survey;

            if(frmname.survy_sec_db.value == "")
            {
                alert("Please Provide Central DB Name");
                frmname.survy_sec_db.focus();
                return false;
            }
            if(frmname.survy_sec_db_srver.value == "")
            {
                alert("Please Provide Central DB Server");
                frmname.survy_sec_db_srver.focus();
                return false;
            }
            if(frmname.survy_sec_db_usr.value == "")
            {
                alert("Please Provide Central DB Username");
                frmname.survy_sec_db_usr.focus();
                return false;
            }
            if(frmname.survy_sec_db_pwd.value == "")
            {
                alert("Please Provide Central DB Password");
                frmname.survy_sec_db_pwd.focus();
                return false;
            }
        }
    }

    function frm_edit_val(frmval)
    {
        $("#id").val(frmval["sur_id"]);
        $("#survy_cde").val(frmval["sur_code"]);
        $("#survy_cde").attr("readonly", true);
        $("#survy_tle").val(frmval["sur_title"]);
        $("#survy_url").val(frmval["sur_url"]);
        $("#survy_ref").val(frmval["sur_onlne_id"]);
        $("#survy_prm_db").val(frmval["sur_pri_db_name"]);
        $("#survy_prm_db_srver").val(frmval["sur_pri_db_server"]);
        $("#survy_prm_db_usr").val(frmval["sur_pri_db_usrnme"]);
        $("#survy_prm_db_pwd").val(frmval["sur_pri_db_paswrd"]);
        $("#survy_sec_db").val(frmval["sur_sec_db_name"]);
        $("#survy_sec_db_srver").val(frmval["sur_sec_db_server"]);
        $("#survy_sec_db_usr").val(frmval["sur_sec_db_usrnme"]);
        $("#survy_sec_db_pwd").val(frmval["sur_sec_db_paswrd"]);
        $("#biometric_ref").val(frmval["biometric_ref"]);
        
        if(frmval["status"] == 'Y')
            $("#status").attr("checked", true);
        else
            $("#status").attr("checked", false);
        
        close_frame();
        $("#delete_img").show();
    }
</script>
<div style="width: 95%; margin: 0 auto; padding: 10px;">
	<div id="frm_display">
        <div id="tabs">
            <ul>
                <li class="active"><a href="#tabs-1">&nbsp;Survey Master&nbsp;</a></li>
                <li><a href="#tabs-2">&nbsp;Local&nbsp;</a></li>
                <li><a href="#tabs-3">&nbsp;Central&nbsp;</a></li>
            </ul>
            
            <form class="form" name="survey" id="survey" method="post" action="<?php echo $action; ?>" onsubmit="return frm_validation();">
                <div id="tabs-1">
                    <div class="questionrow">
                        <div class="question">Survey Code</div>
                        <div class="answer">
                            <input type="hidden" name="tab_id" id="tab_id" value="" />
                            <input type="hidden" name="id" id="id" value="<?php echo $_REQUEST['id']; ?>" />
                            <input type="text" name="survy_cde" id="survy_cde" value="<?php echo $sur_code; ?>" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">Survey Title</div>
                        <div class="answer">
                            <input type="text" name="survy_tle" id="survy_tle" value="<?php echo $survy_tle; ?>" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">Survey URL</div>
                        <div class="answer">
                            <input type="text" name="survy_url" id="survy_url" value="<?php echo $survy_url; ?>" onKeydown="return keyRestricted(this, event, 7, false);" onKeyup="return keyRestricted(this, event, 7, false);" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">Online Survey ID</div>
                        <div class="answer">
                            <input type="text" name="survy_ref" id="survy_ref" value="<?php echo $survy_ref; ?>" />
                            <br/>
                            <span><b>Example:</b> 12345,54321,..</span>
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">Is Active</div>
                        <div class="answer">
                            <input type="checkbox" name="status" id="status" value="Y" <?php echo $status; ?> />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div style="padding: 5px;" align="center">
                            <input type="submit" name="Submit" value="Next" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                </div>
                <div id="tabs-2">
                    <div class="questionrow">
                        <div class="question">Local DB Name</div>
                        <div class="answer">
                            <input type="text" name="survy_prm_db" id="survy_prm_db" value="<?php echo $prm_db; ?>" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">Local DB Server</div>
                        <div class="answer">
                            <input type="text" name="survy_prm_db_srver" id="survy_prm_db_srver" value="<?php echo $prm_db_srver; ?>" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">Local DB Username</div>
                        <div class="answer">
                            <input type="text" name="survy_prm_db_usr" id="survy_prm_db_usr" value="<?php echo $prm_db_usr; ?>" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">Local DB Password</div>
                        <div class="answer">
                            <input type="password" name="survy_prm_db_pwd" id="survy_prm_db_pwd" value="<?php echo $prm_db_pwd; ?>" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question" align="center">
                            <input type="button" name="Primary" value="Test Local" onclick="return test_prm_db();" />
                        </div>
                        <div class="answer" align="center">
                            <input type="submit" name="Submit" value="Next" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                </div>
                <div id="tabs-3">
                    <div class="questionrow">
                        <div class="question">Central DB Name</div>
                        <div class="answer">
                            <input type="text" name="survy_sec_db" id="survy_sec_db" value="<?php echo $sec_db; ?>" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">Central DB Server</div>
                        <div class="answer">
                            <input type="text" name="survy_sec_db_srver" id="survy_sec_db_srver" value="<?php echo $sec_db_srver; ?>" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">Central DB Username</div>
                        <div class="answer">
                            <input type="text" name="survy_sec_db_usr" id="survy_sec_db_usr" value="<?php echo $sec_db_usr; ?>" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">Central DB Password</div>
                        <div class="answer">
                            <input type="password" name="survy_sec_db_pwd" id="survy_sec_db_pwd" value="<?php echo $sec_db_pwd; ?>" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question" align="center">
                            <input type="button" name="Secondary" value="Test Central" onclick="return test_sec_db();" />
                        </div>
                        <div class="answer" align="center">
                            <input type="submit" name="Submit" value="Submit" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
		</div>
            </form>
		</div>
	</div>
</div>