var i;
var charok = new Array(41);
for (i=0;i<26;i++) charok[i]=String.fromCharCode(i+97);			// a-z
for (i=0;i<10;i++) charok[i+26]=String.fromCharCode(i+48);	// 0-9
charok[36]=String.fromCharCode(46); 	// .
charok[37]=String.fromCharCode(45); 	// -
charok[38]=String.fromCharCode(95); 	// _
charok[39]=String.fromCharCode(38);		// &
charok[40]=String.fromCharCode(64); 	// @

function pos2array(str,search,result)
{ var pospt = str.indexOf(search);
	var newstr = str.substring(pospt+1,str.length);
	if (pospt!=-1)
	{ result[result.length] = (result[result.length-1])?pospt+result[result.length-1]+1:pospt;
		pos2array(newstr,search,result);
	}			
}

function checkchar(crt,fields)
{ for (i=0;i<fields;i++) if (charok[i]==crt) return true;
	return false;
}

function checkemail(email)
{	var email = email.toLowerCase();
	var posat = new Array();
  var pospt = new Array();
	// check email length (minimum of 5 chars)
	if (email.length<5) return false;
	// @ and . present and no @ before the .
	if (email.indexOf('@')==-1||email.indexOf('.')==-1) return false;
	// check if only one @ character
	pos2array(email,'@',posat);
	if (posat.length>1) return false;
	// check if . occurs behind the @
	if (email.substring(posat[0],email.length).indexOf('.')==-1) return false;
	// rejects if "unknown" occurs in the email address
	if (email.indexOf('unknown')!=-1) return false;
	// at least 2 chars after the last .
	if (email.length - email.lastIndexOf('.') <= 2) return false;
	//  check on valid characters
  if (!checkchar(email.charAt(0),36) || 
			!checkchar(email.charAt(email.length-1),36) ||
			!checkchar(email.charAt(posat[0]-1),36) || 
			!checkchar(email.charAt(posat[0]+1),36)) return false;
			pos2array(email,'.',pospt);
			for (j=0;j<pospt.length;j++) if (!checkchar(email.charAt(pospt[j]-1),36) || 
																			 !checkchar(email.charAt(pospt[j]+1),36)) return false;
	for (j=0;j<email.length;j++) if (!checkchar(email.charAt(j),41)) return false;
	return true;
}

function isNumeric(elem)
{ var numericExpression = /^[0-9]+$/;
  if(elem.match(numericExpression)) return true;
	else return false;
}

// remove spaces
function trim(value) 
{ value = value.replace(/^\s+/,''); 
  value = value.replace(/\s+$/,'');
  return value;
}

// Replaces all instances of the given substring.
String.prototype.replaceAll = function(strTarget, strSubString)
{	var strText = this;
	var intIndexOfMatch = strText.indexOf(strTarget);
	while (intIndexOfMatch != -1)
	{	strText = strText.replace(strTarget, strSubString )
		intIndexOfMatch = strText.indexOf(strTarget);
	}
	return( strText );
}
