<?php
$abso = str_replace("\\","/", FCPATH);
$virt = str_replace("\\","/", base_url());
?>
<link rel="stylesheet" href="<?php echo base_url(); ?>dist/css/theme/common.css"/>
<link rel="stylesheet" href="<?php echo base_url(); ?>dist/css/theme/extension.css"/>
<link rel="stylesheet" href="<?php echo base_url(); ?>dist/css/theme/hover.css"/>
<link rel="stylesheet" href="<?php echo base_url(); ?>dist/css/theme/animate.css"/>
<link rel="stylesheet" href="<?php echo base_url(); ?>dist/css/theme/admin/dashboard.css"/>

<div class="content-wrapper">
	<section class="content-header">
			<h1 style="font-family:roboto">
					<b><?php echo $this->lang->line('voter'); ?></b>
			</h1>
			<ol class="breadcrumb">
					<li style="color:#0C0"><a href="#"><i class="fa fa-home"></i> <?php echo $this->lang->line('home'); ?></a></li>
					<li style="color:#0C0"><a href="#"><?php echo $this->lang->line('setup'); ?></a></li>
					<li class="active" style="color:#0C0"><?php echo $this->lang->line('setupTitle'); ?></li>
			</ol>
	</section>
		
	<div class="content registration m-0-b">
		<div class="row section-title p-5-l p-5-r p-1-t p-2-b text-center">
			<div class="col">
				<h1 class="m-0-t font-weight-bold"><?php echo $this->lang->line('setupTitle'); ?></h1>
				<p class="m-0-b"><?php echo $this->lang->line('setupTitleDesc'); ?></p>
			</div>
		</div>
		
		<div class="row m-5-b m-2-t">
			<div class="col-md-12">
			
				<div class="row m-1-b m-1-t p-0-y p-3-x p-xs-1-x">
					<div class="col message <?php echo (isset($respond) && $respond['type']) ? strtolower($respond['type']) : NULL; ?> text-center" id="message">
					<?php echo (isset($respond) && $respond['message']) ? $respond['message'] : ((strtolower($respond['type']) == 'success') ? $this->lang->line('success') : NULL); ?>
					</div>
				</div>
			
				<div class="nav-tabs-custom tab-success">
					<ul class="nav nav-tabs">
						<li class="active">
							<a href="#tab_1" data-toggle="tab" aria-expanded="true">
								<i class="fa fa-user"></i><span class="hidden-sm hidden-xs">&nbsp;&nbsp;<?php echo $this->lang->line('accSetup'); ?></span>
							</a>
						</li>
						<li>
							<a href="#tab_2" data-toggle="tab" aria-expanded="true">
								<i class="fa fa-money"></i><span class="hidden-sm hidden-xs">&nbsp;&nbsp;<?php echo $this->lang->line('billSetup'); ?></span>
							</a>
						</li>
						<li>
							<a href="#tab_3" data-toggle="tab" aria-expanded="true">
								<i class="fa fa-building"></i><span class="hidden-sm hidden-xs">&nbsp;&nbsp;<?php echo $this->lang->line('orgSetup'); ?></span>
							</a>
						</li>
						<!--
						<div class="pull-right">
							<a href="<?php echo base_url(); ?>signout" class="btn btn-danger"><i class="fa fa-power-off"></i><span class="hidden-sm hidden-xs">&nbsp;&nbsp;Log Out</span></a>
						</div>
						-->
					</ul>
				
					<div class="tab-content">
						<div class="tab-pane active" id="tab_1">		
							<h3 class="hidden-sm hidden-md hidden-lg hidden-xl m-2-b"><i class="fa fa-user"></i>&nbsp;&nbsp;<?php echo $this->lang->line('accSetup'); ?></h3>
							
							<div class="row">
								<div class="col-md-8">
									<form class="form-horizontal registration-form" method="post" action="<?php echo base_url(); ?>account/save" id="account_form" enctype="multipart/form-data">
										<input type="hidden" name="type" value="ACCOUNT" />
										<input type="hidden" name="ID" id="account_ID" value="<?php echo ($account) ? $account['fldid'] : 0; ?>" />
										<input type="hidden" name="user" id="account_user" value="<?php echo $userID; ?>" />
										
										<div class="box box-solid box-default">
											<div class="box-header">
												<h3 class="box-title"><?php echo $this->lang->line('accDet'); ?></h3>
											</div>
											
											<div class="box-body">
												<div class="row">
													<div class="col-xs-12">
														<div class="helper helper-right m-2-y">* - <?php echo $this->lang->line('required'); ?></div>
													</div>
												</div>
												
												<div class="row">
													<div class="col-md-4">
														<div class="form-group">
															<label class="col-xs-12 registration-form-label"><?php echo $this->lang->line('profImg'); ?></label>
															<div class="col-xs-12">
																<?php																
																$image = $virt."dist/img/_sys/user.png";
																if($account['imageFile'] && file_exists($abso."img/profile/".$account['imageFile'])) {
																	$image = $virt."img/profile/".$account['imageFile'];
																}
																?>
																
																<div class="uploader">
																	<input type="file" id="account_imageFile" name="account_imageFile" accept="image/*" />
																	<input type="hidden" name="account[resetImageFile]" value="0" />
																	<img src="<?php echo $image; ?>" class="img-responsive" />
																</div>
																
																<div class="helper">
																	<ul>
																		<li><?php echo $this->lang->line('profImgHelper'); ?></li>
																		<li><a href="javascript:void(0);" onclick="return clearProfileImage(this);"><?php echo $this->lang->line('profImgReset'); ?></a></li>
																	</ul>
																</div>
															</div>
														</div>
														
													</div>
													
													<div class="col-md-8">												
														<div class="form-group">
															<label class="col-xs-12 required registration-form-label"><?php echo $this->lang->line('fullName'); ?></label>
															<div class="col-xs-12">
																<input type="text" class="form-control validate[required] registration-form-input" name="account[username]" id="account_username"
																	value="<?php echo ($account) ? $account['username'] : NULL; ?>" />
															</div>
														</div>
														
														<div class="form-group">
															<label class="col-xs-12 required registration-form-label"><?php echo $this->lang->line('email'); ?></label>
															<div class="col-xs-12">
																<span class="form-control registration-form-input" id="account_email"><?php echo ($account) ? $account['email'] : NULL; ?></span>
																<div class="helper">
																	<ul>
																		<li><?php echo $this->lang->line('emailHelper1'); ?></li>
																		<li><?php echo $this->lang->line('emailHelper2'); ?></li>
																	</ul>
																</div>
															</div>
														</div>													
													</div>
												</div>
											</div>								
											
											<div class="box-footer">
												<div class="flex-btn-group">
													<button type="submit" class="btn btn-block btn-success"><i class="fa fa-check"></i>&nbsp;<?php echo $this->lang->line('save'); ?></button>
													<a href="<?php echo base_url(); ?>account" class="btn btn-danger"><i class="fa fa-backward"></i>&nbsp;<?php echo $this->lang->line('back'); ?></a>
												</div>
											</div>
										</div>
									</form>	
								</div>
								
								<div class="col-md-4">
									<form class="form-horizontal registration-form" method="post" action="<?php echo base_url(); ?>account/save" id="password_form">
										<input type="hidden" name="type" value="PASSWORD" />
										<input type="hidden" name="ID" id="password_ID" value="<?php echo ($account) ? $account['fldid'] : 0; ?>" />
										<input type="hidden" name="user" id="password_user" value="<?php echo $userID; ?>" />
										
										<div class="box box-solid box-default">
											<div class="box-header">
												<h3 class="box-title"><?php echo $this->lang->line('changePwd'); ?></h3>
											</div>
											
											<div class="box-body">
												<div class="row">
													<div class="col-xs-12">
														<div class="helper helper-right">* - <?php echo $this->lang->line('required'); ?></div>
													</div>
												</div>
												
												<div class="form-group">
													<label class="col-xs-12 required registration-form-label"><?php echo $this->lang->line('oldPwd'); ?></label>
													<div class="col-xs-12">
														<input type="password" class="form-control validate[required] registration-form-input" name="password[oldpassword]" id="account_old_password" />
														<div class="helper">
															<ul>
																<li><?php echo $this->lang->line('oldPwdHelper1'); ?></li>
																<li><?php echo $this->lang->line('oldPwdHelper2'); ?></li>
															</ul>
														</div>
													</div>
												</div>
												
												<div class="form-group">
													<label class="col-xs-12 required registration-form-label"><?php echo $this->lang->line('newPwd'); ?></label>
													<div class="col-xs-12">
														<input type="password" class="form-control validate[required] registration-form-input" name="password[newpassword]" id="account_new_password" />
														<div class="helper">
															<ul>
																<li><?php echo $this->lang->line('newPwdHelper1'); ?></li>
																<li><?php echo $this->lang->line('newPwdHelper2'); ?></li>
																<li><?php echo $this->lang->line('newPwdHelper3'); ?></li>
															</ul>
														</div>
													</div>
												</div>
												
												<div class="form-group">
													<label class="col-xs-12 required registration-form-label"><?php echo $this->lang->line('confPwd'); ?></label>
													<div class="col-xs-12">
														<input type="password" class="form-control validate[required] registration-form-input" name="password[confirmpassword]" id="account_confirm_password" />
														<div class="helper">
															<ul>
																<li><?php echo $this->lang->line('confPwdHelper1'); ?></li>
																<li><?php echo $this->lang->line('confPwdHelper2'); ?></li>
															</ul>
														</div>
													</div>
												</div>
											</div>
										</div>
									
										<div class="box-footer">
											<div class="flex-btn-group">
												<button type="submit" class="btn btn-block btn-success"><i class="fa fa-check"></i>&nbsp;<?php echo $this->lang->line('save'); ?></button>
												<a href="<?php echo base_url(); ?>account" class="btn btn-danger"><i class="fa fa-backward"></i>&nbsp;<?php echo $this->lang->line('back'); ?></a>
											</div>
										</div>
									</form>
								</div>
							</div>						
						</div>
						
						<div class="tab-pane" id="tab_2">
							<h3 class="hidden-sm hidden-md hidden-lg hidden-xl m-2-b"><i class="fa fa-money"></i>&nbsp;&nbsp;<?php echo $this->lang->line('billSetup'); ?></h3>
							
							<div class="row">
								<div class="col-md-6">
									<form class="form-horizontal registration-form" method="post" action="<?php echo base_url(); ?>account/save" id="billing_form">
										<input type="hidden" name="type" value="BILLING" />
										<input type="hidden" name="ID" id="billing_ID" value="<?php echo ($billing) ? $billing['ID'] : 0; ?>" />
										<input type="hidden" name="user" id="billing_user" value="<?php echo $userID; ?>" />
										
										<div class="box box-solid box-default">
											<div class="box-header">
												<h3 class="box-title"><?php echo $this->lang->line('billDet'); ?></h3>
											</div>
											
											<div class="box-body">
												<div class="row">
													<div class="col-xs-12">
														<div class="helper helper-right">* - <?php echo $this->lang->line('required'); ?></div>
													</div>
												</div>
												
												<div class="row p-0">
													<div class="col-md-6">
														<div class="form-group">
															<label class="col-xs-12 required registration-form-label"><?php echo $this->lang->line('phoneNo'); ?></label>
															<div class="col-xs-12">
																<input type="text" class="form-control validate[required] registration-form-input" name="billing[phoneNo]" id="billing_phoneno" 
																	value="<?php echo ($billing) ? $billing['phoneNo'] : NULL; ?>" />
															</div>
														</div>
													</div>
													<div class="col-md-6">
														<div class="form-group">
															<label class="col-xs-12 registration-form-label"><?php echo $this->lang->line('faxNo'); ?></label>
															<div class="col-xs-12">
																<input type="text" class="form-control registration-form-input" name="billing[faxNo]" id="billing_faxno" 
																	value="<?php echo ($billing) ? $billing['faxNo'] : NULL; ?>" />
															</div>
														</div>
													</div>
												</div>
												
												<div class="form-group">
													<label class="col-xs-12 required registration-form-label"><?php echo $this->lang->line('email'); ?></label>
													<div class="col-xs-12">
														<input type="text" class="form-control validate[required] registration-form-input" name="billing[email]" id="billing_email" 
															value="<?php echo ($billing) ? $billing['email'] : (($account['email']) ? $account['email'] : NULL); ?>" />
													</div>
												</div>
												
												<div class="form-group">
													<label class="col-xs-12 required registration-form-label"><?php echo $this->lang->line('address'); ?></label>
													<div class="col-xs-12">
														<textarea class="form-control validate[required] registration-form-input" rows="5"
																name="billing[address]" id="billing_address"><?php echo ($billing) ? $billing['address'] : NULL; ?></textarea>
													</div>
												</div>
												
												<div class="row p-0">
													<div class="col-md-6">
														<div class="form-group">
															<label class="col-xs-12 required registration-form-label"><?php echo $this->lang->line('postcode'); ?></label>
															<div class="col-xs-12">
																<input type="text" class="form-control validate[required] registration-form-input" name="billing[postcode]" id="billing_postcode" 
																	value="<?php echo ($billing) ? $billing['postcode'] : NULL; ?>" />
															</div>
														</div>
													</div>
													<div class="col-md-6">
														<div class="form-group">
															<label class="col-xs-12 required registration-form-label"><?php echo $this->lang->line('city'); ?></label>
															<div class="col-xs-12">
																<input type="text" class="form-control validate[required] registration-form-input" name="billing[city]" id="billing_city" 
																	value="<?php echo ($billing) ? $billing['city'] : NULL; ?>" />
															</div>
														</div>
													</div>
												</div>
												
												<div class="row p-0">
													<div class="col-md-6">
														<div class="form-group">
															<label class="col-xs-12 required registration-form-label"><?php echo $this->lang->line('country'); ?></label>
															<div class="col-xs-12">
																<select class="form-control validate[required] registration-form-input" name="billing[country]" id="billing_country" onchange="return populateBillingState();">
																</select>
															</div>
														</div>
													</div>
													<div class="col-md-6">
														<div class="form-group">
															<label class="col-xs-12 registration-form-label"><?php echo $this->lang->line('state'); ?></label>
															<div class="col-xs-12">
																<select class="form-control registration-form-input" name="billing[state]" id="billing_state">
																</select>
															</div>
														</div>
													</div>
												</div>
											</div>
											
											<div class="box-footer">
												<div class="flex-btn-group">
													<button type="submit" class="btn btn-block btn-success"><i class="fa fa-check"></i>&nbsp;<?php echo $this->lang->line('save'); ?></button>
													<a href="<?php echo base_url(); ?>account" class="btn btn-danger"><i class="fa fa-backward"></i>&nbsp;<?php echo $this->lang->line('back'); ?></a>
												</div>
											</div>
										</div>
									</form>
								</div>
								
								<div class="col-md-6">
								</div>
							</div>
						</div>
						
						<div class="tab-pane" id="tab_3">
							<h3 class="hidden-sm hidden-md hidden-lg hidden-xl m-2-b"><i class="fa fa-building"></i>&nbsp;&nbsp;<?php echo $this->lang->line('orgSetup'); ?></h3>
							
							<div class="row">
								<div class="col-md-6">
									<form class="form-horizontal registration-form" method="post" action="<?php echo base_url(); ?>account/save" id="organization_form" enctype="multipart/form-data">
										<input type="hidden" name="type" value="ORGANIZATION" />
										<input type="hidden" name="ID" id="organization_ID" value="<?php echo ($organization) ? $organization['ID'] : 0; ?>" />
										<input type="hidden" name="user" id="organization_user" value="<?php echo $userID; ?>" />
										
										<div class="box box-solid box-default">
											<div class="box-header">
												<h3 class="box-title"><?php echo $this->lang->line('orgDet'); ?></h3>
											</div>
											
											<div class="box-body">
												<div class="row">
													<div class="col-xs-12">
														<div class="helper helper-right">* - <?php echo $this->lang->line('required'); ?></div>
													</div>
												</div>
												
												<div class="form-group">
													<label class="col-xs-12 registration-form-label"><?php echo $this->lang->line('orgDet'); ?></label>
													<div class="col-xs-12">
														<?php														
														$image = $virt."dist/img/logo/logo_xs.png";
														if($organization['logoImage'] && file_exists($abso."img/logos/".$organization['logoImage'])) {
															$image = $virt."img/logos/".$organization['logoImage'];
														}
														?>
														
														<div class="uploader">
															<input type="file" name="organization_logoImage" accept="image/*" />
															<input type="hidden" name="organization[resetLogoImage]" value="0" />
															<img src="<?php echo $image; ?>" class="img-responsive" />
														</div>
														
														<div class="helper">
															<ul>
																<li><?php echo $this->lang->line('orgLogoHelper1'); ?></li>
																<li><a href="javascript:void(0);" onclick="return clearLogoImage(this);"><?php echo $this->lang->line('orgLogoHelper2'); ?></a></li>
															</ul>
														</div>
													</div>
												</div>
					
												<div class="form-group registration-form-group">
													<label class="col-xs-12 required registration-form-label"><?php echo $this->lang->line('orgName'); ?></label>
													<div class="col-xs-12">
														<input type="text" class="form-control validate[required] registration-form-input" name="organization[name]" id="organization_name"
															value="<?php echo ($organization) ? $organization['name'] : NULL; ?>" />
													</div>
												</div>
											
												<div class="form-group registration-form-group">
													<label class="col-xs-12 registration-form-label"></label>
													<div class="col-xs-12">
														<input type="text" class="form-control registration-form-input" name="organization[regNo]" id="organization_regno"
															value="<?php echo ($organization) ? $organization['regNo'] : NULL; ?>" />
													</div>
												</div>						
												
												<div class="row p-0">
													<div class="col-md-6">
														<div class="form-group">
															<label class="col-xs-12 registration-form-label"><?php echo $this->lang->line('phoneNo'); ?></label>
															<div class="col-xs-12">
																<input type="text" class="form-control registration-form-input" name="organization[phoneNo]" id="organization_phoneno" 
																	value="<?php echo ($organization) ? $organization['phoneNo'] : NULL; ?>" />
															</div>
														</div>
													</div>
													<div class="col-md-6">
														<div class="form-group">
															<label class="col-xs-12 registration-form-label"><?php echo $this->lang->line('faxNo'); ?></label>
															<div class="col-xs-12">
																<input type="text" class="form-control registration-form-input" name="organization[faxNo]" id="organization_faxno" 
																	value="<?php echo ($organization) ? $organization['faxNo'] : NULL; ?>" />
															</div>
														</div>
													</div>
												</div>
												
												<div class="form-group">
													<label class="col-xs-12 registration-form-label"><?php echo $this->lang->line('web'); ?></label>
													<div class="col-xs-12">
														<input type="text" class="form-control registration-form-input" name="organization[website]" id="organization_website" 
															value="<?php echo ($organization) ? $organization['website'] : NULL; ?>" />
													</div>
												</div>
												
												<div class="form-group">
													<label class="col-xs-12 registration-form-label"><?php echo $this->lang->line('email'); ?></label>
													<div class="col-xs-12">
														<input type="text" class="form-control registration-form-input" name="organization[email]" id="organization_email" 
															value="<?php echo ($organization) ? $organization['email'] : NULL; ?>" />
													</div>
												</div>
												
												<div class="form-group">
													<label class="col-xs-12 registration-form-label"><?php echo $this->lang->line('address'); ?></label>
													<div class="col-xs-12">
														<textarea class="form-control registration-form-input" rows="5"
															name="organization[address]" id="organization_address"><?php echo ($organization) ? $organization['address'] : NULL; ?></textarea>
													</div>
												</div>
												
												<div class="row p-0">
													<div class="col-md-6">
														<div class="form-group">
															<label class="col-xs-12 registration-form-label"><?php echo $this->lang->line('postcode'); ?></label>
															<div class="col-xs-12">
																<input type="text" class="form-control registration-form-input" name="organization[postcode]" id="organization_postcode" 
																	value="<?php echo ($organization) ? $organization['postcode'] : NULL; ?>" />
															</div>
														</div>
													</div>
													<div class="col-md-6">
														<div class="form-group">
															<label class="col-xs-12 registration-form-label"><?php echo $this->lang->line('city'); ?></label>
															<div class="col-xs-12">
																<input type="text" class="form-control registration-form-input" name="organization[city]" id="organization_city" 
																	value="<?php echo ($organization) ? $organization['city'] : NULL; ?>" />
															</div>
														</div>
													</div>
												</div>
												
												<div class="row p-0">
													<div class="col-md-6">
														<div class="form-group">
															<label class="col-xs-12 registration-form-label"><?php echo $this->lang->line('country'); ?></label>
															<div class="col-xs-12">
																<select class="form-control registration-form-input" name="organization[country]" id="organization_country" onchange="return populateOrganizationState();">
																</select>
															</div>
														</div>
													</div>
													<div class="col-md-6">
														<div class="form-group">
															<label class="col-xs-12 registration-form-label"><?php echo $this->lang->line('state'); ?></label>
															<div class="col-xs-12">
																<select class="form-control registration-form-input" name="organization[state]" id="organization_state">
																</select>
															</div>
														</div>
													</div>
												</div>
											</div>
											
											<div class="box-footer">
												<div class="flex-btn-group">
													<button type="submit" class="btn btn-block btn-success"><i class="fa fa-check"></i>&nbsp;<?php echo $this->lang->line('save'); ?></button>
													<a href="<?php echo base_url(); ?>account" class="btn btn-danger"><i class="fa fa-backward"></i>&nbsp;<?php echo $this->lang->line('back'); ?></a>
												</div>
											</div>
										</div>
									</form>
								</div>
								
								<div class="col-md-6">
								</div>
							</div>
						</div>
					</div>
				</div>
			
			</div>
		</div>
	</div>
