<?php

ini_set('display_errors', 0); // disable error display
ini_set('log_errors', 0); // disable error logging
//echo $html;exit;
//if(!empty($_POST["clientId"])){
 require __DIR__.'/../config/app.settings.php';    

 $image1 = $_POST["image1"];
 $image2 = $_POST["image2"];
 $image3 = $_POST["image3"];
 $image4 = $_POST["image4"];
 $image5 = $_POST["image5"];
 $image6 = $_POST["image6"];

 if(ENVIROMENT == 1){
    $cut = APP_SERVER_HOST_URL."webroot";
    $image1 = str_replace($cut,"..",$image1);
    $image2 = str_replace($cut,"..",$image2);
    $image3 = str_replace($cut,"..",$image3);
    $image4 = str_replace($cut,"..",$image4);
    $image5 = str_replace($cut,"..",$image5);
    $image6 = str_replace($cut,"..",$image6);
 }
 
 //print_r($html);exit;
 
 $experian_report = $_POST["experian_report"];
 
 $alleged = $_POST["alleged"];
 $creditor_details = nl2br($_POST["creditor_details"]);
 $alleged_account = $_POST["alleged_account"];
// $file_name = mt_rand(0, 1000).time().'_'.$pdf_name;
// if(isset($cra_report))
//    {
// 	
// 	//get html content from pre defined format
// 	$today = date("d M, Y");
// 	$html = file_get_contents($html_format_name);
 	
// 	if(!empty($_POST["clientId"])){
           $varList = array(
 			'__CRA_REPORT__', '__FIRST_NAME__','__LAST_NAME__','__STREET_NO__','__STREET_NAME__',
 			'__CITY__','__STATE__','__TODAY__','__PERSONAL_TRACKING_NUMBER__',
 			'__credit_bureau_name_address__','__sincerely__','__image1__','__image2__',
                        '__image3__','__image4__','__image5__','__image6__','__EXPERIAN_REPORT__', '__NAME_ADDRESS__','__RECORDS__',
 			'__SOCIAL_SECURITY_NUMBER__','__ADDRESS__','__REPORT_NAME_PREFIX__','__alleged__','__Creditor_Details__',
                        '__ALLEGED_ACCOUNT__','__RECORD_NAME__','__RECORD_DOCKET__','__RECORD_ADDRESS__','__RECORD_DATE__',
						'__CURRENT_SSN__', '__CURRENT_DOB__', '__COUNTRY__', 
			'__SIGNATURE_AFFIANT__','__AFFIANT__', '__MONTH__', '__DAY__', '__YEAR__', '__TIME__', '__ADDRESSEE__','__TRACKING_NUMBER__',
 	
            );
           //echo $cra_report;exit;
           $valueList = array(
 			$cra_report, $first_name,$last_name,$street_no,$street_name,
 			$city,$state,$today,$personal_tracking_number,
 			$credit_bureau_name_address,$sincerely,$image1,$image2,$image3,$image4,$image5,$image6,$experian_report,
                        $name_address,$records,$personal_tracking_number,$address,$report_name_prefix,$alleged,$creditor_details,
                        $alleged_account,$record_name,$record_docket,$record_address,$record_date,$current_ssn, $current_dob, $country, 
						$signature_affiant,$affiant, $month, $day, $year, $time, $addressee,$tracking_number,
               );

//        }
//        else{
// 	  
// 	$varList = array(
// 			'__CRA_REPORT__', '__FIRST_NAME__','__LAST_NAME__','__STREET_NO__','__STREET_NAME__',
// 			'__CITY__','__STATE__','__TODAY__','__PERSONAL_TRACKING_NUMBER__',
// 			'__credit_bureau_name_address__','__sincerely__'
// 	
// 	);
// 	
//
// 	$valueList = array(
// 			$cra_report, $first_name,$last_name,$street_no,$street_name,
// 			$city,$state,$today,$personal_tracking_number,
// 			$credit_bureau_name_address,$sincerely );
// 	
//        }
// 	$serialLineHTML = "";
// 	
// 	//Optional Creditors and Dates
// 	for($i=1;$i<= 8;$i++){
// 		if(isset($_POST["creditor".$i]) && !empty($_POST["creditor".$i])){
// 			$creditor[$i] = $_POST["creditor".$i];
// 			$account[$i] = $_POST["account".$i];
// 			$varList[] = "__creditor".$i."__";
// 			$varList[] = "__account".$i."__";
// 			$valueList[] = $creditor[$i];
// 			$valueList[] = $account[$i];
// 			
// 			$start = "__START".$i."__";
// 			$end = "__END".$i."__";
// 			
// 			$html =  str_replace(array($start,$end),array("",""),$html); 
// 			$serialLineHTML .= '<br/>Medical creditor #'.$i.': <span style="background-color: #FFFF00">__creditor'.$i.'__</span>  Account #'.$i.' ALLEGED: <span style="background-color: #FFFF00">__account'.$i.'__</span><br/>';
// 			
// 		} else {
// 			
// 			
// 			$from = strpos($html, "__START".$i."__");
// 			$to = strpos($html, "__END".$i."__"); 			
// 			
// 			$html =  str_replace(substr($html,$from,($to-$from)+7+strlen($i)),"",$html);
// 		}
// 	}
// 	
 	  
