function getkey(e) {
	if (window.event)
	   return window.event.keyCode;
	else if (e)
	   return e.which;
	else
	   return null;
}

function goodchars(e, goods) {
	var key, keychar;
	key = getkey(e);
	if (key == null) return true;

	// get character
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	goods = goods.toLowerCase();

	// check goodkeys
	if (goods.indexOf(keychar) != -1)
			return true;

	// control keys
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
	   return true;

	// else return false
	return false;
}

function ValidateControl(control, def, prompt) {
  if (control.value=="") {
    alert("The " + prompt +" field is a required field, and it must be filled in before your form can be sent to our server.")
    control.focus()
    return false }
  if (control.value==def) {
    alert("The " + prompt +" field is a required field, and it must be filled in before your form can be sent to our server.")
    control.focus()
    return false }
  return true
}

function validate_phone(theControl) {
 if(theControl.value.search(/\d{3}\-\d{3}\-\d{4}/)==-1)
   {
      alert("The phone number you entered is not valid.\r\nPlease enter a phone number with the format xxx-xxx-xxxx.");
      return false;
   }
   return true;
}

function validate_email(theControl) {

	if ( theControl.value == "" || theControl.value.length <= 0 )   {
	   alert("I'm sorry. This email address must be filled in correct to send the form to our server. Please"

   		+" check the prefix and '@' sign.");
	   theControl.focus()

	   return false;

    }

    var reEmail = /^.+\@.+\..+$/
    var holderValue;
    var thisValue = theControl.value;

    // Check for e-mail addresses from ISPs and other sources that have been consistently
    // entered incorrectly.  If detected, correct the situation.
    if  (

            (thisValue.substring(thisValue.length - 4, thisValue.length).toLowerCase()) == '@aol' ||
            (thisValue.substring(thisValue.length - 4, thisValue.length).toLowerCase()) == '@msn' ||
            (thisValue.substring(thisValue.length - 6, thisValue.length).toLowerCase()) == '@yahoo' ||
            (thisValue.substring(thisValue.length - 6, thisValue.length).toLowerCase()) == '@lycos' ||
            (thisValue.substring(thisValue.length - 7, thisValue.length).toLowerCase()) == '@excite' ||
            (thisValue.substring(thisValue.length - 10, thisValue.length).toLowerCase()) == '@altavista' ||
            (thisValue.substring(thisValue.length - 11, thisValue.length).toLowerCase()) == '@compuserve' ||
            (thisValue.substring(thisValue.length - 8, thisValue.length).toLowerCase()) == '@prodigy' ||
            (thisValue.substring(thisValue.length - 8, thisValue.length).toLowerCase()) == '@hotmail' ||
            (thisValue.substring(thisValue.length - 9, thisValue.length).toLowerCase()) == '@netscape'
        )

        {

            holderValue = thisValue.concat('.com');
            thisValue = holderValue;
            theControl.value = thisValue;

        }

    if  (

            (thisValue.substring(thisValue.length - 5, thisValue.length).toLowerCase()) == '@home'

        )

        {

            holderValue = thisValue.concat('.net');
            thisValue = holderValue;
            theControl.value = thisValue;

        }

    // Now check the actual value of the e-mail address for validity.

    var flagFirstCheck = (theControl.value.length < 6) ||
        (thisValue.indexOf('@') == -1) ||
        (thisValue.indexOf('.') == -1) ||
        (thisValue.indexOf('@',(thisValue.indexOf('@')+1)) != -1) ||
        ((thisValue.indexOf('.')+1) == thisValue.length) ||
        ((thisValue.indexOf('@')+1) == thisValue.length)

    var flagSecondCheck = reEmail.test(thisValue)
    if ( flagFirstCheck || !flagSecondCheck)
    {

   alert("I'm sorry. This email address seems to be incorrect. Please"
   +" check the prefix and '@' sign.");
   theControl.focus()

        return false;

    }

    else {
        return true;

    }

}

function Sanitize_Fields(f) {

	// Sanitize Fields
	for(i=0; i<f.elements.length; i++) {
		if ((f.elements[i].type == 'text') || (f.elements[i].type == 'textarea')) {
			if ( f.elements[i].value.match("<html") ||
				 f.elements[i].value.match("<body") ||
				 f.elements[i].value.match("<f") ||
				 f.elements[i].value.match("<href") ||
				 f.elements[i].value.match("http:") ||
				 f.elements[i].value.match("<a") ||
				 f.elements[i].value.match("<img") ||
				 f.elements[i].value.match("<javascript") ||
				 f.elements[i].value.match("<vbscript") ||
				 f.elements[i].value.match("<script") ||
				 f.elements[i].value.match("<object") ||
				 f.elements[i].value.match("content-type") ||
				 f.elements[i].value.match("document.write") ||
				 f.elements[i].value.match("<\?php") ||
				 f.elements[i].value.match("bcc:") ||
				 f.elements[i].value.match("content-type") ||
				 f.elements[i].value.match("<\?xml") ) {
					alert("Sorry .. you are entering text that is unacceptable.")
					f.elements[i].focus();
					return false; }
		}
	}

	return true;
}

function ValidateCaptcha(formobj) {
  
	if (formobj.randomcode.value != formobj.code.value)
  {
  
	alert("The correct image must be selected before your form can be processed.");
    
	return false;
  }
  return true;
}


	
	function ContactSubmitValidation (formobj) {
			if (!Sanitize_Fields(formobj)) return false		

			if (!ValidateControl(formobj.fullname,'Name','Name')) return false
			if (!ValidateControl(formobj.address,'Address','Address')) return false
			if (!ValidateControl(formobj.city,'City','City')) return false	
			if (!ValidateControl(formobj.state,'State','State')) return false
        	
			if (!ValidateControl(formobj.zip,'Zip','Zip')) return false
			if (!ValidateControl(formobj.phone,'Phone','Phone')) return false
			if (!validate_phone (formobj.phone)) return false
			if (!validate_email(formobj.email)) return false
			if (!ValidateCaptcha(formobj)) return false
		

        
			return true
	}



function RegisterSubmitValidation (formobj) {
			if (!Sanitize_Fields(formobj)) return false		
			if (!ValidateControl(formobj.fullname,'Name','Name')) return false
			if (!ValidateControl(formobj.address,'Address','Address')) return false
			if (!ValidateControl(formobj.city,'City','City')) return false	
			if (!ValidateControl(formobj.state,'State','State')) return false
        	if (!ValidateControl(formobj.zip,'Zip','Zip')) return false
			if (!ValidateControl(formobj.phone,'Phone','Phone')) return false
			if (!validate_phone (formobj.phone)) return false
			if (!validate_email(formobj.email)) return false
			if (!ValidateCaptcha(formobj)) return false	     
			return true
	}

function newsletterSubmitValidation (formobj) {
if (!Sanitize_Fields(formobj)) return false		
if (!ValidateControl(formobj.firstname,'FirstName','FirstName')) return false
if (!ValidateControl(formobj.lastname,'LastName','LastName')) return false
if (!ValidateControl(formobj.zipcode,'Zip','Zip')) return false
if (!validate_email(formobj.email)) return false
if (!ValidateCaptcha(formobj)) return false


return true


}


