<?
	include("connect.php") ;
	include_once("admin.config.inc.php");
	include("security.php");
	$type1 = "1";
	$type3 = "3";
	include("pagepermission.php");
	include("functions.php");
	include("imgsize.php");
//	include("common.inc.php");

$ex="";
$msg="";

/***************************
Function hesk_input()
***************************/

		if($_POST['addadv'])
		{
			$adv_name = hesk_input($_POST["adv_name"]);
			$adv_type=addslashes($_POST["adv_type"]);
			$pic_desc=addslashes($_POST["pic_desc"]);
			$adv_status=$_POST["adv_status"];
			$disp_order=$_POST["disp_order"];
				
			$q=mysql_query("SELECT * FROM advert WHERE adv_name='".addslashes($adv_name)."'");
			
			$rows=mysql_fetch_array($q);
			if($rows)
			{
				$ex=1;
				$msg='该广告已经存在!';
			}
			
			if($ex!=1)
			{	
				
				$sql = "INSERT INTO advert (adv_name,adv_type, pic_desc,pic_link,adv_status,disp_order) VALUES ('".hesk_input($_POST["adv_name"])."','$adv_type','".hesk_input($_POST["pic_desc"])."','".hesk_input($_POST["pic_link"])."','".$adv_status."','$disp_order')";				
				$q = mysql_query($sql) or die ("insert error".mysql_error());
				
				$pid = mysql_insert_id();
				if($_FILES["pic_url"]["name"]!=""){
					$time = time();		
					$logo = $time."_".$_FILES["pic_url"]["name"];
					$logo_temp = $_FILES["pic_url"]["tmp_name"];
					advertimage($logo,$pid,$logo_temp);						
			  }
				?>
				<script language="javascript">
					window.location.href="message.php?msg=81";
				</script>				
				<?
				exit;
			}
		}
		
		//*** SECOND CATEGORY UPDATE ****//

		elseif($_POST['editadv'])
		{
			$adv_name=hesk_input($_POST["adv_name"]);
			$pic_desc=hesk_input($_POST["pic_desc"]);
			$adv_status=$_POST["adv_status"];
			
			if(isset($_POST['edit']))
			{
				$q=mysql_query("SELECT * FROM advert WHERE adv_name='".addslashes($_POST["adv_name"])."' and id<>'".$_POST['edit']."'");
				
				$rows=mysql_fetch_array($q);
				if($rows)
				{
					$ex=1;
					$msg="该广告已经存在 !";					
				}
			}
			
			if ($ex!=1)
			{	
				if(isset($_POST['edit']))
				{
						mysql_query("UPDATE advert SET adv_name='".str_replace("<","&lt;",hesk_input($_POST["adv_name"]))."',adv_type='".hesk_input($_POST["adv_type"])."',pic_desc='".hesk_input($_POST["pic_desc"])."',pic_link='".hesk_input($_POST["pic_link"])."',disp_order='".$_POST["disp_order"]."',adv_status='".$adv_status."' WHERE id='".$_POST['edit']."'") or die (mysql_error());
					
				$pid = $_POST['edit'];
				if($_FILES["pic_url"]["name"]!=""){
					$time = time();		
					$logo = $time."_".$_FILES["pic_url"]["name"];
					$logo_temp = $_FILES["pic_url"]["tmp_name"];
					advertimage($logo,$pid,$logo_temp);						
			  }

		?>
				<script language="javascript">
					window.location.href="message.php?msg=82";
				</script>				
				<?
				exit;
			}
			
			} //end if $ex
		}
		
		else
		{
			//*** THIRD CATEGORY DELETE ****//			
				if(isset($_GET['delete']))
				{
					$q = mysql_query("SELECT * FROM advert WHERE id='".$_GET['delete']."' and id<>0") or die (mysql_error());
					$row = mysql_fetch_row($q);
					$totalrow = mysql_affected_rows();
					if($totalrow>0)
					{
							$qryd = "delete from advert where id=".$_GET['delete'];
							mysql_query($qryd) or die(mysql_error());
							header("location: message.php?msg=83");
							exit;
					}
				}
	
		// EXIST FETCH THE VALUE FOR GET EDIT TIME FROM MANAGE CATEGORY //
		if($_GET["adv_edit"] || $_GET["adv_delete"])
		{
			
			if(isset($_GET["adv_edit"]))
			{
				$id=$_GET["adv_edit"];
			}
			if(isset($_GET["adv_delete"]))
			{
				$id=$_GET["adv_delete"];
			}
			
			$q = mysql_query("SELECT * FROM advert WHERE id='".$id."'") or die (mysql_error());
			$row = mysql_fetch_array($q);
			if (!$row) //can't find advert....
			{
				echo "<center><font color=red>ERROR CAN NOT FIND REQUIRED PAGE</font>\n<br><br>\n";
				exit;
			}
		}
}
?>
<HTML>
<HEAD>
<TITLE></TITLE>
<META http-equiv=Content-Type content="text/html; charset=<?=$lng_characset;?>">
<LINK href="main.css" type="text/css" rel="stylesheet">
<script type="text/javascript">

function delconfirm(loc)
{
	if(confirm("Are you sure do you want to delete this?"))
	{
		window.location.href=loc;
	}
	return false;
}

function checkform(f1)
{
	if(f1.adv_status.value=="")
	{
		alert("请选择广告状态.");
		f1.adv_status.focus();
		return false;
	}
}

