var refreshT;
$(function() {
    searchTrim = [];
    // Give the input class="searchValue" in the form
	$(".searchValue").each(function(i){
		searchTrim[i] = $(this).val();
        $(this).focus(function(){
          if ($(this).val() == searchTrim[i]) {
          $(this).val("");
          }
        }).blur(function(){
           if ($.trim($(this).val()) == "") {
           $(this).val(searchTrim[i]);
           }
        });
    });
});

function reloadPage() {
	window.location.reload();
	clearInterval(refreshT);
}

// Toggle the 'Account' menu item
$(document).ready(function(){
	$("#menu ul.nav li a#myAccount").click(function() {
		// ul#myOptions is the hidden list
		$("#menu ul.nav ul#myOptions").toggle();
		$(this).toggleClass("active");
		return false;
	});
	
	$('#website').click(function() {
		$(this).next('label').removeClass('inputError');
	});
	$('#email').click(function() {
		$(this).next('label').removeClass('inputError');
	});
	$('#pass').click(function() {
		$(this).next('label').removeClass('inputError');
	});
	
	//Register box
	 $('a[name=pop]').click(function(e) {
        e.preventDefault();
        var popbox = $(this).attr('href');
		showBox(popbox);
    });	

	$('#register').click(function() {
		var web = $('#website').val();
		var mail = $('#email').val();
		var pass = $('#pass').val();
		if(web == '') {
			$('#website').next('label').addClass('inputError');
			$('#website').focus();
			return false;
		}
		if(mail == '') {
			$('#email').next('label').addClass('inputError');
			$('#email').focus();
			return false;
		}
		if(mail!='')
		{
			var b=checkRegexp(mail, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
			if(b==false)
			{
				
				$('#email').next('label').addClass('inputError');
				$('#email').focus();
				//alert("Please Enter Correct Email Address. e.g. john@example.com");
				return false;
			}
		}
		if(pass == '') {
			$('#pass').next('label').addClass('inputError');
			$('#pass').focus();	
			return false;
		}
		$(this).parents('.popbox').fadeOut();
		$('#screen').fadeOut();
		$.post("register_user.php", {mail: mail, web: web, pass: pass}, 
		function(data){ 
			$("#popUpDialogArea").append(data);
			showBox('#regAfter');
			//refreshT = setInterval("reloadPage()", 3000);
		});
		return false;
	});
	
	$('#login').click(function() {
		var mail = $('#email_login').val();
		var pass = $('#pass_login').val();
		if(mail == '') {
			$('#email_login').next('label').addClass('inputError');
			$('#email_login').focus();
			return false;
		}
		if(pass == '') {
			$('#pass_login').next('label').addClass('inputError');
			$('#pass_login').focus();	
			return false;
		}
		$(this).parents('.popbox').fadeOut();
		$('#screen').fadeOut();
		$.post("login.php", {mail: mail, pass: pass}, 
		function(data){ 
			$("#popUpDialogArea").append(data);
			showBox('#logAfter');
			refreshT = setInterval("reloadPage()", 10000);
		});
		
		return false;
	});
	
	$('.gobutton').click(function () {
		var atmpt=$('#attempt').val();
		var title = $('#Title').val();
		var Link = $('#Link').val();
		var tags= $('#Tags').val();
		// var Description = $('#Description').val();
		// var Category= $('#Category').val();
		var myip=$('#myip').val();		
		$.post("addmessage.php", {title: title, Link: Link, tags: tags, myip: myip, atmpt: atmpt}, 
		function(data){ 
			$("#linkAdd").remove();
			$("#popUpDialogArea").append(data);
			showBox('#linkAdd');
			//if(atmpt>=1)
				//refreshT = setInterval("reloadPage()", 8000);
		});
		//alert(atmpt);
		$('#attempt').val(1);
		return false;
		
	});
	
	$('#forgot').click(function () {
		var email = $('#forgotemail').val();
		$.post("forgotpassword.php", {email: email}, 
		function(data){ 
			$("#popUpDialogArea").append(data);
			hideBox('#forgotBox');
			showBox('#forgotMsgbox');
			//refreshT = setInterval("reloadPage()", 8000);
		});
		return false;
	});
	
	
});

function checkRegexp( mail, regexp ) {

			if ( !( regexp.test( mail ) ) ) {
				return false;
			} else {
				return true;
			}

}
