<?
session_start();
include("config.php");
include("inquiry.php");
//login box


function loginbox( &$login ) {
if(!isset($_COOKIE[traidusername])  ){
$login=
"
<div align='center'>
<form name='traid' action='login.html' method='post'  onSubmit='return traidntvalid(this)' >
<div id='ajax'>
<div>إسم المستخدم</div>
<input type='text' name='username' id='username' size='15'>
<div>كلمة المرور</div>
<input type='password' name='password' id='password' size='15'>
<div></div>
<input type='submit' value='دخول'>

</div>
</form>
   <script language='JavaScript' type='text/javascript'>
//التحقق من الفورم
  var frmvalidator  = new Validator('traid');
  frmvalidator.addValidation('username','req','من فضلك ادخل اسم المستخدم');

  frmvalidator.addValidation('password','req','من فضلك ادخل  كلمة المرور');
</script>
<a href='register.html'>تسجيل عضوية جديدة</a>
<br>
<a href='lostpassword.html'>فقدان كلمة المرور</a>
</div>
";
}else{
$username = $_COOKIE['traidusername'];
$utfuser = iconv('windows-1256','utf-8',$username);
$lastv = $_COOKIE['traidlastvisit'];
$login = "
<div id='ajax' align='center'>
<div>مرحبا بك يا $username</div>
آخر زيارة لك كانت بتاريخ<br>
$lastv
<br><a href='usercp.html'>الملف الشخصي </a>
<br>
<a href='fav.html'>المفضلة</a>
<br>
<a href='logout.html'>تسجيل الخروج</a>
</div>

";
}
}

function addcom(&$addcom) {
if(isset($_COOKIE[traidusername])  ){
$addcom =
"<form action='addcom.html' method='post' name=form1>
		<table border='0' width='472' cellspacing='0' cellpadding='0'>
			<tr>
				<td width='89' bgcolor='#DDC98C' class='font'>
				<p align='center'><span lang='ar-eg'>نص التعليق :</span></td>
				<td width='383' class='font' bgcolor='#F1E6BF'>

<textarea name='C1' rows='9' cols='55' class='input'></textarea></td>
			</tr>
			<tr>
				<td width='89' bgcolor='#DDC98C' class='font'>
				<p align='center'>صورة التحقق :</td>
				<td width='383' class='font' bgcolor='#F1E6BF'>&nbsp;<input type='text' name='vcode'><img border='0' src='captcha.php' ></td>
			</tr>
			<tr>
				<td width='89' bgcolor='#DDC98C' class='font'>&nbsp;</td>
				<td width='383' class='font' bgcolor='#F1E6BF'>&nbsp;
				<div align='center'>
				<input type='submit' value='اضافه'>
				</div>
				</td>
			</tr>

	</TD>
	<input name='poeimid' type='hidden' value='$_GET[poemid]'>
    </TR>
        </form>
</table>

";
}else{
$addcom = "
		<table border='0' width='472' cellspacing='0' cellpadding='0'>
			<tr>
				<td  class='font' >

<p align='center'>عفوا التعليقات للأعضاء فقط</td><br>
			</tr>

	</TD>
    </TR>
     </table>

";


}
}

//Export poem To Wordfile
function ExportToWord($thispoem,$poemname,$sitename,$sitelink)
{
$rand = rand(1,100);
$type = "Content-Disposition: attachment; filename=$poemname.doc";
 global $cCharset;
 header("Content-type: application/msword");
 echo "<html dir=\"rtl\"><center>";
 echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$cCharset."\">";
 header($poemname);
echo  $thispoem ;
 echo"<br><div align='center'>";
  echo "<table border=0><tr><td><b>";
echo  "جميع الحقوق محفوظة لــ"."<br>".$sitename ;
echo"</td></tr><tr><td><br>$sitelink";
 echo "</td></tr></table></b>";
 echo"</div></center>";
}
 //Export to Html File
function ExportToHtml($thispoem,$poemname,$sitename,$sitelink)
{
 global $cCharset;
 $type = "Content-Disposition: attachment; filename=$poemname.html";
 header("Content-type: text/plain");
 echo "<html dir=\"rtl\"><center>";
 echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$cCharset."\">";
 header($type);
echo  $thispoem ;
 echo"<br><div align='center'>";
  echo "<table border=0><tr><td><b>";
echo  "جميع الحقوق محفوظة لــ"."<br>".$sitename ;
echo"</td></tr><tr><td><br>$sitelink";
 echo "</td></tr></table></b>";
 echo"</div></center>";
}

