var gShowFld;
var gDataFld;
var gLang;

var gEditorUrl = "/blank.php?page=dhtmled/editor.php";
var gShowSourceUrl = "/blank.php?page=dhtmled/showsource.php";
var gEditorWidth = 600;
var gEditorHeight = 500;

function filterKeys() {
    ev = window.event;
    if (ev.ctrlKey && ev.shiftKey && ev.keyCode == 12) {
        st = ev.srcElement.value;
        ch = st.substr(st.length-1,1);
        // alert(ch);
        // st = st.substring(0,st.length-1);
        switch (ch) {
            case 'A':
            case 'a':
            case 'I':
            case 'i':
            case 'U':
            case 'u':
            case 'E':
            case 'e':
            case 'O':
            case 'o':
                st += String.fromCharCode(0x304);
                break;
            default:
                break;
        }
        ev.srcElement.value = st;
        return false;
    }  else {
        return true;
    }
}


function loadData() {
    return gShowFld.innerHTML;
}

function saveEditor(pData) {
    alert("Blam");
    pData = replaceQuotes(pData);
    gShowFld.innerHTML = pData;
    gDataFld.value = pData;
}

function openEditor(pShowFld, pDataFld, pLang) {
    gShowFld = pShowFld;
    gDataFld = pDataFld;
    gLang = pLang;

    if ( window.event.shiftKey && window.event.ctrlKey ) {
        win2 = window.open(gShowSourceUrl, "", "dependant=1,resizable=1,height="+gEditorHeight+",width="+gEditorWidth+",scrollbars=auto");
    } else {
        win2 = window.open(gEditorUrl, "", "dependant=1,resizable=1,height="+gEditorHeight+",width="+gEditorWidth+",scrollbars=auto");
    }
}

function showSource(pShowFld, pDataFld) {
    gShowFld = pShowFld;
    gDataFld = pDataFld;
    win2 = window.open(gShowSourceUrl, "", "dependant=1,resizable=1,height="+gEditorHeight+",width="+gEditorWidth+",scrollbars=auto");
}

function replaceQuotes(pData) {
    alert(gLang);
    switch ( gLang ) {
        case 'de':
            return pData + 'de';
            break;
        default:
            return pData + 'def';
    }
}

function showWin(_url, _width, _height, _scroll) {

    if (_scroll == 1) {
	    win2 = window.open(_url, "", "dependant=1,resizable=1,height="+_height+",width="+_width+",scrollbars");
	} else {
	    win2 = window.open(_url, "", "dependant=1,resizable=1,height="+_height+",width="+_width);
	}

	win2.resizeTo(_width,_height);

	return false;
}


function checkDate(pElement, pMsg) {
  var DD;
  var MM;
  var YY;
  var len;
  var bOk;
  var dat;

  bOk = true;

  len = pElement.value.length;

  if ( len == 0) {
       return true;
  }

  if (len!=10 && len != 7 && len != 4) {
        bOk = false;
  } else {
      DD = parseInt(pElement.value.substr(8,2),10);
      MM = parseInt(pElement.value.substr(5,2),10);
      YY = parseInt(pElement.value.substr(0,4),10);
  }

  if (len > 4) {
      bOk = MM <= 12;
      if (len > 7) {
          if ( YY == 0
                  ||  DD>30 && (MM==2||MM==4||MM==6||MM==9||MM==11)
                  ||  DD>29 &&  MM==2 && !YY%4
                  ||  DD>28 &&  MM==2 &&  YY%4) {
                bOk = false;
          }
      } else {
         pElement.value += '-01';
      }
  } else {
     pElement.value += '-01-01';
  }


  if ( !bOk ) {
    alert(pMsg);
    pElement.select();
    pElement.focus();
    return false;
  } else {
    return true;
  }

}


