function randomizer(min, max) {
  if (min != null) {
    var min = min;
  }
  else {
    var min = 0;
  }
  if (max != null) {
    var max = max;
  }
  else {
    var max = 10;
  } 
  return min + Math.floor((max - min + 1) * Math.random());
}

function selectWatch () {
  // First, grab current select option and toggle appropriately
  toggleOffice($("#os option:selected").attr("value"));
  
  $("#os").bind(($.browser.msie ? "click" : "change"), function() {
    toggleOffice($("#os option:selected").attr("value"));
  });
}

function toggleOffice(value) {
  // First, clear office select list
  $("#office > option").remove();

  // Array of office options based on OS
  var office = { 
    open:"Open Office",
    std:"Office Std",
    pro:"Office Pro"
  };
  var office_os = { 
    lin:{open:office['open']},
    win:{open:office['open'], std:office['std'], pro:office['pro']}
  };
  var optArray = buildOptArray(office_os[value]);
  $.each(optArray, function(v,k) {
    $("#office").append(k);
  });
}

function buildOptArray(hashArray) {
  var optArray = new Array();
  var i = 1;
  optArray[0] = '<option value="none">None</option>';
  $.each(hashArray, function(k,v){
    optArray[i] = '<option value="' + k + '">' + v + '</option>';
    i=i+1;
  });
  return optArray;
}

function calcWatch(selector, url) {
  $(selector).bind(($.browser.msie ? "click" : "change"), function(){ // ie doesn't support onchange event here
    $.post(url, $(selector).serialize(), function(data){$("#estimate").text(data);})
  });
}

function estimate(selector) {
  $.post("/pricing/estimate", $(selector).serialize(), function(data){$("#estimate").text(data);})
}

/*
| tabArray should be defined as { trigger:container, etc:etc}
| where trigger is the tab, container is the body of the tab that is displayed
| callback fires on tab switch
*/
function tabSwitcher(tabArray, callback) {
  this.tabs = tabArray;

  this.makeClickable = function(trigger, container, instance) {
    $("#" + trigger).click(function(e){
      instance.switchTab(trigger, container);
      return false;
    });
  }
  this.switchTab = function(trigger, container) {
    this.unsetTabs();
    $("#" + trigger).addClass("current");
    $("#" + container).show();
    try {
      callback();
    }
    catch(e) { /* no callback specified */}
  }
  this.unsetTabs = function(tabs) {
    try {
      $.each(this.tabs, function(i, val) {
        $("#" + val).hide();
        if($("#" + i).hasClass("current")) {
          $("#" + i).toggleClass("current");
        }
      });
    }
    catch(e) { /* error */ }
  }
}

function verifyFormat(id, type) {}

function copyInputs() {
  var inputArray = $("#tab1_area input");
  inputArray.push($("#tab2_area input, #tab2_area select"));
  var html = '<legend>Review your information</legend>';
  $.each(inputArray, function(i) {
    html += '<div>' + $(inputArray[i]).attr("name") + ': ' + $(inputArray[i]).val() +'</div>';
  });
  html += '<input type="button" value="calculate" class="submit"  id="tco_calc" />';
  $("#tab3_area").html(html);
}

function tabButton(id, trigger, container, tabInstance) {
  $("#" + id).bind("click", function(e){
    tabInstance.switchTab(trigger, container);
    $("#" + trigger).show();
    return false;
  });
}
