// JavaScript Document
function strip_tags(str, allowed_tags) {
 
    var key = '', allowed = false;
    var matches = [];
    var allowed_array = [];
    var allowed_tag = '';
    var i = 0;
    var k = '';
    var html = '';
 
    var replacer = function(search, replace, str) {
        return str.split(search).join(replace);
    };
 
    // Build allowes tags associative array
    if (allowed_tags) {
        allowed_array = allowed_tags.match(/([a-zA-Z]+)/gi);
    }
  
    str += '';
 
    // Match tags
    matches = str.match(/(<\/?[\S][^>]*>)/gi);
 
    // Go through all HTML tags
    for (key in matches) {
        if (isNaN(key)) {
            // IE7 Hack
            continue;
        }
 
        // Save HTML tag
        html = matches[key].toString();
 
        // Is tag not in allowed list? Remove from str!
        allowed = false;
 
        // Go through all allowed tags
        for (k in allowed_array) {
            // Init
            allowed_tag = allowed_array[k];
            i = -1;
 
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
            if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}
 
            // Determine
            if (i == 0) {
                allowed = true;
                break;
            }
        }
 
        if (!allowed) {
            str = replacer(html, "", str); // Custom replace. No regexing
        }
    }
 
    return str;
}

function trim (str, charlist) {
 
    var whitespace, l = 0, i = 0;
    str += '';
    
    if (!charlist) {
        // default list
        whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
    } else {
        // preg_quote custom list
        charlist += '';
        whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
    }
    
    l = str.length;
    for (i = 0; i < l; i++) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(i);
            break;
        }
    }
    
    l = str.length;
    for (i = l - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    
    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}

function validate(form){
	var errors = ''; //errors array initialized.
	//check for email address
	if(form.EmailAddress.value==''){
		errors+= "You did not enter an email address.\n";
	//}else if(!eregi('^[[:alnum:]][a-z0-9_\.\-]+@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim(form.EmailAddress.value)))){
		//errors += "The email address is not in the valid format.\n";
	}else{
		var email = strip_tags(trim(form.EmailAddress.value));
	}
	//check for first name
	if(form.FirstName.value==''){
		errors += "You did not enter your first name.\n";
	//} else if(!eregi('^[[:alpha:]]{2,}$', stripslashes(trim(form.FirstName.value)))){
		//errors += "The First Name was not in the correct format.\n";
	}else {var first = strip_tags(trim(form.FirstName.value));
	}
	//check for the last name
	if(form.LastName.value==''){
		errors += "You did not enter your last name.\n";
	//}else if(!eregi('^[[:alpha:]]{2,}$', stripslashes(trim(form.LastName.value)))){
		//errors += "The Last Name was not in the correct format.\n";
	}else {var last = strip_tags(trim(form.LastName.value));
	}
	//check for the first address field
	if(form.MailingAdd_Street.value==''){
		errors += "You did not enter your address.\n";
	} /*else if(!eregi('^[[:alnum:]\n\r\v\f\t]$', stripslashes(trim(form.MailingAdd_Street.value)))){
		errors += "The address 1 field was not in the correct format";
		}*/
	else {var address = strip_tags(trim(form.MailingAdd_Street.value));
	}
	//check to ensure a state has been entered.
	if(form.MailingAdd_State.value == ""){
		errors += "You did not select a state.\n";
	}else {
		var state = form.MailingAdd_State.value;
	}
	//check for city
	if(form.MailingAdd_City.value==''){
		errors += "You did not enter your city name\n";
	//}else if(!eregi('^[[:alpha:]\.\' \-]{3,}$', stripslashes(trim(form.MailingAdd_City.value)))){
		//errors += "The City name was in the incorrect formatn";
	}
	else{
		var city = strip_tags(trim(form.MailingAdd_City.value));
	}
	//check for zipcode	
	if(form.MailingAdd_City.value==''){
		errors += "You did not enter your zipcode.\n";
		}
		else{
		var zip = form.MailingAdd_City;
	}
	//check for the phone number.	
	if(form.CellPhoneNumber.value==''){
		errors += "You did not enter a valid phone number.\n";
	}else{var phone = strip_tags(form.CellPhoneNumber.value);
	}
	
	if(errors!==''){
		alert(errors);
		return false;
	}
	return true;
}