function emailcheck(str)
       {
               var at="@"
               var dot="."
               var lat=str.indexOf(at)
               var lstr=str.length
               var ldot=str.indexOf(dot)
 
               if (str.indexOf(at)==-1){
               
                  return false
               }
               if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
               
                  return false
               }
			  
               if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
               
                   return false
               }
                if (str.indexOf(at,(lat+1))!=-1){
               
                   return false
                }
                if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
               
                   return false
                }
                if (str.indexOf(dot,(lat+2))==-1){
               
                   return false
                }
                if (str.indexOf(" ")!=-1){
					
                   return false
                }
				
       }

$(function(){
			
			

	//if newsletter subscription submit button is clicked
	$('#myform').submit(function () {		
		
		//Get the data from all the fields
		var email = $('input[name=email]');

		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
		var theemail=document.getElementById("emailfield");
			if (theemail.value=='') {
				alert("Please enter your email address!");
				theemail.focus();
				return false;
			}
			else if (emailcheck(theemail.value)==false) {
				alert("Email address you've entered is invalid.\nPlease re-enter!");
				theemail.focus();
				return false;
			}
		
		//organize the data properly
		var data = 'email=' + email.val();
		
		//disabled all the text fields
		//$('.text').attr('disabled','true');
		
		//show the loading sign
		//$('.loading').show();
		
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "process.php",	
			
			//GET method is used
			type: "GET",

			//pass the data			
			data: data,		
			
			//Do not cache the page
			cache: false,
			
			//success
			success: function (html) {
              //alert(html);				
				//if process.php returned 1/true (send mail success)
				if (html==1) {					
					//hide the form
					$('.form').fadeOut('slow');					
					
					//show the success message
					$('.done').fadeIn('slow');
					
				//if process.php returned 0/false (send mail failed)
				}
				else if (html==2) {					
					//hide the form
					$('.form').fadeOut('slow');					
					
					//show the success message
					$('.existed').fadeIn('slow');
					
				//if process.php returned 0/false (send mail failed)
				} else alert('Sorry, unexpected error. Please try again later.');				
			}		
		});
		
		//cancel the submit button default behaviours
		return false;
	});	

	//when back link clicked from success panel
	$('#buttonDoneS').click(function(){
					//hide the success message
					$('.done').fadeOut('slow');					
					
					//show the form
					$('.form').fadeIn('slow');
	});

	//when back link clicked from failure panel
	$('#buttonExistedF').click(function(){
					//hide the failure message
					$('.existed').fadeOut('slow');					
					
					//show the form
					$('.form').fadeIn('slow');
	});	

		});
