// For the cookie stuff
var zsPreprocCookieLifespan = 1000*60*60*24; // One day
var zsCookieLifespan = 1000*60*60*24; // One day
var zsLongtermCookieLifespan = 1000*60*60*24*365*10; // Ten years
var today = new Date();

// Maxes
var MAX_BROWSER_ZOOMIES = 3;
var MAX_EMAIL_ZOOMIES = 3;

//// form validation scripts 
// Thanks H!
var alpha = /[^A-Za-z]/ig;
var alphaNum =/[^\w \-]/ig;
var alphaNumPunc =/[^\w \- \. , \']/ig;
var num = /[^\d]/ig;
var _float = /[^\d*][\.\d*]?$/i;
var notNull = /[^\s]/ig;
var email = /(\w{1,})@.*\.(\w{2,})/ig;
var URL = /(http:\/\/)?[\w\.-]+\.[A-Za-z\.]{2,}/ig;
var dte = /\d{1,2}\/\d{1,2}\/[20]?[\d{2}][ ]?[\d:ampm ]?/ig;
var img = /\.(gif|jp[e]?g)$/ig;
var nospace = /\s/g;

function charCheck(f,ty,m,e) {
	var e = (e || !e) ? e : true;
	if (e) {
		var f_type = f.type;
		var f_val = (f_type == "checkbox" || f_type == "select" || f_type == "radio") ? f[f.selectedIndex].value : f.value;
		if (ty == "alpha") {
			e = (f_val.search(alpha) == -1) ? true : false;
		} else if (ty == "alphaNum") {
			e = (f_val.search(alphaNum) == -1) ? true : false;
		} else if (ty == "alphaNumPunc") {
			e = (f_val.search(alphaNumPunc) == -1) ? true : false;
		} else if (ty == "num") {
			e = (f_val.search(num) == -1) ? true : false;
		} else if (ty == "float") {
			e = (f_val.search(_float) == -1) ? true : false;
		} else if (ty.toLowerCase() == "notnull") {
			e = (f_val.search(notNull) == -1) ? false : true;
		} else if (ty.toLowerCase() == "url") {
			e = (f_val.match(URL) == null) ? false : true;
		} else if (ty.toLowerCase() == "email") {
			e = (f_val.match(email) == null) ? false : true;
		} else if (ty == "date") {
			e = (f_val.match(dte) == null) ? false : true;
		} else if (ty == "nospace") {
			e = (f_val.match(nospace) == null) ? true : false;
		}
		
		if (!e) {
			if (ty == "date") {
				alert("The "+m+" you've entered is invalid.  \nPlease enter dates using the following format:\n\n\tMM/DD/YYYY");
			} else if (ty == "nospace") {
				alert ("Sorry, there are no spaces allowed in " + m + ".  Please remove all spaces and try again.");
			} else if (m != "" && m != "no") {
				var msg = (ty.toLowerCase() != "notnull") ? "The value you have entered for " + m + " is invalid.  Please re-enter this information." : "The " + m + " field is required.  Please correct this and resubmit.";
				alert(msg);
			}
			if (f_type != "hidden")
			{
				f.focus();
			}
		}
		
	}
	return e;
}

function zipAuth(zip,country) {
	var z = zip.value;
	var c = country;
	var zFit, zCountry, zMask;
	var procZip = "";
		if (c == 1) { // USA
			zFit = /\d{5}[\s-]?(\d{4})?/i;
			zCountry = "USA";
			zMask = "\t- #####\n\t- #####-####";
		} else if (c == 32) { // canada
			zFit = /\w\d\w([\s-]?)[0-9][a-zA-Z][0-9]/i;
			zCountry = "Canada";
			zMask = "\t- !#!-#!#";
		} else if (c == 60 || c == 143 || c == 218) { // england, n.ireland, wales
			zFit = /[\d\w]{3}[\s-]?[\d\w]{3}/i;
			zCountry = "England, Northern Ireland or Wales";
			zMask = "\t- XXX-XXX";
		} else if (c == 170) { // scotland
			zFit = /[\d\w]{2}[\s-]?[\d\w]{3}/i;
			zCountry = "Scotland";
			zMask = "\t- XX-XXX";
		} else if (c == 227) { // australia
			zFit = /[\d]{4,}/i;
			zCountry = "Australia";
			zMask = "\t- ####";
		} else {
			zFit = /[\d\w]+/i;
			zMask = "";
		}
		procZip = String(z.match(zFit));
		//alert(procZip);
		if (procZip == "" || procZip == "null") {
			msg = (zMask != "") ? "The postal code you have entered is incorrectly formatted\nfor "+zCountry+"  \n\nPlease enter your postal code in the following format:\n\n"+zMask+"\n\n where " : "The Postal Code field is required.\nPlease enter your postal code";
			if (zMask != "") {
				if (zMask.indexOf("X") > -1) {
					msg += "X can be either a letter or a number.";
				} else if (zMask.indexOf("!") > -1) {
					msg += "! represents a letter and # represents a number.";
				} else if (zMask.indexOf("!") == -1 && zMask.indexOf("X") == -1) {
					msg += "# represents a number.";
				}
			}
			alert(msg);
			zip.focus();
			return false;
		} else {
			
			zip.value = z.replace(/[^\w]/ig,"");
			return true;
		}
}

// When processing starts
function zsProcStart (thedoc, email)
{
	var expires = new Date ();

	expires.setTime (today.getTime () + zsPreprocCookieLifespan);

	setCookie (
		thedoc,
		"zsProcHold",
		email,
		expires
	);

	return true;
}

// When processing ends
function zsProcDone (thedoc)
{
	var email = getCookie (thedoc, "zsProcHold");
	var expires = new Date ();
	var killit = new Date ();
	var longex = new Date ();
	var countCheck = 0;

	// We can't do anything if there's no holding cookie.
	if (email == null)
	{
		return true;
	}

	expires.setTime (today.getTime () + zsCookieLifespan);
	longex.setTime (today.getTime () + zsLongtermCookieLifespan);
	killit.setTime (today.getTime () - (zsLongtermCookieLifespan*2));

	// Now we work with the global cookie
	countCheck = getCookie (thedoc, "zsBrowser");
	//alert ("zsBrowser=='"+countCheck+"'");
	if (countCheck == null)
	{
		setCookie (
			thedoc,
			"zsBrowser",
			1,
			expires
		);
	}
	else
	{
		setCookie (
			thedoc,
			"zsBrowser",
			countCheck,
			killit
		);
		setCookie (
			thedoc,
			"zsBrowser",
			parseInt (countCheck) + 1,
			expires
		);
	}
	countCheck = getCookie (thedoc, "zsBrowser");
	//alert ("zsBrowser=='"+countCheck+"'");

	// Let's move the holding cookie to a processed cookie
	setCookie (
		thedoc,
		"zoomshare." + email,
		"hello",
		expires
	);
	// Erase the holding cookie
	setCookie (
		thedoc,
		"zsProcHold",
		email,
		killit
	);

	// Work with long-term email-specific cookie
	countCheck = getCookie (thedoc, "zsLong." + email);
	if (countCheck == null)
	{
		setCookie (
			thedoc,
			"zsLong." + email,
			1,
			longex
		);
	}
	else
	{
		setCookie (
			thedoc,
			"zsLong." + email,
			countCheck,
			killit
		);
		setCookie (
			thedoc,
			"zsLong." + email,
			parseInt (countCheck) + 1,
			longex
		);
	}

	return true;
}

// Reset the timer for the persistent ones
function zsExtend (doc, email)
{
	setCookie (
		doc,
		"zoomshare." + email,
		"hello",
		today.getTime () + zsCookieLifespan
	);

	return true;
}

// Check if we already have a cookie for this user or not.
function zsDenyCheck (doc, email)
{
	var check = null;

	// Work with email-specific one-day cookie
	check = getCookie (doc, "zoomshare."+email);
	//alert ("check == '" + check + "'");
	if (check != null)
	{
		return true;
	}
	
	// Work with browser-specific one-day cookie
	check = getCookie (doc, "zsBrowser");
	//alert ("check == '" + check + "'");
	if (check != null && parseInt (check) >= MAX_BROWSER_ZOOMIES)
	{
		return true;
	}

	// Work with email-specific long-term cookie
	check = getCookie (doc, "zsLong." + email);
	//alert ("check == '" + check + "'");
	if (check != null && parseInt (check) >= MAX_EMAIL_ZOOMIES)
	{
		return true;
	}
	
	return false;
}

