﻿function printCoupon(thisform,url)
{
	var validates;
	validates = validateForm(thisform);
	if(validates==false)
	{
		return false;
	} else {
		window.open(url);
		return true;
	}
}

function emailCoupon(thisform)
{
	var validates;
	validates = validateForm(thisform);
	if(validates==false)
	{
		return false;
	} else {
		return true;
	}
}

function validateForm(thisform) 
{
	with (thisform) 
	{
		if(validateRequired(customerfirstname,"First name")==false)
		{
			customerfirstname.focus();
			return false;
		}
		if(IsAlpha(customerfirstname,"First name")==false)
		{
			customerfirstname.focus();
			return false;
		}
		if(validateRequired(customerlastname,"Last name")==false)
		{
			customerlastname.focus();
			return false;
		}
		if(IsAlpha(customerlastname,"Last name")==false)
		{
			customerlastname.focus();
			return false;
		}
		if(validateRequired(customeremail,"Email")==false)
		{
			customeremail.focus();
			return false;
		}
		if(validateEmail(customeremail)==false)
		{
			customeremail.focus();
			return false;
		}
		if(validateRequired(customerstate,"State")==false)
		{
			customerstate.focus();
			return false;
		}
		if(IsAlpha(customerstate,"State")==false)
		{
			customerstate.focus();
			return false;
		}
	}
}

function validateRequired(field,title)
{
	with(field)
	{
		if(value==null || value=="")
		{
			var isRequired = false;
		} else {
			var isRequired = true;
		}
	}
	if (isRequired==false)
	{
		alert(title + " is a required field.")
		return false;
	} else {
		return true;
	}
}
function validateEmail(field)
{
	with (field)
	{
    var splitted = value.match("^(.+)@(.+)$");
    if(splitted == null) 
		{
			alert("Please enter a valid email address.");
			return false;
		}
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null)
			{
				alert("Please enter a valid email address.");
				return false;
			}
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    	var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    	if(splitted[2].match(regexp_ip) == null)
				{
					alert("Please enter a valid email address.");
					return false;
      	}// if
      	return true;
			}
    }
	}
}

function IsPhone(field) {
	var validChars = "0123456789.()-";
	var IsNumber = true;
	var char;
	with (field)
	{
		for (i=0; i<value.length && IsNumber == true; i++)
		{
			char = value.charAt(i);
			if(validChars.indexOf(char) == -1)
			{
				IsNumber = false;
			}
		}
	}
	if(IsNumber == true) {
		return true;
	} else {
		alert("Please enter a valid telephone number");
		return false;
	}
}
	
function IsAlpha(field,title) {
	with (field)
	{
		var validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz. ";
		var IsAlpha = true;
		var char;
		for (i=0; i<value.length && IsAlpha == true; i++)
		{
			char = value.charAt(i);
			if(validChars.indexOf(char) == -1)
			{
				IsAlpha = false;
			}
		}
	}
	if(IsAlpha == true) {
		return true;
	} else {
		alert("Please only use alphabetical characters for " + title + ".");
		return false;
	}
}

function IsNumeric(field,title) {
	with (field)
	{
		var validChars = "0123456789- ";
		var IsNumeric = true;
		var char;
		for (i=0; i<value.length && IsNumeric == true; i++)
		{
			char = value.charAt(i);
			if(validChars.indexOf(char) == -1)
			{
				IsNumeric = false;
			}
		}
	}
	if(IsNumeric == true) {
		return true;
	} else {
		alert("Please only use numeric characters for " + title + ".");
		return false;
	}
}

function IsAlphaNumeric(field,title) {
	with (field)
	{
		var validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789. ";
		var IsAlphaNumeric = true;
		var char;
		for (i=0; i<value.length && IsAlphaNumeric == true; i++)
		{
			char = value.charAt(i);
			if(validChars.indexOf(char) == -1)
			{
				IsAlphaNumeric = false;
			}
		}
	}
	if(IsAlphaNumeric == true) 
	{
		return true;
	} else {
		alert("Please only use alphabetical and numeric characters " + title + ".")
		return false;
	}
}

function validateRadio(field,title)
{
	var elements;
	elements = document.getElementsByName(field);
	for(var k=0, elm; elm=elements[k];k++)
	{
		if(elm.checked)
		{
			return true;
		} else {
			alert("Please choose an option for " + title + ".");
			return false;
		}
	}
}