#!/usr/bin/perl --
use lib '/home/irasperi/public_html/mylibs';
use CGI;

#################################################################### 
#
#	Attachment Mailer Plus
#	©2005, PerlScriptsJavaScripts.com
#
#	Requires: Perl5+
#	Created:  March 12, 2002
#	Author:   PerlScriptsJavaScripts.com
#	Contact:  john@perlscriptsjavascripts.com
#	
#	This script / program is copyright material!
#	
#################################################################### 
#
#	There is no need to edit anything in this file
#	Edit the _key.cgi file only
#
#################################################################### 

#my ( $ext, %HSH, $ScriptName, $publicURL, $HN, $OS, @qstr, $cmnd, $val1, $val2 );
#my ( $isMac, $isUNIX, $isWIN, %uploadSuccess, %uploadFailure );

$ext        = "cgi";
$ScriptName = "amp"; 
$publicURL  = "$ScriptName\.$ext";
$CGITempFile::TMPDIRECTORY = '/home/irasperi/public_html/tmp';

# Determine OS and set a seperator
$OS = $^O;
if($OS =~ /Darwin/i) { $isMac = 1; $isUNIX = 1;}
elsif($OS =~ /win/i) { $isWIN = 1; }
else {$isUNIX = 1;}
if($isWIN){ $S{S} = "\\\\"; }
else { $S{S} = "/";}

# get config file
if($isWIN){	
	$absPath            = $ENV{PATH_TRANSLATED};
	$relPath            = $ENV{PATH_INFO};
	$relPath            =~ s/\//$S{S}/g;
	$ENV{DOCUMENT_ROOT} = $absPath; 
	$ENV{DOCUMENT_ROOT} =~ s/$relPath//i; 
	
	$TransPath          = $ENV{PATH_TRANSLATED};
	$ConfigPath         = $TransPath;
	$ConfigPath         =~ s/\.$ext/\_cfg\.$ext/;
	$KeyPath            = $TransPath;
	$KeyPath            =~ s/\.$ext/\_key\.$ext/;
} elsif($isMac){
	$TransPath          = $ENV{SCRIPT_FILENAME};
	$ConfigPath         = $TransPath;
	$ConfigPath         =~ s/\.$ext/\_cfg\.$ext/;
	$KeyPath            = $TransPath;
	$KeyPath            =~ s/\.$ext/\_key\.$ext/;
} else {
	$ConfigPath = "$ScriptName\_cfg\.$ext";
	$KeyPath    = "$ScriptName\_key\.$ext";
}
require $ConfigPath; 
require $KeyPath; 

# immediate security check
if($DomainCheck =~ /\w+/ && $ENV{HTTP_REFERER} !~ /$DomainCheck/i){ 
	&error('Form resides on foreign domain'); exit;
}

$ScriptName .= $FolderPass;

$ScriptURL = qq~http://$ENV{SERVER_NAME}$ENV{SCRIPT_NAME}~;
if($SecureServer){ $ScriptURL = qq~$SecureServer/$publicURL~; }

$Data = qq~$ENV{DOCUMENT_ROOT}$S{S}psjs_datalogs$S{S}$ScriptName~;
if($DefaultData){ $Data = qq~$DefaultData$S{S}psjs_datalogs$S{S}$ScriptName~; }

use CGI; $CGI::oDISABLE_UPLOADS = $CGI::DISABLE_UPLOADS; $CGI::DISABLE_UPLOADS = 0;

@qstr = split (/=/,$ENV{QUERY_STRING});
$cmnd = $qstr[0]; # Command
$val1 = $qstr[1]; # Form ID
$val2 = $qstr[2]; # UniqueID
# not yet in use
$val3 = $qstr[3]; # required
$val4 = $qstr[4]; # groupid
$val5 = $qstr[5]; # redirect

&checkFormID;
&Initialize($FORM{id}); #$CGI::POST_MAX = $CONFIG{max_size};
&Date;
&GetCookies;

#################################################################### 
#                        Script Commands                           #
#################################################################### 

if($cmnd eq "process" && $CONFIG{preview} == 1 && $val2 ne "done"){
	&preview;
} else {
	if($cmnd eq "progress")   { &progress }
	elsif($cmnd eq "process") { &process  }
	elsif($cmnd eq "printjs") { &printjs  }
	elsif($cmnd eq "CGIinfo") { &CGIinfo  }
	else                      { &error('Invalid request') }
}

#################################################################### 

#################################################################### 