function popImage(img){
  foto1= new Image();
  foto1.src=(img);
  Controlla(img);
}
function Controlla(img){
  if((foto1.width!=0)&&(foto1.height!=0)){
    viewFoto(img);
  }
  else{
    funzione="Controlla('"+img+"')";
    intervallo=setTimeout(funzione,20);
  }
}
function viewFoto(img){
  largh=foto1.width+20;
  altez=foto1.height+20;
  PositionX = 100;
  PositionY = 350;
  stringa="width="+largh+",height="+altez+",top="+PositionX+",left="+PositionY;
  finestra=window.open(img,"",stringa);
}
</script>
</HEAD>
<BODY>      
<form action='' method='POST' enctype="multipart/form-data" onSubmit="return checkform(this);">
<table width="100%" cellpadding="0" cellSpacing="10">
	<tr>
		<td class="H1"><? if($_GET['adv_edit']!="") { ?> 编辑广告<? } else { if($_GET['adv_delete']!=""){ ?> 确认删除此广告 <? }else { ?> 增加广告 <? } } ?></td>
	</tr>
	<TR>
		<TD background="images/vdots.gif"><IMG height=1 src="images/spacer.gif" width=1 border=0></TD>
	</TR>
	
	<? if($msg!="") {?>
	<tR>
		<td align="center"><font color="#FF0000"><?=$msg;?></font></td>
	</tR>
	<? } ?>
	<tr>
		<td class="a" align="right" colspan="2">* 必填</td>
	</tr>
	
	<tr>
		<td>
			<table cellpadding="2" cellspacing="2" width="100%">

			<tr>
				<TD class="f-c" align="right"><font class="a">*</font>广告名称 :</TD>
				
				<TD><input type="text" name="adv_name" size="32"  value="<? echo $row["adv_name"]; ?>"> </TD>
			</tr>
			<tr>
				<td class="f-c" align="right"><font class="a">*</font>广告类型 :</td>
				<td>				
					<select name="adv_type" >				
						<option value="0" <?php if($row["adv_type"]==0){ echo " selected";} ?> >首页中部(701*169像素)</option>
						<option value="1" <?php if($row["adv_type"]==1){ echo " selected";} ?> >首页右侧一(225*148像素)</option>
						<option value="2" <?php if($row["adv_type"]==2){ echo " selected";} ?> >首页右侧二(113*140像素)</option>
						<option value="3" <?php if($row["adv_type"]==3){ echo " selected";} ?> >首页右侧三(227*75像素)</option>
					</select>				
				</td>
			</tr>			
			<tr>
				<td class="f-c" align="right"><font class="a">*</font>广告图片 :</td>
				<td><? if($row["pic_url"]!=""){?>
					<img src="../uploads/advert/<?=$row["pic_url"];?>" height=100><?}?><input name="pic_url" type="file" class="solidinput"  value="<? echo $row["pic_url"];?>" size="35"></td>
			</tr>								
			<tr>
				<td class="f-c" align="right"><font class="a">*</font>广告描述 :</td>
				<td><textarea name="pic_desc" rows="3" cols="30"><?php echo $row["pic_desc"] ; ?></textarea></td>
			</tr>			
			<tr>
				<td class="f-c" align="right"><font class="a">*</font>广告指向链接 :</td>
				<td><input type="text" name="pic_link" size="32"  value="<? echo $row["pic_link"]; ?>"></td>
			</tr>			
			<tr>
				<TD class="f-c" align="right"><font class="a">*</font>状态 :</TD>
				<td>				
					<select name="adv_status" >				
						<option value="1" <?php if($row["adv_status"]==1){ echo " selected";} ?> selected="selected">可用</option>
						<option value="0" <?php if($row["adv_status"]!="" and $row["adv_status"]==0){ echo " selected";} ?>>不可用</option>
					</select>				
				</td>
			</tr>	
			<tr>
				<td class="f-c" align="right"><font class="a">*</font>显示顺序 :</td>
				<td><input type="text" name="disp_order" size="32"  value="<? echo $row["disp_order"]; ?>"> </td>
			</tr>			
			<tr>
				<td>&nbsp;</td><td>&nbsp;</td>
			</tr>
			  
			<tr>
					<td>&nbsp;</td>
					<td>
					<?php
					if($_GET['adv_delete']!="" and $id!="")
					{ 
					
					?>
					<input type='button' name='<? if($_GET['adv_delete']!="" or $id!=""){?>deleteadv<? }else {?>addadv<? } ?>' value='<? if($_GET['adv_delete']!="" or $id!="") {?> 删除广告 <? }else{ ?> 新增广告 <? } ?>' class="bttn" onClick="delconfirm('addadv.php?delete=<?=$id?>')">
					<?php 
					}
					else
					{ 
					
					?>
					<input type='submit' name='<? if($_GET['adv_edit']!="" or $id!=""){?>editadv<? }else {?>addadv<? } ?>' value='<? if($_GET['adv_edit']!="" or $id!="") {?> 编辑广告 <? }else{ ?> 新增广告 <? } ?>' class="bttn">
					<?php 
					}
					?>
					</td>
			</tr>
			<tr>
				<td colspan="2">&nbsp;</td>
			</tr>
			</table>
			
<? if($_GET['adv_edit']!="") {?>
<input type="hidden" name="edit" value="<?=$_GET['adv_edit']?>">

<? } ?>

		</td>
	</tr>
</table>
</form>
<br><br>
</BODY>
</HEAD>
</HTML>
