// JavaScript Document

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function test() {
		$chk = document.myForm.chk_aftermarket.checked;
		alert('weve got a '+$chk);
		//document.getElementById('mail_results').innerHTML = 'Something Happened!!';
}

function clear_form() {
	
document.myForm.name.value = "";
document.myForm.title.value ="";
document.myForm.company.value = "";
document.myForm.address.value = "";
document.myForm.city.value = "";
document.myForm.state.value = "";
document.myForm.zip.value = "";
document.myForm.fax.value ="";
document.myForm.phone.value = "";
document.myForm.email.value = "";
document.myForm.aft_serial_no.value = "";
document.myForm.aft_part_no.value = "";
document.myForm.aft_part_description.value = "";
document.myForm.aft_part_quantity.value ="";
document.myForm.svc_serial_no.value = "";
document.myForm.svc_comment.value = "";
document.myForm.equip_comments.value = "";
document.myForm.add_comments.value = "";
document.myForm.chk_aftermarket.checked = false;
document.myForm.aft_shp_standard.checked = false;
document.myForm.aft_shp_expidited.checked = false;
document.myForm.svc_department.checked = false;
document.myForm.chk_equip.checked = false;


//error spans
clear_errs();



}

function clear_errs() {
	document.getElementById('name_err').innerHTML = "";
document.getElementById('company_err').innerHTML = "";
document.getElementById('phone_err').innerHTML = "";
document.getElementById('email_err').innerHTML = "";

document.getElementById('aftermarket_err').innerHTML = "";
document.getElementById('svc_comment_err').innerHTML = "";
document.getElementById('equip_comments_err').innerHTML = "";
document.getElementById('gen_err').className = 'normal';
document.getElementById('svc_serial_no_err').innerHTML = "";
document.getElementById('mail_results').innerHTML = "";

	
}


function validate() {

	
	//clear error msgs
	clear_errs();

	var valid = 1;
	
	//validate inputs
	if(document.myForm.name.value == "") {
	//display error and exit
		document.getElementById('name_err').innerHTML = " *Required Field";
		valid = 0;
		//document.MapForm.vendor_err.innerHTML = "*Required Field";
	}
	if (document.myForm.phone.value == "") {
		document.getElementById('phone_err').innerHTML = " *Required Field";
		valid = 0;
	}
	
	//check for at least one check box
	var b_aft = document.myForm.chk_aftermarket.checked;
	var b_svc = document.myForm.svc_department.checked;
	var b_equip = document.myForm.chk_equip.checked;
	
	//check first for at least on checked, then all required for each checked
	if (b_aft == true || b_svc == true || b_equip == true) {
		if (b_aft) {
			//check aftermarket fields // require at least one of 4 inputs must be filled out
			b_aft_used = true
			b_aft_used2 = true
			b_aft_used3 = true
			b_aft_used4 = true
			if (document.myForm.aft_serial_no.value == "") {
				b_aft_used = false;
			}
			if (document.myForm.aft_part_no.value == "") {
				b_aft_used2 = false;
			}
			if (document.myForm.aft_part_description.value == "") {
				b_aft_used3 = false;
			}
			if (document.myForm.aft_part_quantity.value == "") {
				b_aft_used4 = false;
			}
			if (b_aft_used == false && b_aft_used2 == false && b_aft_used3 == false && b_aft_used4 == false) {
				valid = 0;
				document.getElementById('aftermarket_err').innerHTML = "Please include at least one detail.";
			}
		}
		
		if (b_svc) {
			//check service fields // both fields required
			if (document.myForm.svc_serial_no.value == "") {
				document.getElementById('svc_serial_no_err').innerHTML = " Required Field.";
				valid = 0;
			}
			else {
				if (document.myForm.svc_comment.value == "") {
					document.getElementById('svc_comment_err').innerHTML = " Please include a description.";
					valid = 0;
				}
			}
					
		}
		
		if (b_equip) {
			//check equipment request fields
			if (document.myForm.equip_comments.value == "") {
		document.getElementById('equip_comments_err').innerHTML = " Please include a request.";
		valid = 0;
	}
			
		}	
	}
	else {
		valid = 0;
		document.getElementById('gen_err').className = 'err';
		window.location = '#gen_err_mrk';
	}
	
	
	if(valid == 1) {
	document.getElementById('mail_results').innerHTML = 'Checking....';
	sendemail();
	//alert('sending mail');
	}
	
	

}


