<script type="text/javascript">

function validate_admin(){
	if($("#admin_fname").val()==''){
		$("#msg_admin_fname").html('Please enter first name');
		$("#admin_fname").addClass('jquery_err');
		$("#admin_fname").focus();
		return false;
	}else{
		$("#msg_admin_fname").html('');
		$("#admin_fname").removeClass('jquery_err');
	}
	
	if($("#admin_lname").val()==''){
		$("#msg_admin_lname").html('Please enter last name');
		$("#admin_lname").addClass('jquery_err');
		$("#admin_lname").focus();
		return false;
	}else{
		$("#msg_admin_lname").html('');
		$("#admin_lname").removeClass('jquery_err');
	}
	
	if($("#admin_email_address").val()==''){
		$("#msg_admin_email_address").html('Please enter Email address');
		$("#admin_email_address").addClass('jquery_err');
		$("#admin_email_address").focus();
		return false;
	}else if($("#admin_email_address").val()!='' && !validateEmail($("#admin_email_address")) ){
		$("#msg_admin_email_address").html('Please enter valid Email address');
		$("#admin_email_address").addClass('jquery_err');
		$("#admin_email_address").focus();
		return false;
	}else{
		$("#msg_admin_email_address").html('');
		$("#admin_email_address").removeClass('jquery_err');
	}

	var password = $("#admin_password").val();
	var passed = validatePassword(password, {
		length:   [6, Infinity],
		numeric:  1,
		special:  1
	});
	
	if($("#admin_password").val()==''){
		$("#msg_admin_password").html('Please enter Password');
		$("#admin_password").addClass('jquery_err');
		$("#admin_password").focus();
		return false;
		
	}else if(!passed){
		$("#msg_admin_password").html('Password should have minimum 6 char and atleast one numeric or one special char.');
		$("#admin_password").addClass('jquery_err');
		$("#admin_password").focus();
		return false;
	}else if($("#admin_password").val()!='' && $("#admin_password").val()!=$("#admin_repassword").val()){
		$("#msg_admin_password").html('Password and Again password do not matched');
		$("#admin_password").addClass('jquery_err');
		$("#admin_password").focus();
		return false;
	}else{
		$("#msg_admin_password").html('');
		$("#admin_password").removeClass('jquery_err');
	}
	
}

function validate_email(email,admin_id){
	$("#loading_email").show();

	$.ajax({
		  url: "addadmin.php",
		  data: 'act=validate_email&email='+email.value+'&admin_id='+admin_id,
		  method: 'POST',
		  success: function(msg){
				$("#loading_email").hide();
		  		if(msg!=''){
					$("#admin_email_address").addClass('jquery_err');
					$("#msg_admin_email_address").html(msg);
		  		}else{
					$("#admin_email_address").removeClass('jquery_err');
					$("#msg_admin_email_address").html('');
		  		}
		  }
		});
}

$(document).ready(function(){
	
	$(".module").click()
	
	$("#admin_password").blur(function (){
		$("#loading_pass").show();

		var password = $("#admin_password").val();
		var passed = validatePassword(password, {
			length:   [6, Infinity],
			numeric:  1,
			special:  1
		});
		
		if($("#admin_password").val()==''){
			$("#msg_admin_password").html('Please enter Password');
			$("#admin_password").addClass('jquery_err');
			$("#loading_pass").hide();
			$("#admin_password").focus();
			return false;
		}else if(!passed){
			$("#msg_admin_password").html('Password should have minimum 6 char and atleast one numeric or one special char. ');
			$("#admin_password").addClass('jquery_err');
			$("#loading_pass").hide();
			$("#admin_password").focus();
			return false;
		}else{
			$("#admin_password").removeClass('jquery_err');
			$("#loading_pass").hide();
			$("#msg_admin_password").html('');
  		}
  		
 	});

	$("#admin_repassword").blur(function (){
		$("#loading_repass").show();

		if($("#admin_repassword").val()!==$("#admin_password").val()){
			$("#admin_repassword").addClass('jquery_err');
			$("#msg_admin_repassword").html('Password and Again password do not matched');
	  		$("#loading_repass").hide();
	  	}else{
			$("#admin_repassword").removeClass('jquery_err');
			$("#msg_admin_repassword").html('');
	  		$("#loading_repass").hide();
  		}

	});
});

</script>

