<?php

	include "admin/inc/configure.php";
	include "admin/inc/connect.php";
	include "admin/inc/sysparam.php";

	$f_name=trim($_GET["name"]);
	$email=trim($_GET["from"]);
	$search_engine=trim($_GET['search_engine']);
	$key_word=trim($_GET['key_word']);
	$surl=trim($_GET['surl']);
	$id=trim($_GET['id']);
	$id1=trim($_GET['id1']);

	$l_name=trim($_GET['last_name']);
	$address=trim($_GET['address']);
	$city=trim($_GET['city']);
	$state=trim($_GET['state']);
	$zip=trim($_GET['zip']);
    $phone=trim($_GET['phone']);
	$country=trim($_GET['country']);
	$cap_inv=trim($_GET['capital_inv']);
	$cap_from=substr($cap_inv,0,strpos($cap_inv, "_"));
	$cap_to=substr($cap_inv,strpos($cap_inv, "_")+1);
	$cap_invD = "$".number_format($cap_from)." - $".number_format($cap_to);
	$industry1=trim($_GET['industry1']);
	$comment=trim($_GET['comment']);

	$sql1="select * from closing_tip where emailid = '".$email."'";
    $result1=mysql_query($sql1,$link);
    $numrows=mysql_num_rows($result1);
	if ($numrows>0) {
	    $row1=mysql_fetch_object($result1);
        header("Location: thankyou_duplicate.php?id=".$row1->id."&country=".$row1->country."&cap_from=".$row1->cap_from);
		exit;
    }

	
	
	$entdate=date("d/m/Y,  g:i a"); 
	$today = date("Y-m-d");
	$tmp_url=$HTTP_REFERER;
	$pieces = explode("/", $tmp_url);
	$tot_num=count($pieces);
	$page_name=$pieces[$tot_num-1];
	$sysdate1 = date("d/m/Y");


	$ip = $_SERVER['REMOTE_ADDR'];

	$query="Insert Into closing_tip(first_name, last_name, address, city, state, country, zipcode, phone, emailid, comment,template, page, ldate, adate, search_engine, key_word, surl, cap_from, cap_to, ip, industry1) values ('$f_name', '$l_name', '$address',  '$city', '$state', '$country', '$zip', '$phone', '$email','$comment', '$tmp_url', '$page_name', '$entdate', '$today', '$search_engine', '$key_word', '$surl', '$cap_from','$cap_to', '$ip', '$industry1')";

	$result=mysql_query($query, $link);
	//Take closing_tip last ID inserted in the above insert statement to be passed to pAGE 2 registration thankyou2reg.php
	$temp_array = mysql_fetch_array(mysql_query("select last_insert_id()")); 
    $clos_id = $temp_array['last_insert_id()']; 
    
	$convertion="Converted"; 
	$query2="Update java_reg set conversion='$convertion' where id =".$id1;
		  $result2=mysql_query($query2, $link);

	$query1="Update convetion_check set convertion='$convertion' where id=".$id;
	if($surl){
	  $result1=mysql_query($query1, $link);
	  //$result2=mysql_query($query2, $link);
	}

	if (!$result) {
       die('Invalid query: ' . mysql_error());
	}else{
		if ($sendthankyou_email==1) {
			$query1="Select * from thankyoum where country='".$country."'";
			$result1=mysql_query($query1, $link);
			$row1=mysql_fetch_object($result1);
			$s_name =  $row1->s_name;
			$s_email = $row1->s_emailid;
			$subject_db = $row1->subject;
			$email_body = $row1->email_body;

			 /* recipients */
			$to  = $email; 
			
			/* subject */
			$subject = $f_name.' - '.$subject_db;

			/* message */
			$query1="Select  headtext_mail from sysparam ";
			$result1=mysql_query($query1, $link);
			$row1=mysql_fetch_object($result1);
			$headtext_mail = $row1->headtext_mail;
				//echo $headtext_mail ;
					$email_body=str_replace("%headtextmail%", "$headtext_mail", $email_body);

			$email_body=str_replace("%fname%", "$f_name", $email_body);
			$email_body=str_replace("%lname%", "$l_name", $email_body);
			$email_body=str_replace("%address1%", "$address", $email_body);
			$email_body=str_replace("%city%", "$city", $email_body);
			$email_body=str_replace("%state%", "$state", $email_body);
			$email_body=str_replace("%zip%", "$zip", $email_body);
			$email_body=str_replace("%email%", "$email", $email_body);
			$email_body=str_replace("%phone%", "$phone", $email_body);
			$email_body=str_replace("%country%", "$country", $email_body);
			$email_body=str_replace("%capital_to_invest%", "$cap_invD", $email_body);


			$message = 'Dear '.$f_name.',<br><br><pre>'.$email_body.'</pre>';

			/* To send HTML mail, you can set the Content-type header. */
			$headers  = "MIME-Version: 1.0\r\n";
			$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

			/* additional headers */
			$headers .= "To: $f_name <$email>\r\n";
			$headers .= "From: $s_name <$s_email>\r\n";

			/* and now mail it */
		   $sent=mail($to, $subject, $message, $headers);
		}
		//For the condition $country="United States" and cap_inv >= $page2_amt redirect to registration PAGE 2 and then to thankyou page
		// no Prepop URL shown
	   if ($us_ind==1){ 
		   $tpage2url="thankyou2reg.php?country=".$country."&id=".$clos_id."&cap_from=".$cap_from."&cap_to=".$cap_to;
           header("Location: $tpage2url"); 
	   }
		
	  $query3="Select * from prepopurl where country='".$country."'" ;

	  $result3=mysql_query($query3, $link);
	  $nrows3=mysql_num_rows($result3);
	  if($nrows3<=0){	
	 	 $query3="Select * from prepopurl where country='".$country."'";

		 $result3=mysql_query($query3, $link);
		 $nrows3=mysql_num_rows($result3);
	   }
	   $row3=mysql_fetch_object($result3);
	   $prepopurl = $row3->prepopurl;
	   $on_off = $row3->on_off;
	   $prepopurl1 = $row3->prepopurl1;
	   $prepopurl2 = $row3->prepopurl2;
	   $prepopurl3 = $row3->prepopurl3;
	   $prepopurl4 = $row3->prepopurl4;
	   $prepopurl5 = $row3->prepopurl5;
	   $fraon_off = $row3->fraon_off;
	}

	if($nrows3>0 && $fraon_off==1 && $us_ind==0){	
	    $tpageurl="frameprepop.php?id=".$clos_id;
        header("Location: $tpageurl"); 

/*		$pos=strpos($prepopurl,"starting-small-business.us");

		if ($pos > 0) {
			$query1="Update closing_tip set othersite_code = 2 where id='".$clos_id."'" ;
		    $result1=mysql_query($query1, $link);
		}
		
		if ($prepopurl1<>"") {		
			$prepopurl1=str_replace("%fname%", "$f_name", $prepopurl1);
			$prepopurl1=str_replace("%lname%", "$l_name", $prepopurl1);
			$prepopurl1=str_replace("%address1%", "$address", $prepopurl1);
			$prepopurl1=str_replace("%city%", "$city", $prepopurl1);
			$prepopurl1=str_replace("%state%", "$state", $prepopurl1);
			$prepopurl1=str_replace("%zip%", "$zip", $prepopurl1);
			$prepopurl1=str_replace("%email%", "$email", $prepopurl1);
			$prepopurl1=str_replace("%phone%", "$phone", $prepopurl1);
			$prepopurl1=str_replace("%country%", "$country", $prepopurl1);
			$prepopurl1=str_replace("%capital_to_invest%", "$cap_inv", $prepopurl1);

			$pos=strpos($prepopurl1,"thankyoupage.php");
			if ($pos > 0 ) {
			   $prepopurl1=$prepopurl1."?country=".$country."&cap_from=".$cap_from;
			} */

	?>
<!--		<FRAMESET COLS="25%, 50%, *">

		<FRAME SRC=<?php echo $prepopurl1; ?>>

		</FRAMESET> -->
	<?php
		//	header("Location: $prepopurl"); 
//		}	
	} 
	if($nrows3>0 && $on_off==1 && $us_ind==0){	
		$pos=strpos($prepopurl,"starting-small-business.us");

		if ($pos > 0) {
			$query1="Update closing_tip set othersite_code = 2 where id='".$clos_id."'" ;
		    $result1=mysql_query($query1, $link);
		}
		
		$prepopurl=str_replace("%fname%", "$f_name", $prepopurl);
		$prepopurl=str_replace("%lname%", "$l_name", $prepopurl);
		$prepopurl=str_replace("%address1%", "$address", $prepopurl);
		$prepopurl=str_replace("%city%", "$city", $prepopurl);
		$prepopurl=str_replace("%state%", "$state", $prepopurl);
		$prepopurl=str_replace("%zip%", "$zip", $prepopurl);
		$prepopurl=str_replace("%email%", "$email", $prepopurl);
		$prepopurl=str_replace("%phone%", "$phone", $prepopurl);
		$prepopurl=str_replace("%country%", "$country", $prepopurl);
		$prepopurl=str_replace("%capital_to_invest%", "$cap_inv", $prepopurl);

		$pos=strpos($prepopurl,"thankyoupage.php");
		if ($pos > 0 ) {
		   $prepopurl=$prepopurl."?country=".$country."&cap_from=".$cap_from;
		}
		header("Location:$prepopurl"); 
	
	}else if($country && $us_ind==0){
		   $tpageurl="thankyoupage.php?country=".$country."&cap_from=".$cap_from;	
		 
           header("Location: $tpageurl"); 

	}
	else{

?>
<html>
<head>
<title><?php echo $title; ?> Thankyou page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<LINK href="admin/images/<?php echo $css; ?>" type=text/css rel=stylesheet>

</head>

<?php include "header.php"; ?>
<table width="779" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
  
<TR >
    <TD vAlign=top align="center" background="img/bg.jpg">
            <a href="index.php">Home</a>
	</TD>
</TR>
</TR>
  <?php include "footer2.php"; ?>
	  <!-- Google Code for Lead Conversion Page -->
<script language="JavaScript" type="text/javascript">
<!--
var google_conversion_id = 1071333486;
var google_conversion_language = "en_US";
var google_conversion_format = "1";
var google_conversion_color = "FFFFFF";
if (1) {
  var google_conversion_value = 1;
}
var google_conversion_label = "Lead";
//-->
</script>
<script language="JavaScript"
src="http://www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<img height=1 width=1 border=0
src="http://www.googleadservices.com/pagead/conversion/1071333486/?value=1&label=Lead&script=0">
</noscript>

	  </td>
  </tr>
</table>

</body>
</html>
<?php
	}
?>
