
// check
function checkCart(f) {
  var check = document.forms[f].elements['accept'].checked;
  if(!check) {
    Ext.get('error').update('Sie müssen die AGBs bestätigen.');
    return false;
  }
  Ext.get('error').update('');
  return true;
}


// show invoice
function showInvoice(url) {
  var invoice =  window.open (url, "invoice", "menubar=yes,status=yes,location=yes,width=720,height=800,toolbar=yes,scrollbars=yes,resizable=yes");
  invoice.focus();
} 

// load products by brand
function loadProductsByBrand(id, p) {
  var content = Ext.get('content');
  // get code
  Ext.Ajax.request({
    url: 'plugin/shop/products',
    method: 'post',
    success: function(o, s, r) {
      content.update(o.responseText);
    },
    failure: function() {
      alert('Es ist ein Fehler aufgetreten!');
    },
    params: {brand: id, p: p || 1}
  });
  return false;
} 


// load products
function loadProducts(id, p) {
  var content = Ext.get('content');
  // get code
  Ext.Ajax.request({
    url: 'plugin/shop/products',
    method: 'post',
    success: function(o, s, r) {
      content.update(o.responseText);
    },
    failure: function() {
      alert('Es ist ein Fehler aufgetreten!');
    },
    params: {id: id, p: p || 1}
  });
  return false;
} 

// buy product
function addToCart(id, count) {
  var content = Ext.get('content');
  // get code
  Ext.Ajax.request({
    url: 'plugin/cart/add-item',
    method: 'post',
    success: function(response, opts) {
      var cont = Ext.get('cart-info');
      cont.update(response.responseText);
    },
    failure: function() {
      alert('Es ist ein Fehler aufgetreten!');
    },
    params: {id: id, count: count}
  });
  return false;
} 


// buy product
function addToWishlist(id) {
  // get code
  Ext.Ajax.request({
    url: 'plugin/wishlist/add',
    method: 'post',
    success: function(o, s) {
      var el = Ext.get('top').child('.wishlist a');
      var r  = Ext.decode(o.responseText);
      el.update('Meine Wunschliste ('+r.count+')');
    },
    failure: function() {
      alert('Es ist ein Fehler aufgetreten!');
    },
    params: {id: id}
  });
  return false;
}


// show content
function showForm(type, name) {
  // show shader
  var shader  = Ext.get('shader');
  var height  = getWindowHeight();
  shader.setHeight(height).show();
  shader.on('click', hideContent, this, {
    single: true,
    delay: 100
  });
  // show content
  var content = Ext.get('shader-content');
  // get code
  Ext.Ajax.request({
    url: 'plugin/'+type+'/'+name,
    method: 'get',
    success: function(o, s, r) {
      content.update(o.responseText);
      content.show();
      //console.log(content.getWidth());
    },
    failure: function() {
      hideContent();
      alert('Es ist ein Fehler aufgetreten!');
    },
    params: {id: id}
  });
} 

// post form
function postForm(form) {
  var f = Ext.get(form);
  // show content
  var content = Ext.get('shader-content');
  // get code
  Ext.Ajax.request({
    url: f.getAttribute('action'),
    success: function(o, s, r) {
      content.update(o.responseText);
    },
    failure: function() {
      alert('Es ist ein Fehler aufgetreten!');
    },
    form: f
  });
} 

// show content
function showContent(id) {
  // show shader
  var shader  = Ext.get('shader');
  var height  = getWindowHeight();
  shader.setHeight(height).show();
  shader.on('click', hideContent, this, {
    single: true,
    delay: 100
  });
  // show content
  var content = Ext.get('shader-content');
  // get code
  Ext.Ajax.request({
    url: 'plugin/suendhaft/content',
    success: function(o, s, r) {
      content.update(o.responseText);
      content.show();
      //console.log(content.getWidth());
    },
    failure: function() {
      hideContent();
      alert('Es ist ein Fehler aufgetreten!');
    },
    params: {id: id}
  });
} 

function hideContent() {
  var shader = Ext.get('shader');
  shader.hide();
  var content = Ext.get('shader-content');
  content.update('');
  content.hide();
} 


function loadModels(m) {
  Ext.Ajax.request({
    params: {m: m, action: 'getModels'},
    url: 'search.agent.php',
    success: function(response, opts) {
      var cont = Ext.get('models-container');
      cont.update(response.responseText);
    },
    failure: function(response, opts) {
      console.log('server-side failure with status code ' + response.status);
    }
  });
}

// load price info
function priceInfo(id) {
  Ext.Ajax.request({
    params: {id: id},
    url: 'extension/catalog/price-info',
    success: function(response, opts) {
      var cont = Ext.get('product-price-info');
      cont.update(response.responseText);
    },
    failure: function(response, opts) {
      alert('server-side failure with status code ' + response.status);
    }
  });
}


function openWindow(name, url, breite, hoehe, menue, status, urlbar, tool, scroll, resize, left, top) {
	var name =  window.open (url, "name", "menubar="+menue+",status="+status+",location="+urlbar+",width="+breite+",height="+hoehe+",toolbar="+tool+",scrollbars="+scroll+",resizable="+resize+",left="+left+",top="+top+"");
  name.focus();
}
  
function gotoUrl(url) {
  document.location.href = url;
}

function bookmark() {
  if(navigator.appName == 'Microsoft Internet Explorer')
    window.external.AddFavorite('http://www.gute-rate.de','gute-rate.de');
  else if(navigator.appCodeName == "Mozilla")
    window.sidebar.addPanel('gute-rate.de','http://www.gute-rate.de','');
 	else if(window.opera && window.print)
		return true;
  else
    alert('wrong browser');
}

function printview() {
  window.print();
}

function submit(f) {
  document.forms[f].submit();
}

// get window height
function getWindowHeight() {  
  var yScroll;
  if (window.innerHeight && window.scrollMaxY) {
    yScroll = window.innerHeight + window.scrollMaxY + 1;
  } else if (document.body.scrollHeight > document.body.offsetHeight){
    yScroll = document.body.scrollHeight;
  } else {
    yScroll = document.getElementsByTagName("html").item(0).offsetHeight;
    yScroll = (yScroll < document.body.offsetHeight) ? document.body.offsetHeight : yScroll;
  }
  return yScroll;
}
