// JavaScript Document
$(function() {
			
		var name = $("#name");
		var	email = $("#email");
		var	phone = $("#phone");
		var	address = $("#address");
		var	confirmEmail = $("#confirm");
		var	username = $("#username");
		var	lipassword = $("#lipassword");
		var	allRegFields = $([]).add(name).add(email).add(phone).add(confirmEmail).add(address);
		var	allLoginFields = $([]).add(username).add(lipassword);
		var	regTips = $("#signupTips");
		var	loginTips = $("#loginTips");
		
		
			$.ajaxSetup ({  
        					cache: false  
    					});
			
    	var ajax_load = "<img src='img/load.gif' alt='loading...' />";  
  
  
		function updateTips(o, t) 
		{
			o.text(t).addClass('ui-state-highlight');
			setTimeout(function() {
				o.removeClass('ui-state-highlight', 1500);
			}, 500);
		}

		function checkLength(o,n,min,max, t) {

			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips(t, n + " must be between "+min+" and "+max+" characters.");
				o.focus();
				return false;
			} else {
				return true;
			}

		}

		function checkRegexp(o,regexp,n, t) {

			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(t, n);
				o.focus();
				return false;
			} else {
				return true;
			}

		}
		
		
		function checkSame(o,o2,n, t) {

			if ( o.val() != o2.val() ) {
				o.addClass('ui-state-error');
				updateTips(t, n);
				o2.focus();
				return false;
			} else {
				return true;
			}

		}

		
		$('#regDialogButton').click( function(){ 
										  	var bValid = true;
											allRegFields.removeClass('ui-state-error');
						
											bValid = bValid && checkLength(name,"Names",5,64, regTips);
											bValid = bValid && checkLength(email,"Email addresses",6,128, regTips);
											bValid = bValid && checkLength(phone,"Phone numbers",6,128, regTips);
											//bValid = bValid && checkLength(address,"Addresses",16,255, regTips);
											
											bValid = bValid && checkRegexp(name,/^[a-zA-Z]([a-zA-Z\ ])+$/i,"Names may only consist of letters and spaces and must begin with a letter.", regTips);
											// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
											bValid = bValid && checkRegexp(email,/^((([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,"The email address is invalid", regTips);
											
											
											bValid = bValid && checkSame(email, confirmEmail,"Email addresses supplied do not match", regTips);
											
											if (bValid) 
											{
												var url = "http://"+document.domain + '/register.php';
												$.post(url, { data : $('#signupForm').serializeArray()}, function(data){ if( jQuery.trim(data) != '') { updateTips(regTips, data); } else { $('#signupDialog').fadeOut(); lightbox(true);$('#regSuccessPanel').show(); $('#regCreditButton').click( function(){ 
																																																																											 
																																																							 var returnURL = $('#returnReg').val();

																																																								if(returnURL != '')
																																																								{
																																																									window.location = "http://"+document.domain + '/'+returnURL;
																																																								}	 
																																																								 else
																																																								 {	
																																																								 	window.location = '/Account/Buy/';
																																																								 }
																																																								 }) } }, 'html' );
											}
											
										  	return false;
										  });
		
		
		
		
		$('#loginDialogButton').click( function(){ 
										  	var bValid = true;
											allLoginFields.removeClass('ui-state-error');
						
											bValid = bValid && checkLength(username,"Username",3,64,loginTips);
											bValid = bValid && checkLength(lipassword,"Password",6,64,loginTips);
										
											
											if (bValid) 
										  	{
												var url = "http://"+document.domain + '/login.php';
												$.post(url, { data : $('#loginForm').serializeArray()}, function(data){ if( jQuery.trim(data) != '') { updateTips(loginTips, data); } else {  var returnURL = $('#returnLI').val();
												
												
												if(returnURL != '')
												{
													window.location = "http://"+document.domain + '/'+returnURL;
												}
												else
													{
														lightbox(false);
														$('#loginDialog').fadeOut();
													} }  }, 'html' );
											}
											
										  	return false;
										  });
		
		$('a.signupButton').click(function() { lightbox(true); $('#signupDialog').fadeIn(); return false; });
		$('#signupNow').click(function() {$('#loginDialog').hide(); $('#signupDialog').fadeIn();  return false; });
		$('#loginButton').click(function() { lightbox(true); $('#loginTips').empty(); $('#loginDialog').fadeIn();  return false; });
		$('span.login-close-button').click(function() { $('div.dialogPanel').fadeOut(); lightbox(false);  return false; });
		$('span.reg-close-button').click(function() { $('div.dialogPanel').fadeOut(); lightbox(false);  return false; });
		$('span.success-close-button').click(function() { $('div.dialogPanel').fadeOut(); lightbox(false);  return false; });
		
		$('#loginForm input[type=text]').keyup( function(e){ var code=e.charCode || e.keyCode; if(code == 13) {  $('#loginDialogButton').trigger('click'); } });
		$('#signupForm input[type=text]').keyup( function(e){ var code=e.charCode || e.keyCode; if(code == 13) {  $('#regDialogButton').trigger('click'); } });
		$('#loginForm input[type=password]').keyup( function(e){ var code=e.charCode || e.keyCode; if(code == 13) {  $('#loginDialogButton').trigger('click'); } });
		$('#signupForm input[type=password]').keyup( function(e){ var code=e.charCode || e.keyCode; if(code == 13) {  $('#regDialogButton').trigger('click'); } });
	});

	function lightbox(show)
	{
	if(show)
	{
		$('<div />').addClass('lightbox_bg').click(function(){ $(this).remove(); $('div.dialogPanel').hide();}).appendTo('body').show();
	}
	else
		{
			$('div.lightbox_bg').remove();
		}
	}