</div>

<!-- SELECT2 -->
<link rel="stylesheet" href="<?php echo base_url(); ?>dist/vendors/select2/dist/css/select2.css">
<script src="<?php echo base_url(); ?>dist/vendors/select2/dist/js/select2.js"></script>
<!-- VALfldidATION ENGINE -->
<script src="<?php echo base_url(); ?>dist/vendors/validationengine/jquery.validationEngine.js"></script>
<script src="<?php echo base_url(); ?>dist/vendors/validationengine/languages/jquery.validationEngine-en.js"></script>
<script>
var billing_state = Number('<?php echo ($billing && $billing['state']) ? $billing['state'] : 0; ?>');
var billing_country = Number('<?php echo ($billing && $billing['country']) ? $billing['country'] : 0; ?>');
var organization_state = Number('<?php echo ($organization && $organization['state']) ? $organization['state'] : 0; ?>');
var organization_country = Number('<?php echo ($organization && $organization['country']) ? $organization['country'] : 0; ?>');
var emailexists = false;
$(function() {
	$('form#account_form').off('submit').on('submit',function() {
		if ($(this).validationEngine('validate')) {
			var proceed = true;
			
			var username = $.trim(	$('#account_username').val()	);
			if(username == "") {
				proceed =  false;
				$('#account_username').validationEngine('showPrompt', 'Please enter your full name', 'load','bottomRight',1);
				$('#account_username').parents('.form-group').eq(0).addClass('has-error');
			}
			if(!proceed) return false;
			
			/*
			var accemail = $.trim(	$('#account_email').val()	);
			if(accemail == "") {
				proceed =  false;
				$('#account_email').validationEngine('showPrompt', 'Please enter your account email', 'load','bottomRight',1);
				$('#account_email').parents('.form-group').eq(0).addClass('has-error');
			}
			if(!proceed) return false;
			
			if(emailexists) {
				proceed =  false;
				$('#account_email').validationEngine('showPrompt', 'Email already exists', 'load','bottomRight',1);
				$('#account_email').parents('.form-group').eq(0).addClass('has-error');
			}
			if(!proceed) return false;
			*/
			
			openLoader();
			return true;
		} else {
			return false;
		}
	});
	
	$('form#password_form').off('submit').on('submit',function() {
		if ($(this).validationEngine('validate')) {
			var proceed = true;
			
			var oldpass = $.trim(	$('#account_old_password').val()	);
			if(oldpass == "") {
				proceed =  false;
				$('#account_old_password').validationEngine('showPrompt', 'Please enter your old password', 'load','bottomRight',1);
				$('#account_old_password').parents('.form-group').eq(0).addClass('has-error');
			}
			if(!proceed) return false;
			
			var newpass = $.trim(	$('#account_new_password').val()	);
			if(newpass == "") {
				proceed =  false;
				$('#account_new_password').validationEngine('showPrompt', 'Please enter your new password', 'load','bottomRight',1);
				$('#account_new_password').parents('.form-group').eq(0).addClass('has-error');
			}
			if(!proceed) return false;
			
			if(newpass.length < 5) {
				proceed =  false;
				$('#account_new_password').validationEngine('showPrompt', 'Password must be at least 5 characters', 'load','bottomRight',1);
				$('#account_new_password').parents('.form-group').eq(0).addClass('has-error');
			}
			if(!proceed) return false;
			
			var confpass = $.trim(	$('#account_confirm_password').val()	);
			if(confpass == "") {
				proceed =  false;
				$('#account_confirm_password').validationEngine('showPrompt', 'Please enter your new password confirmation', 'load','bottomRight',1);
				$('#account_confirm_password').parents('.form-group').eq(0).addClass('has-error');
			}
			if(!proceed) return false;
			
			if(confpass != newpass) {
				proceed =  false;
				$('#account_confirm_password').validationEngine('showPrompt', 'New password and confirmation not matched', 'load','bottomRight',1);
				$('#account_confirm_password').parents('.form-group').eq(0).addClass('has-error');
			}
			if(!proceed) return false;
			
			return true;
		} else {
			return false;
		}
	});
	
	$('form#billing_form').off('submit').on('submit',function() {
		if ($(this).validationEngine('validate')) {
			var proceed = true;
			
			var billemail = $.trim(	$('#billing_email').val()	);
			if(billemail == "") {
				proceed =  false;
				$('#billing_email').validationEngine('showPrompt', 'Please enter your billing email', 'load','bottomRight',1);
				$('#billing_email').parents('.form-group').eq(0).addClass('has-error');
			}
			if(!proceed) return false;
			
			var address = $.trim(	$('#billing_address').val()	);
			if(address == "") {
				proceed =  false;
				$('#billing_address').validationEngine('showPrompt', 'Please enter your billing address', 'load','bottomRight',1);
				$('#billing_address').parents('.form-group').eq(0).addClass('has-error');
			}
			if(!proceed) return false;
			
			var postcode = $.trim(	$('#billing_postcode').val()	);
			if(postcode == "") {
				proceed =  false;
				$('#billing_postcode').validationEngine('showPrompt', 'Please enter your billing postcode', 'load','bottomRight',1);
				$('#billing_postcode').parents('.form-group').eq(0).addClass('has-error');
			}
			if(!proceed) return false;
			
			var city = $.trim(	$('#billing_city').val()	);
			if(city == "") {
				proceed =  false;
				$('#billing_city').validationEngine('showPrompt', 'Please enter your billing city', 'load','bottomRight',1);
				$('#billing_city').parents('.form-group').eq(0).addClass('has-error');
			}
			if(!proceed) return false;
			
			var country = $.trim(	$('#billing_country').val()	);
			if(country == "") {
				proceed =  false;
				$('#billing_country').validationEngine('showPrompt', 'Please select your billing country', 'load','bottomRight',1);
				$('#billing_country').parents('.form-group').eq(0).addClass('has-error');
			}
			if(!proceed) return false;
			
			return true;
		} else {
			return false;
		}
	});
	
	$('form#organization_form').off('submit').on('submit',function() {
		if ($(this).validationEngine('validate')) {
			var proceed = true;
			
			var name = $.trim(	$('#organization_name').val()	);
			if(name == "") {
				proceed =  false;
				$('#organization_name').validationEngine('showPrompt', 'Please enter your organization name', 'load','bottomRight',1);
				$('#organization_name').parents('.form-group').eq(0).addClass('has-error');
			}
			if(!proceed) return false;
			
			return true;
		} else {
			return false;
		}
	});
	
	$('#account_email').off('change').on('change',function() {
		var fldid = Number($('#fldid').val());
		var email = $.trim($('#account_email').val());
		
		emailexists = false;		
		$.post('<?php echo base_url(); ?>account/checkemailexistsajax', { fldid : fldid, email : email }, $.debounce(250,function(r) {
			if(Number(r)) {
				emailexists = true;
				$('#account_email').validationEngine('showPrompt', 'Account already exists', 'load','bottomRight',1);
				$('#account_email').parents('.form-group').eq(0).addClass('has-error');
			}
		}));
	});

	$('.uploader input[type="file"]').off('change').on('change', function() {
		var $img = $(this).siblings('img').eq(0);
		var input = this;
		
		if (input.files && input.files[0]) {
			$img.attr('src', '');
			var reader = new FileReader();			
			reader.onload = function (e) {
				$img.attr('src', e.target.result);
			}			
			reader.readAsDataURL(input.files[0]);
		}	
		$(this).siblings('input[type="hidden"]').val('0');
		closeLoader();
	});
	
	$('.uploader input[type="file"]').off('click').on('click', function() {
		openLoader();
	});
		
	$('#account_new_password').off('change').on('change',function() {
		$('#account_new_password').parents('.form-group').eq(0).removeClass('has-error');
		var newpass = $.trim(	$('#account_new_password').val()	);
		
		if(newpass == "") {
			$('#account_new_password').validationEngine('showPrompt', 'Please enter your new password', 'load','bottomRight',1);
			$('#account_new_password').parents('.form-group').eq(0).addClass('has-error');
		} else {		
			if(newpass.length < 5) {
				$('#account_new_password').validationEngine('showPrompt', 'Password must be at least 5 characters', 'load','bottomRight',1);
				$('#account_new_password').parents('.form-group').eq(0).addClass('has-error');
			}
		}
	});
	
	$('#account_confirm_password').off('change').on('change',function() {
		$('#account_confirm_password').parents('.form-group').eq(0).removeClass('has-error');
		
		var newpass = $.trim(	$('#account_new_password').val()	);
		var confpass = $.trim(	$('#account_confirm_password').val()	);
		
		if(confpass == "") {
			$('#account_confirm_password').validationEngine('showPrompt', 'Please enter your new password confirmation', 'load','bottomRight',1);
			$('#account_confirm_password').parents('.form-group').eq(0).addClass('has-error');
		} else {		
			if(confpass != newpass) {
				$('#account_confirm_password').validationEngine('showPrompt', 'New password and confirmation not matched', 'load','bottomRight',1);
				$('#account_confirm_password').parents('.form-group').eq(0).addClass('has-error');
			}
		}
	});
	
	populateBillingCountry();
	populateOrganizationCountry();
});

