

function IsValidPhone(sPhone){
  return sPhone.match(/[0-9\s\+\-]{9,}/);
}


function IsValidEMail(sEMail){
  return ((sEMail.length>6) && (sEMail.indexOf(".")>3) && (sEMail.indexOf("@")>0));
}


function IsValidCreditCard(sCard){
  // remove non-numerics
  var v="0123456789";
  var w="";
  for (i=0; i<sCard.length; i++) {
    x = sCard.charAt(i);
    if (v.indexOf(x,0)>=0) w+=x;
  }
  // validate number
  j = w.length / 2;
  if (j<6.5 || j>8 || j==7) return false;
  k = Math.floor(j);
  m = Math.ceil(j) - k;
  c = 0;
  for (i=0; i<k; i++) {
    a = w.charAt(i*2+m) * 2;
    c += a>9? Math.floor(a/10 + a%10) : a;
  }
  for (i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
  return (c%10==0);
}


function IsValidExpiryDate(nMonth,nYear){
  var today  = new Date();
  var expiry = new Date(nYear, nMonth-1, 22);
  return (today.getTime()<=expiry.getTime());
}


function FormIsChanged(oForm){
  var nChanges = 0;
  for (i=0; i<oForm.elements.length; i++) {
    var element = oForm.elements[i];
    if (element.type.slice(0,6)=='select') {
      for (j=0; j<element.options.length; j++) {
        if (element.options[j].selected!=element.options[j].defaultSelected) {
          nChanges++;
          element.style.borderColor="orange";
          break;
        }
      }
    } else 
    if ((element.type=='checkbox')||(element.type=='radio')) {
      if (element.checked!=element.defaultChecked) {
        nChanges++;
        element.style.borderColor="orange";
      }
    } else {
      if (element.value!=element.defaultValue) {
        nChanges++;
        element.style.borderColor="orange";
      } 
    }
  }
  return nChanges;
}


function EuroCurToStr(nAmount){
  return nAmount.toFixed(2).replace(/\./, ',')+' €';
}


function EuroStrToCur(sAmount){
  var nAmount = parseFloat(sAmount.replace(/\./,'').replace(/\,/,'.'));
  nAmount = (nAmount==NaN)?0:nAmount;
  return nAmount;
}


function GetScrollY() {
  if (window.pageYOffset) {
    return window.pageYOffset;
  } else {
    return  Math.max(document.body.scrollTop,document.documentElement.scrollTop);
  }
}


function GetClientWidth()
{
  var v=0,d=document,w=window;
  if((!d.compatMode || d.compatMode == 'CSS1Compat') && !w.opera && d.documentElement && d.documentElement.clientWidth)
    {v=d.documentElement.clientWidth;}
  else if(d.body && d.body.clientWidth)
    {v=d.body.clientWidth;}
  else if(xDef(w.innerWidth,w.innerHeight,d.height)) {
    v=w.innerWidth;
    if(d.height>w.innerHeight) v-=16;
  }
  return v;
}


function GetClientHeight()
{
  var v=0,d=document,w=window;
  if((!d.compatMode || d.compatMode == 'CSS1Compat') && !w.opera && d.documentElement && d.documentElement.clientWidth)
    {v=d.documentElement.clientHeight;}
  else if(d.body && d.body.clientHeight)
    {v=d.body.clientHeight;}
  else if(xDef(w.innerWidth,w.innerHeight,d.height)) {
    v=w.innerHeight;
  }
  return v;
}


function GetScrollY() {
  if (window.pageYOffset) {
    return window.pageYOffset;
  } else {
    return  Math.max(document.body.scrollTop,document.documentElement.scrollTop);
  }
}

function GetElementY(obj) {
  var Y = 0;
  do {
    Y += obj.offsetTop;
  } while (obj = obj.offsetParent);
  return Y;
}

function GetElementX(obj) {
  var X = 0;
  do {
    X += obj.offsetLeft;
  } while (obj = obj.offsetParent);
  return X;
}

function GetElementTop(oElement,oRef) {
  var nTop = 0;
  while ((oElement!=null)&&(oElement!=oRef)) {
    if (!oElement.offsetTop) {
      oElement = oElement.parentNode;
      continue;
    }
    nTop += parseInt(oElement.offsetTop);
    oElement = oElement.offsetParent;
  }
  return nTop;
}


function ChangeClass(oElement,sClass) {
  oElement.className = sClass;
  oElement.setAttribute("class",sClass);  
}


function EncodeLineBreaks(sText) {
  return sText.replace(/\n/g,"\\n").replace(/\r/g,"\\r");
}


function HideAndWait(oElement) {
  oElement.style.visibility = 'hidden';
  oElement.parentNode.style.cursor = 'wait';
}

function CreateWindow(sName,sUrl,nWidth,nHeight) {
  var oDiv = document.getElementById(sName);
  if (oDiv) {
    document.body.removeChild(oDiv);
  } else {
    oDiv = document.createElement('div');
  }
  oDiv.style.display = 'block';
  oDiv.style.position = 'absolute';
  oDiv.style.left = parseInt((GetClientWidth()-(nWidth+80))/2)+'px';
  oDiv.style.top = (GetScrollY()+64)+'px';
  oDiv.style.zIndex = 1000;
  oDiv.setAttribute('id',sName);

  var sTemp = '';
  sTemp += '<table cellspacing=0 cellpadding=0 border=0><tr>';
  sTemp += '<td class="window1"></td>';
  sTemp += '<td class="window2"></td>';
  sTemp += '<td class="window3"></td>';
  sTemp += '</tr><tr>';
  sTemp += '<td class="window4"></td>';
  sTemp += '<td class="window5"><iframe width='+nWidth+' height='+nHeight+' scrolling=no frameborder=0 src="'+sUrl+'"></iframe></td>';
  sTemp += '<td class="window6"></td>';
  sTemp += '</tr><tr>';
  sTemp += '<td class="window7"></td>';
  sTemp += '<td class="window8"></td>';
  sTemp += '<td class="window9"></td>';
  sTemp += '</tr></table>';

  oDiv.innerHTML = sTemp;

  document.body.appendChild(oDiv);

  /*
  var oBack = document.getElementById('bodyshadow');
  if (oBack) {
    document.body.removeChild(oBack);
  } else {
    oBack = document.createElement('div');
  }
  oBack.style.display = 'block';
  oBack.style.position = 'absolute';
  oBack.style.left = '0px';
  oBack.style.top = '0px';
  oBack.style.width = GetClientWidth();
  oBack.style.height = document.body.clientHeight;
  oBack.style.backgroundColor = 'Black';
  oBack.style.opacity = 0.4;
  oBack.style.filter = 'alpha(opacity=40)';
  oBack.style.zIndex = 999;
  oBack.setAttribute('id','bodyshadow');
  document.body.appendChild(oBack);
  */
}

function CloseWindow(sName) {
  var oDiv = document.getElementById(sName);
  if (oDiv) {
    document.body.removeChild(oDiv);
  }
  oDiv = document.getElementById('bodyshadow');
  if (oDiv) {
    document.body.removeChild(oDiv);
  }
}

function DragWindow1() {
  DragElement = this;
  DragOffX = event.clientX;
  DragOffY = event.clientY;
  DragNowX = parseInt(this.style.left);
  DragNowY = parseInt(this.style.top);
  document.onmousemove=DragWindow3;
}

function DragWindow2() {
  document.onmousemove=undefined;
  DragElement = null;
}

function DragWindow3() {
  if (!DragElement) return;
  DragElement.style.left = (DragNowX + event.clientX-DragOffX)+'px';
  DragElement.style.top  = (DragNowY + event.clientY-DragOffY)+'px';
}

function CookieWrite(sName,sValue,nDays,sPath) {
  if (nDays) {
    var dDate = new Date();
    dDate.setTime(dDate.getTime()+(nDays*24*60*60*1000));
    var sExpires = "; expires="+dDate.toGMTString();
  } else {
    var sExpires = "";
  }
  if (sPath) {
    sPath='; path='+sPath;
  } else {
    sPath = "";
  }
  document.cookie = sName+'='+sValue+sExpires+sPath;
}

function CookieRead(sName) {
  var sValue='';
  if (typeof document.cookie!='undefined') {
   sValue = (sValue=document.cookie.match(new RegExp("(^|;|\\s)"+sName+'=([^;]+);?'))) ? sValue[2] : '';  
  }
  return sValue;
}

function GetUrlParam(sName) {
  var oRegExp = new RegExp('[\\?&]'+sName+'=([^&#]*)');
  var aValues = oRegExp.exec(window.location.href);
  if (aValues==null) {
    return ''; 
  } else {
    return aValues[1];
  }
}

function Encrypt(theText) {
  output = new String;
  Temp = new Array();
  TextSize = theText.length;
  for (i = 0; i < TextSize; i++) {
    output += String.fromCharCode(theText.charCodeAt(i)+17+(i%26));
  }
  return output;
}

function Decrypt(theText) {
  output = new String;
  Temp = new Array();
  TextSize = theText.length;
  for (i = 0; i < TextSize; i++) {
    output += String.fromCharCode(theText.charCodeAt(i)-17-(i%26));
  }
  return output;
}