function sendemail() {
	
	//get values
	var name = document.myForm.name.value;
	var title = document.myForm.title.value;
	var company = document.myForm.company.value;
	var address = document.myForm.address.value;
	var city = document.myForm.city.value;
	var state = document.myForm.state.value;
	var zip = document.myForm.zip.value;
    var fax = document.myForm.fax.value;
	var phone = document.myForm.phone.value;
	var email = document.myForm.email.value;
	var aft_serial_no = document.myForm.aft_serial_no.value;
	var aft_part_no = document.myForm.aft_part_no.value;
	var aft_part_desc = document.myForm.aft_part_description.value;
	var aft_part_quantity = document.myForm.aft_part_quantity.value;
	var svc_serial_no = document.myForm.svc_serial_no.value;
	var svc_comment = document.myForm.svc_comment.value;
	var equip_comments = document.myForm.equip_comments.value;
	var add_comments = document.myForm.add_comments.value;
	var chk_aft = document.myForm.chk_aftermarket.checked;
	var chk_aft_standard = document.myForm.aft_shp_standard.checked;
	var chk_aft_expidite = document.myForm.aft_shp_expidited.checked;
	var chk_svc = document.myForm.svc_department.checked;
	var chk_equip = document.myForm.chk_equip.checked;
	
	document.getElementById('mail_results').innerHTML = 'Sending....';

    http.open('get', 'ajax/quote.php?name='+name+'&title='+title+'&company='+company+'&address='+address+'&city='+city+'&state='+state+'&zip='+zip+'&name='+name+'&email='+email+'&fax='+fax+'&phone='+phone+'&aft_serial_no='+aft_serial_no+'&aft_part_no='+aft_part_no+'&aft_part_desc='+aft_part_desc+'&aft_part_quantity='+aft_part_quantity+'&svc_serial_no='+svc_serial_no+'&svc_comment='+svc_comment+'&equip_comments='+equip_comments+'&add_comments='+add_comments+'&chk_aft='+chk_aft+'&chk_aft_standard='+chk_aft_standard+'&chk_aft_expidite='+chk_aft_expidite+'&chk_svc='+chk_svc+'&chk_equip='+chk_equip+'&action=send');
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();

        if(response.indexOf('|' != -1)) {
            update = response.split('|');
			//var display = document.getElementById(update[0]).innerHTML;
			if(update[0] == 'Loading...') {
			document.getElementById('mail_results').innerHTML = 'Generic Message';
			}
			else {
            document.getElementById(update[0]).innerHTML = update[1];
			}
         
        }
    }
}

function is_selected_aft() {
	if (document.getElementById('chk_aftermarket').checked) {
		enable_aft();
	
	} else {
		//disable
		disable_aft();
	}
	
}

function is_selected_service() {
	if (document.getElementById('svc_department').checked) {
		enable_service();

	} else {
		//disable
		disable_service();
	}
	
}

function is_selected_equip() {
	if (document.getElementById('chk_equip').checked) {
		enable_equip();
	
	} else {
		//disable
		disable_equip();
	}
	
}

function enable_aft() {
	document.myForm.aft_serial_no.disabled=false;
	document.myForm.aft_part_no.disabled=false;
	document.myForm.aft_part_description.disabled=false;
	document.myForm.aft_part_quantity.disabled=false;
	document.myForm.aft_shp_standard.disabled=false;
	document.myForm.aft_shp_expidited.disabled=false;
}

function disable_aft() {
	document.myForm.aft_serial_no.disabled=true;
	document.myForm.aft_part_no.disabled=true;
	document.myForm.aft_part_description.disabled=true;
	document.myForm.aft_part_quantity.disabled=true;
	document.myForm.aft_shp_standard.disabled=true;
	document.myForm.aft_shp_expidited.disabled=true;
}

function enable_service() {
	document.myForm.svc_serial_no.disabled=false;
	document.myForm.svc_comment.disabled=false;
}

function disable_service() {
		document.myForm.svc_serial_no.disabled=true;
	document.myForm.svc_comment.disabled=true;
}

function enable_equip() {
	document.myForm.equip_comments.disabled=false;
}

function disable_equip() {
	document.myForm.equip_comments.disabled=true;
}