/**
* BILLING
*/
function populateBillingCountry() {
	$("#billing_country").html('');
	var $option = $('<option />', { 'value' : 0 });
			if( 0 == billing_country ) $option.attr('selected','selected').prop('selected',1);
			$option.html('OTHER');
			$option.appendTo($('#billing_country'));
	
	$.post('<?php echo base_url(); ?>account/getcountriesajax',{},function(r) {
		var obj = JSON.parse(r);
		if(obj) {
			$.each(obj,function(k,v) {
				var $option = $('<option />', { 'value' : v['ID'] });
						if( Number(v['ID']) == billing_country ) $option.attr('selected','selected').prop('selected',1);
						$option.html(v['name']);
						$option.appendTo($('#billing_country'));
			});
	
			$("#billing_country").select2({
				placeholder: "Select Country",
				allowClear : true,
				dropdownParent: $('#billing_form')
			});
			
			populateBillingState();
			
			$('.select2-selection').addClass('form-control');
		}
	});	
}

function populateBillingState() {
	billing_country = Number($('#billing_country').val());
	if(!billing_country) return;
	$("#billing_state").html('');
	var $option = $('<option />', { 'value' : 0 });
			if( 0 == billing_state ) $option.attr('selected','selected').prop('selected',1);
			$option.html('OTHER');
			$option.appendTo($('#billing_state'));
	
	$.post('<?php echo base_url(); ?>account/getstatesajax',{country:billing_country},function(r) {
		var obj = JSON.parse(r);
		if(obj) {
			$.each(obj,function(k,v) {
				var $option = $('<option />', { 'value' : v['ID'] });
						if( Number(v['ID']) == billing_state ) $option.attr('selected','selected').prop('selected',1);
						$option.html(v['name']);
						$option.appendTo($('#billing_state'));
			});
	
			$("#billing_state").select2({
				placeholder: "Select State",
				allowClear : true,
				dropdownParent: $('#billing_form')
			});
			
			$('.select2-selection').addClass('form-control');
		}
	});	
}

