function loginMember() {

    new Ajax.Request(url + '/members/login', {
      method: 'post',
      requestHeaders: {Accept: 'application/json'},
      postBody: 'login_email=' + $F('login_email') + '&login_pass=' + MD5($F('login_pass')),
      onSuccess: function(res) {
          if(res.responseText[0] != '!') {
              if($('checkout_text')) {
                  window.location.reload();
              } else {
                  $('my_toffmusic').update(res.responseText);
                  new Ajax.Request(url + '/product/wishlist', {
                      method: 'post',
                      requestHeaders: {Accept: 'application/json'},
                      onSuccess: function(res) {
                          $('wishlist').update(res.responseText);
                          if($('wishlist_contents').visible() == false) {
                              wishlistToggle();
                          }
                      }
                  });
              }
          } else {
              $('my_toffmusic').update((res.responseText).substring(1));
          }
      }
    });
    return false;
}

function logoutMember() {

    new Ajax.Request(url + '/members/logout', {
      method: 'post',
      onSuccess: function(res) {
          if($('checkout_text')) {
              window.location.reload();
          } else {
            $('my_toffmusic').update(res.responseText);
            new Ajax.Request(url + '/product/wishlist', {
                  method: 'post',
                  requestHeaders: {Accept: 'application/json'},
                  onSuccess: function(res) {
                      $('wishlist').update(res.responseText);
                  }
              })
          }
      }
    });
    return false;
}

function subscribeMailinglist() {
	new Ajax.Request(url + '/mailinglist/subscribe', {
	  method: 'post',
	  requestHeaders: {Accept: 'application/json'},
	  postBody: 'mailinglist_email=' + $F('mailinglist_email'),
	  onSuccess: function(res) {
	    if (res.responseText == 'true'){
	      $('newsletterResponse').update('You\'re now subscribed to the newsletter!').className = 'green';
	    }else{
	      $('mailinglist_email').select();
	      $('newsletterResponse').update(Object.values(res.responseText.evalJSON(true).mailinglist_email)[0]).className = 'red';
		}
	  }
	});
	return false;
}

function musicMoreDetails(id) {

    if(currentPlayerId){
        $('player_' + currentPlayerId + '_container').hide();
        currentPlayerId = null;
    }

    record = 'record_' + id;
    extendedRecord = 'record_' + id + '_extension';

    if($(extendedRecord).innerHTML!='') {
        Element.hide(record);
        Element.show(extendedRecord);
    } else {
        Element.show('loader_' + id);
        new Ajax.Request(url + '/music/more-details', {
          method: 'post',
          postBody: 'id=' + id,
          onSuccess: function(res) {
              Element.hide('loader_' + id);
              $(extendedRecord).update(res.responseText);
              Element.hide(record);
              Element.show(extendedRecord);
          }
        });
    } 
}

function musicLessDetails(id) {
    
    if(currentPlayerId){
        $('player_' + currentPlayerId + '_container').hide();
        currentPlayerId = null;
    }

    record = 'record_' + id;
    extendedRecord = 'record_' + id + '_extension';    
    Element.hide(extendedRecord);
    Element.show(record);
}

function addToShoppingCart(product_id, from, directCheckout) {
    if($('shopping_cart_summary_contents').visible() == false) {
        shoppingCartToggle();
    }
    if(from != 'list') {
        $(from + '_ajax_loader_' + product_id).show();
    }
    
    optionValues = '';
    $$('#record_' + product_id + ' select').each(function(s) {
	  optionValues += ',' + $F(s);
	});
    
    new Ajax.Request(url + '/product/add-to-shopping-cart', {
          method: 'post',
          postBody: 'id=' + product_id + '&optionValues=' + optionValues,
          onSuccess: function(res) {
    		  if(res.responseText == 'no_stock') {
    			  alert('The amount you are trying to order is not in stock at the moment!');
    		  } else {
	              $('shopping_cart_summary').update(res.responseText);
	              
	              if(from == 'shopping_cart') {
	                  updateShoppingCart(product_id, from); 
	              } else {
	                  $('shopping_cart_summary_product_' + product_id).highlight({startcolor:'#0884b4', endcolor:'#36363c', restorecolor:'#36363c'});
	              }
	              if(directCheckout == true) {
	                  document.location = url + '/product/checkout';
	              } else {
	            	  if(from != 'shopping_cart') {
	            		  alert('You\'ve added this product to your shopping cart!');
	            	  }
	              }
    		  }
    		  if(from != 'list') {
                  $(from +'_ajax_loader_' + product_id).hide();
              }
          }
        });
}

