/******************************************/
/* Coded with: Macromedia Dreamweaver 8   */
/* File Name: ContactFunctions.js         */
/* Created By: PricelessSurveys.com       */
/* Created: June 7, 2006                  */
/* Last Modified: June 11, 2006           */
/* Comment: this is only for free-surveys */
/*          section                       */
/******************************************/


// main function: doSubmit()
function doSubmit()
{
	if(isNameOK() && isEmailOK3("senderEmail") && isCategoryOK() && isSubjectOK() && isQuestionTextOK())
	{
		document.contactForm.question.value = 1;
		document.contactForm.submit();
		return true;
	}
	else
	{
		return false;
	}
}

// check for correct chars in name field
function isNameOK()
{
	// declare local vars
	var form = document.contactForm;
	var field = form.elements['senderName'];
	var is_name_only_spaces = field.value.replace(/[ ]/g, "");
//	var name_to_check = document.contactForm.senderName.value;
	var name_to_check = field.value;
	var valid_symbol = /^([A-Za-z]{1})+([A-Za-z'. ]{0,71})+$/;
	// check if there is any invalid symbol in name field
	if (!valid_symbol.test(name_to_check))
	{
		alert('Please enter your real name.');
		document.contactForm.senderName.focus();
		return false;
	}
	// everything is valid up to this point
	return true;
}

// check for correct choice in category field
function isCategoryOK()
{
	// declare local vars
	var form = document.contactForm;
	var field = form.elements['category'];
	var category = field.value;
	
	// see if a choice has been made for category
	// 0 means no selection; selection = 'billing', 'cancel', etc.
	if (category == 0)
	{
		alert('Please indicate the Category your inquiry falls under.');
		document.contactForm.category.focus();
		return false;
	}

	// everything is valid up to this point
	return true;
}

// check for correct chars in subject field
function isSubjectOK()
{
	// declare local vars
	var form = document.contactForm;
	var field = form.elements['subject'];
	var is_name_only_spaces = field.value.replace(/[ ]/g, "");
//	var subject = document.contactForm.subject.value;
	var subject = field.value;
	// these characters are not allowed: "<>\:
	var invalid_symbol = '"' + ":*%+=,/#<>\\";
	
	// see if only spaces or nothing provided (at least 2 chars)
	if (is_name_only_spaces.length <= 1)
	{
		alert('Please enter the subject of your inquiry.');
//		document.contactForm.subject.value="";
		document.contactForm.subject.focus();
		return false;
	}

	// check if there is any invalid symbol in the subject field
/*	for (i=0; i<subject.length; i++)
	{
		if (invalid_symbol.indexOf(subject.charAt(i)) != -1)
		{
			alert('Please enter the Subject!\nIt must be at least 2 characters!');
//			document.contactForm.subject.value="";
			document.contactForm.subject.focus();
			return false;
		}
	}
*/
	// everything is valid up to this point
	return true;
}

// check for correct chars in question text field
function isQuestionTextOK()
{
	// declare local vars
	var form = document.contactForm;
	var field = form.elements['questionText'];
	var is_name_only_spaces = field.value.replace(/[ ]/g, "");
//	var text = document.contactForm.questionText.value;
	var text = field.value;
	
	// see if only spaces or nothing provided (at least 4 chars)
	if (is_name_only_spaces.length <= 3)
	{
		alert('Please let us know what\'s on your mind.');
//		document.contactForm.questionText.value="";
		document.contactForm.questionText.focus();
		return false;
	}

	// everything is valid up to this point
	return true;
}

// check if email is a correct, generic version
function isGenericEmail(email)
{
	var email_str;  // e-mail string
	// var emstr_confirm;
	var at_position;    // '@' position in e-mail
	var dot_position;    // '.' position in e-mail
	var invalid_symbol = '"' + " :*'!%+=;,/#<>\\"; // these characters are not allowed

	// first let's make email lowercase
	email_str = email.toLowerCase();
	// find positions of @ and . symbols
	at_position = email_str.indexOf('@');
	dot_position = email_str.indexOf('.');

	// let's check to make sure we have "text@text.text" as our email form
	if ((at_position == -1) || (dot_position == -1))
	{
		return false;
	}
	if ((at_position == 0) || (dot_position == 0))
	{
		return false;
	}
	if ((email_str.charAt(email_str.length-1) == '@') || (email_str.charAt(email_str.length-1) == '.'))
	{
		return false;
	}
	if (email_str.split('@').length != 2)
	{
		return false;
	}
	if ((email_str.split('@')[1].indexOf('.') == -1) || (email_str.split('@')[1].indexOf('.') == 0))
	{
		return false;
	}

	// check if there is any invalid symbol in the email field
	for (i=0; i<email_str.length; i++)
	{
		if (invalid_symbol.indexOf(email_str.charAt(i)) != -1)
		{
			return false;
		}
	}
	
	// if everything is ok, let's return that it's a correct generic email
	return true;
}

// check email after user clicks login
function isEmailOK()
{
	// declare local vars
	var form = document.contactForm;
	var field = form.elements['senderEmail'];
	// check to see if email is in valid generic form
	if(isGenericEmail(field.value))
	{
		return true;
	}
	else
	{
		// if not, send out an alert to correct it
		alert('Please enter a valid email address.');
		//document.contactForm.senderEmail.value="";
		document.contactForm.senderEmail.focus();
		return false;
	}
}

function isEmailOK2()
{
	// declare local vars
	var form = document.contactForm;
	var field = form.elements['senderEmail'];
	var wrong_symbol='"' + " :*'!%+=;,/#";
	var oField = eval(field);
	var emstr=oField.value.toLowerCase(); // e-mail string
	var sht=emstr.indexOf('@');   // '@' position in e-mail
	var dot=emstr.indexOf('.');   // '.' position in e-mail

	// no @ or . symbols
	if(sht==-1 || dot==-1)
	{
		alert('Please enter a valid email address.');
		oField.value="";
		oField.focus();
		return false;
	}
	if(sht==0||dot==0)
	{
		alert('Please enter a valid email address.');
		oField.value="";
		oField.focus();
		return false;
	}
	if(emstr.charAt(emstr.length-1)=='@'||emstr.charAt(emstr.length-1)=='.')
	{
		alert('Please enter a valid email address.');
		oField.value="";
		oField.focus();
		return false;
	}
	if(emstr.split('@').length!=2)
	{
		alert('Please enter a valid email address.');
		oField.value="";
		oField.focus();
		return false;
	}
	if(emstr.split('@')[1].indexOf('.')==-1||emstr.split('@')[1].indexOf('.')==0)
	{
		alert('Please enter a valid email address.');
		oField.value="";
		oField.focus();
		return false;
	}
	for(i=0;i<emstr.length;i++)
	{
		if(wrong_symbol.indexOf(emstr.charAt(i))!=-1)
		{
			alert('Please enter a valid email address.');
			oField.value="";
			oField.focus();
			return false;
		}
	}
	
	// everything is ok (for now)
	return true;
}


// checks if email is valid (only the form)
/* Got this script from: http://javascript.internet.com/forms/check-email.html */
function isEmailOK3(field_name)
{
	// declare local vars
	var form = document.contactForm;
	var field = eval("document.contactForm."+field_name);
	var emailStr = field.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('Please enter a valid email address.');
		field.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('Please enter a valid email address.');
		field.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)
			{
				// the IP address is invalid
				alert('Please enter a valid email address.');
				field.focus();
				return false;
			}
		}
		// IP address seems to be valid
		return true;
	}

	// Domain is symbolic name
	var domainArray=domain.match(domainPat);
	if (domainArray==null)
	{
		alert('Please enter a valid email address.');
		field.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('Please enter a valid email address.');
		field.focus();
		return false;
	}

	// Make sure there's a host name preceding the domain.
	if (len<2)
	{
		alert('Please enter a valid email address.');
		field.focus();
		return false;
	}

	// If we've gotten this far, everything's valid!
	return true;
}
