jQuery(document).ready(function()
{
    // the benefits button is selected from the technology menu        
	jQuery("#technology_benefits").livequery('click', function(e) 
	{
		// the class of the selected image <a href class is selected as the identifier of the image and passed on
		var option = jQuery(this).attr('class');
		swapImages(option); 
		var technology =  jQuery(".current_technology").attr('id');
		jQuery('#technology_text').fadeOut(500,function()
		{
			//jQuery("#technology_"+option+"_img").addClass("imagesOn");
		});
		jQuery("#technology_text").load(technology+"/benefits",function()
		{
			//when content is loaded fade in
			jQuery('#technology_text').fadeIn(800);
		});
		
	})	

    // the costs button is selected from the technology menu        
	jQuery("#technology_costs").livequery('click', function(e) 
	{
		var option = jQuery(this).attr('class');
		swapImages(option); 
		var technology =  jQuery(".current_technology").attr('id');
		jQuery('#technology_text').fadeOut(500,function()
		{
			jQuery("#technology_"+option+"_img").addClass("imagesOn");
		});
		jQuery("#technology_text").load(technology+"/costs",function()
		{
			//when content is loaded fade in
			jQuery('#technology_text').fadeIn(800);
			jQuery("#technology_costs_img").addClass("imagesOn");
		});
		
	})
	
	// the installation button is selected from the technology menu        
	jQuery("#technology_installation").livequery('click', function(e) 
	{
		var option = jQuery(this).attr('class');
		swapImages(option); 
		swapImages(jQuery(this).attr('class'));
		var technology =  jQuery(".current_technology").attr('id');
		jQuery('#technology_text').fadeOut(500,function()
		{
			jQuery("#technology_"+option+"_img").addClass("imagesOn");
		});
		jQuery("#technology_text").load(technology+"/installation",function()
		{
			//when content is loaded fade in
			jQuery('#technology_text').fadeIn(800);
		});
	})
	
	// the faq button is selected from the technology menu        
	jQuery("#technology_faq").livequery('click', function(e) 
	{
		var option = jQuery(this).attr('class');
		swapImages(option); 
		swapImages(jQuery(this).attr('class'));
		var technology =  jQuery(".current_technology").attr('id');
		jQuery('#technology_text').fadeOut(500,function()
		{
			jQuery("#technology_"+option+"_img").addClass("imagesOn");
		});
		jQuery("#technology_text").load(technology+"/faq",function()
		{
			//when content is loaded fade in
			jQuery('#technology_text').fadeIn(800);
		});
	})
	
	// the case studies button is selected from the technology menu        
	jQuery("#technology_casestudies").livequery('click', function(e) 
	{
		var option = jQuery(this).attr('class');
		swapImages(option); 
		swapImages(jQuery(this).attr('class'));
		var technology =  jQuery(".current_technology").attr('id');
		jQuery('#technology_text').fadeOut(500,function()
		{
			jQuery("#technology_"+option+"_img").addClass("imagesOn");
		});
		jQuery("#technology_text").load(technology+"/casestudies",function()
		{
			//when content is loaded fade in
			jQuery('#technology_text').fadeIn(800);
		});
	})
	
	// the introduction button is selected from the technology menu        
	jQuery("#technology_enquiries").livequery('click', function(e) 
	{
		var technologyId = jQuery("#technologyId").val();
		var option = jQuery(this).attr('class');
		swapImages(option); 
		swapImages(jQuery(this).attr('class'));
		var technology =  jQuery(".current_technology").attr('id');
		jQuery('#technology_text').fadeOut(10,function()
		{
			jQuery("#technology_"+option+"_img").addClass("imagesOn");
		});
		jQuery("#technology_text").load(technology+"/enquiries?technologyId="+technologyId,function()
		{
			//when content is loaded fade in
			jQuery('#technology_text').fadeIn(1000);
			jQuery('#enquiryTechnologyId').val(technologyId);
			
			jQuery('#technlogyEnquiryForm').ajaxForm(
			{
				beforeSubmit: validate, //validate before submitting
				success: function(result) 
				{
					if (result == "OK")
					{
						jQuery("#technology_text").html("<h1>Thank You</h1><p>Your Enquiry has been posted and we will be in touch very shortly</p>");
					}else
					{
						jQuery("#technology_text").html(result);
					}
				}
			});
	
		});
	})
	
	// the introduction button is selected from the technology menu        
	jQuery("#technology_introduction").livequery('click', function(e) 
	{
		var option = jQuery(this).attr('class');
		swapImages(option); 
		swapImages(jQuery(this).attr('class'));
		var technology =  jQuery(".current_technology").attr('id');
		jQuery('#technology_text').fadeOut(500,function()
		{
			jQuery("#technology_"+option+"_img").addClass("imagesOn");
		});
		jQuery("#technology_text").load(technology+"/introduction",function()
		{
			//when content is loaded fade in
			jQuery('#technology_text').fadeIn(800);
		});
	})

	var previousLink = "introduction";

	function swapImages(option)
	{
		var imgLoc = jQuery(".images_location").attr("id"); // get all images location
		var imgSrcOff =imgLoc+"/buttons-off/";  // location for buttons off
		imgSrcOn =imgLoc+"/buttons-on/"+option+"-on.jpg"; // location for buttons on
		var imgEnd = "-off.jpg"; 
		
		
		// Remove previous active button if there is one
		if (previousLink != "")
		{
			jQuery("#technology_"+previousLink).html('<img class="imagesOff" id="technology_"'+previousLink+'"_img" src="'+imgLoc+'buttons-off/'+previousLink+'-off.jpg">');
		}

		// set image as active on and remove the imagesOff class
		//jQuery("#technology_"+option+"_img").attr("src",imgSrcOn);
		
		jQuery("#technology_"+option).html('<img class="imagesOn" id="technology_"'+option+'"_img" src="'+imgLoc+'buttons-on/'+option+'-on.jpg">');
	
		//jQuery("#technology_"+option+"_img").removeClass("imagesOff");
		
		// Set this button as previous link
		previousLink = option;
	}
	
	/* Technology Enquiry submit*/     
	
	
		/**
	* Check if valid email address
	*/
	function isValidEmail(str)
	{
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if (filter.test(str))
		{
			testresults=true;
		}
		else
		{
			testresults=false;
		}
			return (testresults);
	}
	
	
	/**
	* Validation for the enquiry form
	*/
	function validate(formData, jqForm, options) { 
		 var form = jqForm[0]; 
		 var name = form.name.value;
		 var email = form.recipient.value;
		 var message = form.text.value;
		 var n = jQuery("input:checked").length;

		if(n == 0) 
		{ 
			showErrorMessage("Please select atleast one supplier!");
			return false;
		}
		 // check name, email and message
		 if (name == "")
		 {
			showErrorMessage("Please enter your name!");
			return false;
		 }else
		  if(!isValidEmail(email))
		 {
			showErrorMessage("Please enter a valid email address!");
			return false;
		 }else
		 if (message == "")
		 {
		 	showErrorMessage("Please enter your query in the message box!");
			return false;
		 }
		
	}
	
		
	/**
	* Displaying errors for validation of enquiry form
	*/
	function showErrorMessage(error)
	{
		jQuery("#MyerrorMessage").html(error);
	}
	

});


