<?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["userid"]) and $_GET["userid"]!= ""){
	$userid = $_GET["userid"];
}else if(isset( $_POST["userid"]) and $_POST["userid"]!= ""){
	$userid = $_POST["userid"];
}

$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"]);
	
	
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($txtCity)) {
        $message .= E_CITY." <br>";
    }  
	if (!isNotNull($txtState)) {
        $message .= E_STATE." <br>";
    }
	if (!isNotNull($txtZIP)) {
        $message .= E_ZIP." <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>";
	    } 
	}
	
/*-----------Multicart 2.0 Upgradation Starts-------------*/	

	$sql_check_custom_data = "SELECT cf_value.nvalue_id,cf.vcustom_field_name   
							  FROM ".$tableprefix."custom_fields_value cf_value,".$tableprefix."custom_fields cf
							  WHERE cf_value.ncustom_field_id = cf.ncustom_id 
							  AND cf.vcustom_field_form_id = '1'
							  AND cf_value.ncustom_table_id = ".GetSQLValueString($userid,"text");		
	
	$result_check_custom_data = mysql_query($sql_check_custom_data) or die(mysql_error());
	
	if(mysql_num_rows($result_check_custom_data) > 0)
	{
	
		while($check_custom_data_row = mysql_fetch_array($result_check_custom_data))
		{
			
			$field_name_required = "txtFieldRequired_".$check_custom_data_row['nvalue_id'];
			$field_value_name 	 = "txtFieldValue_".$check_custom_data_row['nvalue_id'];
			
			if($_POST[$field_name_required] == "Y")
			{
			
				if (!isNotNull($_POST[$field_value_name]))
				{
					$message .= "* ".ucwords($check_custom_data_row['vcustom_field_name'])." cannot be empty! <br>";
				}
			
			}
						
		}
				
	}		
	
	
/*-----------Multicart 2.0 Upgradation Ends-------------*/	
	
	
	
    $qry2 = "select email from  " . $tableprefix . "users where email='" . addslashes($txtEmail) . "' AND user_id <> '".addslashes($userid)."' ";
    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."users 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)."'
			WHERE user_id =  '".addslashes($userid)."'
			";
		mysql_query($sql);
		
/*---------Multicart 2.0 Upgradation Starts------*/		
		
/*----------Custom Fields Data Updation----------*/		
		
	$sql_custom_data = "SELECT cf_value.nvalue_id   
						FROM ".$tableprefix."custom_fields_value cf_value,".$tableprefix."custom_fields cf
						WHERE cf_value.ncustom_field_id = cf.ncustom_id 
						AND cf.vcustom_field_form_id = '1'
						AND cf_value.ncustom_table_id = ".GetSQLValueString($userid,"text");		
	
	$result_custom_data = mysql_query($sql_custom_data) or die(mysql_error());
	
	if(mysql_num_rows($result_custom_data) > 0)
	{
	
		while($custom_data_row = mysql_fetch_array($result_custom_data))
		{
			
			$field_value_name = "txtFieldValue_".$custom_data_row['nvalue_id'];
						
			$update_custom_fields_value = "UPDATE ".$tableprefix."custom_fields_value 
										   SET vfield_name_value = ".GetSQLValueString($_POST[$field_value_name],"text").
										   " WHERE nvalue_id = ".GetSQLValueString($custom_data_row['nvalue_id'],"text");
			mysql_query($update_custom_fields_value) or die(mysql_error());							   
						
		}
				
	}		
		
		
		
		
		
/*---------Multicart 2.0 Upgradation Starts------*/		

			
		$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."users SET 
			password = '".addslashes(md5($txtNewPassword))."'
			WHERE user_id =  '".addslashes($userid)."'";
		mysql_query($sql);
		if($chkNotify == "on"){
			$sql = "SELECT email,first_name,last_name,user_name FROM ".$tableprefix."users WHERE user_id = '".addslashes($userid)."' ";
			$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"];
				$username = $row["user_name"];
			}
			
			$userfullname = $firstname;
			if($lastname != ""){
				$userfullname .= " ". $lastname;	
			}
			$to = $email;
			$subject = "Your account information with ".stripslashes(SITE_NAME)." updated by Administrator"; 
			
