<?php	ob_start();

    include_once 'includes/session.php';
    include_once 'includes/constants.php';
    include_once 'includes/functions.php';

    $partners = new partners;
    $partners->connection($host,$user,$pass,$db);

    /**************************variables***************************************/
    $login		=trim($_POST['login']);       //login email id
    $flag		=trim($_POST['flag']);         //differentiate merchant and affiliate
	$today            = date("Y-m-d");
    /**************************************************************************/



    /***********************checking type of login*****************************/
    if  ($flag=='affiliate')
          $type='a';                          //affilaite login
    else
         $type='m';                           //merchant login
    /**************************************************************************/

     $sql        ="SELECT * FROM partners_login where login_email='$login'  and login_flag='$type'";
     $result     =mysql_query($sql);
     echo mysql_error();

    if(mysql_num_rows($result)>0)
    {
            $row    =mysql_fetch_object($result);
            $mailid	=$login;
            $pwd	=$row->login_password;
			$loginId = $row->login_id;
			
			if($type == 'a')
			{
				$sql_aff="select * from partners_affiliate where affiliate_id='$loginId'";
				$res_aff=mysql_query($sql_aff);
				// echo $sql1;
				if(mysql_num_rows($res_aff)>0)
				{
					$rows             =mysql_fetch_object($res_aff);
					$firstname        =stripslashes($rows->affiliate_firstname);
					$lastname         =stripslashes($rows->affiliate_lastname);
					$company          =stripslashes($rows->affiliate_company);
					$loginlink        =stripslashes($rows->affiliate_url);
				}
			} 
			else if($type == 'm')
			{
				$sql_mer1 = "SELECT * FROM partners_merchant WHERE merchant_id='$loginId'";
				$res_mer1 = mysql_query($sql_mer1);
				if(mysql_num_rows($res_mer1) > 0)
				{
					$row_mer1 = mysql_fetch_object($res_mer1);
					$mer_firstname        =stripslashes($row_mer1->merchant_firstname);
					$mer_lastname         =stripslashes($row_mer1->merchant_lastname);
					$mer_company          =stripslashes($row_mer1->merchant_company);
					$mer_loginlink        =stripslashes($row_mer1->merchant_url);
				}
			}
			
             
			        $sql="select * from partners_admin";
                    $ret1=mysql_query($sql);
                    $row=mysql_fetch_object($ret1);  //common header and footer
                    $adminheader=stripslashes($row->admin_mailheader);
                    $adminfooter=stripslashes($row->admin_mailfooter);
					$admin_email	 =    stripslashes($row->admin_email);

					if($type == 'a')
					{
					   $sql_event="select * from partners_adminmail where adminmail_eventname='Affiliate Remember Password' ";
					} 
					else if($type == 'm'){
						$sql_event="select * from partners_adminmail where adminmail_eventname='Merchant Remember Password' ";
					}
                    $result1=mysql_query($sql_event);

                   if(mysql_num_rows($result1)>0)
                   {

                      $row=mysql_fetch_object($result1);
                      $sub           =stripslashes($row->adminmail_subject);
                      $message       =stripslashes($row->adminmail_message);
                      $head          =stripslashes($row->adminmail_header);
                      $footer        =stripslashes($row->adminmail_footer);
                      $from          =stripslashes($row->adminmail_from);
                      //$toaddress  = $to;
                      $subject        = $sub;
                  }



                    $to=$mailid;
                    //$sub="Password Change Notification Mail";

                    $headers        =  "Content-Type: text/html; charset=iso-8859-1\n";
                    $headers       .=  "From: $from\n";
					$headers		=str_replace("[from]",$admin_email,$headers); 
                    $body="<table border ='0' cellpadding='0' cellspacing='0' width='90%' >";
                    $body.="<tr>";
                    $body.="<td width='100%' align='center' valign='top'><br/>";
                    $body.="<table border='0' cellpadding='0' cellspacing='0'  width='80%'>";
                    $body.="<tr>";
                    $body.="<td  width='100%' align='center' bgcolor='#000000'>&nbsp;</td>";
                    $body.="</tr>";
                    $body.="<tr>";
                    $body.="<td  width='100%' align='center'> $adminheader</td>";
                    $body.="</tr>";
                    $body.="<tr>";
                    $body.="<td  width='100%' align='left'> $head</td>";
                    $body.="</tr>";

                    $body.="<tr>";
                    $body.="<td  width='100%' align='left'>&nbsp;</td>";
                    $body.="</tr>";
                    $body.="<tr>";
                    $body.="<td width='100%' align='left'>$message";
                    $body.="</td></tr>";

                      $body.="<tr>";
                    $body.="<td width='100%' align='left'>Your Password is $pwd";
                    $body.="</td></tr>";
                    $body.="<tr>";
                    $body.="<td  width='100%' align='left'>&nbsp;</td>";
                    $body.="</tr>";
                    $body.="<tr>";
                    $body.="<td  width='100%' align='left'>$footer</td>";
                    $body.="</tr>";
                    $body.="<tr>";
                    $body.="<td  width='100%' align='center'>$adminfooter</td>";
                    $body.="</tr>";
                    $body.="<tr>";
                    $body.="<td  width='100%' align='left'  bgcolor='#000000'>&nbsp;</td>";
                    $body.="</tr>";
                    $body.="</table>";
                    $body.="</td>";
                    $body.="</tr>";
                    $body.="</table>";
					
					//Replace variable in the content with values
                     $body=str_replace("[aff_firstname]",$firstname,$body);
                     $body=str_replace("[aff_lastname]",$lastname,$body);
                     $body=str_replace("[aff_company]",$company,$body);
                     $body=str_replace("[aff_email]",$affemail,$body);
                     $body=str_replace("[aff_loginlink]",$loginlink,$body);
                     $body=str_replace("[aff_password]",$pwd,$body);
                     
                     $body=str_replace("[mer_firstname]",$mer_firstname,$body);
                     $body=str_replace("[mer_lastname]",$mer_lastname,$body);
                     $body=str_replace("[mer_company]",$mer_company,$body);
                     $body=str_replace("[mer_email]",$mer_mail,$body);
                     $body=str_replace("[mer_loginlink]",$mer_loginlink,$body);
                     $body=str_replace("[mer_password]",$pwd,$body);
					 
					 $body=str_replace("[from]",$admin_email,$body);
                     $body=str_replace("[today]",$today,$body);
					
                 mail($to,$subject,$body,$headers);
                 $mailok="Password Change Notification Mail Has been Send \n";
                 $mailok .= " To :".$to." \n";
                 $mailok.=" From :".$from;

                $Err="Your Password has been send to your Email Id!!!! ";
            }
    else
           {
            $Err="Invalid Id";
           }
       header("location:forgotpass.php?Err=$Err&Action=$flag");
?>