function selectOption( selectBox, valueToSearch )
{
    if( selectBox == null ){
      return;
    }
    with( selectBox )
    {
        for( var i = 0; i < selectBox.length; i ++ )
        {
            if( options[i].value == valueToSearch )
                selectedIndex=i;
        }
    }    
}

function validateSelect( selectBox ) {
    if( selectBox.selectedIndex == 0 ) return false;
		else return true;
}

/**
 * Validates whether one of a number of radio items is filled in.
 */
function validateRadio( radioButton ) {
{
  var radioSelected = false;

  // Loop from zero to the one minus the number of radio button selections
  for (var i = 0; i < radioButton.length; i++) {
	  if (radioButton[i].checked)
		  radioSelected = true; 
	  }
  }
  if (!radioSelected) return false;
	else return true;
}

function validateText(s)
{
  if( s == null || s == "" ) return false;
	//test for a string
	if (s.length > 0)
	{
		return true;
	}
	return false;
}

function validateEmail(s)
{
	//Test for a string
    if (s.length > 0)
    {
        // Return false if e-mail field does not contain a '@' and '.' .
        if (s.indexOf ('@',0) == -1 || s.indexOf ('.',0) == -1)
        {
            return false;
        }
        return true;
    }
    return false;
}

function validateTeleNum(s)
{
    //Test for a string
    if (s.length == 10)
    {
        for (i = 0;  i < s.length;  i++)
        {
            ch = s.charAt(i);
            if(! validChar(ch))
            {
                return false;
            }
        }
        return true;
	}
	return false;
}

function validChar(c)
{
    var checkOK = "0123456789";
    var ret  = false;

    for (j = 0;  j < checkOK.length;  j++)
    {
        if (c != checkOK.charAt(j))
        {
            continue;
        }
        else
        {
            ret = true;
            break;
        }
    }
    return ret;
}

function isInteger(value) {
  return (parseInt(value) == value);
}

function removeSpaces(s)
{
    var returnValue = "";
    for (i = 0;  i < s.length;  i++)
    {
        ch = s.charAt(i);
        if( !( ch == ' ' ) )
            returnValue += ch;
    }
    return returnValue;
}


/*
*	Piet added this, see if we like it.
*/
/* generic formsubmitter, takes unlimited arguments.
 * 1st argument is formnumber on page (0 if only one form is present)
 * 2nd argument is name of field to set before submitting
 * 3rd argument is value the field is to be set to.
 * 4th and 5th are another field and its value, etc....
 * if parameter is a string, this is taken as the DOM ID of the form.
 */
function submitForm (form){
	if( isInteger( form ) )
		frm = document.forms[form];
	else
		frm = new getObj( form ).obj;
		
	for (var i=1; i<arguments.length; i=i+2) {
		eval("input = frm."+arguments[i]);
		input.value = arguments[i+1];
	}
	return true;
}

// validation data of admin_faq_overview.tpl.php
function checkAdminRenameCatForm(){
	if (checkFilled('categoryName') == 0) return submitForm(0);
	else return false;
}

// validation data of admin_new_faq.tpl.php
function checkAdminNewFaqForm(){
	if (checkFilled('question') + checkFilled('answer') == 0) return submitForm(0);
	else return false;
}

/******
 * miscellaneous 
 ******/

// get radio button value
function getRadio(frm, str)
{
	radio = document.forms[frm].elements[str];
	for( i=0; i<radio.length; ++i){
		if( radio[i].checked) return radio[i].value;
	}
	// default (non selected)
	return -1;
}
 
// Get an element by its id-tag in different browsers
function getById(id){
	// Netscape 4
	if(document.layers){ 
		return document.layers[id]; 
	} 
	// IE4
	else if(document.all && !document.getElementById){ 
		return document.all[id];
	}
	// W3C - Explorer 5+ and Netscape 6+ 
	else { 
		return document.getElementById(id); 
	}
}

/******
 * Stringvalidators
 *****/

function checkPhoneNr(str){
	inp = getById(str).value;
	if (arguments[1] && (inp=="" || inp==null)){
		(getById(str+"_c")).style.display = "inline";
		return 1;
	}
	
	valid = "0123456789-+() ";
	for (i=0;i<inp.length;i++){
		if ((valid.indexOf(inp.charAt(i)) < 0)){ 
			(getById(str+"_c")).style.display = "inline";
			return 1;
		}
	}
	(getById(str+"_c")).style.display = "none";
	return 0;
}


function checkFilled(str){
	inp = (getById(str)).value;
	if (inp=="" || inp==null){
		(getById(str+"_c")).style.display = "inline";
		return 1;
	}
	(getById(str+"_c")).style.display = "none";
	return 0;
}

function checkCorrectPass(str1, str2){
	inp1 = getById(str1).value;
	inp2 = getById(str2).value;
	if (inp1 != inp2 || inp1=="" || inp1.indexOf(" ")>-1){
		(getById(str1+"_c")).style.display = "inline";
		return 1;
	}
	(getById(str1+"_c")).style.display = "none";
	return 0;
}

function checkNotSame(str1, str2){
	inp1 = getById(str1).value;
	inp2 = getById(str2).value;
	if (inp1 == inp2){
		(getById(str1+"_c")).style.display = "inline";
		return 1;
	}
	(getById(str1+"_c")).style.display = "none";
	return 0;
}

// wrapper for isValidMail()
function checkMail(str){
	inp = getById(str).value;
	
	if (!isValidMail(inp)){
		(getById(str+"_c")).style.display = "inline";
		return 1;
	}
	(getById(str+"_c")).style.display = "none";
	return 0;
}

function isValidMail(emailStr){
	
	/* The following variable tells the rest of the function whether or not
	to verify that the address ends in a two-letter country or well-known
	TLD.  1 means check it, 0 means don't. */
	
	var checkTLD=1;
	
	/* The following is the list of known TLDs that an e-mail address must end with. */
	
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	
	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */
	
	var emailPat=/^(.+)@(.+)$/;
	
	/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ] */
	
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	
	/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed.*/
	
	var validChars="\[^\\s" + specialChars + "\]";
	
	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */
	
	var quotedUser="(\"[^\"]*\")";
	
	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */
	
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	
	/* The following string represents an atom (basically a series of non-special characters.) */
	
	var atom=validChars + '+';
	
	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */
	
	var word="(" + atom + "|" + quotedUser + ")";
	
	// The following pattern describes the structure of the user
	
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	
	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */
	
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	
	/* Finally, let's start trying to figure out if the supplied address is valid. */
	
	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */
	
	var matchArray=emailStr.match(emailPat);
	
	if (matchArray==null) {
	
	/* Too many/few @'s or something; basically, this address doesn't
	even fit the general mould of a valid e-mail address. */
	
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	
	// Start by checking that only basic ASCII characters are in the strings (0-127).
	
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			return false;
	   	}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			return false;
	   	}
	}
	
	// See if "user" is valid 
	
	if (user.match(userPat)==null) {
	
	// user is not valid
	
		return false;
	}
	
	/* if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */
	
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
	
	// this is an IP address
	
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return false;
	   		}
		}
		return true;
	}
	
	// Domain is symbolic name.  Check if it's valid.
	 
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			return false;
	   	}
	}
	
	/* domain name seems valid, but now make sure that it ends in a
	known top-level domain (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */
	
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
	domArr[domArr.length-1].search(knownDomsPat)==-1) {
		return false;
	}
	
	// Make sure there's a host name preceding the domain.
	
	if (len<2) {
		return false;
	}
	
	// If we've gotten this far, everything's valid!
	return true;
	}
