$(function() {

	// load the modal window
	$('a.modal').click(function(){

		// scroll to top
		$('html, body').animate({scrollTop:0}, 'fast');

		// before showing the modal window, reset the form incase of previous use.
		
		$('form#contactForm').show();
		
		// Reset all the default values in the form fields
		//$('#name').val('Your name');
		//$('#email').val('Your email address');
		//$('#comment').val('Enter your comment or query...');

		//show the mask and contact divs
		$('#mask').show().fadeTo('', 0.7);
		$('div#contact').fadeIn();

		// stop the modal link from doing its default action
		return false;
	});

	// close the modal window is close div or mask div are clicked.
	$('div#close, div#mask').click(function() {
		$('div#contact, div#mask').stop().fadeOut('slow');

	});

	$('#contactForm input').focus(function() {
		//$(this).val(' ');
	});
	
	$('#contactForm textarea').focus(function() {
       //  $(this).val('');
    });

	// when the Submit button is clicked...
	$('#sendtofriend').click(function() {
	
		//Inputed Strings
		var urname = $('#ur_name').val(),
			uremail = $('#ur_email').val(),
			fren_name = $('#fren_name').val();
			fren_email = $('#fren_email').val();
		
	
		//Error Count
		var error_count = 0;
		
		//Regex Strings
		var name_regex = /^[a-zA-Z]$/;
			email_regex = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
		
			//Test name
			if(urname == "" || urname.length <3 ) {
				$('#error_urname').html('Enter your name!');
				$('#error_urname').show();
				error_count += 1;
				
			}
			else 
			{$('#error_urname').hide();}
			
			//Test Email
			if(!email_regex.test(uremail)) {
				$('#error_uremail').html('Enter your valid e-mail address!');
				$('#error_uremail').show();
				error_count += 1;
			}else {$('#error_uremail').hide();}
			
			//Test name
			if(fren_name == "" || urname.length <3 ) {
				$('#error_frenname').html("Enter your Friend's name!");
				$('#error_frenname').show();
				error_count += 1;
			}else {$('#error_frenname').hide();}
			
			//Test Email
			if(!email_regex.test(fren_email)) {
				$('#error_frenemail').html("Enter your Friend's valid e-mail address!");
				$('#error_frenemail').show();
				error_count += 1;
			}else {$('#error_frenemail').hide();}
			
			
			
			//No Errors?
			if(error_count == 0) {//alert('dddd');
				$.post("send2friend_mail.php", $("#contactForm").serialize(),function(result)
				{
					$('#message').html(result);
					$('#send_form').hide();
					$('#message').show();
				});
			}
			
			
			
		return false;
	});
	
});

function sendfren()
{
	document.contactForm.reset();
	$('#send_form').show();
	$('#message').hide();
}

function signup()
{
	var name = $('#name').val();
	var email = $('#email').val();
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	if(name == 'Name' || name == '')
	{alert('Please enter your name.');$('#name').focus();return false;}
	else if(email == 'Email' || email == '')
	{alert('Please enter a valid email address.');$('#email').focus();return false;}
	else if(!emailReg.test(email))
	{alert('Please enter a valid email address.');$('#email').focus();return false;}
	else{$('#load').html('<img src="images/ajax_loader_small.gif" />');}
	$.post("ajax_signup.php", { user_name: name, user_email: email} , function(data)
	      {
			 alert(data);
					
					$('.error_text').hide();
					$('.success_text').hide();
			  var msg=data.split('#');	
			  			  
				if($.trim(msg[0])=="error")
				{		
					 $('#load').hide();
					$('.error_text').html(msg[1]);
					$('.error_text').show();
				}
				if($.trim(msg[0])=="success")
				{
					 $('#load').hide();					
					 $('.success_text').html(msg[1]);
					 $('.success_text').show();
				}
				
          });
				
	return false;	
}
