function Validate_Email_Address(email_address) {
	
	at = email_address.indexOf('@');
	dot = email_address.indexOf('.');
	 
	if(at == -1 || 
	   dot == -1 || 
	   dot <= at + 1 ||
	   dot == 0 || 
	   dot == email_address.length - 1)
	   return(false);
	    
	user_name = email_address.substr(0, at);
	domain_name = email_address.substr(at + 1, email_address.length);                  
	 
	if(Validate_String(user_name) === false || 
	   Validate_String(domain_name) === false)
	   return(false);                     
	 
	return(true);
}

function Validate_String(string, return_invalid_chars) {
	 valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	 var invalid_chars = '';
 
	 if(string == null || string == '')
	    return(true);
 
	 //For every character on the string.   
	 for (index = 0; index < string.length; index++) {
	 	char = string.substr(index, 1);
 	
 		//Is it a valid character?
		if (valid_chars.indexOf(char) == -1) {
			//If not, is it already on the list of invalid characters?
			if (invalid_chars.indexOf(char) == -1) {
				//If it's not, add it.
				if (invalid_chars == '') {
					invalid_chars += char;
				} else {
					invalid_chars += ', ' + char;
				}
			}
		}
	}              
    
 //If the string does not contain invalid characters, the function will return true.
 //If it does, it will either return false or a list of the invalid characters used
 //in the string, depending on the value of the second parameter.
 if(return_invalid_chars == true && invalid_chars != '')
   {
   last_comma = invalid_chars.lastIndexOf(',');
   
   if(last_comma != -1)
      invalid_chars = invalid_chars.substr(0, $last_comma) + 
      ' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);
              
   return(invalid_chars);
   }
 else
   return(invalid_chars == ''); 
 }
 
var digitsOnly = /[1234567890\{\}\+\(\)\-\.\ ]/g;
var integerOnly = /[0-9\.]/g;
var alphaOnly = /[A-Z]/g;

function restrictCharacters(myfield, e, restrictionType) {
	if (!e) var e = window.event
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	var character = String.fromCharCode(code);

	// if they pressed esc... remove focus from field...
	if (code==27) { this.blur(); return false; }
	
	// ignore if they are press other keys
	// strange because code: 39 is the down key AND ' key...
	// and DEL also equals .
	if (!e.ctrlKey && code!=9 && code!=8 && code!=36 && code!=37 && code!=38 && (code!=39 || (code==39 && character=="'")) && code!=40) {
		if (character.match(restrictionType)) {
			return true;
		} else {
			return false;
		}
		
	}
}

 
$(function() {
	
	$(".optional").each(function(i) {
		$(this).css("color", "#666666");
	});
	
	$('#singleNavLeft').click(function() {
		$('#contactMiddleLeft #name').val("");
		$('#contactMiddleLeft #company').val("");
		$('#contactMiddleLeft #email').val("");
		$('#contactMiddleLeft #phone').val("");
		$('#contactMiddleLeft #country').val("");
		$('#contactMiddleLeft #province').val("");
		$('#contactMiddleLeft #name').val("");
		$('#contactMiddleLeft #comment').val("");
	});
	
	$('#singleNavRight').click(function() {
		
		var error = 0;
		
		if ($('#contactMiddleLeftRight #name').val() == "") {
			error++;
			$('.nameText').css("color", "#ff0000");
		} else {
			$('.nameText').css("color", "#84b43a");
		}
		
		if ($('#contactMiddleLeftRight #email').val() == "") {
			error++;
			$('.emailText').css("color", "#ff0000");
		} else {
			$('.emailText').css("color", "#84b43a");
		}
		
		if ($('#contactMiddleLeftRight #state').val() == "") {
			error++;
			$('.stateText').css("color", "#ff0000");
		} else {
			$('.stateText').css("color", "#84b43a");
		}
		
		if ($('#contactMiddleLeftRight #country').val() == "") {
			error++;
			$('.countryText').css("color", "#ff0000");
		} else {
			$('.countryText').css("color", "#84b43a");
		}
		
		if ($('#contactMiddleLeft #comment').val() == "") {
			error++;
			$('.commentText').css("color", "#ff0000");
		} else {
			$('.commentText').css("color", "#84b43a");
		}
		
		if (error == 0) {
		
			var name = $('#contactMiddleLeftRight #name').val();
			var company = $('#contactMiddleLeftRight #company').val();
			var email = $('#contactMiddleLeftRight #email').val();
			var phone = $('#contactMiddleLeftRight #phone').val();
			var department = $('#contactMiddleLeftRight #department').val();
			var state = $('#contactMiddleLeftRight #state').val();
			var country = $('#contactMiddleLeftRight #country').val();
			var province = $('#contactMiddleLeftRight #province').val();
			var message = $('#contactMiddleLeft #comment').val();
			
			$('#singleNav').append('<img src="images/loading.gif" id="loading">');
			
			$.ajax({
			
				url: "sendComment.php",
				type: "POST",
				data: "name=" + name + "&company=" + company + "&email=" + email + "&phone=" + phone + "&department=" + department + "&state=" + state + "&country=" + country + "&province=" + province + "&message=" + message,
				
				success: function(r){
					$('#loading').remove();
					$('#contactFormProper').animate({
						"left": "-1030px"
					});
				}
				
			})
			
		}
		
		return false;
		
	});
	
});