<?php

function idlanding_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'webform_client_form_4' || $form_id == 'webform_client_form_7' || $form_id == 'webform_client_form_9' || $form_id == 'webform_client_form_12' || $form_id == 'webform_client_form_13' || $form_id == 'webform_client_form_20' || $form_id == 'webform_client_form_22' || $form_id == 'webform_client_form_21' || $form_id == 'webform_client_form_19' || $form_id == 'webform_client_form_17') {
    $form['#submit'][]= 'idlanding_webform_submit';
	$form['#validate'][]= 'idlanding_webform_validate';
	
  }
}

function idlanding_webform_validate($form, &$form_state){
	//Ensure at least one phone number was submitted
	if ($form_state['values']['submitted']['cell_phone'] == "" && 
		$form_state['values']['submitted']['daytime_phone'] == "" && 
		$form_state['values']['submitted']['evening_phone'] == "")
	{
		return form_set_error('',t('Please enter at least one phone number.'));
	}
	
	if ($form_state['values']['submitted']['cell_phone'] != "" && !_idlanding_format_phone($form_state['values']['submitted']['cell_phone']))
	{
		return form_set_error('',t('Please enter a valid Cell Phone number in the format of ###-###-####'));
	}
	if ($form_state['values']['submitted']['daytime_phone'] != "" && !_idlanding_format_phone($form_state['values']['submitted']['daytime_phone']))
	{
		return form_set_error('',t('Please enter a valid Daytime Phone number in the format of ###-###-####'));
	}
	if ($form_state['values']['submitted']['evening_phone'] != "" && !_idlanding_format_phone($form_state['values']['submitted']['evening_phone']))
	{
		return form_set_error('',t('Please enter a valid Cell Phone number in the format of ###-###-####'));
	}
	if ($form_state['values']['submitted']['state'] == "NONE")
	{
		return form_set_error('',t('State field is required.'));
	}
	if ($form_state['values']['submitted']['category'] == "NONE")
	{
		return form_set_error('',t('Please select the category your product or patent falls into.'));
	}
	if ($form_state['values']['submitted']['patent_status'] == "NONE")
	{
		return form_set_error('',t('Please tell us if your currently hold a patent for your product.'));
	}
	if ($form_state['values']['submitted']['research_report'] == "NONE")
	{
		return form_set_error('',t('Please tell us if you have a preliminary research report for your product or patent.'));
	}
	if ($form_state['values']['submitted']['trade_shows'] == "NONE")
	{
		return form_set_error('',t('Please tell us if you have attended any industry trade shows with your product or patent.'));
	}
	
}

