//validate email address
function emailCheck (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. */

alert("Email address seems incorrect (check @ and .'s)");
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 txt_email 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;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid

alert("The Email doesn't seem to be 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) {
alert("Destination IP address is invalid!");

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.");

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.");

return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
alert("This address is missing a hostname!");


return false;
}

// If we've gotten this far, everything's valid!
return true;
}


//Check if string is empty
function isEmpty(stringValue) {
	var regExp = / /g;
		stringValue = stringValue.replace(regExp,'');
		if (stringValue.length <= 0) {
			return true;
		} else {
			return false;	
		}
		
}

//Check if string is empty
function isString(stringValue) {
	var regExp = / /g;
		stringValue = stringValue.replace(regExp,'');
		if (stringValue.length <= 0) {
			return true;
		} else {
			return false;	
		}
		
}

function IsNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }

	return IsNumber;
 }
   
function browserInfo () {
		var nVer = navigator.appVersion;
		var nAgt = navigator.userAgent;
		var browserName  = '';
		var fullVersion  = 0; 
		var majorVersion = 0;
		
		// In Internet Explorer, the true version is after "MSIE" in userAgent
		if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
			 browserName  = "Microsoft Internet Explorer";
			 fullVersion  = parseFloat(nAgt.substring(verOffset+5));
			 majorVersion = parseInt(''+fullVersion);
		}
				// In Firefox, the true version is after "Firefox" 
		else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
			 browserName  = "Firefox";
			 fullVersion  = parseFloat(nAgt.substring(verOffset+8));
			 majorVersion = parseInt(''+fullVersion);
		}

		// In Opera, the true version is after "Opera" 
		else if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
		 browserName  = "Opera";
		 fullVersion  = parseFloat(nAgt.substring(verOffset+6));
		 majorVersion = parseInt(''+fullVersion);
		}
		
		
		// In most other browsers, "name/version" is at the end of userAgent 
		else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < (verOffset=nAgt.lastIndexOf('/')) ) 
		{
		 browserName  = nAgt.substring(nameOffset,verOffset);
		 fullVersion  = parseFloat(nAgt.substring(verOffset+1));
		 if (!isNaN(fullVersion)) majorVersion = parseInt(''+fullVersion);
		 else {fullVersion  = 0; majorVersion = 0;}
		}
		
		// Finally, if no name and/or no version detected from userAgent...
		if (browserName.toLowerCase() == browserName.toUpperCase()
			|| fullVersion==0 || majorVersion == 0 )
		{
		 browserName  = navigator.appName;
		 fullVersion  = parseFloat(nVer);
		 majorVersion = parseInt(nVer);
		}	
		
		/*document.write('Browser name  = '+browserName+'<br>');
		document.write('Full version  = '+fullVersion+'<br>');
		document.write('Major version = '+majorVersion+'<br>');
		document.write('navigator.appName = '+navigator.appName+'<br>');
		document.write('navigator.userAgent = '+navigator.userAgent+'<br>');*/
		
}//end function assignCSS

function addTableRow () {
	
	
		var rowRef = document.getElementById('tblEmailInfo').insertRow(document.getElementById('tblEmailInfo').rows.length);
		rowRef.align = "center";
		rowRef.valign = "middle";
		var a=rowRef.insertCell(0);
		var b=rowRef.insertCell(1);
		var c=rowRef.insertCell(2);
		var d=rowRef.insertCell(3);

		a.innerHTML="<input name='email[]' type='text' id='email[]' size='17' class='flat'>";
		b.innerHTML="<input name='field1[]' type='text' id='field1[]' size='17' class='flat'>";
		c.innerHTML="<input name='field2[]' type='text' id='field2[]' size='17' class='flat'>";
		d.innerHTML="<input name='field3[]' type='text' id='field3[]' size='17' class='flat'>";

}

