var computer_serial;
var computer_offset = 0;

var light_border = '2px solid #fff';
var dark_border = '2px solid #404040';

function editCompLic(_button, _customer_id, _computer_serial) {
  computer_serial = _computer_serial;

  $('confirm').show();

  $$('input.licenseSelector').each(function(_box) { 
    _box.style.display = 'none'; 
  });

  $$('input.licenseSelector_'+_computer_serial).each(function(_box) { 
    _box.style.display = 'block'; 
  });
}

function editCompDesc(customer_id, serial) {
  var box = $(serial + '_desc');

  box.style.borderTop    = '2px solid #404040';
  box.style.borderLeft   = '2px solid #404040';
  box.style.borderRight  = '1px solid #D4D0C8';
  box.style.borderBottom = '1px solid #D4D0C8';

  box.readOnly = false;

  var link = $(serial + '_desc_lnk');

  link.innerHTML = 'Apply';
  link.href = "javascript:applyCompDesc("+customer_id+", '"+serial+"')";
}

function applyCompDesc(customer_id, serial) {
  var box = $(serial + '_desc');

  box.style.border = '0';
  
  var link = $(serial + '_desc_lnk');

  new Ajax.Request(
    'includes/server/registerAjax.php?func=setCompDesc',
    {
      parameters: {
        customer_id: customer_id,
        computer_serial: serial,
        computer_description: box.value
      }
    }
  );

  link.innerHTML = 'Edit';
  link.href = "javascript:editCompDesc("+customer_id+", '"+serial+"')";
}

function resetLic(customer_id) {
  var url = 'includes/server/registerAjax.php?func=setLic&customer_id='+customer_id;
  new Ajax.Request(url);

  $('confirm').hide();

  $$('input.licenseSelector').each(function(_box) { 
    _box.style.display = 'none'; 
  });

  $$('.computerSelector').each(function(button) {
    button.checked = false;
  });
}

function setLic(customer_id) {
  if(computer_serial) {
    var url = 'includes/server/registerAjax.php?func=setLic&customer_id='+customer_id+'&computer_serial='+computer_serial;

    var features = $$('input.features.licenseSelector_'+computer_serial).select(function(_box) {
      return _box.checked;
    });

    var patterns = $$('input.patterns.licenseSelector_'+computer_serial).select(function(_box) {
      return _box.checked;
    });

    var needs_comma = false;
    features.each(function(_box) {
      if(needs_comma) url += ',';
      else {
        url += '&features=';
        needs_comma = true;
      }

      url += _box.value;
    });

    needs_comma = false;
    patterns.each(function(_box) {
      if(needs_comma) url += ',';
      else {
        url += '&patterns=';
        needs_comma = true;
      }

      url += _box.value;
    });

    if(features.length > 0 || patterns.length > 0) {
      new Ajax.Request(url);
    }
  }
}

function makeCheckBox(computer_serial, type, item_id) {
  var box = document.createElement('input');

  box.type = 'checkbox';
  box.className = 'licenseSelector licenseSelector_'+computer_serial+' '+type; 
  box.value = item_id;
  box.style.display = 'none';
  box.style.cssFloat = 'right';
  box.style.styleFloat = 'right';
  box.title = 'Add License';

  return box;
}

setLic_callback = function(_info, _available) {

  $$('.licenseSelector').each(function(box) {
    box.parentNode.removeChild(box);
  });

  $$('.computerSelector').each(function(button) {
    button.style.display = 'none';
    button.checked = false;
  });

  $$('.available').each(function(el) {
    el.innerHTML = '0';
  });

  var computers = $H(_info);
  computers.each(function(pair) {
    var computer_serial = pair.key;

    var licenses = $H(pair.value);

    licenses.each(function(pair) {
      var type = pair.key;
      var items = $H(pair.value);

      items.each(function(pair) {
        var item_id = pair.key;
        var item_info = pair.value;

        var code = item_info['code'];
        var message = item_info['message'];
        var remaining = (item_info['available'] != null) ? item_info['available'] : 0;
        $(type + '_' + item_id + '_available').innerHTML = remaining;

        var cell = $('cell_' + type + '_' + computer_serial + '_' + item_id);

        if(code != null) {
          if(code.length > 18)
            $(computer_serial+'_'+type+'_'+item_id+'_lic').innerHTML = 'Licensed';
          else
            $(computer_serial+'_'+type+'_'+item_id+'_lic').innerHTML = code;
        }
        else {
          if(message != null) {
            cell.appendChild(document.createTextNode(message));
          }
          if(remaining > 0) {
            $(computer_serial+'_radio').style.display = 'block';

            var box = makeCheckBox(computer_serial, type, item_id);
            cell.appendChild(box);
            cell.innerHTML += '&nbsp;';
          }
        }
      });
    });
  });
}
function checkPw(customer_id) {
  new Ajax.Request(
    'includes/server/registerAjax.php?func=checkPw',
    {
      parameters: {
        customer_id: customer_id,
        password: $('password').value
      }
    }
  );
}

