// JavaScript Document
function openWindow(path)
{
	 newwindow = window.open (path, "andersonPopup","menubar=0,resizable=0,width=600,height=355");
	 newwindow.focus();
}

function checkAmount(f)
{
	var amount=document.forms[f].amount;
	var qty=document.forms[f].quantity;
	//var add=document.forms[f].add;
	
	if(qty.value <= 5)
		amount.value = 129;
	else if(qty.value > 5 && qty.value <= 10)
		amount.value = 109;
	else if(qty.value > 10 )
		amount.value = 99;
		
	//add.value = qty.value;
}

function checkForm(f)
{
	email = checkEmail(f);
	name = checkInput('name');
	subject = checkInput('subject');
	comment = checkInput('comments');
	
	if(!email || !name || !subject || !comment )
	{
		return false;
	}
	else
	{
		new Ajax.Request('processform.php', {
						 	method: 'post',
							parameters: document.forms[f].serialize(),
							onSuccess: function(transport)
							{
								alert(transport.responseText);
							},
							onFailure: function(transport)
							{ 
								alert("Send Failed");
							}
						 }
						 ); 
		return false;
	}
}

function checkEmail(f)
{
	var emailID=document.forms[f].email;
	
	if ((emailID.value==null)||(emailID.value=="")){
		$('errorBox').innerHTML = "No Email entered";
		$('errorBox').className = "errorbox";
		
		return false;
	}
	if (echeck(emailID.value)==false){
		$('errorBox').innerHTML = "Invalid Email Address";
		$('errorBox').className = "errorbox";
		return false;
	}
	
	$('errorBox').innerHTML = "";
	$('errorBox').className = "";
	
	return true;
}

function checkNumeric(field,vallen)
{
	//alert(zip);
	var ziplen = $(field).value.length;
	var ValidChars = "0123456789.";
  	var IsNumber=true;
   	var Char;
   
   	//alert(ziplen);
	if($(field).value != '' || $(field).value != null)
	{
		if(ziplen <= vallen)
		{
			for (i = 0; i < ziplen && IsNumber == true; i++) 
			{ 
			  	Char = $(field).value.charAt(i); 
			  	if (ValidChars.indexOf(Char) == -1) 
				{
					IsNumber = false;
				}
			}
			
			if(!IsNumber)
			{
				$(field+'error').innerHTML = "Invalid value in this field.";
				$(field+'error').className = "errorbox";
				
				return false;
			}
			else
			{
				$(field+'error').innerHTML = "";
				$(field+'error').className = "";
				return true;
			}			
		}
		else
		{
			vallen += 1;
			$(field+'error').innerHTML = "Must be less than  "+vallen+" characters";
			$(field+'error').className = "errorbox";
				
			return false;
		}
	}
	
	return true;
}

function checkPhone(zip)
{
	//alert(zip);
	var ziplen = $(zip).value.length;
	var ValidChars = "0123456789";
  	var IsNumber=true;
   	var Char;
   
   	//alert(ziplen);
	if($(zip).value != '' || $(zip).value != null)
	{
		if(ziplen >= 10)
		{
			for (i = 0; i < ziplen && IsNumber == true; i++) 
			{ 
			  	Char = $(zip).value.charAt(i); 
			  	if (ValidChars.indexOf(Char) == -1) 
				{
					IsNumber = false;
				}
			}
			
			if(!IsNumber)
			{
				$(zip+'error').innerHTML = "Telephone Numbers can only contain numbers. No Letters Or Symbols.";
				$(zip+'error').className = "errorbox";
				
				return false;
			}
			else
			{
				$(zip+'error').innerHTML = "";
				$(zip+'error').className = "";
				return true;
			}			
		}
		else
		{
			$(zip+'error').innerHTML = "Telephone number must be 10 characters or greater.";
			$(zip+'error').className = "errorbox";
				
			return false;
		}
	}
	
	return true;
}

function checkZipcode(f)
{
	if(document.forms[f].zipcode)
	{
		pre = 'zip';
		var zip=document.forms[f].zipcode.value;
	}
	else if($('zipcode'))
	{
		pre = 'zip';
		var zip=$('zipcode').value;
	}
	else
	{
		pre ='postalcode';
		var zip=document.forms[f].postalcode.value;
	}
	var ziplen = zip.length;
	var ValidChars = "0123456789-";
  	var IsNumber=true;
   	var Char;
   
	if(zip != '' || zip != null)
	{
		if(zip >= 5)
		{
			for (i = 0; i < ziplen && IsNumber == true; i++) 
			{ 
			  	Char = zip.charAt(i); 
			  	if (ValidChars.indexOf(Char) == -1) 
				{
					IsNumber = false;
				}
			}
			
			if(!IsNumber)
			{
				$('errorBox').innerHTML = "System only allows US residential postal codes.";
				$('errorBox').className = "errorbox";
				
				return false;
			}
			else
			{
				$('errorBox').innerHTML = "";
				$('errorBox').className = "";
				return true;
			}			
		}
		else
		{
			$('errorBox').innerHTML = "Zipcode must be 5 characters or greater.";
			$('errorBox').className = "errorbox";
				
			return false;
		}
	}
	
	return true;
}

function checkInput(field)
{
	//alert(field);
	if($(field).value == '' || $(field).value == null)
	{
		$('errorBox').innerHTML = field+' is a required field';
		$('errorBox').className = "errorbox";
		
		return false;
	}
	
	$('errorBox').innerHTML = '';
	$('errorBox').className = "";
	return true;
}

function echeck(str)
{
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    
		    return false
		 }

 		 return true					
	}
