var Tpassword=true; 
var TEmail =true;
$(document).ready(function() {	
		$('#uEmail').blur(function(){
			var eformat = '/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/';
			var email = $.trim($(this).val());			
			if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email) == false){
				//$('#msgp').html('');
				$(this).parent().append('<span class="labelMsg" style="color:red">Email Address Not Valid</span>');
				$(this).addClass('error');	
				TEmail=false;
				$(this).val('');				
				$(this).focus();
			}else{
				TEmail=true;
			}
		});
		
		$('#achievements').keyup(function(e){
			 var len=$(this).val().length;
			 $('#brefcount').text(len);
			 
		});
		
		
		$('#uPhone').keyup(function(e){
		  if (/\D/g.test(this.value)){
		    this.value = this.value.replace(/\D/g, '');
		  }
		});
		
		$('#uPhone').blur(function(){
			var lenght = $(this).val().length;
			var lenvalue = $(this).val();
			if(lenght != 10){
				//$('#msgp').html('');
				$('#createMsg').html('<span class="labelMsg" style="color:red">Please enter 10 digit mobile number.</span>');
				closeCreate();
				//$('#errorboxPOpUP').show();
				$(this).focus();
			}
			/* else if($.inArray(lenvalue.charAt(0), ['9','8','7']) == -1){
				$('#msgp').html('');
				$('#msgp').html('Phone Number Not Valid');
				$('#errorbox').show();
				$(this).focus();
			} */
		});
	
	
	
	$(document).on('blur', '#uPassword', function (){		 	 
		$('#uCpassword').val('');
		var pass = $.trim($(this).val());
		if(pass.length < 8){			 
			$(this).parent().append('<span class="labelMsg" style="color:red">Password Not Valid, At Least 8 Charecter.<br> Mininmum 1 Caps,1 Digit and 1 Special Character.</span>');
			 $(this).addClass('error');		 
			$(this).focus();
			Tpassword=false;
			return false;
		}
		
		var regex = new Array();
		regex.push("[A-Z]"); //Uppercase Alphabet.
		regex.push("[a-z]"); //Lowercase Alphabet.
		regex.push("[0-9]"); //Digit.
		regex.push("[$@$!%*#?&]"); //Special Character.		 
		
		var passed = 0;

		//Validate for each Regular Expression.
		for (var i = 0; i < regex.length; i++) {
			if(new RegExp(regex[i]).test(pass)) {
				passed++;
			}
		}
		
		if(passed<4){
			$(this).addClass('error');			
			$(this).parent().append('<span class="labelMsg" style="color:red">Password Not Valid.<br> Mininmum 1 Caps,1 Digit and 1 Special Character</span>');
			 Tpassword=false;
			$(this).focus();
			return false;
		} 				
	});
	
	$(document).on('blur', '#uCpassword', function (){
		var pass  = $.trim($("#uPassword").val());
		var cPass = $.trim($(this).val())
		if(pass==""){
			$("#uPassword").focus();
			$(this).val('');
			Tpassword==false;
			return false;
		}
		if(cPass!=pass){
			$(this).parent().append('<span class="labelMsg" style="color:red">Confirm Password is not match.</span>');
			$(this).addClass('error');		
		}else{
			Tpassword=true
		}
		
	});
	
	$("#FormLogin").submit(function() {				 
		var data = $("#FormLogin").serialize();		
		document.getElementById("LoginBTN").disabled = true;
		$.ajax({
			url: HTTP_PATH+"Member/signIn",
			data: data,
			type: "POST",
			dataType : "JSON",
			success: function(msg) {
				var data = msg;
				$('#LoginMSG').html(data['text']);
				if(data['success']==true){					 
					window.location.href=HTTP_PATH +'member';					 					
				}else{
					document.getElementById("LoginBTN").disabled = false;
					if(data['field_name']=="both"){
						$('#uName').addClass('error');
						$('#login-pass').addClass('error');
					}else{
						var dd=data['field_name'];
						$('#'+dd).addClass('error');
					}
				}				
			}
		});
		return false;
	});
	
	
	$('#FormLogin').on('focus','input[type=text],input[type=email],input[type=password]', function () {	 
		$(this).removeClass('error');	
		$('#LoginMSG').html('');
	});
	
	$('#msform').on('focus','.errorCheck', function () {
		$(this).removeClass('error');
		$(this).parent().find('.labelMsg').remove();
		$('#signUPDiv').html('');	
	});

});





$(document).on('click','#SignUpBtn',function(){	 
	var error=false;
	$('#msform').find('.errorCheck').each(function () {
		$(this).removeClass('error');	
		$(this).val($.trim($(this).val()));
		if($(this).val()=="" || $(this).val()==0){	
			$(this).addClass('error');	
			
			error=true;			
		} 
	});
	if(Tpassword == false){
		error=true;
		$("#uPassword").addClass('error');
		$("#uCpassword").addClass('error');
	}
	if(TEmail == false){
		error=true;
		
		//$(this).addClass('error');
	}
	

	if(error==true){
		var divText='<span class="labelMsg" style="color:red;">All fields are required.</span>';
		$('#signUPDiv').html(divText);
		return false;
	}else{
		var DST = 0;
		var Terms = 0;
		if($('#uDSTScheme').prop('checked')){
			DST = 1;
		}
		if($('#uTerms').prop('checked')){
			Terms = 1;
		}		
		
		document.getElementById("SignUpBtn").disabled = true;
		var data = {};
		data.EmailId   = $("#uEmail").val();		 
		data.Firstname = $("#uFname").val();
		data.Lastname  = $("#uLname").val();
		data.Password  = $("#uPassword").val();	
		data.CPassword = $("#uCpassword").val();
		data.AwsarDST  = DST;
		data.Terms     = Terms;		
		
		if(data.Password!=data.CPassword){
			var divText='<span class="labelMsg" style="color:red;">Password not match.</span>';
			$('#signUPDiv').html(divText);
			return false;	
		}else{	
			$.ajax({
				url: HTTP_PATH+"Member/signUp",
				data: data,
				type: "POST",
				dataType : "JSON",
				success: function(msg) {
					var data = msg;					 
					if(data['success']==true){						
						$('#signUPDiv').html('<span class="labelMsg" style="color:green;">You are registered Successfully.</span>');
						window.location.href=HTTP_PATH +'member';
					}else if(data['success']==false){
						$('#signUPDiv').html(data['text']);						
						 document.getElementById("SignUpBtn").disabled = false;
						
					}	
					 setTimeout(function(){
						$('#signUPDiv').html('');
					 },6000);				
				}
			});
		}
		 
	}

});

