<?php
//echo "combined_telephone= $combined_telephone";

$sessionHandling = 1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">
<html>
<head>
  <title>Warpit</title>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head><body>
<?php 
include_once ("_dirinfo.php");
include_once (PATH_TO_ROOT."init.php");

ini_set('display_errors', '1');     # don't show any errors...
error_reporting(E_ERROR);  # ...but do log them

echo "<form name=telephones action=".$_SERVER['PHP_SELF']." method=post>";

$uniqa_css = "Style=font-family:Arial;font-size:12px;color:black;";


if (isset($_REQUEST['save_telephones_x'])) {
	foreach ($_REQUEST['phones_tel'] AS $keyt => $itemt) {
		if ($keyt==0 AND $itemt) {
			$combined_telephone = $_REQUEST['phones_oms'][$keyt]."/".$itemt;
			$main_oms = $_REQUEST['phones_oms'][$keyt];
			$main_telephone = $itemt;
		}
		if ($keyt>0 AND $itemt) $arr[] = $_REQUEST['phones_oms'][$keyt]."/".$itemt;
		if ($keyt>0 AND $itemt) $iframe_telephones .= ($keyt).". - <b>".$_REQUEST['phones_oms'][$keyt]."/".$itemt."</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	}
	
	
	$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);
	$update_ok = false;
	if ($arr AND $main_telephone AND $main_oms) {
		$arr_telephones = implode("*",$arr);
		//echo $combined_telephone." :after save combined_telephone<br>";
		//echo $arr_telephones." :after save arr_telephones<br>";
		
		$sql = "UPDATE ".$_REQUEST['telName']." SET ".$_REQUEST['field_t2']."='".$arr_telephones."', oms='".$main_oms."', telephone='".$main_telephone."' WHERE id='".$_REQUEST['id_tel']."'";
		//echo "$sql<br>";
		$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);
		try {
			$db->SQLexecute($sql);
			$update_ok=true;
		} catch(Exception $e) {
			echo "<font face=Arial color=red><b>Additional telephone numbers are not supported on this project! </b></font>";
		}
	} elseif ($main_telephone AND $main_oms) {
		$sql = "UPDATE ".$_REQUEST['telName']." SET oms='".$main_oms."', telephone='".$main_telephone."' WHERE id='".$_REQUEST['id_tel']."'";
		//echo "$sql<br>";
		$db->SQLexecute($sql);
		$update_ok=true;
	} else {
		echo "<font face=Arial color=red><b>You can not save numbers without a main number. Please try again! </b></font>";
		$combined_telephone = $combined_telephone_bck;
		$iframe_telephones = $iframe_telephones_bck;
		$arr_telephones = $arr_telephones_bck;
	}
	
	
	if ($update_ok) {
		?>
		<script>
			parent.document.getElementById('main_telephone').innerHTML='<?php echo $combined_telephone?>';
		</script>
		<?php			
	}
}

	
	if ($_REQUEST['edit_telephones_x']) {
		//echo "ct:".$combined_telephone ." <-> rct:". $_REQUEST['combined_telephone'] . " <-> at:" . $arr_telephones ." <-> rat:". $_REQUEST['arr_telephones'];
		if (isset($_REQUEST['combined_telephone'])) $combined_telephone = $_REQUEST['combined_telephone'];
		if (isset($_REQUEST['arr_telephones'])) $arr_telephones = $_REQUEST['arr_telephones'];
		
		$main_phone = explode("/", $combined_telephone, 2);
		
		$arr = explode("*",$arr_telephones); 
		echo "1. <input size=1 type=input name=phones_oms[0] value=\"".$main_phone[0]."\">/<input size=12 type=input name=phones_tel[0] value=\"".$main_phone[1]."\">&nbsp;&nbsp;&nbsp;&nbsp;";
		for ($n=1;$n<=4;$n++) {
			$add_phone = explode("/",$arr[($n-1)], 2);
			echo ($n+1).". <input size=1 type=input name=phones_oms[$n] value=\"".$add_phone[0]."\">/<input size=12 type=input name=phones_tel[$n] value=\"".$add_phone[1]."\">&nbsp;&nbsp;&nbsp;&nbsp;";
		}
		echo "<input type=hidden name=combined_telephone_bck value=\"".$_REQUEST['combined_telephone_bck']."\">";
		echo "<input type=hidden name=iframe_telephones_bck value=\"".$_REQUEST['iframe_telephones_bck']."\">";
		echo "<input type=hidden name=arr_telephones_bck value=\"".$_REQUEST['arr_telephones_bck']."\">";
		echo "<input name=save_telephones type=image src=../_img/OK16ng.gif>";
		
	} else {
		if (isset($_REQUEST['iframe_telephones'])) $iframe_telephones = $_REQUEST['iframe_telephones'];
		if (isset($_REQUEST['combined_telephone'])) $combined_telephone = $_REQUEST['combined_telephone'];
		if (isset($_REQUEST['arr_telephones'])) $arr_telephones = $_REQUEST['arr_telephones'];
		
		echo "<span $uniqa_css>".$iframe_telephones."</span>";
		echo "<input name=edit_telephones type=image src=../_img/database/EditRecord16ni8.gif>";
		
		echo "<input id=ID_main_telephone type=hidden name=combined_telephone value=\"".$combined_telephone."\">";
		echo "<input type=hidden name=iframe_telephones value=\"".$iframe_telephones."\">";
		echo "<input type=hidden name=arr_telephones value=\"".$arr_telephones."\">";
		if ($_REQUEST['combined_telephone']) echo "<input id=ID_main_telephone type=hidden name=combined_telephone_bck value=\"".$combined_telephone."\">";
		if ($_REQUEST['iframe_telephones']) echo "<input type=hidden name=iframe_telephones_bck value=\"".$iframe_telephones."\">";
		if ($_REQUEST['arr_telephones']) echo "<input type=hidden name=arr_telephones_bck value=\"".$arr_telephones."\">";
		
		
		
		
	}

	echo "<input type=hidden name=field_t2 value=\"".$_REQUEST['field_t2']."\">";
	echo "<input type=hidden name=id_tel value=\"".$_REQUEST['id_tel']."\">";
	echo "<input type=hidden name=telName value=\"".$_REQUEST['telName']."\">";

echo "</form>";

?></body>
<html>