<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Programmer<programmer@armia.com>        		              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems, Inc and iScripts.com � 2005                |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
?>
<?php
include "../includes/config.php";
include "../includes/session.php";
include "../includes/functions.php";

if (get_magic_quotes_gpc()) {
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE); 
} 

if(isset( $_SESSION["sess_adminname"]) and $_SESSION["sess_adminname"]!= ""){
	$adminname = $_SESSION["sess_adminname"];
}else{
	header("Location:login.php");
	exit;
}
if (isset($_GET["affiliateid"]) and $_GET["affiliateid"] != "") {
    $affiliateid = $_GET["affiliateid"];
} else if (isset($_POST["affiliateid"]) and $_POST["affiliateid"] != "") {
    $affiliateid = $_POST["affiliateid"];
} else{
	header("Location:affiliates.php");
    exit;
}

$txtUserName = trim($_POST["txtUserName"]);
$txtFirstName = trim($_POST["txtFirstName"]);
$txtLastName = trim($_POST["txtLastName"]);
$txtAddress1 = trim($_POST["txtAddress1"]);
$txtAddress2 = trim($_POST["txtAddress2"]);
$txtCity = trim($_POST["txtCity"]);
$txtState = trim($_POST["txtState"]);
if(!$txtState)
$txtState = trim($_POST["bill_state"]);
$ddlCountry = trim($_POST["ddlCountry"]);
$txtZIP = trim($_POST["txtZIP"]);
$txtEmail = trim($_POST["txtEmail"]);
$txtPhone = trim($_POST["txtPhone"]);
$txtFAX = trim($_POST["txtFAX"]);
$txtPassword = trim($_POST["txtPassword"]);
$txtConfirmPassword = trim($_POST["txtConfirmPassword"]);

$txtNewPassword = trim($_POST["txtNewPassword"]);
$chkNotify = trim($_POST["chkNotify"]);
$txtTaxId = trim($_POST["txtTaxId"]);

