jQuery(document).ready(
    function ()
    {
        jQuery("#cat").val('');
        jQuery('#cat').bind("change", function()
        {
            jQuery('#subcat').empty();
            jQuery('#subcat').attr('disabled', 'disabled');
            jQuery('#model').empty();
            jQuery('#model').attr('disabled', 'disabled');
            jQuery('#contactFormArea').css('display', 'none');
			jQuery('#freq').css('display', 'none');
            LoadSeries(jQuery(this).val());
			
        });
        
        jQuery("#subcat").bind("change", function()
        {
            jQuery('#model').empty();
            jQuery('#model').attr('disabled', 'disabled');
            jQuery('#contactFormArea').css('display', 'none');
			jQuery('#freq').css('display', 'block');	
            LoadFAQ(jQuery(this).val());
			LoadModels(jQuery(this).val());
			
        });
        
        jQuery("#model").bind("change", function(){
	
			
			ShowContactForm();
			
       });
       
        jQuery('#contactFormArea').css('display', 'none');

        jQuery('#contactForm').submit(function()
        {
            var error = false;
            jQuery.each(jQuery('#contactForm input'), function(i)
            {                        
                if(jQuery(this).val() == '')
                {  
                    
                    if(jQuery(this).attr('id') != "Company" && jQuery(this).attr('id') != "Title" && jQuery(this).attr('id') != "Phone")
                    {                    
                        jQuery(this).prev().css('color', 'red');                
                        error = true;
                    }else
                    {
                        jQuery(this).prev().css('color', 'black');
                    }
                }
            });
            
            if(error)
            {
                jQuery('#contactFormMissingInfo').css('display', 'block');
            }else
            {        
               jQuery('#contactFormMissingInfo').css('display', 'none');
               jQuery('#contactFormArea').css('display', 'none');
               jQuery('#SubmitProgress').css('display','block');
               jQuery.post('emailcontact.php', jQuery(this).serialize(), function (data)       
               {                   
                   if(data.Status == "OK")
                   {                   
                       jQuery('#SubmitProgress').css('display','none');
                       jQuery('#SubmitFinished').css('display','block');
                   }else
                   {
                       for(var key in data)
                       {
                           if(key == "Status")
                                continue;
                           var element = "#" + key;
                           jQuery(element).prev().css('color', 'red');
                           jQuery('#contactFormArea').css('display', 'block');
                           jQuery('#SubmitProgress').css('display','none');
                           jQuery('#contactFormMissingInfo').css('display', 'block');
                       }
                   }
               }, "json");
            }
           return false;
        });    
    }
)

function LoadSeries(Identifier)
{    
    $.getJSON("/series.php?id=" + Identifier, function(data)
    {
                       
       var output = [];
       output.push("<option value=''></option>");
       jQuery.each(data, function(index, option)
       {
           output.push("<option value='" + option.ID + "'>" + option.Title + "</option>");           
       });
       jQuery("#subcat").html(output.join(''));       
       jQuery('#subcat').removeAttr('disabled');
    });
}

function LoadModels(SeriesID)
{
    $.getJSON("/models.php?sid=" + SeriesID, function(data)
    {        
        var output = [];
        output.push("<option value=''></option>");
        jQuery.each(data, function(index, option)
        {
            output.push("<option value='" + option + "'>" + option + "</option>");
        });
       jQuery("#model").html(output.join(''));       
       jQuery('#model').removeAttr('disabled');
	   if (jQuery('#subcat').val() == '107')
	   {
			jQuery('#model').attr('disabled', 'disabled');
			//jQuery('#model').val('#model', 'GPS-4100M');
	   }
	   if(jQuery('#subcat').val() == '951')
	   {
			jQuery('#model').attr('disabled', 'disabled');
			ShowContactForm();
	   }
    });
}

function LoadFAQ(SeriesID) 
{
	$.getJSON("faq.php?sid=" + SeriesID, function(data) 
	{
		var output = [];
		var toc = [];
		jQuery.each(data, function(key, val)
		{
			toc.push('<li><a href="#' + val.AutoID + '" class="faqQuestion">' + val.Question + '</a></li>');
			output.push('<div id="' + val.AutoID + '" class="faqQuestion">' + val.Question + '</div><div class="faqAnswer">' + val.Answer + '</div><div class="faqQuestion" align="center"><a href="#top">Back To Top</a></div>');
		});
		
		$("#toc").html(toc.join(''));
		$("#faq").html(output.join(''));
	});
}

function ShowContactForm()
{
    jQuery('#contactFormArea').css('display', 'block');
    jQuery('#Model').val(jQuery('#model :selected').val());
}
