var ajax_estimate_url = '/ajax/ajax_estimate_list.php';
var estimate_count_id = 'estimate_count';
var estimate_link_id = 'estimate_link';

// Add or Del estimate list
function toggleEstimate( obj, id )
{
	var mode = '';
	if ( obj.checked ) {
	  mode = 'add';
	} else {
	  mode = 'del';
	}
	new Ajax.Request( ajax_estimate_url,
  {
    method:'get',
		parameters: {"id":id,"mode":mode},
    onSuccess: affectToggleEstimate,
    onFailure: function(){ alert('通信が成功しませんでした。'); return false; }
  });
	return true;
}
// Del all estimate list
function clearEstimate()
{
	var mode = 'delall';
	new Ajax.Request( ajax_estimate_url,
  {
    method:'get',
		parameters: {"mode":mode},
    onSuccess: affectToggleEstimate,
    onFailure: function(){ alert('通信が成功しませんでした。'); return false; }
  });
	return true;
}
// Add or Del estimate list
function affectToggleEstimate( transport )
{
	var response = transport.responseText;
	var cmd;
	var check_obj;
	var res;
	var changed_prefecture = false;
	if ( response ) {
	  cmd = 'res = ' + response + ';';
		eval( cmd );
	}
	if ( res.result ) {
		alert( res.MESS );
		document.getElementById( estimate_count_id ).innerHTML = parseInt( res.companies );
		if ( parseInt( res.companies ) > 0 ) {
			document.getElementById( estimate_link_id ).style.display = 'block';
			document.getElementById( estimate_link_id+'_none' ).style.display = 'none';
		} else {
			document.getElementById( estimate_link_id ).style.display = 'none';
			document.getElementById( estimate_link_id+'_none' ).style.display = 'block';
		}
		if ( res.mode == 'delall' ) {
		  for ( var i=0; i<ids.length; i++ ) {
			  check_obj = document.getElementById( 'estimate' + ids[i] );
				if ( check_obj ) {
					check_obj.checked = false;
				}
			}
		}
		return true;
	} else {
		alert( res.MESS );
		if ( res.id ) {
			if ( res.mode == 'add' ) {
			  document.getElementById( 'estimate' + res.id ).checked = false;
			} else if ( res.mode == 'del' ) {
			  document.getElementById( 'estimate' + res.id ).checked = true;
			}
		}
		return false;
	}
}