sub printjs {
	
	print "Content-type: text/html\n\n";
	
	my ( %VAR, @types1, @types2 );
	
	@types1 = split(/ +/, $CONFIG{types});
	
	foreach(@types1){ $_ = lc($_); push(@types2, $_); }
	
	if($CGI::VERSION < 3.03){ $CONFIG{useMeter} = 2; }
	
	if($CONFIG{useMeter} != 2){
	print qq~
	var InitialState = $CONFIG{InitialState};  // 1 or 2 = hidden or visible 
	var showDetails  = $CONFIG{showDetails};   // 1 or 2 = hidden or visible 
	var showFileList = $CONFIG{showFileList};  // 1 or 2 = hidden or visible 
	var MeterWidth   = $CONFIG{MeterWidth};    // Must be multiple of 100 pixels
	
	var HideForm     = $CONFIG{HideForm};      // Colapse form when submitted ? 1 : 2
	var DisableBtn   = $CONFIG{DisableBtn};    // Disable Submit Button onsubmit ? 1 : 2
	
	var Row1BG       = "$CONFIG{Row1BG}";      // File stat table's first row background color
	var Row2BG       = "$CONFIG{Row2BG}";      // 2nd and 4th row's background colors
	var Row3BG       = "$CONFIG{Row3BG}";      // 3rd row's background color
	
	var CurRowBG     = "$CONFIG{CurRowBG}";    // current file row's background color
	var FinRowBG     = "$CONFIG{FinRowBG}";    // finished file row's background color
	var borderClr    = "$CONFIG{borderClr}";   // color of border for both info tables
	var borderWidth  = $CONFIG{borderWidth};   // pixel width of border for both info tables
	
	var ProgBarBG    = "$CONFIG{ProgBarBG}";   // Progress bar's background color
	var ProgBarFG    = "$CONFIG{ProgBarFG}";   // Progress bar's completed color
	
	var FontSze      = $CONFIG{FontSze};       // font size in pixels
	var FontFamily   = "$CONFIG{FontFamily}";  // font name
	var FontClr      = "$CONFIG{FontClr}";     // font color
	
	var UseKBytes    = $CONFIG{UseKBytes};     // 1 or 2 = kb or mb
	var JSdebugging  = $CONFIG{JSdebugging};   // 1 or 2 = kb or mb
	~;
	}
	
	print qq~
	var req          = 0;
	var JSargs       = "";
	var ScriptURL    = "";
	var ScriptCmd1   = "";
	var ScriptCmd2   = "progress";
	var UID          = new Date(); UID = UID.getTime(); 
	var types        = new Array();
	~;
	
	for($a = 0; $a < @types2; $a++){
		print qq~types[$a] = "$types2[$a]";\n\t~;
	}
	
	if($CONFIG{useMeter} != 2){
	print qq~
	function printProgressMeter(){
		var UMMeter = "";
		
		BS         = UseKBytes == 1 ? 'KB' : 'MB';
		divVis     = InitialState == 1 ? 'block' : 'none';
		
		MeterWidth = MeterWidth % 100 == 0 ? MeterWidth : 300;
		MeterWidth = MeterWidth < 100 ? 100 : MeterWidth > 1000 ? 1000 : MeterWidth;
		MeterFactr = MeterWidth / 100;
		
		UMMeter += '<style>\\n';
		UMMeter += '.progTable {\\nfont-family: ' + FontFamily + ';\\nfont-size: ' + FontSze;
		UMMeter += 'px;\\ncolor: ' + FontClr + '}\\n';
		UMMeter += '.head3D {\\nborder-top: #ffffff solid 1px;\\n';
		UMMeter += 'border-bottom: #000000 solid 1px;\\n}\\n';
		UMMeter += '.row3D {\\nborder-top: #eeeeee solid 1px;\\n';
		UMMeter += 'border-bottom: #666666 solid 1px;\\n}\\n';
		UMMeter += '#progcase {\\nbackground:  ' + ProgBarBG + ';\\n';
		UMMeter += 'border: ' + borderClr + ' solid ' + borderWidth + 'px;\\n';
		UMMeter += '\\nheight: 20px;\\nwidth: ' + MeterWidth + 'px;\\n}\\n';
		UMMeter += '#progstat {\\nbackground:' + ProgBarFG + ';\\n';
		UMMeter += 'border-top: white outset 1px;\\nborder-right: white outset 1px;\\n';
		UMMeter += 'border-bottom: white outset 1px;\\nborder-left: 0px;\\n';
		UMMeter += 'height: 18px;\\nwidth: 0px;\\n}\\n</style>\\n';
		
		UMMeter += '<table border="0" cellpadding="0" cellspacing="0"><tr><td>';
		UMMeter += '<div id="progscreen" style="position: relative; display:' + divVis + ';';
		UMMeter += 'font-family: ' + FontFamily + '; font-size:' + FontSze + 'px;">\\n\\n';
		UMMeter += '<div id="progcase"><div id="progstat"></div></div>\\n';
		UMMeter += '<div id="progtext" style="position: relative; text-align: ';
		UMMeter += 'center; height: 20px;"></div>\\n\\n';
		
		if(showDetails == 1 || JSdebugging == 1){
			UMMeter += '<table border="0" class="progTable" cellpadding="2" cellspacing="0" ';
			UMMeter += 'style="border: ' + borderClr + ' solid ' + borderWidth + 'px;" ';
			UMMeter += 'width="' + MeterWidth + '">\\n';
			UMMeter += '<tr bgcolor="' + Row1BG + '">\\n';
			UMMeter += '<td class="head3D" width="25%" nowrap>Completed</td>\\n';
			UMMeter += '<td class="head3D" width="25%" nowrap>Transferred (' + BS + ')</td>\\n';
			UMMeter += '<td class="head3D" width="25%" nowrap>Rate (KB/S)</td>\\n';
			UMMeter += '<td class="head3D" width="25%" nowrap>Time Left</td>\\n';
			UMMeter += '</tr>\\n';
			UMMeter += '<tr bgcolor="' + Row2BG + '">\\n';
			UMMeter += '<td class="row3D" id="fDone" nowrap>&nbsp;</td>\\n';
			UMMeter += '<td class="row3D" id="dDone" nowrap>&nbsp;</td>\\n';
			UMMeter += '<td class="row3D" id="tRate" nowrap>&nbsp;</td>\\n';
			UMMeter += '<td class="row3D" id="lTime" nowrap>&nbsp;</td>\\n';
			UMMeter += '</tr>\\n';
			UMMeter += '</table><br>\\n';
		}
		
		if(showFileList == 1 || JSdebugging == 1){
			UMMeter += '<div id="fListDiv"><table border="0" class="progTable" ';
			UMMeter += 'style="border: ' + borderClr + ' solid ' + borderWidth + 'px;" ';
			UMMeter += 'cellpadding="2" cellspacing="0" width="' + MeterWidth + '">';
			UMMeter += '<tr bgcolor="' + Row1BG + '">';
			UMMeter += '<td class="head3D" width="25%">File Name</td>';
			UMMeter += '<td class="head3D" width="25%">Status</td>';
			UMMeter += '</tr>';
			UMMeter += '<tr bgcolor="' + Row2BG + '">';
			UMMeter += '<td class="row3D" colspan="2">&nbsp;</td>';
			UMMeter += '</tr></table></div>';
		}
		
		if(JSdebugging == 1){
			UMMeter += '<br><div id="showdata" class="progTable" ';
			UMMeter += 'style="border: ' + borderClr + ' solid ' + borderWidth + 'px; ';
			UMMeter += 'padding: 3px; background : #eeeeee;">Troubleshooting Data <a href="$ScriptURL?CGIinfo" target="_blank">Click here for more information</a></div>\\n';
		}
		
		UMMeter += '</div></td></tr></table>\\n'; document.write(UMMeter);
	}
	
	function sendRqst(URL){
		if(window.ActiveXObject){ // MSIE
			req = new ActiveXObject("Microsoft.XMLHTTP");
		} else if(window.XMLHttpRequest){ // Moz and Opera
			req = new XMLHttpRequest();
			if(req.overrideMimeType){ req.overrideMimeType('text/xml'); }
		}
		if(!req){
			msg  = 'Your Web Browser does not support XML Override technology<br>';
			msg += 'File Transfer continuing without Progress Meter.<br><br>';
			msg += '<b>Please wait while your files are uploaded.</b><br>';
			document.getElementById('progscreen').innerHTML = msg;
			return;
		}
		
		req.onreadystatechange = getResults; req.open('GET', URL, true); req.send(null);
	}
	
	function getResults(){
		if(req.readyState == 4){
			if(req.status == 200){
				res = new Array(); res = req.responseText.split('||');
				
				if(req.responseText){
					totalSize  = res[0]; bytesDone = res[1]; 
					secsLapsed = res[2]; filePrgss = res[3];
					
					// Client totalSize||filesDone||bytesDone||duration in secs
					if(req.responseText.search(/^Complete/i) >= 0){
						msg = '<b>File transfer complete.... please wait....</b><br>';
						if(JSdebugging == 1){
							msg += '<br><font size="3">' + req.responseText + '</font>';
						}
						document.getElementById('progscreen').innerHTML = msg;
						return;
					} else if(req.responseText.search(/^NoLog/i) >= 0){
						msg = '<b>Querying.... please wait....</b><br>';
						if(JSdebugging == 1){
							msg += '<br><font size="3">' + req.responseText + '</font>';
						}
						document.getElementById('progscreen').innerHTML = msg;
						return;
					}
					
					filePrgss  = filePrgss.substr(0, filePrgss.length - 2);
					
					fsD = new Array(); fsD = filePrgss.split('++');
					filesDone  = fsD.length - 1; // skip current file
					
					bytesDone = bytesDone < 1 ? 1 : bytesDone;
					
					percentDone = Math.floor((bytesDone / totalSize) * 100);
					percentDone = percentDone < 1 ? 1 : percentDone;
					progressPxW = percentDone * MeterFactr;
					progressPxW = parseInt(progressPxW);
					progressPxS = progressPxW.toString();
					if(progressPxS.search(/^\\d+\$/) < 0){progressPxW = 1}
					secsReqed   = parseInt((secsLapsed * 100) / percentDone);
					
					transRate   = convertBits(bytesDone / secsLapsed);
					percentHT   = percentDone + '%';
					
					if(JSdebugging == 1){
						msg  = '<table><tr><td>totalFiles</td><td>' + totalFiles + '</td></tr>\\n';
						msg += '<tr><td>percentDone</td><td>' + percentDone + '</td></tr>\\n';
						msg += '<tr><td>progressPxW</td><td>' + progressPxW + '</td></tr>\\n';
						msg += '<tr><td>fileNames</td><td>' + fileNames + '</td></tr>\\n';
						msg += '<tr><td>RawString</td><td>' + req.responseText + '</td></tr>';
						msg += '</table>\\n';
						document.getElementById('showdata').innerHTML = msg;
					}
					
					if(showDetails == 1 || JSdebugging == 1){
						secsLeft  = convertSecs(secsReqed - secsLapsed);
						totalSize = convertBits(totalSize);
						bytesDone = convertBits(bytesDone);
						
					document.getElementById('fDone').innerHTML = filesDone  + ' of ' + totalFiles;
					document.getElementById('dDone').innerHTML = bytesDone  + ' of ' + totalSize;
					document.getElementById('tRate').innerHTML = transRate;
					document.getElementById('lTime').innerHTML = secsLeft;
					}
					
					if(showFileList == 1 || JSdebugging == 1){
						allFiles = new Array();
						allFiles = fileNames.split('++');
						
						dneFiles = new Array();
						dneFiles = filePrgss.split('++');
						
					fListMsg  = '<table border="0" class="progTable" cellpadding="2" ';
					fListMsg += 'style="border: ' + borderClr + ' solid ' + borderWidth + 'px;" ';
					fListMsg += 'cellspacing="0" width="' + MeterWidth + '">\\n';
					fListMsg += '<tr bgcolor="' + Row1BG + '">\\n';
					fListMsg += '<td class="head3D" width="25%">File Name</td>\\n';
					fListMsg += '<td class="head3D" width="25%">Status</td>\\n';
					fListMsg += '</tr>\\n';
						
						for(a = 0; a < allFiles.length; a++){
							bg = a % 2 == 0 ? Row2BG : Row3BG;
							
							if(a < dneFiles.length - 1){
								fileStatus = 'Finished';
								bg = FinRowBG;
							} else if(a == dneFiles.length - 1){
								thisFile = new Array();
								thisFile = dneFiles[a].split('=');
								if(thisFile[1] == 'undefined' || !thisFile[1]){
									thisFile = '0.00';
								} else {
									thisFile = convertBits(thisFile[1]);
								}
								fileStatus = thisFile + '' + BS;
								bg = CurRowBG;
							} else {
								fileStatus = 'Pending';
							}
							fListMsg += '<tr bgcolor="' + bg + '">\\n';
							fListMsg += '<td class="row3D">' + allFiles[a] + '</td>\\n';
							fListMsg += '<td class="row3D">' + fileStatus + '</td>\\n';
							fListMsg += '</tr>\\n';
						}
						
						fListMsg += '</table>\\n';
						document.getElementById('fListDiv').innerHTML = fListMsg;
					}
				}
				document.getElementById('progtext').style.top   = '-19px';
				document.getElementById('progtext').innerHTML   = percentHT;
				document.getElementById('progstat').style.width = progressPxW + 'px';
				
				window.setTimeout("sendRqst(ScriptURL + '?' + ScriptCmd2 + '=' + JSargs)", 1000);
			} else {
				alert('Progress request failed. Data transfer has terminated .... ' + req.status);
			}
		}
	}
	~;
	}
	
	print qq~
	function transInit(b){
		if(arguments.length > 1 && arguments[1] == false){ return; }
		f = b.form; ScriptURL = f.action; totalFiles = 0; fileName = ""; fileNames = "";
		typeOK = 0; same1 = 0; same2 = 0; same3 = 0;
		
		ScriptCmd1 = ScriptURL.indexOf("?");
		ScriptCmd1 = ScriptURL.substr(ScriptCmd1 + 1);
		ScriptURL  = ScriptURL.replace(/\\?.*/ig, "");
		
		for(a = 0; a < f.elements.length; a++){
			if(f.elements[a].name.search(/^FILE\\d+\$/) >= 0){
				if(f.elements[a].value.length > 1){
					
					totalFiles++; typeOK = 0; same1 = 0; same2 = 0; same3 = 0;
					f.elements[a].style.color = "#000000";
					
					fileName   = f.elements[a].value;
					fileName   = fileName.replace(/^.*(\\\\|\\/)/ig, "");
					fileNames += fileName + '++';
					
					ext = f.elements[a].value.lastIndexOf(".");
					ext = f.elements[a].value.substr(ext + 1);
					ext = ext.toLowerCase();
					
					for(c = 0; c < types.length; c++){
						if(ext == types[c] || types[c] == "all"){ typeOK++; }
					}
					if(!typeOK){
						msg  = "Illegal file type.\\n\\nWe do not allow " + ext + " ";
						msg += "file uploads.\\n\\nPlease change or remove the selected ";
						msg += "file from the hilighted field."; alert(msg);
						f.elements[a].style.color = "#00aa00";
						return;
					}
					typeOK = 0;
				}
				
				for(aa = a + 1; aa < f.elements.length - 1; aa++){
					nextF = f.elements[aa].value;
					if(f.elements[a].value == nextF && nextF.length > 0){
						same1++; same2 = a; same3 = aa;
					}
					if(same1){
						msg  = "Duplicate file name.\\n\\nTwo or more fields contain ";
						msg += "identical file names\\n\\nPlease change or remove the ";
						msg += "Duplicate file."; alert(msg);
						f.elements[same2].style.color = "#00aa00";
						f.elements[same3].style.color = "#00aa00";
						return;
					}
				}
			}
		} fileNames  = fileNames.substr(0, fileNames.length - 2);
		
		JSargs   = f.id.value + '=' + UID;
		f.action = ScriptURL + '?' + ScriptCmd1 + '=' + JSargs; f.submit();
		~;
		
		if($CONFIG{useMeter} != 2){
		print qq~
		if(totalFiles){
			if(HideForm == 1){f.style.display = 'none';}if(DisableBtn == 1){b.disabled = true;}
			document.getElementById('progscreen').style.display = 'block';
			window.setTimeout("sendRqst(ScriptURL + '?' + ScriptCmd2 + '=' + JSargs)", 1000);
		} else { document.getElementById('progscreen').style.display = 'none'; }
		~;
		}
		
		print qq~
	}
	~;
	
	if($CONFIG{useMeter} != 2){
	print qq~
	function convertBits(bytes){
		bytes  = bytes / 1024; if(UseKBytes == 2){ bytes = bytes / 1024;}
		bytes  = Math.floor(100 * bytes) / 100;
		bytes  = String(bytes); ena = bytes.length - 2; dio = bytes.length - 3;
		deci   = bytes.substr(ena,1) == "." ? "0" : bytes.substr(dio,1) == "." ? "" : ".00";
		bytes += deci; return bytes;
	}
	
	function convertSecs(secs){
		remsecs = 0; remmins = 0; remhrs  = 0;
		if(secs >= 3600){
			remsecs = secs % 60;
			remmins = secs / 60;
			remhrs  = secs / 3600;
		} else if(secs >= 60){
			remsecs = secs % 60;
			remmins = secs / 60;
		} else { remsecs = secs % 60; }
		
		remsecs = parseInt(remsecs);
		remmins = parseInt(remmins);
		remhrs  = parseInt(remhrs);
		
		remsecs = remsecs < 10 ? '0' + remsecs : remsecs;
		remmins = remmins < 10 ? '0' + remmins : remmins;
		remhrs  = remhrs  < 10 ? '0' + remhrs  : remhrs ;
		secs    = remhrs + ':' + remmins + ':' + remsecs;
		return secs;
	}
	printProgressMeter();
	~;
	}
}

