<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")
            catgry = 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(catgry.app_name.value == "")
            {
                alert("Please Provide Application Name");
                catgry.app_name.focus();
                return false;
            }
            if(catgry.dt_fmt.value == "")
            {
                alert("Please Select Date Format");
                catgry.dt_fmt.focus();
                return false;
            }
            if(catgry.tz.value == "")
            {
                alert("Please Select Timezone");
                catgry.tz.focus();
                return false;
            }
        }
        if(tab2)
        {
            if(catgry.org_name.value == "")
            {
                alert("Please Provide Organisation Name");
                catgry.org_name.focus();
                return false;
            }
            if(catgry.org_addrs.value == "")
            {
                alert("Please Provide Address");
                catgry.org_addrs.focus();
                return false;
            }
            if(catgry.org_phone.value == "")
            {
                alert("Please Provide Phone Number");
                catgry.org_phone.focus();
                return false;
            }
            if(catgry.org_mail.value == "")
            {
                alert("Please Provide E-Mail");
                catgry.org_mail.focus();
                return false;
            }
            else
            {
                if(!checkEmail(catgry.org_mail.value))
                {
                    alert("Please Provide Valid E-Mail");
                    catgry.org_mail.focus();
                    return false;
                }
            }
            if(catgry.org_website.value == "")
            {
                alert("Please Provide Website");
                catgry.org_website.focus();
                return false;
            }
            else
            {
                if(!isValidURL(catgry.org_website.value))
                {
                    alert("Please Provide Valid URL");
                    catgry.org_website.focus();
                    return false;
                }
            }
        }
        if(tab3)
        {
            if(catgry.frm_name.value == "")
            {
                alert("Please Provide Admin Name");
                catgry.frm_name.focus();
                return false;
            }
            if(catgry.frm_mail.value == "")
            {
                alert("Please Provide Admin Email");
                catgry.frm_mail.focus();
                return false;
            }
            else
            {
                if(!checkEmail(catgry.frm_mail.value))
                {
                    alert("Please Provide Valid E-Mail");
                    catgry.frm_mail.focus();
                    return false;
                }
            }
            if(catgry.smtp_server.value == "")
            {
                alert("Please Provide SMTP Server");
                catgry.smtp_server.focus();
                return false;
            }
            if(catgry.smtp_port.value == "")
            {
                alert("Please Provide SMTP Port");
                catgry.smtp_port.focus();
                return false;
            }
            if(catgry.usr_name.value == "")
            {
                alert("Please Provide Username");
                catgry.usr_name.focus();
                return false;
            }
            if(catgry.usr_pwd.value == "")
            {
                alert("Please Provide Password");
                catgry.usr_pwd.focus();
                return false;
            }
        }
    }
</script>
<?php
	$qry = "SELECT * FROM ".$table_name;
	$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
	$rec = $common->sql->record[0];
	$id = $rec[$primary_key];
	$tz = $rec['stngs_timezone'];
	$dt_fmt = $rec['stngs_dateformat'];
	$survey_id = $rec['stngs_survey_code'];
	$survey_id_chk_val = $rec['stngs_survey_fixed'];
	
	if($survey_id_chk_val == 'Y')
		$survey_id_chk = "checked";
	
	$app_name = $rec['stngs_app_name'];
	$org_name = $rec['stngs_org_name'];
	$org_addrs = $rec['stngs_org_addrs'];
	$org_phone = $rec['stngs_org_phone'];
	$org_mail = $rec['stngs_org_mail'];
	$org_website = $rec['stngs_org_website'];
	
	$frm_name = $rec['smtp_admin_name'];
	$frm_mail = $rec['smtp_frm_mail'];
	$smtp_server = $rec['smtp_server_name'];
	$smtp_port = $rec['smtp_server_port'];
	$usr_name = $rec['smtp_server_usrname'];
	$usr_pwd = $rec['smtp_server_pwd'];
	$usr_pwd = base64_decode($rec['smtp_server_pwd']);
	$smtp_ssl = $rec['smtp_server_ssl'];
	$smtp_auth = $rec['smtp_server_auth'];
	
	if($smtp_ssl == 1)
		$smtp_sll = "checked";
	if($smtp_auth == 1)
		$smtp_auth = "checked";
?>
<style>
	#dataTable {
		line-height: 15px !important
	}
