
// JavaScript Document
$().ready(function(){
	bindEvents();
	validateForms();
	hideSearchLabel();
	clearSearchField();	
	colorboxProductImages();
	customerSupport();		   
});

function bindEvents(){

	// homepage scroller
	$('#banner_menu').find('a').lm_scrollTo({scroller:'#panels'});

	$('#prod_spec').find('div.expand').hide();
	$('#prod_spec').find('a.spec_toggle').click(function() {
		if ($(this).hasClass('spec_toggle')) {
			$(this).removeClass('spec_toggle').addClass('spec_toggle_open');
		} else {
			$(this).removeClass('spec_toggle_open').addClass('spec_toggle');
		} 
		$(this).next('div.expand').slideToggle('medium');
		return false;
	});
	
	//history page
	/*
	function hidePnls() {
		$('.our_history').find('.pnl_hldr').hide();
	}
	
	$('#history_nav').find('li').click(function() {
		$('#history_nav').find('li').removeClass('active');
		$(this).addClass('active');
	});
	
	$('#ntft_btn').click(function() {
		hidePnls();
		$('#ntft_pnl').show();
	});
	$('#ntsx_btn').click(function() {
		hidePnls(); 
		$('#ntsx_pnl').show();
	});
	$('#ntsv_btn').click(function() {
		hidePnls();
		$('#ntsv_pnl').show();
	});
	$('#ntet_btn').click(function() {
		hidePnls();
		$('#ntet_pnl').show();
	});
	$('#ntnt_btn').click(function() {
		hidePnls();
		$('#ntnt_pnl').show();
	});
	$('#tt_btn').click(function() {
		hidePnls();
		$('#tt_pnl').show();
	});
	$('#twtn_btn').click(function() {
		hidePnls();
		$('#twtn_pnl').show();
	});
	*/
	
	//FAQs page
	
    $( ".accordion" ).accordion({ collapsible: true ,active: false,autoHeight: false });	
	$("#access_control_pnl").hide();
		
	function hidePnls() {
		$('.faqs').find('.pnl_hldr').hide();
	}
	
	$('#faqs_nav').find('li').click(function() {
		$('#faqs_nav').find('li').removeClass('active');
		$(this).addClass('active');
	});
	
	$('#pasco_btn').click(function() {
		hidePnls();
		$('#pasco_pnl').show();
	});
	$('#access_control_btn').click(function() {
		hidePnls(); 
		$('#access_control_pnl').show();
	});
	
	
	

}

// Form validation
function validateForms() {

    $("#product_comment").validate({
		rules: {
			name: "required",
			email: {
				required: true,
				email: true
			},
			subject: "required",
			message: "required"			
		},
		messages: {
			name: {
				required: "Please enter your name."
			},
			email: {
				required: "Please enter your email.",
				email: "Please enter a valid email."
			},
			subject: {
			    required: "Please enter a subject."	
			},
			message: {
			    required: "Please enter a message."	
			}			
		},
		errorClass: "error",
		submitHandler: function(form) {
			ajaxFormSubmit(form);
		}
	});	

	$("#contact_form").validate({
		rules: {
			title: "required",
			name: "required",
			email: {
				required: true,
				email: true					
			},
			tel: "required",
			organisation: "required",
			country: "required",
			message: "required"					
		},
		messages: {
			title: {
				required: "Please enter your title."				
			},
			name: {
				required: "Please enter your name."				
			},			
			email: {
				required: "Please enter your email.",
				email: "Please enter a valid email."								
			},
			tel: {
				required: "Please enter your telephone number."				
			},
			organisation: {
				required: "Please enter your organisation."				
			},			
			country: {
				required: "Please enter your country."				
			},			
			message: {
				required: "Please enter your message."				
			},
			errorClass: "error"													
		}	
	});	
	
}

// Submit form via AJAX
function ajaxFormSubmit(form) {

	$(form).ajaxSubmit({  
	
		target:".ajax_form_result", 
		beforeSubmit:function() {
		   $(".ajax_form_result").html('<p class="sending_message">Sending message...</p>');
	    },
	    success:function(response) {
			$(form).resetForm();
			$(".ajax_form_result").html('<p class="thankyou_message">Thank you, your message has been sent.</p>');
		}
	});
	
	return false;		
}


// Hide search label when label or field clicked
function hideSearchLabel() {
	$('.srch_bar label').click(function() {
		$(this).fadeOut(200);
	});
	$('.srch_bar .srch_field').click(function() {
		$('.srch_bar label').fadeOut(200);
	});	
}