$(document).on('click','#prevBtnReset, .ff-close-btn',function(){	 
	$('#msform').find('.errorCheck').val('');
	$('#msform').find('.errorCheck').removeClass('error');
	$('#msform').find('.errorCheck').parent().find('.labelMsg').remove();
	$('#msform').find('input[type="checkbox"]').prop('checked',false);
});



var radioLastDiv='';
var SubmitManuscript='';

$(document).ready(function() {
	$(".add").click(function() {
		if($('.contents').find('input').length<4){
			$('<div class="more-field"><input type="text" class="textbox" name="ResearchPub[]" placeholder="Add More Details of Research Publications"><span class="rem" ><a href="javascript:void(0);" ><i class="fa fa-minus-circle"></i></span></div>').appendTo(".contents");
		}
	});		
});

$(window).on('load',function(){
	var id=$('input[name="CurrentPosition"]:checked').val();
	// alert(id);
	SubmitManuscript = $('#SubmitManuscript').html();
	if(id==null || id==1){		
		$('input[name="CurrentPosition"][value="1"]').prop('checked',true);
		$('#search-last').find('.firstL').attr('value','');	
		radioLastDiv = $('#search-last').html();		 
		$('#search-all').html($('#search-first').html());		
		$('#search-last').remove();
		$('#search-first').remove();
		$('#PhDJoining').text('Date of Registration.');
		
		var Pid=$('input[name="PhpDegreeAward"]:checked').val();
		
		if(Pid!=null){			
			// alert(id);
			if(Pid=='Yes'){				
				$('#SubmitManuscript').html('<div>As your PhD degree has been awarded or you have already defended your thesis, you are not eligible for applying in AWSAR Program.</div>');
			}else{
				$('#SubmitManuscript').html(SubmitManuscript);	
				 	 
			}
		}else{$('#SubmitManuscript').html(SubmitManuscript);}
		 
	}else{
		
		$('#search-first').find('.firstS').attr('value','');				
		radioLastDiv = $('#search-first').html();
		$('#search-all').html($('#search-last').html());			
		$('#search-last').remove();
		$('#search-first').remove();
		$('#PhDJoining').text('Date of joining');
		$('#SubmitManuscript').html(SubmitManuscript);
		 
	}
	 
});


$('.contents').on('click', '.rem', function() {
	$(this).parent("div").remove();
});


$(document).on('change','input[name="CurrentPosition"]',function (event) {
	var id=$('input[name="CurrentPosition"]:checked').val();
	// alert(id);
	
	if(id==null || id==1){
		$('#PhDJoining').text('Date of Registration.');
		
	}else{
		$('#PhDJoining').text('Date of joining');
			
		 
	}
	$('#SubmitManuscript').html(SubmitManuscript);
	var getAll=$('#search-all').html();
	$('#search-all').html(radioLastDiv);
	radioLastDiv=getAll;
	$('input[name="PhpDegreeAward"]').prop('checked',false);

}); 


$(document).on('change','input[name="PhpDegreeAward"]',function (event) {
	var id=$('input[name="PhpDegreeAward"]:checked').val();	
	 
	if(id=='Yes'){
		SubmitManuscript = $('#SubmitManuscript').html();
		$('#SubmitManuscript').html('<div>As your PhD degree has been awarded or you have already defended your thesis, you are not eligible for applying in AWSAR Program.</div>');
	}else{
		if(SubmitManuscript!=""){
			$('#SubmitManuscript').html(SubmitManuscript);	
		}		
	}
	

});



$(document).on('click','.world-btn',function(){
	var id=$(this).data('id');
	$('#hiddenSubmit').val(id);
	if(id==3){
		var files=$('#file-upload')[0].files[0];
		if(files){
			return true;
		}else{
			return false;
		}
	}
});

$("#UploadUserImage").on("change", function (e) {
    //var file = $(this)[0].files[0];
    var formData = new FormData();
	formData.append('UploadUserImage', $(this)[0].files[0]);
	var img = HTTP_PATH+'assets/images/icons/submit-loader.gif';
	$('#proImg').find('img').attr('src',img);
	$.ajax({
		   url : HTTP_PATH+"Member/picupload",
		   type : 'POST',
		   data : formData,
		   dataType : "JSON",
		   processData: false,  // tell jQuery not to process the data
		   contentType: false,  // tell jQuery not to set contentType
		   success : function(msg) {
			  var data = msg;					 
				if(data['success']==true){						
					 
					window.location.href=HTTP_PATH +'profile';
				}
		   }
	});
});
									 