<?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_userid"]) and $_SESSION["sess_userid"]!= ""){
	$userid = $_SESSION["sess_userid"];
}else{
	header("Location:login.php");
	exit;
}
$txtUserName = $_POST["txtUserName"];
$txtFirstName = $_POST["txtFirstName"];
$txtLastName = $_POST["txtLastName"];
$txtAddress1 = $_POST["txtAddress1"];
$txtAddress2 = $_POST["txtAddress2"];
$txtCity = $_POST["txtCity"];
$txtState = $_POST["bill_state"];
$ddlCountry = $_POST["bill_country"];
$txtZIP = $_POST["txtZIP"];
$txtEmail = $_POST["txtEmail"];
$txtPhone = $_POST["txtPhone"];
$txtFAX = $_POST["txtFAX"];
$txtPassword = $_POST["txtPassword"];
$txtConfirmPassword = $_POST["txtConfirmPassword"];
$check_news = $_POST["check_news"];

if($check_news==1)
$check_news="Y";
else
$check_news="N";

if (isset($_POST["btnSaveChanges"])) {
    $message = "" ;
	if (!isNotNull($txtFirstName)) {
        $message .= FIRST_NAME_EMPTY_MSG." <br>";
    }
	/*	if(isNotNull($txtFirstName))
	{
	
	 if(!preg_match("/^[a-zA-Z ]+$/", $txtFirstName))
	 { 
	   $message .= "* First name accepts alphabets only !! <br>";
	 }
	}*/
	if (!isNotNull($txtLastName)) {
        $message .= LAST_NAME_EMPTY_MSG." <br>";
    }
	/*if(isNotNull($txtLastName))
	{
	 if(!preg_match("/^[a-zA-Z ]+$/", $txtLastName))
	 { 
	   $message .= "* Last name accepts alphabets only !! <br>";
	 }
	}*/

	if (!isNotNull($txtAddress1)) {
        $message .= ADDR_EMPTY_TEXT." <br>";
    }  
	if (!isNotNull($txtState)) {
        $message .= STATE_EMPTY_TEXT." <br>";
    }
		if(isNotNull($txtPhone))
	{ 
		if(!check_phone($txtPhone,$ddlCountry))
		{
		 $message .= INVALID_PHONE_NUMBER." <br>";
		}
	}
	
	if(!isNotNull($txtZIP))
	{
		
		 $message .= ZIP_EMPTY_MSG." <br>";
		
	}    
	if (!isNotNull($txtEmail)) {
        $message .= EMAIL_EMPTY_MSG." <br>";
    } else{
		if (!isValidEmail($txtEmail)) {
	        $message .= INVALID_EMAIL_MSG." <br>";
	    } 
	}
	
/*---------Selecting the custom fields data if exists---------------*/

		$select_custom_update = "SELECT cv.nvalue_id,cf.vrequired_custom_field,vcustom_field_name
							     FROM ".$tableprefix."custom_fields cf,".$tableprefix."custom_fields_value cv
							     WHERE cf.ncustom_id = cv.ncustom_field_id
								 AND cf.vcustom_field_form_id = 1
								 AND cv.ncustom_table_id = ".addslashes($userid)."
								 ORDER BY cf.vcustom_sort_order";
								 
		//echo $select_custom_data;						 
								 
		$result_custom_update = mysql_query($select_custom_update) or die(mysql_error());	
		
		if(mysql_num_rows($result_custom_update) > 0)
		{
		
			while($custom_update_row = mysql_fetch_array($result_custom_update))
			{
		
				$custom_name = "txtCustom_".$custom_update_row['nvalue_id'];
				
				if($custom_update_row['vrequired_custom_field'] == 'Y')
				{
				
					
					if (!isNotNull($_POST[$custom_name]))
					{
						//$message .= ucfirst($custom_update_row['vcustom_field_name']).CANNOT_BE_EMPTY_TEXT."  <br>";
						$message .= CANNOT_BE_EMPTY_TEXT." <br>";
						$message  = str_replace("FIELD",ucfirst($custom_update_row['vcustom_field_name']),$message);
					}
					
					
				
				}
			
			
			}
				
		}

	
    $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_ADDRESS_MSG." <br>";
    } 

    if ($message != "") { // error
        $message = "<br>".CORRECT_ERRORS_TEXT."<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)."',
			newsLetter = '".addslashes($check_news)."',
			email = '".addslashes($txtEmail)."'
			WHERE user_id =  '".addslashes($userid)."'
			";
		mysql_query($sql) or die(mysql_error());
		
/*---------Selecting the custom fields data if exists---------------*/

		$select_custom_update = "SELECT cv.nvalue_id
							     FROM ".$tableprefix."custom_fields cf,".$tableprefix."custom_fields_value cv
							     WHERE cf.ncustom_id = cv.ncustom_field_id
								 AND cf.vcustom_field_form_id = 1
								 AND cv.ncustom_table_id = ".addslashes($userid)."
								 ORDER BY cf.vcustom_sort_order";
								 
		//echo $select_custom_data;						 
								 
		$result_custom_update = mysql_query($select_custom_update) or die(mysql_error());	
		
		if(mysql_num_rows($result_custom_update) > 0)
		{
		
			while($custom_update_row = mysql_fetch_array($result_custom_update))
			{
		
				$custom_name = "txtCustom_".$custom_update_row['nvalue_id'];
			
		
				$update_custom_value = "UPDATE ".$tableprefix."custom_fields_value 
										SET vfield_name_value = ".GetSQLValueString($_POST[$custom_name],"text").
										" WHERE nvalue_id = ".$custom_update_row['nvalue_id'];
										
				mysql_query($update_custom_value) or die(mysql_error());	
		
			}
				
		}

		
		$message= PROFILE_UPDATE_SUCCESS_TEXT;
    } 
} 
$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);
	$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"];
	$newsLetter = $row["newsLetter"];

}
$countrylist = makeCountryList();

