<?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";
include_once "classes/cls_user.php";
include_once "classes/cls_store.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,cf.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());
                
          //Multicart 3.0  Modifications for single account for seller and buyer 
                
                $sellerAccObj   =   User::is_seller_account_activated($userid);
                
                if($sellerAccObj->status==SUCCESS)
            {
                    $sellerAccObjVal            =   $sellerAccObj->data;
                    $seller_activated_status    =   $sellerAccObjVal->is_seller;
                    $activated_seller_id        =   $sellerAccObjVal->seller_id;
                    
                    
                    if($seller_activated_status=='Y')
                    {
                        $sellerVo                       =   new stdClass();
                        $sellerVo->first_name           =   $txtFirstName;
                        $sellerVo->last_name            =   $txtLastName;
                        $sellerVo->address1             =   $txtAddress1;
                        $sellerVo->address2             =   $txtAddress2;
                        $sellerVo->city                 =   $txtCity;
                        $sellerVo->state                =   $txtState;
                        $sellerVo->country              =   $ddlCountry;
                        $sellerVo->zip                  =   $txtZIP;
                        $sellerVo->email                =   $txtEmail;
                        $sellerVo->phone                =   $txtPhone;
                        $sellerVo->seller_id            =   $activated_seller_id;
                        $sellerVo->fax                  =   $txtFAX;
                        
                        // Update seller account information Multicart 3.0 
                        
                        $sellerid_info                  =   Store::updateSellerProfileInformation($sellerVo);
                    }
            }
            
		
