<?php
/*
 * This file is part of a larger software package. Please read the License file 
 * in the root of this installation for terms of usage and distribution.
 * 
 * @author The Web Pro Miami, Inc. <info@thewebpro.com>
 * @copyright The Web Pro Miami, Inc. <info@thewebpro.com>, http://www.thewebpro.com/
 * 
 * CORE: manage/edit/system_settings.php - version 1.0
*/

require("../inc/stop_cache.php");

$nAdminOnlyPg = 1;

// Secure and connect
require("../../inc/dbconnect.php");
require("../inc/secure_page.php");

require("../../inc/config.php");
require("../inc/lang.php");
require("../inc/langutil.php");
include("../inc/calendar.php");
include("../inc/rc4.php");


// Get passed-in vars
$nRecID = (isset($_GET['id']) ? $_GET['id'] : NULL);
if(!$nRecID){ $nRecID = (isset($_POST['id']) ? $_POST['id'] : NULL); }
$nEditMode = (isset($_GET['editMode']) ? $_GET['editMode'] : NULL);
if(!$nEditMode){ $nEditMode = (isset($_POST['editMode']) ? $_POST['editMode'] : NULL); }

// AJAX Specific vars
$nPassThru = "?id=$nRecID";
$nDivToUpdate = "settingsTable"; // must be same as parent edit page
$nPageInDiv = "edit/sys_settings.php";

// Get info
$nSettingsARRAY = array();
$nSQL = "SELECT * FROM nSettings";
mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
while($Row = mysql_fetch_array($Result)){
	$nSetting = $Row["nSetting"];
	$nValue = $Row["nValue"];
	$nSettingsARRAY[$nSetting] = $nValue;
}

$nCoName = $nSettingsARRAY["coName"];
$nSiteName = $nSettingsARRAY["siteName"];
$nFullSitePath = $nSettingsARRAY["fullSitePath"];
$nForceWWW = $nSettingsARRAY["forceWWW"];
?>

<?php if(!$nEditMode){ ?>

<div id="editBox">
	<div id="editBoxStatic" class="form no-border">
		<table border="0" cellspacing="0" cellpadding="0">
		  <tr>
			<td valign="top" style="padding-right:10px;"><strong><?php echo _("Site Name"); ?>:</strong></td>
			<td valign="top"><?php echo $nCoName;?></td>
		  </tr>
		  <tr>
			<td valign="top" style="padding-right:10px;"><strong><?php echo _("Site URL Name"); ?>:</strong></td>
			<td valign="top"><?php echo $nSiteName;?> <?php if($nForceWWW){?><span class="caption"><?php echo _("('www.' use enforced)"); ?></span><?php } ?></td>
		  </tr>
		  <tr>
			<td valign="top" style="padding-right:10px;"><img src="img/website_s.png" alt="" width="16" height="16" border="0" style="margin:0px 4px -2px 0px;"><strong><?php echo _("Site Path:"); ?></strong></td>
			<td valign="top"><a href="<?php echo $nFullSitePath;?>" target="_blank"><?php echo $nFullSitePath;?><img src="img/external.png" alt="" width="16" height="16" border="0" style="margin:0 0 -3px 4px;"></a></td>
		  </tr>
		</table>
		
		<div class="clearfix center" id="buttonBox">
			<input type="button" name="nEdit" value="<?php echo _("Edit"); ?>" class="button blue" onClick="loadContentToID('#<?php echo $nDivToUpdate?>','<?php echo $nPageInDiv.$nPassThru?>&editMode=1');">
		</div>
	</div>
</div>
<?php } ?>

