<?php
//error_reporting(E_ALL);
	class WebinarView extends Slot {

	var $ModuleID = 13;
	var $SectionID = 13;
	var $flag = -1;	

	function __construct($pquery) {
		parent::__construct($pquery);
		$this->Cache = 0;
		$this->Passing = TRUE;
	}

	function init() { 
		global $ROOT_PATH;
		if(!empty($this->Request)) {
			if ($this->validateForm()) {
//--------SalesForce
			include_once($ROOT_PATH.'/CURL/class.curl.php');
			$webinars = '';
			foreach($this->Request as $key => $value) {
				if (strrpos($key, "webinar") !== false && $value ==1) {
					$webinars .= $this->Request['webinartitle'.substr($key, 7)].",";
				}
			}
			$webinars = substr($webinars, 0, -1);
			$fields = array(
					"oid" 		=>	"00D30000001HzLZ",
					"retURL" 	=>	"http://www.atncorp.com/",
					"debug" 	=>	0,
//					"debugEmail"    =>	"mail@gmail.com",
					"first_name"    =>	$this->Request["firstName"],
					"last_name"     =>	$this->Request["lastName"],
					"email"         =>	$this->Request["email"],     
					"company"       =>	$this->Request["company"],   
					"recordType"    =>	"012a0000000AQBl",
					"00Na0000002fyIC"    =>	$webinars,
			            );
			$post = http_build_query($fields);
			$c = new curl("https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8");
			$c->setopt(CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; msie6xpv1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)");
			$c->setopt(CURLOPT_POST, 1);
			$c->setopt(CURLOPT_POSTFIELDS, $post);
			$c->setopt(CURLOPT_VERBOSE, 0);
			$c->setopt(CURLOPT_HEADER, 1);
			$c->setopt(CURLOPT_SSL_VERIFYPEER, false);
			$c->setopt(CURLOPT_SSL_VERIFYHOST, false);
			$c->setopt(CURLOPT_RETURNTRANSFER, true);
			$result = $c->exec();
			$c->close();
//------SalesForce END
                			$this->Action->processEvent($this->SectionID,'sendwebinar',array($this->Request),$this) and
					header('Location: '.$this->Home.'/webinar/1'.$this->SID) and
					$this->flag=1 ;
				} else {
                			$this->Action->processEvent($this->SectionID,'sendnewsletter',array($this->Request),$this) and
					header('Location: '.$this->Home.'/webinar/1'.$this->SID) and
					$this->flag=1 ;
				  }
		}
		if (isset($this->Target[2]))  $this->Target[2] ==1?	$this->flag=1:	$this->flag=0; 
	}

	function fillElements() {
				if ($this->flag==1 ) { 
						$this->TempFillEls['thanks'][0] = array(); 	
				}
				elseif ($this->flag==0 ) {	
						$this->TempFillEls['sorry'][0] = array(); 	
						$this->TempFillEls['firstName'] = !empty(	$this->Request['firstName'])? $this->Request['firstName']: '';
						$this->TempFillEls['lastName'] = !empty(	$this->Request['lastName'])? $this->Request['lastName']: '';
						$this->TempFillEls['email'] = !empty(		$this->Request['email'])? $this->Request['email']: '';
						$this->TempFillEls['company'] = !empty(		$this->Request['company'])? $this->Request['company']: '';
				}
				else {
					$this->TempFillEls['describe'][0] = array();
					$this->TempFillEls['firstName'] = !empty(	$this->Request['firstName'])? $this->Request['firstName']: '';
					$this->TempFillEls['lastName'] = !empty(	$this->Request['lastName'])? $this->Request['lastName']: '';
					$this->TempFillEls['email'] = !empty(		$this->Request['email'])? $this->Request['email']: '';
					$this->TempFillEls['company'] = !empty(		$this->Request['company'])? $this->Request['company']: '';
				} 
		$path = $this->Home."/webinar/";
		$this->TempFillEls['callback_true']  = $path."1";
		$this->TempFillEls['callback_false'] = $path."2";

		if(count($this->errors) > 0) $this->errors['header'] = $this->globalLangContent['error_view']['header_error'];
		foreach($this->errors as $k => $v) 
				$this->TempFillEls[$k.'_error'] = '<strong class=star>'.$v.'</strong><p>' ;

	}
	
	function validateForm() {
		if( !validate(@$this->Request['firstName']) ||
			!validate(@$this->Request['lastName']) ||
			!is_Email(@$this->Request['email']) ) return FALSE;
		if(sizeof($this->errors) > 0) return FALSE;
		return TRUE;
	}
	
}
?>