/*===============================================================================*/
//	contactus.js
//	  Created 11/24/2008 by Greg Spry
//	    Javacript for American Vacuum Contact Us Page
//
/*===============================================================================*/
function submitContactForm(theForm) {
	var product_list = '';
	with(theForm) {
		if(name.value == '') {
			alert('Please enter your first and last name.');
			name.focus();
			return false;
		}
		if(company.value == '') {
			alert('Please enter the name of your company or organization.');
			company.focus();
			return false;
		}
		if(imEmailField.value == '') {
			alert('Please enter your email address.');
			imEmailField.focus();
			return false;
		}
		if(!isValidEmailAddress(imEmailField.value)) {
			alert('Please enter a valid email address.');
			imEmailField.focus();
			return false;
		}
		if(phone.value == '') {
			alert('Please enter your phone number.');
			phone.focus();
			return false;
		}
		if(city.value == '') {
			alert('Please enter the name of your city.');
			city.focus();
			return false;
		}
		if(country.value == 'United States' && state.value == '0') {
			alert('Please select your state.');
			state.focus();
			return false;
		}
		if(country.value != 'United States' && territory.value == '') {
			alert('Please enter your territory.');
			territory.focus();
			return false;
		}
		if(ZIP.value == '') {
			alert('Please enter your zip code.');
			ZIP.focus();
			return false;
		}
		if(comments.value == '') {
			alert('Please enter your comment or question.');
			comments.focus();
			return false;
		}
		
		// Get multiple product selections
		for (var i = 0; i < products.options.length; i++)  {
			if (products.options[i].selected) {
				product_list += products.options[i].value + ', ';
			}
		}
		if(product_list != '') {
			product_list = product_list.substring(0, product_list.length - 2)
		}
		allProducts.value =	product_list;
		
		// Post the contact to Insite Metrics
		insitePost('11nn7aq-3a6ynmb5oj', 'contactUsForm', 'imEmailField', 'American Vacuum - New Contact');
		
		// Submit the form and send the email
		submit();
	}
}

/*===============================================================================*/
function isValidEmailAddress(stringValue) {
	return stringValue.match(/^(\w|-|_|\.)+@(\w|-|_|\.){2,}\.\w{2,5}$/)
}

/*===============================================================================*/
function tidyPhoneNumber(textInput) {
	with (textInput) {
		if (value != "") {
			if (value.indexOf("x") == -1) {
			rawNumber = value;
			rawExtension = ""
		}
		else {  // we'll strip off the extension portion
			rawNumber    = value.substring(0, value.indexOf("x"));
			rawExtension = value.substring(value.indexOf("x"));
		}
		
		// let's now get the numerals from the number:
		rawDigits = extractCharsInBag(rawNumber, "0123456789");
		
		// Strip off a leading one if present:
		if (rawDigits.charAt(0) == '1')
			rawDigits = rawDigits.substring(1);
			
		// Now we expect the number to have 10 digits.
		if (rawDigits.length == 10)  // we can put it into the format (xxx) xxx-xxxx
			phoneNumber =  "(" + rawDigits.substring(0, 3) + ") " +
										 rawDigits.substring(3, 6) + "-" + rawDigits.substring(6, 10);
		else // we'll not mess around with it
			phoneNumber = rawNumber;
		
		// Now let's tend to the extension:
		extension = extractCharsInBag(rawExtension, "0123456789");
		if (extension.length > 0)
			extension = " x" + extension;
			
		// Both parts done, so let's set the value with our result:
		value = phoneNumber + extension;
		}
	}
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function extractCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }
    return returnString;
}

/*===============================================================================*/
function toggleStateOptions() {
	
	var state = $('state');
	var territory = $('territory');
	var stateField = $('stateField');
	
	if($('country').value == 'United States') {
		state.setStyles({
			'visibility': 'visible',
			'position': 'relative'
		});
		territory.setStyles({
			'visibility': 'hidden',
			'position': 'absolute'
		});
		stateField.setHTML('State:&nbsp;&nbsp;');
	}
	else {
		territory.setStyles({
			'visibility': 'visible',
			'position': 'relative'
		});
		state.setStyles({
			'visibility': 'hidden',
			'position': 'absolute'
		});
		stateField.setHTML('Territory:&nbsp;&nbsp;');
		state.options[0].selected = true;
	}
	
}

/*===============================================================================*/

