// Script by Steffen Lieberkind - www.SequelSite.dk - Copyright © 2002

var currentField = "";
function checkdropdown(theform,thefield,alerttext){
  currentField = thefield;
  check = eval("document."+theform+"."+thefield+".selectedIndex")
  if (check < 1){
    if (alerttext.length>0){showalert(alerttext);}
	focusField(theform);
    return(false);
  }
  return(true);
}

function checkradio(theform,thefield,alerttext) {
  var radioSelected = false;
  for (i=0;i<eval("document."+theform+"."+thefield+".length"); i++) {
    if (eval("document."+theform+"."+thefield+"[i].checked")) {radioSelected=true;break;}
  }
  if (!radioSelected) {
    showalert(alerttext);
    eval("document."+theform+"."+thefield+"[0].focus()")
    return (false);
  }
  return (true);
}

function checktextfield(theform,thefield,alerttext){
  currentField = thefield;
  check = eval("document."+theform+"."+thefield+".value.length");
  if (check < 1){
    if (alerttext.length>0){showalert(alerttext);}
	focusField(theform);
    return(false);
  }
  return(true);
}

function checktextfieldIsNumbers(theform,thefield,alerttext){
  currentField = thefield;
  var checkOK = "0123456789";
  var checkStr = eval("document."+theform+"."+thefield+".value")
  var talOk = true;
  for (i = 0;  i < checkStr.length;  i++){
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
      if (j == checkOK.length){
        talOk = false;
        break;
      }
    }
  if (!talOk){
    if (alerttext.length>0){showalert(alerttext);}
	focusField(theform);
    return(false);
  }
  return(true);
}

function checktextfieldIsNumbersOrSpaces(theform,thefield,alerttext){
  currentField = thefield;
  var checkOK = "0123456789 ";
  var checkStr = eval("document."+theform+"."+thefield+".value")
  var talOk = true;
  for (i = 0;  i < checkStr.length;  i++){
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
      if (j == checkOK.length){
        talOk = false;
        break;
      }
    }
  if (!talOk){
    if (alerttext.length>0){showalert(alerttext);}
	focusField(theform);
    return(false);
  }
  return(true);
}

function checkmaxchars(theform,themax,thefield,alerttext){
  currentField = thefield;
  check = eval("document."+theform+"."+thefield+".value.length")
  if (check>themax){
	if (alerttext.length>0){showalert(alerttext+"\n\n(du har indtastet "+check+")");}
	focusField(theform);
    return(false);
  }
  return(true);
}

function checkminchars(theform,themin,thefield,alerttext){
  currentField = thefield;
  check = eval("document."+theform+"."+thefield+".value.length")
  if (check<themin){
	if (alerttext.length>0){showalert(alerttext+" (du har indtastet "+check+").");}
	focusField(theform);
    return(false);
  }
  return(true);
}

function checkemail(theform,thefield,alerttext){
  currentField = thefield;
  var check = eval("document."+theform+"."+thefield+".value")
  if (check.indexOf("@")==-1 || check.indexOf(".")==-1){
	if (alerttext.length>0){showalert(alerttext);}
	focusField(theform);
    return(false);
  }
  return(true);
}

function checkfield(theform,thefield,alerttext,OKchars){
  currentField = thefield;
  var checkOK = OKchars;
  var checkStr = eval("document."+theform+"."+thefield+".value")
  var talOk = true;
  for (i = 0;  i < checkStr.length;  i++){
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
      if (j == checkOK.length){
        talOk = false;
        break;
      }
    }
  if (!talOk){
    if (alerttext.length>0){showalert(alerttext);}
	focusField(theform);
    return(false);
  }
  return(true);
}

function showalert(x) {
	if (x!=""){alert(x)}
}

function focusField(theform){
	if (eval("document."+theform+"."+currentField+".style.display!='none'")) {
		eval("document."+theform+"."+currentField+".focus()")
	}
}