#################################################################### 

#################################################################### 

sub progress {
	my ( %VAR, @lne, @sts, @divs );
	
	print "Cache-Control: no-store, no-cache\n"; 
	print "Content-type: text/xml\n\n";
	
	if(open(N, "<$CONFIG{def_dir}$S{S}$val2\.prog")){
		flock N, 1; $VAR{lne} = <N>; close N;
		
		@lne = split(/\|\|/, $VAR{lne});
		@sts = split(/\+\+/, $lne[2]);
		
		$VAR{bts} = 0; 
		$VAR{scs} = time - $lne[0];
		
		for(@sts){ @divs = split(/\=/, $_); $VAR{bts} += $divs[1]; } 
		
		print qq~$lne[1]||$VAR{bts}||$VAR{scs}||$lne[2]~;
	} else { print qq~NoLog: $CONFIG{def_dir}$S{S}$val2\.prog $!~; }
}

#################################################################### 

#################################################################### 

sub preview {
	my ( %VAR, %hash, %TMP, %formkey, @files, %done, %hide, @ar, @adm, @req );
	my ( @todel, @UID, @name, @thankstemplate, @bbuttemplate, @nbuttemplate );
	my ( @missing, @sorted, @sorttemplate );
	
	unless(-d $CONFIG{def_dir}){
		mkdir($CONFIG{def_dir}, 0777);
		chmod(0777, $CONFIG{def_dir});
	}
	
	if($CGI::VERSION < 3.03 || $CONFIG{useMeter} == 2){
		$VAR{CGI} = new CGI; @FKEYS = $VAR{CGI}->param;
		foreach(@FKEYS){
			$FORM{$_} = $VAR{CGI}->param($_);
			if($_ =~ /^FILE\d+$/ && $FORM{$_} =~ /\w+/){
				$files{$_} = $FORM{$_};
			}
		}
		
		&CGIParse;
		
		$VAR{res} = &psjs_uploadV1(%files);
	} else {
		$VAR{res} = &psjs_uploadV2; $VAR{delStatFile} = 1;
	}
	
	@name      = &getline($FORM{id}, "$Data$S{S}data$S{S}details\.list");
	@req       = split(/\,/, $FORM{required});
	$VAR{time} = time;
	
	foreach(@req){
		unless($FORM{$_}){
			$VAR{missing}++;
			$_ =~ s/\_/ /ig;
			
			$VAR{marker}   = $marker{$_};
			$VAR{marker} ||= $_;
			
			$VAR{mfields} .= qq~$VAR{marker}<br>~;
		}
	}
	
	if($FORM{email}){
		unless(check_email($FORM{email})){
			&PrintHead($FORM{id});
			
			print qq~
			<table border="0" cellpadding="3" cellspacing="0" style="color:$CONFIG{text}; font-size:$CONFIG{size}; font-family:$CONFIG{face};" width="$CONFIG{previewtabw}">
			<tr>
			<td>Please enter a valid e-mail address</td>
			</tr>
			<tr><form>
			<td><br><input type="Button" value=" Back " onclick="history.back();" style="cursor:hand;"></td>
			</tr></form>
			</table>~;
			
			&PrintFoot($FORM{id});
			exit;
		}
	}
	
	if($VAR{missing}){
		$VAR{missing} = qq~<font color="#ff0000"><b>Error : You forgot to enter the following required fields : </b></font>
		<p>
		$VAR{mfields}
		~;
	
		&PrintHead($FORM{id});
		
		print qq~
		<table border="0" cellpadding="3" cellspacing="0" style="color:$CONFIG{text}; font-size:$CONFIG{size}; font-family:$CONFIG{face};" width="$CONFIG{previewtabw}">
		<tr>
		<td>$VAR{missing}</td>
		</tr>
		<tr><form>
		<td><br><input type="Button" value=" Back " onclick="history.back();" style="cursor:hand;"></td>
		</tr></form>
		</table>~;
		
		&PrintFoot($FORM{id});
		exit;
	}
	
	if($VAR{res} =~ /failed/i){
		&PrintHead($FORM{id});
		
		print qq~
		<form>
		<table border="0" cellpadding="3" cellspacing="0" style="color:$CONFIG{text}; font-size:$CONFIG{size}; font-family:$CONFIG{face};" width="$CONFIG{previewtabw}">
		<tr>
		<td><b>File Upload Failed.</b> <p> $VAR{res}</td>
		</tr>
		<tr>
		<td><br><input type="Button" value=" Back " onclick="history.back();"></td>
		</tr>
		</table>
		</form>
		~;
		
		&PrintFoot($FORM{id});
		exit;
	}
	
	if($CONFIG{pass} ne $FORM{pass}){
		&PrintHead($FORM{id});
		
		print qq~
		<table border="0" cellpadding="3" cellspacing="0" style="color:$CONFIG{text}; font-size:$CONFIG{size}; font-family:$CONFIG{face};" width="$CONFIG{previewtabw}">
		<tr>
		<td><font color="#ff0000"><b>Error:</b></font> Password entered does not match password in database.</td>
		</tr>
		<tr><form>
		<td><br><input type="Button" value=" Back " onclick="history.back();" style="cursor:hand;"></td>
		</tr></form>
		</table>~;
		
		&PrintFoot($FORM{id});
		exit;
	}
	
	&PrintPreviewHead($FORM{id});

	# don't bother showing these fields
	$hide{groupid}  = 1;
	$hide{SessID}   = 1;
	$hide{required} = 1;
	$hide{redirect} = 1;
	$hide{id}       = 1;
	$hide{ID}       = 1;
	$hide{Form_ID}  = 1;
	
	if($FORM{groupid}){
		@sorttemplate = getfile("$Data$S{S}$FORM{groupid}$S{S}mpfsorttemplate.ftf");
	} else {
		@sorttemplate = getfile("$Data$S{S}$FORM{id}$S{S}sorttemplate.ftf");
	}
	
	foreach(@sorttemplate){
		unless($_ =~ /%%%allfields%%%/i){
			if($_ =~ /\w+/){
				$_ =~ s/\%//ig;
				$_ =~ s/^\s+//ig;
				$_ =~ s/\s+$//ig;
				push(@sorted, $_);
			}
		}
		$done{$_} = 1;
	}
	
	foreach(keys %FORM){ unless($done{$_} || $hide{$_}){ push(@missing, $_); } }
	
	foreach(keys %FORM){ $formkey{$_} = 1; }
	
	if($CONFIG{previewtabw} =~ /\%/){ $VAR{userperc} = 1; }
	
	$CONFIG{previewtabw} =~ s/\D+//ig;
	$CONFIG{previewtabw} ||= qq~660~;
	
	if($VAR{userperc}){ $CONFIG{previewtabw} .= qq~%~; }
	
	print qq~
	<table border="0" cellpadding="3" cellspacing="1" style="border: white outset 1px; color:$CONFIG{text}; font-size:$CONFIG{size}; font-family:$CONFIG{face};" width="$CONFIG{previewtabw}">
	~;
	
	if($CONFIG{previewmess}){
		print qq~
		<tr bgcolor="$CONFIG{prevhead}">
		<td colspan="2">$CONFIG{previewmess}</td>
		</tr>
		~;
	}
	
	foreach(@sorted){
		unless($_ =~ /^FILE\d+Href$/ || $_ =~ /^FILE\d+Path$/ || $_ =~ /^FILE\d+Size$/ || $_ =~ /^FILE\d+Link$/){
			if($formkey{$_}){
				unless($hide{$_}){
					if($_ =~ /^FILE\d+/){
						$VAR{fldNme} = $_;
						$VAR{fldNme} =~ s/(\d+)/ $1/ig;
						$VAR{fldNme} = lc($VAR{fldNme});
						$VAR{fldNme} = ucfirst($VAR{fldNme});
					
						if($FORM{$_}){
							$VAR{$_} = $FORM{$_};
							$VAR{$_} =~ s/^.*(\\|\/)//;
							$VAR{fldVal} = qq~<a href="$CONFIG{def_url}/$VAR{$_}" target="_blank">$VAR{$_}</a>~;
						} else {
							$VAR{fldVal} = qq~~;
						}
						
						print qq~
						<tr bgcolor="$CONFIG{prevtable}">
						<td valign="top" width="50%">$VAR{fldNme}</td>
						<td valign="top" width="50%">$VAR{fldVal}</td>
						</tr>
						~;
					} elsif($_ =~ /^comments$/ || $_ =~ /TA$/){
						$VAR{fldNme} = $_;
						$VAR{fldNme} = ucfirst($VAR{fldNme});
						$VAR{fldNme} =~ s/\_/ /ig;
						$VAR{fldNme} =~ s/TA$//ig;
						$TMP{$_}     = $FORM{$_};
						$TMP{$_}     =~ s/(!LBCR!|\n)/<br>/ig;
						
						print qq~
						<tr bgcolor="$CONFIG{prevtable}">
						<td valign="top" width="50%">$VAR{fldNme}</td>
						<td valign="top" width="50%">$TMP{$_}</td>
						</tr>
						~;
					} else {
						$VAR{fldNme}   = $marker{$_};
						$VAR{fldNme} ||= $_;
						
						$VAR{fldNme} = ucfirst($VAR{fldNme});
						$VAR{fldNme} =~ s/\_/ /ig;
						$TMP{$_}     = $FORM{$_};
						
						print qq~
						<tr bgcolor="$CONFIG{prevtable}">
						<td valign="top" width="50%">$VAR{fldNme}</td>
						<td valign="top" width="50%">$TMP{$_}</td>
						</tr>
						~;
					}
				}
			}
		}
	}
	
	foreach(@missing){
		unless($_ =~ /^FILE\d+Href$/ || $_ =~ /^FILE\d+Path$/ || $_ =~ /^FILE\d+Size$/ || $_ =~ /^FILE\d+Link$/){
			if($formkey{$_}){
				unless($hide{$_}){
					if($_ =~ /^FILE\d+/){
						$VAR{fldNme} = $_;
						$VAR{fldNme} =~ s/(\d+)/ $1/ig;
						$VAR{fldNme} = lc($VAR{fldNme});
						$VAR{fldNme} = ucfirst($VAR{fldNme});
						if($FORM{$_}){
							$VAR{$_} = $FORM{$_};
							$VAR{$_} =~ s/^.*(\\|\/)//;
							$VAR{fldVal} = qq~<a href="$CONFIG{def_url}/$VAR{$_}" target="_blank">$VAR{$_}</a>~;
						} else {
							$VAR{fldVal} = qq~~;
						}
						
						print qq~
						<tr bgcolor="$CONFIG{prevtable}">
						<td valign="top" width="50%">$VAR{fldNme}</td>
						<td valign="top" width="50%">$VAR{fldVal}</td>
						</tr>
						~;
					} elsif($_ =~ /^comments$/ || $_ =~ /TA$/){
						$VAR{fldNme} = $_;
						$VAR{fldNme} = ucfirst($VAR{fldNme});
						$VAR{fldNme} =~ s/\_/ /ig;
						$VAR{fldNme} =~ s/TA$//ig;
						$TMP{$_}     = $FORM{$_};
						$TMP{$_}     =~ s/(!LBCR!|\n)/<br>/ig;
						
						print qq~
						<tr bgcolor="$CONFIG{prevtable}">
						<td valign="top" width="50%">$VAR{fldNme}</td>
						<td valign="top" width="50%">$TMP{$_}</td>
						</tr>
						~;
					} else {
						$VAR{fldNme}   = $marker{$_};
						$VAR{fldNme} ||= $_;
						
						$VAR{fldNme} = ucfirst($VAR{fldNme});
						$VAR{fldNme} =~ s/\_/ /ig;
						$TMP{$_}     = $FORM{$_};
						
						print qq~
						<tr bgcolor="$CONFIG{prevtable}">
						<td valign="top" width="50%">$VAR{fldNme}</td>
						<td valign="top" width="50%">$TMP{$_}</td>
						</tr>
						~;
					}
				}
			}
		}
	}
	
	print qq~
	</table>
	<br>
	~;
	
	#foreach(@sorted){
	#	print qq~S $_<br>~;
	#}
	
	#foreach(@missing){
	#	print qq~M $_<br>~;
	#}
	
	#foreach(keys %formkey){
	#	print qq~F $_<br>~;
	#}
	
	#foreach(@sorttemplate){
	#	print qq~T $_<br>~;
	#}
	
	print qq~
	<table border="0" cellpadding="0" cellspacing="0" style="color:$CONFIG{text}; font-size:$CONFIG{size}; font-family:$CONFIG{face};" width="$CONFIG{previewtabw}">
	<tr>
	<td align="right">
	<form style="display:inline;">
	~;
	
	foreach(keys %FORM){
		unless($_ eq "action"){
			print qq~
			<input type="Hidden" name="$_" value="$FORM{$_}">
			~;
		}
	}
	
	@bbuttemplate = getfile("$Data$S{S}$FORM{groupid}$S{S}bbuttemplate.ftf");
	
	foreach(@bbuttemplate){ $VAR{bbutstr} .= qq~$_\n~; }
	unless($VAR{bbutstr} =~ /(button|input)/i){
		@bbuttemplate = getfile("$Data$S{S}default$S{S}bbuttemplate.ftf");
		foreach (@bbuttemplate){ $VAR{bbutstr} .= qq~$_\n~; }
	}
	
	print qq~$VAR{bbutstr}</form> &nbsp; &nbsp; 
	<form action="$ScriptURL?process=$FORM{id}=done" method="post" enctype="multipart/form-data" style="display:inline;">
	~;
	
	foreach(keys %FORM){
		unless($_ eq "action"){
			print qq~
			<input type="Hidden" name="$_" value="$FORM{$_}">
			~;
		}
	}
	
	@nbuttemplate = getfile("$Data$S{S}$FORM{groupid}$S{S}nbuttemplate.ftf");
	
	foreach(@nbuttemplate){ $VAR{nbutstr} .= qq~$_\n~; }
	unless($VAR{nbutstr} =~ /(submit|input)/i){
		@nbuttemplate = getfile("$Data$S{S}default$S{S}nbuttemplate.ftf");
		foreach (@nbuttemplate){ $VAR{nbutstr} .= qq~$_\n~; }
	}
	
	print qq~$VAR{nbutstr}</form>
	</td>
	</tr>
	</table>
	~;
	
	&PrintPreviewFoot($FORM{id});
	
	unlink("$CONFIG{def_dir}$S{S}$val2\.prog") if $VAR{delStatFile};
}