function subtractFromShoppingCart(product_id, from) {
    $(from + '_ajax_loader_' + product_id).show();
    new Ajax.Request(url + '/product/substract-from-shopping-cart', {
          method: 'post',
          postBody: 'id=' + product_id,
          onSuccess: function(res) {
              $('shopping_cart_summary').update(res.responseText);
              $(from +'_ajax_loader_' + product_id).hide();
              if(from == 'shopping_cart') {
                  updateShoppingCart(product_id, from); 
              } else {
                  $('shopping_cart_summary_product_' + product_id).highlight({startcolor:'#0884b4', endcolor:'#36363c', restorecolor:'#36363c'});
              }
          }
        });
}

function updateShoppingCart(product_id, from)
{
    $(from + '_ajax_loader_' + product_id).show();
    new Ajax.Updater('shopping_cart_table', url + '/product/update-shopping-cart', {
        onComplete:function() {
            $(from + '_ajax_loader_' + product_id).hide();
        }
    });
}

function addToWishlist(product_id, from) {
	if($('wishlist_product_'+product_id)) {
		alert("This product is already in the wishlist.");
		return;
	}
    if($('wishlist_contents').visible() == false) {
        wishlistToggle();
    }
    if(from != 'list') {
        $(from + '_ajax_loader_' + product_id).show();
	}
	
    optionValues = '';
    $$('#record_' + product_id + ' select').each(function(s) {
	  optionValues += ',' + $F(s);
	});
    
    new Ajax.Request(url + '/product/add-to-wishlist', {
          method: 'post',
		  postBody: 'id=' + product_id + '&optionValues=' + optionValues,          
          onSuccess: function(res) {
              if(from != 'list') {
                  $(from + '_ajax_loader_' + product_id).hide();
              }
              if(res.responseText == 'false') {
                  alert('Please login!');
              } else {
                  $('wishlist').update(res.responseText);
                  $('wishlist_product_' + product_id).highlight({startcolor:'#0884b4', endcolor:'#36363c', restorecolor:'#36363c'});
              }
          }
        });
}

function removeFromShoppingCart(wishlist_id) {
    new Ajax.Request(url + '/product/remove-from-shopping-cart', {
          method: 'post',
          postBody: 'id=' + wishlist_id,
          onSuccess: function(res) {
              $('shopping_cart_summary').update(res.responseText);
              if($('shopping_cart_table')) {
                  new Ajax.Updater('shopping_cart_table', url + '/product/update-shopping-cart');
              }
          }
        });
}

function directCheckout(product_id, from) {
    addToShoppingCart(product_id, from, true);
}

function removeFromWishlist(product_id) {
    new Ajax.Request(url + '/product/remove-from-wishlist', {
          method: 'post',
          postBody: 'id=' + product_id,
          onSuccess: function(res) {
              $('wishlist').update(res.responseText);
          }
        });
}

function shoppingCartToggle() {
    new Ajax.Request(url + '/product/toggle-shopping-cart-summary', {
          method: 'post',
          postBody: 'hide=' + $('shopping_cart_summary_contents').visible(),
          onSuccess: function(res) {
              $('shopping_cart_summary_contents').toggle();
          }
        });
}

function wishlistToggle() {
    new Ajax.Request(url + '/product/toggle-wishlist', {
          method: 'post',
          postBody: 'hide=' + $('wishlist_contents').visible(),
          onSuccess: function(res) {
              $('wishlist_contents').toggle();
          }
        });
}

function sendEvent(swf,typ,prm) {
  thisMovie(swf).sendEvent(typ,prm);
};
function getUpdate(typ,pr1,pr2,swf) {};
function thisMovie(swf) {
  if(navigator.appName.indexOf("Microsoft") != -1) {
    return window[swf];
  } else {
    return document[swf];
  }
};

function playTrack(playerId, trackIndex) {
    
    if(currentPlayerId && currentPlayerId != playerId){
        sendEvent('player_' + currentPlayerId, 'stop');
        $('player_' + currentPlayerId + '_container').hide();
    }
    if($('player_' + playerId + '_container').visible()) {
        sendEvent('player_' + playerId, 'playitem', trackIndex);
    }
    else {
        $('player_' + playerId + '_container').show();
        currentPlayerId = playerId; 
        window.setTimeout('playTrack(\''+playerId+'\', '+trackIndex+')', 2000);
    }
}

function checkStock(product_id, option_type_id) {

	var optionValues = '';
	
	$$('#record_' + product_id + ' select').each(function(s) {
	  optionValues += ',' + $F(s);
	});

	new Ajax.Request(url + '/product/check-stock', {
        method: 'post',
        postBody: 'product_id=' + product_id + '&optionValues=' + optionValues ,
        onSuccess: function(res) {
            $('stock' + product_id).replace(res.responseText);
        }
    });
}