//rate func


function getRating($id){

	$total = 0;
	$rows = 0;

	$sel = mysql_query("SELECT rating_num FROM ratings WHERE rating_id = '$id'");
	if(mysql_num_rows($sel) > 0){

		while($data = mysql_fetch_assoc($sel)){

			$total = $total + $data['rating_num'];
			$rows++;
		}

		$perc = ($total/$rows) * 20;

		//$newPerc = round($perc/5)*5;
		//return $newPerc.'%';

		$newPerc = round($perc,2);
		return $newPerc.'%';

	} else {

		return '0%';

	}
}

function outOfFive($id){

	$total = 0;
	$rows = 0;

	$sel = mysql_query("SELECT rating_num FROM ratings WHERE rating_id = '$id'");
	if(mysql_num_rows($sel) > 0){

		while($data = mysql_fetch_assoc($sel)){

			$total = $total + $data['rating_num'];
			$rows++;
		}

		$perc = ($total/$rows);

		return round($perc,2);
		//return round(($perc*2), 0)/2; // 3.5

	} else {

		return '0';

	}


}

function getVotes($id){

	$sel = mysql_query("SELECT rating_num FROM ratings WHERE rating_id = '$id'");
	$rows = mysql_num_rows($sel);
	if($rows == 0){
		$votes = '0 اجمالي ';
	}
	else if($rows == 1){
		$votes = '1 اجمالي ';
	} else {
		$votes = $rows.'اجمالي';
	}
	return $votes;

}

function pullRating($id,$show5 = false, $showPerc = false, $showVotes = false, $static = NULL){

	// Check if they have already voted...
	$text = '';

	$sel = mysql_query("SELECT id FROM ratings WHERE IP = '".$_SERVER['REMOTE_ADDR']."' AND rating_id = '$id'");
	if(mysql_num_rows($sel) > 0 || $static == 'novote' || isset($_COOKIE['has_voted_'.$id])){



		if($show5 || $showPerc || $showVotes){

			$text .= '<div class="rated_text">';

		}

			if($show5){
				$text .= 'النسبة <span id="outOfFive_'.$id.'" class="out5Class">'.outOfFive($id).'</span>/5';
			}
			if($showPerc){
				$text .= ' (<span id="percentage_'.$id.'" class="percentClass">'.getRating($id).'</span>)';
			}
			if($showVotes){
				$text .= ' (<span id="showvotes_'.$id.'" class="votesClass">'.getVotes($id).'</span>)';
			}

		if($show5 || $showPerc || $showVotes){

			$text .= '</div>';

		}


		return $text.'
			<ul class="star-rating2" id="rater_'.$id.'">
				<li class="current-rating" style="width:'.getRating($id).';" id="ul_'.$id.'"></li>
				<li><a onclick="return false;" href="#" title="ا من 5" class="one-star" >1</a></li>
				<li><a onclick="return false;" href="#" title="2 من 5" class="two-stars">2</a></li>
				<li><a onclick="return false;" href="#" title="3 من 5" class="three-stars">3</a></li>
				<li><a onclick="return false;" href="#" title="4 من 5" class="four-stars">4</a></li>
				<li><a onclick="return false;" href="#" title="5 من 5" class="five-stars">5</a></li>
			</ul>
			<div id="loading_'.$id.'"></div>';


	} else {

		if($show5 || $showPerc || $showVotes){

			$text .= '<div class="rated_text">';

		}
			if($show5){
				$show5bool = 'true';
				$text .= 'النسبة <span id="outOfFive_'.$id.'" class="out5Class">'.outOfFive($id).'</span>/5';
			} else {
				$show5bool = 'false';
			}
			if($showPerc){
				$showPercbool = 'true';
				$text .= ' (<span id="percentage_'.$id.'" class="percentClass">'.getRating($id).'</span>)';
			} else {
				$showPercbool = 'false';
			}
			if($showVotes){
				$showVotesbool = 'true';
				$text .= ' (<span id="showvotes_'.$id.'" class="votesClass">'.getVotes($id).'</span>)';
			} else {
				$showVotesbool = 'false';
			}

		if($show5 || $showPerc || $showVotes){

			$text .= '</div>';

		}

		return $text.'
			<ul class="star-rating" id="rater_'.$id.'">
				<li class="current-rating" style="width:'.getRating($id).';" id="ul_'.$id.'"></li>
				<li><a onclick="rate(\'1\',\''.$id.'\','.$show5bool.','.$showPercbool.','.$showVotesbool.'); return false;" href="includes/rating_process.php?id='.$id.'&rating=1" title="سيئ" class="one-star" >1</a></li>
				<li><a onclick="rate(\'2\',\''.$id.'\','.$show5bool.','.$showPercbool.','.$showVotesbool.'); return false;" href="includes/rating_process.php?id='.$id.'&rating=2" title="ضعيف" class="two-stars">2</a></li>
				<li><a onclick="rate(\'3\',\''.$id.'\','.$show5bool.','.$showPercbool.','.$showVotesbool.'); return false;" href="includes/rating_process.php?id='.$id.'&rating=3" title="جيد" class="three-stars">3</a></li>
				<li><a onclick="rate(\'4\',\''.$id.'\','.$show5bool.','.$showPercbool.','.$showVotesbool.'); return false;" href="includes/rating_process.php?id='.$id.'&rating=4" title="جيد جدا" class="four-stars">4</a></li>
				<li><a onclick="rate(\'5\',\''.$id.'\','.$show5bool.','.$showPercbool.','.$showVotesbool.'); return false;" href="includes/rating_process.php?id='.$id.'&rating=5" title="ممتاز" class="five-stars">5</a></li>
			</ul>
			<div id="loading_'.$id.'"></div>';

	}
}


