function ShowPopupImage(lnk){
  window.open('/image.php?f='+escape(lnk.href)+'&t='+escape(lnk.title),'','status=0,menubar=0,toolbar=0,scrollbars=1');
  return false;
}
function insertAtCaret(oFieldId,selec,type)
{
  type = 'smiley';
  var isMozilla = (navigator.userAgent.toLowerCase().indexOf('gecko')!=-1) ? true : false;
  var regexp = new RegExp("[\n]","gi");
  var oField = document.getElementById(oFieldId);//document.forms['news'].elements['newst'];
  if (isMozilla){ // Firefox
    objectValue = oField.value;
    objectValueStart = objectValue.substring( 0 , oField.selectionStart );
    objectValueEnd = objectValue.substring( oField.selectionEnd , oField.textLength );
    objectSelected = objectValue.substring( oField.selectionStart ,oField.selectionEnd );
    if (type) // smiley
    {
    oField.value = objectValueStart + " " + selec + objectSelected + objectValueEnd;
    }
    else
    {
    oField.value = objectValueStart + "[" + selec + "]" + objectSelected + "[/" + selec + "]" + objectValueEnd;
    }
    oField.focus();
    if (type) // smiley
    {
    oField.setSelectionRange(objectValueStart.length + selec.length + 2,objectValueStart.length + selec.length + 1);
    }
    else
    {
    oField.setSelectionRange(objectValueStart.length + selec.length + 2,objectValueStart.length + selec.length + 2);
    }
    }
  else{ // IE
    //oField = document.forms['news'].elements['newst'];
    var str = document.selection.createRange().text;
    if (str.length>0)
    {// if we have selected some text,,
      var sel = document.selection.createRange();
      if (type) // smiley
      {
        sel.text = " " + selec;
      }
      else
      {
        sel.text = "[" + selec + "]" + str + "[/" + selec + "]";
      }
      sel.collapse();
      sel.select();
    }
    else
    {
      oField.focus(oField.caretPos);
      oField.focus(oField.value.length);
      oField.caretPos = document.selection.createRange().duplicate();
      var bidon = "%~%"; // needed to catch the cursor position with IE
      var orig = oField.value;
      oField.caretPos.text = bidon;
      var i = oField.value.search(bidon);
      if (type) // smiley
      {
        oField.value = orig.substr(0,i) + " " + selec +  orig.substr(i, oField.value.length);
      }
      else
      {
        oField.value = orig.substr(0,i) + "[" + selec + "][/" + selec + "]" + orig.substr(i, oField.value.length);
      }
      var r = 0;
      for(n = 0; n < i; n++)
      {
        if(regexp.test(oField.value.substr(n,2)) == true)
      {r++;}
      };
      if (type) // smiley
      {
        pos = i + 1 + selec.length - r;
      }
      else
      {
        pos = i + 2 + selec.length - r;
      }
      // re-format the textarea & move the cursor to the correct position
      var r = oField.createTextRange();
      r.moveStart('character', pos);
      r.collapse();
      r.select();
    }
  }
}



