<?php
include "chkuser.php";
include "includefilemng.php";
	
	$edit=false;
	$edit_id=0;
	$proceed_flag=0;
	$id=0;
	$franname=''; 
	$fname='';
	$lname='';
	$address='';
	$city='';
	$postcode='';
	$l_phone='';
	$m_phone='';
	$email='';
	$uname='';
	$pass='';
	$conpass='';	
	$update_id="id";	
	$insert_primary_table="franchise";	
	$mendatry_check_array=array('franname','fname','lname','email','postcode','uname','pass','conpass'); 
	$unset_array=array('savesend','save','edit_id','id','conpass','fid');
	$validate_check_array=array(
		                            'unique'=>array( 
		                                      	 array('name'=>'uname',
												  'tablename'=>'franchise',
												  'tablefield'=>'uname',
												  'updateidmatch'=>'id',	
												  'error'=>'User name already exist.')),	
							        'numeric'=>array(
								                    
											         array('name'=>'postcode',
													 'error'=>'Post code must be numeric.')),	
							        'email'=>array(
											       array('name'=>'email',
												   'error'=>'Enter Valid email address.')),
												   
							        'match'=>array('name'=>'pass',
									               'name2'=>'conpass',
									               'error'=>'Password and Confirm Password must be same.')
							                );		
							
	if(isset($_GET['fid'])){
	  $edit_id=$_GET['fid'];
	  $edit=true;	  
	  }
  
     if($edit){  
		$pdelre =array(); 
		$pdelr = "select * from ".$insert_primary_table." where  ".$update_id." ='$edit_id'";		
		//echo $pdelr;
		$pdelq = mysql_query($pdelr);
		$pdelre = mysql_fetch_array($pdelq);
		extract ($pdelre);		
      }


if(count($_POST)){
$temp_data=$post_data=$_POST;
$edit_id=$post_data['edit_id'];
$proceed_flag=0;
$errormsg='';
list($proceed_flag,$errormsg)=validate_form_field($mendatry_check_array,$validate_check_array,$post_data,$edit_id);
	 
	if($proceed_flag < 1)
	{		
	
	foreach($unset_array as $key=>$value)
		unset($post_data[$value]);

			 foreach($post_data as $k=>$v){			
			 $sql_data_array[$k]=stripslashes(htmlentities($v,ENT_QUOTES));			  
			}
					 
		
		if($edit_id>0){
		
			$updatecond=$update_id."='$edit_id'";
			tep_db_perform($insert_primary_table, $sql_data_array,"update",$updatecond);		
			$errormsg = " Record updated";
			//-------------------------
			$franname=''; 
			$fname='';
			$lname='';
			$address='';
			$city='';
			$postcode='';
			$l_phone='';
			$m_phone='';
			$email='';
			$uname='';
			$pass='';
			$conpass='';
			?>	
			<META HTTP-EQUIV=REFRESH CONTENT="0;URL=view_franchise.php">
			
            <?php
            //----------------------
		}else{	
		    
			 //-------------------------------------------
		   if(isset($_POST['save'])){	
		   			
			tep_db_perform($insert_primary_table, $sql_data_array,"insert");		
			$id=mysql_insert_id();
			$errormsg = " Record inserted";
			//-------------------------
			$franname=''; 
			$fname='';
			$lname='';
			$address='';
			$city='';
			$postcode='';
			$l_phone='';
			$m_phone='';
			$email='';
			$uname='';
			$pass='';
			$conpass='';	
			//----------------------
			}
			 if(isset($_POST['savesend'])){	
			 
			 $fname=$sql_data_array['fname'];
			 $lname=$sql_data_array['lname'];
			 $username=$sql_data_array['uname'];
			 $password=$sql_data_array['pass'];
			 $mail_to=$sql_data_array['email'];
			 $urllink="http://www.ourdemourl.com/perfectlyclean/franchise_account.php";
			    
				$headers="";
				$description="";				
				$sql = "select sendemailid,siteurl,site_name from admin";
				$result = mysql_query($sql);
				$data_admin = mysql_fetch_object($result);
				$sendemailid=$data_admin->sendemailid;
				$siteurl=$data_admin->siteurl;
				$sitename=$data_admin->site_name; 
			 
			$description.="Dear ".$fname."&nbsp;".$lname."<br/>";
			$description.="Welcome to www.perfectlymaid.com.au<br/>";
			$description.="Here is the franchise details :<br/><br/>";
			$description.="Username : ".$username."<br/>";
			$description.="Password : ".$password."<br/><br/>";
			$description.="<a href=\"$urllink\">http://www.ourdemourl.com/perfectlyclean/franchise_account.php</a><br/>";
			
			 
			    
							
			
		    tep_db_perform($insert_primary_table, $sql_data_array,"insert");		
			$id=mysql_insert_id();
			$errormsg = " Record inserted";
			//-------------------------
			    $mail_subject="Mail from www.perfectlymaid.com.au";				
				$mail_body = "<table style=font:Verdana; font-size:11px; color:#000000><tr><td>";
				$mail_body .= $description;
				$mail_body .= "</td></tr></table>";
				$headers =  "Content-Type: text/html;charset=iso-8859-1\n";
				$headers = $headers . "From:".$sendemailid."";
				mail($mail_to,$mail_subject,$mail_body,$headers);				
				//--------------------------------------------
	        $description="";
			$franname=''; 
			$fname='';
			$lname='';
			$address='';
			$city='';
			$postcode='';
			$l_phone='';
			$m_phone='';
			$email='';
			$uname='';
			$pass='';
			$conpass='';	
			//----------------------
			 }
			
			
		}	
	}
	else
	{
	$haderr="yes";
	$errormsg = $errormsg;
	extract($temp_data);
	}


}

