/***************************************************************

Copyright: OSI Codes
Author: Nate Lee, nate@osicodes.com

These functions are written by OSI Codes and is property of OSI Codes.
Modification or re-use of this code must comply with the OSI Codes
Software License Agreement.  A copy of the License Agreement is
located with the Software package.

***************************************************************/

function replace(target, oldT, newT)
{
	var work = target;
	var ind = 0;
	var next = 0;
	
	while((ind = work.indexOf(oldT,next)) >= 0)
	{
		target = target.substring(0,ind) + newT + target.substring(ind+oldT.length,target.length);
		work = work.substring(0,ind) + newT + work.substring(ind+oldT.length,work.length);

		next = ind + newT.length;
		if(next >= work.length) { break; }
	}
	return target;
}

function unique()
{
	var date = new Date();
	var seconds = date.getSeconds();
	var minutes = date.getMinutes();
	var hours = date.getHours();
	var day = date.getDay();
	var unique = day+""+""+hours+""+minutes+""+seconds;
	return unique;
}

function letternumber(e)
{
	var key;
	var keychar;

	if (window.event)
	key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;

	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();

	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
		return true;

	else if ((("abcdefghijklmnopqrstuvwxyz0123456789_-").indexOf(keychar) > -1))
		return true;
	else
		return false;
}

function numbersonly(e)
{
	var key;
	var keychar;

	if (window.event)
	key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;

	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();

	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
		return true;

	else if ((("0123456789").indexOf(keychar) > -1))
		return true;
	else
		return false;
}

function email_check( emailStr )
{

	var checkTLD=1;

	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;

	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";

	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

	var matchArray=emailStr.match(emailPat);
	if (matchArray==null)
	{
		alert("Email address seems incorrect (check @ and .'s)");
		return false;
	}

	var user=matchArray[1];
	var domain=matchArray[2];

	for (i=0; i<user.length; i++)
	{
		if (user.charCodeAt(i)>127)
		{
			alert("Ths username contains invalid characters.");
			return false;
   		}
	}

	for (i=0; i<domain.length; i++)
	{
		if (domain.charCodeAt(i)>127)
		{
			alert("Ths domain name contains invalid characters.");
			return false;
   		}
	}

	if (user.match(userPat)==null)
	{
		alert("The username doesn't seem to be valid.");
		return false;
	}

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null)
	{
		for (var i=1;i<=4;i++)
		{
			if (IPArray[i]>255)
			{
				alert("Destination IP address is invalid!");
				return false;
   			}
		}
		return true;
	}

	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)
		{
			alert("The domain name does not seem to be valid.");
			return false;
   		}
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1)
	{
		alert("The address must end in a well-known domain or two letter " + "country.");
		return false;
	}

	if (len<2)
	{
		alert("This address is missing a hostname!");
		return false;
	}

	return true;
}
