<?php

function idlanding_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'webform_client_form_1') {
    
	$form['submitted']['statement_of_authenticity_confidentiality']['#field_suffix'] = ", do hereby declare and assert that I am the
original inventor of the idea described herein. I hereby authorize Innovation Direct to provide me with their free initial product
review with no purchase required. I understand submitting my concept is not a release and that this information cannot be used, disclosed,
or sold without my expressed written permission. Further, I understand Innovation Direct cannot be held responsible for the return
of voluntarily submitted materials (i.e.: photos, drawings, prototypes) including this Confidential Product Outline. I further understand
that all phone calls with Innovation Direct will be monitored and recorded for quality assurance, training & promotional purposes
and I consent to use. I further understand that the signing of this Statement of Authenticity/Confidentiality in no way guarantees 
that my product is unique nor that the signing of this statement means that my idea is patented.";
	
    $form['#submit'][]= 'idlanding_webform_submit';
	$form['#validate'][]= 'idlanding_webform_main_validate';
  }
  if ($form_id == 'webform_client_form_2') {
	//Removed for now
	//$form['#validate'][] = 'idlanding_webform_validate'; 
  
  }
}

function idlanding_webform_main_validate($form,&$form_values) {
	if ($form_values['values']['submitted']['questionnaire']['other_companies'] == 'yes') {
		if ($form_values['values']['submitted']['questionnaire']['other_companies_details'] == '') {
			form_set_error('other_companies_details', t('You have indicated that you have worked with other companies on this product. Please offer us some information about the other companies you have worked with.'));
		}
	}
	if ($form_values['values']['submitted']['questionnaire']['already_patented'] == 'yes') {
		if ($form_values['values']['submitted']['questionnaire']['patent_number'] == '') {
			form_set_error('patent_number', t('You have indicated that you have patented this product. Please tell us what the patent number is.'));
		}
		if ($form_values['values']['submitted']['questionnaire']['been_licensed'] == '') {
			form_set_error('', t('You have indicated that you have patented this product. Please let us know if you have licensed this product.'));
		}
	}
	if ($form_values['values']['submitted']['questionnaire']['been_licensed'] == 'yes') {
		if ($form_values['values']['submitted']['questionnaire']['been_licensed_comment'] == '') {
			form_set_error('', t('You have indicated that your product has been licensed. Please indicate to whom the product was licensed in the comment.'));
		}
	}
}

function idlanding_webform_validate($form,&$form_values) {
	if ($form_values['values']['submitted']['file_'] != 'A0000009') {
    form_set_error('', t('We\'re sorry, but your file number is not valid. If you need a file number, please contact Innovation Direct at 1-877-991-0909'));
	}
}

function idlanding_webform_submit($form_values) {
  require_once('DrupalRESTWebServices.php');
 
	//$file_uri = file_load($form_values['submitted']['questionnaire']['upload']['_fid']['#value']);
	//$file_url = file_create_url($file_uri);
	
    $api = new DrupalRESTWebServices('http://database.innovationdirectonline.com/my_test', 'rkent', 'hello', FALSE);
    $node = array (
                'name' => 'rkent',
                'status' => 1,
                'type' => 'confidential_product_outline',
				'field_status' => array('und' =>array('key' => '1')),
                'field_name' => array('und' => array(array('value' => $form_values['submitted']['contact_information']['name']['#value']))),
                'field_address' => array('und' => array(array('value' => $form_values['submitted']['contact_information']['address']['#value']))),
                'field_phone_number' => array('und' => array(array('value' => $form_values['submitted']['contact_information']['main_phone']['#value']))),
				'field_phone_2' => array('und' => array(array('value' => $form_values['submitted']['contact_information']['work_phone']['#value']))),
				'field_phone_3' => array('und' => array(array('value' => $form_values['submitted']['contact_information']['cell_phone']['#value']))),
                'field_city'  => array('und' => array(array('value' => $form_values['submitted']['contact_information']['city']['#value']))),
                'field_state' => array('und' => array(array('value' => $form_values['submitted']['contact_information']['state']['#value']))),
                'field_zip' => array('und' => array(array('value' => $form_values['submitted']['contact_information']['zip']['#value']))),
                'field_email' => array('und' => array(array('value' => $form_values['submitted']['contact_information']['email']['#value']))),
                'field_lead_source' => array('und' => array(array('value' => 'CPO Form'))),
				'field_new_product_representative' => array('und' => array(array('value' => $form_values['submitted']['contact_information']['new_product_representative']['#value']))),
				'field_conceive' => array('und' => array(array('value' => $form_values['submitted']['questionnaire']['concieve']['#value']))),
				'field_prototype' => array('und' => array(array('value' => $form_values['submitted']['questionnaire']['prototype']['#value']))),
				'field_category' => array('und' => array(array('value' => $form_values['submitted']['questionnaire']['category']['#value']))),
				'field_function' => array('und' => array(array('value' => $form_values['submitted']['questionnaire']['function']['#value']))),
				'field_materials' => array('und' => array(array('value' => $form_values['submitted']['questionnaire']['materials']['#value']))),
				'field_aware' => array('und' => array(array('value' => $form_values['submitted']['questionnaire']['aware']['#value']))),
				'field_price' => array('und' => array(array('value' => $form_values['submitted']['questionnaire']['price']['#value']))),
				'field_stores' => array('und' => array(array('value' => $form_values['submitted']['questionnaire']['stores']['#value']))),
				'field_inventory' => array('und' => array(array('value' => $form_values['submitted']['questionnaire']['inventory']['#value']))),
				'field_website' => array('und' => array(array('value' => $form_values['submitted']['questionnaire']['website']['#value']))),
				'field_other_companies' => array('und' => array(array('value' => $form_values['submitted']['questionnaire']['other_companies']['#value']))),
				'field_other_companies_details' => array('und' => array(array('value' => $form_values['submitted']['questionnaire']['other_companies_details']['#value']))),
				'field_already_patented' => array('und' => array(array('value' => $form_values['submitted']['questionnaire']['already_patented']['#value']))),
				'field_patent_number' => array('und' => array(array('value' => $form_values['submitted']['questionnaire']['patent_number']['#value']))),
				'field_been_licensed' => array('und' => array(array('value' => $form_values['submitted']['questionnaire']['been_licensed']['#value']))),
				'field_been_licensed_comment' => array('und' => array(array('value' => $form_values['submitted']['questionnaire']['been_licensed_comment']['#value']))),
				'field_patent_outside_us' => array('und' => array(array('value' => $form_values['submitted']['questionnaire']['patent_outside_us']['#value']))),
				//'field_upload' => array('und' => array(array('value' => $file_url))),
    );
    $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 to 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/TheSecretsWebPDF.pdf';
	$text = "Thanks ".$form_values['submitted']['first_name']['#value']." ".$form_values['submitted']['last_name']['#value']."!

As you requested, we have sent to you free of charge our informational booklet \"Learn the Secrets to Selling or Licensing Your New Product or Patent\". With this information you'll be better prepared for the existing market and learn more about why companies are interested in products or patents just like yours.

Additionally, we will be sending information about our company Innovation Direct to your home. This introductory packet will describe our various product licensing services and provide you with a means to contact us if you are interested in product or patent marketing assistance through our company.

Thanks Again, And We Hope To Hear From You Soon!

Signed,

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);
 
	$body = $message->get();
 
	$extraheaders = array("From"=>$from, "Subject"=>$subject);
 
	$headers = $message->headers($extraheaders);
 
	$mail = Mail::factory("mail");
 
	$mail->send($to, $headers, $body);
*/
	
	
	
}

?>