function traidntright (){
echo"
<p align='center' dir='ltr'><font face='Verdana' size='1'>&nbsp;<b><font color='#000080'><span style='text-decoration: none'><a target='_blank' href='http://traidnt.com'><font color='#000000'><span style='text-decoration: none'>Traidnt&nbsp;Poem</span></font></a></span></font></b>
<i>Version
1.0.</i></font></p>
";
}

// Added in version 1.5
function getTopRated($limit, $table, $idfield, $namefield){

	$result = '';

	$sql = "SELECT ratings.rating_id,".$table.".".$namefield." as thenamefield,ROUND(AVG(ratings.rating_num),2) as rating
			FROM ratings,".$table." WHERE ".$table.".".$idfield." = ratings.rating_id GROUP BY rating_id
			ORDER BY rating DESC LIMIT ".$limit."";

	$sel = mysql_query($sql);

	$result .= '<ul class="topRatedList">'."\n";

	while($data = @mysql_fetch_assoc($sel)){
		$result .= '<li>'.$data['thenamefield'].' ('.$data['rating'].')</li>'."\n";
	}

	$result .= '</ul>'."\n";

	return $result;

}


  function man ($code){
   $code = explode("*",$code);
   $num = sizeof($code);
   $i = 0;
    $codes =  "<div align='center'>
<table cellspacing='20' cellpadding='0' dir='rtl' border='0' width='100%' >
<tr>
<td width=0 align=center valign=top nowrap>";

    while($i < $num){
       $cos = $i/2;
       if (is_float($cos)){
       	$codes = $codes. "<div style='margin-right:29pt' class='style2'>
			<span  style='font-size:1pt;letter-spacing:1.5pt&nbsp;;visibility:hidden;'>ii</span><strong> $code[$i]</strong>
		</div>";
       }else{
       	$codes = $codes."<div style='margin-left:31pt' class='style2'><strong>$code[$i]</strong></div>";
       	}

       $i++;
		}
   $codes = $codes."</td>

</tr>
</table>
</div>";

   return($codes);
  }


   function nltobr($var, $xhtml = FALSE){
    if($var){
        if($xhtml == FALSE){
            $array = array("\r\n", "\n\r", "\n", "\r");
            $var = str_replace($array, "<br /><br />", $var);
            return $var;
        }
        else{
            $array = array("\r\n", "\n\r", "\n", "\r");
            $var = str_replace($array, "<br /> <br /><br />", $var);
            return $var;
        }
    }
    else{
        return FALSE;
    }
}

?>