var failsafe;
function chkMethod(elem){
	var what = elem.id;
	if(elem.checked==true){
		if(what=='cash'){
			document.getElementById('check').checked = false;
			document.getElementById('charge').checked = false;
		} else if(what=='check'){
			document.getElementById('cash').checked = false;
			document.getElementById('charge').checked = false;
		} else if(what=='charge'){
			document.getElementById('cash').checked = false;
			document.getElementById('check').checked = false;
		}
	}
}
function chkPhone(){
	if(document.getElementById('phone').value=='include area code'){
		document.getElementById('phone').value = '';
		document.getElementById('phone').style.color = '#000000';
	} else if(document.getElementById('phone').value=='') {
		document.getElementById('phone').value = 'include area code';
		document.getElementById('phone').style.color = '#838383';
	} else {
		document.getElementById('phone').style.color = '#000000';
	}
}
function paraPhone(){
	var phoneNum = document.getElementById('phone').value;
	if(phoneNum.length==3){
		if(phoneNum.indexOf("(")!=0){
			document.getElementById('phone').value = '('+phoneNum+') ';
		}
		failsafe = 'yes';
	} else if(phoneNum.length==4){
		if(phoneNum.indexOf("(")==0 && failsafe!='no'){
			document.getElementById('phone').value = phoneNum+') ';
		}
		failsafe = 'no';
	} else if(phoneNum.length==5){
		failsafe = 'no';
	} else if(phoneNum.length==8){
		failsafe = 'yes';
	} else if(phoneNum.length==9 && failsafe!='no'){
		document.getElementById('phone').value = phoneNum+'-';
		failsafe = 'no';
	} else if(phoneNum.length==10){
		newPhone = '('+phoneNum.substring(0,3)+') '+phoneNum.substring(3,6)+'-'+phoneNum.substring(6,10);
		document.getElementById('phone').value = newPhone;
	}
}
function sendReq(){
	// Grab all of the submitted information
	var company = document.getElementById('companyname').value;
	var address = document.getElementById('address').value;
	var city = document.getElementById('city').value;
	var state = document.getElementById('state').value;
	var zip = document.getElementById('zip').value;
	var name = document.getElementById('contact').value;
	var phone = document.getElementById('phone').value;
	var brand = document.getElementById('brand').value;
	var model = document.getElementById('model').value;
	var serial = document.getElementById('serial').value;
	var part = document.getElementById('part').value;
	var problem = document.getElementById('problem').value;
	var cash = document.getElementById('cash').checked;
	var check = document.getElementById('check').checked;
	var charge = document.getElementById('charge').checked;
	var params;
	
	if(company!=''){
		if(address!=''){
			if(city!=''){
				if(zip!=''){
					if(name!=''){
						if(phone!=''){
							if(brand!=''){
								if(model!=''){
									if(part!=''){
										if(problem!=''){
											// All of the necessary information is correct and they can go ahead and send the email
											if(serial==''){
												serial = 'No Serial Number(s) given';
											}
											
											if(cash==true){
												method = 'cash';
											} else if(check==true){
												method = 'check';
											} else if(charge==true){
												method = 'charge';
											}
											
											params = 'company='+company+'&address='+address+'&city='+city+'&state='+state+'&zip='+zip+'&name='+name+'&phone='+phone+'&brand='+brand+'&model='+model+'&serial='+serial+'&part='+part+'&problem='+problem+'&method='+method;
											
											// Send it via ajax
											ajaxSend('error', 'sendMail.php', params);
										} else {
											// Let them know to fill in the problem
											document.getElementById('error').innerHTML = 'Error: Please let us know what the problem is so we may better assist you.';
										}
									} else {
										// Let them know to fill in the part
										document.getElementById('error').innerHTML = 'Error: Please tell us what part numbers are necessary to remedy your problem.';
									}
								} else {
									// Let them know to fill in the model
									document.getElementById('error').innerHTML = 'Error: Please let us know what model number you have so we may better assist you.';
								}
							} else {
								// Let them know to fill in the brand
								document.getElementById('error').innerHTML = 'Error: Please enter the brand of the equipment you have so we may better assist you.';
							}
						} else {
							// Let them know to fill in the phone
							document.getElementById('error').innerHTML = 'Error: Please enter your phone number so we may return your request easily.';
						}
					} else {
						// Let them know to fill in the contact name
						document.getElementById('error').innerHTML = 'Error: Please let us know who we are contacting to get this issue resolved.';
					}
				} else {
					// Let them know to fill in the zip
					document.getElementById('error').innerHTML = 'Error: Please enter the company zip code.';
				}
			} else {
				// Let them know to fill in the city
				document.getElementById('error').innerHTML = 'Error: Please fill in the city field for us.';
			}
		} else {
			// Let them know to fill in the street address
			document.getElementById('error').innerHTML = 'Error: Please let us know what your street address is.';
		}
	} else {
		// Let them know that they forgot to enter a company name
		document.getElementById('error').innerHTML = 'Error: Please let us know which company you are with so we may better assist you.';
	}
}
function ajaxSend(div, url, params){
	var xmlHttp;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){
		// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
				// Doesn't support AJAX - Upgrade time?
				return false;
			}
		}
	}
	
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState==4&&xmlHttp.status==200){
			document.getElementById(div).innerHTML=xmlHttp.responseText;
			
			// get rid of all the previous input
			document.getElementById('companyname').value = '';
			document.getElementById('address').value = '';
			document.getElementById('city').value = '';
			document.getElementById('state').value = '';
			document.getElementById('zip').value = '';
			document.getElementById('contact').value = '';
			document.getElementById('phone').value = 'include area code';
			document.getElementById('brand').value = '';
			document.getElementById('model').value = '';
			document.getElementById('serial').value = '';
			document.getElementById('part').value = '';
			document.getElementById('problem').value = '';
			document.getElementById('cash').checked = false;
			document.getElementById('check').checked = false;
			document.getElementById('charge').checked = false;
			document.getElementById('phone').style.color = '#838383';
		}
	}
	
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}