// ****************************************************************
// Variables
// ****************************************************************

defaultEmptyOK = true;

daysInMonth = new Array(13);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;


// ****************************************************************
// Functions
// ****************************************************************

function daysInFebruary (year) {   

	// February has 29 days in any year evenly divisible by four,
  // EXCEPT for centurial years which are not also divisible by 400.
  return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function isDate (year, month, day) {   

	// catch invalid years (not 2- or 4-digit) and invalid months and days.
	
  if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;

  // Explicitly change type to integer to make code work in both
  // JavaScript 1.1 and JavaScript 1.2.
  var intYear = parseInt(year);
  var intMonth = parseInt(month);
  var intDay = parseInt(day);
	
  // catch invalid days, except for February
  if (intDay > daysInMonth[intMonth]) 
  	return false; 

  if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) 
  	return false;

  return true;
}

function isDay (s) {   
  if (isEmpty(s)) 
    if (isDay.arguments.length == 1) 
    	return defaultEmptyOK;
    else 
    	return (isDay.arguments[1] == true);   
  return isIntegerInRange (s, 1, 31);
}

function isDigit (c) {   
	return ((c >= "0") && (c <= "9"));
}

function isEmpty(s) {   
	return ((s == null) || (s.length == 0))
}

function isInteger (s) {   
	if (isEmpty(s)) 
    if (isInteger.arguments.length == 1) 
      return defaultEmptyOK;
    else 
   	  return (isInteger.arguments[1] == true);

  // Search through string's characters one by one
  // until we find a non-numeric character.
  // When we do, return false; if we don't, return true.

  for (var i = 0; i < s.length; i++) {   
    // Check that current character is number.
    var c = s.charAt(i);
    if (!isDigit(c)) 
      return false;
  }

  // All characters are numbers.
  return true;
}

function isIntegerInRange (s, a, b) {   
	if (isEmpty(s)) 
    if (isIntegerInRange.arguments.length == 4) 
      return (isIntegerInRange.arguments[1] == true);
    else 
    	return defaultEmptyOK;

  // Catch non-integer strings to avoid creating a NaN below,
  // which isn't available on JavaScript 1.0 for Windows.
  if (!isInteger(s, false)) return false;

  // Now, explicitly change the type to integer via parseInt
  // so that the comparison code below will work both on 
  // JavaScript 1.2 (which typechecks in equality comparisons)
  // and JavaScript 1.1 and before (which doesn't).
  
  var num = parseInt (s,10);
  return ((num >= a) && (num <= b));
}

function isMonth (s) {   
	if (isEmpty(s)) 
    if (isMonth.arguments.length == 1) 
    	return defaultEmptyOK;
    else 
    	return (isMonth.arguments[1] == true);
  return isIntegerInRange (s, 1, 12);
}

function isNonnegativeInteger (s) {   
	var secondArg = defaultEmptyOK;

  if (isNonnegativeInteger.arguments.length > 1)
    secondArg = isNonnegativeInteger.arguments[1];

  // The next line is a bit byzantine.  What it means is:
  // a) s must be a signed integer, AND
  // b) one of the following must be true:
  //    i)  s is empty and we are supposed to return true for
  //        empty strings
  //    ii) this is a number >= 0

  return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}

function isSignedInteger (s) {   
	if (isEmpty(s)) 
    if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
    else return (isSignedInteger.arguments[1] == true);

  else {
    var startPos = 0;
    var secondArg = defaultEmptyOK;

    if (isSignedInteger.arguments.length > 1)
      secondArg = isSignedInteger.arguments[1];

    // skip leading + or -
    if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
      startPos = 1;    
    return (isInteger(s.substring(startPos, s.length), secondArg))
  }
}

function isYear (s) {   
	if (isEmpty(s)) 
    if (isYear.arguments.length == 1) 
    	return defaultEmptyOK;
    else 
    	return (isYear.arguments[1] == true);
  if (!isNonnegativeInteger(s)) 
   	return false;
  return ((s.length == 2) || (s.length == 4));
}

function stripCharsNotInBag (s, bag) {   
    var returnString = "";

    // Search through string's characters one by one.
    // If character is in bag, append to returnString.

    for (var i = 0; i < s.length; i++) {   
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) 
        	returnString += c;
    }
    return returnString;
}

