function validateEmail(email)
{
	var emailExpression = /^(?=[a-zA-Z0-9])([a-zA-Z0-9_\.\-])*([a-zA-Z0-9])\@(([a-zA-Z0-9])([a-zA-Z0-9\-])*([a-zA-Z0-9])\.)+([a-zA-Z0-9]{2,6})$/;
	
	if (email == "" || email == "Enter email address" || !email.match(emailExpression))
	{
		alert("Please enter a valid email address.\n", "Invalid Email Address");
		return false;
	}
	
	return true;
}

function validatePhoneNumber(phonenumber, download, login)
{
	var strippedNumber = phonenumber.replace(/[^0-9]+/g, "");
	if (phonenumber == "" || phonenumber == "ex. 5551234567" || (!login && strippedNumber == ""))
	{
		if(login)
		{
			alert("Please enter a valid phone number or username.\n", "Invalid Credentials");
		}
		else
		{
			alert("Please enter a valid phone number.\n", "Invalid Phone Number");
		}
		return false;
	}
	
	if(download)
	{
		KISSmetrics_requestDownload(phonenumber);
	    		
		var download = getCookie('download');
		if (download == null || download == "" || download == "download")
		{
			//alert('Setting cookie to a number');
			setCookie('download',phonenumber,1);
		}
	}
	return true;
	
}

function stripNonDigits(e)
{
	  //Removes all non digit - & .
	  e.value = e.value.replace( /[^\d-.]/g , '' );
}

function existsInArray(item, array)
{
	for(var i = 0; i < array.length; i++)
	{
		if(array[i] == item)
			return true;
	}
	return false;
}

function mobile_charges()
{
	window.open("mobile_charges.html","mywindow","width=400,height=240,left=580,top=300");
}


function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
	    {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
	    }
	}
	return "";
}

function deleteCookie(c_name)
{
	//alert('delete cookie');
	document.cookie = c_name + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}

function setCookie(c_name,value,expiredays)
{
	//alert('set cookie');
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function checkCookie()
{
	//alert('check cookie');
	var download = getCookie('download');
	
	//first time being set or not yet hit the server
	if (download == null || download == "" || download == "download")
	{
		//alert('First Time');
		setCookie('download','download',1);
		
		document.getElementById('download_input').style.display = 'block';
		document.getElementById('download_success').style.display = 'none';
	}
	//has hit the server, show sent image and message
	else
	{
		//alert('Download done... i assume!');
		deleteCookie('download');
		
		document.getElementById('download_input').style.display = 'none';
		document.getElementById('download_success').style.display = 'block';
	}
}

function KISSmetrics_setup(pageName)
{
	KM.record("Page View", {"Page":pageName});
}

function KISSmetrics_viewDownload(pageName)
{
	KM.record("Viewed Form", {"Page Name":pageName});	
}

function KISSmetrics_requestDownload(phoneNumber)
{
	KM.record('Download Link Submit', {number: phoneNumber})	
}