<?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_utils.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;
}

if($userid!='')
    {
            $obj        =   User::getUserInfo($userid);

            if($obj->status==SUCCESS)
                    {
                        $row    =   $obj->data;
                        $username = $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;
                    }
    }



  if(getSettingsValue('enable_membership')=='Y')
        {
                $planObj    =   Utils::getsellerPlans();

                //echo $planObj->status ;
                if($planObj->status==SUCCESS)
                 {
                       $sellerPlanRows      =   $planObj->data;
                 }

        }
 

if (isset($_POST["btnSaveChanges"])) {
    
        $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"];
        $txtStorename       =   $_POST['txtStorename'];
        $txtPaypalEmail     =   $_POST['txtPaypalEmail'];
        $txtCompany         =   $_POST['txtCompany'];
        $txtTimeZone        =   $_POST['txtTimeZone'];
    $message = "" ;
	if (!isNotNull($txtStorename)) {
        $message .= TEXT_STORENAME_EMPTY_MSG." <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($txtCompany))
	{
		
		 $message .= COMPANY_EMPTY_MSG." <br>";
		
	} 
        	
	if(!empty($txtStorename)){
                $txtStorename = getStoreAlias($txtStorename,$txtStorename);
        }

  // Email Address Validation

        $paymentFlag    =   false;

        $sellerPlanId   =   0;
        
  if(getSettingsValue('enable_membership')=='Y' && $planObj->status==SUCCESS ) { 
            if (!isNotNull($txtPaypalEmail)) {
            $message .= VAL_MSG_PAYPAL_MAIL;
        }//end if
    else {
            if (!isValidEmail($txtPaypalEmail)) {
                $message .= INVALID_EMAIL_MSG;
            }//end if
      }//end else

      if(!empty($txtPaypalEmail))
          {
               $paypalExistFlag         =  Store::checksellerPaypalemailExists($txtPaypalEmail);
               if($paypalExistFlag->status==SUCCESS)
                       {
                           $message.= MSG_PAYPAL_EMAIL_EXISTS;
                       }

          }
      
      $planRow    =   Utils::getPlanPeriod($_POST['subOption']);
        if($planRow->data=='F')
                {
                       
                    $paymentFlag    =   false;
                }else{
                    $paymentFlag    =   true;
                }

                $sellerPlanId       =   $_POST['subOption'];

  }
        
  
/*---------Selecting the custom fields data if exists---------------*/
   
    if ($message != "") { // error
     
        $message = "<br>".CORRECT_ERRORS_TEXT."<br>" . $message;
        $msg_class   =  'warning';
    } else { // no error so insert user details


                $userobj        =   User::getUserInfo($userid);

                if($userobj->status==SUCCESS)
                    {
                        $row                = $userobj->data;
                        
                    }


                $admincommision             =   getSettingsValue('artist_commission');
               
                if(getSettingsValue('approve_sellers')==1)
                    {
                        $txtapprovesellers      ="Y";
                    }else{
                        $txtapprovesellers      ="NN";
                    }

                 $defaultflatrate       =       getSettingsValue('default_flatrate');
                 $additionalflatrate    =       getSettingsValue('additional_flatrate');
                 
                $objVo  =   new stdclass();

                $objVo->artist_name         =     $row->user_name;
                $objVo->password            =     $row->password;
                $objVo->first_name          =     $row->first_name;
                $objVo->last_name            =    $row->last_name;
                $objVo->address1            =     $txtAddress1;
                $objVo->address2            =     $txtAddress2;
                $objVo->city                =     $txtCity;
                $objVo->state               =     $txtState;
                $objVo->country             =     $ddlCountry;
                $objVo->zip                 =     $txtZIP;
                $objVo->fax                 =     $txtFAX;
                $objVo->phone               =     $txtPhone;
                $objVo->email               =     $row->email;
                $objVo->storename           =     $txtStorename;
                $objVo->commission          =     $admincommision;
                $objVo->date_registered     =     date('Y-m-d');
                $objVo->paypalemail         =     $txtPaypalEmail;
                $objVo->sellerPlanID        =     $sellerPlanId;
                $objVo->vapproved           =     $txtapprovesellers;
                $objVo->defaultflatrate     =     $defaultflatrate;
                $objVo->additionalflatrate  =     $additionalflatrate;
                $objVo->company             =     $txtCompany;
                $objVo->seller_time_zone    =     $txtTimeZone;

                $objPlanVo                  =   new stdClass();

  
                if($paymentFlag)
            {
                    // Redirect user to the payment page
                    $_SESSION['SELLER_PLAN_OBJ']            = $objVo;
                    $redirecturl                            =   SITE_URL."/choosesellerplan.php";
                    generalMethods::redirect($redirecturl);
            }
            else{
                  //Create Seller Account with user details
                $accObj       =   Store::createsellerAccount($objVo);
                if($accObj->status==SUCCESS)
                        {
                             $sellerid              = $accObj->data;
                            //Update Seller Account Activation in User side
                             $userObjVo             =   new stdclass();
                             $userObjVo->userid     =   $userid;
                             $userObjVo->sellerid   =  $sellerid;
                             
                             $userObj               =   User::updateSellerAccountActivation($userObjVo);
                             //Get Seller Approval Status And Re-direction
                             $sellerStatusObj       =   Store::get_seller_account_info($sellerid);
                             if($sellerStatusObj->status==SUCCESS)
                                        {
                                            $sellerobjDataVal   =  $sellerStatusObj->data;
                                            
                                            // Save Seller Plan Details 
                                            $noOfDays=30;
                                            $fromDate= date("Y-m-d");
                                            $endDate= date("Y-m-d",strtotime("+".$noOfDays." day"));
                
                                            $objPlanVo->artist_id           =   $sellerid;
                                            $objPlanVo->artist_memStartDate =   $fromDate;
                                            $objPlanVo->artist_memEndDate   =   $endDate;
                                            $objPlanVo->plan_Id             =   $sellerPlanId;
                                            $objPlanVo->plan_SubscriptionCharge =   0;

                                            $objVo->seller_id         =     $sellerid;
                                            
                                            $saveSellerPlanInfo =  Store::savesellerPlanDetails($objPlanVo);

                                            // Send Mail Alert to Admin And seller
                                           $malFlag             = Store::sendSellerAccountActivationMail($objVo);
                                          
                                            // End 
                                          // End Plan details 
                                            $approvalStatus    = $sellerobjDataVal->vapproved;
                                                //header("Location:activateselelraccount")
                                                      if($approvalStatus=='NN')
                                                          {
                                                                $activation_msg = MSG_SELLER_ACCOUNT_ACTIVATION;
                                                          }else{
                                                                $activation_msg = MSG_SELLER_ACCOUNT_ACTIVATED;
                                                          }

                                                           $_SESSION['SELLER_ACCOUNT_ACTIVATED_MSG'] = $activation_msg;
                                                                $active_template = displayTemplate();
                                                                include_once "includes/htmltop.php";
                                                                $var_alert= '<link href="'.SITE_URL.'/themes/'.$active_template[5].'/jquery-confirm.css" rel="stylesheet" type="text/css">';
                                                                $var_alert.='<script src="'.SITE_URL.'/js/jquery-confirm.js"></script><script type="text/javascript">$.alert({
                                                        title: "",
                                                        content: "'.$activation_msg.'",
                                                        confirmButton: "okay",
                                                        confirmButtonClass: "btn-info",
                                                        animation: "bottom",
                                                        icon: "fa fa-check",
                                                        backgroundDismiss: true
                                                    }); window.location.href = "myaccount.php"';
                                                       //  $var_alert.='window.location.href = "myaccount.php" ';
                                                        $var_alert.='</script>';

                                                    echo $var_alert;
                                                    exit;
                                       
                                        }
                                
                         }
               
            }

		$msg_class          =   'text_information';
		$message            = PROFILE_UPDATE_SUCCESS_TEXT;
    } 
}



