<!-- Activate Cloaking Device

/*
-----------------------------------------------------------------------------------
Author:	Ahmad Al-Asad
Date:		March, 2002
Description:	This script loops through all the elements of the form bypassing
		all the fields that are not validated (i.e., hidden, submit, image, 
		etc. fields).
		This script expects a naming convention within the elements of the
		form (see checkSubmission function).
		This script was written while using cgiemail and therefore making 
		certain settings at the successful validation of the form.
		With a few tweaks, I'm sure this could work on anything else.
		The Document Object Model (DOM) used is very basic and should be
		compatible on generation 3+ browsers.  Although, I think switch statements
		are for generation 4+ browsers ... Might wanna check that out.
		This script could use some cleaning up, but I'll leave that to
		another version update.
		Here're examples of lines needed within the html file:

		<BODY BGCOLOR="#ffffff" TOPMARGIN="0" onLoad="Reset('required-name*1')">

		<FORM NAME="inquiry" METHOD="post" ACTION="">
		<INPUT TYPE=HIDDEN NAME="success">
		<INPUT TYPE=HIDDEN NAME="required-to">
		<INPUT TYPE=HIDDEN NAME="required-subject">


		<INPUT TYPE=button VALUE="Submit" NAME="mysubmit" onClick="checkSubmission()">
		<INPUT TYPE=button VALUE="Reset" NAME="reset" onClick="Reset('required-name*1')">

Version:	v1.5 Feb 9th, 2003
Size:		22.6KB  Including the comments, of course
Copyright:	Ahmad Al-Asad.
Comments:	My best attempt at form validation.

First off, what is a regular expression? Put simply, a regular expression is a string of special values that programmers can use to explicitly match a specific string of text. 

Before we get into using regular expressions to parse text, it is important that you understand a bit about how regular expressions work and what special characters do what. There is just too much to get into here, but here are a few that come up often: 

       . matches any singular character.
       ? matches one or none of the preceding character.
       + matches at least one of the preceding character.
       * matches none or all of the preceding character.
       ^ matches the absolute beginning of the string.
       $ matches the absolute end of the string.
     \w+ matches a whole word.
      \w matches a "word" character (alphanumerics and the "_" character).
     \W+ matches whitespace.
     x|y matches one or the other of x or y.
  [0..9] matches ONE number, ranging from 0 to 9.
[A-Za-z] matches any letter, uppercase or lowercase.
Parentheses can be used to group characters together. 

 (this)+ matches at least one occurrence of "this".
If you wish to search for one of the special characters, you must first delimit it with a backslash(\). 

     \. matches a period.
     \? matches a question mark.
     \[ matches a left square bracket.
     \| matches a "pipe" character.
In addition to these, modifiers can be added after the regular expression to control how it searches through the string. Some of more useful ones include these: 

/somematch/g - global (matches all instances).
/somematch/i - ignore case.
/somematch/gi - you can combine them, too


-----------------------------------------------------------------------------------
*/

//_______________________________________________
//                                              |
// Adjust the following values to fit your form.|
//______________________________________________|

// Status is the bottom of the browser.  Remove this line if not needed.
window.defaultStatus = "Document: Done  -  S a m y a   S o l u t i o n s";

// Defining some form element names and values.
var toField = "required-to"
var subjField = "required-subject";
var nameField = "required-name*1";					// Don't forget the */+# naming convention part of the name.
var commentField = "required-business+7";				// Don't forget the */+# naming convention part of the name.
var successField = "success";
var successPage = "http://www.samyasolutions.com/thankyou.html";
var destEmail = "inquiry@samyasolutions.com";













/*
++++++++++++++++++++++++++++  Modify below this line at your own risk  ++++++++++++++++++++++++++++
*/
function printDate() {
	// function returns Sun., December 23rd
	dayNames = new Array('Sun.','Mon.','Tues.','Wed.','Thur.','Fri.','Sat.'); 
	monthNames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

	today = new Date();
	k = today.getDate();
	while(k>10)
		k=k-10;
	switch(k) {
		case 1:
		suffix="st"; break;
		case 2:
		suffix="nd"; break;
		case 3:
		suffix="rd"; break;
		default:
		suffix="th";
	}
	thisDay = today.getDate();
	// Fixing a bug when date is 11, 12 or 13
	// Above code subtracts 10 making the date display 11st, 12nd and 13rd respectively.
	if (thisDay == 11 || thisDay == 12 || thisDay == 13) {
		suffix = "th";
	}
	var yy = today.getYear();
	var year = (yy < 1000) ? yy + 1900 : yy;
	return dayNames[today.getDay()]+', '+monthNames[today.getMonth()]+' '+thisDay+suffix+' '+year;
}


