$().ready(function(){
    $('#mobile').on('focus',function(){
        
        $('#mobile').val('+91');
     
    });
    
});


$.validator.setDefaults({
	submitHandler: function () {
	}
});



$().ready(function () {
	$("#Register").validate({

		rules: {

			mobile: {
				required: true,
				minlength: 10
			},

	


			password: {
				required: true,
				minlength: 5
			},
			rcode: {
				required: true,
				minlength: 5
			},

			remember: "required",

		},
// 		messages: {

// 			password: {
// 				required: "Please provide a password",
// 				minlength: ""
// 			},

		/*	email: "Please enter a valid email address",
			remember: "Please accept our policy",*/
// 		}

	});
});

//===============================================login validation=======================================================
$().ready(function () {
	$("#loginForm").validate({

		rules: {

			login_mobile: {
				required: true,
			

			},

			login_password: {
				required: true,
				
			},
		},
	/*	messages: {

			password: {
				required: "Please provide a password",
				minlength: "5"
			},

			email: "Please enter a valid email address",
			remember: "Please accept our policy",
		} */

	});
});

$(document).ready(function () {

	$("#Register").on('submit', (function (e) {
	   // alert('hello');
		e.preventDefault();
		var mobile = $('input#mobile').val();
	
		var password = $('input#password').val();
		var rcode = $('input#rcode').val();

		if ((mobile) == "") {
			$("input#mobile").focus();
			$('#mobile').addClass('borderline');
			return false;
		}
		if (mobile.length < 10) {
			$("input#mobile").focus();
			$('#mobile').addClass('borderline');
			return false;
		}


		if ((password) == "") {
			$("input#password").focus();
			$('#password').addClass('borderline');
			return false;
		}
		if (password.length < 5) {
			$("input#password").focus();
			alert("Please enter 5 or more digit password.");
			$('#password').addClass('borderline');
			return false;
		}
		if ((rcode) == "") {
			$("input#rcode").focus();
			$('#rcode').addClass('borderline');
			return false;
		}
		if ($('#remember').is(':checked')) { }
		else { return false; }

		$.ajax({
			type: "POST",
			url: "registerNow.php",
			data: new FormData(this),
			contentType: false,
			cache: false,
			processData: false,

			success: function (html) { //alert(html);
				var arr = html.split('~');
				
				if(arr.length == 3){
	                $('#registerthanksPopup').modal({ backdrop: 'static', keyboard: false })
					$('#registerthanksPopup').modal('show');
				}

				if (arr[0] == 1) {
					$("#Register")[0].reset();
					window.location.href = "login";
					$('#registerthanksPopup').modal({ backdrop: 'static', keyboard: false })
					$('#registerthanksPopup').modal('show');
				}
				else if (arr[0] == 2) {
					document.getElementById('regtoast').innerHTML = ('<font  style="color:#fff;">This mobile had registered</font>');
					$('#registertoast').modal({ backdrop: 'static', keyboard: false })
					$('#registertoast').modal('show');
				}
				else if (arr[0] == 3) {
					document.getElementById('regtoast').innerHTML = ('<font  style="color:#fff;">Wrong recommendation code enter !</font>');
					$('#registertoast').modal({ backdrop: 'static', keyboard: false })
					$('#registertoast').modal('show');
				}
				else if (arr[0] == 4) {
					document.getElementById('regtoast').innerHTML = ('<font  style="color:#fff;">Please verify your mobile no. !</font>');
					$('#registertoast').modal({ backdrop: 'static', keyboard: false })
					$('#registertoast').modal('show');
				}
				else if (arr[0] == 0) {
					document.getElementById('serror').innerHTML = ('<font  style="color:#fff;">Some Technical error !</font>');
				}

			}
		});


	}));
	//=====================Login Script================================================================

	$("#loginForm").on('submit', (function (e) {
		e.preventDefault();

		var loginmobile = $('input#login_mobile').val();
		var loginpassword = $('input#login_password').val();

		if ((loginmobile) == "") {
			$("input#login_mobile").focus();
			$('#login_mobile').addClass('borderline');
			return false;
		}
		if (loginmobile.length < 13) {
			$("input#login_mobile").focus();
			$('#login_mobile').addClass('borderline');
			return false;
		}

		if ((loginpassword) == "") {
			$("input#login_password").focus();
			$('#login_password').addClass('borderline');
			return false;
		}

		if (loginpassword.length < 5) {
			$("input#login_password").focus();
			$('#login_password').addClass('borderline');
			return false;
		}

		$.ajax({
			type: "POST",
			url: "loginNow.php",
			data: new FormData(this),
			contentType: false,
			cache: false,
			processData: false,

			success: function (html) { //alert(html);
				var arr = html.split('~');

				if (arr[0] == 1) {
					window.location.href = "mine.php";
				}
				else if (arr[0] == 0) {
					document.getElementById('regtoast').innerHTML = ('<font  style="color:#fff;">Wrong mobile number or password Entered !</font>');
					$('#registertoast').modal({ backdrop: 'static', keyboard: false })
					$('#registertoast').modal('show');
				}

			}
		});

		//=====================change password================================================================

	}));

	//================================================match otp===================================================
	$("#otp").on('change',function(){
		var otp = $('input#otp').val();

       if ((otp) == "") {
			$("input#otp").focus();
			$('#otp').addClass('borderline');
			return false;
		}
		if ((otp.length) < 6) {
			// alert();
			$("input#otp").focus();
			$('#otp').addClass('borderline');
			return false;
		}
		else{
			$.ajax({
				type: "POST",
				url: "veryfynumberNow.php",
				data: "otp="+otp + "&type="+"otpval",
				
				success: function (html) { //alert(html);
					var arr = html.split('~');
	
					if (arr[0] == 1) {
						$("#otpclose").click();
						document.getElementById('regtoast').innerHTML = ('<font  style="color:#fff;">OTP match successfully !</font>');
						$('#registertoast').modal({ backdrop: 'static', keyboard: false })
						$('#registertoast').modal('show');
						
						$('#mobile').attr('readonly', true);
					}
					else if (arr[0] == 0) {
						document.getElementById('regtoast').innerHTML = ('<font  style="color:#fff;">Wrong OTP Entered !</font>');
						$('#registertoast').modal({ backdrop: 'static', keyboard: false })
						$('#registertoast').modal('show');
					}
	
				}
			});
		}
		
	});
	$("#otpsubmitForm").on('submit', (function (e) {
		e.preventDefault();

		var otp = $('input#otp').val();


		if ((otp) == "") {
			$("input#otp").focus();
			$('#otp').addClass('borderline');
			return false;
		}

		$.ajax({
			type: "POST",
			url: "veryfynumberNow.php",
			data: new FormData(this),
			contentType: false,
			cache: false,
			processData: false,

			success: function (html) { //alert(html);
				var arr = html.split('~');

				if (arr[0] == 1) {
					$("#otpclose").click();
					document.getElementById('regtoast').innerHTML = ('<font  style="color:#fff;">OTP match successfully !</font>');
					$('#registertoast').modal({ backdrop: 'static', keyboard: false })
					$('#registertoast').modal('show');
					$('#mobile').attr('readonly', true);
				}
				else if (arr[0] == 0) {
					document.getElementById('regtoast').innerHTML = ('<font  style="color:#fff;">Wrong OTP Entered !</font>');
					$('#registertoast').modal({ backdrop: 'static', keyboard: false })
					$('#registertoast').modal('show');
				}

			}
		});

		//=====================change password================================================================

	}));



});

