﻿//1. convert string's case
//2. check for patterns
	//a. patterns can check for characters and strip them	(computer changes)
	//b. if string does not match pattern then user should change	(user changes)
//3. display changed field value


//Case Checking:
//=============
function toFirstUpper(id){
	var elem = document.getElementById(id);
		str = elem.value;
		str = str.substr(0,1).toUpperCase() + str.substr(1);
		elem.value = str;
		}
		//*Converts first letter to uppercase

		
function toLowerCase(id){
	var elem = document.getElementById(id);
		str = elem.value;
		str = str.toLowerCase(str);
		elem.value = str;
		}
		//*Converts string to lowercase



//Pattern Checking
//================
function trimString(str) {
	var	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
		while (ws.test(str.charAt(--i)));
		return str.slice(0, i + 1);
		}
		//*Strips white space from string


function formatReturn(id){
	var elem = document.getElementById(id);
	var str = elem.value; 
		str = str.replace(/\r|\n|\r\n/g,' ');
		str = str.replace(/,/g, '');
		elem.value = str;
		}
		//*Strips carriage returns from string


function removeCommas(id){
	var elem = document.getElementById(id);
	var str = elem.value;
		str = str.replace(/,/g,'');
		elem.value = str;
		//*Strips commas from string
		}


//Specific Elements
//Needs to handle case for not filling out form.
function formatPhone(id){
	var elem = document.getElementById(id);
	var phone = elem.value;
		phone = phone.replace(/\D/g,'');
		
		elem.value = "";
		elem.style.background= "";
		
		if(phone.length !=0){
			if(phone.length < 10 || phone.length > 11){
				elem.value = "Please enter a correct number";
				elem.style.background = "#ff9999";
				}
				
			else{
				var phArray = new Array();
	
					if(phone.length == 11 && phone.charAt(0) == '1'){
						for(i=0; i<=phone.length -1; i++){
							phArray[i] = phone.charAt(i+1);
							}
						}
						else{
							for(i=0; i<= phone.length -1; i++){
								phArray[i] = phone.charAt(i);
								}
							}

					
					var area = phArray[0] + phArray[1] + phArray[2];
					var pref = phArray[3] + phArray[4] + phArray[5];
					
					var suff = phArray[6] + phArray[7] + phArray[8] + phArray[9];
					
					phone = "(" + area + ") " + pref + "-" + suff;
					
					elem.value = phone;
					}
				}
	}



//Initialize Fields, Clear Fields
function clearFormField(id){
	var elem = document.getElementById(id)
	var lbl = document.getElementById(id + "_LBL")

	var str = elem.value;
	
		if (str.value == str.defaultValue){
			elem.style.background = "#FFF";
			elem.value = "";
			elem.style.fontStyle = "normal";
			elem.style.color = "black";

			lbl.value = "";
			}
		else
			elem.style.background = "#FFF";
			lbl.value = "";

		}





//Putting it all together
//The submit function
function submitForm(){
	for(i=0; i<= document.form1.length; i++){
		name = document.getElementsByTagName('input')

		for(j=0; j<=name.length; j++){
			//var str = toFirstUpper(name[j].value);
			//window.alert(name[j] + str);
			}
		}
}

var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var alphaExp = "/^[a-zA-Z]+$/"


// initialize variables for different RegExp
// we need a special characters string
// we need a alphanumeric string

// dynamically construct with RegExp() if pattern is not yet known


function checkFormField(id){
	var elem = document.getElementById(id);
	var lbl = document.getElementById(id + "_LBL");	

	var str = trimString(elem.value);

		if (str == ""){
			elem.style.background = "#ff9999";
			lbl.value = "* Please Fix Required Field";
			}
		else
			if (id == "AC_EMAIL"){
				return emailCheck(str, lbl, id);
				}
			else
				for (i=0; i<=2; i++){
					for (j=0; j<=10; j++){
						if (str.charAt(i) == specialChars.charAt(j)){
							window.alert("Please fix your field" + str.charAt(i) + "=" + specialChars.charAt(j));
							elem.style.background = "#761414";
							}
						}
					}
			}





function checkInput(fname, lname, email) {
	v_fname	= document.getElementById(fname).value;
	v_lname	= document.getElementById(lname).value;
	v_email	= document.getElementById(email).value;

	var v_message = "";
	var n_errorcount = 0;

	v_message = "Please check the following information: \n\n";
	
//	if (v_fname == "" ) {
//		n_errorcount = n_errorcount + 1;
//	  	v_message =v_message + " (" + n_errorcount + ") " + " Please enter First Name\n";
//	   }
	
//	if (v_lname == "") {
//		n_errorcount = n_errorcount + 1;
//	  	v_message =v_message + " (" + n_errorcount + ") " + " Please enter Last name\n";
//	   }
	   

	if (v_email == "") {
		n_errorcount = n_errorcount + 1;
	  	v_message =v_message + " (" + n_errorcount + ") " + " Please enter Email Address\n";
	  	document.getElementById(email).focus();
	   }
	   			
	if (n_errorcount == 0) {
		return emailCheck(v_email, 'AC_EMAIL_LBL', email);
		return true;
		}
	else {
			alert (v_message);
			return false;
		}
		

  	window.alert(v_message);		
	}







function emailCheck(emailStr, label, id){
	
	if (emailStr != ""){
		/* 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)");
			label.value = "* Email address seems incorrect (check @ and .'s)";
			document.getElementById(id).focus();
			return false;
		}
		var user=matchArray[1];
		var domain=matchArray[2];

		// See if "user" is valid 
		if (user.match(userPat)==null) {
			// user is not valid
			alert("The username doesn't seem to be valid.");
			label.value = "* The username doesn't seem to be valid.";
			document.getElementById(id).focus();
			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!");
					label.value = "* Destination IP address is invalid!";
					document.getElementById(id).focus();
				return false;
				}
			}
			return true
			}

		// Domain is symbolic name
		var domainArray=domain.match(domainPat);
		if (domainArray==null) {
			alert("The domain name doesn't seem to be valid.");
			label.value = "* The domain name doesn't seem to be valid.";
			document.getElementById(id).focus();
			return false;
			}
		/* domain name seems valid, but now make sure that it ends in a
		three-letter word (like com, edu, gov) or a two-letter word,
		representing country (uk, nl), and that there's a hostname preceding 
		the domain or country. */

		/* Now we need to break up the domain to get a count of how many atoms
		it consists of. */
		var atomPat=new RegExp(atom,"g");
		var domArr=domain.match(atomPat);
		var len=domArr.length;
		if (domArr[domArr.length-1].length<2 || 
			domArr[domArr.length-1].length>3) {
		// the address must end in a two letter or three letter word.
		alert("The address must end in a three-letter domain, or two letter country.");
		label.value = "* The address must end in a three-letter domain, or two letter country.";
		document.getElementById(id).focus();
		return false
		}

		// Make sure there's a host name preceding the domain.
		if (len<2) {
			var errStr="This address is missing a hostname!"
			document.getElementById(id).focus();
			alert(errStr);
			label.value = "* This address is missing a hostname!";
			return false;
			}

		// If we've gotten this far, everything's valid!
		return true;
		}
		}