// 	if(!empty($image1 || $image2 || $image3 || $image4 || $image5))
//        {      
           //echo "<pre>";print_r($_POST);exit;
           if(empty($_POST['form_id']) || $_POST['form_id'] == 63){
            
            if(empty($image1)){                            
                $from = strpos($html, "__STARTM1__");
                $to = strpos($html, "__ENDM1__");
                if(!empty($from) && !empty($to))
                $html =  str_replace(substr($html,$from,($to-$from)+9),"",$html);  
            }
             
         
            if(empty($image2)){                            
                $from = strpos($html, "__STARTM2__");
                $to = strpos($html, "__ENDM2__");
                if(!empty($from) && !empty($to))
                $html =  str_replace(substr($html,$from,($to-$from)+9+strlen($i)),"",$html);  
                }
                
                
            if(empty($image3)){                            
                $from = strpos($html, "__STARTM3__");
                $to = strpos($html, "__ENDM3__");
                if(!empty($from) && !empty($to))
                $html =  str_replace(substr($html,$from,($to-$from)+9+strlen($i)),"",$html);  
                }
                
                
            if(empty($image4)){                            
                $from = strpos($html, "__STARTM4__");
                $to = strpos($html, "__ENDM4__");
                if(!empty($from) && !empty($to))
                $html =  str_replace(substr($html,$from,($to-$from)+9+strlen($i)),"",$html);  
                }
               
            if(empty($image5)){                            
                $from = strpos($html, "__STARTM5__");
                $to = strpos($html, "__ENDM5__");
                if(!empty($from) && !empty($to))
                $html =  str_replace(substr($html,$from,($to-$from)+9+strlen($i)),"",$html);  
                }
                 
            if(empty($image6)){                            
                $from = strpos($html, "__STARTM6__");
                $to = strpos($html, "__ENDM6__");
                if(!empty($from) && !empty($to)){
                    $html =  str_replace(substr($html,$from,($to-$from)+9+strlen($i)),"",$html); 
                }
            }
            
            
            
                 
            for($i=1;$i<= 6;$i++){ 
                $html = str_replace("__STARTM".$i."__","", $html);
            }
            for($i=1;$i<= 6;$i++){
                $html = str_replace("__ENDM".$i."__","", $html);
            }
                
           }else{
               
               if(empty($image1)){                            
                    $from = strpos($html, "__STARTM1__");
                    $to = strpos($html, "__ENDM1__");
                    if(!empty($from) && !empty($to))
                    $html =  str_replace(substr($html,$from,($to-$from)+9+strlen($i)),"",$html);  
                }
                
                
                if(empty($image2)){                            
                    $from = strpos($html, "__STARTM2__");
                    $to = strpos($html, "__ENDM2__");
                    if($from > 0 && $to > 0)
                    $html =  str_replace(substr($html,$from,($to-$from)+9+strlen($i)),"",$html);  
                    }
                if(empty($image3)){                            
                    $from = strpos($html, "__STARTM3__");
                    $to = strpos($html, "__ENDM3__");
                    if($from > 0 && $to > 0)
                    $html =  str_replace(substr($html,$from,($to-$from)+9+strlen($i)),"",$html);  
                    }
                if(empty($image4)){                            
                    $from = strpos($html, "__STARTM4__");
                    $to = strpos($html, "__ENDM4__");
                    if($from > 0 && $to > 0)
                    $html =  str_replace(substr($html,$from,($to-$from)+9+strlen($i)),"",$html);  
                    }
                if(empty($image5)){                            
                    $from = strpos($html, "__STARTM5__");
                    $to = strpos($html, "__ENDM5__");
                    if($from > 0 && $to > 0)
                    $html =  str_replace(substr($html,$from,($to-$from)+9+strlen($i)),"",$html);  
                    }
                    
                if(empty($image6)){                            
                    $from = strpos($html, "__STARTM6__");
                    $to = strpos($html, "__ENDM6__");
                    if($from > 0 && $to > 0)
                    $html =  str_replace(substr($html,$from,($to-$from)+9),"",$html);  
                }
              
               
                for($i=1;$i<= 6;$i++){ 
                    $html = str_replace("__STARTM".$i."__","", $html);
                }
                for($i=1;$i<= 6;$i++){
                    $html = str_replace("__ENDM".$i."__","", $html);
                }
                
                
           }
            
            //for upload image name
            if(!empty($_POST['form_id'])){
                if($_POST["form_id"] == 63){
                    if(empty($image1)){                            
                        $from = strpos($html, "__STARTL1__");
                        $to = strpos($html, "__ENDL1__");
                        if(!empty($from) && !empty($to))
                        $html =  str_replace(substr($html,$from,($to-$from)+8+strlen($i)),"",$html);  
                        }
                    if(empty($image2)){                            
                        $from = strpos($html, "__STARTL2__");
                        $to = strpos($html, "__ENDL2__");
                        if(!empty($from) && !empty($to))
                        $html =  str_replace(substr($html,$from,($to-$from)+8+strlen($i)),"",$html);  
                        }
                    if(empty($image3)){                            
                        $from = strpos($html, "__STARTL3__");
                        $to = strpos($html, "__ENDL3__");
                        if(!empty($from) && !empty($to))
                        $html =  str_replace(substr($html,$from,($to-$from)+8+strlen($i)),"",$html);  
                        }
                    if(empty($image4)){                            
                        $from = strpos($html, "__STARTL4__");
                        $to = strpos($html, "__ENDL4__");
                        $html =  str_replace(substr($html,$from,($to-$from)+8+strlen($i)),"",$html);  
                        }
                    if(empty($image5)){                            
                        $from = strpos($html, "__STARTL5__");
                        $to = strpos($html, "__ENDL5__");
                        $html =  str_replace(substr($html,$from,($to-$from)+8+strlen($i)),"",$html);  
                        }
                        
                    if(empty($image6)){                            
                        $from = strpos($html, "__STARTM6__");
                        $to = strpos($html, "__ENDM6__");
                        if(!empty($from) && !empty($to))
                        $html =  str_replace(substr($html,$from,($to-$from)+9+strlen($i)),"",$html);  
                    }
                    for($i=1;$i<= 6;$i++){               
                        $html = str_replace("__STARTL".$i."__","", $html);
                    }
                    for($i=1;$i<= 6;$i++){
                        $html = str_replace("__ENDL".$i."__","", $html);
                    }
                }
            }

       //  if(!empty($_POST['form_id'])){
       //      if($_POST["form_id"] == 63){
       //          if(empty($image1)){                            
       //              $from = strpos($html, "__STARTM1__");
       //              $to = strpos($html, "__ENDM1__");
       //              $html =  str_replace(substr($html,$from,($to-$from)+9+strlen($i)),"",$html);  
       //              }
       //          if(empty($image2)){                            
       //              $from = strpos($html, "__STARTM2__");
       //              $to = strpos($html, "__ENDM2__");
       //              $html =  str_replace(substr($html,$from,($to-$from)+9+strlen($i)),"",$html);  
       //              }
       //          if(empty($image3)){                            
       //              $from = strpos($html, "__STARTM3__");
       //              $to = strpos($html, "__ENDM3__");
       //              $html =  str_replace(substr($html,$from,($to-$from)+9+strlen($i)),"",$html);  
       //              }
       //          if(empty($image4)){                            
       //              $from = strpos($html, "__STARTM4__");
       //              $to = strpos($html, "__ENDM4__");
       //              $html =  str_replace(substr($html,$from,($to-$from)+9+strlen($i)),"",$html);  
       //              }
       //          if(empty($image5)){                            
       //              $from = strpos($html, "__STARTM5__");
       //              $to = strpos($html, "__ENDM5__");
       //              $html =  str_replace(substr($html,$from,($to-$from)+9+strlen($i)),"",$html);  
       //              }
       //          for($i=1;$i<= 5;$i++){               
       //              $html = str_replace("__STARTM".$i."__","", $html);
       //          }
       //          for($i=1;$i<= 5;$i++){
       //              $html = str_replace("__ENDM".$i."__","", $html);
       //          }
       //     }
       // }
            
            
            if(!empty($_POST['form_id'])){
                if($_POST["form_id"] == 35 || $_POST["form_id"] == 39 || $_POST["form_id"] == 40 || $_POST["form_id"] == 52 || $_POST["form_id"] == 54 || $_POST["form_id"] == 55 || $_POST["form_id"] == 56 || $_POST["form_id"] == 43 || $_POST["form_id"] == 44 || ($_POST["form_id"] >= 81 && $_POST["form_id"] <= 88)){
                    if(empty($image1)){                            
                        $from = strpos($html, "__START1__");
                        $to = strpos($html, "__END1__");
                        if(!empty($from) && !empty($to))
                        $html =  str_replace(substr($html,$from,($to-$from)+8+strlen($i)),"",$html);  
                        }
                        
                    if(empty($image2)){                            
                        $from = strpos($html, "__START2__");
                        $to = strpos($html, "__END2__");
                        if(!empty($from) && !empty($to))
                        $html =  str_replace(substr($html,$from,($to-$from)+8+strlen($i)),"",$html);  
                        }
                    if(empty($image3)){                            
                        $from = strpos($html, "__START3__");
                        $to = strpos($html, "__END3__");
                        if(!empty($from) && !empty($to))
                        $html =  str_replace(substr($html,$from,($to-$from)+8+strlen($i)),"",$html);  
                        }
                    if(empty($image4)){                            
                        $from = strpos($html, "__START4__");
                        $to = strpos($html, "__END4__");
                        if(!empty($from) && !empty($to))
                        $html =  str_replace(substr($html,$from,($to-$from)+8+strlen($i)),"",$html);  
                        }
                    if(empty($image5)){                            
                        $from = strpos($html, "__START5__");
                        $to = strpos($html, "__END5__");
                        if(!empty($from) && !empty($to))
                        $html =  str_replace(substr($html,$from,($to-$from)+8+strlen($i)),"",$html);  
                        }
                    if(empty($image6)){                            
                        $from = strpos($html, "__STARTM6__");
                        $to = strpos($html, "__ENDM6__");
                        if(!empty($from) && !empty($to))
                        $html =  str_replace(substr($html,$from,($to-$from)+9+strlen($i)),"",$html);  
                    } 
                    for($i=1;$i<= 6;$i++){               
                        $html = str_replace("__START".$i."__","", $html);
                    }
                    for($i=1;$i<= 6;$i++){
                        $html = str_replace("__END".$i."__","", $html);
                    }
                }
            }
            
 	