function mobileveryfication() {
    // alert("OTP has been sended on your number");

    
    $('#reg_otp').css('pointer-events', 'none')
    
    var mobile = $('input#mobile').val();
	if ((mobile) == "") {
		$("input#mobile").focus();
		$('#mobile').addClass('borderline');
		$()
		return false;
	}
	if (mobile.length < 10) {
		$("input#mobile").focus();
		$('#mobile').addClass('borderline');
		return false;
	}
	$.ajax({
		type: "Post",
		data: "mobile=" + mobile + "& type=" + "mobile",
		url: "veryfynumberNow.php",

		success: function (html) {
		    	document.getElementById('regtoast').innerHTML = ('<font  style="color:#fff;">OTP Sended</font>');
	$('#registertoast').modal({ backdrop: 'static', keyboard: false })
	$('#registertoast').modal('show');
			//alert(html);
			var arr = html.split('~');
			if (arr[0] == 1) {
				//data = JSON.parse(arr[1])
			//new  	
			var counter = 60;
                var interval = setInterval(function() {
                    counter--;
                    if (counter <= 0) {
                     	clearInterval(interval);
                      	$('.btn-otp').html("OTP");  
                        return;
                    }else{
                        $('.btn-otp').text(counter);
                    }
                }, 1000);
            
            	// end
				
				
				
				
				$("#otpclose").click();
				$('#otploader').show()
				// setTimeout(function(){
			
			}
			else if (arr[0] == 2) {
				document.getElementById('regtoast').innerHTML = ('<font  style="color:#fff;">Mobile Number already register with us !</font>');
				$('#registertoast').modal({ backdrop: 'static', keyboard: false })
				$('#registertoast').modal('show');
			}
			return false;
		},
		error: function (e) { }
	});

}