<?php

include("../includes/site_config.php");
include('../session_handler.inc');
include('../message.php');
include('../readenglish.php');
include("../includes/functions.php");

/*if ($Sess_UserType != "A") {
	exit;
}*/
include('../pop_lists.inc');
include('../error.php');

//$langs=formGet('langs');
//$langs=$_REQUEST['langs'];
//$mode=formGet('mode');
//$mode=$_REQUEST['mode'];
# retrieve the template
$area = 'member';
$lang_id=strtoupper($_REQUEST['lang_id']);
$lang_name=form_get('lang_name');
$lang_charset=form_get('lang_charset');
$flag=form_get('flag')."."."png";
$mode=$_GET['mode'];
$msg=$_GET['msg'];
$active_lange = $_REQUEST['langs'];


if($lang_name!="" && $lang_name!="english")
{

	$lowename = strtolower($lang_name);
	
	$file2 = "../languages/english.php";
	$newfile2 =$SITE_ROOT. "languages/$lowename.php"; 
	
	if (!copy($file2,$newfile2)) 
	{
	   echo "failed to copy $file...\n";
	}
	
	$file1 = "../languages/english_js.php";
	$newfile1 =$SITE_ROOT. "languages/".$lowename."_js.php"; 
	
	if (!copy($file1,$newfile1)) 
	{
	   echo "failed to copy $file...\n";
	}
	
	$file3 = "../languages/english_admin.php";
	$newfile3 =$SITE_ROOT. "languages/".$lowename."_admin.php"; 
	
	if (!copy($file3,$newfile3)) 
	{
	   echo "failed to copy $file...\n";
	}
}

$rdoquery = mysql_query("SELECT count(*) as ifExists from langfile where lang_name = '".$lang_name."'");
$rsstock = mysql_fetch_array($rdoquery);
if($rsstock["ifExists"] > 0)
{
	echo "<script> location.href='$CONST_LINK_ROOT/admin/language.php?msg=inv?PHPSESSID=.session_id()</script>";
}
else
{
		if(isset($_REQUEST['ins_language']))
		{
		$ins=mysql_query("insert into langfile(lang_id, lang_name, lang_filename,lang_imagename, lang_charset) values('$lang_id','$lang_name','$lang_name','$flag', '$lang_charset')");
		unset($_REQUEST['ins_language']);
		compile_lang($lang_id);
		
		}
}

if(isset($_REQUEST['rem']) && $_REQUEST['rem']!='EN')
{
$str_Query=mysql_fetch_object(mysql_query("select * from langfile where md5(lang_id)='".form_get('rem')."'"));
$lang_name=strtolower($str_Query->lang_name);
$file1 = "$lang_name.php";
unlink($SITE_ROOT. "languages/$file1");
$file2 = $lang_name."_js.php";
unlink($SITE_ROOT. "languages/$file2");
$file3 = $lang_name."_admin.php";
unlink($SITE_ROOT. "languages/$file3");
$del=mysql_query("delete from langfile where md5(lang_id)='".form_get('rem')."'");
$dell=mysql_query("delete from language where md5(language_id)='".form_get('rem')."'");

}

switch ($mode) {
	case 'save':
		restrict_demo();
        $language->DeactivateAll();
        $language->Activate($CONST_DEFAULT_LANGUAGE);
        if (!in_array($CONST_DEFAULT_LANGUAGE,$langs)) error_page("You can't deactivate default language [$CONST_DEFAULT_LANGUAGE]",GENERAL_USER_ERROR);
		foreach ((array)$langs as $lang) {
			$language->Activate($lang);
		}
        if (count($language->error)) error_page(join("<br>",$language->error),GENERAL_USER_ERROR);
		break;
}

###################
#  Check Box Value
###################

if(isset($_REQUEST['langs']))
{
mysql_query("UPDATE langfile SET lang_active = 0");
 foreach($active_lange as $key =>$lang_id)
  {
    mysql_query("UPDATE langfile SET lang_active = 1 WHERE lang_id = '$lang_id'");
   }
}
############################

$search_key=form_get('search_key');
$search_text=form_get('search_text');

if(isset($_REQUEST['del'])){

$del=mysql_query("delete from language where lan_id='".form_get('del')."'");

}

$ret_key=mysql_query("select * from language where title LIKE '%$search_text%' OR value LIKE '%$search_text%'");



?>
<?php include("include/header.php"); ?>