if (isset($_POST["btnSaveChanges"])) {
    $message = "" ;
    if (!isNotNull($txtFirstName)) {
        $message .= E_FNAME." <br>";
    } 
	/*if(isNotNull($txtFirstName))
	{
	
	 if(!preg_match("/^[a-zA-Z ]+$/", $txtFirstName))
	 { 
	   $message .= "* First name accepts alphabets only !! <br>";
	 }
	}*/
	if (!isNotNull($txtLastName)) {
        $message .= E_LNAME." <br>";
    }
	/*if(isNotNull($txtLastName))
	{
	 if(!preg_match("/^[a-zA-Z ]+$/", $txtLastName))
	 { 
	   $message .= "* Last name accepts alphabets only !! <br>";
	 }
	}*/
    if (!isNotNull($txtAddress1)) {
        $message .= E_ADDRESS." <br>";
    } 
    if (!isNotNull($txtState)) {
        $message .= E_STATE." <br>";
    } 
	if (!isNotNull($txtCity)) {
        $message .= E_CITY." <br>";
    }
		/*if(isNotNull($txtZIP))
	{ 
		if(strlen($txtZIP)!=5 && $ddlCountry=="US")
		{
		 $message .= "* Enter a valid Zipcode!<br>";
		}
	} */
	if (!isNotNull($txtPhone)) {
        $message .= E_PHONE." <br>";
    } 
	if(isNotNull($txtPhone))
	{ 
		if(!check_phone($txtPhone,$ddlCountry))
		{
		 $message .= INVALID_PHONE;
		}
	}
	
    if (!isNotNull($txtEmail)) {
        $message .= E_EMAIL." <br>";
    } else {
        if (!isValidEmail($txtEmail)) {
            $message .= INVALID_EMAIL." <br>";
        } 
    } 

    $qry2 = "select email from  " . $tableprefix . "affiliates where email='" . addslashes($txtEmail) . "' AND affiliate_id <> '" . addslashes($affiliateid) . "' ";
    if (mysql_num_rows(mysql_query($qry2)) > 0) {
        $message .= EMAIL_IN_USE." <br>";
    } 

    if ($message != "") { // error
        $message = "<br>".MSG_CORRECT_THE_ERRORS."<br>" . $message;
    } else { // no error so insert user details
        $sql = "UPDATE " . $tableprefix . "affiliates SET 
			first_name = '" . addslashes($txtFirstName) . "',
			last_name = '" . addslashes($txtLastName) . "',
			address1 = '" . addslashes($txtAddress1) . "',
			address2 = '" . addslashes($txtAddress2) . "',
			city = '" . addslashes($txtCity) . "',
			state = '" . addslashes($txtState) . "',
			country = '" . addslashes($ddlCountry) . "',
			phone = '" . addslashes($txtPhone) . "',
			fax = '" . addslashes($txtFAX) . "',
			zip = '" . addslashes($txtZIP) . "',
			email = '" . addslashes($txtEmail) . "',
			taxid = '" . addslashes($txtTaxId) . "'
			WHERE affiliate_id =  '" . addslashes($affiliateid) . "'
			";
        mysql_query($sql);
        $success_message = PROFILE_UPDATED;
    } 
} else if (isset($_POST["btnResetPassword"])) {
    if (!isNotNull($txtNewPassword)) {
        $message .= NEW_PWD_EMPTY." <br>";
    } 

    if ($message != "") { // error
        $message = "<br>".MSG_CORRECT_THE_ERRORS."<br>" . $message;
    } else { // no error so insert user details
        $sql = "UPDATE " . $tableprefix . "affiliates SET 
			password = '" . addslashes(md5($txtNewPassword)) . "'
			WHERE affiliate_id =  '" . addslashes($affiliateid) . "'
			";
         mysql_query($sql);
        if ($chkNotify == "on") {
            $sql = "SELECT email,first_name,last_name,affiliate_name FROM " . $tableprefix . "affiliates WHERE affiliate_id = '" . addslashes($affiliateid) . "' ";
            $result = mysql_query($sql);
            if (mysql_num_rows($result) != 0) {
                $row = mysql_fetch_array($result);
                $email = $row["email"];
                $firstname = $row["first_name"];
                $lastname = $row["last_name"];
                $affiliatename = $row["affiliate_name"];
            } 

            $affiliatefullname = $firstname;
            if ($lastname != "") {
                $affiliatefullname .= " " . $lastname;
            } 
            $to = $email;
            $subject = MAIL_AFFILIATE_ACCOUNT_SUBJECT_1." " . stripslashes(SITE_NAME) . " ".MAIL_AFFILIATE_ACCOUNT_SUBJECT_2;
			
/*----------Multicart 2.0 Upgradation---------------*/				

			$mailmessage = readEmailTemplate('change_affiliatepassword');
			
			$mailmessage = str_replace('[name]',$affiliatefullname,$mailmessage);
			$mailmessage = str_replace('[user_name]',$affiliatename,$mailmessage);
			$mailmessage = str_replace('[password]',$txtNewPassword,$mailmessage);
			
			$mailmessage = nl2br($mailmessage);
			
			$mail_send_details = "<html><head><link href='".SITE_URL."/style/template1-admin.css' rel='stylesheet' type='text/css'></head><body><center><div align='left' id='layout'><div align='left' class='header'><div align='left' class='logo'><img src=".$logofolder."/".$logoimage." width='125' height='39'/></div></div><div>".$mailmessage."</div></div><div align='left' class='clear_float'><img src=".$imagefolder."/clear.gif width=1 height=1/></div></div></div></center></body></html>";
						
			$headers  = "MIME-Version: 1.0" . "\r\n";
			$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
			$headers .= "From: ".SITE_EMAIL."<".SITE_EMAIL.">" . "\r\n";
			$mailsent = @mail($to, $subject, $mail_send_details, $headers);
				
/*----------Multicart 2.0 Upgradation---------------*/	
            
            $updatepass = true;
        } else {
            $updatepass = true;
        } 

        if ($updatepass) {
            mysql_query($sql);
            $success_message = PWD_UPD;
        } else {
            $message = UNK_ERROR;
        } 
    } 
} 
$sql = "SELECT * FROM " . $tableprefix . "affiliates WHERE affiliate_id = '" . addslashes($affiliateid) . "' ";
$result = mysql_query($sql);
if (mysql_num_rows($result) != 0) {
    $row = mysql_fetch_array($result);
	$txtUserName = $row["affiliate_name"];
    $txtFirstName = $row["first_name"];
    $txtLastName = $row["last_name"];
    $txtAddress1 = $row["address1"];
    $txtAddress2 = $row["address2"];
    $txtCity = $row["city"];
    $txtState = $row["state"];
    $ddlCountry = $row["country"];
    $txtPhone = $row["phone"];
    $txtFAX = $row["fax"];
    $txtZIP = $row["zip"];
    $txtEmail = $row["email"];
	$txtTaxId = $row["taxid"];
}