#################################################################### 

#################################################################### 

sub process {
	my ( %VAR, %TMP, %files, %hash, %divs, %discard, @thankstemplate );
	my ( @ar, @adm, @req, @todel, @UID, @name, @divs, @FKEYS );
	
	if($CONFIG{preview} == 1){
		$VAR{CGI} = new CGI; @FKEYS = $VAR{CGI}->param;
		foreach(@FKEYS){ $FORM{$_} = $VAR{CGI}->param($_);}
		
		&CGIParse;
	} else {
		unless(-d $CONFIG{def_dir}){
			mkdir($CONFIG{def_dir}, 0777);
			chmod(0777, $CONFIG{def_dir});
		}
		
		if($CGI::VERSION < 3.03 || $CONFIG{useMeter} == 2){
			$VAR{CGI} = new CGI; @FKEYS = $VAR{CGI}->param;
			foreach(@FKEYS){
				$FORM{$_} = $VAR{CGI}->param($_);
				if($_ =~ /^FILE\d+$/ && $FORM{$_} =~ /\w+/){
					$files{$_} = $FORM{$_};
				}
			}
			
			&CGIParse;
			
			$VAR{res} = &psjs_uploadV1(%files);
		} else {
			$VAR{res} = &psjs_uploadV2; $VAR{delStatFile} = 1;
		}
	}
	
	@name      = &getline($FORM{id}, "$Data$S{S}data$S{S}details\.list");
	@req       = split(/\,/, $FORM{required});
	$VAR{time} = time;
	
	foreach(@req){
		unless($FORM{$_}){
			$VAR{missing}++;
			$_ =~ s/\_/ /ig;
			
			$VAR{marker}   = $marker{$_};
			$VAR{marker} ||= $_;
			
			$VAR{mfields} .= qq~$VAR{marker}<br>
			~;
		}
	}
	
	if($FORM{email}){
		unless(check_email($FORM{email})){
			&PrintHead($FORM{id});
			
			print qq~
			<table border="0" cellpadding="3" cellspacing="0" style="color:$CONFIG{text}; font-size:$CONFIG{size}; font-family:$CONFIG{face};" width="$CONFIG{previewtabw}">
			<tr>
			<td>Please enter a valid e-mail address</td>
			</tr>
			<tr><form>
			<td><br><input type="Button" value=" Back " onclick="history.back();" style="cursor:hand;"></td>
			</tr></form>
			</table>~;
			
			&PrintFoot($FORM{id});
			exit;
		}
	}
	
	if($VAR{missing}){
		$VAR{missing} = qq~<font color="#ff0000"><b>Error : You forgot to enter the following required fields : </b></font>
		<p>
		$VAR{mfields}
		~;
	
		&PrintHead($FORM{id});
		
		print qq~
		<table border="0" cellpadding="3" cellspacing="0" style="color:$CONFIG{text}; font-size:$CONFIG{size}; font-family:$CONFIG{face};" width="$CONFIG{previewtabw}">
		<tr>
		<td>$VAR{missing}</td>
		</tr>
		<tr><form>
		<td><br><input type="Button" value=" Back " onclick="history.back();" style="cursor:hand;"></td>
		</tr></form>
		</table>~;
		
		&PrintFoot($FORM{id});
		exit;
	}
	
	unless($CONFIG{preview} == 1){
		if($VAR{res} =~ /failed/i){
			&PrintHead($FORM{id});
			
			print qq~
			<form>
			<table border="0" cellpadding="3" cellspacing="0" style="color:$CONFIG{text}; font-size:$CONFIG{size}; font-family:$CONFIG{face};" width="$CONFIG{previewtabw}">
			<tr>
			<td>File Upload Failed. <p> $VAR{res}</td>
			</tr>
			<tr>
			<td><br><input type="Button" value=" Back " onclick="history.back();"></td>
			</tr>
			</table>
			</form>
			~;
			
			&PrintFoot($FORM{id});
			exit;
		}
	}
	
	if($CONFIG{pass} ne $FORM{pass}){
		&PrintHead($FORM{id});
		
		print qq~
		<table border="0" cellpadding="3" cellspacing="0" style="color:$CONFIG{text}; font-size:$CONFIG{size}; font-family:$CONFIG{face};" width="$CONFIG{previewtabw}">
		<tr>
		<td><font color="#ff0000"><b>Error:</b></font> Password entered does not match password in database.</td>
		</tr>
		<tr><form>
		<td><br><input type="Button" value=" Back " onclick="history.back();" style="cursor:hand;"></td>
		</tr></form>
		</table>~;
		
		&PrintFoot($FORM{id});
		exit;
	}
	
	# dont include in %hash storage
	$discard{id}          = 1;
	$discard{required}    = 1;
	$discard{redirect}    = 1;
	$discard{preview}     = 1;
	
	# if we got this far upload was successful
	@UID = getfile("$Data$S{S}$FORM{id}$S{S}uid\.list");
	unless($UID[0]){ $UID[0] = $CONFIG{startuid}; } $UID[0]++;
	
	if(open(N, ">$Data$S{S}$FORM{id}$S{S}uid\.list")){
		flock(N, 2);
		print N qq~$UID[0]~;
		close N;
	}
	
	$FORM{UID}      = $UID[0];
	$FORM{IP}       = $ENV{REMOTE_ADDR};
	$FORM{Host}     = &gethostname;
	$FORM{Agent}    = $ENV{HTTP_USER_AGENT};
	
	if($CONFIG{preview} == 1){
		foreach(sort(keys %FORM)){
			if($_ =~ /^FILE\d+$/ && $FORM{$_} =~ /\w+/){
				$FORM{$_} =~ s/^.*(\\|\/)//;
				push(@success, $FORM{$_});
			}
		}
	}
	
	# prepare arg for attachments
	if($success[0]){
		foreach(@success){
			$VAR{atts} .= qq~$CONFIG{def_dir}$S{S}$_\,~;
			push(@todel,  qq~$CONFIG{def_dir}$S{S}$_~);
		} chop $VAR{atts};
	}
	
	$FORM{toemail}      =~ s/\s+//ig;
	$CONFIG{adminemail} =~ s/\s+//ig;
	if($FORM{toemail}){
		@admemails = split(/\,/, $FORM{toemail});
		@admnames  = split(/\,/, $FORM{toname});
	} else {
		delete $FORM{toemail};
		$CONFIG{adminemail} =~ s/\s+//ig;
		@admemails = split(/\,/, $CONFIG{adminemail});
		@admnames  = split(/\,/, $CONFIG{adminname});
	}
	
	# history data
	foreach(keys %FORM){ unless($discard{$_}){ $hash{$_} = $FORM{$_}; } }
	
	# email names and fields
	$VAR{usrname}    = $FORM{email};
	$VAR{usrname}    = $FORM{name} if $FORM{name} =~ /\w+/;
	$VAR{usrname}    = $FORM{Name} if $FORM{Name} =~ /\w+/;
	$VAR{usrname}    = $FORM{realname} if $FORM{realname} =~ /\w+/;
	$VAR{usrname}    = qq~$FORM{fname} $FORM{lname}~ if $FORM{fname} =~ /\w+/;
	
	$VAR{usremail}   = $FORM{email} =~ /\w+/ ? $FORM{email} : $admemails[0];
	$VAR{arsubject}  = $CONFIG{arsubject};
	$VAR{admsubject} = $CONFIG{admsubject};
	
	$VAR{arAtts} = $CONFIG{arfiles};
	
	$FORM{Date} = qq~$month_name[$MONTH - 1] $DAYOFMONTH, $THISYEAR~;
	
	# admin notif
	if(check_email($admemails[0])){
		@adm = getfile("$Data$S{S}$FORM{id}$S{S}admtemplate.ftf");

		foreach(@adm){
			$_ =~ s/\r//ig;
			$VAR{adm} .= qq~$_\n~;
		}
		
		foreach(keys %FORM){ if($_ =~ /HTML$/){ $FORM{$_} =~ s/\</\&lt;/ig; } }
		
		if($VAR{adm} =~ /%%%allfields%%%/i){
			$VAR{allfields} = qq~~;
			foreach(sort(keys %FORM)){
				unless($discard{$_}){
					$VAR{marker}   = $marker{$_};
					$VAR{marker} ||= $_;
					
					if($CONFIG{mail_format} == 1){
						$FORM{$_} =~ s/\n/<br>/ig;
						$VAR{allfields} .= qq~$VAR{marker} : $FORM{$_}<br>\n~;
					} else {
						$VAR{allfields} .= qq~$VAR{marker} : $FORM{$_}\n~;
					}
				}
			}
			$VAR{adm} =~ s/%%%allfields%%%/$VAR{allfields}/ig;
		}
		
		foreach(keys %FORM){
			$TMP{$_} = $FORM{$_};
			if($CONFIG{mail_format} == 1){
				$TMP{$_} =~ s/\n/<br>/ig;
			}
			$VAR{adm} =~ s/%%%$_%%%/$TMP{$_}/ig;
		}
		
		$VAR{admmessage} = qq~$VAR{adm}\n\n$VAR{errMsg}~;
		$VAR{admmessage} =~ s/%%%.*?%%%//ig;
		$VAR{admmessage} =~ s/\r//ig;
		
		# admin subject
		foreach(keys %FORM){
			$TMP{$_} = $FORM{$_};
			$TMP{$_} =~ s/(\n|\r)/ /ig;
			$VAR{admsubject} =~ s/%%%$_%%%/$TMP{$_}/ig;
		}
		$VAR{admsubject} =~ s/%%%.*?%%%//ig;
		
		if($CONFIG{sendfiles} == 2){ $VAR{atts} = ""; }
		
		for($a = 0; $a < @admemails; $a++){
			if($CONFIG{mailprogram} =~ /\/usr\//i || $CONFIG{mailprogram} =~ /\/sendmail/i){
				&send_mail($VAR{usremail}, $VAR{usrname}, $admemails[$a], $admnames[$a], $VAR{admsubject}, $VAR{admmessage}, "", "", $VAR{atts});
				if($a == 0){
					# store first copy only
					$hash{ID}      = $VAR{time};
					$hash{Form_ID} = $FORM{id};
					&store_hist(%hash);
				}
			} else {
				&send_mail_NT($VAR{usremail}, $VAR{usrname}, $admemails[$a], $admnames[$a], $VAR{admsubject}, $VAR{admmessage}, "", "", $VAR{atts});
				if($a == 0){
					# store first copy only
					$hash{ID}      = $VAR{time};
					$hash{Form_ID} = $FORM{id};
					&store_hist(%hash);
				}
			}
		}
	}
	
	# start autoresponder
	if($CONFIG{usear} == 1){
		if(check_email($admemails[0]) && check_email($VAR{usremail})){
			@ar = getfile("$Data$S{S}$FORM{id}$S{S}artemplate.ftf");
			if(@ar){
				foreach(@ar){
					$_ =~ s/\r//ig;
					$VAR{ar} .= qq~$_\n~;
				}
				
				if($VAR{ar} =~ /%%%allfields%%%/i){
					$VAR{allfields} = qq~~;
					foreach(sort(keys %FORM)){
						unless($discard{$_}){
							$VAR{marker}   = $marker{$_};
							$VAR{marker} ||= $_;
							
							if($CONFIG{mail_format} == 1){
								$FORM{$_} =~ s/\n/<br>/ig;
								$VAR{allfields} .= qq~$VAR{marker} : $FORM{$_}<br>\n~;
							} else {
								$VAR{allfields} .= qq~$VAR{marker} : $FORM{$_}\n~;
							}
						}
					}
					$VAR{ar} =~ s/%%%allfields%%%/$VAR{allfields}/ig;
				}
				
				foreach(keys %FORM){
					$TMP{$_} = $FORM{$_};
					if($CONFIG{mail_format} == 1){
						$TMP{$_} =~ s/\n/<br>/ig;
					}
					$VAR{ar} =~ s/%%%$_%%%/$TMP{$_}/ig;
				}
				
				$VAR{armessage} = qq~$VAR{ar}~;
				$VAR{armessage} =~ s/%%%.*?%%%//ig;
				$VAR{armessage} =~ s/\r//ig;
				
				# ar subject
				foreach(keys %FORM){
					$TMP{$_} = $FORM{$_};
					$TMP{$_} =~ s/(\n|\r)/ /ig;
					$VAR{arsubject} =~ s/%%%$_%%%/$TMP{$_}/ig;
				}
				$VAR{arsubject} =~ s/%%%.*?%%%//ig;
				
				if($CONFIG{mailprogram} =~ /\/usr\//i || $CONFIG{mailprogram} =~ /\/sendmail/i){
					if(&send_mail($admemails[0], $admnames[0], $VAR{usremail}, $VAR{usrname}, $VAR{arsubject}, $VAR{armessage}, "", "", $VAR{arAtts})){
						$VAR{sent} = 1;
					}
				} else {
					if(&send_mail_NT($admemails[0], $admnames[0], $VAR{usremail}, $VAR{usrname}, $VAR{arsubject}, $VAR{armessage}, "", "", $VAR{arAtts})){
						$VAR{sent} = 1;
					}
				}
			}
		}
	}
	
	if($CONFIG{delfile} == 1){
		foreach(@todel){
			unlink($_);
		}
	}
	
	if($FORM{redirect} =~ /^http/i){
		# redirect user
		print qq~Location: $FORM{redirect}\n\n~;
	} elsif($CONFIG{redirect} =~ /^http/i){
		# redirect user
		print qq~Location: $CONFIG{redirect}\n\n~;
	} else {
		print "Content-type: text/html\n\n";
	
		@thankstemplate = getfile("$Data$S{S}$FORM{id}$S{S}thankstemplate.ftf");
		
		foreach(@thankstemplate){
			$VAR{thankstemplate} .= qq~$_\n~;
		}
		
		if($VAR{thankstemplate} =~ /%%%allfields%%%/i){
			$VAR{allfields} = qq~~;
			foreach(keys %FORM){
				unless($_ =~ /^FILE\d+$/ || $_ eq "id" || $_ eq "required"){
					$VAR{marker}   = $marker{$_};
					$VAR{marker} ||= $_;
					
					$VAR{allfields} .= qq~$VAR{marker} : $FORM{$_}\n~;
				}
			}
			$VAR{thankstemplate} =~ s/%%%allfields%%%/$VAR{allfields}/ig;
		}
		
		foreach(keys %FORM){
			$FORM{$_} =~ s/\n/<br>/ig;
			$VAR{thankstemplate} =~ s/%%%$_%%%/$FORM{$_}/ig;
		}
		$VAR{thankstemplate} =~ s/%%%.*?%%%//ig;
		
		print qq~$VAR{thankstemplate}~;
		
		unlink("$CONFIG{def_dir}$S{S}$val2\.prog") if $VAR{delStatFile};
		
		#foreach(keys %FORM){ print qq~F: $_ -> $FORM{$_}<br>~; }
		#foreach(%uploadSuccess){ print qq~Usxs: $_ -> $uploadSuccess{$_}<br>~; }
		#foreach(%uploadFailure){ print qq~Uflr: $_ -> $uploadFailure{$_}<br>~; }
	}
}

