foreach ($ids as $key => $j) {
     if ($i == $j) {
         continue;
     }
     if (getStr("Device.NAT.PortMapping." . $j . ".LeaseDuration") == 0 && getStr("Device.NAT.PortMapping." . $j . ".InternalPort") == 0) {
         $arrayName = getStr("Device.NAT.PortMapping." . $j . ".Description");
         $arrayIP = getStr("Device.NAT.PortMapping." . $j . ".InternalClient");
         $arrayType = getStr("Device.NAT.PortMapping." . $j . ".Protocol");
         $arraySPort = getStr("Device.NAT.PortMapping." . $j . ".ExternalPort");
         $arrayEPort = getStr("Device.NAT.PortMapping." . $j . ".ExternalPortEndRange");
         if ($name == $arrayName) {
             $result .= "Service name has been used!\n";
             break;
         } else {
             if ($type == "BOTH" || $arrayType == "BOTH" || $type == $arrayType) {
                 $porttest = PORTTEST($sport, $eport, $arraySPort, $arrayEPort);
                 if ($porttest == 1) {
                     $result .= "Conflict with other service. Please check port and IP!";
                     break;
                 }
             }
         }
     }
 }
 if ($result == "") {
     $rootObjName = "Device.NAT.PortMapping.";
     $paramArray = array(array("Device.NAT.PortMapping." . $i . ".Enable", "bool", "true"), array("Device.NAT.PortMapping." . $i . ".InternalClient", "string", $ip), array("Device.NAT.PortMapping." . $i . ".X_CISCO_COM_InternalClientV6", "string", $ip6), array("Device.NAT.PortMapping." . $i . ".InternalPort", "uint", "0"), array("Device.NAT.PortMapping." . $i . ".ExternalPort", "uint", $sport), array("Device.NAT.PortMapping." . $i . ".ExternalPortEndRange", "uint", $eport), array("Device.NAT.PortMapping." . $i . ".Protocol", "string", $type), array("Device.NAT.PortMapping." . $i . ".Description", "string", $name));
     $retStatus = DmExtSetStrsWithRootObj($rootObjName, TRUE, $paramArray);
     if (!$retStatus) {
         $result = "Success!";
     }
 //$result="";
 $ids = explode(",", getInstanceIDs("Device.NAT.PortMapping."));
 foreach ($ids as $key => $j) {
     if (getStr("Device.NAT.PortMapping." . $j . ".LeaseDuration") == 0 && getStr("Device.NAT.PortMapping." . $j . ".InternalPort") != 0) {
         $arrayName = getStr("Device.NAT.PortMapping." . $j . ".Description");
         $arrayIP = getStr("Device.NAT.PortMapping." . $j . ".InternalClient");
         $arrayType = getStr("Device.NAT.PortMapping." . $j . ".Protocol");
         $arraySPort = getStr("Device.NAT.PortMapping." . $j . ".ExternalPort");
         $arrayEPort = getStr("Device.NAT.PortMapping." . $j . ".ExternalPortEndRange");
         $arrayPPort = getStr("Device.NAT.PortMapping." . $j . ".InternalPort");
         if ($name == $arrayName) {
             $result .= "Service name has been used!\n";
             break;
         } else {
             if ($type == "BOTH" || $arrayType == "BOTH" || $type == $arrayType) {
                 $porttest = PORTTEST($startport, $endport, $arraySPort, $arrayEPort);
                 if ($porttest == 1) {
                     $result .= "Conflict with other service. Please check port and IP!";
                     break;
                 }
             }
         }
     }
 }
 if ($result == "") {
     addTblObj("Device.NAT.PortMapping.");
     $IDs = explode(",", getInstanceIDs("Device.NAT.PortMapping."));
     $i = $IDs[count($IDs) - 1];
     $rootObjName = "Device.NAT.PortMapping.";
     $paramArray = array(array("Device.NAT.PortMapping." . $i . ".Enable", "bool", $enableHSEntry), array("Device.NAT.PortMapping." . $i . ".InternalClient", "string", $ip), array("Device.NAT.PortMapping." . $i . ".InternalPort", "uint", $priport), array("Device.NAT.PortMapping." . $i . ".ExternalPort", "uint", $startport), array("Device.NAT.PortMapping." . $i . ".ExternalPortEndRange", "uint", $endport), array("Device.NAT.PortMapping." . $i . ".Protocol", "string", $type), array("Device.NAT.PortMapping." . $i . ".Description", "string", $name));
     $retStatus = DmExtSetStrsWithRootObj($rootObjName, TRUE, $paramArray);
             }
         }
     }
 }
 if ($result == "") {
     /*
      * this piece of code is going to check forward start port and end port not overlapped with port forwarding entry
      */
     $ids = explode(",", getInstanceIDs("Device.NAT.PortMapping."));
     foreach ($ids as $key => $j) {
         if (getStr("Device.NAT.PortMapping." . $j . ".LeaseDuration") == 0 && getStr("Device.NAT.PortMapping." . $j . ".InternalPort") == 0) {
             $portMappingType = getStr("Device.NAT.PortMapping." . $j . ".Protocol");
             $arraySPort = getStr("Device.NAT.PortMapping." . $j . ".ExternalPort");
             $arrayEPort = getStr("Device.NAT.PortMapping." . $j . ".ExternalPortEndRange");
             if ($type == "BOTH" || $portMappingType == "BOTH" || $type == $portMappingType) {
                 $porttest = PORTTEST($tsp, $tep, $arraySPort, $arrayEPort);
                 if ($porttest == 1) {
                     $result .= "Conflict with other service. Please check port and IP!";
                     break;
                 }
             }
         }
     }
     //end of foreach
 }
 if ($result == "") {
     // setStr("Device.NAT.X_CISCO_COM_PortTriggers.Trigger.".$i.".TriggerPortStart",$fsp,false);//from start port
     // setStr("Device.NAT.X_CISCO_COM_PortTriggers.Trigger.".$i.".TriggerPortEnd",$fep,false);
     // setStr("Device.NAT.X_CISCO_COM_PortTriggers.Trigger.".$i.".TriggerProtocol",$type,false);
     // setStr("Device.NAT.X_CISCO_COM_PortTriggers.Trigger.".$i.".ForwardProtocol",$type,false);//need to ask wu
     // setStr("Device.NAT.X_CISCO_COM_PortTriggers.Trigger.".$i.".ForwardPortStart",$tsp,false);//to start port
 }
 $ids = explode(",", getInstanceIDs("Device.X_Comcast_com_ParentalControl.ManagedServices.Service."));
 $result = "";
 foreach ($ids as $key => $j) {
     if ($i == $j) {
         continue;
     }
     $serviceName = getStr("Device.X_Comcast_com_ParentalControl.ManagedServices.Service.{$j}.Description");
     $stport = getStr("Device.X_Comcast_com_ParentalControl.ManagedServices.Service.{$j}.StartPort");
     $edport = getStr("Device.X_Comcast_com_ParentalControl.ManagedServices.Service.{$j}.EndPort");
     $ptcol_type = getStr("Device.X_Comcast_com_ParentalControl.ManagedServices.Service.{$j}.Protocol");
     if ($service == $serviceName) {
         $result .= "Service Name has been used!\n";
         break;
     } elseif ($type == "BOTH" || $ptcol_type == "BOTH" || $type == $ptcol_type) {
         $porttest = PORTTEST($startPort, $endPort, $stport, $edport);
         if ($porttest == 1) {
             $result .= "Conflict with other service. Please check your input!";
             break;
         }
     }
 }
 if ($result == "") {
     setStr("Device.X_Comcast_com_ParentalControl.ManagedServices.Service." . $i . ".Description", $service, false);
     setStr("Device.X_Comcast_com_ParentalControl.ManagedServices.Service." . $i . ".Protocol", $protocol, false);
     setStr("Device.X_Comcast_com_ParentalControl.ManagedServices.Service." . $i . ".StartPort", $startPort, false);
     setStr("Device.X_Comcast_com_ParentalControl.ManagedServices.Service." . $i . ".EndPort", $endPort, false);
     if ($block == "false") {
         setStr("Device.X_Comcast_com_ParentalControl.ManagedServices.Service." . $i . ".StartTime", $_POST['startTime'], false);
         setStr("Device.X_Comcast_com_ParentalControl.ManagedServices.Service." . $i . ".EndTime", $_POST['endTime'], false);
         setStr("Device.X_Comcast_com_ParentalControl.ManagedServices.Service." . $i . ".BlockDays", $_POST['days'], false);