var Tpassword=true; 
var TEmail =true;

$(document).ready(function() {		 
	
	

	setTimeout(function(){
		$('.MsgError').remove();
	},10000);


	$('#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('');				
			 
		}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();
			 
		}
		/* else if($.inArray(lenvalue.charAt(0), ['9','8','7']) == -1){
			$('#msgp').html('');
			$('#msgp').html('Phone Number Not Valid');
			$('#errorbox').show();
			$(this).focus();
		} */
	});
	
	
	

	
	$(document).on('click', '#LoginBTN', 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){	
					if(data['FormSubmit']==0){
						window.location.href=HTTP_PATH +'member';	
					}else{
						window.location.href=HTTP_PATH +'profile';
					}						
				}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,.errorCheckT,.errorForm', function () {
		$(this).removeClass('error');
		$(this).parent().find('.labelMsg').remove();
		$('#signUPDiv').html('');	
	});
	
	$(document).on('click','#LiginBoxDiv',function(){
		
		$('#loginLi').css('display','block');
		$('#ForgotLi').css('display','none');
	});
	$(document).on('click','#ShowForgotPWD',function(){
		$('#loginLi').css('display','none');
		$('#ForgotLi').css('display','block');
	});	
});

	$(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">Your Password must have minimum 8 letters and should be a combination of digit, capital, small letter and special character.</span>');
			$(this).addClass('error');		 
			 
			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">Your Password must have minimum 8 letters and should be a combination of digit, capital, small letter and special character.</span>');
			 Tpassword=false;
			 
			return false;
		} 				
	});
	
	$(document).on('blur', '#uCpassword', function (){
		var pass  = $.trim($("#uPassword").val());
		var cPass = $.trim($(this).val());
		if(pass.length < 8){			 
				$(this).val('');
				Tpassword==false;
				return false;
		}else{
			
			if(cPass!=pass){
				$(this).parent().append('<span class="labelMsg" style="color:red">Confirm Password does not match.</span>');
				$(this).addClass('error');		
			}else{
				Tpassword=true
			}
		}
		
	});



$(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;			
		} 
	});
	var pass = $.trim($("#uPassword").val());
		 
	if(pass.length < 8){
		$("#uPassword").addClass('error');		 
		 
		Tpassword=false;
		return false;
	}
	if(Tpassword == false){
		error=true;
		$("#uPassword").addClass('error');
		$("#uCpassword").addClass('error');
	}
	
	if(TEmail == false){
		error=true;
		
		//$(this).addClass('error');
	}
	var DST = 0;
	var Terms = 0;
	 
	if($('#uDSTScheme').prop('checked')){
		DST = 1;
	}else{
		error=true;
		$('.uDSTScheme').addClass('error');
	}
	if($('#uTerms').prop('checked')){
		Terms = 1;
	}else{
		error=true;
		$('.uTerms').addClass('error');
	}

	if(error==true){
		var divText='<span class="labelMsg" style="color:red;">All fields are required.</span>';
		$('#signUPDiv').html(divText);
		return false;
	}else{
				
		$('#SignUpBtn').find('img').css('display','block');
		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;">Confirm Password does 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;
						 $('#SignUpBtn').find('img').css('display','none');
						
					}	
					 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() {
	setTimeout(function(){
		if($('#ResearchPubID').val()==""){
			 
			$('.addmore').hide();
		}
	},2000);
	$(".add").click(function() {
		if($('.contents').find('input').length<4){
			$('<div class="more-field"><input required type="text" class="textbox" name="ResearchPub[]" placeholder="Add Another Research Publication"><span class="rem" ><a href="javascript:void(0);" ><i class="fa fa-minus-circle"></i></span></div>').appendTo(".contents");
		}
	});		
});
$(document).on('blur','#ResearchPubID',function(){
	if($('#ResearchPubID').val()==""){
		$('.addmore').hide();
	}else{
		$('.addmore').show();
	}
});

