<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(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 -->