<?
class mailMe{
	function mailMe() {
	global $ses;
	global $db;
	$this->db=$db;
	$this->ses=$ses;
	}


	function formFive($ar, $maxTeach=3){
	$fName=	$ar['mainForm_formName'];
	$fPhone=$ar['mainForm_formPhone'];
	$fEmail=$ar['mainForm_formEmail'];
	$fFname=$ar['mainForm_formfName'];
	$fNotes=$ar['mainForm_formNotes'];
	$fCityID=$ar['bizCity'];
	$ServID=$ar['mainForm_serviceType'];
	$ordBy='';
	$vars='ref_type , city_id , service_id , c_name , c_fName , c_phone , c_mail , c_notes';
	$vals='"1","'.$fCityID.'","'.$ServID.'","'.$fName.'","'.$fFname.'","'.$fPhone.'","'.$fEmail.'","'.$fNotes.'"';

		if($this->db->insert('referrals',$vars,$vals)){
		$newRefID = mysql_insert_id(); 
		$pks=$this->db->selectJ('*','teach_city_rel','teach_types_rel','teach_types_rel.teach_id=teach_city_rel.teach_id
		 AND  teach_types_rel.type_id='.$ServID.' ','','',' WHERE teach_city_rel.city_id=\''.$fCityID.'\' ',' ORDER BY 
		 teach_city_rel.teach_id');
	 
			if($pks!=false){
			$tIdList='';
				foreach($pks as $i => $v){
					if($i>0){$tIdList.=',';}
					$tIdList.=$pks[$i]['teach_id'];
				}
				$tArr=$this->db->select('*','teach','WHERE t_id IN('.$tIdList.') AND t_active=1 ',$ordBy);
				if($tArr==true){
					$numT=count($tArr);
					if($numT>$maxTeach){
					// random here	
					$newtArr =array();
						while(count($newtArr)<$maxTeach){
						$tmp=array_rand($tArr);
						array_push($newtArr,$tArr[$tmp]);
						unset($tArr[$tmp]);
						}
					}
					else{$newtArr=$tArr;}
					//send and insert to each teach
				$posCount=0;
				$negCount=0;
				$bugList='';
					foreach($newtArr as $ii=>$vv){
					$vars='teach_id , referral_id, city_id , service_id , c_name , c_fName , c_phone ,c_mail , c_notes';
					$vals='"'.$newtArr[$ii]['t_id'].'","'.$newRefID.'","'.$fCityID.'","'.$ServID.'","'.$fName.'"
					,"'.$fFname.'","'.$fPhone.'","'.$fEmail.'","'.$fNotes.'"';
						if($this->db->insert('teach_ref_rel',$vars,$vals)){$posCount++;}
						else{$negCount++; $bugList.='couldn\'t create teach_ref realtions between: teachID-'.$newtArr[$ii]['t_id'].' and refID-'.$newRefID.'<br>';
						}
					}
					if($negCount>0){
//////SEND Admin MAIL => bug adding referral to teacher////////////////////////////////////////////////////										
					}
				echo 'תודה על פנייתך, הפנייה הועברה לגורמים המתאימים<br>RishYon.co.il ';
				}
				else{
//////SEND Admin MAIL => notify me that there's uncovered areas
				echo 'תודה על פנייתך, נטפל בה בהקדם האפשרי ';
				}
			}
			else{
				echo 'תודה על פנייתך, אנו כבר מטפלים בה....(אין מורים)';}
		}
		else{echo 'ארעה שגיאה ולא הצלחתי לשמור את פנייתך<br><span class="redMust">אנא נסו לשלוח את הטופס שוב</span>';}
	//by serv ID decide what kind of services and users to look for
	//search all teach_city_rel WHERE city_id =cID	JOIN type_teach_rel WHERE =uID;
	//get uID list
	//if 5 or less send them all (foreach mail)
	//if more then 5 => randomize 5 users
	//foreach uID send mail
	}

	function formOne(){
		
	}	


	function getUserMail(){
		
	}
	
	function welcomeMail(){
		
	}

	function paymentConfirm(){
		
	}



	function checkMail($email){
		if(!filter_var($email, FILTER_VALIDATE_EMAIL)){return false;}
		else{
			if(preg_match('/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/',$email)){return true;}
			else {return false;}
		}
	}
	


}






?>