<?php
// ------------------------------------------------------------------------- //
//                XOOPS - PHP Content Management System                      //
//                       <http://www.xoops.org/>                             //
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
// ------------------------------------------------------------------------- //
include("mainfile.php");

if ( $action == "showpopups" ) {
	xoops_header(false);
	// show javascript close button?
	$closebutton = 1;
	switch ( $type ) {
		case "smilies":
			echo "<script type=\"text/javascript\">\n"
			."<!--\n"
			."function doSmilie(addSmilie) {\n"
			."var currentMessage = window.opener.xoopsGetElementById(\"".$target."\").value;\n"
			."window.opener.xoopsGetElementById(\"".$target."\").value=currentMessage+addSmilie;\n"
			."return;\n"
			."}\n"
			."</script>\n\n";
			echo "</head><body>\n";
			echo "<div>";
			echo "<table width='100%'>
			<tr bgcolor='#ffffff'>
			<td colspan='3'><big><b>"._SMILIES."</b></big><br />"._CLICKASMILIE."</td>
			</tr>
			<tr class='bg2'>
			<td><b>"._CODE."</b></td>
			<td><b>"._EMOTION."</b></td>
			<td><b>"._IMAGE."</b></td>
			</tr>";
			if ( $getsmiles = $xoopsDB->query("SELECT * FROM ".$xoopsDB->prefix("smiles")) ) {
				$rcolor = 'bg1';
				$tcolor = 'fg2';
				while ( $smile = $xoopsDB->fetchArray($getsmiles) ) {
					echo "<tr class='$rcolor'><td>".$smile['code']."</td><td>".$smile['emotion']."</td><td><a href='javascript: justReturn()' onclick='doSmilie(\" ".$smile['code']." \");'><img src='".XOOPS_URL."/images/smilies/".$smile['smile_url']."' alt='' /></a></td></tr>";
					if ( $rcolor == 'bg1' ) {
						$rcolor = 'bg2';
						$tcolor = 'fg1';
					} else {
						$rcolor = 'bg1';
						$tcolor = 'fg2';
					}
     			}
			} else {
				echo "Could not retrieve data from the database.";
			}
			echo "</table></div>";
			break;
		case "avatars":
			?>
			<script language='javascript'>
			function myimage_onclick(counter){
				window.opener.xoopsGetElementById("user_avatar").options[counter].selected = true;
				showAvatar();
				window.opener.xoopsGetElementById("user_avatar").focus();
				window.close();
			}
			function showAvatar() {
				window.opener.xoopsGetElementById("avatar").src='<?php echo XOOPS_URL;?>/images/avatar/' + window.opener.xoopsGetElementById("user_avatar").options[window.opener.xoopsGetElementById("user_avatar").selectedIndex].value;
			}
			</script>
			</head><body>
			<div><h4><?php echo _AVAVATARS;?></h4>
			<form name='avatars'>
			<table width='100%'><tr>
			<?php
			include_once(XOOPS_ROOT_PATH."/class/xoopslists.php");
			if ( !isset($subdir) ) {
				$subdir = "";
			}
			$lists = new XoopsLists;
			$avatarslist = $lists->getAvatarsList($subdir);
			$cntavs = 0;
			$counter = isset($start) ? intval($start) : 0;
    			foreach ( $avatarslist as $avatar ) { 
				echo "<td><img src='images/avatar/".$avatar."' alt='".$avatar."' style='padding: 10px;vertical-align:top;'  /><br /><input name='myimage' type='button' value='"._SELECT."' onclick='myimage_onclick(".$counter.")' /></td>\n";
				$counter++;
				$cntavs++; 
				if ( $cntavs > 8 ) { 
					echo "</tr><tr>\n";
					$cntavs=0;
				}
			}
    			echo "</tr></table></form></div>\n"; 
	        	break;
        	case "friend":
			if ( !isset($op) || $op == "sendform" ) {
				if ( $xoopsUser ) {
					$yname = $xoopsUser->getVar("uname");
					$ymail = $xoopsUser->getVar("email");
					$fname = "";
					$fmail = "";
				} else {
					break;
				}
				printCheckForm();
				echo "</head><body>
				<table width='100%' border='0' cellspacing='1' cellpadding='0'><tr class='bg2'><td valign='top'>
				<table width='100%' border='0' cellspacing='1' cellpadding='8'><tr class='bg3'><td valign='top'>
				<div>
				<h4>"._RECOMMENDSITE."</h4>
				<form action='".XOOPS_URL."/misc.php' method='post' id='friend' onsubmit='return checkForm();'>
				<table width='100%'><tr><td>
    				<input type='hidden' name='op' value='sendsite' />
				<input type='hidden' name='action' value='showpopups' />
				<input type='hidden' name='type' value='friend' />\n";
    				echo _YOURNAMEC."</td><td><input type='text' name='yname' value='$yname' id='yname' /></td></tr>
    				<tr><td>"._YOUREMAILC."</td><td>".$ymail."</td></tr>
    				<tr><td>"._FRIENDNAMEC."</td><td><input type='text' name='fname' value='$fname' id='fname' /></td></tr>
				<tr><td>"._FRIENDEMAILC."</td><td><input type='text' name='fmail' value='$fmail' id='fmail' /></td></tr>
    				<tr><td align='center' colspan='2'><br /><br /><input type='submit' value='"._SEND."' />&nbsp;<input value='"._CLOSE."' type='button' onclick='javascript:window.close();' /></td></tr>
				</table></form></div>\n";
				$closebutton = 0;
    				echo "</td></tr></table></td></tr></table>\n";
			} elseif ( $op == "sendsite" ) {
				if ( $xoopsUser ) {
					$ymail = $xoopsUser->getVar("email");
				} else {
					break;
				}
				if ( !isset($yname) || $yname == "" || !isset($fname) || $fname == ""  || !$fmail ) {
				    redirect_header(XOOPS_URL."/misc.php?action=showpopups&amp;type=friend&amp;op=sendform",2,_NEEDINFO);
					exit();
				}
				if ( !checkEmail($fmail) ) {
					$errormessage = _INVALIDEMAIL1."<br />"._INVALIDEMAIL2."";
					redirect_header(XOOPS_URL."/misc.php?action=showpopups&amp;type=friend&amp;op=sendform",2,$errormessage);
					exit();
				}
				$xoopsMailer =& getMailer();
				$xoopsMailer->setTemplate("tellfriend.tpl");
				$xoopsMailer->assign("SITENAME", $xoopsConfig['sitename']);
				$xoopsMailer->assign("ADMINMAIL", $xoopsConfig['adminmail']);
				$xoopsMailer->assign("SITEURL", XOOPS_URL."/");
				$xoopsMailer->assign("YOUR_NAME", $yname);
				$xoopsMailer->assign("FRIEND_NAME", $fname);
				$xoopsMailer->setToEmails($fmail);
				$xoopsMailer->setFromEmail($xoopsUser->getVar("email"));
				$xoopsMailer->setFromName($yname);
				$xoopsMailer->setSubject(sprintf(_INTSITE,$xoopsConfig['sitename']));
				//OpenTable();
				if ( !$xoopsMailer->send() ) {
					echo $xoopsMailer->getErrors();
				} else {
					echo "<div><h4>"._REFERENCESENT."</h4></div>";
				}
				//CloseTable();
			}
			break;
		default:
			break;

	}
	if ($closebutton) {
		echo "<div><input value='"._CLOSE."' type='button' onclick='javascript:window.close();' /></div>\n";
	}
	xoops_footer();
}


function printCheckForm(){
?>
<script language='javascript'>
<!--
function checkForm()
{
	if ( xoopsGetElementById("yname").value == "" ){
			alert( "<?php echo _ENTERYNAME;?>" );
			xoopsGetElementById("yname").focus();
			return false;
	}
	else if ( xoopsGetElementById("fname").value == "" ){
			alert( "<?php echo _ENTERFNAME;?>" );
			xoopsGetElementById("fname").focus();
			return false;
	}
	else if ( xoopsGetElementById("fmail").value ==""){
			alert( "<?php echo _ENTERFMAIL;?>" );
			xoopsGetElementById("fmail").focus();
			return false;
	}
	else {
		return true;
	}
}
//--->

</script>
<?php
}
?>