// returns true if the string is a US phone number formatted as...
// (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
function isPhoneNumber(str){
  var re = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
  return re.test(str);
}
// returns true if the string is a valid email
function isEmail(str){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str)){
		return true;
	}else{
		return false;
	}
}
function isValidZipCode(str){
   var reg = /^\d{5}([\-]\d{4})?$/;
  return reg.test(str);
}
function isValidName(str){
   var reg = /^[a-zA-Z\s.\-]+$/;
  return reg.test(str);
}


// returns true if the string is empty
function isEmpty(str){
  return (str == null) || (str.length == 0) || (str.length == '');
}


//validate hotLeadForm inputs in contact.html
function validateHotLeadForm(f){
    
    var errors = '';
	var i,e,t,n,v;
	var alertStr;
	var em,phone;
	var c=0;
	for(i=0; i < f.elements.length; i++){
		e = f.elements[i];
		t = e.type;
		if(!e.type) continue;
		n = e.name;
		v = e.value;
		
		nametest = n.toLowerCase();
		
		if(t == 'text' || t == 'Text' || t == 'textarea' || t == 'Textarea' || t=='radio'){
			v = validated(v);
			f.elements[i].value = v;
			
			if(nametest.indexOf("firstname") != -1){
			    
				if(!isValidName(v)){
				     errors += 'You must enter a valid First Name.\n'; continue;
				   }
				   
				  
			}
			if(nametest.indexOf("lastname") != -1){
			    
				if(!isValidName(v)){
				   
				   errors += 'You must enter a valid Last Name.\n'; continue;
				}
			}
				
		  if(nametest.indexOf("email") != -1){
			    if(!isEmail(v) )
			    {  
			      if(n == 'email'){errors += 'You must enter a valid Email.\n'; continue;}
			       
			      if(n == 'emailConf' && em != v){errors +='The e-mail addresses you entered do not match.\n'; continue;}
			    
			       	               
			    }else{
			       if(n == 'email'){em=v;continue;}
			       
			       if(n == 'emailConf' && em != v ){errors +='The e-mail addresses you entered do not match.\n Please correct and re-submit.\n'; continue;}
			    }
			        			
			}
			
			if( nametest.indexOf("street") != -1){
				if(v == null || v =='' ){
					errors += 'Street cannot be empty.\n'; continue;
				}
			}
			
			if( nametest.indexOf("city") != -1){
				if(v == null || v =='' ){
					errors += 'City cannot be empty.\n'; continue;
				}
			}
			
			if(nametest.indexOf("zip") != -1){
				if(!isValidZipCode(v)){
					errors += 'You must enter a valid Zipcode.\n'; continue;
				}
			}
			
			if(nametest.indexOf("phone1") != -1){phone=v;}
			if(nametest.indexOf("phone2") != -1){phone += v ;}
			if(nametest.indexOf("phone3") != -1){  
			     phone += v ;
			     if(!isPhoneNumber(phone)){errors += 'You must enter a valid Phone Number.\n'; continue;}
			}
			 
				
			if( nametest.indexOf("question") != -1){
				if(v == null || v =='' ){
					errors += 'Question cannot be empty.\n'; continue;
				}
			}
			
			if( nametest.indexOf("preferredcalltime") != -1){
			    
				if(e.checked){c=1;}
				if( (c == 0) && (errors.indexOf("Preferred") == -1))
				   { 
				    
				    errors += 'You must select Preferred Time to Call.\n'; 
				    continue;
				   }
			}
			if (e.required ){
				if (isEmpty(v)){ 
					errors += n+' cannot be empty.\n'; continue;
				}
			}else{
				continue;
			}
		}
	}
	if(errors != '') {
		alert(errors);
		return false;
	}
	return true;
}
// validate the form
function validateForm(f){
	var errors = '';
	var i,e,t,n,v;
	var alertStr;
	for(i=0; i < f.elements.length; i++){
		e = f.elements[i];
		t = e.type;
		if(!e.type) continue;
		n = e.name;
		v = e.value;
		nametest = n.toLowerCase();
		if(t == 'text' || t == 'Text' || t == 'textarea' || t == 'Textarea'){
			v = validated(v);
			f.elements[i].value = v;
			
			if(nametest.indexOf("email") != -1){
				if(!isEmail(v)){
				if(n == 'recipientEmail'){
				errors += 'Recipient\'s Email is not valid.\n'; continue;
				}else{
				 errors += 'Email is not valid.\n'; continue;
				}}
			}
			if(nametest.indexOf("zip") != -1){
				if(!isValidZipCode(v)){
					errors += 'Zipcode is not valid.\n'; continue;
				}
			}
			if(nametest.indexOf("name") != -1){
				if(!isValidName(v)){
				if(n == 'recipientName'){
					errors += 'Recipient\'s Name is not valid.\n'; continue;
					}else{
					errors += 'Name is not valid.\n'; continue;
					}
				}
			}
			if(nametest.indexOf("topic") != -1 ){
				if(v == null || v =='' ){
					errors += n+' cannot be empty.\n'; continue;
				}
			}
			if( nametest.indexOf("message") != -1){
				if(v == null || v =='' ){
					errors += 'message cannot be empty.\n'; continue;
				}
			}
			if (e.required ){
				if (isEmpty(v)){ 
					errors += n+' cannot be empty.\n'; continue;
				}
			}else{
				continue;
			}
		}
	}
	if(errors != '') {
		alert(errors);
		return false;
	}
	return true;
}
function validated(string) {
    for (var i=0, output='', invalid="[]{}<>'\""; i<string.length; i++)
       if (invalid.indexOf(string.charAt(i)) != -1){
	   }else{
          output += string.charAt(i);
	   }
	  
    return output;
} 

