<?php
if($_POST)
{
    // print_r($_POST);
    // die();
	$_SESSION['form']=$_POST;	$flgEr=FALSE;
	foreach($_POST as $field=>$value)
	{
	   // echo "<h4>".$field." = $value</h4><br>";
	   // echo $field;
// 		$flgUp=$db->updateAry("update settings set `value`='".mysqli_real_escape_string($value)."' where `field`='".$field."'");
// 		$query = mysql_query("update settings set value='$value' where field='$field' ");
            
            $updary = array(
                "value" => "$value"
                );

            $db->updateAry("settings", $updary, "where field='$field' ");
	}
// 	die();
	$_SERVER["msg"]="Updated Successfully";
	unset($_SESSION['form']);
	redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Promote", "page_id"=>"setting")));	
}

if(isset($_SESSION['form'])){
	$aryForm=$_SESSION['form'];
	unset($_SESSION['form']);
}
else{
	$aryFormTemp=$db->getRows("select * from settings where field not in ('school_id','url_root','is_install','systemname','expiredate')");
	if(!is_null($aryFormTemp) && is_array($aryFormTemp) && count($aryFormTemp)>0)
	{
		foreach($aryFormTemp as $iFormTemp)
		{
			$aryForm[$iFormTemp['field']]=$iFormTemp['value'];
		}
	}
}
echo $db->getErMsg();
if(isset($_SESSION['msg']) && $_SESSION['msg']!="")
{
	?>
	<div class="alert alert-success">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-thumbs-up-alt"></i> <b>Notice</b><?php echo $_SESSION['msg'] ?></i>
	</div>
	<?php
	unset($_SESSION["msg"]);
}
if(is_array($alert_err) && count($alert_err)>0)
{
	?>
	<div class="alert alert-error">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-warning-sign"></i> <b>Notice</b> <?php echo $alert_err[0] ?></i>
	</div>
	<?php
}
?>
<div class="row-fluid">
    <form method="post" action="" enctype="multipart/form-data" onsubmit="return validate()">
        <!-- Forms: Box -->
        <div class="span12">
    
            <!-- Forms: Top Bar -->
            <div class="top-bar">
                <h3><i class="icon-cog"></i> Site Setting</h3>
            </div>
            <!-- / Forms: Top Bar -->
            <!-- Forms: Content -->
            <div class="well no-padding">
				<?php
                foreach($aryForm as $key=>$value){
                    ?>
                    <div class="control-group">
                    <label class="control-label" f for="<?php echo $key; ?>"><?php echo ucfirst(str_replace("_"," ",$key)); ?></label>
                    <div class="controls">
                     <textarea class="input-block-level" <?php if($key=="message") echo 'style="height:160px"'; ?> name="<?php echo $key; ?>" id="<?php echo $key; ?>" ><?php echo $value; ?></textarea>
                    </div>
                    </div>
				<?php
                }
                ?>
            </div>
            <div class="form-actions">
                <button type="submit" class="btn btn-primary"> Save</button>
                <button type="button" class="btn"> Cancel</button>
            </div>
        </div>
	</form>
 
</div>

<script>

    function validate(){
        var smslimit = document.getElementById('sms_limit').value;
        
        if(smslimit == 160 || smslimit == 320)
        {
            return true;
        }
        
        alert('SMS Limit must be either 160 or 320');        
        return false;
        
    }

</script>

