<?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=$_REQUEST['lang_name'];
$lang_charset=$_REQUEST['lang_charset'];
$flag=$_REQUEST['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 lang_id='".$_REQUEST['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 lang_id='".$_REQUEST['rem']."'");
$dell=mysql_query("delete from language where language_id='".$_REQUEST['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=$_REQUEST['search_key'];
$search_text=$_REQUEST['search_text'];

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

$del=mysql_query("delete from language where lan_id='".$_REQUEST['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>
<style type="text/css">
<!--
.style1 {
	color: #FF0000;
	font-weight: bold;
}
-->
</style>
<div class="mainwidth">
<div class="admin-page-header">
  <div class="admin-page-heading">Add Language</div>
</div>
<div class="clear"></div>
<div class="admin-page">
<table cellpadding="0" cellspacing="0" border=0 width="650px">
  <!--DWLayoutTable-->
<tr><td width="1152" valign="top">
  <table cellpadding="0" cellspacing="0" border=0 width="650px">
    <!--DWLayoutTable-->
    <tr>
      <td width="1152"><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%">
		</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';">
		  <? 
		
		$dir="$SITE_ROOT/templates/default/images/flags/";
		
		
		if ($handle = opendir($dir)) {
   

    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
	if (($file != '.') && ($file != '..')) {
         "$file\n";
		$files=str_replace('.png','',$file);
	echo"<option value=\"$files\">$files</option>";
    	}
	}

    /* This is the WRONG way to loop over the directory. */
  
    closedir($handle);
}
			
		?>
            <!--<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> 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 height="19">&nbsp;</td>
        </tr>
    <tr>
      <td height="125" valign="top"><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=<?= $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></td>
      </tr>
    <tr>
      <td valign="top"><p><span class="style1">Note:</span> When you add a new language, you need to know the charset of that language. That is something that you will have to research and find out. It's based on the language that you are wanting to add. To add a new language, choose the country from the dropdown. Once you select the country you want, you can add the language. Then add the Charset, then add the ID. </p>
          <p>We have an example below using Spanish as the new language being added.</p>
          <p><strong>Language:</strong> &nbsp;Spanish<br />
            <strong>Charset:</strong>  &nbsp;iso-8859-1<br />
            <strong>ID:</strong>  &nbsp;SP</p>
          <p>Then you want to click save. What that will do is copy all the English to the Spanish area. So, when you do a search in the <a href="language.php"><em><strong>Language Area</strong></em></a>, you will get two results. One will be EN and the other will be SP. To translate the spanish, you wanted to edit the SP result only. When you click edit, it will show you a popup box. Don't change/edit the code part, only edit the Text/HTML part. That is where you put your Spanish translation. Then you click Update. </p>        <p>So, for example, if you want to add Spanish to the About Us page, then you search for ABOUTUS_TEXT then you will get two results, one for EN and one for SP. Only edit the SP and then replace that text that you see there with the Spanish version. Then update. Then you will see that page change from English to Spanish when you click the flags that are at the footer of the site.</p>
          <p>Once you have added the language, you will need to activate it. Click the checkbox next to it after it's created then click Save.</p></td>
      </tr>
    </table></td>
  </tr>
</table>
</div></div></div><div class="admin-page-bottom"></div></div>

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