// Check whether string s is empty.
function isEmpty(s) {
	return ( (s==null) || (s.length==0) );
}

// Returns true if string s is empty or
// whitespace characters only.
function isWhitespace (s) {
	var i;

    // Is s empty?
    if( isEmpty(s) ) {
		return true;
	}

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.
    var whitespace = " \t\n\r";
    for(i=0; i<s.length; i++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if( whitespace.indexOf(c)==-1 ) {
			return false;
		}
    }

    // All characters are whitespace.
    return true;
}

// isEmail (STRING s [, BOOLEAN emptyOK])
//
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
function isEmail(s) {
	if( isEmpty(s) ) {
       return false;
    }

    // is s whitespace?
    if( isWhitespace(s) ) {
		return false;
	}

    // there must be >= 1 character before @, so we
    // start looking at character position 1
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while( (i<sLength) && (s.charAt(i)!="@") ) {
		i++;
    }

    if( (i>=sLength) || (s.charAt(i)!="@") ) {
		return false;
	}
    else {
		i+=2;
	}

    // look for .
    while( (i<sLength) && (s.charAt(i)!=".") ) {
		i++;
    }

    // there must be at least one character after the .
    if( (i>=sLength-1) || (s.charAt(i)!=".") ) {
		return false;
	}
    return true;
}

  	
// ------------------------------------------------------------------------------------------
// contact us rountine
// by: alho
function checkContactUs() {
    var valid = true;
    var form = document.forms["contactusform"];
    var msg = "";
    var i = 1;
    
    if (form.subject.value == "0") {
        valid = false;
        msg += i+". Please select a subject\n\n";
        i++;
    }
    
    if (isWhitespace(form.name.value)){
        valid = false;
        msg += i+". Please enter a name\n\n";
        i++;
    }
    
    if (isWhitespace(form.comment.value)){
        valid = false;
        msg += i+". Please fill in a message\n\n";
        i++;
    }
    
    if (!form.email.value.match(/[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})/)){
        valid = false;
        msg += i+". Please fill in a valid Email\n\n";
        i++;
    }
    
//  if (isWhitespace(form.phone.value)/* && !isLocalPhone(form.phone.value)*/){
//        valid = false;
//        msg += i+". Please enter a valid phone No. (e.g. 21234567)\n";
//        i++;
//    }
    
    if (valid) {
        form.submit();
    } else {
        alert(msg);
    }
}

function isLocalPhone(v){
    return v.match(/[0-9]{8}/);
}

//------------------------------------------------------------------------------------------

// Returns true if character c is a digit
// (0 .. 9).
function isDigit (c) {
	return( (c>="0") && (c<="9") );
}

// isInteger (STRING s )
//
// Returns true if all characters in string s are numbers.
//
// Accepts non-signed integers only. Does not accept floating
// point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// EXAMPLE FUNCTION CALL:     RESULT:
// isInteger ("5")            true
// isInteger ("")             false
// isInteger ("-5")           false
function isInteger (s) {
	var i;

   	if( !isEmpty(s) ) {
    		// 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(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;
    	}
    	return false;
}


// isSignedInteger (STRING s [, BOOLEAN emptyOK])
//
// Returns true if all characters are numbers;
// first character is allowed to be + or - as well.
//
// Does not accept floating point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// EXAMPLE FUNCTION CALL:          RESULT:
// isSignedInteger ("5")           true
// isSignedInteger ("")            false
// isSignedInteger ("-5")          true
// isSignedInteger ("+5")          true
function isSignedInteger (s) {
	if( !isEmpty(s) ) {
        	var startPos = 0;
        	
        	// skip leading + or -
        	if( (s.charAt(0)=="-") || (s.charAt(0)=="+") ) {
           		startPos = 1;
           	}
        	return ( isInteger(s.substring(startPos, s.length)) );
	}
	return false;
}


// isNonnegativeInteger (STRING s [, BOOLEAN emptyOK])
//
// Returns true if string s is an integer >= 0.
function isNonnegativeInteger (s) {

    // 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 false for
    //        empty strings
    //    ii) this is a number >= 0
    return( !isEmpty(s) && isSignedInteger(s) && (parseInt(s)>=0) );
}

// Returns true if string s is empty or
// whitespace characters only.
function isWhitespace(s) {
	
	// whitespace characters
	var whitespace = " \t\n\r";

	var i;

    	// Is s empty?
    	if( isEmpty(s) ) {
    		return true;
    	}

    	// Search through string's characters one by one
    	// until we find a non-whitespace character.
    	// When we do, return false; if we don't, return true.
    	for(i=0; i<s.length; i++) {
        	// Check that current character isn't whitespace.
        	var c = s.charAt(i);
        	if( whitespace.indexOf(c)==-1 ) {
        		return false;
        	}
    	}

	// All characters are whitespace.
    	return true;
}


// Returns true if character c is an English letter
// (A .. Z, a..z).
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.
function isLetter(c) {
	return( ((c>="a") && (c<="z")) || ((c>="A") && (c<="Z")) );
}


// isAlphabetic (STRING s [, BOOLEAN emptyOK])
//
// Returns true if string s is English letters
// (A .. Z, a..z) only.
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.
function isAlphabetic(s) {
	var i;
	
    	if( !isEmpty(s) ) {
    		// Search through string's characters one by one
    		// until we find a non-alphabetic character.
    		// When we do, return false; if we don't, return true.
		for(i=0; i<s.length; i++) {
		        
		        // Check that current character is letter.
		        var c = s.charAt(i);
		        if( !isLetter(c) ) {
		        	if( !isWhitespace(c) ) {
		        		return false;
		        	}
		    	}
		}
    		// All characters are letters.
    		return true;
	}
	return false;
}
