//*************************************************************************************************
//Program Name	:	function.js
//Description	:	
//					
//Created By	:	Zhonghai Yu on 8/23/01
//					
//Modified By	:	
//				:	
//
//***************************************************************************************************

function popsubcat(list,sublist,subselected){
	var index = 0;
	var selected = "";
	if (list.type == "radio")	{ 
		selected = list.value;
	//	alert("I am here! " + selected)
	}else{
		if (list.type == "select-one"){
			var size = list.length;
		//	alert(size)
			for (var i=0; i<size; i++){
				if(list.options[i].selected){
					selected = list.options[i].value;
					break;
				}
			}
		}else{
			selected = list
		}
	}
	if (selected != ""){
	//	alert (mainCat.length)
		for (var i=0; i<mainCat.length; i++){
			if (mainCat[i] == selected){
				index = i;
				break;
			}
		}
		sublist.length = 1;
	//	alert("selected sub cat: " + subCat[index].length)
		for (var i=0;i<subCat[index].length; i++){
			sublist.length++;
			sublist.options[i+1].value = subCat[index][i];
			sublist.options[i+1].text = subCat[index][i];
			if (subCat[index][i] == subselected){
				sublist.options[i+1].selected = true;
			}
		}
	}else{
		sublist.length = 1;
	}
}

// whitespace characters
var whitespace = " \t\n\r";
/****************************************************************/

// This function determines if the string passed in is a valid
// US zip code.  It accepts either ##### or #####-####.  If the
// string is valid, it returns true, else false.

function isZipcode(strZip)
{
	var s = new String(strZip);

	if (s.length != 5 && s.length != 10)
		// inappropriate length
		return false;


	for (var i=0; i < s.length; i++)
		if ((s.charAt(i) < '0' || s.charAt(s) > '9') && s.charAt(i) != '-')
			return false;

	return true;
}

/****************************************************************/

// isEmail (FieldName [, 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
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail (FieldName)
{	var defaultEmptyOK = true;
	var s = Trim(FieldName.value);
	if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) 
	{	alert("Invalid Email: It can not be whitespace.")
	//	FieldName.focus();
		FieldName.value="";
		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) != "@"))
	{	alert("You need to enter a valid email address.")
	//	FieldName.focus();
		FieldName.value="";
		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) != "."))
	{	alert("You need to enter a valid email address.")
	//	FieldName.focus();
		FieldName.value="";
		return false;
	}
    else return true;
}

/****************************************************************/

// 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.

    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;
}

/****************************************************************/

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

/**********************************************************************************
Function	: LTrim(String)
Description	: This function is used to Trim the Left spaces from a String.
Arguments	: String
Returns		: A string that has the left spaces trimed or an empty string
Modified	:
***********************************************************************************/	
	function LTrim(String){
		String += "";		// Force argument to string.
		if(String.length == 0){
			return(String);
		}
		var Count = 0;
		for(Count = 0;Count < String.length;Count++){
			if(!(String.charAt(Count) == " ")){
				return(String.substring(Count,String.length));
			}
		}
		return("");
	}

/**********************************************************************************
Function	: RTrim(String)
Description	: This function is used to Trim the Right spaces from a String.
Arguments	: String
Returns		: A string that has the Right spaces trimed or an empty string
Modified	:
***********************************************************************************/	
	function RTrim(String){
		String += "";		// Force argument to string.
		if(String.length == 0){
			return(String);
		}
		var Count = 0
		for(Count = String.length -1;Count >= 0;Count--){
			if(!(String.charAt(Count) == " ")){
				return(String.substring(0,Count + 1));
			}
		}
		return("");
	}

/**********************************************************************************
Function	: Trim(String)
Description	: This function uses LTrim() and RTrim Functions to Trim the spaces out
				of a string
Arguments	: String
Returns		: A string that has spaces trimed or an empty string
Modified	:
***********************************************************************************/	
	function Trim(String){
		String += "";		// Force argument to string.
		return(RTrim(LTrim(String)));
	}

function mypopup(url)
 {
   mywindow = window.open(url,
  "mywindow","location=0,status=0,scrollbars=1,width=400,height=300");
  mywindow.moveTo(0,0);
 } 

 function mypopup2(url)
 {
   mywindow = window.open(url,
  "mywindow","location=0,status=0,scrollbars=1,width=400,height=300");
  mywindow.moveTo(0,0);
 } 



function emailCheck (FiledName) {

/* 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 emailStr = FiledName.value;
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. */

alert("Email address seems incorrect (check @ and .'s)");
FiledName.value = "";
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) {
alert("Ths username contains invalid characters.");
FiledName.value = "";
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("Ths domain name contains invalid characters.");
FiledName.value = "";
return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid

alert("The username doesn't seem to be valid.");
FiledName.value = "";
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) {
alert("Destination IP address is invalid!");
FiledName.value = "";
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) {
alert("The domain name does not seem to be valid.");
FiledName.value = "";
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) {
alert("The address must end in a well-known domain or two letter " + "country.");
FiledName.value = "";
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
alert("This address is missing a hostname!");
FiledName.value = "";
return false;
}

// If we've gotten this far, everything's valid!
return true;
}