<script language="JavaScript" type="text/JavaScript">
<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0

  window.open(theURL,winName,features);
  return false;
}
//-->
</script>
<script language="JavaScript" type="text/JavaScript">
function validation()
{
	if(document.Frmlanguage.lang_name.value.length=="")
	{
	alert("The Language Name Field can not be left blank");
	document.Frmlanguage.lang_name.focus();
	return(false);
	}
    
	if(document.Frmlanguage.lang_charset.value.length=="")
	{
	alert("The Charset Field can not be left blank");
	document.Frmlanguage.lang_charset.focus();
	return(false);
	}
	
	if(document.Frmlanguage.lang_id.value.length!=2)
	{
	alert("The Id Field should be 2 characters long");
	document.Frmlanguage.lang_id.focus();
	return(false);
	}
}


function valid_key(){

if(document.FrmListbox.search_text.value.length<2)
	{
	alert("The Serch Field can not be left blank");
	document.FrmListbox.search_text.focus();
	return(false);
	}
}


//function validate_lang(){
//
//if(document.Frmlanguage.lang_name.value.length<2)
//	{
//	alert("The Serch Field can not be left blank");
//	document.Frmlanguage.search_text.focus();
//	return(false);
//	}
//}
</script><div class="mainwidth">
<div class="admin-page-header"><div class="admin-page-heading">&nbsp;&nbsp;Language Admin</div></div>
<div class="clear"></div>
<div class="admin-page">
<table cellpadding="0" cellspacing="0" border=0 width="650px">
<tr><td>
<table cellpadding="0" cellspacing="0" border=0 width="650px">
<tr>
    <td class="pageheader"><?php echo LANGUAGE_SECTION_NAME ?></td>
  </tr>
  <tr>
    <td width="650px"><div style="border:1px solid #dcdcdc; padding:10px">
	<form action="<?=$_SERVER['PHP_SELF']?>" method="post" name="Frmlanguage" onsubmit="return validation()">
	<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="43%"><div align="center"><strong>Default Language:        
      </strong>
      <select name="menu1" onchange="MM_jumpMenu('parent',this,0)">
        <option>English</option>
      </select>
    </div></td>
    <td width="4%">&nbsp;</td>
    <td width="53%"><div align="right"><strong>Flag:</strong>  
      <select name="flag" onChange="javascript: flagImage = document.getElementById('flagImageId'); flagImage.src = '<?=$CONST_IMAGE_ROOT?>/flags/' + this.value + '.png';">
		<option value="China">China</option>
		<option value="France">France</option>
		<option value="Germany">Germany</option>
   		<option value="Japan">Japan</option>
		<option value="Mexico">Mexico</option>        
   		<option value="Portugal">Portugal</option>
   		<option value="Thailand">Thailand</option>
		<option value="United States of America (USA)" selected="selected">United States</option>
        </select> <img src="<?=$CONST_IMAGE_ROOT?>/flags/United States of America (USA).png" id="flagImageId" width="18" width="12" alt="flag" />
    </div></td>
  </tr>
  <tr>
    <td><? if($_GET['msg']!="")
	  {
	  echo "<strong>Please Chosse Another Language<strong><br>";
	  }
	  ?></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td align="right"><strong>Add New Language: </strong>
      <input type="text" name="lang_name" style="width:100px" /></td>
    <td>&nbsp;</td>
    <td><div align="left"><strong>Charset:</strong> 
      <input type="text" name="lang_charset" style="width:100px" /> 
      
    </div></td>
  </tr>
  <tr>
    <td align="right"><strong>Id: </strong>
      <input type="text" name="lang_id" style="width:100px" maxlength="2" /></td>
	  <td>&nbsp;</td>
	  <td><div align="left"><input type="submit" name="ins_language" value="Save Changes" onclick="return validate_lang()"/></div></td></tr>
</table></form>

	<?php //echo LANGUAGE_HELP ?></div></td>
  </tr>
  <tr><td>&nbsp;</td></tr>
  <tr>
    <td><table width="650px"  border="1" cellpadding="3" cellspacing="0">
      
        <tr>
          <td align="left" valign="top" bgcolor="#cecddf"><strong>Language</strong></td>
          <td align="left" valign="top" bgcolor="#cecddf"><strong>Flag</strong></td>
          <td colspan="3" align="left" valign="top" bgcolor="#cecddf">&nbsp;</td>
          </tr>
		  <form action='<?=$_SERVER['PHP_SELF']?>' method='post' name="FrmLang1" id="FrmLang1">
        <?php
