es = {
	c: 0,
		
	on_load: function () {	
	
		$('#group-login').click( function() {
			if( !$('#group-id').val() ) {
				alert('Please enter your group id');
			} else {
				location.href = "http://" + $('#group-id').val() + ".eclipseski.co.uk";							
			}
		});

		$('#group-id').keypress( function(event) {
			if( event.which == 13 ) {
				if( !$('#group-id').val() ) {
                                	alert('Please enter your group id');
	                        } else {
	                                location.href = "http://" + $('#group-id').val() + ".eclipseski.co.uk";
					return false;
	                        }
			}
		});

		//load the gallery
		egal.on_load();
		
		$('#adv-text').mouseenter( function() {		
			$('#adv-text').css('background','url("../images/search/adv-arrow-hover.jpg") no-repeat scroll right top transparent');		
		});
		$('#adv-text').mouseleave( function() {		
			$('#adv-text').css('background','url("../images/search/adv-arrow.jpg") no-repeat scroll right top transparent');		
		});
		
		$('#adv-text').click( function() {
			var h = parseInt($('#adv-search').css('height').replace('px',''));		
			if( h > 0 ) {			
				$('#adv-search').slideUp();
				$('#adv-text').css('background','url("../images/search/adv-arrow.jpg") no-repeat scroll right top transparent')
				$('#adv-text').mouseenter( function() {		
					$('#adv-text').css('background','url("../images/search/adv-arrow-hover.jpg") no-repeat scroll right top transparent');		
				});
				$('#adv-text').mouseleave( function() {		
					$('#adv-text').css('background','url("../images/search/adv-arrow.jpg") no-repeat scroll right top transparent');		
				});
			} else {			
				$('#adv-search').slideDown();
				$('#adv-text').css('background','url("../images/search/adv-arrow-up.jpg") no-repeat scroll right top transparent')
				$('#adv-text').mouseenter( function() {		
					$('#adv-text').css('background','url("../images/search/adv-arrow-up-hover.jpg") no-repeat scroll right top transparent');		
				});
				$('#adv-text').mouseleave( function() {		
					$('#adv-text').css('background','url("../images/search/adv-arrow-up.jpg") no-repeat scroll right top transparent');		
				});
			}				
			var moveup = ($(window).height() - 233)*-1;				
			$.scrollTo( '#adv-search', 1000, {offset:moveup} );		
		});
		
		$('.cufon-hide').css('visibility','visible');
			
		$('#enquire-homepage').click( function() {
			$('body > *').css('cursor','wait');
		});
		
		$('#clear-cache').click( function() {
			$('body > *').css('cursor','wait');
			
			$.ajax({
				  url: 'clear-cache.php',
				  success: function(data) {			    
				    $('body > *').css('cursor','auto');
				    if( data == 'done' ) {
				    	alert('Cache now empty!');
				    }
				  }
				});
		});
		
		$('.book-now').click( function() {		
			$.scrollTo('#MakeAnEnquiryForm', 1000);
		});
		
		es.contact_form_init();
		es.contact_form_home_init();
		
		$('#holidayEnquiryForm #SubmitBtn').click( function() {
			es.ajax_submit_form($(this));
		});
	
		$('#Enquiry .submit').click( function() {
			es.ajax_home_enquire_form();
		});
		
		$('#newsletter .submit').click( function(){
			es.ajax_newsletter();
		});
			
		//bind the onclick events for what question marks?
		$('.what').click( function() {
			
			if( $('.what-arrow').is(':visible') ) {
				$('.what-arrow').hide();
			} else {
				$('.what-arrow').show();
			}
			
			$('.what-popup',this).animate({
				width: ['toggle', 'swing'],
			    height: ['toggle', 'swing'],
			    opacity: 'toggle'
			  }, 300, function() {
			    // Animation complete.
			  });

		});
	},
	
	ajax_newsletter: function(){
		
		$('#newletter .small-timer').show();
		$('#newsletter .submit').css('cursor','wait');
		$('body > *').css('cursor','wait');
					
			var dataToServer = 'email=' + $('#newsletter #email').val();

			$.ajax({
			      type: "GET",
			      url: "newsletter-process.php",
			      data: dataToServer,			      
			      success: function(data) {
					if( data == 'done' ) {
						$('#newsletter .msg').html('You have been added to our newsletter group.');
						$('#newsletter .msg').slideDown();
						$('#newletter .small-timer').hide();
						$('#newsletter .submit').css('cursor','auto');
						$('body > *').css('cursor','auto');
					}				
			      }	
			 });
		
	},
		
	ajax_submit_form: function(){
		$('#holidayEnquiryForm .small-timer').show();
		$('#SubmitBtn').css('cursor','wait');
		$('body > *').css('cursor','wait');
		$('#ajax_result').slideUp();
				
		if( es.is_valid( $('#holidayEnquiryForm') ) ) {
			
			var dataToServer = 'name=' + $('#holidayEnquiryForm #enquire_LeadPassengerName').val() + '&' +
		    				   'persons=' + $('#holidayEnquiryForm #enquire_NumberOfPersons').val() + '&' +
		    				   'email=' + $('#holidayEnquiryForm #enquire_Email').val() + '&' + 
		    				   'tel=' + $('#holidayEnquiryForm #enquire_TelephoneNumber').val() + '&' + 
		    				   'notes=' + $('#holidayEnquiryForm #NotesField').val() + '&' + 
		    				   'contactme=' + $('#holidayEnquiryForm #ContactOffersCheck input').val() + '&' +		
		    				   'itineraryid=' + $(document).getUrlParam("ItineraryId");

			$.ajax({
			      type: "GET",
			      url: "dealDetail-process.php",
			      data: dataToServer,			      
			      success: function(data) {
					$('#holidayEnquiryForm .small-timer').hide();
					if( data == 'done' ) {
                                          $('#MakeAnEnquiryForm').append('<iframe src="ppc.html" width="0px" height="0px"></iframe>');
					  $.scrollTo( '#MakeAnEnquiryForm', 1000);						
					  $('#ajax_result').slideDown();								  
					  $('#ajax_result').html('<b>Success</b>.<br />  Thank you for your interest.  A member of our staff will contact you shortly to discuss your holiday.');
					  $('body > *').css('cursor','auto');
					  $('#SubmitBtn').css('cursor','pointer');
					}
			      }
			 });
		} else {
			
			//errors exist, scroll to top of form and display message
			$('#holidayEnquiryForm .small-timer').hide();
			$.scrollTo( '#MakeAnEnquiryForm', 1000);
			es.inline_validation( $('#MakeAnEnquiryForm'), 'enquire_LeadPassengerName' );
			es.inline_validation( $('#MakeAnEnquiryForm'), 'enquire_NumberOfPersons' );
			es.inline_validation( $('#MakeAnEnquiryForm'), 'enquire_Email' );
			es.inline_validation( $('#MakeAnEnquiryForm'), 'enquire_TelephoneNumber' );
										
			$('#ajax_error').html('Sorry, there appear to be errors on the form below, please resolve these and try again.');
			$('#ajax_error').css('border','2px solid red');
			$('#ajax_error').slideDown();
			$('#ajax_result').slideUp();
			$('body > *').css('cursor','auto');
			$('#SubmitBtn').css('cursor','pointer');		    				
		}
		
		return false;
	},
	
	ajax_home_enquire_form: function(){
		
		$('#Enquiry .small-timer').show();		
		$('body > *').css('cursor','wait');
		$('#Enquiry .submit').css('cursor','wait');
						
		if( es.home_is_valid( $('#Enquiry') ) ) {
		
			var dataToServer = 'quote-full-name=' + $('#Enquiry #quote-full-name').val() + '&' +
		    				   'quote-tel=' + $('#Enquiry #quote-tel').val() + '&' +
		    				   'quote-email=' + $('#Enquiry #quote-email').val() + '&' + 
		    				   'quote-msg=' + $('#Enquiry #quote-msg').val() + '&' + 
		    				   'quote-news=' + $('#Enquiry #quote-news-yes').val(); 
		    				   
			$.ajax({
			      type: "GET",
			      url: "enquiry-process.php",
			      data: dataToServer,			      
			      success: function(data) {
												
					$('#Enquiry .small-timer').hide();
					
					if( data == 'done' ) {					  						
					  $('.home-ajax-result').slideDown();								  
					  $('.home-ajax-result').html('<b>Success</b>.<br />  Thank you for your interest.  A member of our staff will contact you shortly to discuss your holiday.');
					  $('body > *').css('cursor','auto');
					  $('#Enquiry .submit').css('cursor','pointer');
					}
			      }
			 });
		} else {
			//errors exist, scroll to top of form and display message			
			es.inline_home_validation( $('#Enquiry'), 'quote-full-name' );
			es.inline_home_validation( $('#Enquiry'), 'quote-email' );
			es.inline_home_validation( $('#Enquiry'), 'quote-tel' );
			
			$('.small-timer').hide();
										
			$('body > *').css('cursor','auto');
			$('#Enquiry .submit').css('cursor','pointer');		    				
		}
		
		return false;
	},
	
	home_is_valid: function(form){
		
		var valid = true;
    	var has_email = true;
    	
    	var name = $("#quote-full-name", form).val();        	
    	if (name == "") {  
    		valid = false;  
    	} 

    	var email = $("#quote-email", form).val();        	
    	if (email == "") {  
    		has_email = false;  
    	}
    	
    	var tel = $("#quote-tel", form).val();        	
    	if (tel == "" && !has_email) {  
    		valid = false;  
    	}    	 
    	
    	return valid;  	
	},
	
	contact_form_init: function() {
    	   	
    	$('#holidayEnquiryForm input').blur( function() {    		
    		//do inline validations
    		es.inline_validation( $('#holidayEnquiryForm'), $(this).attr('id') );
    	});
    	
    },
    
    contact_form_home_init: function() {
	   	
    	$('#Enquiry input').blur( function() {    		
    		//do inline validations
    		es.inline_home_validation( $('#Enquiry'), $(this).attr('id') );
    	});
    	
    },
    
    inline_validation: function( form, id ) {

    	var name = $("input#enquire_LeadPassengerName", form).val();        	
    	if (name == "" && id == 'enquire_LeadPassengerName') {        		
    		$('.name', form).html('Your name is required, please enter a name.');
    		$('.name', form).slideDown();
    	    return false;  
    	} else if ( name != "" ) {
    		$('.name', form).slideUp();
    		$('#ajax_error').slideUp();
    	}
    	
    	var persons = $("input#enquire_NumberOfPersons", form).val();        	
    	if ( (persons == "" || persons == 0) && id == 'enquire_NumberOfPersons') {        		
    		$('.persons', form).html('The number of people travelling is required, please enter an amount greater than zero.');
    		$('.persons', form).slideDown();
    	    return false;  
    	} else if ( persons != "" && persons != 0 ) {
    		$('.persons', form).slideUp();
    		$('#ajax_error').slideUp();
    	}
    	    	
    	var email = $("input#enquire_Email", form).val();
    	if (email == "" && id == 'enquire_Email') {        		
    		$('.email', form).html('An email address is required, please enter an email address.');
    		$('.email', form).slideDown();
    	    return false;  
    	} else if ( email != "" ) {        		
    		if( !es.isValidEmailAddress(email) ) {        			
    			$('.email', form).html('Please enter a valid email address');
    			$('.email', form).slideDown();
        		return false;  
    		} else {
    			$('.email', form).slideUp();
    			$('#ajax_error').slideUp();
    		}        		
    	}    	
    	    
    	var tel = $("input#enquire_TelephoneNumber", form).val();        	
    	if ( (tel == "" || tel == 0) && id == 'enquire_TelephoneNumber') {        		
    		$('.tel', form).html('A contact number is required, please enter a telephone number.');
    		$('.tel', form).slideDown();
    	    return false;  
    	} else if ( tel != "" && tel != 0 ) {
    		$('.tel', form).slideUp();
    		$('#ajax_error').slideUp();
    	}
    	return true;  
    },
    
    inline_home_validation: function( form, id ) {

    	var name = $("#quote-full-name", form).val();        	
    	if (name == "" && id == 'quote-full-name') {        		
    		$('.quote-full-name', form).html('Your name is required, please enter a name.');
    		$('.quote-full-name', form).slideDown();
    	    return false;  
    	} else if ( name != "" ) {
    		$('.quote-full-name', form).slideUp();
    		$('#ajax_error').slideUp();
    	}
    	
    	var email = $("#quote-email", form).val();
    	if (email == "" && id == 'quote-email') {    		
    		if( $("#quote-tel", form).val() ) {
	    		return true;
	    	} else {
	    		$('.quote-email', form).html('An email address is required, please enter an email address.');
		    	$('.quote-email', form).slideDown();
	    		return false;
	    	}	    	
    	} else if ( email != "" ) {
    		$('.quote-email', form).slideUp();
    		$('.quote-tel', form).slideUp();
    		$('#ajax_error').slideUp();
    	}
    	
    	var tel = $("#quote-tel", form).val();    	
    	if (tel == "" && id == 'quote-tel') {    		
    		if( $("#quote-email", form).val() ) {
	    		return true;
	    	} else {
	    		$('.quote-tel', form).html('A telephone number is required, please enter a telephone number.');
	    		$('.quote-tel', form).slideDown();
	    		return false;
	    	}    	      
    	} else if ( tel != "" ) {
    		$('.quote-tel', form).slideUp();
    		$('.quote-email', form).slideUp();
    		$('#ajax_error').slideUp();
    	}
    	    	   	
    	
    	
    	return true;  
    },
    
	is_valid: function(form){
		var valid = true;
    	var has_email = true;
    	
    	var name = $("input#enquire_LeadPassengerName", form).val();        	
    	if (name == "") {  
    		valid = false;  
    	} 
    	
    	var persons = $("input#enquire_NumberOfPersons", form).val();        	
    	if (persons == "") {  
    		valid = false;  
    	} 
    	
    	var email = $("input#enquire_Email", form).val();
    	if (email == "") { 
    		has_email = false;  
    	} else if ( email != "" ) {        		
    		if( !es.isValidEmailAddress(email) ) {  
        		has_email = false;  
    		}        		
    	}
    	    
    	var tel = $("input#enquire_TelephoneNumber", form).val();        	
    	if (tel == "") {  
    		valid = false;  
    	} 
    	
    	return valid;  	
	},
	
	isValidEmailAddress: function( email_address ) {
    	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    	return pattern.test(email_address);
    },
    
	stat_holiday_page: function(searchCacheId,page) {		
		$.ajax({
			  url: 'stats/stat-enquire.php?searchCacheId=' + searchCacheId + '&page=' + page,
			  success: function(data) {			    
			    //result should be done or error msg
			  }
			});
	},
	
	stat_book: function(holidayCacheId, searchCacheId) {		
		$.ajax({
			  url: 'stats/stat-book.php?holidayCacheId=' + holidayCacheId + '&searchCacheId=' + searchCacheId,
			  success: function(data) {			    
			    //result should be done or error msg
			  }
			});
	}
}