<?php if($nEditMode){ ?>
<div id="editBox">
<?php
include("../inc/form_class.php");

$nFormName = "FormEdit";
$nForm1 = new Form();
//$nForm1->DEBUG = true;
$nForm1->Name = $nFormName;
$nForm1->Action = $_SERVER['PHP_SELF'];
$nForm1->Encoding = "multipart/form-data";
$nForm1->SubmitText = _("Update");
$nForm1->CancelText = _("Cancel");
$nForm1->CancelRedirect = "loadContentToID('#$nDivToUpdate','$nPageInDiv$nPassThru');";
$nForm1->CancelRedirectIsJS = true;
// AJAX settings
$nForm1->SubmitToJS = "return submitForm('$nDivToUpdate','$nPageInDiv$nPassThru',this.name);return false;";
// Form styling
$nForm1->FormClass = "form no-border";
$nForm1->CleanMarkup = true;
$nForm1->SubmitClass = "button blue";
$nForm1->CancelClass = "button";
$nForm1->ButtonTemplate = "<div class=\"clearfix grey-highlight\"><div class=\"input no-label\">[#Buttons#]</div></div>\n";
$nForm1->FieldTemplate = "<div id=\"[#RowID#]\" class=\"clearfix\" style=\"[#RowStyle#]\">[#Label#]<div class=\"input\" style=\"[#ItemStyle#]\">[#BeforeField#][#Field#][#AfterField#]<span class=\"info\">[#Caption#]</span></div></div>\n";
$nForm1->GroupTemplate = "<div id=\"[#ID#]\" style=\"[#Style#]\" class=\"[#Class#]\">\n[#Name#][#FieldCode#]\n</div>\n";


// Create groups
$nFormGroup1 = new FormGroup();
//$nFormGroup1->Name = "Defaults";

// Create fields
$nFormField = new FormField("hidden","editMode");
$nFormField->Value = "1";
$nFormGroup1->AddField($nFormField);

$nFormField = new FormField("text","nCoName",_("Site Name:"));
$nFormField->Value = $nCoName;
$nFormField = $nForm1->AddValidation("maxlength",$nFormField,100);
$nFormField->Caption = _("(max. 100 characters, e.g. My Website)");
$nFormField->Class = "xlarge";
$nFormGroup1->AddField($nFormField);

$nFormField = new FormField("text","nSiteName",_("Site URL Name:"));
$nFormField->Value = $nSiteName;
$nFormField = $nForm1->AddValidation("maxlength",$nFormField,100);
$nFormField->Caption = _("(max. 100 characters, e.g. MyWebsite.com)");
$nFormField->Class = "xlarge";
$nFormGroup1->AddField($nFormField);

$nFormField = new FormField("text","nFullSitePath","<img src=\"img/website_s.png\" alt=\"\" width=\"16\" height=\"16\" border=\"0\" style=\"margin:0px 4px -2px 0px;\">"._("Site Path:"));
$nFormField->Value = $nFullSitePath;
$nFormField->onBlur = "formatURL(this);";
$nFormField = $nForm1->AddValidation("maxlength",$nFormField,255);
$nFormField->Caption = _("(max. 255 characters, e.g. http://www.mywebsite.com/)");
$nFormField->Class = "xlarge";
$nFormGroup1->AddField($nFormField);

$nFormField = new FormField("checkbox","nForceWWW","");
$nFormField->Value = $nForceWWW;
$nFormField->DataARRAY = array("nForceWWW" => ""._("Force use of 'www.' subdomain")."<BR><span class=\"caption\">"._("(IMPORTANT: If this is turned on, make sure \"www.\" is included in your Site Path above)")."</span>");
if($nForceWWW){ $nFormField->SelectedARRAY = array("nForceWWW" => "selected"); }
$nFormGroup1->AddField($nFormField);

/*
$nFormField = new FormField("file","nSiteLogo",_("Logo (for Manager only):"));
$nFormField->Caption = _("(format: PNG, width:400px max., height:80px max., 5Mb or less only)");
$nFormField->onBlur = "uploadFile(this.id,'$nFormName','width:420px; height:100px; background-color:#AAA;');";
$nFormGroup1->AddField($nFormField);
*/

// Finalize and display form
$nForm1->AddGroup($nFormGroup1);
$nFormToDisplay = $nForm1->GetForm();

// AJAX redirect if form submitted w/o error, otherwise display form (again, if necessary)
if($nForm1->HasProcessed){
	// Save other settings
	if(isset($_POST["nCoName"])){ $nCoName = $_POST["nCoName"]; } else { $nCoName = ""; }
	$Query = "UPDATE nSettings SET nValue='$nCoName' WHERE nSetting='coName' LIMIT 1";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
	
	if(isset($_POST["nSiteName"])){ $nSiteName = $_POST["nSiteName"]; } else { $nSiteName = ""; }
	$Query = "UPDATE nSettings SET nValue='$nSiteName' WHERE nSetting='siteName' LIMIT 1";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
	
	if(isset($_POST["nFullSitePath"])){ $nFullSitePath = $_POST["nFullSitePath"]; } else { $nFullSitePath = ""; }
	$Query = "UPDATE nSettings SET nValue='$nFullSitePath' WHERE nSetting='fullsitePath' LIMIT 1";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
	
	if(isset($_POST["nForceWWW"])){ $nForceWWW = $_POST["nForceWWW"]; } else { $nForceWWW = ""; }
	$Query = "UPDATE nSettings SET nValue='$nForceWWW' WHERE nSetting='forceWWW' LIMIT 1";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
	
	echo "<script>window.location.reload();</script>"; // reload full page (for logo update)
	//echo "<script>loadContentToID('#$nDivToUpdate','$nPageInDiv$nPassThru');</script>";
} else {
	echo $nFormToDisplay;
}
?>
<script>
uploadFile = function(fldName,frmName,iframeStyle){
	fldName = "#"+fldName;
	frmName = "#"+frmName;
	if($(fldName).val()){
		// save original form info
		var currFormOnSumbit = $(frmName).attr('onsubmit');
		var currFormTarget = $(frmName).attr('target');
		var currFormAction = $(frmName).attr('action');
		// set new form info to post to
		$(frmName).attr('target',"imageSubmitFrame");
		$(frmName).attr('onsubmit',"");
		$(frmName).attr('action',"inc/imgupload.php");
		// create new iframe
		if($('#imageSubmitFrame')){ $('#imageSubmitFrame').remove(); }
		$('<iframe name="imageSubmitFrame" id="imageSubmitFrame" scrolling="no" frameborder="0" />').appendTo('body');
		$(frmName).submit();
		// reset form info
		$(frmName).attr('target',currFormTarget);
		$(frmName).attr('onsubmit',currFormOnSumbit);
		$(frmName).attr('action',currFormAction);
	}
}
</script>
<script>
formatURL = function(fld){
	fldVal = fld.value;
	if(fldVal){
		// strip all spaces
		var fldValStr = new String(fldVal); 
		fldVal = fldValStr.replace(/ /g,"");
		// strip "http://" (to be re-added later)
		if(fldVal){
			var fldValStr = new String(fldVal); 
			fldVal = fldValStr.replace(/http:\/\//gi,'');
		}
		// add a trailing slash, and remove double-slashes (just in case user already put one)
		fldVal = fldVal+"/";
		if(fldVal){
			var fldValStr = new String(fldVal); 
			fldVal = fldValStr.replace("//","/");
		}
		fldVal = "http://"+fldVal;
	}
	fld.value = fldVal;
}
</script>
</div>
<?php } ?>
<?php require("../../inc/db_close.php"); ?>