function noBlank(item){
	var itemValue = item.value;
	var itemName = item.name;
	var msg = "\nInput is required for this field.";
	if ( (itemName.substring((itemName.length - 2), (itemName.length - 1)) == "+") ) {
		if ( (itemValue == "" || itemValue == " ") ) {
			msg += "\nIf you have nothing to input, please enter \"None\" without the quotes.";
			item.select();
			item.focus();
			alert(msg);
			return false;
		} else if ( (itemValue.toLowerCase() == "none") || (itemValue.toLowerCase() == "n/a") ) {
			return "plus";
		}
	} else {
		if ( (itemValue == "" || itemValue == " ") || ((itemValue.toLowerCase() == "none") || (itemValue.toLowerCase() == "n/a")) ) {
			item.select();
			item.focus();
			alert(msg);
			return false;
		}
	}
	return true;
}

function alphaCheck(item){
	for (var i = 0; i < item.value.length; i++){
		var ch = item.value.substring(i, i + 1);
		if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && ch != ' ' && ch != "-" && ch != "." && ch != "'"){
			alert("\nThis field only accepts letters, spaces, periods & dashes.\n\nPlease re-enter.");
			item.select();
			item.focus();
			return false;
		}
	}
	return true;
}


function alphaNumericCheck(item){
	for (var i = 0; i < item.value.length; i++){
		var ch = item.value.substring(i, i + 1);
		if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && (ch < "0" || "9" < ch) && ch != ' ' && ch != "-" && ch != "." && ch != "'" && ch != "_"){
			alert("\nThis field is an alpha numeric field only.\nPlease re-enter.");
			item.select();
			item.focus();
			return false;
		}
	}
	return true;
}

function numericCheck(item){
	// Return false if field is not of numeric value only.
	for (var i = 0; i < item.value.length; i++){
		var ch = item.value.substring(i, i + 1);
		if ( (ch < "0" || "9" < ch) ){
			alert("\nThis field accepts numeric values only.\nPlease re-enter.");
			item.select();
			item.focus();
			return false;
		}
	}
	return true;
}


function contains(fullString, testString) {
        // used to verify a string contains a specific sequence of characters
        // examples:
        // email : if (!contains(form.email_addr.value, "@"))
        istrue = false;
        fullLength = fullString.length;
        testLength = testString.length;
        //alert(fullLength);
        //alert(testLength);
        for (i=0;i<=fullLength;i++) {
                comp=fullString.substring(i-1,fullLength);
                comp=comp.substring(0,testLength);
                //alert(comp);
                if (comp == testString) {
                        istrue = true;
                        //alert("Match found!");
                        return istrue;
                }
        }
        return istrue;
}


