//JAVA SCRIPT VALIDERING AV FORMULÄR INPUT



// -------------------------------------------------------------------
// Function:        isMatch
// Parameters:      formelement - the element you want to validate
//                  strFormat - the format you want to match against (eg. "####-##-##")
//                    Usage: "#" = Any one digit
//                           Any other character = Match that exact character
// Return values:   True = Match
//                  False = Element does not match the requested format
// -------------------------------------------------------------------
function isMatch (formelement, strFormat)
{
	var i;

	if (strFormat.length != formelement.value.length)
	{
		return false;
	}

	for (i = 0; i < formelement.value.length; i++)
	{   
	// Check that current character matches current character of the requested format.
	    var c = formelement.value.charAt(i);
	    var d = strFormat.charAt(i);
	    // Check if current character should be a digit.
	    if (d == "#")
		{
			if (!isDigit(c))
			{
				return false;
			}
		}
		else
		{
			// Check if character matches exactly the same character in strFormat.
			if (c != d)
			{
				return false;
			}
		}
	}

	// Everything OK, strings match.
	return true;
}
// -------------------------------------------------------------------
// Function:        isDigit
// Parameters:      c - the character you want to validate
// Return values:   True = Character is digit
//                  False = Not a digit
// -------------------------------------------------------------------
function isDigit (c)
{
	return ((c >= "0") && (c <= "9"))
}
// -------------------------------------------------------------------
// Function:        isNum , isOnlyNumeric
// Parameters:      ch - the character you want to validate
// Return values:   True = String contains only numbers
//                  False = String contains no numbers
// -------------------------------------------------------------------


 function isNum(str) 
{
 for (var i = 0; i < str.length; i++) {
  var ch = str.substring(i, i + 1)
  if ((ch < "0" || "9" < ch)) {
   return false
  }
 }
 return true
}
                     
function isOnlyNumeric(textfield) {
 if (isNum(textfield.value))
  return true;
 else
	return false;
}
// -------------------------------------------------------------------
// Function:        isBlank
// Parameters:      formelement - the element you want to validate
// Return values:   True = Element is blank
//                  False = Not blank
// -------------------------------------------------------------------
function isBlank(formelement)
{
	if ((formelement.value.length > 0) && (formelement.value != null) && (formelement.value != "")){
		return false;
	}
	return true;
}
// -------------------------------------------------------------------
// Function:        isMail
// Parameters:      formelement - the element you want to validate
// Return values:   True =Looks like a valid address
//                  False = Not a valid address
// -------------------------------------------------------------------
function isMail(formelement)
{
	var	i=formelement.value.indexOf("@");
	var j=formelement.value.indexOf(".",i);
	var k=formelement.value.indexOf(",");
	var kk=formelement.value.indexOf(" ");
	var jj=formelement.value.lastIndexOf(".")+1;
	var len=formelement.value.length;

	if ((i>0) && (j>(1+1)) && (k==-1) && (kk==-1) && (len-jj >=2) && (len-jj<=3)) 
	{
		return true;
	}
	else
	{
		return false;
	}

}
//******************************************************************
// checks if date passed is valid
// will accept dates in following format:
// isDate(dd,mm,ccyy), or
// isDate(dd,mm) - which defaults to the current year, or
// isDate(dd) - which defaults to the current month and year.
// Note, if passed the month must be between 1 and 12, and the
// year in ccyy format.
//******************************************************************
function isDate (day,month,year) 
{
    var today = new Date();
    year = ((!year) ? y2k(today.getYear()):year);
    month = ((!month) ? today.getMonth():month-1);
    if (!day) return false
    var test = new Date(year,month,day);
    if ( (y2k(test.getYear()) == year) &&
         (month == test.getMonth()) &&
         (day == test.getDate()) )
        return true;
    else
        return false
}
//*******************************************************************
function y2k(number) { return (number < 1000) ? number + 1900 : number; }
//*******************************************************************

//Kontrollerar om namn börjar med bokstäver
function isLetter(letter) {

   if (!letter) return false;
   var iChars = " 1234567890,;.:-_'*^~<>|§½!\"@#£¤$%&/()[]{}=+?´`¨";

   for (var i = 0; i < 1; i++) {
      if (iChars.indexOf(letter.charAt(i)) != -1)
         return false;
   }
   return true;
}                      
//*******************************************************************