function isEmail(addr) {
  var a = addr.indexOf("@");
  if (a <= 0 || addr.lastIndexOf("@") != a)
    return false;
  var d = addr.indexOf(".");
  if (d <= 0)
    return false;
  d = addr.lastIndexOf(".");
  if (d < a+2 || d > addr.length-3)
    return false;
  if (addr.charAt(a+1) == "." || addr.charAt(a-1) == "." || addr.indexOf("..") >= 0)
    return false;
  return true;
}


// ****************************************************************
// Variables, constructors and initializers
// ****************************************************************

  var Alert = "";

  var NUM_msg;
  var DAT_msg;
  var EML_msg;
  var MAN_msg;
  var DateFormat;

  var check;

  function initialize() {
    check = new Array();
  }

  function Check(field, type) {
    this.field = field;
    this.type = type;
  }

// ****************************************************************
// Default values
// ****************************************************************

  NUM_msg = 'er ikke et tal! ';
  DAT_msg = 'er ikke en gyldig dato! Dato format er ';
  MAN_msg = 'Dette felt skal udflydes!!';
  EML_msg = 'er ikke en gyldig e-mail adresse!';
  DateFormat = 'DDMMYY';

// *****************************************************************
// Field check functions
// *****************************************************************

  function CheckEmail(attr) {
    Alert = attr.value + " " + EML_msg;
    return (isEmail(attr.value));
  }

  function CheckMan(attr) {
    Alert = MAN_msg;
    return !isEmpty(attr.value);
  }

  function CheckNum(att) {
    var attr = att.value;
    Alert = attr + ' ' + NUM_msg;
    var i = -1;
    if ((i = attr.indexOf(".")) == attr.indexOf(".") && i+1<attr.length && i != 0) {
      return isInteger(attr.substring(0,i)) && isInteger(attr.substring(i+1,attr.length));
      }
    else 
      return false;
  }

  function CheckDat(attr) {
    var s = attr.value;
    if (s.length == 0)
			return defaultEmptyOK;
    Alert = s + ' ' + DAT_msg + DateFormat;

    if (isInteger(s)) {

    	// Check for match with stripped DateFormat
			var date = stripCharsNotInBag(DateFormat,"MDY");
			
			if (date.length != s.length) 
				return false;
			
			var day = s.substring(date.indexOf("D"),date.lastIndexOf("D")+1);
			var month = s.substring(date.indexOf("M"),date.lastIndexOf("M")+1);
			var year = s.substring(date.indexOf("Y"),date.lastIndexOf("Y")+1);

			if (isDate(year,month,day)) {
				// Format date as according to the DateFormat
				attr.value = makeDate(year,month,day,DateFormat);
				return true;
			}
			else
				return false;
    }
     else if (s.length==DateFormat.length) {

    	var day = s.substring(DateFormat.indexOf("D"),DateFormat.lastIndexOf("D")+1);
			var month = s.substring(DateFormat.indexOf("M"),DateFormat.lastIndexOf("M")+1);
			var year = s.substring(DateFormat.indexOf("Y"),DateFormat.lastIndexOf("Y")+1);

			if (!isDate(year,month,day))
			  return false;
			else {
			  attr.value = makeDate(year,month,day,DateFormat);
			  return true;
			}
    }
    else
			return false;
  }


  function makeDate(year,month,day,toDateFormat) {
		var date = toDateFormat;
		date = date.substring(0,date.indexOf("D")) + day + date.substring(date.lastIndexOf("D")+1,date.length);
		date = date.substring(0,date.indexOf("M")) + month + date.substring(date.lastIndexOf("M")+1,date.length);
		date = date.substring(0,date.indexOf("Y")) + year + date.substring(date.lastIndexOf("Y")+1,date.length);
		return date;
  }

// ****************************************************************
// Event routines
// ****************************************************************

  function isOkay() {
    if (check == null)
      return true;
    for (var i=0; i<check.length; i++) {
      var result = true;
      if (check[i].type == "MAN") 
        result = CheckMan(check[i].field);
      else if (check[i].type == "NUM")
        result = isEmpty(check[i].field.value) || CheckNum(check[i].field);
      else if (check[i].type == "DATE")
        result = isEmpty(check[i].field.value) || CheckDat(check[i].field);
      else if (check[i].type == "EMAIL")
        result = isEmpty(check[i].field.value) || CheckEmail(check[i].field);
      if (!result) {
        check[i].field.focus();
        alert(Alert);
        return false;
      }
    }
    return true;
  }