function domainCheck(item){
	/*
	Finds a URL using the http: or https: or ftp: protocol. Works in most situations, and includes query strings, but fails if the URL points to an IP address instead of a host name in some circumstances.
	*/
	invalidFormat = false;
	var iVal = item.value;
	//alert(iVal);
	var domainPat = new RegExp(/(((https?)|(ftp)):\/\/([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\/+@&#;`~=%!]*)(\.\w{2,})?)*\/?)/);
	var domainStr = iVal.match(domainPat)
	if (domainStr == null) {
		invalidFormat = true;
	}
	if ( (invalidFormat) ) {
		alert("Invalid domain name format.\nPlease re-enter.\nIP Numbres are not accepted.");
		item.select();
		item.focus();
		return false;
	}
	return true;
}


function phoneCheck(item){   
	// Return false if characters are not numeric and the raw number is not less than 10 digits..
	var iv = item.value;
	for (var i = 0; i < iv.length; i++){
		filteredValues = "().- ";     // Characters stripped out
		var rawPhone = "";
		var ch = iv.substring(i, i + 1);
		if ((ch < "0" || "9" < ch) && ch != "-" && ch != "(" && ch != ")" && ch != "." && ch != ' '){
			alert("\nThis field only accepts the common formats of a telephone number.\n\(972\) 55-SAMYA will not work, for example.\nPlease re-enter.");
			item.select();
			item.focus();
			return false;
		}
		for (var j = 0; j < iv.length; j++){
			var c = iv.charAt(j);
			if (filteredValues.indexOf(c) == -1) rawPhone += c;
		}
		if ( (rawPhone.length < 10) ) {
			alert("\nThis field\'s value is short.  Please be sure to include your area code.\nPlease correct & re-submit.");
			item.select();
			item.focus();
			return false;
		} 
	}
	return true;
}



<!-- Changes:  Sandeep V. Tamhankar (stamhankar@hotmail.com) -->

/* 1.1.2: Fixed a bug where trailing . in e-mail address was passing
            (the bug is actually in the weak regexp engine of the browser; I
            simplified the regexps to make it work).
   1.1.1: Removed restriction that countries must be preceded by a domain,
            so abc@host.uk is now legal.  However, there's still the 
            restriction that an address must end in a two or three letter
            word.
     1.1: Rewrote most of the function to conform more closely to RFC 822.
     1.0: Original  
The following function my also be useful for faster delivery:
function isEmail(string) 
{ 
if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) 
	return true; 
else 
	return false; 
} 

*/

function emailCheck(item) {
	var emailStr = item.value;
	/* 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("The e-mail address seems incorrect (check @ and .'s)");
		item.select();
		item.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.");
		item.select();
		item.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!");
				item.select();
				item.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.");
		item.select();
		item.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.");
		item.select();
		item.focus();
		return false;
	}

	// Make sure there's a host name preceding the domain.
	if (len<2) {
		var errStr="This address is missing a hostname!"
		alert(errStr);
		item.select();
		item.focus();
		return false;
	}

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


/*
Checks if it's a valid username
The first / is the leftmost delimiter for the regular expression. No surprises there. 
The ^ (caret) symbol represents the absolute beginning of the function. This is important, since if it were left out the match would return true if the search() method found the pattern ANYWHERE in the string. Malicious users could then include illegal characters before a valid name and get away with it. 
The \w+ which is next indicates that we want to match at LEAST one or more alphanumeric characters, including the underscore. the \w represents the character, and the + symbol means at least one. No magic there. 
The following part of the regular expression is special, since it has to be treated together. Let's break it down a bit, however. First, notice the whole picture. What we are doing is using a ? symbol, which means match one or none of the preceding character. So, what happens is the regular expression looks for one or none of a space, followed by at least one legal word character. This represents the optional last name of the user. Please take a moment to understand that. 
The last $ symbol represents the end of the string. This makes sure that no characters can appear after our matched string in the regular expression, thus eliminating the possibility of someone sending bad data after a valid username. 
The final / is the rightmost delimiter for the regular expression. Again, no surprises there.

This function returned a javascript error.  Requires investigation
fuction isUserName (string) {
	alert("you should get a js error");
	if (!string) return false;
	if (string.search(/^\w+( \w+)?$/) != -1)
		return true;
	else
		return false;
}

*/

// Reports if all forms have filled out properly.
function checkSubmission(actionPoint){
	var theForm = document.forms[0];
	var flag = true;
	// Loop through and get all the elements of the form.
	for (var i = 0; i < theForm.length; i++){
		var elem = theForm.elements[i];
		// Ignore the elements that are hidden.
		var elemType = elem.type;
		if ( (elemType != "hidden" && 
			elemType != "button" && 
			elemType != "image" && 
			elemType != "submit" && 
			elemType != "reset" && 
			flag != "false") ) {
			// Switch statement cases are:
			/* 	*		="None" NOT allowed
				+		="None" is allowed
					1	=alphaCheck 
					2	=alphaNumericCheck 
					3	=emailCheck 
					4	=phoneCheck 
					5	=numericCheck 
					6	=domainCheck 
					7	=noBlank
			*/
			var elemName = elem.name;
			var plusAstrisk = elemName.substring((elemName.length - 2), (elemName.length - 1));
			if ( plusAstrisk == "+" || plusAstrisk == "*" ) {
				if ( !noBlank(elem) ) {
					flag = false;
					break;
				} else if ( (noBlank(elem) == "plus") ) {
					// Breaking gets out of for loop with flag being true & therefore form submission.
					// Must use continue.
					continue;
				}
			}
			switch (elemName.substring((elemName.length - 2), elemName.length)) {
				case "*1": case "+1":
					if ( !alphaCheck(elem) ) {
						flag = false;
						break;
					}
					break;
				case "*2": case "+2":
					if ( !(alphaNumericCheck(elem)) ) {
						flag = false;
						break;
					}
					break;
				case "*3": case "+3":
					if ( !(emailCheck(elem)) ) {
						flag = false;
						break;
					}
					break;
				case "*4": case "+4":
					if ( !(phoneCheck(elem)) ) {
						flag = false;
						break;
					}
					break;
				case "*5": case "+5":
					if ( !(numericCheck(elem)) ) {
						flag = false;
						break;
					}
					break;
				case "*6": case "+6":
					if ( !(domainCheck(elem)) ) {
						flag = false;
						break;
					}
					break;
				case "*7": case "+7":
					if ( !(noBlank(elem)) ) {
						flag = false;
						break;
					}
					break;
			} // end of switch statement.
		} // end of elemType != hidden, image, etc.
		// Whether or not to break and restart the for loop.
		if ( !flag ) {
			break;
		}
	} // end of for loop.

	// The following is what to do if the form checked out.  This is obviously different for each form.
	// Proceed only if all checked out ... flag would be true.
	if ( flag ) {
		submitForm(actionPoint);
	}
}



// Assign certain default values and submit the form.
function submitForm(actionPoint) {
	var theForm = document.forms[0];
	for (var i = 0; i < theForm.length; i++){
		var elem = theForm.elements[i]
		var elemName = elem.name;
		// Assumes that required-business is the name of a textarea/comments.
		// This can be modified to include a flag at the beginning of the name field.
		if ( elemName.indexOf(commentField) != -1) {
			theForm.elements[commentField].value += "\r\r\r\r" + printDate();
		}
		// Assumes the name of the person submitting the form is always in a field called: required-name.
		if ( (elemName.indexOf(nameField) != -1) ) {
			theForm.elements[subjField].value = "Inquiry from " + theForm.elements[nameField].value;
		}
	} // end of for loop.
	theForm.elements[successField].value = successPage;
	theForm.elements[toField].value = destEmail;
	theForm.action = actionPoint;
	theForm.submit();
}


// Resets fields
function Reset(focusField) {
	var theForm = document.forms[0];
	theForm.action = "";							// Initializing to where the form will submit.
	for (var i = 0; i < theForm.length; i++){
		var elem = theForm.elements[i]
		var elemName = elem.name;
		// Ignore the elements that are hidden.
		var elemType = elem.type;
		switch (elemType) {
			case "text":
				// Don't clear required domain names (domains you like/dislike) ... it's a pain to add those.
				if ( elemName.substring((elemName.length - 2), elemName.length) != "*6" ) {
					elem.value = "";
				}
				break;
			case "textarea":
				elem.value = ""; break;
			case "checkbox": case "radio":
				elem.checked = false; break;
			default:
				break;
		} // end of switch statement.
	} // end of for loop.
	theForm.elements[focusField].focus();				// Set focus on field name passed.

}


// Add the following onBlur="checkForProfanity(this);" to the text field.
var k = 20;
var isProfane = new makeArray(k);
var word = new makeArray(k);
function checkForProfanity(field) {
	var temp = field.value;
	temp = temp.toLowerCase();
	word[1] = "shit";
	word[2] = "motherfucker";
	word[3] = "nigger";
	word[4] = " ass ";
	word[5] = "cunt";
	word[6] = "suck ";
	word[7] = "pussy";
	word[8] = "cock ";
	word[9] = "cum";
	word[10] = "penis";
	word[11] = "sex";
	word[12] = " dick";
	word[13] = "fuck";
	word[14] = "nigga";
	word[15] = " fag ";
	word[16] = "bloody hell";
	word[17] = "asshole";
	word[18] = "bitch";
	word[19] = "blow job";
	word[20] = "biatch";
	for (var j = 1; j <= k; j++) {
		isProfane[j] = temp.indexOf(word[j]);
	}
	for (var j = 1; j <= k; j++) {
		if (isProfane[j] != -1) {
//			alert("Please refrain from using the word \'"+word[j]+"\'.");
			alert("SamyaSolutions.com is a respectable site.  Please refrain from using PROFANITY.\nYour personal message will now be erased.  Try again, only cleaner this time.");
			field.value = "";
			j = k + 1;
			field.focus();
		}
		else {}
	} // end of for loop.
}

function makeArray(n) {
	this.length = n
	for (var i = 1; i<=n; i++) {
		this[i] = new String();
	}
	return this
}


// Deactivate Cloaking -->