function checkPw_callback(auth, customer_id) {
  if(auth) {
    setLic(customer_id);
    $('pwd_msg').innerHTML = '';
  }
  else {
    $('pwd_msg').innerHTML = 'Incorrect password!';
  }
}

function hideCol(serial) {
  $$('.cell_'+serial).each(function(cell) {
    cell.style.display = 'none';
  });
}

function showCol(serial) {
  $$('.cell_'+serial).each(function(cell) {
    //IE doesn't support table-cell
    if (navigator.appVersion.indexOf("MSIE")!=-1)
      cell.style.display = 'block';
    else
      cell.style.display = 'table-cell';
  });
}

function updatePageInfo(start, end, total) {
  $('pageInfo').innerHTML = '('+start+' to '+end+' of '+total+')';
}

function pageFwd(per_page) {
  if(computer_offset < computers.length - per_page) {
    hideCol(computers[computer_offset]);
    showCol(computers[computer_offset + per_page]);

    updatePageInfo(computer_offset+2,computer_offset+per_page+1,computers.length); 

    computer_offset += 1;
  }
}

function pageBack(per_page) {
  if(computer_offset > 0) {
    computer_offset -= 1;

    showCol(computers[computer_offset]);
    hideCol(computers[computer_offset + per_page]);
    updatePageInfo(computer_offset+1,computer_offset+per_page,computers.length); 
  }
}

function statesUpdate(name)
{
	var page = $('regform').action;
	var url  = page+'?act=updateStates&country_id='+$(name).value
	new Ajax.Request(url,
		{
			method:'get',
			onSuccess: function(transport){
				var response = transport.responseText || "no response text";
				$('state').replace(response);
			},
			onFailure: function(){
				alert('Server did not respond...try again...')
			}
		}
	);
}

function actComp(customer_id, serial, computer_id) {
  new Ajax.Request(
    'includes/server/registerAjax.php?func=actComp',
    {
      parameters: {
        customer_id: customer_id,
        computer_serial: serial
      },
      onSuccess: function() {
        resetLic(customer_id);
      }
    }
  );

  $$('.cell_'+serial).each(function(cell) {
    cell.style.backgroundColor = 'white';
  });

  $(serial + '_act').style.display = 'none';
  $(serial + '_deact').style.display = 'block';

  $(serial + '_radio').style.display = 'block';

  $$('.licenseSelector_'+serial).each(function(box) {
    box.style.display = 'block';
  });

  $$(".cell_"+serial+" a.itemAct").each(function(link) {
    link.style.display = 'none';
  });

  $$(".cell_"+serial+" a.itemDeact").each(function(link) {
    link.style.display = 'block';
  });

}

function deactComp(customer_id, serial, computer_id) {
  new Ajax.Request(
    'includes/server/registerAjax.php?func=deactComp',
    {
      parameters: {
        customer_id: customer_id,
        computer_serial: serial
      },
      onSuccess: function() {
        resetLic(customer_id);
      }
    }
  );
  $$('.cell_'+serial).each(function(cell) {
    cell.style.backgroundColor = '#caa';
  });

  $(serial + '_act').style.display = 'block';
  $(serial + '_deact').style.display = 'none';

  $(serial + '_radio').style.display = 'none';

  $$('.licenseSelector_'+serial).each(function(box) {
    box.style.display = 'none';
    box.checked = false;
  });

  $$(".cell_"+serial+" a.itemAct").each(function(link) {
    link.style.display = 'none';
  });

  $$(".cell_"+serial+" a.itemDeact").each(function(link) {
    link.style.display = 'none';
  });
}

function deactLic(customer_id, computer_id, license_code, cell_id) {
  new Ajax.Request(
    'includes/server/registerAjax.php?func=deactLic',
    {
      parameters: {
        computer_id: computer_id,
        license_code: license_code
      },
      onSuccess: function() {
        resetLic(customer_id);
      }
    }
  );

  $(cell_id).style.backgroundColor = '#caa';

  $(computer_id + '_' + license_code + '_act').style.display = 'block';
  $(computer_id + '_' + license_code + '_deact').style.display = 'none';
}

function actLic(customer_id, computer_id, license_code, cell_id) {
  new Ajax.Request(
    'includes/server/registerAjax.php?func=actLic',
    {
      parameters: {
        computer_id: computer_id,
        license_code: license_code
      },
      onSuccess: function() {
        resetLic(customer_id);
      }
    }
  );

  $(cell_id).style.backgroundColor = '#fff';

  $(computer_id + '_' + license_code + '_act').style.display = 'none';
  $(computer_id + '_' + license_code + '_deact').style.display = 'block';
}

function sendLicenseEmail(customerId)
{
	$('licMailStatus').innerHTML = "Sending...";
	$('licMailStatus').className = "statusWorking";
	new Ajax.Request('includes/server/registerAjax.php?func=mailLic&customer_id='+customerId);
}