$query="SELECT * FROM langfile";
$res = mysql_query($query,$link);
while ($lang = mysql_fetch_assoc($res)) {
//echo $lang['lang_id'];
?>
        <tr>
          <td align='left' valign='top' >
		  <table cellpadding="0" cellspacing="0" width="100%"><tr><td>
		  <?= $lang['lang_name'] ?></td><td align="right">
            <input border="0" type='checkbox' value="<?=$lang['lang_id'] ?>" name="langs[]" <? if ($lang['lang_active'] == 1){ echo "checked" ;}?> />
			</td></tr></table>			</td>
          <td width="5%" align='left' valign='top' ><img src="<?=$SKIN_IMAGE_PATH?>/flags/<?=$lang['lang_imagename']?>" height="20" width="30"></td>


          <td width="5%" align='left' valign='top' ><?=KEY_EDIT?></td>
          <td width="5%" align='left' valign='top' >Compile</td>
          <td width="5%" align='left' valign='top' > <? if($DEMO==false && $lang['lang_id']!='EN'){?><a href="<?=$_SERVER['PHP_SELF']?>?rem=<?=md5($lang['lang_id'])?>" onclick="javascript:return confirm('Are you sure you want to DELETE this language?')"><?=KEY_DELETE?></a><? }elseif($lang['lang_id']=='EN'){?>Default<? }else{?>Demo<? }?></td>
        </tr>
        <?
									}
?>
        <tr>
          <td colspan="5" align="center"><input name="submit" type='submit' onclick="FrmListbox.mode.value='save';" value='<?= GENERAL_SAVE?>' class='button' />
              <input type='hidden' name='mode' value='save' />          </td>
        </tr>
		</form>
    </table>
<table width="650" border="0" cellspacing="0" cellpadding="0">
<form action='<?php echo $CONST_LINK_ROOT?>/admin/language.php<?php print("?PHPSESSID=".session_id()); ?>' method='post' enctype='multipart/form-data' name="FrmListbox" id="FrmListbox">
  <tr>
    <td align="right" class="pageheader"><div align="left"><strong>Search for Text</strong></div></td>
  </tr>
  <tr>
    <td align="right"><div style="border:1px solid #dcdcdc; padding:10px" align="left">Look for: 
      <input type="text" name="search_text" />

      <input type="submit" name="search_key" value="Search" onclick="return valid_key()" />

    </div></td>
  </tr> </form>
  <tr>
    <td align="right" class="pageheader"><div align="left"><strong>Search Results </strong></div></td>
  </tr>
  <tr>
    <td align="right"><div style="border:1px solid #dcdcdc; padding:10px" align="left"><a href="javascript:;" 
onclick="MM_openBrWindow('ins_lang.php?mode=create','','width=500,height=400');"><?=ADD_LANGUAGE_KEY?></a>
        <hr class="line" />


<table width="90%" border="1" cellspacing="0" cellpadding="3">
  <tr>
    <td bgcolor="#F1F1F1" class="normal"><strong>Lang Text Code</strong> </td>
    <td bgcolor="#F1F1F1" class="normal"><strong>Language Text</strong> </td>
    <td bgcolor="#F1F1F1" class="normal"><strong>Language</strong></td>
    <td colspan="2" bgcolor="#F1F1F1" class="normal"><div align="center" class="normal"><strong>Modify</strong></div></td>
    </tr>
	
	<? if(isset($_REQUEST['search_key'])){  
while($search=mysql_fetch_object($ret_key)){?>

  <tr>
    <td><?=$search->title?></td>
    <td><?=$search->value?></td>
    <td><?=$search->language_id?></td>
    <td><? if($DEMO!=true){?><a href="javascript:;" 
onclick="MM_openBrWindow('ins_lang.php?mode=edit&tit=<?=$search->lan_id?>','','width=500,height=400');"><?=KEY_EDIT?></a><? }else{echo "Disabled";}?></td>
    <td><? if($DEMO!=true){?><a href="<?=$_SERVER['PHP_SELF']?>?del=<?=$search->lan_id?>&search_key=<?=$search_key?>&search_text=<?=$search_text?>" onclick="javascript:return confirm('Are you sure you want to DELETE this language key?')"><?=KEY_DELETE?></a><? }else{echo "Disabled";}?></td>
  </tr> <? } }?>
</table>

    </div></td>
  </tr>
  <tr>
    <td align="right">&nbsp;</td>
  </tr>
  <tr>
    <td align="right">         </td>
  </tr>
</table></td>
  </tr>
</table></td>
  </tr>
</table></div></div></div><div class="admin-page-bottom"></div></div>

<?php include("include/footer.php"); ?>
