function Shop_getFrameSetObj() {
  var obj = this.Fwin;
  var i=0;
  while (i<10) { if (!obj.GLOBAL_MAINWINDOW) { obj=obj.parent } i++ }
  return obj;
}

function Shop_notActivate() {
  alert("Dieser Shop befindet sich noch im Aufbau.\nWir versuchen ihn so schnell wie möglich Online zu stellen");
}

function Shop_add(obj) {
  
//  this.notActivate();
  
//  /*
  var form = this.getFormularObj();
  var articleID = form.name;
  var quantity = (form.quantity.value);
	quantity++;
	
	this.connectToServer(articleID,'add','shopFrame');
	var i=0;
	while(i!= 100000){i++}
	
	this.setQuantity(articleID,quantity);
  form.quantity.value=quantity;
//  */
}
function Shop_del(obj) {
  
//  this.notActivate();
  
//  /*
  var form = this.getFormularObj();
  var articleID = form.name;
  var quantity = (form.quantity.value);
	if ( quantity > 0 ) {
	  quantity--;
	  
    this.connectToServer(articleID,'delete','shopFrame');
    var i=0;
    while(i!= 100000){i++}
    
    this.setQuantity(articleID,quantity)
    form.quantity.value=quantity;
	}
//	*/
}

function Shop_show() {
//  this.notActivate();

  this.connectToServer(0,'show','content');
}

function Shop_clearArray() {
  var obj = this.getFrameSetObj();
  
  obj.shopArticleIDArray = new Array();
  obj.shopQuantityArray = new Array();
}

function Shop_connectToServer(articleID,proc,target) {
  
  var frameSetObj = this.getFrameSetObj();
  
  var shopForm = frameSetObj.shopFrame.document.getElementsByTagName('FORM')[0];
  
  if (target != null ) shopForm.target = target;
  
  shopForm.articleID.value = articleID;
  shopForm.proc.value = proc;
  //alert('Target: '+shopForm.target+', ArticleID: '+shopForm.articleID.value+', proc: '+shopForm.proc.value);
	shopForm.submit();  
}

function Shop_setQuantity(id,quantity) {
  
//  /*
  var frameSetObj = this.getFrameSetObj();
  
  var index = this.getIndex(id);
  if ( index == null ) {
    index = frameSetObj.shopArticleIDArray.length;
  }
  
  frameSetObj.shopArticleIDArray[index] = id;
  frameSetObj.shopQuantityArray[index]  = quantity;
//  */
}
/*
function Shop_getShopQuantityArray(i) {
  var frameSetObj = this.getFrameSetObj();
  return frameSetObj.shopQuantityArray[i] || 0;
}

function Shop_setShopQuantityArray(i,content) {
  var frameSetObj = this.getFrameSetObj();
  frameSetObj.shopQuantityArray[i]=content;
}
*/

function Shop_getQuantity() {
  
//  /*
  var frameSetObj = this.getFrameSetObj();
  
  var formObj = this.getFormularObj();
  var articleID = formObj.name;
  
  var index = this.getIndex(articleID);
  var quantity = frameSetObj.shopQuantityArray[index] || 0;
  //alert('ArticleID: '+articleID+',Index: '+index+',Quantity: '+quantity);
  formObj.quantity.value=quantity || 0;
//  */
}

function Shop_getFormularObj() {
  return document.getElementsByTagName('FORM')[0];
}
  
function Shop_getIndex(id) {
  
  var frameSetObj = this.getFrameSetObj();
  
  var cnt = frameSetObj.shopArticleIDArray.length;
    
  var i,index;
  
  for (i=0; i<cnt; i++) {
    if ( frameSetObj.shopArticleIDArray[i] == id ) index=i;
  }
  return index;
}

function Shop(Awin) {
  
  this.Fwin             = Awin;
  
  this.show             = Shop_show;
  this.add              = Shop_add;
  this.del              = Shop_del;
  this.setQuantity      = Shop_setQuantity;
  this.getQuantity      = Shop_getQuantity;
  this.getIndex         = Shop_getIndex;
  this.connectToServer  = Shop_connectToServer;
  this.getFormularObj   = Shop_getFormularObj;
  this.update           = Shop_update;
  this.clearArray       = Shop_clearArray;
  this.getFrameSetObj   = Shop_getFrameSetObj;
  this.notActivate      = Shop_notActivate;
  
  return this;
}

function Shop_update() {
//  /*
  formObj = this.getFormularObj();
  var y = formObj.elements;
  var i;
  
  this.clearArray();
  
  for (i=0; i<y.length; i++) {
    if ( y[i].type == 'text' ) {
      var articleID = y[i].name.substr(3,y[i].name.length);
      var quantity = y[i].value;    
//      alert('ID: '+articleID+',quantity: '+quantity);
      this.setQuantity(articleID,quantity);
    }
  }
//  */
}

function Shop_getFormularObj() {
  return this.Fwin.document.getElementsByTagName('FORM')[0];
}

shopObj = new Shop(window);
