jQuery(document).ready(function(){     /***START JQUERY*************/
function isValidPhoneNumber(ph) {
    if (ph == null) {return false;}
    var stripped = ph.replace(/[\s()+-]|ext\.?/gi, "");
    // 10 is the minimum number of numbers required
    return ((/\d{8,}/i).test(stripped));
}
function validEmail(str) {
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}
function isNumeric(form_value) { 
    if (form_value.match(/^\d+$/) == null) 
        return false; 
    else 
        return true; 
}
/************************************************************************************************************/
//      CONTACT US
/************************************************************************************************************/
    /*GO BACK TO CONTACT FORM*/
    jQuery("#back_contact_form").click(function() {
	    jQuery('#contact_done').fadeOut('slow');							
	    jQuery('#contact_form')[0].reset();				
	    jQuery('#contact_form').fadeIn('slow');	
    });
    /*START VALIDATING & SENDING CONTACT US FORM*/
    jQuery('#send_form').click(function(e){

        //stop the form from being submitted
        e.preventDefault();

        /* declare the variables, var error is the variable that we use on the end
        to determine if there was an error or not */
        var error = false;
        var fname = jQuery('input[name=contact_name]');
				var email = jQuery('input[name=contact_email]');
				var phone = jQuery('input[name=contact_tel]');
				var comment = jQuery('textarea[name=contact_message]');
		
				//Simple validation to make sure user entered something
				//If error found, add hightlight class to the text field
				if (fname.val()=='') {
					var error = true;
					fname.addClass('hightlight');
					return false;
				} else fname.removeClass('hightlight');
				
				if ((email.val()=='') || (email.val().indexOf("@")==-1) || (email.val().indexOf(".")==-1)) {
					var error = true;
					email.addClass('hightlight');
					return false;
				} else email.removeClass('hightlight');
		
				if ((phone.val()=='') || (!isValidPhoneNumber(phone.val()))) {
					var error = true;
					phone.addClass('hightlight');
					return false;
				} else phone.removeClass('hightlight');
				
				/*if (comment.val()=='') {
					var error = true;
					comment.addClass('hightlight');
					return false;
				} else comment.removeClass('hightlight');*/

        //now when the validation is done we check if the error variable is false (no errors)
        if(error == false){
            //disable the submit button to avoid spamming
            //and change the button text to Sending...
            //jQuery('#send_form').attr({'disabled' : 'true'});
            jQuery('#send_form').attr({'disabled' : 'disabled'});
            //jQuery('#send_form').attr({src : '/images/right_box_btn_arrow_off.gif'});

            /* using the jquery's post(ajax) function and a lifesaver
            function serialize() which gets all the data from the form
            we submit it to send_email.php */
            jQuery.post("/includes/jquery/process_contact_form.php", jQuery("#contact_form").serialize(),function(result){
                //and after the ajax request ends we check the text returned
                //alert(result);
                if(result){
                    //hide the form
					jQuery('#contact_form').fadeOut('slow');					
					
					//show the success message
					jQuery('#contact_done').fadeIn('slow');											                    
                }else{
                    alert('מצטערים, שגיאה בלתי צפויה. אנא נסה שנית.')                    
                    //reenable the submit button by removing attribute disabled and change the text back to Send The Message
                    jQuery('#send_form').removeAttr('disabled');
                }
            });
        }
    });
/************************************************************************************************************/
//      NEWSLETTER
/************************************************************************************************************/
    /*GO BACK TO NEWSLETTER FORM*/
     jQuery("#back_newsletter_form").click(function() {
        jQuery('#newsletter_done').fadeOut('slow');                            
        jQuery('#newsletter_form')[0].reset();                
        jQuery('#newsletter_form').fadeIn('slow');    
    });
    /*START VALIDATING & SENDING NEWSLETTER FORM*/
    jQuery('#send_news_form').click(function(e){

        //stop the form from being submitted
        e.preventDefault();

        /* declare the variables, var error is the variable that we use on the end
        to determine if there was an error or not */
        var error = false;
        var fname = jQuery('input[name=news_name]');
                var email = jQuery('input[name=news_email]');
                var phone = jQuery('input[name=news_tel]');
        
                //Simple validation to make sure user entered something
                //If error found, add hightlight class to the text field
                if (fname.val()=='') {
                    var error = true;
                    fname.addClass('hightlight');
                    return false;
                } else fname.removeClass('hightlight');
                
                if ((email.val()=='') || (email.val().indexOf("@")==-1) || (email.val().indexOf(".")==-1)) {
                    var error = true;
                    email.addClass('hightlight');
                    return false;
                } else email.removeClass('hightlight');
        
                if ((phone.val()=='') || (!isValidPhoneNumber(phone.val()))) {
                    var error = true;
                    phone.addClass('hightlight');
                    return false;
                } else phone.removeClass('hightlight');
                
        //now when the validation is done we check if the error variable is false (no errors)
        if(error == false){
            //disable the submit button to avoid spamming
            //and change the button text to Sending...
            //jQuery('#send_news_form').attr({'disabled' : 'true'});
            jQuery('#send_news_form').attr({'disabled' : 'disabled'});
            //jQuery('#send_form').attr({src : '/images/right_box_btn_arrow_off.gif'});

            /* using the jquery's post(ajax) function and a lifesaver
            function serialize() which gets all the data from the form
            we submit it to send_email.php */
            jQuery.post("/includes/jquery/process_news_form.php", jQuery("#newsletter_form").serialize(),function(result){
                //and after the ajax request ends we check the text returned
                //alert(result);
                if(result){
                    //hide the form
                    jQuery('#newsletter_form').fadeOut('slow');                    
                    
                    //show the success message
                    jQuery('#newsletter_done').fadeIn('slow');                                                                
                }else{
                    alert('מצטערים, שגיאה בלתי צפויה. אנא נסה שנית.')                    
                    //reenable the submit button by removing attribute disabled and change the text back to Send The Message
                    jQuery('#send_news_form').removeAttr('disabled');
                }
            });
        }
    });
/************************************************************************************************************/
//      EVENTS / TRIPS
/************************************************************************************************************/
    /*GO BACK TO EVENT FORM*/
     jQuery("#back_event_form").click(function() {
        jQuery('#event_done').fadeOut('slow');                            
        jQuery('#event_form')[0].reset();                
        jQuery('#event_form').fadeIn('slow');    
    });
    /*START VALIDATING & SENDING EVENT FORM*/
    jQuery('#send_event_form').click(function(e){

        //stop the form from being submitted
        e.preventDefault();

        /* declare the variables, var error is the variable that we use on the end
        to determine if there was an error or not */
        var error = false;
        var tid = jQuery('input[name=tid]');
        var fname = jQuery('input[name=event_name]');
        var email = jQuery('input[name=event_email]');
        var phone = jQuery('input[name=event_tel]');
        var num1 = jQuery('input[name=event_num1]');
        var num2 = jQuery('input[name=event_num2]');
        var comment = jQuery('textarea[name=event_message]'); 
        var news = jQuery('textarea[name=event_register]');
        
                //Simple validation to make sure user entered something
                //If error found, add hightlight class to the text field
                if (fname.val()=='') {
                    var error = true;
                    fname.addClass('hightlight');
                    return false;
                } else fname.removeClass('hightlight');
                
                if ((email.val()=='') || (email.val().indexOf("@")==-1) || (email.val().indexOf(".")==-1)) {
                    var error = true;
                    email.addClass('hightlight');
                    return false;
                } else email.removeClass('hightlight');
        
                if ((phone.val()=='') || (!isValidPhoneNumber(phone.val()))) {
                    var error = true;
                    phone.addClass('hightlight');
                    return false;
                } else phone.removeClass('hightlight');
                
                if ((num1.val()=='') || (!isNumeric(num1.val()))) {
                    var error = true;
                    num1.addClass('hightlight');
                    return false;
                } else num1.removeClass('hightlight');
                
                if ((num2.val()=='') || (!isNumeric(num2.val()))) {
                    var error = true;
                    num2.addClass('hightlight');
                    return false;
                } else num2.removeClass('hightlight');
                
        //now when the validation is done we check if the error variable is false (no errors)
        if(error == false){
            //disable the submit button to avoid spamming
            //and change the button text to Sending...
            //jQuery('#send_event_form').attr({'disabled' : 'true'});
            jQuery('#send_event_form').attr({'disabled' : 'disabled'});
            //jQuery('#send_form').attr({src : '/images/right_box_btn_arrow_off.gif'});

            /* using the jquery's post(ajax) function and a lifesaver
            function serialize() which gets all the data from the form
            we submit it to send_email.php */
            jQuery.post("/includes/jquery/process_event_form.php", jQuery("#event_form").serialize(),function(result){
                //and after the ajax request ends we check the text returned
                //alert(result);
                if(result){
                    //hide the form
                    jQuery('#event_form').fadeOut('slow');                    
                    
                    //show the success message
                    jQuery('#event_done').fadeIn('slow');                                                                
                }else{
                    alert('מצטערים, שגיאה בלתי צפויה. אנא נסה שנית.')                    
                    //reenable the submit button by removing attribute disabled and change the text back to Send The Message
                    jQuery('#send_event_form').removeAttr('disabled');
                }
            });
        }
    });    
/************************************************************************************************************/
//      REPLIES
/************************************************************************************************************/
    /*GO BACK TO EVENT FORM*/
     jQuery("#back_reply_form").click(function() {
        jQuery('#reply_done').fadeOut('slow');                            
        jQuery('#reply_form')[0].reset();                
        jQuery('#reply_form').fadeIn('slow');    
    });
    /*START VALIDATING & SENDING EVENT FORM*/
    jQuery('#send_reply_form').click(function(e){

        //stop the form from being submitted
        e.preventDefault();

        /* declare the variables, var error is the variable that we use on the end
        to determine if there was an error or not */
        var error = false;
        var rname = jQuery('input[name=reply_name]');
        //var comment = jQuery('textarea[name=reply_desc]');                 
        var comment = jQuery("textarea[name='reply_desc']");
        alert("editor value-->"+comment.val());
        
                //Simple validation to make sure user entered something
                //If error found, add hightlight class to the text field
                if (rname.val()=='') {
                    var error = true;
                    rname.addClass('hightlight');
                    return false;
                } else rname.removeClass('hightlight');
                
                if (comment.val()=='') {
                    var error = true;
                    comment.addClass('hightlight');
                    return false;
                } else comment.removeClass('hightlight');
                
                
        //now when the validation is done we check if the error variable is false (no errors)
        if(error == false){
            //disable the submit button to avoid spamming
            //and change the button text to Sending...
            //jQuery('#send_reply_form').attr({'disabled' : 'true'});
            jQuery('#send_reply_form').attr({'disabled' : 'disabled'});

            /* using the jquery's post(ajax) function and a lifesaver
            function serialize() which gets all the data from the form
            we submit it to send_email.php */
            jQuery.post("/includes/jquery/process_reply_form.php", jQuery("#reply_form").serialize(),function(result){
                //and after the ajax request ends we check the text returned
                alert(result);
                if(result){
                    //hide the form
                    jQuery('#reply_form').fadeOut('slow');                    
                    
                    //show the success message
                    jQuery('#reply_done').fadeIn('slow');                                                                
                }else{
                    alert('מצטערים, שגיאה בלתי צפויה. אנא נסה שנית.')                    
                    //reenable the submit button by removing attribute disabled and change the text back to Send The Message
                    jQuery('#send_reply_form').removeAttr('disabled');
                }
            });
        }
    });    
}); /***END JQUERY*************/