var $ = jQuery.noConflict();


$(document).ready(function() {
	
		$('input[type="text"],select,textarea').focus(function () {
			$(this).removeClass("invalidfield");
		});
	
		//Ajax forms
			
		// Newsletter Form
			
		$('#newsletterform_submit').click(function () {
          
        //Get the data from all the fields
        
        var regemail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  		 
        var ajaxnewsletteremail = $('input[name="newsletterform_email"]');  
          
        if (ajaxnewsletteremail.val()=='') {  
            ajaxnewsletteremail.addClass('invalidfield');  
            return false;  
        } else ajaxnewsletteremail.removeClass('invalidfield');
		
		if (ajaxnewsletteremail.val()=='Subscribe Now to get Discount Coupons &amp; Other Promotions') {  
            ajaxnewsletteremail.addClass('invalidfield');  
            return false;  
        } else ajaxnewsletteremail.removeClass('invalidfield');        
		
		if(regemail.test(ajaxnewsletteremail.val()) == false) {
  			ajaxnewsletteremail.addClass('invalidfield');  
            return false;  
        } else ajaxnewsletteremail.removeClass('invalidfield');
		
		//organize the data properly  
        var data = 'email=' + ajaxnewsletteremail.val();
          
        //start the ajax  
        $.ajax({  
            //this is the php file that processes the data and send mail  
            url: "newsletter.php",   
              
            //GET method is used  
            type: "GET",  
  
            //pass the data           
            data: data,       
              
            //Do not cache the page  
            cache: false,  
              
            //success  
            success: function (html) {                
                //if process.php returned 1/true (send mail success)  
                if (html==1) {                    
                    //hide the form  
                    $('#newsletter').hide();                   
                      
                    //show the success message  
                    $('#newsletterform_successmsg').fadeIn('slow');  
                      
                //if process.php returned 0/false (send mail failed)  
                } else alert('Sorry, unexpected error. Please Refresh the Page & Try Again.');                 
            }         
        });  
          
        //cancel the submit button default behaviours  
        return false;  
    });
    
});