#################################################################### 

#################################################################### 

sub checkFormID {

	$FORM{id} ||= $val1;

	if($FORM{id} !~ /^\d+$/ && $cmnd ne "progress" && $cmnd ne "printjs" && $cmnd ne "CGIinfo"){
		$HSH{error} = qq~
		<table align="center" style="font-family:Arial; font-size: 13px;" width="600">
		<tr>
		<td><font color="#FF0000"><b>Your Form is missing some required HTML</b></font>
		<br>
		<br>
		Each AMP Form you create requires a unique identifier obtained from 
		the <a href="$ScriptURL?manage">Summary</a> page of the 
		<a href="$ScriptURL?manage">Admin Section</a> in order to function correctly. 
		<br>
		<br>
		<b>To acquire this Form's correct ID :</b>
		<br>
		<br>
		<ol>
		<li>Log in to, or load the <a href="$ScriptURL?manage">Summary</a> page 
		of the <a href="$ScriptURL?manage">Admin Section</a>.
		<br>
		<br>
		<li>Click any of the Form names you've listed in the <a href="$ScriptURL?manage=0">Name - Code \\\/</a> column.
		<br>
		<br>
		<li>Copy the generated code from the textbox positioned beneath the list of Forms in the main window.
		</ol>
		
		<b>Notes:</b>
		<ul type="square">
		<li>The generated <font color="#808080">&lt;form></font> tag includes the <font color="#808080">enctype</font> attribute and sets it's value to <font color="#808080">multipart/form-data</font>. If you change or remove this attribute, file uploads will fail.
		<br>
		<br>
		<font color="#808080"><i>&lt;form action="http://www.web.net/cgi/amp.cgi?process" <font color="#FF0000">enctype="multipart/form-data"</font> meth...</i></font>
		<br>
		<br>
		<li>The generated code also contains a hidden Form field named <font color="#808080">id</font> and bears the Form's unique identifier in it's value.
		<br>
		<br>
		<font color="#808080"><i>&lt;input type="Hidden" <font color="#FF0000">name="id"</font> value="1141398164"></i></font>
		<br>
		<br>
		<li>The same unique identifier is also planted into the <font color="#808080">&lt;script></font> tag conveys the id to the Progress Meter.
		<br>
		<br>
		<font color="#808080"><i>&lt;script src="http://www.web.com/amp/amp.cgi?printjs=<font color="#FF0000">1141398164</font>">&lt;/script></i></font>
		<br>
		<br>
		<li>If your Form's action includes <font color="#808080">www.</font> be sure the <font color="#808080">&lt;script></font> tag also includes <font color="#808080">www.</font>
		<br>
		<br>
		<font color="#808080"><i>&lt;form action="http://<font color="#FF0000">www.</font>web.net/cgi/amp.cgi?process" enct...</i></font>
		<br>
		<br>
		<li>You may optionally remove the remaining Form fields generated however the Customer Autoresponder will only be delivered if your Form contains a field named <font color="#808080">email</font> in lowercase.
		<br>
		<br>
		<font color="#808080"><i>&lt;input type="Text" name="<font color="#FF0000">email</font>"></i></font>
		<br>
		<br>
		<li>If you need to implement the <font color="#808080">onSubmit</font> event handler see our <a href="http://www.perlscriptsjavascripts.com/psjs_faqs/attachment_mailer_1.html" target="_blank">F.A.Q.</a> for guidance instructions.
		</ol></td>
		</tr>
		</table>
		~;
		
		&error($HSH{error}); 
		exit;
	}
}