/*----------Multicart 2.0 Upgradation---------------*/				

			$mailmessage = readEmailTemplate('change_userpassword');
			
			$mailmessage = str_replace('[name]',$userfullname,$mailmessage);
			$mailmessage = str_replace('[user_name]',$username,$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);
			$message= PWD_UPD;
		}else{
			$message= UNK_ERROR;
		}
		
		
		
		
    } 
}
$sql = "SELECT * FROM ".$tableprefix."users WHERE user_id = '".addslashes($userid)."' ";
$result = mysql_query($sql);
if(mysql_num_rows($result) != 0){
	$row = mysql_fetch_array($result);
	$txtUserName = $row["user_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"];

}
$countrylist = makeCountryList();


/*----------Multicart 2.0 Upgradation Starts-----*/

/*----------Editing Custom Fields---------------*/


$sql_custom_data = "SELECT cf.ncustom_id,cf.vcustom_field_name,cf_value.vfield_name_value,cf.vcustom_field_type,
						cf_value.nvalue_id,cf.vrequired_custom_field,cf.vcustom_display_field,
					CASE WHEN vrequired_custom_field = 'Y' THEN '*' ELSE '' END AS required_status   
					FROM ".$tableprefix."custom_fields_value cf_value,".$tableprefix."custom_fields cf
					WHERE cf_value.ncustom_field_id = cf.ncustom_id 
					AND cf.vcustom_field_form_id = '1'
					AND cf_value.ncustom_table_id = ".GetSQLValueString($userid,"text").
					" ORDER BY cf.vcustom_sort_order";		
$result_custom_data = mysql_query($sql_custom_data) or die(mysql_error());


/*----------Multicart 2.0 Upgradation Ends-----*/

$ddlCountry = $ddlCountry == ""?"US":$ddlCountry;
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" onSubmit="return checkCustomData();">
	
		<input type="hidden" name="userid" value="<?php echo htmlentities($userid);?>">
			<!-- 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_USER_PROFILE;?></div>
						<div align="center" class="text_information grey_tr"><?php echo $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 align="right" ><?php echo UNAME;?></td>
					  <td >&nbsp;</td>
					  <td align="left" ><input name="txtUserName" type="text" class="textarea" id="txtUserName" value="<?php echo stripslashes($txtUserName);?>" size="30" maxlength="100" readonly="readonle"></td>
					  </tr>
					<tr><td width="28%" align="right" ><?php echo FNAME;?>&nbsp;<span class="required">*</span></td>
					<td width="3%" >&nbsp;</td><td width="41%" 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 >&nbsp;</td><td align="left" ><input type="text" class="textarea" 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 >&nbsp;</td><td align="left" ><input type="text" class="textarea" value="<?php echo stripslashes($txtAddress1);?>" name="txtAddress1" size="40" maxlength="100"></td></tr>
					<tr><td align="right" ><?php echo ADDR_LINE2;?></td>
					<td >&nbsp;</td><td align="left" ><input type="text" value="<?php echo stripslashes($txtAddress2);?>" class="textarea" name="txtAddress2" size="40" maxlength="100"></td></tr>
					<tr><td align="right" ><?php echo CITY;?>&nbsp;<span class="required">*</span></td>
					<td >&nbsp;</td><td align="left" ><input type="text" class="textarea" 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>
					<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;?>&nbsp;<span class="required">*</span></td>
					<td >&nbsp;</td><td align="left" ><input type="text" class="textarea" value="<?php echo htmlentities($txtZIP);?>" name="txtZIP" size="30" maxlength="20"></td></tr>
					<tr><td align="right" ><?php echo PHONE;?>&nbsp;<span class="required">*</span></td>
					<td >&nbsp;</td><td align="left" ><input type="text" class="textarea" value="<?php echo htmlentities($txtPhone);?>" name="txtPhone" size="30" maxlength="100"></td></tr>
					<tr><td align="right" ><?php echo FAX;?></td>
					<td >&nbsp;</td><td align="left" ><input type="text" class="textarea" value="<?php echo htmlentities($txtFAX);?>" name="txtFAX" size="30" maxlength="100"></td></tr>
					<tr><td align="right" ><?php echo EMAIL;?>&nbsp;<span class="required">*</span></td>
					<td >&nbsp;</td><td align="left" ><input type="text" class="textarea" value="<?php echo htmlentities($txtEmail);?>" name="txtEmail" size="30" maxlength="100"></td></tr>
																											