$(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 (mm/yyyy)');
		
		var Pid=$('input[name="PhpDegreeAward"]:checked').val();
		
		if(Pid!=null){			
			// alert(id);
			if(Pid=='Yes'){				
				$('#SubmitManuscript').html('');
				$('input[name="PhpDegreeAward"]').parent().parent().append('<div class="PhpDegreeAwardText">As your PhD degree has been awarded, you are not eligible for applying in AWSAR Program.</div>');
				$('#FormbtnDiv').hide();
			}else{
				$('.PhpDegreeAwardText').remove();
				$('#SubmitManuscript').html(SubmitManuscript);
				$('#FormbtnDiv').show();
				
				 	 
			}
		}else{
			$('#SubmitManuscript').html(SubmitManuscript);
			$('#FormbtnDiv').show();	
		}
		 
	}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 (mm/yyyy)');
		$('#ForPHDDetails').text('Your Fellowship Details:');
		$('#SubmitManuscript').html(SubmitManuscript);
		 
	}
	var PUid=$('input[name="PaperPublished"]:checked').val();	
	 
	if(PUid=='No'){
		$('#GuideCitationDiv').hide();
		$('#GuideCitationDiv').find('input').removeAttr('required');
	}else{
		$('#GuideCitationDiv').show();
		$('#GuideCitationDiv').find('input').prop('required',true);
	}
	 
});


$('.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 (mm/yyyy)');
		$('#ForPHDDetails').text('Your PhD Details:');		
	}else{
		$('#PhDJoining').text('Date of joining (mm/yyyy)');
		$('#ForPHDDetails').text('Your Fellowship Details:');		 
	}
	$('#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('');
		$('input[name="PhpDegreeAward"]').parent().parent().append('<div class="PhpDegreeAwardText">As your PhD degree has been awarded, you are not eligible for applying in AWSAR Program.</div>');
		$('#FormbtnDiv').hide();
	}else{
		$('.PhpDegreeAwardText').remove();
		if(SubmitManuscript!=""){
			
			$('#SubmitManuscript').html(SubmitManuscript);	
			$('#FormbtnDiv').show();
		}		
	}
	

});
 
$(document).on('change','input[name="PaperPublished"]',function (event) {
	var id=$('input[name="PaperPublished"]:checked').val();	
	 
	if(id=='Yes'){
		$('#GuideCitationDiv').show();
		$('#GuideCitationDiv').find('input').prop('required',true);
	}else{
		$('#GuideCitationDiv').find('input').attr('value','');
		$('#GuideCitationDiv').hide();
		$('#GuideCitationDiv').find('input').removeAttr('required');
	}
	

});

$(document).ready(function(){
	/* if($('input[name="Gender"]').prop('checked')==false){		
		$('input[name="Gender"]').prop('checked'); 
	} */
	
});

$(document).on('click','.world-btn',function(){
	var id=$(this).data('id');
	$('#hiddenSubmit').val(id);
	//alert(id);
	var error=false;
	if(id==2 || id==1){
		$('#msform').find('input').removeAttr('required');
		$('#msform').find('textarea').removeAttr('required');
	}
	if(id==4){
		$('#msform').find('.errorForm').each(function () {
			$(this).removeClass('error');	
			$(this).val($.trim($(this).val()));
			if($(this).val()=="" || $(this).val()==0){	
				$(this).addClass('error');	
				
				error=true;			
			} 
		});
		
		if($('input[name="Gender"]:checked').val()==null){			 
			error=true;
		}
		if($('input[name="CurrentPosition"]').val()==null){
			error=true;
		}
		/* if($('input[name="PhpDegreeAward"]').val()==null){
			error=true;
		} */
		if($('input[name="PaperPublished"]').val()==null){
			error=true;
		}
		
		if($('#IsInformation').prop('checked')){
			$('.errorCheckT').removeClass('error');
		}else{
			$('.errorCheckT').addClass('error');
			error=true;
		}
		
		var files=$.trim($('#getuploadfilename').val());
		if(files==""){
			$('#FormErrorMea').html('Please upload word file.');
			 return false;
		}
		if(error==true){
			$('#FormErrorMea').html('Please Fill all fields.');
			return false;
		}else{
			$('#ViewConfirmModel').css('display','block');
			return false;
		}
		
	}
	
	if(id==9){
		$('#msform').find('.errorForm').each(function () {
			$(this).removeClass('error');	
			$(this).val($.trim($(this).val()));
			if($(this).val()=="" || $(this).val()==0){	
				$(this).addClass('error');	
				
				error=true;			
			} 
		});
		
		if($('input[name="Gender"]:checked').val()==null){			 
			error=true;
		}
		if($('input[name="CurrentPosition"]').val()==null){
			error=true;
		}
		 
		if($('input[name="PaperPublished"]').val()==null){
			error=true;
		}
		
		 
		if(error==true){
			$('#FormErrorMea').html('Please Fill all fields.');
			return false;
		}else{
			//$('#ViewConfirmModel').css('display','block');
			return true;
		}	
		
		 
		
		
	}
});
$(document).on('click','#ViewConfirmModel .close',function(){
	$('#ViewConfirmModel').css('display','none');
});