#################################################################### 

#################################################################### 

sub CGIinfo {
	my ( %VAR, @folder );
	
	&PrintHead('CGIinfo');
	
	$VAR{uploadsOn} = $CGI::oDISABLE_UPLOADS > 0 ? qq~Disabled~ : qq~Enabled~;
	$VAR{dataLimit} = $CGI::POST_MAX > 0 ? $CGI::POST_MAX : qq~Unlimited~;
	$VAR{tempDir}   = $CGITempFile::TMPDIRECTORY =~ /\w+/ && -d $CGITempFile::TMPDIRECTORY ? 
					  $CGITempFile::TMPDIRECTORY : qq~Not set~;
	
	if($CGI::VERSION < 3.03){
		$VAR{oldversion} = qq~<br><br><font color="#ff0000"><b>Your server's version of the "CGI Perl Module" has been superseded. Contact your host and request to upgrade to "CGI.pm Version 3.04" or later.</b></font>~;
	} else {
		$VAR{oldversion} = qq~ &nbsp; <font color="#ff0000"><b>Progress Meter is supported</b></font>~;
	}
	
	print qq~
	<html>
	<head>
	<title>CGI Perl Module Version installed on this server</title>
	</head>
	<body>
	<table align="center" border="0" cellpadding="3" cellspacing="1" style="border: white outset 1px; font-size: 12px; font-family: Arial;" width="450">
	<tr bgcolor="$bg1">
	<td colspan="2"><b>CGI Perl Module Version installed on this server</b></td>
	</tr>
	</table>
	<br>
	
	<table align="center" border="0" cellpadding="3" cellspacing="1" style="border: white outset 1px; font-size: 12px; font-family: Arial;" width="450">
	<tr bgcolor="$bg1">
	<td colspan="2"><b>Your host has:</b></td>
	</tr>
	<tr bgcolor="$bg2">
	<td>File Uploads</td>
	<td>$VAR{uploadsOn}</td>
	</tr>
	<tr bgcolor="$bg2">
	<td>Form Data limited to </td>
	<td>$VAR{dataLimit} bytes</td>
	</tr>
	<tr bgcolor="$bg2">
	<td>CGI Version </td>
	<td>$CGI::VERSION $VAR{oldversion}</td>
	</tr>
	<tr bgcolor="$bg2">
	<td>CGI Revision </td>
	<td>$CGI::revision</td>
	</tr>
	<tr bgcolor="$bg2">
	<td>Temp directory set to </td>
	<td>$VAR{tempDir}</td>
	</tr>
	~;
	
	if($CGITempFile::TMPDIRECTORY =~ /\w+/ && -d $CGITempFile::TMPDIRECTORY){
		if(opendir(D, $CGITempFile::TMPDIRECTORY)){
			@folder = readdir(D);
			closedir D;
			
			$VAR{tSize} = 0;
			foreach(@folder){
				unless($_ eq '.' || $_ eq '..'){
					$VAR{tSize} += -s "$CGITempFile::TMPDIRECTORY$S{S}$_";
					#print qq~$CGITempFile::TMPDIRECTORY$S{S}$_<br>~;
				}
			}
			
			$VAR{tSize} = $VAR{tSize} > 0 ? $VAR{tSize} / 1024 / 1024 : 0;
			
			$VAR{tSize} = sprintf "%.2f", $VAR{tSize};
			1 while $VAR{tSize} =~ s/(.*\d)(\d\d\d)/$1,$2/g;
			$VAR{tSize} =~ s/\s+//ig;
			
			print qq~
			<tr bgcolor="$bg2">
			<td>Temp dir's current size is </td>
			<td>$VAR{tSize} MB</td>
			</tr>
			~;
		}
	}
	
	print qq~</table>
	<br>
	
	<table align="center" border="0" cellpadding="3" cellspacing="1" style="border: white outset 1px; font-size: 12px; font-family: Arial;" width="450">
	<tr bgcolor="$bg1">
	<td><b>Please note:</b></td>
	</tr>
	<tr bgcolor="$bg2">
	<td>The settings displayed on this page are read only. That is they cannot be set by the scripts you're running, only your host or a root user with SSH access to the server has the power to alter these settings. They take precedence over the script's settings. If you need these altered or increased, <b>contact your host</b> with your request. </td>
	</tr>
	</table>
	</body>
	</html>
	~;
	
	&PrintFoot('CGIinfo');
}

#################################################################### 

#################################################################### 

sub error { &PrintHead($FORM{id}); print $_[0]; &PrintFoot($FORM{id}); }

#################################################################### 

#################################################################### 