<!-- Content (Right Column) -->
		<div id="content" class="box">

			<h1>Administrator &raquo; Add Administrator</h1>

			<?php if(isset($successMsg) && $successMsg!=''){ ?>
				<p class="msg done"><?php echo $successMsg; ?></p>
			<?php } ?>
			
			<?php if(isset($failedMsg) && $failedMsg!=''){ ?>
				<p class="msg error"><?php echo $failedMsg; ?></p>
			<?php } ?>

			<form action="addadmin.php" method="post" id="adminFrm" name="adminFrm" onsubmit="return validate_admin();">
			
				<?php if(isset($_REQUEST['admin_id']) && $_REQUEST['admin_id']!=''){ ?>
					<input type="hidden" id="admin_id" name="admin_id" value="<?php echo $adminDetail->admin_id ?>" >
				<?php } ?>
				
				<!-- Login -->
				<fieldset>
					<legend>Administrator Details</legend>
					<p class="nomt">
						<label for="inp-01" class="req">First Name:</label><br />
						<input type="text" size="50" name="admin_fname" id="admin_fname" value="<?php echo $adminDetail->admin_fname; ?>" class="input-text-02" id="" /><br/>
						<span class="smaller low message" id="msg_admin_fname">&nbsp;</span>
					</p>

					<p class="nomt">
						<label for="inp-01" class="req">Last Name:</label><br />
						<input type="text" size="50" name="admin_lname" id="admin_lname" value="<?php echo $adminDetail->admin_lname ?>" class="input-text-02" id="" /><br/>
						<span class="smaller low message" id="msg_admin_lname">&nbsp;</span>
					</p>

					<p class="nomt">
						<label for="inp-01" class="req">Email Address:</label><br />
						<input type="text" size="50" name="admin_email_address" id="admin_email_address" value="<?php echo $adminDetail->admin_email_address ?>" class="input-text-02" onblur="validate_email(this,'<?php echo $adminDetail->admin_id ?>');" /><span id="loading_email" class="aloader" style="display:none"><img src="images/ajaxLoader.gif"></span><br/>
						<span class="smaller low message" id="msg_admin_email_address">&nbsp;</span>
					</p>

					<?php if(!isset($adminDetail->admin_password) || $adminDetail->admin_password==''){ ?>

					<p>
						<label for="inp-02" class="req">Password:</label><br />
						<input type="password" size="50" name="admin_password" id="admin_password" value="<?php echo $adminDetail->admin_password ?>" class="input-text-02" id="inp-02" /><span id="loading_pass" class="aloader" style="display:none"><img src="images/ajaxLoader.gif"></span><br/>
						<span class="smaller low message" id="msg_admin_password">&nbsp;</span>
					</p>

					<p class="nomb">
						<label for="inp-03" class="req">Password again:</label><br />
						<input type="password" size="50" name="admin_repassword" id="admin_repassword" value="" class="input-text-02" id="inp-03" /><span id="loading_repass" class="aloader" style="display:none"><img src="images/ajaxLoader.gif"></span><br/>
						<span class="smaller low message" id="msg_admin_repassword">&nbsp;</span>
					</p>

					<?php } ?>

					<p class="nomb">
						<label for="inp-03" class="req">Status :</label><br />
						<select id="admin_status" name="admin_status" class="input-text-02">
							<option value="0" <?php echo ($adminDetail->admin_status==0) ? "selected='selected'" : "" ?>>Inactive</option>
							<option value="1" <?php echo (!isset($adminDetail->admin_status) || $adminDetail->admin_status==1) ? "selected='selected'" : "" ?>>Active</option>
						</select><br/>
						<span class="smaller low message" id="msg_admin_status">&nbsp;</span>
					</p>

				</fieldset>

				<?php if($adminDetail->admin_id!=1){ ?>
				
				<!-- Permissions -->
				<fieldset>
					<legend>Module Permissions</legend>
					
					  <table cellpadding="0" cellspacing="0"  width="100%">
					  
					  	<tr>
					  		<td align="center"><strong>Modules</strong></td> 		
					  		<td colspan="3" align="center"><strong>Access Level</strong></td> 		
					  	</tr>
					  
					  	<tr>
					  		<td>&nbsp;</td> 		
					  		<td align="center"> Add</td> 		
					  		<td align="center"> Edit</td> 		
					  		<td align="center"> Delete</td> 		
					  	</tr>
					  
					<?php
						
						$accessArr = array();
						$accessEvents = array();
						
						if(isset($adminDetail->admin_access) && $adminDetail->admin_access!=''){
							$accessArr = explode(',',$adminDetail->admin_access);	
						}
						if(isset($adminDetail->access_event) && $adminDetail->access_event!=''){
							$accessEvents = unserialize($adminDetail->access_event);
							
						}
					 ?>
					
					<?php foreach ($adminModules as $key => $val){ ?>
					
					  	<tr>
					  		<td><input type="checkbox" name="admin_access[]" id="admin_access[]"  value="<?php echo $val->id ?>" <?php echo ($accessArr && in_array($val->id,$accessArr)) ? "checked='checked'" : '' ?>  />&nbsp;&nbsp;<label><?php echo $val->name; ?></label></td> 		
					  		<td align="center"><input type="checkbox" name="access[<?php echo $val->id ?>][]" id="access[<?php echo $val->id ?>][]" value="add"  <?php echo ($accessEvents[$val->id] && in_array('add',$accessEvents[$val->id])) ? "checked='checked'" : ""  ?>  /></td> 		
					  		<td align="center"><input type="checkbox" name="access[<?php echo $val->id ?>][]" id="access[<?php echo $val->id ?>][]" value="edit" <?php echo ($accessEvents[$val->id] && in_array('edit',$accessEvents[$val->id])) ? "checked='checked'" : ""  ?>  /></td> 		
					  		<td align="center"><input type="checkbox" name="access[<?php echo $val->id ?>][]" id="access[<?php echo $val->id ?>][]" value="delete" <?php echo ($accessEvents[$val->id] && in_array('delete',$accessEvents[$val->id])) ? "checked='checked'" : ""  ?>  /></td> 		
					  	</tr>
					  	
					<?php } ?>
					
					</table>

				</fieldset>
				
				<?php } ?>
				
				<?php if(isset($adminDetail->admin_id) && $adminDetail->admin_id!=''){ ?>
					<p><input type="submit" value="Edit Admin" id="submit" name="submit" class="input-submit" /></p>
				<?php }else{ ?>
					<p><input type="submit" value="Add Admin" id="submit" name="submit" class="input-submit" /></p>
				<?php } ?>
                 <input type="button" value="Cancel" id="cancel" name="cancel" class="input-submit" onclick="javascript:window.location='adminlist.php'" />
			</form>

		</div> <!-- /content -->