﻿

var active_flag=false

var select_title = 'null'

var select_country = 'United States'



var errorStr





function submit()

{

	if (!active_flag)

	{

		if (select_title!='null')

		{

			var first_name=document.getElementById('first-name').value

			if (first_name.length>2)

			{

				var last_name=document.getElementById('last-name').value

				if (last_name.length>2)

				{

					var contact_email=document.getElementById('email').value

					var repeat_email=document.getElementById('repeat-email').value

						

					if (emailCheck(contact_email))

					{

						if (contact_email==repeat_email)

						{

							var contact_message=document.getElementById('message').value 

							if (contact_message.length>2)

							{

								var URL = 'contact.php'

								URL += '?title=' + select_title	

								URL += '&first_name=' + escape(first_name)

								URL += '&last_name=' + escape(last_name)

								URL += '&country=' + select_country

								URL += '&email=' + escape(contact_email)		

								URL += '&message=' + escape(contact_message)

								URL += '&rand=' + Math.random()*999	

									

								active_flag=true 

								document.getElementById('throbber').style.visibility='visible'

								ajaxGet(URL)

							}

							else

							{

								alert('Please enter a message')

							}

						}

						else

						{

							alert('The email addresses you have entered are not identical')

						}

					}

					else

					{

						alert('Please enter a valid email address') 

					}

				}

				else

				{

					alert('Your last name must consist of at least 2 characters')

				}

			}

			else

			{

				alert('Your first name must consist of at least 2 characters')

			}

		}

		else

		{

			alert('Please select a title (or salutation)')

		}	

	}

}







function emailCheck(email)

{

	var success=true

	var email=email.toLowerCase()

	var validationStr="0123456789abcdefghijklmnopqrstuvwxyz-_@."

	var validCharacterCheck=0

		

	for (var i=0, len_email=email.length; i<len_email; i++) 

	{

		for (var j=0, len_validation=validationStr.length; j<len_validation; j++)

		{

			if (email.charAt(i)==validationStr.charAt(j))

			{

				validCharacterCheck++

			}

		}

	}

		

	success=validCharacterCheck==len_email



	var email_check=email.indexOf('@')

	var at_check=email.lastIndexOf('@')

	

	if (email_check==-1 || email_check!=at_check) {success=false}

	else

	{

		email_check = email.split("@")

		email_check[1].length<5?success=false:''

		email_check[0].length<2?success=false:''			

	}

	

	email.indexOf('.')==-1?success=false:''

	return success	

}





function XHR(method)

{

	http_request=false

	if (window.XMLHttpRequest)

	{

		http_request=new XMLHttpRequest()

	}

	else if (window.ActiveXObject)

	{

		try {http_request=new ActiveXObject('Msxml2.XMLHTTP')}

		catch (e)

		{

			try {http_request=new ActiveXObject('Microsoft.XMLHTTP')}

			catch (e){}

		}

	}

	else return false

}



function ajaxGet(url)

{

	XHR('get')

	http_request.onreadystatechange=function() {loadPage(http_request)}

	http_request.open('GET', url, true)

	http_request.send(null)

}



function loadPage(http_request)

{

	if (http_request.readyState==4 && http_request.status==200)

	{

		document.getElementById('throbber').style.visibility='hidden'

		alert('Thank you for contacting Forms \'n\' More.  We received your message, and we will forward it to one of our customer support specialists.  You can expect a response within 1-2 business days or less.')

	}

}



