// Is the string empty?
function isEmpty(s) {
   return ((s == null) || (s.length == 0))
}

// Returns true if the string s is emtpy or contains a whitespace
function isWhitespace(s) { 
	var i;
	// Whitespace signs.
	var whitespace = " \t\n\r";

    // Is s empty?
    if (isEmpty(s)) return true;

	// Searching through all charaters in s until the first whitespace.
	// If a whitespace exists - return false; if not return true
    for (i = 0; i < s.length; i++)
    {   
        // Is the character a whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

function isEmailAdress(s) {  
    // Is it whitespace
    if (isWhitespace(s)) return false;
    
    // Det må være >= 1 tegn før @, så vi starter
    // å se på tegnet i posisjon 1.
    var i = 1;
    var sLengde = s.length;

    // Ser etter @
    while ((i < sLengde) && (s.charAt(i) != "@")) {
    	i++
    }

    if ((i >= sLengde) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // Ser etter .
    while ((i < sLengde) && (s.charAt(i) != ".")) {
    	i++
    }
    
    // Det må være minst et tegn etter .
    if ((i >= sLengde - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function isInteger(checkStr) {
	var checkOK = "0123456789-+";
  	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){
      		return false;
      	}
  	}

   return true;
}

function isNumber(checkStr) {
	var checkOK = "0123456789.-+";
  	for (i = 0;  i < checkStr.length;  i++){
    	ch = checkStr.charAt(i);
    	if (ch==',')
    		ch='.';
    	for (j = 0;  j < checkOK.length;  j++)
      		if (ch == checkOK.charAt(j))
        		break;
    	if (j == checkOK.length){
      		return false;
      	}
  	}

   return true;
}

function validText(s,sign,formName) {
	var checkOK = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ"+sign+" \t\r\n\f";
 	var allValid = true;
  	for (i = 0;  i < s.length;  i++){
    	ch = s.charAt(i);
    	for (j = 0;  j < checkOK.length;  j++)
      		if (ch == checkOK.charAt(j))
        		break;
    	if (j == checkOK.length){
      		allValid = false;
      		break;
    	}
  	}
  	if (!allValid){
    	alert("Du har skrevet inn et ulovlig tegn, "+ch+", i '"+formName+"'");
    	return false;
  	}
  	return true;	
}

function rightLength(s,min,max,formName) {
	if (s.length < min) {
		alert("Innholdet i '"+formName+"' er ikke langt nok. Lenge må være minimum "+min+" og maks "+max+" tegn.");
		return false;
	}else if(s.length > max){
		alert("Innholdet i '"+formName+"' er for langt. Lenge må være minimum "+min+" og maks "+max+" tegn.");
		return false;
	}else{
		return true;
	}
}