</style>
<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;General Settings&nbsp;</a></li>
                <li><a href="#tabs-2">&nbsp;Organisation Settings&nbsp;</a></li>
                <li><a href="#tabs-3">&nbsp;SMTP Settings&nbsp;</a></li>
            </ul>
            
            <form class="form" name="survey" id="survey" method="post" action="<?php echo $action; ?>" enctype="multipart/form-data" onsubmit="return frm_validation();">
                <div id="tabs-1">
                    <div class="questionrow">
                        <div class="question">Organisation Name</div>
                        <div class="answer">
                            <input type="text" name="app_name" id="app_name" value="<?php echo $app_name; ?>" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">Date Format</div>
                        <div class="answer">
                        	<input type="hidden" name="id" id="id" value="<?php echo $id; ?>" />
                        	<input type="hidden" name="tab_id" id="tab_id" value="" />
                            <select name="dt_fmt" id="dt_fmt">
                                <option value="">-- Select --</option>
                                <option value="dd-mm-yyyy">dd-mm-yyyy</option>
                                <option value="mm-dd-yyyy">mm-dd-yyyy</option>
                                <option value="yyyy-mm-dd">yyyy-mm-dd</option>
                            </select>
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">TimeZone</div>
                        <div class="answer">
                            <select name="tz" id="tz">
                                <option value="">-- Select --</option>
                                <?php
                                    foreach($zonelist as $key => $value)
                                        echo '<option value="' . $key . '">' . $value . '</option>' . "\n";
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
						<br/>
                    	<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; line-height: 40px;" id="dataTable" align="center">
                        	<tr>
                            	<td class="question_title" width="50%">&nbsp;</td>
                                <td class="question_title">&nbsp;</td>
                                <td class="question_title" align="center">Fixed</td>
                            </tr>
                            <tr>
                            	<td class="question_title">Survey</td>
                                <td class="question">
									<?php 
										echo '&nbsp;';
										$common->dropdown('sur_code', 'sur_title', 'custom_survey_master', '', 'sur_title', 'survey_id', $survey_id, 'class="required"');
									?>
								</td>
                                <td class="question" align="center">
									<input type="checkbox" name="survey_id_chk" id="survey_id_chk" value="Y" <?php echo $survey_id_chk; ?> />
								</td>
                            </tr>
                        </table>
                        <br/>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div style="padding: 5px;" align="center">
                            <input type="submit" name="Submit" value="Submit" />
                        </div>
                    </div>
				</div>
                <div id="tabs-2">
                    <div class="questionrow">
                        <div class="question">Organisation Logo</div>
                        <div class="answer">
                            <input type="file" name="org_logo" id="org_logo" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">Organisation Name</div>
                        <div class="answer">
                            <input type="text" name="org_name" id="org_name" value="<?php echo $org_name; ?>" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">Address</div>
                        <div class="answer">
                            <input type="text" name="org_addrs" id="org_addrs" value="<?php echo $org_addrs; ?>" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">Phone Number</div>
                        <div class="answer">
                            <input type="text" name="org_phone" id="org_phone" value="<?php echo $org_phone; ?>" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">E-Mail</div>
                        <div class="answer">
                            <input type="text" name="org_mail" id="org_mail" value="<?php echo $org_mail; ?>" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">Website</div>
                        <div class="answer">
                            <input type="text" name="org_website" id="org_website" value="<?php echo $org_website; ?>" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div style="padding: 5px;" align="center">
                            <input type="submit" name="Submit" value="Submit" />
                        </div>
                    </div>
				</div>
                <div id="tabs-3">
                    <div class="questionrow">
                        <div class="question">Admin Name</div>
                        <div class="answer">
                            <input type="text" name="frm_name" id="frm_name" value="<?php echo $frm_name; ?>" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">Admin Email</div>
                        <div class="answer">
                            <input type="text" name="frm_mail" id="frm_mail" value="<?php echo $frm_mail; ?>" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">SMTP Host</div>
                        <div class="answer">
                            <input type="text" name="smtp_server" id="smtp_server" value="<?php echo $smtp_server; ?>" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">SMTP Port</div>
                        <div class="answer">
                            <input type="text" name="smtp_port" id="smtp_port" value="<?php echo $smtp_port; ?>" size="7" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">SMTP Username</div>
                        <div class="answer">
                            <input type="text" name="usr_name" id="usr_name" value="<?php echo $usr_name; ?>" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">SMTP Password</div>
                        <div class="answer">
                            <input type="password" name="usr_pwd" id="usr_pwd" value="<?php echo $usr_pwd; ?>" />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">SMTP SSL</div>
                        <div class="answer">
                            <input type="checkbox" name="smtp_ssl" id="smtp_ssl" value="1" <?php echo $smtp_sll; ?> />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">SMTP Auth</div>
                        <div class="answer">
                            <input type="checkbox" name="smtp_auth" id="smtp_auth" value="1" <?php echo $smtp_auth; ?> />
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div style="padding: 5px;" align="center">
                            <input type="submit" name="Submit" value="Submit" />
                        </div>
                    </div>
                </div> 
            </form>
		</div>
	</div>
</div>
<?php
	echo '<script language="javascript">';
	if($tz != "")
		echo 'select_checked("tz", "'.$tz.'", "");';
	if($dt_fmt != "")
		echo 'select_checked("dt_fmt", "'.$dt_fmt.'", "");';
	echo '</script>';
?>