var strHostUrl = top.location.protocol+"//"+top.location.hostname;

function validForm(oF){
  aE = oF.elements;
  for(f=0;f<aE.length;f++){
    if(aE[f].type=="text"){

      if(aE[f].maxLength>249 && aE[f].value==""){
        alert("Bitte füllen Sie das Feld "+aE[f].name+" aus!");
        aE[f].focus();
        return false;
      }
      // auf numeric prüfen ( 12 oder 13.456 )
      if((aE[f].maxLength==251 || aE[f].maxLength==241) && aE[f].value!=""){
        if (isNaN(aE[f].value) && aE[f].value != ""){
          alert("Bitte geben Sie im Feld "+aE[f].name+" eine Zahl ein!");
          aE[f].focus();
          aE[f].select();
          return false;
        }
      }
      // auf mailsyntax prüfen ( name.sonstwas@domain.de )
      if((aE[f].maxLength==252 || aE[f].maxLength==242)  && aE[f].value!=""){
        if (aE[f].value.replace(/[\w\._-]+@[\w\.-]+[\w\.-]+\.[\w]+[\w]+/, '') != '' && aE[f].value != ""){
          alert("Bitte geben Sie im Feld "+aE[f].name+" eine gültige E-Mailadresse ein!");
          aE[f].focus();
          aE[f].select();
          return false;
        }
      }
      // Auf gültiges Datumsformat prüfen ( 12.03.2001 )
      if((aE[f].maxLength==253 || aE[f].maxLength==243)  && aE[f].value!=""){
        if (aE[f].value.replace(/(([0-2][0-9])|([0-9])|([3][0-1]))([\.])(([0-9])|([1][0-2])|([0][0-9]))([\.])([1-2][0-9][0-9][0-9])/, '') != ''){
          // TODO Prüfen ob es das Datum überhaupt gibt ( 31.02.2000 muß ausgeschlossen werden )
          alert("Bitte geben Sie im Feld "+aE[f].name+" ein gültiges Datum ein!");
          aE[f].focus();
          aE[f].select();
          return false;
        }
      }
    }
  }
  return true;
}
function getDocEditor(doc_id,kat_id,vorlage,lang_id){
  wx = screen.availWidth/2;
  wy = screen.availHeight/2;
  w = window.open("/editor/editor.php?doc_id="+doc_id+"&kat_id="+kat_id+"&vorlage="+vorlage+"&language="+lang_id, "DOCFORM"+doc_id,'width=950,height=600,resizable=yes,scrollbars=no,status=yes');
  w.focus();
}
// Berechnung des Gesamtpreises dieses Produkts (Aufpreise/Menge inbegriffen)
SPAP = unescape("%A7AP%A7");
function aktGPREIS(oForm){
  iANZ = Number(oForm.ANZ.value);
  if(isNaN(iANZ)||iANZ==0){
    if(iANZ!=0){
      oForm.ANZ.value=1;
    }
    iANZ=1;
  }
  aEL = oForm.elements;
  iGPREIS = Number(oForm.BPREIS.value.replace(/,/ig,"."));
  for(f=0;f<aEL.length;f++){
    if(aEL[f].tagName.toLowerCase()=="select"){
      iAufPreis = Number(aEL[f].value.split(SPAP)[1].replace(/,/ig,"."));
      if(!isNaN(iAufPreis)){
        iGPREIS = iGPREIS + iAufPreis;
      }
    }
  }
  oForm.PREIS.value = number_format(iGPREIS*iANZ,2); //(Math.round(iGPREIS*100*iANZ)/100).toString().replace(/\./ig,",") ;
}
function removePosition(iPos){
  location.replace("?action=basketRemovePos&rp="+iPos);
}
function aktPosition(oANZ,iPos){
  if(isNaN(oANZ.value)){
    alert("Bitte geben Sie eine Gültige Menge ein!");
    oANZ.focus();
    oANZ.select();
    return;
  }
  location.replace("?action=aktBasketPos&POS="+iPos+"&ANZ="+oANZ.value);
}
function initJSRS( name, guid ){
  aKeys = eval("K"+guid);
  len = aKeys.length;
  aData = eval("D"+guid);
  z = 0; az = 0; aOut = Array();
  for(f=0;f<aData.length;f++){
    index = (f % len);
    if(index == 0){
      aOut[z] = Array();
      az = z; z++;
    }
    aOut[az][aKeys[index]] = unescape(aData[f].replace(/\+/g," "));
  }
  eval(name+"=aOut");
}
// formatiert eine zahl mit x kommastellen und rundet kaufmännisch
function number_format( sNumber, iDigits ){
  var pos;
  var Digits;
  sNumber = new String( sNumber );
  sNumber = sNumber.replace(/,/ig,".");
  iNumber = Number( sNumber );

  iNumber = Math.round(iNumber * Math.pow(10,iDigits) ) / Math.pow(10,iDigits);
  sNumber = new String( iNumber );
  pos = sNumber.indexOf(".");
  Digits = pos!=-1 ? iDigits - (sNumber.length - pos) + 1 : iDigits;
  sNumber += ( pos == -1 && iDigits != 0 ) ? "." : "" ;
  for(f=0;f<Digits;f++){
    sNumber += "0";
  }
  return sNumber.replace(/\./ig,",");
}

