var ua = navigator.userAgent.toLowerCase();
isIE     = (ua.indexOf('msie')>=0);
isIE5    = (ua.indexOf('msie 5')>=0);
isIE6    = (ua.indexOf('msie 6')>=0);
isIE7    = (ua.indexOf('msie 7')>=0);
isIE8    = (ua.indexOf('msie 8')>=0);
isFF     = (ua.indexOf('firefox')>=0);
isSAFARI = (ua.indexOf('safari')>=0);
isCHROME = (ua.indexOf('chrome')>=0);
isOPERA  = (ua.indexOf('opera')>=0);


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(/\./, ',')+' &euro;';
}


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 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 CenteredPopup(sHref,sTarget,nWidth,nHeight) {
  if (isIE6) nWidth+=24;
  if (isCHROME) {
    var nLeft = parseInt((window.screen.width-nWidth)/2);
    var nTop  = parseInt((window.screen.height-nHeight)/2-40);
  } else if (isFF) {
    var nLeft = parseInt(screenX+(window.innerWidth-nWidth)/2);
    var nTop  = parseInt(screenY+((window.innerHeight+64)-nHeight)/2);
  } else if (isIE6) {
    var nLeft = parseInt(screenLeft+(document.body.clientWidth-nWidth)/2);
    var nTop  = parseInt((window.screen.height-(nHeight+48))/2); 
  } else if (isIE) {
    var nLeft = parseInt(screenLeft+(document.documentElement.clientWidth-(nWidth+16))/2);
    var nTop  = parseInt(screenTop+(document.documentElement.clientHeight-(nHeight+96))/2);
  } else {
    var nLeft = parseInt((window.screen.width-nWidth)/2);
    var nTop  = parseInt((window.screen.height-nHeight)/2);
  }
  oPopup=window.open(sHref,sTarget,'left='+nLeft+',top='+nTop+',width='+nWidth+',height='+nHeight+',status=0,scrollbars=1,resizable=1');
  oPopup.window.focus();
  return !oPopup;
}

function CenteredPopup2(sHref,sTarget,nWidth,nHeight) {
  nHeight -= 64;
  newDiv = document.createElement('div');
  newDiv.style.position = 'absolute';
  newDiv.style.left = '50%';
  newDiv.style.top = '50%';
  newDiv.style.marginTop = (-nHeight/2)+'px';
  newDiv.style.marginLeft = (-nWidth/2)+'px';
  newDiv.style.width = nWidth+'px';
  newDiv.style.height = nHeight+'px';
  newDiv.style.padding = '8px';
  newDiv.style.backgroundColor = 'white';
  newDiv.style.border = '1px solid black';
  newDiv.style.zIndex = '1000';
  newDiv.innerHTML = '<iframe name="'+sTarget+'" width="100%" height="100%" frameborder="0" src="'+sHref+'"></iframe>';
  document.body.appendChild(newDiv);
}

function ClosePopup(bRefresh) {
  var myOpener=window.opener;
  window.close();
  if (myOpener) {
    if (bRefresh) {
      nTemp = myOpener.pageYOffset;
      myOpener.location.reload();
      myOpener.pageYOffset = nTemp;
    }
    myOpener.focus();
  }
}


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];
  }
}



