<?php
include("../includes/connect.php");

$chiavi = array_keys($_POST['values']);

// di base considero valido l'input, specificare le validazioni da eseguire tramite IF
$valid = true;

if($chiavi[0] == "postcode")
{
	$valid = false;
	// verifica cap
$postcode = $_POST['values']['postcode'];
$comuni = $connection->query("SELECT locality FROM geopc_places WHERE postcode='$postcode'");

if($comuni->rowCount() > 0)
{
	$valid = true;
}
else
{
?>
{"success":false,"results":{"postcode":{"isValid":false,"message":"No Postecode resulting."}},"errors":{"postcode":"No Postecode resulting."}}
<?php
}
}
else if($chiavi[0] == "email")
{
	$valid = false;
	// controllo duplicati
	
	$email = $_POST['values']['email'];
	$eid = explode("-",$_POST['externalId']);
	$cid = $eid[1];
	$tabellaLeads = "campaign" . $cid . "_leads";
	
	$checkEmail =  $connection->prepare("SELECT email FROM $tabellaLeads WHERE email=?");
	$checkEmail->execute([$email]);
	$quante = $checkEmail->fetchAll();
	if(count($quante) == 0)
	{
		$valid = true;
	}
	else
	{
	    if($cid<65){?>
?>

{"success":false,"results":{"email":{"isValid":false,"message":"Questa email partecipa già al concorso!"}},"errors":{"email":"Questa email partecipa già al concorso!"}}



<? }else{
    ?>
    {"success":false,"results":{"email":{"isValid":false,"message":"This E-mail has already been used! If you have forgotten your password, you can retrieve it from the Login panel on the Homepage! Click here to get it now."}}
    ,"errors":{"email":"This E-mail has already been used! If you have forgotten your password, you can retrieve it from the Login panel on the Homepage! Click here to get it now."}}



    <?
} 
    
	
	    
	}


}
else if($chiavi[0] == "phone")
{
	$valid = false;
	// controllo duplicati
	
	$cell = $_POST['values']['phone'];
	$eid = explode("-",$_POST['externalId']);
	$cid = $eid[1];
	$tabellaLeads = "campaign" . $cid . "_leads";
	
	$checkEmail =  $connection->prepare("SELECT cellulare FROM $tabellaLeads WHERE cellulare=?");
	$checkEmail->execute([$cell]);
	$quante = $checkEmail->fetchAll();
	if(count($quante) == 0)
	{
		$valid = true;
	}
	else
	{
        if(($cid<65)){?>
{"success":false,"results":{"email":{"isValid":false,"message":"Questo cellulare partecipa già al concorso"}},"errors":{"phone":"partecipa già al concorso"}}
<?php		
	    }else{$valid=true;}
	    
	}


}
if($valid == true)
{
?>
{"success":true,"results":{"<?php echo $chiavi[0]; ?>":{"isValid":true}}}
<?php	
}
else
{
?>

<?php	
}
?>

