function ShowError(field_name) {
	if (document.contact_form.elements[field_name].type == "text" || document.contact_form.elements[field_name].type == "textarea") {
		document.contact_form.elements[field_name].className = "text_box_error";
	}
	var label = field_name + "_label";
	document.getElementById(label).className = "form_label_error";
	document.contact_form.elements[field_name].focus();
}

function ClearErrors() {
	for (var i=0; i<document.contact_form.elements.length; i++) {
		if (document.contact_form.elements[i].className == "text_box_error") {
			document.contact_form.elements[i].className = "text_box";
			var label = document.contact_form.elements[i].name + "_label";
			document.getElementById(label).className = "form_label";
		}
	}
	// reset the product literature, company literature, and sales call labels
	document.getElementById('product_literature_label').className = "";
	document.getElementById('company_literature_label').className = "";
	document.getElementById('sales_call_label').className = "";
}

function SendInformation() {

	ClearErrors();

	var requesting_info = false;
	
	if (document.contact_form.name.value == "") {
		alert("Please enter your name.");
		ShowError('name');
		return false;
	}
	
	// if samples exist, make sure the user fills in name and address - AND A TAX ID NUMBER!
	if (document.contact_form.fabric_samples.options[0].value != "" || document.contact_form.product_literature.checked || document.contact_form.company_literature.checked) {
		
		requesting_info = true;
		
		if (document.contact_form.address.value == "" || document.contact_form.city.value == "" || document.contact_form.state.value == "") {
			alert("We need an address to know where to send your information!");
			ShowError('address2');
			ShowError('city');
			ShowError('state');
			ShowError('zip');
			ShowError('country');
			ShowError('address');
			return false;
		}
		
	}
	
	if (document.contact_form.fabric_samples.options[0].value != "" && document.contact_form.tax_id.value == "") {
		alert("Fabric sample cards are available for qualified businesses only. A Tax Resale ID number is required to request samples!");
		ShowError('tax_id');
		return false;
	}
	
	// if user request a salesperson contact them, make sure name and phone or email is filled in
	if (document.contact_form.sales_call.checked) {
		
		requesting_info = true;
		
		if (document.contact_form.name.value == "") {
			alert("You've requested a salesperson contact you. Please enter your name.");
			ShowError('name');
			return false;
		}
		if (document.contact_form.phone.value == "" && document.contact_form.email.value == "") {
			alert("You've requested a salesperson contact you. Please enter a phone number or email address.");
			ShowError('email');
			ShowError('phone');
			return false;
		}
	}
	
	// prevent some crappy half empty submissions - must be requesting or commenting...
	
	if (!requesting_info && document.contact_form.comments.value == "") {
		alert("Please use this form to send your comments or request information. You need to tick a checkbox to request the appropriate information, or enter your comments & questions in the space provided.");
		ShowError('product_literature');
		ShowError('company_literature');
		ShowError('comments');
		return false;
	}
	
	// if an email is entered, check that it is a legit format
	if (document.contact_form.email.value != "") {
		if (!ValidEmail(document.contact_form.email.value)) {
			alert("I think you mistyped your email address. We can't reach you with that one.");
			ShowError('email');
			return false;
		}
    }
	
	
	document.images['spinner'].src = "/images/spin.gif";
	
	document.contact_form.submit();
	
	//alert(document.contact_form.ymd.value);

}



function RemoveSampleReply() {
	if(http.readyState == 4){
		document.getElementById('sample_list').innerHTML = http.responseText;
	}
}

function RemoveSample() {
	
	if (document.contact_form.fabric_samples.selectedIndex == -1) {
		alert("Please select the sample you would like to remove.");
		return false;
	}
	else if (document.contact_form.fabric_samples.options[document.contact_form.fabric_samples.selectedIndex].value == "") {
		return false;
	}
	else {
		// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
		var fabric_sample = document.contact_form.fabric_samples.options[document.contact_form.fabric_samples.selectedIndex].text;
		
		// Set the random number to add to URL request
		nocache = Math.random();
		http = createObject();
		
		// Pass the sample variables like URL variable
		http.open('get', '/contact/includes/remove_sample.php?remove=' + fabric_sample + '&nocache = '+nocache);
		http.onreadystatechange = RemoveSampleReply;
		http.send(null);
	}
}