
function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}


function validSimplePhone(phoneNum)
{
	strlen=phoneNum.length; 	
	if (strlen>=10) {
		//alert('Valid phone number: '+phoneNum);
		return true; 
	}else{
		alert('Please enter a valid phone number');
		return false;
	}
}	

function validPhone(phoneNum)
{
		
	strlen=phoneNum.length; 
	
	if ( (strlen==10) && (allDigits(phoneNum)) ) {
		// 10 digits is a valid format
		//alert('valid phone number: '+phoneNum);
		return true; 
	}
	
	if (strlen!=12) 
	{
		alert('Please enter a phone number in the format: 123-456-7890');
		//s.sendFormEvent('e',s.pageName,'Request','Phone Number: Length Error');
		return false;
	}	
	
	if (!allDigits(phoneNum.substring(0,3)))
	{
		alert('Please enter only numbers');
		//s.sendFormEvent('e',s.pageName,'Request','Phone Number: Not Numeric');
		return false;	
	}
	
	 if ('-'.indexOf(phoneNum.charAt(3))<0) 
	{
			alert('Please enter a phone number in the format: 123-456-7890');
			//s.sendFormEvent('e',s.pageName,'Request','Phone Number: Missing -');
			return false;
	}
	
	if (!allDigits(phoneNum.substring(4,7)))
	{
		alert('Please enter a phone number in the format: 123-456-7890');
		//s.sendFormEvent('e',s.pageName,'Request','Phone Number: Not Numeric');
		return false;	
	}

	if ('-'.indexOf(phoneNum.charAt(7))<0) 
	{
			alert('Please enter a phone number in the format: 123-456-7890');
			//s.sendFormEvent('e',s.pageName,'Request','Phone Number: Not Numeric');
			return false;
	}
	
	if (!allDigits(phoneNum.substring(8,12)))
	{
		alert('Please enter a phone number in the format: 123-456-7890');
		//s.sendFormEvent('e',s.pageName,'Request','Phone Number: Not Numeric');
		return false;	
	}
	return true;
	
}


function validRequired(formField,fieldLabel)
{
	var result = true;

	if (trimAll(formField.value) == "")
	{
		if(formField.name == "zip")
		{
			alert('Please enter a value for the "' + fieldLabel +'" field.');
		}
		else
		{
			alert('Please enter a value for the "' + fieldLabel +'" field.');
		}
		formField.focus();
		result = false;
	}

	return result;
}


function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}


function validPostalCode(field)
{
	// Reference - http://home.cogeco.ca/~ve3ll/jstutorb.htm
	
	
	// CANADIAN CODES ONLY
	strlen=field.length; 
	if (strlen!=7) 
	{
		alert('Please enter a postal code in the following format: A1B-2C3');
		return false;
	}


	field=field.toUpperCase();    // in case of lowercase characters

	// Check for legal characters in string - note index starts at zero
	if ('ABCEGHJKLMNPRSTVXY'.indexOf(field.charAt(0))<0) 
	{
		alert('Please enter a Canadian postal code in the following format: A1B-2C3');
		return false;
	}
	if ('0123456789'.indexOf(field.charAt(1))<0) 
	{	
		alert('Please enter a Canadian postal code in the following format: A1B-2C3');	
		return false;
	}
	if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(field.charAt(2))<0) 
	{
		alert('Please enter a Canadian postal code in the following format: A1B-2C3');	
		return false;
	}
	if ('-'.indexOf(field.charAt(3))<0) 
	{
		alert('Please enter a Canadian postal code in the following format: A1B-2C3');		
		return false;
	}
	if ('0123456789'.indexOf(field.charAt(4))<0) 
	{
		alert('Please enter a Canadian postal code in the following format: A1B-2C3');		
		return false;
	}
	if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(field.charAt(5))<0) 
	{
		alert('Please enter a Canadian postal code in the following format: A1B-2C3');		
		return false;
	}
	if ('0123456789'.indexOf(field.charAt(6))<0) 
	{	
		alert('Please enter a Canadian postal code in the following format: A1B-2C3');	
		return false;
	}
	
	return true;
}


function validZipCode(field) 
{
	// Reference - http://javascript.internet.com/forms/val-zip-code.html
	
	var valid = "0123456789-";
	var hyphencount = 0;

	if (field.length!=5 && field.length!=10)
	{
	
		alert("Please enter your 5 digit or 5 digit+4 zip code.");		
		return false;
	}
	
	for (var i=0; i < field.length; i++) 
	{
		temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") 
		{
			alert("Invalid characters in your zip code.  Please try again.");			
			return false;
		}
		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-"))
		{
			alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");	
			return false;
		}
	}
	
	return true;
}