<!--
	Multicart 2.0 Upgradation Starts
-->																											
			
<?php			

																										$java_script = "<script>
																										function checkCustomData()
																										{";
																									
																										if(mysql_num_rows($result_custom_data) > 0)
																										{
																										
																											while($custom_data_row = mysql_fetch_array($result_custom_data))
																											{
																											
																												$field_value_name = "txtFieldValue_".$custom_data_row['nvalue_id'];
																												$field_name_required = "txtFieldRequired_".$custom_data_row['nvalue_id'];
																												$description = ucwords($custom_data_row['vcustom_field_name']);
																												
																												$java_script .=" if(trimSpaces(document.frmProfile.$field_name_required.value) == 'Y')
																																 {
																																 
																																	if(trimSpaces(document.frmProfile.$field_value_name.value) == '')
																																	{
																																		alert('$description is a required field !!');
																																		document.frmProfile.$field_value_name.focus();
																																		return false;
																																	}
																																	return true;
																																		
																																 }";
																																																																																				
?>				
		
			
			
			
																											<tr>
																												<td align="right" valign="top"><?php echo ucwords($custom_data_row['vcustom_field_name'])?>
																													<span class="required"><?php echo $custom_data_row['required_status']?></span>
																												</td>
																												<td>&nbsp;<input type="hidden" name="<?php echo $field_name_required?>" value="<?php echo $custom_data_row['vrequired_custom_field']?>"></td>				
																																	
																											
<?php
																											
																															switch($custom_data_row['vcustom_field_type'])
																															{
																															
																																case 'text_area':
?>																															
																																<td align="left">
																																		<textarea name="<?php echo $field_value_name?>" rows="5" cols="15" class="textarea"><?php echo $custom_data_row['vfield_name_value']?></textarea>	
																																</td>		
<?php				
																																break;
																																case 'text_box':
?>					
																																	<td width="28%" align="left">
																																		<input type="text" class="textarea" name="<?php echo $field_value_name?>" value="<?php echo $custom_data_row['vfield_name_value']?>">
																																	</td>			
																										
<?php				
																																break;
																											
																																	
																															}
																											
																													}
																											
																												}
																												
																												$java_script .= "}</script>";
																																																							
?>																																														
																										

<!--
	Multicart 2.0 Upgradation Ends
-->																											
																											
									
									<tr><td colspan="3"></tr>
									<tr><td colspan="3" align="center"><input type="submit" value="<?php echo BTN_SAVE_CHANGES;?>" name="btnSaveChanges" class="button"></td></tr>
									<tr><td colspan="3">&nbsp;</td></tr>
									<tr><td colspan="3">&nbsp;</td></tr>
									<tr><td colspan="3" align="center"><?php echo CHANGE_PASSWORD;?></tr>
									<tr><td align="right" ><?php echo NEW_PWD;?>&nbsp;<span class="required">*</span></td>
									<td >&nbsp;</td><td align="left" ><input type="password" class="textarea" value="" name="txtNewPassword" size="30" maxlength="100"></td></tr>
									<tr><td align="right" ><?php echo NOTIFY_EMAIL;?>&nbsp;</td>
									<td >&nbsp;</td><td align="left" ><input type="checkbox" class="checkbox" checked  name="chkNotify" ></td></tr>
									<tr><td colspan="3">&nbsp;</td></tr>
									<tr><td colspan="3" align="center"><input type="button" class="button" name="btnbackToUsers" value="<?php echo BACK_TO_USERS;?>" onClick="window.location.href='users.php?<?php echo $_GET['qstring']?>&txtSearch=<?php echo $_GET['txtSearch']?>'">&nbsp;<input class="button" type="submit" value="<?php echo RST_PWD;?>" name="btnResetPassword"></td></tr>
									<tr><td colspan="3">&nbsp;</td></tr>
								</table>
								
<?php

echo $java_script;

?>								
								

						</div>
						
						
				<!--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 -->
				</div>
				
				
<?php
		
	include "../includes/footer.php";
?>
