
function callRemoteAvailablePieces(style_id)
{
  new Ajax.Updater('availablePieces', '/one_of_a_kind/availablePieces/style_id/' + style_id + '.html', {asynchronous:true, evalScripts:false, onComplete:function(request, json){Element.hide('indicator')}, onLoading:function(request, json){Element.show('indicator')}})

}




var homeMainMarketingSwapGraphic = (function (){
  // private variables
  var currentGraphic = '1';
  var idPrefix = 'homeMainMarketingBoxImg';

  var func = function(e) {
    var elem = Event.element(e);
    elem = elem.up('li');

    var newGraphic = elem.previousSiblings().length + 1;
    if (newGraphic != currentGraphic) {
      Element.hide(idPrefix + currentGraphic);

      currentGraphic = elem.previousSiblings().length + 1;
      id = idPrefix + currentGraphic;

      var currentTab = elem.up().down('.current-tab');
      if (currentTab) currentTab.removeClassName('current-tab');

      new Effect.Appear(id, { duration:1.0 });

      elem.addClassName('current-tab');
    }

    Event.stop(e);
  };

  return func;
})();

function mainNavMenus()
{
	var nodes = ['mainNavDiamondsMenu', 'mainNavFinishedJewelryMenu', 'mainNavClientSupportMenu', 'mainNavAboutGnMenu'];
	nodes.each(function(node) {
		if(node != thisElement)
		{
			Element.hide(node);
		}
		else
		{
			Element.show(node);
		}
	});
}

function changeDiamondResultRowColor(e)
{
  v = Event.element(e);
  if (v.tagName !== 'TR') v = v.up('TR');

  v.toggleClassName('rowSelected');
}

var maBoxBlocked = false;
function showHideMaBox(id)
{
  if (!maBoxBlocked) {
    maBoxBlocked = true;
    var elem = $(id);
    var plusMinus = elem.previous('H3').down('IMG');
    v = elem.style.display;
    if(v == 'none') {
      Effect.BlindDown(id, {duration:.5, afterFinish: function () {maBoxBlocked = false;}} );
      plusMinus.src = '/images/minus.png';
    }else{
      Effect.BlindUp(id, {duration:.5, afterFinish: function () {maBoxBlocked = false;}} );
      plusMinus.src = '/images/plus.png';
    }
  }
}



function changeWidth(id, v1, v2)
{
  gadget = $(id);
  cur_width = gadget.style.width;
  
  if(cur_width == v1) {
    gadget.style.width = v2;
  }else{
    gadget.style.width = v1;
  }
}

function changeDisplay(id)
{
  gadget = $(id);
  cur_display = gadget.style.display;
  
  if(cur_display == 'none')
  {
    Element.show(gadget);
  }
  else
  {
    Element.hide(gadget);
  }
}


function format_currency(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}



function diamond_information(id, x, y)
{
	obj = $(id);
	obj.style.display = 'none';
	obj.style.top = x;
	obj.style.left = y;
}

function copyBillingToShipping()
{
	$('co_shipping_store_name').value 	= $('co_billing_store_name').value;

	$('co_shipping_address').value 		= $('co_billing_address').value;
	$('co_shipping_address2').value 	= $('co_billing_address2').value;
	$('co_shipping_city').value			= $('co_billing_city').value;
	$('co_shipping_state').value		= $('co_billing_state').value;
	$('co_shipping_zipcode').value		= $('co_billing_zipcode').value; 
	$('co_shipping_phone_no').value		= $('co_billing_phone_no').value;
	$('co_shipping_phone_ext').value	= $('co_billing_phone_ext').value;		
}


function changeSelectedJewelryPieceStyle(id, className, color)
{
	// change the color of all the pieces first
	nodes = $$(className);
	nodes.each(function(node) {
		obj1 = $(node);
		obj1.style.backgroundColor = '#FFFFFF';
		obj1.style.fontWeight = 'normal';
	});

	// change the color for the piece being clicked
	obj = $(id);
	obj.style.backgroundColor = color;
	obj.style.fontWeight = 'bold';
}


function convertCriteriaNumbertoValue()
{
	if($('color_value_low').innerHTML >= 1 && $('color_value_low').innerHTML < 2) {
		$('colorLetterValueLow').innerHTML = 'D';
	}
	if($('color_value_high').innerHTML >= 1 && $('color_value_high').innerHTML < 2) {
		$('colorLetterValueHigh').innerHTML = 'D';
	}
	
}

function confirmEntry(url, msg) {
	input_box = confirm(msg);
	if (input_box == true) { 
	  // Output when OK is clicked
	  window.location.href = url;
	}else{
      
	}
}


function updateDsResults()
{
  var holder = $('resultsHolder');
  text = holder.innerHTML;
  
  feed = text.split('<!--#-->');
  
  upgrade     = feed[0];
  regular     = feed[1];
  downgrade   = feed[2];

  
  //Element.update('upgradeResults', upgrade);
  Element.update('diamondSearchResults', regular);
  //listenForSortOnResults();
  //Element.update('downgradeResults', downgrade);
  Element.hide('indicator');
}


function finishedJewelrySearchByFormRequest(request, json) {
	var response = request.responseText.evalJSON();
	if(response['msg'] == 'failure') {
	  Element.update('searchByNoResultsMessage', 'That style does not exist');
	} else {
	  // forward to URL
	  url = '/finished_jewelry/show/style/' + response['style'] + '.html';
	  window.location.href = url;
	}
}


function diamondsSearchByFormRequest(request, json) {
	var response = request.responseText.evalJSON();
	if(response['msg'] == 'failure') {
	  Element.update('searchByNoResultsMessage', 'That diamond does not exist');
	} else {
	  // forward to URL
	  url = '/diamonds/show/stock_num/' + response['stock_num'] + '.html';
	  window.location.href = url;
	}
}

function resetForm(myForm) {
	var elem = $(myForm);
	elem.reset();
}

function finalizeEmailDiamond(request, json) {
	var id 			= $('emailDiamondToFriend');
	var form 		= $('emailDiamondForm');
	var feedback	= $('emailFeedback');
	var error_msg 	= 'Please make sure the From and To emails are valid'
	var success_msg = 'The email has been sent';
	var response 	= request.responseText.evalJSON();

	Element.hide('emailDiamondIndicator')

	if(response['msg'] == 'failure') {
	  Element.update('error_msg', error_msg);
	} else {
	  alert(success_msg);
	  resetForm(form.id);
	  Element.update('error_msg', '');
	  new Effect.BlindUp('emailDiamondToFriend', {duration:0.2});; return false;
	}	
}


