// Author: Vauxhall
// Email : vauxhall(at)gmail(dot)com
// Application : EmailScript1.js
// Copyright 2011 Travel Tracker Pty Ltd
// Version 2.00 - 17th March 2011

// Functions

function filter3(string) {
	string = htstring.replace(/(<([^>]+)>)/ig,""); 
	return string;
}

function len3(paramString) {
	return paramString.length;	
}

function trim3(paramString) {
	return paramString.replace(/^\s+|\s+$/g,"");
}

function findURL3(string) {
	string = string.replace(/www\./, "http://www.");
    string = string.replace("http://http://www\.", "http://www.");
    string = string.replace("https://http://www\.", "https://www.");
    string = string.replace("https://", "http://");
    if (string.search(/http\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/) != -1) {  
   		return true;
	} else {
   		return false;
	}	
}

function isEmail3(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 isHTMLTags3(string) {
	if (string.search(/<(\S+).*>(.*)<\/\1>/) != -1) {
		return true;
	} else {
		return false;
	}
}


function checkSizeValid3(mystring, lessThan, greaterThan) {
	// Check name length between 2 and 65.
	if (len3(trim3(mystring)) < lessThan) {
		return false;
	}
	if (len3(trim3(mystring)) > greaterThan) {
		return false;
	}
	return true;
}

function Validator3(paramContactName, paramEmailAddress) {

	// Setup checking variables
	var checkContactName = 1;
	var checkEmailAddress = 1;
	
	// Check for default values
	if (trim3(paramContactName) == 'Name Here') { checkContactName = 0; }
	if (trim3(paramEmailAddress) == 'Email Here') { checkEmailAddress = 0; }

	// Check sizes of freeform text fields
	if (checkSizeValid3(paramContactName,3,65) == false) { checkContactName = 0; }
	if (checkSizeValid3(paramEmailAddress,3,65) == false) { checkEmailAddress = 0; }

	// Check for no HTML tags
	if (isHTMLTags3(paramContactName) == true) { checkContactName = 0; }
	if (isHTMLTags3(paramEmailAddress) == true) { checkEmailAddress = 0; }
	
	// Check for URL's
	if (findURL3(paramContactName) == true) { checkContactName = 0; }
	if (findURL3(paramEmailAddress) == true) { checkEmailAddress = 0; }	

	if (isEmail3(trim3(paramEmailAddress)) == false) { checkEmailAddress = 0; }
	
	// Show warnings if needed
	if ((checkContactName == 0) || (checkEmailAddress == 0)) {
		var alertString = 'Please check the following : \n\n';
		if ((checkContactName) == 0) {
			alertString = alertString + ' * Invalid or missing name.\n';	
		}
		if ((checkEmailAddress) == 0) {
			alertString = alertString + ' * Invalid or missing email.\n';	
		}
  		alertString = alertString + '\nThe form was not sent.'
		alert(alertString);
		return false;
	}

	return true;
}

