//*****************************************************************************
//   Autoweb shared JavaScripts. Part of Autoweb CMS
//*****************************************************************************

function LTrim(value) {
  var re = /\s*((\S+\s*)*)/;
  return value.replace(re, "$1");
}
function RTrim(value) {
  var re = /((\s*\S+)*)\s*/;
  return value.replace(re, "$1");
}
function trim(value) {
  return LTrim(RTrim(value));
}
function closeWnd() {
  opener.location.reload();
  window.close();
}

function showPopup(elemId) {
  document.getElementById(elemId).style.visibility = 'visible';
}
function hidePopup(elemId) {
  document.getElementById(elemId).style.visibility = 'hidden';
}
function selectElements(listName, elemId) {
  document.getElementById(elemId).style.visibility = 'hidden';
  var list = document.getElementById('_' + listName);
  var txt = document.getElementById(listName);
  if (list.options.length > 0 && list.options[0].selected) {
    txt.value = '';
  } else {
    var i;
    var count = 0;
    var selectedArray = new Array();
    for (i=0; i < list.options.length; i++) {
      if (list.options[i].selected) {
        selectedArray[count] = list.options[i].value;
        count++;
      }
    }
    txt.value = selectedArray;
  }
}

function loginDialog(dir) {
  var left = (screen.availWidth/2) - (276/2);
  var top = (screen.availHeight/2) - (148/2);
  var wnd = window.open(dir+'/LoginDlg.jsp', 'LoginDlg',
    'scrollbars=0, resizable=1, toolbar=0, statusbar=0, width=272, height=138, left='+left+', top='+top);
  wnd.focus();
}
function logoutDialog(dir) {
  var left = (screen.availWidth/2) - (276/2);
  var top = (screen.availHeight/2) - (148/2);
  var wnd = window.open(dir+'/LogoffDlg.jsp', 'LogoffDlg',
    'scrollbars=0, resizable=1, toolbar=0, statusbar=0, width=272, height=138, left='+left+', top='+top);
  wnd.focus();
}

function validateText(theText) {
  var val = trim(theText);
  if (val == "")
    return(false);
  return (true);
}

function validatePhoneNr(phoneNr) {
  if (isNaN(phoneNr))
    return ('Kan bare inneholde tall');
  // Valid!
  return null;
}

function validateEmail(emailStr) {
  var emailPat=/^(.+)@(.+)$/;
  var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
  var validChars="\[^\\s" + specialChars + "\]";
  var quotedUser="(\"[^\"]*\")";
  var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
  var atom=validChars + '+';
  var word="(" + atom + "|" + quotedUser + ")";

  var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
  var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

  var matchArray=emailStr.match(emailPat);
  if (matchArray == null)
    return ("Ikke korrekt (sjekk @ og .)");

  var user = matchArray[1];
  var domain = matchArray[2];

  for (i = 0; i < user.length; i++) {
    if (user.charCodeAt(i) > 127)
      return ("Brukernavn inneholder ugyldige tegn.");
  }
  for (i = 0; i < domain.length; i++) {
    if (domain.charCodeAt(i) > 127)
      return ("Domenenavnet inneholder ugyldige tegn.");
  }

  if (user.match(userPat) == null)
    return ("Brukernavnet er ikke gyldig.");
 
  var atomPat = new RegExp("^" + atom + "$");
  var domArr = domain.split(".");
  var len = domArr.length;
  for (i = 0; i < len; i++) {
    if (domArr[i].search(atomPat) == -1)
      return ("Domenenavnet er ikke gyldig.");
  }

  //if (len < 2)
  //  return("Adressen mangler");

  // Valid!
  return null;
}

function validateZipCode(zipCode) {
  if (isNaN(zipCode))
    return ('Kan bare inneholde tall');
  //var len = zipCode.length;
  //if (len != 4)
  //  return("Postnr har 4 tall");
  // Valid!
  return null;
}