/*display the active template*/

$active_template = displayTemplate();


include "includes/htmltop.php";

/* Top File Name */

include_once ("includes/".$active_template[0]);


/*---------Selecting the custom fields data if exists---------------*/

$select_custom_data = "SELECT cf.vcustom_field_name,cf.vcustom_field_type,cv.vfield_name_value,cv.nvalue_id,cf.vrequired_custom_field
					   FROM ".$tableprefix."custom_fields cf,".$tableprefix."custom_fields_value cv
					   WHERE cf.ncustom_id = cv.ncustom_field_id
					    AND cf.vcustom_field_form_id = 1
						AND cv.ncustom_table_id = ".addslashes($userid)."
						 ORDER BY cf.vcustom_sort_order";
						 
//echo $select_custom_data;						 
						 
$result_custom_data = mysql_query($select_custom_data) or die(mysql_error());

$ddlCountry = $ddlCountry == ""?"US":$ddlCountry;
?>
<script language="javascript" src="includes/function.js"></script>
<script src="./js/registration_ajax.js"></script>
<script src="./js/javascript_check_userside.js"></script>
<script>
function checkUserRegisterForm()
{

  var form_name = new Array('txtFirstName','txtLastName','txtEmail','txtAddress1','txtCity','billCountrySelect','bill_state','txtZIP','txtPhone');
  var display_name = new Array('<?php echo FIRST_NAME_TEXT;?>','<?php echo LAST_NAME_TEXT;?>','<?php echo EMAIL_LABEL;?>','<?php echo ADDR1_TEXT;?>','<?php echo CITY_TEXT;?>','<?php echo COUNTRY_TEXT;?>','<?php echo STATE_LABEL;?>','<?php echo ZIP_TEXT;?>','<?php echo PHONE_TEXT;?>');
  var count = 9;
  var email           = document.frmProfile.txtEmail.value;
  if(check_fieldsOnly('frmProfile',form_name,display_name,count))
  {

        if(email != '')
        {
            if(!echeck(email))
            {
                return false;
            }
            else
                {
                    return true;
                }
        }
   return false;
  }
  else
  {//alert("v")
  
  		return false;
  
  }
  return true;
  
}
</script>


