

$(document).ready(function () {
	// Username validation logic
	var valadation_first_name = $('#valadation_first_name');

	$('#first_name').keyup(function () {
		// cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
		var t = this;

		// only run the check if the username has actually changed - also means we skip meta keys
		if (this.value != this.lastValue){

			// the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
			// a particular key, it will only fire when the release the key.
			if (this.timer){
				clearTimeout(this.timer);
			}

			// show our holding text in the validation message space
			valadation_first_name.removeClass('error').html('<img src="images/ajax-loader.gif" height="11" width="14" />');

			// fire an ajax request in 1/5 of a second
			//'t.value' is the value of first_name
			this.timer = setTimeout(function () {
				$.ajax({
					url: 'index.php',
					data: 'action=check_username&first_name=' + t.value,
					dataType: 'json',
					type: 'post',
					success: function (j) {
						// put the 'msg' field from the $$_VAR_MSG array from function is_input_correct($var_name, $input_data) in to the validation message
						valadation_first_name.html(j.first_name);
					}
				});
			}, 1000);

			// copy the latest value to avoid sending requests when we don't need to
			this.lastValue = this.value;
		}
	});

	////////////////////////////////////
	//Valid last name
	///////////////////////////////////
	var valadation_last_name = $('#valadation_last_name');
	$('#last_name').keyup(function () {
		// cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
		var t = this;

		// only run the check if the username has actually changed - also means we skip meta keys
		if (this.value != this.lastValue){

			// the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
			// a particular key, it will only fire when the release the key.
			if (this.timer){
				clearTimeout(this.timer);
			}

			// show our holding text in the validation message space
			valadation_last_name.removeClass('error').html('<img src="images/ajax-loader.gif" height="11" width="14" /> ');

			// fire an ajax request in 1/5 of a second
			//'t.value' is the value of first_name
			this.timer = setTimeout(function () {
				$.ajax({
					url: 'index.php',
					data: 'action=check_username&last_name=' + t.value,
					dataType: 'json',
					type: 'post',
					success: function (j) {
						//put the 'msg', which is received from the arry $_VAR_MSG.
						//Function is_input_correct($var_name, $input_data) return the msg
						valadation_last_name.html(j.last_name);
					}
				});
			}, 1000);

			// copy the latest value to avoid sending requests when we don't need to
			this.lastValue = this.value;
		}
	});

	////////////////////////////////////
	//Valid company name
	///////////////////////////////////
	var valadation_company_name = $('#valadation_company_name');
	$('#company_name').keyup(function () {
		// cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
		var t = this;

		// only run the check if the username has actually changed - also means we skip meta keys
		if (this.value != this.lastValue){

			// the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
			// a particular key, it will only fire when the release the key.
			if (this.timer){
				clearTimeout(this.timer);
			}

			// show our holding text in the validation message space
			valadation_company_name.removeClass('error').html('<img src="images/ajax-loader.gif" height="11" width="14" /> ');

			// fire an ajax request in 1/5 of a second
			//'t.value' is the value of first_name
			this.timer = setTimeout(function () {
				$.ajax({
					url: 'index.php',
					data: 'action=check_username&company_name=' + t.value,
					dataType: 'json',
					type: 'post',
					success: function (j) {
						//put the 'msg', which is received from the arry $_VAR_MSG.
						//Function is_input_correct($var_name, $input_data) return the msg
						valadation_company_name.html(j.company_name);
					}
				});
			}, 1000);

			// copy the latest value to avoid sending requests when we don't need to
			this.lastValue = this.value;
		}
	});

	////////////////////////////////////
	//Valid Landline number
	///////////////////////////////////
	var valadation_phone = $('#valadation_phone');
	$('#phone').keyup(function () {
		// cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
		var t = this;

		// only run the check if the username has actually changed - also means we skip meta keys
		if (this.value != this.lastValue){

			// the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
			// a particular key, it will only fire when the release the key.
			if (this.timer){
				clearTimeout(this.timer);
			}

			// show our holding text in the validation message space
			valadation_phone.removeClass('error').html('<img src="images/ajax-loader.gif" height="11" width="14" /> ');

			// fire an ajax request in 1/5 of a second
			//'t.value' is the value of first_name
			this.timer = setTimeout(function () {
				$.ajax({
					url: 'index.php',
					data: 'action=check_username&phone=' + t.value,
					dataType: 'json',
					type: 'post',
					success: function (j) {
						//put the 'msg', which is received from the arry $_VAR_MSG.
						//Function is_input_correct($var_name, $input_data) return the msg
						valadation_phone.html(j.phone);
					}
				});
			}, 1000);

			// copy the latest value to avoid sending requests when we don't need to
			this.lastValue = this.value;
		}
	});


	////////////////////////////////////
	//Valid mobile number
	///////////////////////////////////
	var valadation_cell_phone = $('#valadation_cell_phone');
	$('#cell_phone').keyup(function () {
		// cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
		var t = this;

		// only run the check if the username has actually changed - also means we skip meta keys
		if (this.value != this.lastValue){

			// the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
			// a particular key, it will only fire when the release the key.
			if (this.timer){
				clearTimeout(this.timer);
			}

			// show our holding text in the validation message space
			valadation_cell_phone.removeClass('error').html('<img src="images/ajax-loader.gif" height="11" width="14" /> ');

			// fire an ajax request in 1/5 of a second
			//'t.value' is the value of first_name
			this.timer = setTimeout(function () {
				$.ajax({
					url: 'index.php',
					data: 'action=check_username&cell_phone=' + t.value,
					dataType: 'json',
					type: 'post',
					success: function (j) {
						//put the 'msg', which is received from the arry $_VAR_MSG.
						//Function is_input_correct($var_name, $input_data) return the msg
						valadation_cell_phone.html(j.cell_phone);
					}
				});
			}, 1000);

			// copy the latest value to avoid sending requests when we don't need to
			this.lastValue = this.value;
		}
	});

	////////////////////////////////////
	//Valid email address
	///////////////////////////////////
	var valadation_email = $('#valadation_email');
	$('#email').keyup(function () {
		// cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
		var t = this;

		// only run the check if the username has actually changed - also means we skip meta keys
		if (this.value != this.lastValue){

			// the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
			// a particular key, it will only fire when the release the key.
			if (this.timer){
				clearTimeout(this.timer);
			}

			// show our holding text in the validation message space
			valadation_email.removeClass('error').html('<img src="images/ajax-loader.gif" height="11" width="14" /> ');

			// fire an ajax request in 1/5 of a second
			//'t.value' is the value of first_name
			this.timer = setTimeout(function () {
				$.ajax({
					url: 'index.php',
					data: 'action=check_username&email=' + t.value,
					dataType: 'json',
					type: 'post',
					success: function (j) {
						//put the 'msg', which is received from the arry $_VAR_MSG.
						//Function is_input_correct($var_name, $input_data) return the msg
						valadation_email.html(j.email);
					}
				});
			}, 1000);

			// copy the latest value to avoid sending requests when we don't need to
			this.lastValue = this.value;
		}
	});

	////////////////////////////////////
	//Valid how much client process?
	///////////////////////////////////
	var valadation_how_much_month = $('#valadation_how_much_month');
	$('#how_much_month').mousedown(function () {
		// cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
		var t = this;

		// only run the check if the username has actually changed - also means we skip meta keys
		if (this.value != this.lastValue){

			// the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
			// a particular key, it will only fire when the release the key.
			if (this.timer){
				clearTimeout(this.timer);
			}

			// show our holding text in the validation message space
			valadation_how_much_month.removeClass('error').html('<img src="images/ajax-loader.gif" height="11" width="14" /> ');

			// fire an ajax request in 1/5 of a second
			//'t.value' is the value of first_name
			this.timer = setTimeout(function () {
				$.ajax({
					url: 'index.php',
					data: 'action=check_username&how_much_month=' + t.value,
					dataType: 'json',
					type: 'post',
					success: function (j) {
						//put the 'msg', which is received from the arry $_VAR_MSG.
						//Function is_input_correct($var_name, $input_data) return the msg
					valadation_how_much_month.html(j.how_much_month);
					}
				});
			}, 20);

			// copy the latest value to avoid sending requests when we don't need to
			this.lastValue = this.value;
		}
	});

});


