function makePostUrl(isSandbox, mid) {
  var path = "/api/checkout/v2/checkoutForm/";
  var url = isSandbox ?
    "https://sandbox.google.com/checkout" + path + "storegadgetwizardsandbox-1063" :
    "https://checkout.google.com" + path + "storegadgetwizardproduction-1039";

  return url + "/" + mid + "?integration=store-gadget-wizard";
}
 
function doctest() {
var SS_URL = "http://spreadsheets.google.com/tq?key=0ArNMycobpXr3ckJybUNHVDZ0cEU0SjZvb0prVDhGS2c";
$.ss(SS_URL)
  .setQuery("select * where B like 'test%'")
  .setField("time,title,address,foo,bar,baz,hoge,piyo")
  .send(function(success){
    if(!success) return;
    this.each(function(i){
      console.log(i,this);
     })
  });
} 
 
function toggleDisplay(id) {
  var el = document.getElementById(id);
  setDisplay(el, el.style.display == 'none');
}
 
function setDisplay(el, display) {
  el.style.display = display ? '' : 'none';
}

function showImage(url, title, thumb) {
  var image = new Image();
  image.src = url;
  var imagethumb = new Image();
  imagethumb.src = thumb;
  ClickedImage=url;

  var widest=1024;
  var tallest=768;
  var aspectratio = imagethumb.height/imagethumb.width;
  tallest-=130; //smaller yet to include room for the window bar
  widest-=25; //smaller for scrollbar on right (if applicable)
  
	if (largeimageheight>1) {
		if (largeimageheight>tallest) {var height=tallest;} else {var height=largeimageheight;}
		var width=Math.floor(height/aspectratio);
		if (width>widest) {var tempwidth=widest; var tempheight=Math.floor(tempwidth*aspectratio);}
	}
	if (largeimagewidth>1) {
		if (largeimagewidth>widest) {var width=widest;} else {var width=largeimagewidth;}
		var height=Math.floor(width*aspectratio);
		if (height>tallest) {var tempheight=tallest; var tempwidth=Math.floor(tempheight/aspectratio);}
	} 
	if (tempheight>1) height=tempheight;
	if (tempwidth>1) width=tempwidth;

  var winSize = ' height=' + (height + 25) + ', width=' + (width + 25);
  var imgSize = ' height=' + height + ' width=' + width;

  var config = 'toolbar=0, menubar=0, scrollbars=0, resizable=1, ' +
      'location=0, directories=0, status=0,' + winSize;
 
  var win = window.open('', 'imagepreview', config);
 
  win.document.write('<!DOCTYPE HTML><html><body '+style+'>' +
    '<img id="large-image" style="margin:5px; display: block;" border="0" ' +
    imgSize + '"onblur="closeWin()"/></body></html>'); //"
  win.document.title = title;
  win.document.close();
  win.document.getElementById('large-image').src = url;
  win.focus();
}

function closeWin()
{win.close();}
 
/**
 * regexp to parse spreadsheet url
 * looks for list/spkey/worksheetkey/public/
 */
var PRODUCT_ID_RE = /list\/([^\/]+)\/([^\/]+)\/public/;

function getSpreadsheetKey(id) {
  var matches = PRODUCT_ID_RE.exec(id);
  return '<spreadsheet-key>' + (matches ? matches[1] + '/' + matches[2] : id) +
      '</spreadsheet-key>';
}

function setSelected(option, index, flag) {
  if (flag) {
    setTimeout(function() {option.parentNode.selectedIndex = index;}, 0);
  }
}
 
function renderGroup(options) {
  return renderGroup_(options.productElement, options.groups, options.group,
      options.optionControls);
}
 
function makeRenderOptions(productElement, groups, group, optionControls) {
  return ({
    productElement: productElement,
    groups: groups,
    group: group,
    optionControls: optionControls
  });
}
 
/**
 * re-renders a group of product variants.
 *
 * @param productElement - DOM element to be redrawn. Will render the element
 * so that jsselect="groups" works as expected.
 * @param groups - original array used to render all products
 * @param group - all variants for the current product
 * @param optionControls - links to all product variant controls
 */
function renderGroup_(productElement, groups, group, optionControls) {
  // if we have a single variant product, don't redraw so all user
  // selections on options are preserved
  if (group.rows.length == 1) {
    return;
  }
 
  var variantIndex = mashuphelper.findMapIndex(group.rows,
  makeControlsMap(optionControls));

  if (variantIndex < 0) {
    setInStock(productElement, false);
    return;
 }
 
  // set up wich variant in the group to be displayed
 group.variantIndex = variantIndex;
 
  // using jstemplate to redraw a nested template element that has
  // jsselect attributes, requires an object with the original array
  // accessed by jsselect-ed name, thus {groups: [...]}
  mashuphelper.renderTemplate({groups: groups}, productElement);
}
 
function makeControlsMap(optionControls) {
  var results = {};
  for(var i = 0; i < optionControls.length; i++) {
    var select = optionControls[i]
    results[select.optionColumn] =
      select.options[select.selectedIndex].value;
  }
  return results;
}
	 
function getOptionValues(group, optionColumn) {
  if (group.rows.length == 1) {
    return (group.rows[0][optionColumn] || '').split(',');
  } else {
    return mashuphelper.getUniqueValues(group.rows, optionColumn);
  }
}
 
function setInStock(element, inStock) {
  if (inStock) {
    mashuphelper.classes.remove(element, 'out-of-stock');
  } else {
    mashuphelper.classes.add(element, 'out-of-stock');
  }
}
 
function get(map, key, opt_default) {
  if (opt_default === undefined) {
    opt_default = '';
  }
  return map[key] || map[key.replace(/_/g,'')] || opt_default;
}
 
function onLoad() {
  document.getElementById('search-control').value = '';
}