<div align="left" class="main_body">
			<!--mainbody area start -->
			
		<!--center content area start-->		
		<form name="frmProfile" action="<?php echo $_SERVER["PHP_SELF"]?>" method="POST" onSubmit="return checkUserRegisterForm();">
		<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" id="item_details">
	
	
		<!--items display area start -->
	
		<div align="left" id="items_top_area">
				
				<div align="left" id="items_top_area_ttl"><?php echo EDIT_PROFILE_TEXT;?></div>
		</div>
		<br>
		
		<div class="text_information" align="center"><?php echo $message?></div>
		
		<div>
																						
																										
		<fieldset>
		<div class="edt_profilestyles">
		<table cellpadding="2" cellspacing="2" width="100%" border="0">
			<tr>
				<td colspan="3" align="right"><span class="required">*</span><?php echo MANDATORY_FIELDS_TEXT;?></td>
			</tr>
			<tr><td colspan="3"></tr>
			<tr><td align="left" ><?php echo FIRST_NAME_TEXT;?>&nbsp;<span class="required">*</span></td><td align="left">&nbsp;</td><td align="left"><input class="textarea" type="text" value="<?php echo ($txtFirstName);?>" name="txtFirstName" size="30" maxlength="100"></td></tr>
			<tr><td align="left" ><?php echo LAST_NAME_TEXT;?>&nbsp;<span class="required">*</span></td><td align="left">&nbsp;</td><td align="left"><input class="textarea" type="text" value="<?php echo ($txtLastName);?>" name="txtLastName" size="30" maxlength="100"></td></tr>
			<tr><td align="left" ><?php echo EMAIL_LABEL;?>&nbsp;<span class="required">*</span></td>
			<td align="left">&nbsp;</td><td align="left"><input type="text" value="<?php echo htmlentities($txtEmail);?>"  class="textarea" name="txtEmail" size="30" maxlength="100"></td></tr>
			<tr><td align="left" ><?php echo ADDR_LINE1_TEXT;?>&nbsp;<span class="required">*</span></td><td align="left">&nbsp;</td><td align="left"><input class="textarea"  type="text" value="<?php echo ($txtAddress1);?>" name="txtAddress1" size="40" maxlength="100"></td></tr>
			<tr><td align="left" ><?php echo ADDR_LINE2_TEXT;?></td><td align="left">&nbsp;</td><td align="left"><input type="text" value="<?php echo ($txtAddress2);?>"  class="textarea" name="txtAddress2" size="40" maxlength="100"></td></tr>
			<tr><td align="left" ><?php echo CITY_TEXT;?>&nbsp;<span class="required">*</span></td><td align="left">&nbsp;</td><td align="left"><input type="text" class="textarea"  value="<?php echo ($txtCity);?>" name="txtCity" size="30" maxlength="100"></td></tr>
			<tr><td align="left"><?php echo COUNTRY_TEXT;?>&nbsp;<span class="required">*</span></td><td align="left">&nbsp;</td><td align="left">
				  <select id='billCountrySelect' class="textarea"name='bill_country' onchange='updateState(this.id)'>
       			 </select>
				</td></tr>
				<tr>
                                    <td align="left"><?php echo STATE_LABEL;?>&nbsp;<span class="required">*</span></td>
				  <td align="left">&nbsp;</td>
				  <td align="left"> <select id='billStateSelect' class="textarea" name='bill_state'>
         </select> <SCRIPT  type="text/javascript">initCountry(); </SCRIPT> </td>
			  </tr>
                          <tr><td align="left" ><?php echo ZIP_TEXT;?>&nbsp;<span class="required">*</span></td>
			<td align="left">&nbsp;</td><td align="left"><input type="text" value="<?php echo htmlentities($txtZIP);?>"  class="textarea" name="txtZIP" size="30" maxlength="20"></td></tr>
			<tr><td align="left" ><?php echo PHONE_TEXT;?>&nbsp;<span class="required">*</span></td>
			<td align="left">&nbsp;</td><td align="left"><input type="text" value="<?php echo htmlentities($txtPhone);?>" class="textarea"  name="txtPhone" size="30" maxlength="100"></td></tr>
			<tr><td align="left" ><?php echo FAX_TEXT;?></td><td align="left">&nbsp;</td><td align="left"><input type="text" value="<?php echo htmlentities($txtFAX);?>"  class="textarea" name="txtFAX" size="30" maxlength="100"></td></tr>
<?php
	
	
		if(mysql_num_rows($result_custom_data) > 0)
		{
		
			while($custom_data_row = mysql_fetch_array($result_custom_data))
			{
			
				$custom_name = "txtCustom_".$custom_data_row['nvalue_id'];
				
				if($custom_data_row['vrequired_custom_field'] == 'Y')
				{
					$field_required = "<span class='required'>*</span>";																												
				}
				else
				{
					$field_required = "";
				
				}
	
?>		
			
			
			
			<tr>
				<td align="left"><?php echo ucwords($custom_data_row['vcustom_field_name'])?>&nbsp;<?php echo $field_required?></td>
				<td align="left">&nbsp;</td>
				<td align="left">
<?php
  
		if($custom_data_row['vcustom_field_type'] == 'text_box')
		{
?>				
			<input type="text" value="<?php echo ($custom_data_row['vfield_name_value']);?>"  class="textarea" name="<?php echo $custom_name?>">
<?php
		
		}
		else
		{
?>			
				
			<textarea name="<?php echo $custom_name?>" rows="5" cols="15" class="textbox"><?php echo ($custom_data_row['vfield_name_value']);?></textarea>		
				
<?php
		
		}	
	
?>			
					
				</td>
			</tr>
			
			
<?php
	
			}
	
		}
		
	
?>				
			
			
			<tr><td colspan="3" align="left"><input type="checkbox" name="check_news" value="1" <?php if ($newsLetter=="Y") echo "Checked"; ?>>
			<?php echo NOTIFY_UPDATES_TEXT1;?></td>
			</tr>
			<tr><td colspan="3"></tr>
			<tr>
			  <td colspan="3" align="center">&nbsp;&nbsp;
			    <input type="button" value="<?php echo ACCOUNT_BACK_BUTTON;?>" name="btnBack"  class="button" onClick="window.location.href='myaccount.php'">
			    <input type="submit" value="<?php echo SAVE_CHANGES_BUTTON;?>" name="btnSaveChanges"  class="button" ></td></tr>
		</table>
		<div class="clear"></div>
		</div>
		</fieldset>
		
		</div>
		
		<div align="left" class="float_clear1"></div>
					
					
		 <!--items display area end -->
		  </div>
		
			
		<!--center content area end-->	


		<div align="left" class="clear"></div>
                </form>
		<!--mainbody area end -->
        </div>
		<div align="left" class="clear"></div>




				

<?php
include "includes/".$active_template[2];	/* Bottom File Name */
?>																