//------------------------------------------------------------
function makeValidJpZip(pElement, pMessage) {
//
//  Chekker om pElement indeholder en japansk zip-code på formen
//  nnn-nnnn

    var zip = new String(pElement.value);

    switch (zip.length) {
       case 7:
            if (isValueInteger(zip)) {
                pElement.value = zip.substr(0,3) + '-' + zip.substr(3,4);
            } else {
                alert(pMessage);
                return false;
            }
            break;
       case 8:
            if ( zip.substr(3,1) != '-' || !isValueInteger(zip.substr(0,3)) || !isValueInteger(zip.substr(4,4))) {
                alert(pMessage);
                return false;
            }
            break;
       default:
            alert (pMessage);
            return false;
            break;
    }
    return true;
}
//------------------------------------------------------------
function isValidEmail(pElement, pMessage) {
//
// Check om pElement er en valid email adresse
// Udskriv pMessage og return false hvis pElement ikke er valid (dvs. udfyldt med invalid adresse, ikke blank)
// Kald:  onchange="return isValidEmail(this,'fejltekst')"
//        "return" sikrer at et invalid felt ikke forlades
//
// 27.03.01 SW  Tilrettet
//------------------------------------------------------------
  var email = pElement.value;
  var validEmail = true;

  if ((email.indexOf('@',0) < 1)
  ||  (email.indexOf('@',0) == email.length-1)) {
    validEmail = false;
  }
  if (validEmail) {
    emailsub = email.substr(email.indexOf('@',0)+1,email.length);
    if ((emailsub.substr(0,1)=='.')
    ||  (emailsub.indexOf('.',0)==-1)
    ||  (emailsub.substr(emailsub.length-1,1)=='.')) {
      validEmail = false;
    }
  }
  if (validEmail) {
    if ((email.indexOf('*',0)>=0)
    ||  (email.indexOf('?',0)>=0)
    ||  (email.indexOf('/',0)>=0)
    ||  (email.indexOf('\\',0)>=0)
    ||  (email.indexOf(':',0)>=0)
    ||  (email.indexOf('<',0)>=0)
    ||  (email.indexOf('>',0)>=0)
    ||  (email.indexOf(',',0)>=0)
    ||  (email.indexOf(' ',0)>=0)) {
      validEmail = false;
    }
  }

  if (email == '') { validEmail = true; }

  if (!validEmail) {
    alert(pMessage);
    pElement.focus();
    pElement.select();
  }
  return validEmail;
}


//------------------------------------------------------------
function isValidUser(pElement,pMessage) {
//
// Lav pElement om til en tekst uden blanktegn og som ikke
// starter med et tal.
// Tømmer pElement hvis det ikke er validt
// Kald:  onchange="return isValidUser(this,'fejltekst')"
//
// 27.03.01 MSL  Tilrettet
//------------------------------------------------------------
  var str = pElement.value;
  var validUser = true;
  var nVal;

  nVal = parseInt(str,10);

  validUser = isNaN(nVal) && str.indexOf(' ',0)<0;

  if (!validUser) {
    alert(pMessage);
    pElement.focus();
    pElement.select();
  }

  return validUser;

}

/*******************************************************************************
 * Form validering
 ******************************************************************************/

//------------------------------------------------------------
function isNotNull(pElement, pMessage) {
//
// Udskriv pMessage og return false hvis pElement er tomt
// Kald: OK = isNotNull(form1.alder,'fejltekst');
//
// 27.03.01 SW  Tilrettet
//------------------------------------------------------------
  if (!pElement.value) {
    alert(pMessage);
    pElement.focus();
    return false;
  } else {
    return true;
  }
}

//------------------------------------------------------------
function isValueInteger(pValue) {
//
// Udskriv pMessage og return false hvis pElement ikke er heltal
// Kald:onChange="return isInteger(this,'fejltekst')";
//
// 27.03.01 SW  Tilrettet
//------------------------------------------------------------
  var n = pValue.length;

  for (var i=0;i<n;i++) {
    nChar = pValue.substr(i,1);

    if (nChar < '0' || nChar > '9') {
       return false;
    }
  }
  return true;
}

//------------------------------------------------------------
function isInteger(pElement, pMessage) {
//
// Udskriv pMessage og return false hvis pElement ikke er heltal
// Kald:onChange="return isInteger(this,'fejltekst')";
//
// 27.03.01 SW  Tilrettet
//------------------------------------------------------------
  var n = pElement.value.length;

  for (var i=0;i<n;i++) {
    nChar = pElement.value.substr(i,1);

    if (nChar < '0' || nChar > '9') {
       alert(pMessage);
       return false;
    }
  }
  return true;
}

//------------------------------------------------------------
function isTooShort(pElement, pLength, pMessage) {
//
// Udskriv pMessage og return false hvis pElement er for kort
// Kald: OK = isTooShort(form1.password,4,'fejltekst');
//
// 27.03.01 SW  Tilrettet
// 28.03.01 SW  Hvis feltet er tomt er det OK.
//------------------------------------------------------------
  if (pElement.value && pElement.value.length < pLength) {
    alert(pMessage);
    pElement.focus();
    return false;
  } else {
    return true;
  }
}

