	<?php
		date_default_timezone_set("Pacific/Auckland");

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }
  
  	include("configi.php");


  $theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($conn, $theValue) : mysqli_escape_string($conn, $theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

	 include 'configi.php';	

	$tagsprodcont		=  GetSQLValueString($_REQUEST['tagsprodcont'], "text");
	
	$name_array = explode(' ',trim($_REQUEST['tagsprodcont']));
	$first_name = $name_array[0]; // will print Test
 
	$first_name		=  GetSQLValueString($first_name, "text");


	$querydup="SELECT * from ".$_COOKIE['phxcrmclient_id']."_client where comp_name = ".$tagsprodcont;
			$found = "no";
		
	//	echo $querydup;
		$resultdup=mysqli_query($conn, $querydup);
	


		while($querydup=mysqli_fetch_array($resultdup)) { 

			$uid_client			= $querydup['uid_client'];
			$found = "yes";
			
		}

//echo "found is ".$found;
	
	if ($found == "yes"){
		

		
		$outputarray = array('phx_result' => 'duplicate','new_id' => 0);
		$output2 = json_encode($outputarray);
		echo $output2;

		exit;	
		
	}else{	
		
			$curdate = date("Y-m-d H:i:s");
	

		$insclient="insert into ".$_COOKIE['phxcrmclient_id']."_client(comp_name, cont_name, status, c_supp, c_cust_since)	values (".$tagsprodcont.",".$first_name.",'Current','Yes','".$curdate."')";
	
		//			echo $insclient;
		$resultinsclient=mysqli_query($conn, $insclient);	

		$new_id =  mysqli_insert_id($conn);
		
		$phx_result = "successful";
		
		$outputarray = array('phx_result' => $phx_result,'new_id' => $new_id);
		$output2 = json_encode($outputarray);
		echo $output2;

		exit;
	
	}
			
			?>