$("#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';
				}
		   }
	});
});


 $(document).on("change","#fileUpload", function (e) {
    //var file = $(this)[0].files[0];
    var formData = new FormData();
	formData.append('fileUpload', $(this)[0].files[0]);
	 
	$('#getuploadfilename').attr('value','');
	$('#fileMsg').html('<img style="width:20px;" src="'+HTTP_PATH+'assets/images/icons/submit-loader.gif" />');
	$.ajax({
		url : HTTP_PATH+"member/doc_upload",
		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){						
				var count=data['count'];
				//$('#fileMsg').html(data['count']);
				$('#fileMsg').html('');
				$('#getuploadfilename').val(data['fileName']);
				$('#RemoveDocFile').show();
				$('#FormErrorMea').html('');
			}else{
				if(data['error']==""){
					alert('Your session has expired');
					window.location.href=HTTP_PATH;
				}else{
					$('#fileMsg').html(data['error']);
				}
			}
		}
	}); 
}); 

$(document).on('keyup','#tphone,#mobile,#GuideMobnumber,#feedPhone,.YearofPassing',function(e){
	  if (/\D/g.test(this.value)){
		this.value = this.value.replace(/\D/g, '');
	  }
});

 
 $('#awsarFeedback').on('focus','input[type="text"],input[type="email"],textarea', function () {	 
		$(this).removeClass('error');	
		 
	});
$(document).on('click','#FeedbackBtn',function(){
	var error=false;
	$('#awsarFeedback').find('input[type="text"],input[type="email"],textarea').each(function () {
		$(this).removeClass('error');	
		$(this).val($.trim($(this).val()));
		if($(this).val()=="" || $(this).val()==0){	
			$(this).addClass('error');			
			error=true;			
		} 
	});
	
	if(error==false){	
		$(this).attr('disabled','disabled');
		$(this).find('img').css('display','block');
		$('#awsarFeedback').submit();
	}
});	


$(document).on('click', '#ForgotBtnSubmit', function() {				 
	var data = $("#FormForgot").serialize();		
	document.getElementById("ForgotBtnSubmit").disabled = true;
	$.ajax({
		url: HTTP_PATH+"Member/forgot",
		data: data,
		type: "POST",
		dataType : "JSON",
		success: function(msg) {
			var data = msg;
			$('#LoginMSGF').html(data['text']);
			if(data['success']==true){	
				 						
			}else{
				document.getElementById("ForgotBtnSubmit").disabled = false;
				$('#ForgotEmail').addClass('error');
			}		
		}
	});
	return false;
});

$(document).on('click','#ForOtherDivBtn',function(){
	$('#ForOtherDiv').show();
	$(this).replaceWith('<a id="ForOtherDivBtnR" class="fa fa-minus-square"></a>');
});
$(document).on('click','#ForOtherDivBtnR',function(){
	$('#ForOtherDiv').hide();
	$('#ForOtherDiv').find('input').attr('value','');
	$(this).replaceWith('<a id="ForOtherDivBtn" class="fa fa-plus-square"></a>');
});
			
$(document).on('click','#RemoveDocFile',function(){
	$('#fileUpload').val('');
	$('#getuploadfilename').attr('value','');
	$(this).hide();
});			

function checkLength(el) {
  if (el.value.length != 10) {
    alert("Mobile No. length must be exactly 10 digit");
	$(el).addClass('error');
  }else{$(el).removeClass('error');}
}