<?php include("../controlPanel/includes/fSession.php")?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php include("../controlPanel/includes/fStyle.php")?>
<?php include("../controlPanel/includes/fSctipt.php")?>
<!--Date field script-->
<link rel="STYLESHEET" type="text/css" href="../tools/rich_calendar/rich_calendar.css">
<script language="JavaScript" type="text/javascript" src="../tools/rich_calendar/rich_calendar.js" ></script>
<script language="JavaScript" type="text/javascript" src="../tools/rich_calendar/rc_lang_en.js"></script>
<script language="JavaScript" type="text/javascript" src="../tools/rich_calendar/rc_lang_ru.js"></script>
<script language="javascript" src="../tools/rich_calendar/domready.js"></script>
<script language="JavaScript" type="text/javascript" src="../tools/ret/richtext.js" ></script>
<script type="text/javascript">
var cal_obj2 = null;

var format = '%Y-%m-%j';// '%j %M %Y %H:%i'\

// show calendar
function show_cal(el) {
	
	if (cal_obj2) return;

var text_field = document.getElementById("text_field");

	cal_obj2 = new RichCalendar();
	cal_obj2.start_week_day = 2;
	cal_obj2.show_time = false;
	cal_obj2.language = 'en';
	cal_obj2.user_onchange_handler = cal2_on_change;
	cal_obj2.user_onclose_handler = cal2_on_close;
	cal_obj2.user_onautoclose_handler = cal2_on_autoclose;

	cal_obj2.parse_date(text_field.value, format);

	cal_obj2.show_at_element(text_field, "adj_right-top");
	cal_obj2.change_skin('alt');

}

// user defined onchange handler
function cal2_on_change(cal, object_code) {
	if (object_code == 'day') {
		document.getElementById("text_field").value = cal.get_formatted_date(format);
		cal.hide();
		cal_obj2 = null;
	}
}

// user defined onclose handler (used in pop-up mode - when auto_close is true)
function cal2_on_close(cal) {
	if (window.confirm('Are you sure to close the calendar?')) {
		cal.hide();
		cal_obj2 = null;
	}
}

// user defined onautoclose handler
function cal2_on_autoclose(cal) {
	cal_obj2 = null;
}
</script>
<!--Image upload script-->
<script type="text/javascript">
	var fileTypes=["bmp","gif","png","jpg","jpeg"];
	var imageName = "centerimg";
	var globalPic;
	var defaultPic="../img/Pictures-256.png";
	var maxWidth=250;
	var maxHeight=250;
		
	function preview(what){
		var source=what.value;
		//var sourcee=what.files[0].getAsDataURL();
		var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
		
		for (var i=0; i<fileTypes.length; i++) if (fileTypes[i]==ext) break;
		
		globalPic = new Image();	
		if (i < fileTypes.length) {
			globalPic.src = source;
			//globalPic.src = sourcee; 
			//alert("تم تحميل الملف\n\n");
		}
		else {
		//globalPic.src=defaultPic;
		alert("لن تستطيع تحميل هذا الملف\n:رجاء قم بتحميل الصور ذات الإمتدادات التالية\n\n"+fileTypes.join(", "));
		}
	  setTimeout("applyChanges()",200);
	}
	function applyChanges(){
		//alert("بداية تابع التحميل\n\n");
		//var field=document.getElementById(outImage);
  		var imageField = document.images[imageName];
		if(imageField != null){
			var x=parseInt(globalPic.width);
			var y=parseInt(globalPic.height);
	  
			if (x>maxWidth) {
				y*=maxWidth/x;
				x=maxWidth;
			}
			if (y>maxHeight) {
				x*=maxHeight/y;
				y=maxHeight;
			}
			
			//field.style.display=( x < 1 || y < 1 )?"none":"";
			imageField.style.display=( x < 1 || y < 1 )?"none":"";
			//field.src = globalPic.src;
			//field.width=x;
			//field.height=y;
			imageField.srd = globalPic.src;	
			document.images[imageName].src = globalPic.src;
			//document.images[imageName].refresh();	
		}
	}
</script>

<link rel="stylesheet" href="/Kassarlogy/controlPanel/stylesheets/menu.css" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>المقالات</title>
</head>
<body >
<!--Rich text editor inialize script-->
<script language="JavaScript" type="text/javascript">
<!--
	initRTE("../tools/ret/images/", "../tools/ret/", "");	
//-->
</script>

<?php require_once("../controlPanel/includes/logo.php");?>