$ddlCountry = $ddlCountry == ""?"US":$ddlCountry;
$countrylist = makeCountryList();
include "../includes/htmltop.php";
?>


<body>
<center>
		
		<div align="left" id="layout">
		
		
				
				
				<!--header area start -->
<?php

	include_once("../includes/topheader_admin.php")

?>
				
				<!--header area end -->
				
				
				
				
				<div align="left" class="main_body">
				<!--main body start -->
				
				
				
<?php
		include_once("../includes/admintopmenu.php")
?>
	
	<form name="frmProfile" action="<?php echo $_SERVER["PHP_SELF"]?>" method="POST">
	<input type="hidden" name="affiliateid" value="<?php echo htmlentities($affiliateid);?>">
	<!-- The id of the country field(s).  If more than one, seperate with spaces -->
	<input type="hidden" value="billCountrySelect" name="cs_config_country_field" id="cs_config_country_field">
	<input type="hidden" value="billStateSelect" name="cs_config_state_field"   id="cs_config_state_field">
						
						
	<!-- The id of the the fields holding the default values.  If more than one, seperate with spaces -->
	<input type="hidden" value="billCountryDefault" name="cs_config_country_default" id="cs_config_country_default">
	<input type="hidden" value="billStateDefault"   name="cs_config_state_default"   id="cs_config_state_default">
	<!-- The actual default values -->
	<input type="hidden" value="<?php echo $ddlCountry;?>" name="billCountryDefault" id="billCountryDefault">
	<input type="hidden" value="<?php echo $txtState;?>"   name="billStateDefault"   id="billStateDefault">
	<SCRIPT type="text/javascript" SRC="../js/country-state.js"></SCRIPT>
	<div align="left" class="content_area">
	<!--content area start -->
				
	<div align="left" class="page_title"><?php echo EDIT_AFFLIATE;?></div>
	<div align="center" class="text_information grey_tr"><?php echo $message?></div>
         <div align="center" class="text_information_success grey_tr"><?php echo $success_message; ?></div>
    <br>
							
	<div align="left">
	
	<table width="100%" border="0" cellpadding="0" cellspacing="0" class="manage_table">
	<tr>
		<td colspan="3" align="right"><span class="required">*</span><?php echo MANDATORY_FIELDS;?></td>
	</tr>
																											
	<tr>
		<td colspan="3">
			<fieldset name="affProfile">
				<legend class='page_title2'><?php echo PERSON_DETAILS;?></legend>
				<table cellpadding="2" cellspacing="2" width="100%">
					<tr>
					  <td align="right" ><?php echo UNAME;?> </td>
					  <td >&nbsp;</td>
					  <td align="left" >
				      <input class="textarea" type="text" value="<?php echo (stripslashes($txtUserName)); ?>" name="txtUserName" size="30" maxlength="100" readonly="readonly"></td>
				  </tr>
					<tr><td width="42%" align="right" ><?php echo FNAME;?>&nbsp;<span class="required">*</span></td>
					<td width="2%" >:</td>
					<td width="56%" align="left" ><input class="textarea" type="text" value="<?php echo (stripslashes($txtFirstName)); ?>" name="txtFirstName" size="30" maxlength="100"></td></tr>
					<tr><td align="right" ><?php echo LNAME;?> &nbsp; <span class="required">*</span></td>
					<td >:</td>
					<td align="left" ><input  class="textarea" type="text" value="<?php echo (stripslashes($txtLastName));?>" name="txtLastName" size="30" maxlength="100"></td></tr>
					<tr><td align="right" ><?php echo ADDR_LINE1;?>&nbsp; <span class="required">*</span></td>
					<td >:</td>
					<td align="left" ><input  class="textarea" type="text" value="<?php echo (stripslashes($txtAddress1));?>" name="txtAddress1" size="40" maxlength="100"></td></tr>
					<tr><td align="right" ><?php echo ADDR_LINE2;?></td>
					<td >:</td>
					<td align="left" ><input type="text"  class="textarea" value="<?php echo (stripslashes($txtAddress2));?>" name="txtAddress2" size="40" maxlength="100"></td></tr>
					<tr><td align="right" ><?php echo CITY;?>&nbsp;<span class="required">*</span></td>
					<td align="left" >:</td>
					<td align="left"><input class="textarea"  type="text" value="<?php echo (stripslashes($txtCity));?>" name="txtCity" size="30" maxlength="100"></td></tr>
					<tr><td align="right" ><?php echo COUNTRY;?>&nbsp;<span class="required">*</span></td>
					<td >:</td>
					<td align="left" >
						<select id='billCountrySelect'  class='textarea' name='ddlCountry' onchange='updateState(this.id)'>
     				   </select>
					<tr>
					  <td align="right" ><?php echo STATE;?>&nbsp;<span class="required">*</span></td>
					  <td align="left" >:</td>
					  <td align="left" ><select id='billStateSelect' class='textarea' name='txtState'>
                        </select>
                          <SCRIPT  type="text/javascript">initCountry(); </SCRIPT></td>
				  </tr>
					<tr><td align="right" ><?php echo ZIP;?></td>
					<td >:</td>
					<td align="left" ><input type="text"  class="textarea" value="<?php echo htmlentities(stripslashes($txtZIP));?>" name="txtZIP" size="30" maxlength="20"></td></tr>
					<tr><td align="right" ><?php echo PHONE;?></td>
					<td >:</td>
					<td align="left" ><input type="text"  class="textarea" value="<?php echo htmlentities(stripslashes($txtPhone));?>" name="txtPhone" size="30" maxlength="100"></td></tr>
					<tr><td align="right" ><?php echo FAX;?></td>
					<td >:</td>
					<td align="left" ><input type="text"  class="textarea" value="<?php echo htmlentities(stripslashes($txtFAX));?>" name="txtFAX" size="30" maxlength="100"></td></tr>
					<tr><td align="right" ><?php echo EMAIL;?>&nbsp;<span class="required">*</span></td>
					<td >:</td>
					<td align="left" ><input type="text"  class="textarea" value="<?php echo htmlentities(stripslashes($txtEmail));?>" name="txtEmail" size="30" maxlength="100"></td></tr>
					<tr><td align="right" ><?php echo TAX_ID;?></td>
					<td >:</td>
					<td align="left" ><input type="text"  class="textarea" value="<?php echo htmlentities(stripslashes($txtTaxId));?>" name="txtTaxId" size="30" maxlength="100"></td></tr>
					<tr><td colspan="3" align="center"><input type="submit" value="<?php echo BTN_SAVE_CHANGES;?>" name="btnSaveChanges" class="button"></td></tr>
				</table>
			</fieldset>
		</td>
	</tr>
	
	<tr>
		<td colspan="3">
			<fieldset name="affChangePass">
				<legend  class='page_title2'><?php echo CHANGE_PASSWORD;?></legend>
				<table cellpadding="2" cellspacing="2" width="100%">
					<tr><td width="42%" align="right" ><?php echo NEW_PWD;?>&nbsp;<span class="required">*</span></td>
					<td width="2%" >:</td>
					<td width="56%" align="left"><input type="password" value=""  class="textarea"  name="txtNewPassword" size="30" maxlength="100"></td></tr>
					<tr><td align="right" ><?php echo NOTIFY_EMAIL;?>&nbsp;</td>
					<td >:</td>
					<td align="left"><input  class="checkbox" type="checkbox" checked  name="chkNotify" ></td></tr>
					<tr><td colspan="3"></tr>
					<tr><td colspan="3" align="center"><input type="submit" value="<?php echo RST_PWD;?>" name="btnResetPassword"  class="button" ></td></tr>
				</table>
			</fieldset>
		</td>
	</tr>
	<tr><td colspan="3"></tr>
	<tr>
		<td colspan="3">
			<fieldset name="affOtherDetails">
				<legend  class='page_title2'><?php echo OTHER_DET;?></legend>
				<table cellpadding="2" cellspacing="2" width="100%">
					<?php
					$sql = "SELECT count(affiliate_id) AS regcnt FROM " . $tableprefix . "affiliate_registrations WHERE affiliate_id = '" . addslashes($affiliateid) . "' ";
					$result = mysql_query($sql);
					if (mysql_num_rows($result) != 0) {
						$rw = mysql_fetch_array($result);
						$regcnt = $rw["regcnt"];
					}
					$sql = "SELECT count(affiliate_id) AS regcntnp FROM " . $tableprefix . "affiliate_registrations WHERE affiliate_id = '" . addslashes($affiliateid) . "' AND affiliate_payment_id IS NULL  ";
					$result = mysql_query($sql);
					if (mysql_num_rows($result) != 0) {
						$rw = mysql_fetch_array($result);
						$regcntnp = $rw["regcntnp"];
					}
					$sql = "SELECT count(affiliate_id) AS paycnt FROM " . $tableprefix . "affiliate_payments WHERE affiliate_id = '" . addslashes($affiliateid) . "' ";
					$result = mysql_query($sql);
					if (mysql_num_rows($result) != 0) {
						$rw = mysql_fetch_array($result);
						$paycnt = $rw["paycnt"];
					}
					
					?>
					<tr><td align="left" ><a class=links href='settleaffregistrations.php?aid=<?php echo htmlentities($affiliateid); ?>' ><b><?php echo SETTLE_AFF_REG;?>&nbsp;(<?php echo $regcntnp;?>)</b></a></td><td >&nbsp;</td><td ></td></tr>
					<tr><td align="left" ><a class=links href='viewaffregistrations.php?aid=<?php echo htmlentities($affiliateid); ?>' ><b><?php echo VIEW_AFF_REG;?>&nbsp;(<?php echo $regcnt;?>)</b></a></td><td >&nbsp;</td><td ></td></tr>
					<tr><td align="left" ><a class=links href='affpayments.php?aid=<?php echo htmlentities($affiliateid); ?>' ><b><?php echo VIEW_EDIT_AFF_PAY;?>&nbsp;(<?php echo $paycnt;?>)</b></a></td><td >&nbsp;</td><td ></td></tr>
					<tr><td colspan="3">&nbsp;</td></tr>
				</table>
			</fieldset>
		</td>
	</tr>
	<tr><td colspan="3"></tr>
	<tr><td colspan="3" align="center"><input type="button" name="btnBack" value="<?php echo BACK_AFF_BTN;?>"  class="button"  onClick="window.location.href='affiliates.php';" ></tr>
</table>
</div>

<!--
	
		Multicart 2.0 Design Integration 

-->											
				<!--content area end-->
	</div>
	</form>							
				
				
					<div align="left" class="clear_float"><img src="../images/clear.gif" alt="" width="1" height="1" /></div>
				<!--main body end -->
				
				
<?php
		
	include "../includes/footer.php";
?>
																									