$(document).ready(function(){
	
	
	$("#email").blur(function()
	{
		//remove all the class add the messagebox classes and start fading
		$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
		//check the username exists or not from ajax
		$.post("register3.php",{ email:$(this).val(),action:"email_validation" } ,function(data)
        {
		  if(data=='taken') //if username not avaiable
		  {
		  	$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('This Email Already exists').addClass('messageboxerror').fadeTo(900,1);
			  
			});		
          }
		  else
		  {
		  	$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Email available to register').addClass('messageboxok').fadeTo(900,1);	
			});
		  }
				
        });
 
	});
	
	
	$("#password").blur(function()
	{
		//remove all the class add the messagebox classes and start fading
		$("#msgbox2").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
		//check the username exists or not from ajax
		$.post("register3.php",{ password:$(this).val(),action:"password_validation" } ,function(data)
        {
		  if(data=='less') //if username not avaiable
		  {
		  	$("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Password Cannot be less than 5 characters').addClass('messageboxerror').fadeTo(900,1);
			  
			});		
          }
		  else if(data=='passtaken') //if username not avaiable
		  {
		  	$("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Password is taken already').addClass('messageboxerror').fadeTo(900,1);
			  
			});		
          }
		  else if(data!='less' && data!='passtaken')
		  {
		  	$("#msgbox2").fadeTo(200,0.1,function()  //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Password is OK').addClass('messageboxok').fadeTo(900,1);	
			});
		  }
				
        });
 
	});
	
	 $("#webordering").click(function(e)
  {
    //getting height and width of the message box
    var height = $('#popuup_div').height();
    var width = $('#popuup_div').width();
    //calculating offset for displaying popup message
    leftVal=e.pageX-(width/2)+"px";
    topVal=e.pageY-(height/2)+"px"; 
    //show the popup message and hide with fading effect
    $('#popuup_div').css({left:leftVal,top:topVal}).show().fadeOut(11000);
  });    
	
	$("#submit").click(function(){					   				   
		$(".error").hide();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		var title = $("#title").val();
		var first_name = $("#first_name").val();
		var last_name = $("#last_name").val();
		var company_name = $("#company_name").val();
		var address_1 = $("#address_1").val();
		var address_2 = $("#address_2").val();
		var address_3 = $("#address_3").val();
		var post_code = $("#post_code").val();
		var company_tel = $("#company_tel").val();
		var direct_tel = $("#direct_tel").val();
		var fax = $("#fax").val();
		var email = $("#email").val();
		var confirm_email = $("#confirm_email").val();
		var hear_about = $("#hear_about").val();
		var password= $("#password").val();
		var confirm_password= $("#confirm_password").val();
		var terms = $("#terms").val();	
		var other = $("#other").val();
		var director_first_name = $("#director_first_name").val();
		var director_last_name = $("#director_last_name").val();
		var director_email = $("#director_email").val();
		var director_telephone = $("#director_telephone").val();
		var position = $("#position").val();
		
		if(email == '') {
			$("#email").after('<span class="error"><br/><img src="images/warning.gif">You forgot to enter an Email address.</span>');
			 
			 
			hasError = true;
		} else if(!emailReg.test(email)) {	
			 
			$("#email").after('<span class="error"><br/><img src="images/warning.gif">Enter a valid email address.</span>');
			 
			hasError = true;
		}
		
		if(confirm_email == '') {
			 
			$("#confirm_email").after('<span class="error"><br/><img src="images/warning.gif">You forgot to confirm your Email address.</span>');
			 
			hasError = true;
		} else if(!emailReg.test(confirm_email)) {	
			 
			$("#confirm_email").after('<span class="error"><br/><img src="images/warning.gif">Enter a valid email address.</span>');
			 
			hasError = true;
		}
		
	    if(email!=confirm_email) {
			 
			$("#email").after('<span class="error"><br/><img src="images/warning.gif">The Two emails did not match.</span>');
		 
		hasError = true;
		}
		
		if(title == '') {
			$("#title").after('<span class="error"><br/><img src="images/warning.gif">You forgot to select a title.</span>');
			 
			 
			hasError = true;
		}
		

		if(first_name == '') {
			$("#first_name").after('<span class="error"><br/><img src="images/warning.gif">You forgot to enter your first name.</span>');
			 
			 
			hasError = true;
		}
		
		if(last_name == '') {
			$("#last_name").after('<span class="error"><br/><img src="images/warning.gif">You forgot to enter your last name.</span>');
			 
			 
			hasError = true;
		}
		
		if(company_name == '') {
			$("#company_name").after('<span class="error"><br/><img src="images/warning.gif">You forgot to enter your company name.</span>');
			 
			 
			hasError = true;
		}
		
		if(address_1 == '') {
			$("#address_1").after('<span class="error"><br/><img src="images/warning.gif">You forgot to enter address Line 1.</span>');
			 
			 
			hasError = true;
		}
		
		if(address_2 == '') {
			$("#address_2").after('<span class="error"><br/><img src="images/warning.gif">You forgot to enter address Line 2.</span>');
			 
			 
			hasError = true;
		}
		if(address_3 == '') {
			$("#address_3").after('<span class="error"><br/><img src="images/warning.gif">You forgot to enter address Line 3.</span>');
			 
			 
			hasError = true;
		}
		if(post_code == '') {
			$("#post_code").after('<span class="error"><br/><img src="images/warning.gif">You forgot to enter your post code.</span>');
			 
			 
			hasError = true;
		}
		
		if(company_tel == '') {
			$("#company_tel").after('<span class="error"><br/><img src="images/warning.gif">You forgot to enter your Company telephone.</span>');
			 
			 
			hasError = true;
		}
		if(direct_tel == '') {
			$("#direct_tel").after('<span class="error"><br/><img src="images/warning.gif">You forgot to enter your Direct telephone.</span>');
			 
			 
			hasError = true;
		}
		if(password == '') {
			$("#password").after('<span class="error"><br/><img src="images/warning.gif">You forgot to enter your password.</span>');
			 
			 
			hasError = true;
		}
		
		if(confirm_password == '') {
			$("#confirm_password").after('<span class="error"><br/><img src="images/warning.gif">You forgot to confirm your password.</span>');
			 
			 
			hasError = true;
		}
		
		if(password!=confirm_password) {
			$("#password").after('<span class="error"><br/><img src="images/warning.gif">The Two passwords did not match.</span>');
 
 
		hasError = true;
		}
		
		
		if(hear_about == '') {
			$("#hear_about").after('<span class="error"><br/><img src="images/warning.gif">You forgot to select where did you hear about BETA.</span>');
			 
			 
			hasError = true;
		}
		
		if(hear_about == 'other') {
			if(other == '') {
			$("#other").after('<span class="error"><br/><img src="images/warning.gif">you forgot to fillup OTHER field.</span>');
			 
			 
			hasError = true;
		}
		}
	  
	 function isCheckedById(id) 
    { 
        var checked = $("input[@id="+id+"]:checked").length; 
        if (checked == 0) 
        { 
            return false; 
        } 
        else 
        { 
            return true; 
        }
	}

	 if (!isCheckedById("terms")) 
        { 
            $("#terms").after('<span class="error"><br/><img src="images/warning.gif">you forgot to accept to Terms & Conditions.</span>');
          $("#submit").after('<span class="error"></span>');
		   
			hasError = true;
        } 

	  
	  if(director_email == '') {
			$("#director_email").after('<span class="error"><br/><img src="images/warning.gif">You forgot to enter an Email address.</span>');
			 
			 
			hasError = true;
		} else if(!emailReg.test(director_email)) {	
			 
			$("#director_email").after('<span class="error"><br/><img src="images/warning.gif">Enter a valid email address.</span>');
			 
			hasError = true;
		}
	  
	  
	  if(director_first_name == '') {
			$("#director_first_name").after('<span class="error"><br/><img src="images/warning.gif">You forgot to enter First name of company director.</span>');
			 
			 
			hasError = true;
		}
	  
	  
	  if(director_last_name == '') {
			$("#director_last_name").after('<span class="error"><br/><img src="images/warning.gif">You forgot to enter Last name of company director.</span>');
			 
			 
			hasError = true;
		}
		if(director_telephone == '') {
			$("#director_telephone").after('<span class="error"><br/><img src="images/warning.gif">You forgot to enter direct line of company director.</span>');
			 
			 
			hasError = true;
		}
		
		if(position == '') {
			$("#position").after('<span class="error"><br/><img src="images/warning.gif">You forgot to enter your position.</span>');
			 
			 
			hasError = true;
		}
	  
		if(hasError == true)
		{
		$("#submit").after('<span class="error"><br/><img src="images/warning.gif">There are Errors in the Form , Please check the errors above in RED.</span>');
		}
		if(hasError == false) {
		
			$.post("register.php",
   				{ emailTo: emailToVal, emailFrom: emailFromVal, subject: subjectVal, message: messageVal },
   					function(data){
						$("#sendEmail").slideUp("normal", function() {				   
							
							$("#sendEmail").before('<h1>Success</h1><p>Your email was sent.</p>');											
						});
   					}
				 );
		}
		
		return false;
	});						   
});