// Ensure search field is empty when page refreshed
function clearSearchField() {
	$('.srch_bar .srch_field').val("");	
}


// Colorbox
function colorboxProductImages() {
	$('.product_image').colorbox();	
}
	
// Customer support document selects

function customerSupport() {

	$("#cat_select select").change(function(event) {
		
		var option_one_text = $("#cat_select select option:first-child").text();
		var selected_option_text = $("#cat_select option:selected").text();

		var selected_main_cat = $(this).val();  
		
		$('#subcat_select select option').remove();
		$('#subcat_select .select_result').html("");         
		$('#subcat_select select').append('<option value="">Select sub category -----</option>');
		
		$('#product_select select option').remove();
		$('#product_select .select_result').html(""); 
		$('#product_select select').append('<option value="">Select product -----</option>');
			
			
		if(selected_option_text != option_one_text){	
			
			$.ajax({
				url: '../customer_support/get_cats_and_prods/get_cats/' + selected_main_cat,
				dataType: "json",
				beforeSend: function() {
					$("#loading").show();  
				},
				success: function(data) {
					$.each(data, function(i, item) {
						$('#subcat_select select').append('<option value="' + data[i].cat_id + '">' + data[i].cat_name + '</option>');
						if(i ==0) {
							$('#subcat_select .select_result').html('<span class="success">1 subcategory found.</span>');	
						} else {
							$('#subcat_select .select_result').html('<span class="success">' + (i+1) + ' subcategories found.</span>');
						}
	
					});
					$("#loading").hide(); 				
				},
				error: function(jqXHR, textStatus, errorThrown) {
					$('#subcat_select .select_result').html('<span class="error">No subcategories found.</span>');
					$("#loading").hide();
					$("#documents_form .gen_btn a").hide();
				}
			});		

		} 	

	});    
	
	$("#subcat_select select").change(function(event) {
		
		
		var option_one_text = $("#subcat_select select option:first-child").text();
		var selected_option_text = $("#subcat_select option:selected").text();		
		
		var selected_sub_cat = $(this).val();  
		
		$('#product_select select option').remove();
		$('#product_select select').append('<option value="">Select product -----</option>');
		
		if(selected_option_text != option_one_text){
		
			$.ajax({
				url: '../customer_support/get_cats_and_prods/get_prods/' + selected_sub_cat,
				dataType: "json",
				beforeSend: function() {
				  $("#loading").show();  
				},
				success: function(data) {
	
					$.each(data, function(i, item) {
						$('#product_select select').append('<option value="' + data[i].url_title + '">' + data[i].title + '</option>');
						if(i == 0) {
							$('#product_select .select_result').html('<span class="success">1 product found.</span>'); 						
						} else {
							$('#product_select .select_result').html('<span class="success">' + (i+1) + ' products found.</span>'); 						
						}                 
					});
					$("#loading").hide(); 					
				},
				error: function(jqXHR, textStatus, errorThrown) {
					$('#product_select .select_result').html('<span class="error">No products found.</span>');
					$("#loading").hide();
					$("#documents_form .gen_btn a").hide();
				}
			});		
		
		} else {
			
			$('#product_select select option').remove();
			$('#product_select .select_result').html(""); 
			$('#product_select select').append('<option value="">Select product -----</option>');			
			$("#documents_form .gen_btn a").hide();	
			
		}
		
	});
	
	$("#product_select select").change(function(event) {
		
		var selected_product = $(this).val();
		
		var option_one_text = $("#product_select select option:first-child").text();
		var selected_option_text = $("#product_select option:selected").text();	
		  
		if(selected_option_text != option_one_text){
		
			$("#documents_form .gen_btn a").show();
			$("#documents_form .gen_btn a").attr('href','customer_support/document_select/' + selected_product);			

		} else {
			
			$("#documents_form .gen_btn a").hide();
						
		}		  
		
	});	
	
	$("#downloads_form select").change(function(event) {
		
		var selected_category = $(this).val();
		
		var option_one_text = $("#downloads_form select option:first-child").text();
		var selected_option_text = $("#downloads_form option:selected").text();			  
		
		if(selected_option_text != option_one_text){
		
			$("#downloads_form .gen_btn a").show();
			$("#downloads_form .gen_btn a").attr('href','customer_support/software_select/' + selected_category);

		} else {
			
			$("#downloads_form .gen_btn a").hide();
						
		}			

		return false;
		
	});	
	
		
	  	

}

