function getLampCost(cost)
{
	cost = (cost * 1.05).toFixed(2);
	return '&pound;' + cost + ' + &pound;4.95 delivery (exc VAT)';
}

jQuery(function(){
      
      jQuery('#make').val('');

		jQuery("a[href^=#]").click(function(e) {
	        e.preventDefault();
	    }).bind('selectstart', function(e) {
	        e.preventDefault();
	    });
      
      jQuery('#make').change(function(){
		  jQuery('#search-error').hide();
          if (jQuery(this).val())
          {
              jQuery('#make-load').show();
              jQuery.getJSON("http://www.solutionsav.com/wp-content/themes/mooh-group/midwich_api.php?t=" + new Date().getTime() + "&cmd=fetch_projectors&param=" + jQuery(this).val(), function(j){
      			var options = '<option value="">-- Please Select --</option>';
      			jQuery.each(j, function(key,val){
      				options += '<option value="' + key + '">' + val + '</option>';
      			})
      			jQuery("#model").html(options);
      			jQuery('#model').val('');
      			jQuery('#make-load').hide();
				jQuery('#details').show();
				jQuery("#lamp").hide();
				jQuery("#thanks").hide();
          	})
          }
          else
          {
              	jQuery("#model").html('<option value="">-- Please Select --</option>');
      			jQuery('#model').val('');
				jQuery('#details').show();
				jQuery("#lamp").hide();
				jQuery("#thanks").hide();
				jQuery("#form").hide();
          }
      })
      
      
      jQuery('#model').change(function(){
		  jQuery('#search-error').hide();
          if (jQuery(this).val())
          {
              jQuery('#model-load').show();
              jQuery.getJSON("http://www.solutionsav.com/wp-content/themes/mooh-group/midwich_api.php?t=" + new Date().getTime() + "&cmd=fetch_lamps_by_projector&param=" + jQuery(this).val(), function(j){
      			var lamp = j[0];
      			var stock_colour = '';
      			
      			if (lamp.stock < 1)
      			{
      			    stock_colour = ' style="color: red;"'
      			}
      			
      			jQuery("#lamp").html('\
      			    <h2>Lamp details</h2>\
      			    <p>\
      			        <strong>Manufacturer:</strong> ' + jQuery('#make option:selected').html() + '<br />\
      			        <strong>Manufacturers part no:</strong> ' + lamp.manu_pn + '<br />\
      			        <strong>Lamps Direct part no:</strong> ' + lamp.midw_pn + '<br />\
      			        <strong>Cost:</strong> ' + getLampCost(lamp.cost) + '<br />\
      			        <strong>Stock:</strong> <span' + stock_colour + '>' + lamp.stock + '</span>\
      			    </p>\
      			');
				jQuery("#details").hide();
				jQuery("#lamp").show();
				jQuery("#thanks").hide();
				jQuery("#form").show();
				jQuery("#manu_pn").val(lamp.manu_pn);
				jQuery("#midw_pn").val(lamp.midw_pn);
				jQuery('#model-load').hide();
      			
          	})
          }
          else
          {
					jQuery('#details').show();
					jQuery("#thanks").hide();
					jQuery("#form").hide();
					jQuery('#model-load').hide();
      			
          }
		
      })
      
	jQuery('#search').click(function(){
		jQuery('#search-load').show();
		jQuery('#search-error').hide();
          jQuery.getJSON("http://www.solutionsav.com/wp-content/themes/mooh-group/midwich_api.php?t=" + new Date().getTime() + "&cmd=fetch_lamps_by_part_no&param=" + jQuery('#part-number').val(), function(j){
  			
			if (j)
			{
				var lamp = j[0];
	  			var stock_colour = '';
  			
	  			if (lamp.stock < 1)
	  			{
	  			    stock_colour = ' style="color: red;"'
	  			}
  			
	  			jQuery("#lamp").html('\
	  			    <h2>Lamp details</h2>\
	  			    <p>\
	  			        <strong>Manufacturers part no:</strong> ' + lamp.manu_pn + '<br />\
	  			        <strong>Lamps Direct part no:</strong> ' + lamp.midw_pn + '<br />\
	  			        <strong>Cost:</strong> ' + getLampCost(lamp.cost) + '<br />\
	  			        <strong>Stock:</strong> <span' + stock_colour + '>' + lamp.stock + '</span>\
	  			    </p>\
	  			');
				jQuery("#details").hide();
				jQuery("#lamp").show();
				jQuery("#thanks").hide();
				jQuery("#form").show();
				jQuery("#manu_pn").val(lamp.manu_pn);
				jQuery("#midw_pn").val(lamp.midw_pn);
			}
			else
			{
					jQuery('#search-error').show();
					jQuery("#details").show();
					jQuery("#lamp").hide();
					jQuery("#thanks").hide();
					jQuery("#form").hide();
			}
  			jQuery('#search-load').hide();
		})
	})
	
	jQuery('#submit').click(function(){
		var error = false;
		//jQuery('#name-error').hide();
		//jQuery('#email-error').hide();
		//jQuery('#phone-error').hide();
		jQuery('#name').removeClass('error-field');
		jQuery('#email').removeClass('error-field');
		jQuery('#phone').removeClass('error-field');
		
		if(jQuery('#name').val().length < 2)
		{
			//jQuery('#name-error').show();
			jQuery('#name').addClass('error-field');
			error = true;
		}
		
		if(jQuery('#phone').val().length < 7)
		{
			//jQuery('#phone-error').show();
			jQuery('#phone').addClass('error-field');
			error = true;
		}
		
		if(jQuery('#email').val().length < 5 || jQuery('#email').val().indexOf('.') < 0 || jQuery('#email').val().indexOf('@') < 0)
		{
			//jQuery('#email-error').show();
			jQuery('#email').addClass('error-field');
			error = true;
		}
		
		if (!error)
		{
			jQuery('#submit-load').show();
			jQuery.post('http://www.solutionsav.com/wp-content/themes/mooh-group/enquiry-form.php', jQuery('#enquiry-form').serialize(), function(data, textStatus){
				jQuery("#form").hide();
				jQuery("#thanks").show();
			}, 'html');
		
		}
		jQuery('#submit-load').hide();
	})
	
      
})