<table width="100%" border="1" align="right">
	<tr>
		<td width="85%" height="161" align="right" valign="top"  scope="col">	
        	<?php 
				if(isset($_GET['delete'])){
					if(isset($_GET['value'])){
						$value = $_GET['value'];
					}else {
						//echo "there is no value\n";
					}
					$result  = $_GET['delete'];

					$deletedIDS = "\n";
					if(isset($_GET['value'])){$deletedIDS = $_GET['value'];}
					
					if($result == -2){
						echo printDelete($deletedIDS);	
					} else if($result == -1){
						deleteCenter($deletedIDS);
					}
					else if($result == 0) {
						$deleteMsg = "\n";
						if(isset($_GET['msg'])){
							$deleteMsg = $_GET['msg'];
						}
						echo printFailureDelete($deleteMsg);
					}else if($result == 1){
							echo printSuccessfulDelete();
					 }
				}else {
					$displayRowsCount = 10;
					$pageNumber = 0;
					$country = "(SELECT pubt_value FROM publication_text WHERE pubt_pub_id = artic_pub_id AND pubt_lan_id = 1) AS artic_pub_id \n";
					$displayValue= array("artic_date"=>"varchar", "artic_pub_id"=>"FK_{$country}", "artic_link"=>"varchar", "artic_img_src"=>"varchar");
					$textDisplayValue= array("artict_artic_name");
					if(!isset($_GET['op']) && !isset($_GET['ins']) && !isset($_GET['upd'])){
						require_once("../controlPanel/includes/fNavigation.php"); 	
						if(count($_REQUEST) > 0){
							if(isset($_REQUEST['rowscount'])){
								$displayRowsCount = $_REQUEST['rowscount'];
							}
							if(isset($_REQUEST['pagenumber'])){
									
							}
						}
						if(isset($_GET['n']) && isset($_GET['c'])){
							$displayRowsCount = $_GET['c'];
							$pageNumber = $_GET['n'];
						}
						
						initializeNavigation($displayRowsCount, $pageNumber, "article", "article_text", "artic_id", 
											"artict_artic_id", 	"artict_lan_id", $displayValue, $textDisplayValue,"../controlPanel/article.php", 
											"<a href=\"#\" onclick=\"showDeleteMsg(frm1, 'article')\">", 
											"<a href=\"#\" onclick=\"modifySelectionError('article')\">");
					}else if(isset($_GET['op']) && !isset($_GET['ins']) && !isset($_GET['upd'])){
						$op = $_GET['op'];
						$message = "\n";
						if(isset($_GET['msg'])){$message = $_GET['msg'];}
						if($op == 1){
							echo printInsertForm($message );
						}else if($op == 2){
							$updatID = -1;
							if(isset($_GET['id'] )){
								$updatID = $_GET['id'];
							}
							echo printUpdatetForm($message , $updatID);
						}
						
					}else if(isset($_GET['ins']) && !isset($_GET['op']) && !isset($_GET['upd'])){
						$ins  = $_GET['ins'];
						if($ins == -1){
							insertCernter();
						}
					}else if(isset($_GET['upd']) && !isset($_GET['ins']) && !isset($_GET['op']) ){
						$upd = $_GET['upd'];
						updateCenter($upd);
					}
					$out = "</td>\n";
					$out .= "<td  style=\"background:#C7C7C7\" width=\"15%\" height=\"161\" align=\"right\" valign=\"top\"  scope=\"col\">\n";
					$out .= "<div id=\"rightmenu\">\n";
					$out .= "<p><strong><a href=\"../controlPanel/article.php\">المقالات</a></strong></p>\n";
					$out .= "<ul>\n";
					
					if(isset($_GET['op'])){
						$out .= "<li><a href=\"../controlPanel/article.php\">إلغاء الأمر</a></li>\n";
					}else if(!isset($_GET['op'])){
						$out .= "<li><a  href=\"../controlPanel/article.php?op=1\"  >إضافة</a></li>\n";
						$out .= "<li><a href=\"#\" onclick=\"modifySelectionError('article')\">تعديل</a></li>\n";
						$out .= "<li><a href=\"#\" onclick=\"showDeleteMsg(frm1, 'article')\">حذف</a></li>\n";
					}
					
					echo $out;
	
				}
			?>

                </ul>
	    	</div>
        </td>

	</tr>