function getAjaxObject() {
  if (typeof XMLHttpRequest != 'undefined') {
      return new XMLHttpRequest();
  }
  try {
      return new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
      try {
          return new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
  }
  return false;
}
function SilentGet(url){
  var http = getAjaxObject();
  http.open("GET", url, true);
/*
  http.onreadystatechange = function(){
       if (http.readyState == 4) {
         alert(http.responseText)
       }
   }
*/
  http.send();
}
function OpenCalendar(formName, fieldName){
  w = window.open('/calendar.php?formName='+formName+'&fieldName='+fieldName+'&date='+document.forms[formName].elements[fieldName].value,'','toolbar=0,menubar=0,width=160,height=90');
}
function SetDateField(formName, fieldName, dateObj){
  var f = document.forms[formName];
  if(f && f.elements[fieldName]){
    //f.elements[fieldName].value = (dateObj.getMonth() + 1)+'-'+dateObj.getDate()+'-'+dateObj.getFullYear();
    if(dateObj != null)
      f.elements[fieldName].value = (dateObj.getMonth() + 1)+'-'+dateObj.getDate()+'-'+dateObj.getFullYear();
    else
      f.elements[fieldName].value = '';
  }
}
var showInfoStarted = false;
var keepFromLoop = 0;
function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }
var cartVisible = false;
function ShowCartInfo(divId, e){
  target = (e.target?e.target:e.srcElement);

  y = findPosY(target)+ 20;//(e.y?e.y:e.pageY) + 5;
  b = document.getElementById('maincontainer');
  //if(!b)
  //  b = document.getElementById('containall');
  el = document.getElementById(divId);
  x = b.offsetLeft + b.clientWidth - el.clientWidth;
  el.style.top = y + 'px';
  el.style.left = (x - 1) + 'px';
  //el.style.clip = "rect(0px,100px,50px,0px)";
  el.style.visibility = 'visible';
  if(!showInfoStarted){
    showInfoStarted = true;
    keepFromLoop = 0;
    ShowHideInfoAnimation(divId, 0, el.clientWidth + 2, 0, el.clientWidth, -4, 0)
  }
  cartVisible = true;
}
function T2ShowCartInfo(divId, e){
  target = (e.target?e.target:e.srcElement);

  y = findPosY(target)+ 20;//(e.y?e.y:e.pageY) + 5;
  b = document.getElementById('cartinfocontainer');
  //if(!b)
  //  b = document.getElementById('containall');
  el = document.getElementById(divId);
  x = b.offsetLeft + b.clientWidth - el.clientWidth;
  el.style.top = y + 'px';
  el.style.left = (x - 2) + 'px';
  //el.style.clip = "rect(0px,100px,50px,0px)";
  el.style.visibility = 'visible';
  if(!showInfoStarted){
    showInfoStarted = true;
    keepFromLoop = 0;
    ShowHideInfoAnimation(divId, 0, el.clientWidth + 2, 0, el.clientWidth, -4, 0)
  }
  cartVisible = true;
}
function HideCartInfo(){
  if(!showInfoStarted){
    showInfoStarted = true;
    el = document.getElementById('cartinfo');
    ShowHideInfoAnimation('cartinfo', 0, el.clientWidth, el.clientHeight, 0, 6, 0);
  }
  cartVisible = false;
}
function ShowHideInfoAnimation(divId, t, r, b, l, stepTopLeft, stepRightBottom){
  el = document.getElementById(divId);
  lEnd = false;
  bEnd = false;
  if(stepTopLeft < 0){
    if(l <= el.clientWidth && l >= 0)
      l += stepTopLeft;
    else
      lEnd = true;
    if(b <= el.clientHeight && b >= 0)
      b += (stepTopLeft * -1);
    else
      bEnd = true;
  }
  else if(stepTopLeft > 0){
    if(l <= el.clientWidth && l >= 0)
      l += stepTopLeft;
    else
      lEnd = true;
    if(b <= el.clientHeight && b >= 0)
      b += (stepTopLeft * -1);
    else
      bEnd = true;
  }
  else{
    if(r <= el.clientWidth && r > 0)
      r += stepRightBottom;
    if(b <= el.clientHeight && b > 0)
      b += stepRightBottom;
  }

  el.style.clip = "rect("+t+"px,"+r+"px,"+b+"px,"+l+"px)";
  keepFromLoop++;
  if(keepFromLoop < 1000 && (!lEnd || !bEnd)){
    setTimeout('ShowHideInfoAnimation(\''+divId+'\', '+t+','+r+','+b+','+l+','+stepTopLeft+','+stepRightBottom+')', 10);
  }
  else{
    showInfoStarted = false;
    keepFromLoop = 0;
  }
}
function FormatPrice(price){
  price = Math.round(price * 100);
  price = price / 100;
  tmp =  price.toString().indexOf('.');
  if(tmp > 0){
    start = price.toString().substr(0, tmp);
    end = price.toString().substr(tmp + 1);
    end = end.toString();
    if(end.length < 2)
      end += '0';
    else if(end.length > 2)
      end = end.substr(0,2);
    price = start+'.'+end;
  }

  return price;
}

