$(document).ready(function(){
	$('#data_spinner').css('display', 'inline');
  	$.post('../common/data/refining_search.php', "1=1", function(data){
		$('#comparison-results').html(data);
		$('#data_spinner').css('display', 'none');
		//$('#search-attributes-list')[0].selectedIndex = 0;
	});
	$('#frmRefineSearch').clearForm();
});

function showAdvanced(type){
	$('#app_type').val(type);
	$('#advancedArea' + type).fadeIn();
}

function validForm(){
  form = $('#quick-quote')[0];

  //console.dir(form.agree_tc);

  if(form.title.value.length < 1){
    alert("Select a title");
    return false;
  }

  if(form.first_name.value.length < 2){
    alert("Please enter a valid first name");
    return false;
  }
  
  if(form.contact_number.value.length < 2){
    alert("Please enter a valid contact number");
    return false;
  }

  if(form.agree_tc.checked != true){
    alert('You must agree to the terms and conditions to apply');
    return false;
  }

  return true;
}

var filtersCount = 0;
var compares = 0;

function doCompare(){
  $('#frmProductsList')[0].submit();
  //compares = $('#frmProductsList').serialize();
}

function pre_def(type){
  $.post('../common/data/refining_search.php?pre=' + type, "1=1", function(data){
    $('#comparison-results').html(data);
  });
}

function hideColumn(pid){
  $('.com-column-' + pid).fadeOut("slow");
  
  checkid = 4;
  var col_css = $('.com-column-' + checkid).css('display');

  if(col_css == "none"){
    $('.com-column-' + checkid).fadeOut("slow");
  }
}

function refSearch(block_id, attrib_name, attrib_title, attrib_id, min, max, pos){
  pos = pos+5;
  filtersCount++;

	$('#attrib-id-' + pos).val(attrib_id);
	$('#attrib-name-' + pos).val(attrib_title);
	$('#attrib-min-' + pos).val(min);
	$('#attrib-max-' + pos).val(max);
	$('#attrib-ranges-' + pos).fadeOut();
	filters = $('#frmRefineSearch').serialize();

	$.post('../common/data/refining_search.php?page=' + $('#frmProductsList')[0].current_page.value, filters, function(data){
		$('#comparison-results').html(data);
		$('#search-attributes-list')[0].selectedIndex = 0;
		$('#attrib-chosen-' + pos).html(attrib_title + '<a href="javascript:devSearch(' + pos + ')"><img src="../images/clean/del_filter.gif" border="0" /></a><br/>' + min + ' - ' + max).fadeIn();
	});

  $.post('../common/data/refining_count.php', filters, function(data){
    $('#attrib-ranges-');
  });
}

/**
 * Sort a table by col in the direction 'dir'.
 */
function doSort(col){
  dir = document.getElementById('frmProductsList').sort_dir.value;
  
  if(dir == "ASC"){
    dir = "DESC";
  }else{
    dir = "ASC";
  }
  
  $('#frmProductsList')[0].sort_col.value = col;
  $('#frmProductsList')[0].sort_dir.value = dir;
  
  filters = $('#frmRefineSearch').serialize();
  sorting = $('#frmProductsList').serialize();
  
  $.post('../common/data/refining_search.php?page=' + $('#frmProductsList')[0].current_page.value, filters + '&' + sorting, function(data){
		$('#comparison-results').html(data);
		$('#search-attributes-list')[0].selectedIndex = 0;
	});
}

function pageResults(num){
  filters = $('#frmRefineSearch').serialize();
  
  if($('#frmProductsList')[0].sort_dir.value.length > 1){
    sorting = $('#frmProductsList').serialize();
  }else{
    sorting = null;
  }
  
  $.post('../common/data/refining_search.php?page=' + num, filters + '&' + sorting, function(data){
    $('#comparison-results').html(data);
		$('#search-attributes-list')[0].selectedIndex = 0;
  });
}

function devSearch(pos){
  filtersCount--;
	$('#attrib-id-' + pos)[0].value = null;
	$('#attrib-name-' + pos)[0].value = null;
	$('#attrib-min-' + pos)[0].value = null;
	$('#attrib-max-' + pos)[0].value = null;
	$('#attrib-ranges-' + pos).fadeIn();
	$('#attrib-chosen-' + pos).fadeOut();

	filters = $('#frmRefineSearch').serialize();
	$.post('../common/data/refining_search.php', filters, function(data){
		$('#comparison-results').html(data);
	});
}

function pickAttribute(){
	attribute_id = $('#search-attributes-list')[0].value;
	filters = $('#frmRefineSearch').serialize();

	$.post('../common/data/search_ranges.php?id=' + attribute_id, filters, function(data){
		$('#attrib-range-block-custom').html(data).fadeIn();
	});
}

//Shows the popup for the terms and conditions for quickquote stuff.
function show_popup(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=700,height=700,left = 710,top = 350');");
}