</table>
<!--Rich text editor get value and check user entries values script-->
<script language="javascript" type="text/javascript">
<!--
function onEnterEmptyField(field){
	if(field.value == "حقل فارغ"){
		field.value = "\n";
	}
	
}
function onsubmitInsertForm(){
		updateRTEs();
		var result = true;
		var counter = 0;
		var isUploaded = false;
		<?php 
			$langState = "SELECT * FROM languages\n";
			$langResult = mysql_query($langState);
			while($lng = mysql_fetch_array($langResult)){
				echo "document.centerform.rettxt_".$lng['lan_id'].".value = document.centerform.rte_".$lng['lan_id'].".value;\n";
				echo "if((document.centerform.address_".$lng['lan_id'].".value == null || document.centerform.address_".$lng['lan_id'].".value == \"\") && \n";
				echo "(document.centerform.rettxt_".$lng['lan_id'].".value != null && document.centerform.rettxt_".$lng['lan_id'].".value != \"\")){\n";
				echo "result = false;\n";
				echo "document.centerform.address_".$lng['lan_id'].".value =\"حقل فارغ\"\n";
				echo "}\n";
				echo "if((document.centerform.rettxt_".$lng['lan_id'].".value == null || document.centerform.rettxt_".$lng['lan_id'].".value == \"\") && \n";
				echo "(document.centerform.address_".$lng['lan_id'].".value != null && document.centerform.address_".$lng['lan_id'].".value != \"\")){\n";
				echo "alert(\"حقل تفاصيل المقالة باللغة ".$lng['lan_name']." فارغ\");\n";
				echo "result = false;\n";
				echo "}\n";
				echo "if((document.centerform.address_".$lng['lan_id'].".value != null || document.centerform.address_".$lng['lan_id'].".value != \"\") && \n";
				echo "(document.centerform.rettxt_".$lng['lan_id'].".value != null && document.centerform.rettxt_".$lng['lan_id'].".value != \"\")){\n";
				echo "counter = counter + 1;\n";
				echo "}\n";
			}		
			

		?>
		
		if(document.centerform.text_field.value == null || document.centerform.text_field.value =="" ||
		   document.centerform.text_field.value == "حقل فارغ"){
		   document.centerform.text_field.value = "حقل فارغ";
			result = false;
		}
		if(counter == 0){
			alert("عليك إضافة مقالة واحدة على الأقل, حتى تستطيع استكمال عملية الحفظ.");
			result = false;
		}
		
		return result;
	}
//-->
</script>		

