/**
 * @author linux
 */
$('document').ready(function(){
		$("#menu").accordion("activate", "#clients");
		$('#memForm').validate({
			rules: {
				name: {
					required: true,
					maxlength: 255
				},
				email: {
					required: true,
					maxlength: 255,
					email: true
				},
				password: {
					required: true,
					maxlength: 16,
					minlength: 8
				},
				city: {
					required: true,
					maxlength: 255
				},
				concode: {
					required: true,
					maxlength: 6
				},
				special:{
					required: true,
					maxlength: 255
				}
			},
			errorClass: "validation-advice"
		});

		$('#email').click(function(event){
			if ($('#sameERROR').is('label')) {
				$('#sameERROR').remove();
			}
			if ($('#avi').is('label')) {
				$('#avi').remove();
			}
		});

		$('#email').blur(function(event){
			if ((/.+@.+\.[a-zA-Z]{2,4}$/).test($(event.target).val())) {
				var sendData;
				if ($('#id').val() != '') {
					sendData = {
						email: $(event.target).val(),
						act: 'getUser',
						id: $('#id').val()
					}
				}
				else
					if ($('#id').val().length == 0) {
						sendData = {
							email: $(event.target).val(),
							act: 'getUser'
						}
					}
				$.ajax({
					url: 'register.php',
					type: 'POST',
					data: sendData,
					success: function(data){
						if (data == 1) {
							$('#loadingIMG').remove();
							$(event.target).after('<label id="sameERROR" class="validation-advice">We have the same email in our system</label>');
						}
						else
							if (data == 0) {
								if ($('#sameERROR') != null) {
									$('#loadingIMG').remove();
									$('#sameERROR').remove();
									$(event.target).after('<label id="avi" class="avi">Availabe</label>');
								}
							}
					},
					beforeSend: function(){
						$(event.target).after('<label id="loadingIMG" class="wait">Checking for availability</label>');
					},
					error: function(){
						alert('an error while checking for availability.');
					}
				});
			}
		});

		$('#memForm').submit(function(){
			if ($('#sameERROR').is('label')) {
				$('#email').focus();
				alert('The email you have entered is alread exist in our system , please change it.');
				return false;
			}
		});

		$('#showSearch').click(function(){
			$('#searchMenu').toggle("blind", {
				direction: "vertical"
			}, 500);

			if ($('#showSearch').html() == 'Show Search') {
				$('#showSearch').html('Hide Search');
			}
			else {
				$('#showSearch').html('Show Search');
			}

		});

	});