function preselectKDATA(DATA){
  if(document.KDATA){
    aEL = document.KDATA.elements
    for(f=0;f<aEL.length;f++){
      if(DATA[0][aEL[f].name]){
        aEL[f].value = DATA[0][aEL[f].name];
      }
    }
  }
}

/* KeyWord Suggestions */
var sX;
var sY;
var sW;
var curQ;
var Sdiv;
var Spos=-1;
AXSUG = new ajaxObject("/cms/suggest.php",showSuggestion);
function getSuggestions(e,q){
  curQ = q;
  sX = getposOffset(q,"left");
  sY = getposOffset(q,"top")+q.offsetHeight;  
  sW = q.offsetWidth;
  
  e = (!e) ? window.event : e; // get the event
  target = (!e.target) ? e.srcElement : e.target; // get the event's target
  if (target.nodeType == 3) target = target.parentNode;
  code = (e.charCode) ? e.charCode : ((e.keyCode) ? e.keyCode : ((e.which) ? e.which : 0)); // get the character code of the pressed button

  if (e.type == "keyup"){
    if(Sdiv && ( code==40||code==38 ) ){
      if(Spos==-1){
        restoreVal = q.value;  
      }
      Sdiv.innerHTML = Sdiv.innerHTML.replace(/class=["]*suggsel/,"")
      arSug = Sdiv.getElementsByTagName("span");
      if(code == 40) Spos ++; // down arrow
      if(Spos==arSug.length) Spos--;
      if(code == 38) Spos --; // up arrow
      if(Spos<-1) Spos = -1;
      if(Spos>=0 && Spos<=arSug.length){
        arSug[Spos].className="suggsel";
        q.value = arSug[Spos].innerHTML.replace(/<[^>]*>/g,"");
      }
      if(Spos==-1) q.value = restoreVal; 
      return false;
    }   
  }
  v = q.value; 
  if(v.length>1){
    AXSUG.abort();
    try{window.clearTimeout(tsug);}catch(er){}
    tsug=window.setTimeout( 'AXSUG.update("q='+ encodeURIComponent( v.replace(/["\\]/g,"") )+'","GET");',100 );
  }else{    
    showSuggestion("");
  }
}
function showSuggestion(data){
  if(data==""){
    Spos=-1; AXSUG.abort();
    if(Sdiv) Sdiv.className = "nosuggestion";
    return;
  }
  Spos = -1;
  s = "<span onmousedown='sug(this)'>"+data.replace(/,/g,"</span><span onmousedown='sug(this)'>")+"</span>";
  if(Sdiv){
    div = Sdiv;  
  }else{
    div = document.createElement("div");
    document.body.appendChild( div );     
  }
  div.style.left = sX;  
  div.style.top = sY;
  div.style.minWidth = sW-2;
  if(document.all)div.style.width = sW;
  div.className = "suggestion";
  div.innerHTML = s; 
  Sdiv = div;
}
function sug(o){
  curQ.value = o.innerHTML.replace(/<[^>]*>/g,"");
  setTimeout("curQ.focus();",10);
}

///////////////////////////////////////////////////////////////////////////////////////////
// Ajax Object
function ajaxObject(url, callbackFunction) {
  var that=this;
  this.updating = false;
  this.abort = function() {
    if (that.updating) {
      that.updating=false;
      that.AJAX.abort();
      that.AJAX=null;
    }
  }
  this.update =
  function(passData,postMethod) {
    if (that.updating) { return false; }
    that.AJAX = null;
    if (window.XMLHttpRequest) {
      that.AJAX=new XMLHttpRequest();
    }else{
      that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (that.AJAX==null) {
      return false;
    }else{
      that.AJAX.onreadystatechange = function() {
        if (that.AJAX.readyState==4) {
          that.updating=false;
          that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);
          that.AJAX=null;
        }                                                            }
        that.updating = new Date();
        if (/post/i.test(postMethod)) {
          var uri=urlCall+'?'+that.updating.getTime();
          that.AJAX.open("POST", uri, true);
          that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
          that.AJAX.setRequestHeader("Content-Length", passData.length);
          that.AJAX.send(passData);
        }else{
          var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime());
          that.AJAX.open("GET", uri, true);
          that.AJAX.send(null);
        }
        return true;
      }
    }
    var urlCall = url;
    this.callback = callbackFunction || function () { };
}

///////////////////////////////////////////////////////////////////////////////////////////
// AjaxDataStream zum Array(RS) zusammenfügen
function getAXD(AXD){
  a = AXD.split("<datastream:>");
  if(!a[1]) return Array();
  aKeys = a[0].split(",");
  len = aKeys.length;
  aData = a[1].split("|");
  z = 0; az = 0; aOut = Array();
  for(f=0;f<aData.length-1;f++){
    index = (f % len);
    if(index == 0){
      aOut[z] = Array();
      az = z; z++;
    }
    aOut[az][aKeys[index]] = aData[f].replace(/&#0124;/ig,"|");
  }
  return aOut;
}

///////////////////////////////////////////////////////////////////////////////////////////
//
function O(id){
  return document.getElementById(id);
}

// offsetPos 4 elements
function getposOffset(what, offsettype){
  var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
  var parentEl=what.offsetParent;
  while (parentEl!=null){
    totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
    parentEl=parentEl.offsetParent;
  }
  return totaloffset;
}