//------------------------------------------------------------
function isIdentical (pElement1, pElement2, pMessage) {
//
// Udskriv pMessage og return false hvis pElement1 er forskellig fra pElement2
// Kald: OK = isIdentical(form1.password,form1.password2,'fejltekst');
//
// 27.03.01 SW  Tilrettet
//------------------------------------------------------------
  if (pElement1.value != pElement2.value) {
    alert(pMessage);
    pElement1.focus();
    return false;
  } else {
    return true;
  }
}

//------------------------------------------------------------
function isSmallerDate (pElement1, pElement2, pMessage) {
//
// Udskriv pMessage og return false hvis ikke pElement er mindre end pElement2
// pElement1 og pElement2 skal være valide datoer i format DD-MM-YY!
// Kald: OK = isSmallerDate(form1.fradato,form1.tildato,'fejltekst');
// Issue: Hvordan koordineres denne script med PL/SQL global konstanten DatoFormat??
//
// 27.03.01 SW  Tilrettet
//------------------------------------------------------------
  var FraDato = pElement1.value.substring(6,2) + pElement1.value.substring(3,2) + pElement1.value.substring(0,2);
  var TilDato = pElement2.value.substring(6,2) + pElement2.value.substring(3,2) + pElement2.value.substring(0,2);

  if (FraDato >= TilDato) {
    alert(pMessage);
    pElement1.focus();
    return false;
  } else {
    return true;
  }}

//------------------------------------------------------------
function makeValidDate(pElement) {
//
// Lav pElement om til en valid dato i format DD-MM-YY
// Tøm pElement hvis det ikke er validt
// Kald:  onchange="makeValidDate(this)"
// Issue: Q:Hvordan koordineres denne script med PL/SQL global konstanten DatoFormat??
//        A:Lav includefilen om til PL/SQL (psp procedure) og "parse" global DatoFormat konstanten
//        Q:Hvordan bruge currect date til at gøre indtastet DD og DDMM valid??
//        A:--
//
// 27.03.01 SW  Tilrettet
//------------------------------------------------------------
  var DD;
  var MM;
  var YY;
  var len;

  len = pElement.value.length;
  if (len==10) {        // DD-MM-YYYY
      DD = parseInt(pElement.value.substr(0,2),10);
      MM = parseInt(pElement.value.substr(3,2),10);
      YY = parseInt(pElement.value.substr(8,2),10);
  } else if (len==8) {
    DD = parseInt(pElement.value,10);
    if (DD > 1000000) { // DDMMYYYY
      DD = parseInt(pElement.value.substr(0,2),10);
      MM = parseInt(pElement.value.substr(2,2),10);
      YY = parseInt(pElement.value.substr(6,2),10);
    } else {            // DD-MM-YY
      DD = parseInt(pElement.value.substr(0,2),10);
      MM = parseInt(pElement.value.substr(3,2),10);
      YY = parseInt(pElement.value.substr(6,2),10);
    }
  } else if (len==7) {  // D-MM-YY || DD-M-YY
    DD = parseInt(pElement.value.substr(0,2),10);
    if (DD<10)
      MM = parseInt(pElement.value.substr(2,2),10);
    else
      MM = parseInt(pElement.value.substr(3,1),10);
    YY = parseInt(pElement.value.substr(5,2),10);
  } else if (len==6) {  // DDMMYY || D-M-YY
    DD = parseInt(pElement.value.substr(0,2),10);
    MM = parseInt(pElement.value.substr(2,2),10);
    YY = parseInt(pElement.value.substr(4,2),10);
  } else if (len==5) {  // D-M-Y
    DD = parseInt(pElement.value.substr(0,1),10);
    MM = parseInt(pElement.value.substr(2,1),10);
    YY = parseInt(pElement.value.substr(4,1),10);
  } else {
    pElement.value = '';
    return false;
  }

  if (isNaN(DD) || isNaN(MM) || isNaN(YY) || DD<1 || DD>31 || MM<1 || MM>12 || YY<0) {
    pElement.value = '';
    return false;
  }

  if (DD>30 && (MM==2||MM==4||MM==6||MM==9||MM==11)
  ||  DD>29 &&  MM==2 && !YY%4
  ||  DD>28 &&  MM==2 &&  YY%4) {
    pElement.value = '';
    return false;
  }

  pElement.value = (DD<10?'0':'')+DD+'-'+(MM<10?'0':'')+MM+'-'+(YY<10?'0':'')+YY;
  return true;
}