function idlanding_webform_submit($form_values) {
  require_once('DrupalRESTWebServices.php');
  
    $api = new DrupalRESTWebServices('http://database.innovationdirectonline.com/my_test', 'rkent', 'hello', FALSE);
    $node = array (
                'name' => 'rkent',
                'status' => 1,
                'type' => 'contact',
				'field_status' => array('und' =>array('key' => '1')),
                'field_first_name' => array('und' => array(array('value' => $form_values['submitted']['first_name']['#value']))),
                'field_last_name' => array('und' => array(array('value' => $form_values['submitted']['last_name']['#value']))),
                'field_cell_phone' => array('und' => array(array('value' => _idlanding_format_phone($form_values['submitted']['cell_phone']['#value'])))),
				'field_daytime_phone' => array('und' => array(array('value' => _idlanding_format_phone($form_values['submitted']['daytime_phone']['#value'])))),
				'field_evening_phone' => array('und' => array(array('value' => _idlanding_format_phone($form_values['submitted']['evening_phone']['#value'])))),
                'field_address' => array('und' => array(array('value' => $form_values['submitted']['address']['#value']))),
                'field_city'  => array('und' => array(array('value' => $form_values['submitted']['city']['#value']))),
                'field_state' => array('und' => array(array('value' => $form_values['submitted']['state']['#value']))),
                'field_zip' => array('und' => array(array('value' => $form_values['submitted']['zip']['#value']))),
                'field_email' => array('und' => array(array('value' => $form_values['submitted']['email']['#value']))),
                'field_lead_source' => array('und' => array(array('value' => $form_values['submitted']['source']['#value']))),
				'field_google_keyword' => array('und' => array(array('value' => $form_values['submitted']['keyword']['#value']))),
				'field_from_ip' => array('und' => array(array('value' => filter_var($_SERVER['REMOTE_ADDR'], FILTER_SANITIZE_STRING)))),
				'field_imi_category' => array('und' => array(array('value' => $form_values['submitted']['category']['#value']))),
				'field_imi_patent' => array('und' => array(array('value' => $form_values['submitted']['patent_status']['#value']))),
				'field_research_report' => array('und' => array(array('value' => $form_values['submitted']['research_report']['#value']))),
				'field_attended_tradeshows' => array('und' => array(array('value' => $form_values['submitted']['trade_shows']['#value']))),
    );
    $api->login();
    $api->createNode(array('node' => $node));

	//We'll compose and send the email from here since attachment capability isn't fully formed in either the Mimemail 
	//or HTMLmail Drupal 7 modules
		
	//Relies on the Mail and Mime_mail PEAR packages
	
	$subject = 'Your Free Copy of "Secrets Of Selling Or Licensing Your New Product Or Patent"';
	//$to = '"'.$form_values['submitted']['first_name']['#value'].' '.$form_values['submitted']['last_name']['#value'].'" <'.$form_values['submitted']['email']['#value'].'>';
	$to = $form_values['submitted']['email']['#value'];
	$from = '"Innovation Direct" <info@innovationdirectonline.com>';
	$path_of_uploaded_file = '/home/ittlicen/sites/database/sites/land.innovationdirectonline.com/files/webform/The Secrets of Selling or Licensing Your New Product or Patent.pdf';
	$path_of_uploaded_file2 = '/home/ittlicen/sites/database/sites/land.innovationdirectonline.com/files/webform/Innovation Direct Confidential Product Outline.pdf';
	$text = "Greetings ".$form_values['submitted']['first_name']['#value']." ".$form_values['submitted']['last_name']['#value'].",

Thank you for having requested our literature on the world of patent and invention licensing, the The Secrets Of Selling Or Licensing Your New Product or Patent. This information is attached to this e-mail communication. 

We have also attached a copy of our Confidential Product Outline. This document allows you to provide Innovation Direct™ with the pertinent details about your new product, under a guarantee of complete confidentiality. When complete, please fax the Confidential Product Outline via toll-free 1-855-797-0691 or 305-255-4515. 

Alternatively, you may e-mail the document to us through submissions@innovationdirectonline.com  or upload it through our website directly via http://submit.innovationdirectonline.com.  Please type in A0000009 as a temporary file number when prompted. 

We are also sending you out a printed copy of The Secrets Of Selling Or Licensing Your New Product or Patent, along with other materials about Innovation Direct™ and our product management and trade show representation services, via first class mail on the next business day. 

For more information about our company please visit our official website, www.innovationdirectonline.com.  On our site, you will find details on previously licensed products, information and photos from previously attended trade shows, and our Innovation Direct™ Blog. 

We look forward to being able to work further with you on your new product!

-The Innovation Direct Team";
	set_include_path('.:/home/ittlicen/php');
	include_once('Mail.php');
	include_once('Mail/mime.php');
	$message = new Mail_mime();
 
	$message->setTXTBody($text);
 
	$message->addAttachment($path_of_uploaded_file);
	$message->addAttachment($path_of_uploaded_file2);
 
	$body = $message->get();
 
	$extraheaders = array("From"=>$from, "Subject"=>$subject);
 
	$headers = $message->headers($extraheaders);
 
	$mail = Mail::factory("mail");
 
	$mail->send($to, $headers, $body);
}

function _idlanding_format_phone($phone)
{
	$phone = preg_replace("/[^0-9]/", "", $phone);
	if(strlen($phone) == 10)
		return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "$1-$2-$3", $phone);
	else
		return false;
}
?>