<?php 
	//printInsertForm Function.
	function printInsertForm($result){
		$out  = "<div  id=\"apDiv1\"> \n";
		$out .= "  <form name=\"centerform\"  method=\"post\" action=\"../controlPanel/article.php?ins=-1\" onsubmit=\"return onsubmitInsertForm()\">\n";
		$out .= "    <table align=\"center\" width=\"550\" border=\"0\" align=\"right\">\n";
		$out .= " 		<tr>\n";
		$out .= "          <th align=\"left\" >&nbsp;</th>\n";
		$out .= "          <th align=\"left\" >&nbsp;</th>\n";
		$out .= "          <th align=\"left\" >&nbsp;</th>\n";
		$out .= "          <th colspan=\"2\"align=\"right\"></th>\n";
	    $out .= "        </tr>\n";
		$out .= "        <tr>\n";
		$out .= "          <td height=\"31\" align=\"right\">&nbsp;</td>\n";
		$out .= "          <td align=\"left\" ></td>\n";
		$out .= "          <td align=\"right\"></td>\n";
		$out .= "          <td align=\"right\"><input style=\"width:150px\" type=\"submit\" name=\"save\" id=\"save\" value=\"إضافة\"></td>\n";
	    $out .= "        </tr>\n";
		$out .= " 		<tr>\n";
		$out .= "          <td height=\"31\" align=\"right\">&nbsp;</td>\n";
		$out .= "          <td align=\"left\" >&nbsp;</td>\n";
		$out .= "          <td align=\"center\">".$result."</td>\n";
		$out .= "          <td align=\"right\">&nbsp;</td>\n";
	    $out .= "        </tr>\n";
		//Publication field.
		$out .= "      <tr>\n";
		$out .= "		 <td align=\"center\" rowspan=\"4\" colspan=\"2\">\n";
		$out .= "			<img  name=\"centerimg\" id=\"centerimg\" src=\"../img/Pictures-256.png\"  border=\"1\" width=\"250\" height=\"250\">";
		$out .= "		 </td>";
		$out .= "        <td  width=\"205\" align=\"right\">\n";	
		$out .= "			<select style=\"width:255px\" id=\"publication\" name=\"publication\"  style=\"width:155px\" dir=\"rtl\">\n";
		$publicationState = "SELECT pub_id , (SELECT pubt_value FROM publication_text WHERE pubt_pub_id = pub_id AND pubt_lan_id = 1) AS TEXT FROM publication\n";
		$publicationResult = mysql_query($publicationState);
		while($publication = mysql_fetch_array($publicationResult)){
			$out .="			<option value=".$publication['pub_id'].">".$publication['TEXT']."</option>\n";
		}
		$out .= "			</select>\n";
		$out .= "        </td>\n";
		$out .= "        <td  colspan=\"2\" align=\"right\"><strong>:المنشور</strong></td>\n";
		$out .= "      </tr>\n";
		//Publication Link.
		$out .= "      <tr>\n";
		$out .= "         <td align=\"right\"></label>\n";
		$out .= "            <input style=\"width:250px\" type=\"text\" name=\"link\" id=\"link\" value=\"http://\" /></td>\n";
		$out .= "         <td colspan=\"2\" align=\"right\"><strong>:رابط المقالة</strong></td>\n";
		$out .= "      </tr>\n";
		//Publication image
		$out .= "      <tr>\n";
		$out .= "        <td align=\"right\">\n";
		$out .= "        <input type=\"file\" style=\"width:256px\"  name=\"image\" id=\"image\" onchange=\"preview(this)\" /></td>\n";
		$out .= "        <td colspan=\"2\" align=\"right\"><strong>:صورة</strong></td>\n";
		$out .= "      </tr>\n";
		//Publication date field.
		$out .= "      <tr>\n";
		$out .= "		  <td  width=\"205\" height=\"31\" align=\"right\">\n";	
		$out .= "		     <input type=\"text\" id=\"text_field\" name=\"text_field\" value=\"\" style=\"width:163px\" onfocus=\"onEnterEmptyField(this)\" readonly />\n";
		$out .= "			 <input type=\"button\" onclick=\"show_cal(this)\" value=\"...\" style=\"width:83px\" />\n";
		$out .= "         </td>\n";
		$out .= "         <td colspan=\"2\" align=\"right\"><strong>:تاريخ المقالة</strong></td>\n";
		$out .= "		</tr>\n";

		$out .= "      <tr>\n";
		$out .= "        <td colspan=\"2\">&nbsp;</td>\n";
		$out .= "        <td colspan=\"2\" align=\"right\">&nbsp;</td>\n";
		$out .= "      </tr>\n";
		$out .= "      <tr>\n";
		$out .= "        <td colspan=\"2\">&nbsp;</td>\n";
		$out .= "        <td colspan=\"2\" align=\"right\">&nbsp;</td>\n";
		$out .= "      </tr>\n";
		$out .= "      <tr>\n";
		$out .= "        <td colspan=\"2\" align=\"center\" valign=\"middle\"></td>\n";
		$out .= "        <td  colspan=\"2\" align=\"center\" valign=\"middle\" ></td>\n";
		$out .= "      </tr>\n";
		echo $out;
		$out = "\n";
		$langState = "SELECT lan_id, lan_name FROM languages";
		$langResult = mysql_query($langState);
		$colorCoutner = 1;
		while($lng = mysql_fetch_array($langResult)){
			$colorCoutnerResult = $colorCoutner / 2;
			$rowColor = "#89C0ED";
			if(!strstr($colorCoutnerResult, '.')){
						  $rowColor = "#FFF";
			}
			echo "      <tr >\n";
			echo "        <td colspan=\"2\" align=\"right\" valign=\"middle\" style=\"background:#89C0ED\">\n";
			echo "			<input type=\"text\" name=\"address_{$lng['lan_id']}\" id=\"address_{$lng['lan_id']}\" style=\"width:500px\" onfocus=\"onEnterEmptyField(this)\">\n";
			echo "		</td>\n";
			echo "        <td align=\"center\"  valign=\"middle\"  align=\"right\" style=\"background:#89C0ED\"><strong>عنوان المقالة</strong></td>\n";
			echo "		 <td rowspan=\"2\" align=\"center\" valign=\"middle\" style=\"background:#89C0ED\"><strong>{$lng['lan_name']}</strong></td>\n";//
			echo "      </tr>\n";	
			echo "      <tr style=\"background:#FFF\">\n";
			echo "        <td colspan=\"2\" align=\"center\" valign=\"middle\">\n";
			printEditor($lng['lan_id']);
			echo "			<textarea name=\"rettxt_{$lng['lan_id']}\" id=\"rettxt_{$lng['lan_id']}\" cols=\"85\" rows=\"1\" style=\"visibility:hidden\">im here</textarea>\n";
			echo "		</td>\n";
			echo "        <td align=\"center\"  valign=\"middle\"  align=\"right\"><strong>تفاصيل المقالة</strong></td>\n";
			echo "		 <td align=\"center\" valign=\"middle\" ></td>\n";
			echo "      </tr>\n";	
			$colorCoutner++;
		} 		
		
		echo "    </table>\n";
		echo "    <br /><br /><br />\n";
		echo "  </form>\n";
		echo "</div>\n";	
		return $out;
	}
	//insertCenter Function.
	function insertCernter(){
		$message = "";
		$target = "../controlPanel/imgs/pressarticle/"; 
		if (is_uploaded_file($HTTP_POST_FILES['image']['tmp_name'])) {
			copy($HTTP_POST_FILES['image']['tmp_name'], $target);
		} else {
		    $message = "Possible file upload attack. Filename: " . $HTTP_POST_FILES['image']['name'];}
		/*$target = $target.basename($HTTP_POST_FILES['image']['name']) ; 
		if(move_uploaded_file($HTTP_POST_FILES['image']['tmp_name'], $target)) { 
			$message .= "The file ". basename( $HTTP_POST_FILES['image']['name']). " has been uploaded"; 
		} else { 
			$message .= "Sorry, there was a problem uploading your file."; 
		}*/
		
	
		$date = $_REQUEST['text_field'];
		$link = $_REQUEST['link'];
		$img = $_REQUEST['uloadimage'];
		$publication = $_REQUEST['publication'];
		
		$insertStatement = "INSERT INTO article (artic_date, artic_pub_id, artic_link, artic_img_src) VALUES ('$date', $publication, '$link', '$img');\n";
		$insertResult = mysql_query($insertStatement);		
		
		//Check if Insert artic row finished successfully.
		if($insertResult){
			//get the id of the inserted artic row.
			$idState = "SELECT MAX(artic_id) AS ID FROM article";//WHERE artic_date ='$date' AND artic_pub_id=$publication AND artic_link = '$link' AND artic_img_src = '$img'
			$idResult= mysql_query($idState);
			//Check select id result if there is a value or not.
			if($idResult){
				$id = -1;
				while($arc = mysql_fetch_array($idResult)){
					$id = $arc['ID'];
				}	
				//In case $id biger than -1
				if($id > -1){
					//Ini select languages table statement.
					$langState = "SELECT lan_id, lan_name FROM languages\n";
					$langResult = mysql_query($langState);
					while($lng = mysql_fetch_array($langResult)){
							include_once("../controlPanel/includes/constants.php");
							$detials = ARTICLE_PATH.$id."_".$lng['lan_id'].".txt";
							$file = fopen($detials, 'w');
							if($file != false){
								$fileData =  $_REQUEST["rettxt_".$lng['lan_id']];
								$writeFile = fwrite($file, $fileData);
								if($writeFile != false){
									$address = $_REQUEST["address_".$lng['lan_id']];
									$centerText = "INSERT INTO article_text  (artict_artic_id, artict_lan_id, artict_artic_name, artict_file_name) ".
												"VALUES ($id, ".$lng['lan_id'].",'$address', '$detials')\n";
									$textResult = mysql_query($centerText);
								}else {
									if($_REQUEST["rettxt_".$lng['lan_id']] != null && $_REQUEST["rettxt_".$lng['lan_id']] != "" ){
										$message .= "can't write to file ".$detials."<br / >\n";
									}
								}
								fclose($file);	
							}else {$message .= "can't open file ".$detials."<br / >\n";}
					}
						
					if($message == null && $message == "")$message .= "تمت عملية الحفظ بنجاح";
				}else {$message .= "فشلت عملية الحفظ, Can't find artic_id value";}
			}else {$message .= "فشلت عملية الحفظ, <br /> Can't insert master row";}
			header("location: ../controlPanel/article.php?op=1&msg=".$message);
		} else {header("location: ../controlPanel/article.php?op=1&msg=".$message."<br />".mysql_error()."<br />");}
	}
	//printUpdatetForm Function.
	function printUpdatetForm($result, $id){
		$centerState = "SELECT * FROM centers WHERE cen_id = ".$id;
		$centerResult = mysql_query($centerState);
		$out = "\n";
		//$centerRowsNumber =mysql_num_rows($centerResult);
		if($centerResult){
			$country = -1;
			$city = -1;
			$phone1 = "\n";
			$phone2 = "\n";
			$fax = "\n";
			$mailBox = "\n";
			$email = "\n";
			$img = "\n";

			while($center = mysql_fetch_array($centerResult)){
				$country = $center['cen_nation_id'];
				$city = $center['cen_city_id'];
				$phone1 = $center['cen_phone_number1'];
				$phone2 = $center['cen_phone_number2'];
				$fax = $center['cen_fax'];
				$mailBox = $center['cen_mailbox'];
				$email = $center['cen_email'];
				$img = $center['cen_img'];
			}
			
			$out = "<div id=\"apDiv1\">\n";
			$out .= "  <form id=\"centerform\" name=\"centerform\"  method=\"post\" action=\"../controlPanel/article.php?upd=$id\">\n";
			$out .= "    <table width=\"550\" border=\"0\" align=\"right\">\n";
			$out .= " <tr>\n";
			$out .= "          <td height=\"31\" align=\"right\">&nbsp;</td>\n";
			$out .= "         <td align=\"left\" >&nbsp;</td>\n";
			$out .= "          <td align=\"right\"><input style=\"width:150px\" type=\"submit\" name=\"save\" id=\"save\" value=\"تعديل\"></td>\n";
			$out .= "          <td align=\"right\">&nbsp;</td>\n";
			$out .= "        </tr>\n";
			$out .= "        <tr>\n";
			$out .= "          <td height=\"31\" align=\"right\">&nbsp;</td>\n";
			$out .= "          <td align=\"left\" >"."</td>\n";
			$out .= "          <td align=\"right\">".$result."</td>\n";
			$out .= "          <td align=\"right\">&nbsp;</td>\n";
			$out .= "        </tr>\n";
			$out .= "      <tr>\n";
			$out .= "        <td width=\"205\" height=\"31\" align=\"right\">\n";
			//Prepare city and country array to assign then to sortCity function in javascript.
			?>
			<script type="text/javascript">
				var city = [];
				var country = [];
				<?php
					$counter = 0;
					$cityState = "SELECT city_id , (SELECT ctxt_value FROM cities_text WHERE ctxt_city_id = city_id AND ctxt_lan_id = 1) AS TEXT FROM cities \n";
					$cityResult = mysql_query($cityState);
					$cityScript = "var city = []; \n";
					while($row = mysql_fetch_array($cityResult)){
						print ("city[{$counter}] = new city(\"{$row['TEXT']} \", \"{$row['nation_id']}\");");
						$counter++;
					}	
				?>
			</script>
			<?php
			
			$out .= "			<select style=\"width:150px\" dir=\"rtl\"name=\"city\" id=\"city\">\n";
			
			$cityResult = mysql_query($cityState);
			
			while($row = mysql_fetch_array($cityResult)){
				if($row['city_id'] == $city){
					$out .= "<option value={$row['city_id']} selected>{$row['TEXT']}</option>\n";
				}else {$out .= "<option value={$row['city_id']}>{$row['TEXT']}</option>\n";}
				
			}
			$out .= "        	</select>\n";
			$out .= "        </td>\n";
			$out .= "        <td width=\"45\" align=\"left\" ><strong>:المدينة</strong></td>\n";
			$out .= "        <td width=\"150\" align=\"right\">\n";
			$out .= "        	<select dir=\"rtl\" style=\"width:150px\" name=\"country\"  id=\"country\" onchange=\"\">\n";
			
			$countryState = "SELECT nation_id , (SELECT nationtxt_value FROM nation_text WHERE nationtxt_nation_id = nation_id AND nationtxt_lan_id = 1) AS TEXT 
										FROM nationality \n";
			$countryResult = mysql_query($countryState);
			while($row = mysql_fetch_array($countryResult)){
				if($row['nation_id'] == $country){
					$out .= "<option value={$row['nation_id']} selected>{$row['TEXT']}</option>\n";
				} else {$out .= "<option value={$row['nation_id']}>{$row['TEXT']}</option>\n";}
				
			}
			$out .= "        	</select>\n";
			$out .= "        </td>\n";
			$out .= "        <td width=\"45\" align=\"right\"><strong>:الدولة</strong></td>\n";
			$out .= "      </tr>\n";
			$out .= "      <tr>\n";
			$out .= "         <td colspan=\"2\" align=\"right\"></label>\n";
			$out .= "        	<input style=\"width:150px\" type=\"text\" name=\"phone1\" id=\"phone1\" value=\"{$phone1}\" /></td>\n";
			$out .= "        <td colspan=\"2\" align=\"right\"><strong>:هاتف رقم 1</strong></td>\n";
			$out .= "      </tr>\n";
			$out .= "      <tr>\n";
			$out .= "        <td colspan=\"2\" align=\"right\"></label>\n";
			$out .= "        	<input style=\"width:150px\" type=\"text\" name=\"phone2\" id=\"phone2\" value=\"{$phone2}\"/></td>\n";
			$out .= "        <td colspan=\"2\" align=\"right\"><strong>:هاتف رقم 2</strong></td>\n";
			$out .= "      </tr>\n";
			$out .= "      <tr>\n";
			$out .= "        <td colspan=\"2\" align=\"right\"></label>\n";
			$out .= "        <input style=\"width:150px\" type=\"text\" name=\"fax\" id=\"fax\" value=\"{$fax}\"/></td>\n";
			$out .= "        <td colspan=\"2\" align=\"right\"><strong>:فاكس</strong></td>\n";
			$out .= "      </tr>\n";
			$out .= "      <tr>\n";
			$out .= "        <td colspan=\"2\" align=\"right\"></label>\n";
			$out .= "        <input style=\"width:150px\" type=\"text\" name=\"mailbox\" id=\"mailbox\" value=\"{$mailBox}\" /></td>\n";
			$out .= "        <td colspan=\"2\" align=\"right\"><strong>:صندوق بريد</strong></td>\n";
			$out .= "      </tr>\n";
			$out .= "      <tr>\n";
			$out .= "        <td colspan=\"2\" align=\"right\"><label for=\"email\"></label>\n";
			$out .= "        <input style=\"width:250px\" type=\"text\" name=\"email\" id=\"email\" value=\"{$email}\" /></td>\n";
			$out .= "        <td colspan=\"2\" align=\"right\"><strong>:بريد إلكتروني</strong></td>\n";
			$out .= "      </tr>\n";
			$out .= "      <tr>\n";
			$out .= "        <td colspan=\"2\" align=\"right\">\n";
			$out .= "        <input style=\"width:256px\" type=\"file\" name=\"uploaded\" value=\"{$img}\" /></td>\n";//onchange=\"preview(this)\" id=\"picField\"
			$out .= "        <td colspan=\"2\" align=\"right\"><strong>صورة</strong></td>\n";
			$out .= "      </tr>\n";
			$out .= "      <tr>\n";
			$out .= "        <td colspan=\"2\">&nbsp;</td>\n";
			$out .= "        <td colspan=\"2\" align=\"right\">&nbsp;</td>\n";
			$out .= "      </tr>\n";
			$out .= "      <tr>\n";
			$out .= "        <td colspan=\"2\">&nbsp;</td>\n";
			$out .= "        <td colspan=\"2\" align=\"right\">&nbsp;</td>\n";
			$out .= "      </tr>\n";
			$out .= "      <tr>\n";
			$out .= "        <td colspan=\"2\" align=\"center\" valign=\"middle\"><strong>العنوان</strong></td>\n";
			$out .= "        <td  colspan=\"2\" align=\"center\" valign=\"middle\" ><strong>اللغة</strong></td>\n";
			$out .= "      </tr>\n";
			
			


			$langState = "SELECT lan_id, lan_name FROM languages\n";
			$langResult = mysql_query($langState);
			$colorCoutner = 1;
			while($lng = mysql_fetch_array($langResult)){
				$colorCoutnerResult = $colorCoutner / 2;
				$rowColor = "#89C0ED\n";
				if(!strstr($colorCoutnerResult, '.')){
							  $rowColor = "#FFF\n";
				}
				$out .= "      <tr style=\"background:{$rowColor}\">\n";
				$out .= "        <td colspan=\"2\" align=\"center\" valign=\"middle\">\n";
				$out .= "			<textarea name=\"address_{$lng['lan_id']}\" id=\"address_{$lng['lan_id']}\" cols=\"35\" rows=\"5\">\n";
				$centerTextState = "SELECT * FROM centers_text WHERE centxt_cen_id=".$id." AND centxt_lan_id=".$lng['lan_id'];
				$centerTextResult = mysql_query($centerTextState);
				if( mysql_num_rows($centerTextResult) == 1){
					while($centerText = mysql_fetch_array($centerTextResult)){
						$out .= $centerText['centxt_value'];
					}
				}
				$out .= "</textarea>\n";
				$out .= "		</td>\n";
				$out .= "        <td align=\"center\" valign=\"middle\" colspan=\"2\" align=\"right\">{$lng['lan_name']}</td>\n";
				$out .= "      </tr>\n";	
				$colorCoutner++;
			} 		
			$out .= "    </table>\n";
			$out .= "    <br /><br /><br />\n";
			$out .= "  </form>\n";
			$out .= "</div>\n";	
			$out .= "<div align=\"center\">صورة المركز<br />\n";
			require_once("../controlPanel/includes/constants.php");
			$out .= "<img  name=\"centerimg\" id=\"centerimg\" src=\"".CNT_IMG."/{$img}\" border=\"1\" width=\"250\" height=\"250\">\n";
			$out .= " </div>\n";				
		}else {$out = "No Rows Founded.<br />id=".$id;}

		return $out;
	}
	//updateCenter Function.
	function updateCenter($id){
		$country = $_REQUEST['country'];
		$city = $_REQUEST['city'];
		$phone1 = $_REQUEST['phone1'];
		$phone2 = $_REQUEST['phone1'];
		$fax = $_REQUEST['fax'];
		$mailbox = $_REQUEST['mailbox'];
		$email = $_REQUEST['email'];
		$img = $_REQUEST['uploaded'];
		
		$updateStatement = "UPDATE centers SET cen_nation_id = $country , cen_city_id = $city, cen_phone_number1 = '$phone1', ".
						   " cen_phone_number2 = '$phone2', cen_fax = '$fax', cen_mailbox = '$mailbox', cen_email = '$email', cen_img = '$img' WHERE  ".
						   " cen_id=$id\n";
		$updateResult = mysql_query($updateStatement);
		
		$message = "\n";
		
		if($updateResult){
			$langState = "SELECT lan_id FROM languages\n";
			$langResult = mysql_query($langState);
			$textCounter  = 0 ;
			while($lng = mysql_fetch_array($langResult)){
				$value =  $_REQUEST["address_".$lng['lan_id']];
				$centerText = "UPDATE centers_text SET centxt_value='$value' WHERE centxt_cen_id=$id AND centxt_lan_id=".$lng['lan_id'];
				$textResult = mysql_query($centerText);
				if(!$textResult)$textResult++;

			}
			
			if($textCounter > 0) $message .= "تمت علمية التحديث بنجاح, لكن هناك عملية أو أكثر في التفاصيل لم تنجح.\n";
			else {$message .= "تمت عملية التعديل بنجاح.\n";}
			
			header("location:../controlPanel/article.php?op=2&id=".$id."&msg=".$message);
		}else {header("location:../controlPanel/article.php?op=2&id=$id&msg=".$message);}

				
	}
	//printDelete Function.
	function printDelete($ids){		
		$out =  "<table border=\"0\" width=\"600\" align=\"right\">\n";
		$out .= "<tr height=\"55px\">\n";	  
		$out .=  "<td width=\"70%\">\n";
		$out .=  "	<from id=\"deleteform\"> \n";
		$out .=  "		<input onclick=\"deleteCanceled('article')\" type=\"button\" id=\"cancel\" value=\"إلغاء الأمر\">\n";
		$out .=  "		<input onclick=\"deleteExec('article', '".$ids."')\" type=\"button\" id=\"ok\" value=\"موافق\">\n";
		$out .=  "	</form>\n";
		$out .=  "</td>\n";	  
		$out .=  "<td width=\"70%\"></td>\n";
		$out .=  "<td width=\"30%\">\n";
		$out .=  "</td>\n";
		$out .=  "<td width=\"30%\">\n";
		$out .=  "</td>\n";
		$out .=  "</td>\n";
		
		$out .= "</tr>\n";	  
		$out .= "<tr>\n";
		$out .= "<td width=\"70%\">\n";
		$out .= "</td>\n";	  
		$out .= "<td width=\"30%\">\n";
		$out .= "</td>\n";
		$out .= "<td width=\"30%\">\n";
		$out .= "</td>\n";
		$out .= "<h4 style=\"color:red\"><br>إذا أردت الإستمرار في عملية الحذف اضغط على الزر موافق, وإذا أردت التراجع إضغط على زر إلغاء الأمر</h4>\n";
		$out .= "</tr>\n";	  
		$out .= "</table>\n";
		return $out;
	}
	//printSuccessfulDelete Function.
	function printSuccessfulDelete(){
		$out  =  "<table>\n";
		$out .= "<tr height=\"55px\">\n";	  
		$out .= "<td width=\"70%\">\n";
		$out .= "	<from action=\"article.php\" id=\"deleteform\"> \n";
		$out .= "		<input onclick=\"deleteCanceled('article')\" type=\"button\" id=\"backtopage\" value=\"عودة\">\n";
		$out .= "	</form>\n";
		$out .= "</td>\n";	  
		$out .= "<td width=\"70%\"></td>\n";
		$out .= "<td width=\"30%\">\n";
		$out .= "</td>\n";
		$out .= "<td width=\"30%\">\n";
		$out .= "</td>\n";
		$out .= "<h4 style=\"color:red\"><br>.تمت عملية الحذف بنجاح</h4>\n";
		$out .= "</td>\n";
		$out .= "</table>\n";	
		return $out;	
	}
	//printFailureDelete Function.
	function printFailureDelete($msg){
		$out =  "<table>\n";
		$out .= "<tr height=\"55px\">\n";	  
		$out .= "<td width=\"70%\">\n";
		$out .= "	<from action=\"article.php\" id=\"deleteform\"> \n";
		$out .= "		<input onclick=\"deleteCanceled('article')\" type=\"button\" id=\"backtopage\" value=\"عودة\">\n";
		$out .= "	</form>\n";
		$out .= "</td>\n";	  
		$out .= "<td width=\"70%\"></td>\n";
		$out .= "<td width=\"30%\">\n";
		$out .= "</td>\n";
		$out .= "<td width=\"30%\">\n";
		$out .= "</td>\n";
		$out .= "<h4 style=\"color:red\"><br>.فشلت عملية الحذف<br />{$msg}</h4>\n";
		$out .= "</td>\n";
		$out .= "</table>\n";
		return $out;
	}	
	//deleteCenter function.
	function deleteCenter($ids){
		$ids = rtrim($ids);
		$ids = ltrim($ids);
		$splitedIDS = split("_", $ids);
		foreach($splitedIDS as $myid){
			if($myid != null && $myid != "" && $myid != " "){
				$array .= $myid."<br />\n";
				$result  = true;
				$deleteTextState = "DELETE FROM centers_text WHERE centxt_cen_id =".$myid;
				$deleteTextResult = mysql_query($deleteTextState);
				if($deleteTextResult){
					$deletCenterState = "DELETE FROM centers WHERE cen_id=".$myid;
					$deleteCenterResult = mysql_query($deletCenterState);
					if(!$deleteCenterResult){
						$result = false;
					}
				}else {
					$result = false;
				}				
			}
		}
		if($result){
			header("location:../controlPanel/article.php?delete=1");
		}else if(!$result){
			header("location:../controlPanel/article.php?delete=0&msg=".mysql_errno());//
		}
		
	}
	//printEditor Function
	function printEditor($langID){
		$out = "\n";
		echo "<script language=\"JavaScript\" type=\"text/javascript\">\n";
		echo "<!--\n";
		echo "writeRichText('rte_{$langID}', '', 700, 300, true, false);\n";
		//echo "writeRichText('rte_{$langID}', 'preloaded <b>text</b>', 560, 100, true, false);\n";
		echo"document.writeln('<br><br>');\n";
		echo "//-->\n";
		echo "</script>\n";		
		return $out;
	}
?>