$countrylist = makeCountryList();

/*display the active template*/

$active_template = displayTemplate();


include "includes/htmltop.php";

/* Top File Name */
include_once ("includes/".$active_template[0]);
$ddlCountry = $ddlCountry == ""?"US":$ddlCountry;
?>

<script>
    $(document).ready(function(){
          $('#subOption').change(function(){
              var val  = $('#subOption').val();
               $.ajax({url: "<?php echo SITE_URL?>/plan_ajax_info.php?planid="+val, success: function(result){
            $("#jqTarget").html(result);
                }});
          });

         

          var planId = $('#subOption').val();

          if(planId!='')
          {
                $.ajax({url: "<?php echo SITE_URL?>/plan_ajax_info.php?planid="+planId, success: function(result){
                    $("#jqTarget").html(result);
                }});
          }
          
          //Store URL
        var storeURL = $.trim($("input[name='txtStorename']").val());
        var storeURLNew = "";

        if(storeURL!=""){

            storeURLNew = storeURL.replace(/[^a-z0-9-\s]/gi, '').replace(/[_\s]/g, '-');
        }


        $('#mashable').html(storeURLNew);
        $("input[name='txtStorename']").val(storeURLNew);

        $("input[name='txtStorename']").keyup(function() {
            storeURL = $.trim($("input[name='txtStorename']").val());
            if(storeURL!=""){


            storeURLNew = storeURL.replace(/[^a-z0-9-\s]/gi, '').replace(/[_\s]/g, '-');
            $('#mashable').html(storeURLNew);
            $("input[name='txtStorename']").val(storeURLNew);
            } else {
                var user = <?php echo $username;?>;
                var userNew = user.replace(/[^a-z0-9-\s]/gi, '').replace(/[_\s]/g, '-');
                $('#mashable').html(userNew);
            }
        });
          //Store URL
          
});
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 ACTIVATE_SELLER_ACCOUNT_TEXT; ?>
                                <div class="clear"></div>
                            </div>
                        </div>
                        <div class="<?php echo $msg_class;?>" align="center"><?php echo $message ?></div>
                        <div class="clear"></div>
                       
                            <div class="form_wrapper">
                                <div class="register_form_outer">
                                    <div class="registrationform_container">                                        
                             <?php  if(getSettingsValue('enable_membership')=='Y' && $planObj->status==SUCCESS )
                                        {
                                 ?>
                                        <div class="field_wrapper formfield_row">
                                            <select name="subOption" id="subOption" class="form-control" style="font-size:12px; font-weight:bold;" >

                                               				
                                                                <?php

                                                                foreach($sellerPlanRows as $data_plan) {
                                                                    $subCharge=number_format($data_plan->plan_SubscriptionCharge,2,".","");
                                                                    echo '<option value="'.$data_plan->plan_Id.'"';
                                                                    echo	'>'.$data_plan->plan_Title.'['.$adminCurrency['currency_symbol'] .'&nbsp;'.$subCharge.']</option>';
                                                                }//end while loop
                                            ?>
                                            </select>
                                        </div>
                                  <?php } ?>

                                        <div  id="jqTarget"> </div>
                                         <div class="field_wrapper formfield_row">
                                            <label><?php echo TEXT_STORENAME; ?><span class="required">*</span></label>
                                            <input class="form-control"  type="text" placeholder="<?php echo TEXT_STORENAME;?>" value="<?php echo ($txtStorename);?>" name="txtStorename" size="40" maxlength="100">
                                        </div>
                                        <?php if($enable_seller_friendly == "Y") {?>				

                                            <div class="field_wrapper formfield_row">
                                                    <label><?php echo TEXT_SELLER_STORE_URL;?></label>
                                                    <span class="storeurl">
                                                    <?php echo SITE_URL?>/store/<span id="mashable"></span>
                                                    </span>
                                            </div>
                                        <?php } ?>
                                        <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 COMPANY_TEXT; ?><span class="required">*</span></label>
                                            <input type="text" class="form-control" placeholder="<?php echo COMPANY_TEXT;?>" value="<?php echo htmlentities($txtCompany);?>" name="txtCompany" size="30" maxlength="100">
                                        </div>
                                        <div class="field_wrapper formfield_row">
                                            <label><?php echo PHONE_TEXT; ?><span class="required">*</span></label>
                                            <input type="text" class="form-control" placeholder="<?php echo PHONE_TEXT;?>" value="<?php echo htmlentities($txtPhone);?>" name="txtPhone" size="30" maxlength="100">
                                        </div>
                                        <div class="field_wrapper formfield_row">
											<label><?php echo TEXT_TIME_ZONE;?>&nbsp;<span class="required">*</span></label>
                                                                                        <?php echo makeTimeZoneList('form-control');?>
                                                                                </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  if(getSettingsValue('enable_membership')=='Y' && $planObj->status==SUCCESS ) { ?>

                                   <div class="field_wrapper formfield_row">
                                            <label><?php echo TEXT_PAYPAL_MAILID;?>&nbsp;<span class="required">*</span></label>
                                            <input class="form-control"  type="text" placeholder="<?php echo TEXT_PAYPAL_MAILID;?>" value="<?php echo ($txtPaypalEmail);?>" name="txtPaypalEmail" size="40" maxlength="100">
                                        </div>

                                        <?php } ?>
                                       
                                        <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 */
?>																