/**
* ORGANIZATION
*/
function populateOrganizationCountry() {
	$("#organization_country").html('');
	var $option = $('<option />', { 'value' : 0 });
			if( 0 == organization_country ) $option.attr('selected','selected').prop('selected',1);
			$option.html('OTHER');
			$option.appendTo($('#organization_country'));
	
	$.post('<?php echo base_url(); ?>account/getcountriesajax',{},function(r) {
		var obj = JSON.parse(r);
		if(obj) {
			$.each(obj,function(k,v) {
				var $option = $('<option />', { 'value' : v['ID'] });
						if( Number(v['ID']) == organization_country ) $option.attr('selected','selected').prop('selected',1);
						$option.html(v['name']);
						$option.appendTo($('#organization_country'));
			});
	
			$("#organization_country").select2({
				placeholder: "Select Country",
				allowClear : true,
				dropdownParent: $('#organization_form')
			});
			
			populateOrganizationState();
			
			$('.select2-selection').addClass('form-control');
		}
	});	
}

function populateOrganizationState() {
	organization_country = Number($('#organization_country').val());
	if(!organization_country) return;
	$("#organization_state").html('');
	var $option = $('<option />', { 'value' : 0 });
			if( 0 == organization_state ) $option.attr('selected','selected').prop('selected',1);
			$option.html('OTHER');
			$option.appendTo($('#organization_state'));
	
	$.post('<?php echo base_url(); ?>account/getstatesajax',{country:organization_country},function(r) {
		var obj = JSON.parse(r);
		if(obj) {
			$.each(obj,function(k,v) {
				var $option = $('<option />', { 'value' : v['ID'] });
						if( Number(v['ID']) == organization_state ) $option.attr('selected','selected').prop('selected',1);
						$option.html(v['name']);
						$option.appendTo($('#organization_state'));
			});
	
			$("#organization_state").select2({
				placeholder: "Select State",
				allowClear : true,
				dropdownParent: $('#organization_form')
			});
			
			$('.select2-selection').addClass('form-control');
		}
	});	
}

/**
* HELPER
*/
function clearProfileImage(me) {
	var $u = $(me).parents('.form-group').eq(0).find('.uploader').eq(0);	
	var $i = $u.find('img').eq(0);
	var def = '<?php echo base_url(); ?>dist/img/_sys/user.png';
	
	if(	$.trim(	$i.attr('src')	) == def ) return;
	
	if(confirm('Are you sure to reset your profile image?')) {
		$i.attr('src',def);
		$i.siblings('input[type="hidden"]').val('1');
	}
}

function clearLogoImage(me) {
	var $u = $(me).parents('.form-group').eq(0).find('.uploader').eq(0);	
	var $i = $u.find('img').eq(0);
	var def = '<?php echo base_url(); ?>dist/img/logo/logo_xs.png';
	
	if(	$.trim(	$i.attr('src')	) == def ) return;
	
	if(confirm('Are you sure to reset your organization logo image?')) {
		$i.attr('src',def);
		$i.siblings('input[type="hidden"]').val('1');
	}
}

function deleteAccount() { return confirm('Are you sure to delete this account?'); }

function closeAlert(me) { $(me).slideUp(); }
</script>




