/*function validateRows () {
		//alert('validate emails');
		//alert('validate emails'+document.frm_sign_up.elements["email[]"].length);
		for ( var i=0; i<document.frm_sign_up.elements["email[]"].length; i++) {
				var email = document.frm_sign_up.elements["email[]"][i].value;
				var field1 = document.frm_sign_up.elements["field1[]"][i].value;
				var field2 = document.frm_sign_up.elements["field2[]"][i].value;
				var field3 = document.frm_sign_up.elements["field3[]"][i].value;
/*				alert("email: "+email);
				alert("isEmpty: "+isEmpty(email));
				alert("field1: "+field1);
				alert("field2: "+field2);
				alert("field3: "+field3);
				
				if (isEmpty(email)) {
					if (!(isEmpty(field1)) || !(isEmpty(field2)) || !(isEmpty(field3))) {
						alert("Please enter email address!");
						document.frm_sign_up.elements["email[]"][i].focus();
						return false;
					}
				} else {
						if (emailCheck(email)==false) {
							document.frm_sign_up.elements["email[]"][i].focus();
							return false;
						}
				}
				
				var col1Value = "";
				//alert("col1 length"+document.frm_sign_up.col1.length);
				if (!isEmpty(field1)) {
					for (var j=0; j<document.frm_sign_up.col1.length; j++) {
						if (document.frm_sign_up.col1[j].selected) {
							col1Value = document.frm_sign_up.col1[j].value;
						}
						
					}
				}
				var col2Value = "";
				//alert("col1 length"+document.frm_sign_up.col1.length);
				if (!isEmpty(field2)) {
					for (var j=0; j<document.frm_sign_up.col2.length; j++) {
						if (document.frm_sign_up.col2[j].selected) {
							col2Value = document.frm_sign_up.col2[j].value;
						}
						
					}
				}
				var col3Value = "";
				//alert("col1 length"+document.frm_sign_up.col1.length);
				if (!isEmpty(field3)) {
					for (var j=0; j<document.frm_sign_up.col3.length; j++) {
						if (document.frm_sign_up.col3[j].selected) {
							col3Value = document.frm_sign_up.col3[j].value;
						}
						
					}
				}
				/*alert("col1 value: "+col1Value);
				alert("col2 value: "+col2Value);
				alert("col3 value: "+col3Value);
				
				if (isEmpty(col1Value) && ((!isEmpty(col2Value)) && (!isEmpty(col3Value))) ) {
					if (col2Value == col3Value) {
						alert("Column names must be unique");
						document.frm_sign_up.elements["field3[]"][i].focus();
						return false;
					}
				
				} else if (isEmpty(col2Value) && ((!isEmpty(col1Value)) && (!isEmpty(col3Value))) ) {
				
					if (col1Value == col3Value) {
						alert("Column names must be unique");
						document.frm_sign_up.elements["field3[]"][i].focus();
						return false;
					}
				} else if (isEmpty(col3Value) && ((!isEmpty(col1Value)) && (!isEmpty(col2Value))) ) {
				
						if (col1Value == col2Value) {
							alert("Column names must be unique");
							document.frm_sign_up.elements["field2[]"][i].focus();
							return false;
						}
				} else if ((!isEmpty(col3Value)) && ((!isEmpty(col1Value)) && (!isEmpty(col2Value))) ) {
						if (col1Value == col2Value) {
							alert("Column names must be unique");
							document.frm_sign_up.elements["field2[]"][i].focus();
							return false;
						} else if (col1Value == col3Value) {
							alert("Column names must be unique");
							document.frm_sign_up.elements["field3[]"][i].focus();
							return false;
						} else if (col2Value == col3Value) {
							alert("Column names must be unique");
							document.frm_sign_up.elements["field3[]"][i].focus();
							return false;
						}
				}
				
		}
		return true;
	
	}*/

function getDropDownListValue (dropDownListId) {
	var dropdownIndex = document.getElementById(dropDownListId).selectedIndex;
	var dropdownValue = document.getElementById(dropDownListId)[dropdownIndex].value;	
	
	return dropdownValue;
}

function getDropDownListText (dropDownListId) {
	var dropdownIndex = document.getElementById(dropDownListId).selectedIndex;
	var dropdownText = document.getElementById(dropDownListId)[dropdownIndex].text;	
	
	return dropdownText;
}


function is_valid_url(s) {
		var regexp = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
       return regexp.test(s);
}


//CountWords(field,false, false);
function CountWords (this_field, show_word_count, show_char_count) {

if (show_word_count == null) {
show_word_count = true;
}
if (show_char_count == null) {
show_char_count = false;
}
var char_count = this_field.value.length;
var fullStr = this_field.value + " ";
var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
var splitString = cleanedStr.split(" ");
var word_count = splitString.length -1;
if (fullStr.length <2) {
word_count = 0;
}
if (word_count == 1) {
wordOrWords = " word";
}
else {
wordOrWords = " words";
}
if (char_count == 1) {
charOrChars = " character";
} else {
charOrChars = " characters";
}
if (show_word_count & show_char_count) {
alert ("Word Count:\n" + "    " + word_count + wordOrWords + "\n" + "    " + char_count + charOrChars);
}
else {
if (show_word_count) {
alert ("Word Count:  " + word_count + wordOrWords);
}
else {
if (show_char_count) {
alert ("Character Count:  " + char_count + charOrChars);
      }
   }
}
return word_count;
}