function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}

	return result;
}

function validEmail(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		formField.focus();
		result = false;
	}

  return result;

}

function validNum(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;

 	if (result)
 	{
 		if (!allDigits(formField.value))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();
			result = false;
		}
	}

	return result;
}


function validInt(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;

 	if (result)
 	{
 		var num = parseInt(formField.value,10);
 		if (isNaN(num))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();
			result = false;
		}
	}

	return result;
}


function validDate(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;

 	if (result)
 	{
 		var elems = formField.value.split("/");

 		result = (elems.length == 3); // should be three components

 		if (result)
 		{
 			var month = parseInt(elems[0],10);
  			var day = parseInt(elems[1],10);
 			var year = parseInt(elems[2],10);
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && (day > 0) && (day < 32) &&
					 allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
 		}

  		if (!result)
 		{
 			alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.');
			formField.focus();
		}
	}

	return result;
}

function radio_button_checker(theForm)
{
	// set var radio_choice to false
	var radio_choice = false;
	var radio_choiceother = false;

	// Loop from zero to the one minus the number of radio button selections
	for (counter = 0; counter < theForm.heard.length; counter++)
	{
		// If a radio button has been selected it will return true
		// (If not it will return false)
		if (theForm.heard[counter].checked)
			radio_choice = true;
	}


	if (!radio_choice)
	{
		// If there were no selections made display an alert box
		theForm.fname.focus();
		alert("Please enter a value for the how did you hear about us field.")
		radio_choice = false;
	}

return radio_choice;
}


function checkwhatsChecked()
{

	document.VigilantInc.fname.className="disable";
	document.VigilantInc.lname.className="disable";

	if(document.VigilantInc.requestinfo[0].checked)
	{
	document.VigilantInc.email.className="disable";
	document.VigilantInc.street1.className="disable";
	document.VigilantInc.city.className="disable";
	document.VigilantInc.state.className="disable";
	document.VigilantInc.zip.className="disable";

	}
	else if(!document.VigilantInc.requestinfo[0].checked)
	{
	document.VigilantInc.street1.className="";
	document.VigilantInc.city.className="";
	document.VigilantInc.state.className="";
	document.VigilantInc.zip.className="";
	document.VigilantInc.email.className="";
	}

	if(document.VigilantInc.requestinfo[1].checked)
	{
	document.VigilantInc.phone.className="disable";
	}
	else if(!document.VigilantInc.requestinfo[1].checked)
	{
	document.VigilantInc.phone.className="";
	}

	if((document.VigilantInc.requestinfo[2].checked) || (document.VigilantInc.requestinfo[3].checked))
	{
	document.VigilantInc.email.className="disable";
	}
	else if(!document.VigilantInc.requestinfo[2].checked)
	{
	//document.VigilantInc.email.className="";
	}



	if((!document.VigilantInc.requestinfo[0].checked) && (!document.VigilantInc.requestinfo[1].checked) && (!document.VigilantInc.requestinfo[2].checked) && (!document.VigilantInc.requestinfo[3].checked))
	{
	document.VigilantInc.phone.className="";
	document.VigilantInc.email.className="";
	document.VigilantInc.street1.className="";
	document.VigilantInc.city.className="";
	document.VigilantInc.state.className="";
	document.VigilantInc.country.className="";
	document.VigilantInc.zip.className="";
	}

}
function autoselect()
{

//	if(document.VigilantInc.magazinetype.value != ""){
//		document.VigilantInc.elements.heard[1].checked = true;
//	}else if(document.VigilantInc.magazinetype.value == "Choose One"){
//		document.VigilantInc.elements.heard[1].checked = false;	
//	}
}
function autoselect2()
{
//	if(document.VigilantInc.SearchEngine.value != ""){
//		document.VigilantInc.elements.heard[0].checked = true;
//	}else if(document.VigilantInc.SearchEngine.value == "Choose One"){
//		document.VigilantInc.elements.heard[0].checked = false;	
//	}
}


var letters=',./;:?"!@#$%^&*()-=0123456789ABCÇDEFGHIJKLMNÑOPQRSTUVWXYZabcçdefghijklmnñopqrstuvwxyzàáÀÁéèÈÉíìÍÌïÏóòÓÒúùÚÙüÜ '

function alpha(e,allow) {
var k;
k=document.all?parseInt(e.keyCode): parseInt(e.which);
return (allow.indexOf(String.fromCharCode(k))!=-1);
}
