function check_comments_form(){
	var name = $('input[name=name]').val();
	var email = $('input[name=email]').val();
	var body = $('textarea[name=body]').val();
	if(name==''||email==''||body==''||name.indexOf("Name (Required)")!=-1||email.indexOf('Email (Required)')!=-1||body.indexOf('Type your comment here...')!=-1){
		alert('Check Required fields!');
		return false;
	}
}
function load_states(){
	var country = $('input[name=country]').val();
	$.ajax({
		url: "/dealers/get_states/"+new Date().getTime(),
		type: "POST",
		data: ({country: country}),
		success: function(msg){
			if(msg){
				$('#cusel-scroll-state').html(msg);
				var params = {refreshEl: "#state", visRows: 10, scrollArrows: false };
				cuSelRefresh(params);
				$('#cusel-scroll-state .cuselActive').trigger('click');
			}
		}});
}
function update_dealers(){
	var country = $('input[name=country]').val();
	var state = $('input[name=state]').val();
	if(country!=0&&country!=''){
		$.ajax({
			url: '/dealers/get_dealers/'+new Date().getTime(),
			type: "POST",
			data: ({country : country,state : state}),
			success: function(msg){ $('#results').html(msg);}
		});
	}
	else{
		$('#results').html('');
	}
	return false;
};
function selector_type(obj){
	var machine_type = parseInt($('input[name=machine_type]').val());
	if(machine_type>0&machine_type<5){
		$('.selector_box.selector').show();
		$('.selector_box.selector .inner_box:visible').slideUp();
		$('.selector_box.selector .box_'+machine_type).slideDown();
		obj.parents('.selector_box').find('.status_icon').addClass('check');
	}
	else{
		$('.selector_box.selector').hide();
		obj.parents('.selector_box').find('.status_icon').removeClass('check');
	}
}

function update_products(){
	var machine_type = parseInt($('input[name=machine_type]').val());
	var chuck_size = ($('#chuck_size_check').hasClass('minus')) ? 0 : $('input[name=chuck_size]').val();
	var tool_storage_capacity = ($('#tool_storage_capacity_check').hasClass('minus')) ? 0 : $('input[name=tool_storage_capacity]').val();
	var bar_capacity = ($('#bar_capacity_check').hasClass('minus')) ? 0 : $('input[name=bar_capacity]').val();
	var rpm = ($('#rpm_check').hasClass('minus')) ? 0 : $('input[name=rpm]').val();
	var mill_orientation = ($('#mill_orientation_check').hasClass('minus')) ? 0 : $('input[name=mill_orientation]').val();
	var rpm2 = ($('#rpm2_check').hasClass('minus')) ? 0 : $('input[name=rpm2]').val();
	var spindle_type = ($('#spindle_type_check').hasClass('minus')) ? 0 : $('input[name=spindle_type]').val();
	var axis_travels_x = ($('#axis_travels_x_check').hasClass('minus')) ? 0 : $('input[name=axis_travels_x]').val();
	var axis_travels_y = ($('#axis_travels_y_check').hasClass('minus')) ? 0 : $('input[name=axis_travels_y]').val();
	var axis_travels_z = ($('#axis_travels_z_check').hasClass('minus')) ? 0 : $('input[name=axis_travels_z]').val();
	var part_diameter = ($('#part_diameter_check').hasClass('minus')) ? 0 : $('input[name=part_diameter]').val();
	var part_length = ($('#part_length_check').hasClass('minus')) ? 0 : $('input[name=part_length]').val();
	var spindle_type3 = ($('#spindle_type3_check').hasClass('minus')) ? 0 : $('input[name=spindle_type3]').val();
	var rpm4 = ($('#rpm4_check').hasClass('minus')) ? 0 : $('input[name=rpm4]').val();
	var axis_travels_x4 = ($('#axis_travels_x4_check').hasClass('minus')) ? 0 : $('input[name=axis_travels_x4]').val();
	var axis_travels_y4 = ($('#axis_travels_y4_check').hasClass('minus')) ? 0 : $('input[name=axis_travels_y4]').val();
	var axis_travels_z4 = ($('#axis_travels_z4_check').hasClass('minus')) ? 0 : $('input[name=axis_travels_z4]').val();
	$.ajax({
		url: '/machines/get_products/'+new Date().getTime(),
		type: "POST",
		data: ({machine_type : machine_type,chuck_size : chuck_size,tool_storage_capacity : tool_storage_capacity,bar_capacity : bar_capacity,rpm : rpm,mill_orientation : mill_orientation,rpm2 : rpm2,spindle_type : spindle_type,axis_travels_x : axis_travels_x,axis_travels_y : axis_travels_y,axis_travels_z : axis_travels_z,part_diameter : part_diameter,part_length : part_length,spindle_type3 : spindle_type3,rpm4 : rpm4,axis_travels_x4 : axis_travels_x4,axis_travels_y4 : axis_travels_y4,axis_travels_z4 : axis_travels_z4}),
		success: function(msg){ $('#results').html(msg);}
	});
	return false;
};

var params = {}; 
if (location.search) { 
    var parts = location.search.substring(1).split('&'); 
    for (var i = 0; i < parts.length; i++) { 
        var nv = parts[i].split('='); 
        if (!nv[0]) continue; 
        params[nv[0]] = nv[1] || true; 
    } 
} 
var mt = params.mt; 

jQuery(document).ready(function(){
	$('#header #object_div,#header #object').click(function(){window.location='/';});
	$('.comments_block .delete').click(function(){
		if(!confirm('Are you sure?')) return false;
		$.get(this.href);
		$(this).parents(".comments_block").remove();
		return false;
	});
	$(".cusel").each(function(){ var w = parseInt(jQuery(this).width()), scrollPanel = jQuery(this).find(".cusel-scroll-pane");
		if(w>=scrollPanel.width()) { jQuery(this).find(".jScrollPaneContainer").width(w); scrollPanel.width(w);}
	});
	if($(".cusel")) { 
		var params = { changedEl: ".cusel", visRows: 10, scrollArrows: false }
		cuSel(params);
	}
	$('.drop').mouseenter( function() {
		$(this).addClass('hovered');
		$(this).find('.submenu').stop(true,true).show();
	}).mouseleave(function() {
		$(this).removeClass('hovered');
		$(this).find('.submenu').stop(true,true).hide();
	});
	$('input[name=machine_type]').live('change', function() { selector_type($(this));update_products();});
	$('.selector input').live('change', function() {
		if($(this).val()!=0) $(this).parents('.cusel').next('.status_icon').addClass('check');
		else $(this).parents('.cusel').next('.status_icon').removeClass('check');
		update_products();
	});
	$('.selector .status_icon.check').live('click', function() { $(this).removeClass('check').addClass('minus'); update_products();});
	$('.selector .status_icon.minus').live('click', function() { $(this).removeClass('minus').addClass('check'); update_products();});
	$('.dealers input[name=country]').change(function() { load_states();update_dealers();});
	$('.dealers input[name=state]').live("change",function() { update_dealers();});
	$(function() {if(mt==2 || mt==3 || mt==4) $('#machine_type').val(mt).trigger('change');});
});