?>
<html>
<head>
<title>control panel </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="ssi.css" rel="stylesheet" type="text/css">
<script language="javascript" src="includenp/functions.js"></script>
<style type="text/css">
<!--
.style2 {color: #000000}
-->
</style>
</head>
<body>
<table width="98%" border="0" cellspacing="0" cellpadding="2" align="center">
  <tr>
    <td width="1%"   bgcolor="#C9C9C9"></td>
	<td width="98%"><?php include_once "top.php";?></td>
	<td width="1%"   bgcolor="#C9C9C9"></td>
  </tr>
  <tr>
   <td width="1%"   bgcolor="#C9C9C9"></td>
    <td width="98%"><table width="100%"  border="0" cellpadding="0" cellspacing="0" class="centertable">
      <tr>
        <td><table width="85%"  border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td height="40" class="bigblackTxt">&nbsp;<a href="view_franchise.php" class="normalLink">Manage</a>  :: <a href="franchise_settings.php" class="normalLink">Settings</a></td>
          </tr>
          <tr>
            <td><?php 
		  if(isset($errormsg))
		  	{echo display_error_msg($errormsg);
		   	}
			?>            </td>
          </tr>
          <tr>
            <td>
            
              <table width="100%"  border="0" align="center" cellpadding="0" cellspacing="4">
                <form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>" name="SaveForm" ID="SaveForm" enctype="multipart/form-data" >
                 <input type="hidden" name="edit_id" value="<?php  echo $edit_id;?>">
				
                  <tr>
                    <td colspan="2" class="bigblackTxt"><?php if($edit==true){ echo "Edit";} else { echo "New";}?> Franchise</td>
                  </tr>
				  <tr>
                    <td height="24" colspan="2" class="errFont"> * marked fields are mandatory </td>
                  </tr>
				  <tr>
                    <td colspan="2" class="normalBoldblackTxt">&nbsp;</td>
                  </tr>                 
                  <tr>
                    <td width="26%" class="bluebold"><span class="bluebold"> <span class="errFont">*</span>Franchise Name </span>: </td>
                    <td width="74%"><input name="franname" type="text" class="inputBOX" id="franname" value="<?php echo $franname;?>"></td>
                  </tr>
                   <tr>
                     <td height="25" colspan="2" align="left" valign="middle" class="bluebold">Franchise Contact details</td>
                     </tr>
                   <tr>
                     <td class="bluebold"><span class="errFont">*</span>First Name :</td>
                     <td><input name="fname" type="text" class="inputBOX" id="fname" value="<?php echo $fname;?>"></td>
                   </tr>
                   <tr>
                     <td class="bluebold"><span class="errFont">*</span>Last Name :</td>
                     <td><input name="lname" type="text" class="inputBOX" id="lname" value="<?php echo $lname;?>"></td>
                   </tr>
                   <tr>
                     <td class="bluebold">Address :</td>
                     <td>
                 <textarea name="address" cols="45" rows="5" class="inputBOX" id="address"><?php echo $address;?></textarea></td>
                   </tr>
                   <tr>
                     <td class="bluebold">City :</td>
                     <td><input name="city" type="text" class="inputBOX" id="city" value="<?php echo $city;?>"></td>
                   </tr>
                   <tr>
                     <td class="bluebold"><span class="errFont">*</span>Post code :</td>
                     <td><input name="postcode" type="text" class="inputBOX" id="postcode" value="<?php echo $postcode;?>"></td>
                   </tr>
                   <tr>
                     <td class="bluebold">Phone :</td>
                     <td><input name="l_phone" type="text" class="inputBOX" id="l_phone" value="<?php echo $l_phone;?>"></td>
                   </tr>
                   <tr>
                     <td class="bluebold">Mobile :</td>
                     <td><input name="m_phone" type="text" class="inputBOX" id="m_phone" value="<?php echo $m_phone;?>"></td>
                   </tr>
                   <tr>
                     <td class="bluebold"><span class="errFont">*</span>Email Address :</td>
                     <td><input name="email" type="text" class="inputBOX" id="email" value="<?php echo $email;?>"></td>
                   </tr>
                   <tr>
                     <td class="bluebold"><span class="errFont">*</span>User Name :</td>
                     <td><input name="uname" type="text" class="inputBOX" id="uname" value="<?php echo $uname;?>"></td>
                   </tr>
                   <tr>
                     <td class="bluebold"><span class="errFont">*</span>Password :</td>
                     <td><input name="pass" type="password" class="inputBOX" id="pass" value="<?php echo $pass;?>"></td>
                   </tr>
                   <tr>
                     <td class="bluebold"><span class="errFont">*</span>Confirm Password :</td>
                     <td><input name="conpass" type="password" class="inputBOX" id="conpass" value="<?php if($edit_id>0) echo $pass; else echo $conpass;?>"></td>
                   </tr>
				
				  <tr>
                  <td class="bluebold">&nbsp;</td>
				    <td class="bluebold"><input type="hidden" name="fid" id="fid" value="<?php echo $_GET['fid'];?>"></td>
				    </tr>
				  <tr>
                    <td width="26%" class="bluebold">&nbsp;</td>
                    <td width="74%">                    
                      <input name="save" type="submit" class="hbutton" id="save" value="<?php if($edit_id>0) echo "SAVE"; else echo "SAVE";?>"></td>
                  </tr>
				  <tr>
                    <td align="right">&nbsp;</td>
                    <td>                     
                     <input name="savesend" type="submit" class="hbutton" id="savesend" value="<?php if($edit_id>0) echo "SAVE&SEND"; else echo "SAVE&SEND";?>">
                      </td>
                  </tr>
                  <tr>
                    <td colspan="2">&nbsp;</td>
                  </tr>
                </form>
            </table></td>
          </tr>
        </table></td>
      </tr>
    </table></td>
    <td width="1%"   bgcolor="#C9C9C9"></td>
  </tr>
  <tr>
    <td   bgcolor="#C9C9C9" width="1%"></td>
	<td><?php include_once "footer.php";?></td>
	<td width="1%"   bgcolor="#C9C9C9"></td>
  </tr>
</table>
</body>
</html>