/*---------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";

        // Fetching custom fields    
        $custom_update_select = "SELECT * FROM ".$tableprefix."custom_fields_value WHERE user_id=".addslashes($userid);
		//echo $select_custom_data;						 
								 
		$custom_update_result = mysql_query($custom_update_select) or die(mysql_error());	
		
		if(mysql_num_rows($custom_update_result) > 0)
		{
		
			while($custom_update_row = mysql_fetch_array($custom_update_result))
			{
				$custom_name = "custom_field_".$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>


<!--mainbody area start -->
<div class="main_body">
	<div class="container">
		<div class="row">

			
		<!--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">
	
		
		<div class="col-sm-12 col-lg-4 col-md-4"></div>
                    <div class="col-sm-12 col-lg-4 col-md-4">
                        <div align="left" id="items_top_area">
                            <div align="center" id="items_top_area_ttl">
                                <?php echo EDIT_PROFILE_TEXT; ?>
                                <div class="clear"></div>
                            </div>
                        </div>
						
					<!--
					<div class="suces_msg">Update Profile</div>
						<div class="error_msg">Email is worng</div>-->
						
                        <div class="text_information" align="center"><?php echo $message ?></div>
                        <div class="clear"></div>
                       
                            <div class="form_wrapper">
                                <div class="register_form_outer">
                                    <div class="registrationform_container">                                        
                                        <div class="field_wrapper formfield_row">
                                            <label><?php echo FIRST_NAME_TEXT; ?><span class="required">*</span></label>
                                            <input class="form-control" type="text" placeholder="<?php echo FIRST_NAME_TEXT;?>" value="<?php echo ($txtFirstName);?>" name="txtFirstName" size="30" maxlength="100">
                                        </div>
                                        <div class="field_wrapper formfield_row">
                                            <label><?php echo LAST_NAME_TEXT; ?><span class="required">*</span></label>
                                            <input class="form-control" type="text" placeholder="<?php echo LAST_NAME_TEXT;?>" value="<?php echo ($txtLastName);?>" name="txtLastName" size="30" maxlength="100">
                                        </div>
                                        <div class="field_wrapper formfield_row">
                                            <label><?php echo EMAIL_LABEL; ?><span class="required">*</span></label>
                                            <input type="text" value="<?php echo htmlentities($txtEmail);?>" placeholder="<?php echo EMAIL_LABEL;?>"  class="form-control" name="txtEmail" size="30" maxlength="100">
                                        </div>
                                        <div class="field_wrapper formfield_row">
                                            <label><?php echo ADDR_LINE1_TEXT; ?><span class="required">*</span></label>
                                            <input class="form-control"  type="text" placeholder="<?php echo ADDR_LINE1_TEXT;?>" value="<?php echo ($txtAddress1);?>" name="txtAddress1" size="40" maxlength="100">
                                        </div>
                                        <div class="field_wrapper formfield_row">
                                            <label><?php echo ADDR_LINE2_TEXT; ?></label>
                                            <input type="text" value="<?php echo ($txtAddress2);?>"  class="form-control" placeholder="<?php echo ADDR_LINE2_TEXT;?>" name="txtAddress2" size="40" maxlength="100">
                                        </div>
                                        <div class="field_wrapper formfield_row">
                                            <label><?php echo CITY_TEXT; ?><span class="required">*</span></label>
                                            <input type="text" class="form-control" placeholder="<?php echo CITY_TEXT;?>" value="<?php echo ($txtCity);?>" name="txtCity" size="30" maxlength="100">
                                        </div>
                                        <div class="field_wrapper formfield_row">
                                            <label><?php echo COUNTRY_TEXT; ?><span class="required">*</span></label>
                                            <select id='billCountrySelect' class="form-control" name='bill_country' onchange='updateState(this.id)'> </select>       			
                                        </div>
                                        <div class="field_wrapper formfield_row">
                                            <label><?php echo STATE_LABEL; ?><span class="required">*</span></label>
                                            <select id='billStateSelect' class="form-control" name='bill_state'></select> <SCRIPT  type="text/javascript">initCountry(); </SCRIPT>          
                                        </div>
                                        <div class="field_wrapper formfield_row">
                                            <label><?php echo ZIP_TEXT; ?><span class="required">*</span></label>
                                            <input type="text" value="<?php echo htmlentities($txtZIP);?>" placeholder="<?php echo ZIP_TEXT;?>" class="form-control" name="txtZIP" size="30" maxlength="20">
                                        </div>
                                        <div class="field_wrapper formfield_row">
                                            <label><?php echo FAX_TEXT; ?></label>
                                            <input type="text" class="form-control" placeholder="<?php echo FAX_TEXT;?>" value="<?php echo htmlentities($txtFAX);?>" name="txtFAX" size="30" maxlength="100">
                                        </div>


                                        <?php
                                        //$j for avoiding error only 	
                                        //echo $select_custom_fields = "SELECT * FROM ".$tableprefix."custom_fields WHERE vcustom_display_field = 'Y' AND vcustom_field_form_id ='".$j."' ORDER BY vcustom_sort_order ";
//                                         $select_custom_fields = "SELECT * FROM " . $tableprefix . "custom_fields WHERE vcustom_display_field = 'Y' 
// 									 AND vcustom_delete_status != 'Y' ORDER BY vcustom_sort_order";
                                        
                                        // Not considering the reference of custom fields in tbl_custom_fields
                                        $select_custom_fields = "SELECT * FROM ".$tableprefix."custom_fields_value WHERE user_id=".addslashes($userid);
                                        
                                        $result_custom_fields = mysql_query($select_custom_fields) or die(mysql_error());

                                        while ($custom_field_row = mysql_fetch_array($result_custom_fields)) {

                                            if ($custom_field_row['vrequired_custom_field'] == 'Y') {

                                                $required_class = "field_wrapper_required";
                                            } else {

                                                $required_class = "field_wrapper";
                                            }

                                            $field_name = 'custom_field_' . $custom_field_row['nvalue_id'];  //	$custom_field_row['ncustom_id']
                                            ?>		

                                            <div class="<?php echo $required_class; ?> field_wrapper formfield_row">
                                                <label><?php echo ucwords($custom_field_row['vcustom_field_name'])?>&nbsp;<?php echo $field_required?></label>
                                           

                                                <?php
                                                if ($custom_field_row['vcustom_field_type'] == "text_box") {
                                                    ?>				

                                                    <input class="form-control" type="text" value="<?php echo $custom_field_row['vfield_name_value'] ?>" name="<?php echo $field_name ?>" size="55" maxlength="30" placeholder="<?php echo $custom_field_row['vcustom_field_name'] ?>">

                                                    <?php
                                                } else {
                                                    ?>


                                                    <textarea name="<?php echo $field_name ?>" rows="5" cols="15" class="text_field" placeholder="<?php echo $custom_field_row['vcustom_field_name'] ?>"><?php echo $custom_field_row['vfield_name_value'] ?></textarea>

                                                    <?php
                                                }
                                                ?>																													
                                            </div>


                                            <?php
                                        }
                                        ?>
                                        <div class="field_wrapper formfield_row">
                                            <input type="checkbox" name="check_news" value="1" <?php if ($newsLetter=="Y") echo "Checked"; ?>>

                                            <label class="small_txt"><?php echo NOTIFY_UPDATES_TEXT1; ?></label>
                                        </div>
                                        <div class="field_wrapper formfield_row">
                                            <div class="secondarypannel">
                                               <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>
                                            </div>
                                        </div>                                        
                                    </div>
                                    <div class="clear"></div>
                                </div>
                            </div>
                           

                    </div>
                    <div class="col-sm-12 col-lg-4 col-md-4"></div>

                    <div class="clear"></div>		
	
	
		<div align="left" class="float_clear1"></div>
					
					
		 <!--items display area end -->
		  </div>
		
			
		<!--center content area end-->	


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




				

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