//        }
//        else{
//            for($i=1;$i<= 5;$i++){
// 		if(isset($_FILES["image".$i]["name"]) && !empty($_FILES["image".$i]["name"])){
// 	
// 			$imagefilename[$i]  = mt_rand(0, 1000).time().'_'.$_FILES["image".$i]["name"];
// 			move_uploaded_file($_FILES["image".$i]["tmp_name"], $imagefilename[$i]);
// 			$deletionArray[] = $imagefilename[$i];
// 	
// 	
// 	
// 			$varList[] = "__image".$i."__";
// 			$valueList[] = $imagefilename[$i];
// 	
// 			$start = "__STARTM".$i."__";
// 			$end = "__ENDM".$i."__";
// 	
// 			$html =  str_replace(array($start,$end),array("",""),$html);
// 	
// 			// $serialLineHTML = $serialLineHTML.$fileNameArray[$i-1]."<br/>";
// 	
// 	
// 		} else {
// 	
// 			$from = strpos($html, "__STARTM".$i."__");
// 			$to = strpos($html, "__ENDM".$i."__");
// 	
// 			$html =  str_replace(substr($html,$from,($to-$from)+8+strlen($i)),"",$html);
// 	
// 		}
// 	}
//        
//        }
// 	replace serial lines
 	//$html =  str_replace('__SERIAL_LINES__',$serialLineHTML,$html);
// 	
// 	//
// 	
// 	require("../common_file.php");
// 	
//    } else {
//           $url =  "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."RTG-med-COLLECTIONS-cra.html";
//           header("Location:".$url);
//    }
 //}
            
            
?>