// signup.js - used everywhere that quoteform.inc.php is used
// @author: cn

$(document).ready(function(){
    
	$("#year").bind("change", function(e){
		$.post('/includes/quoteformiframe.php', { func: "returnMake", year: $("#year").val()}, function(data){
	    	$("#makeDiv").html(data);
			$("#make").bind("change", function(e){
				$.post('/includes/quoteformiframe.php', { func: "returnModel", make_id: $("#make").val()}, function(data){
					$("#modelDiv").html(data);
				});
			});
		});
	});
	
	$("#make").bind("change", function(e){
		$.post('/includes/quoteformiframe.php', { func: "returnModel", make_id: $("#make").val()}, function(data){
			$("#modelDiv").html(data);
		});
	});
	
	$("#yes_insurance").bind("click", function(e){
		$("#insurance").css('display','block');
	});
	$("#no_insurance").bind("click", function(e){
		$("#insurance").css('display','none');
		$("#insurance_provider").val('');
		$("#other_provider").val('');
	});
	
	$("#yes_tint").bind("click", function(e){
		$("#colorSel").css('display','block');
	});
	$("#no_tint").bind("click", function(e){
		$("#colorSel").css('display','none');
		$("#req_tintCol").val('');
	});
	
	$("#yes_received_quote").bind("click", function(e){
		$("#quote_company1").css('display','block');
		$("#quote_company2").css('display','block');
		$("#quote_company3").css('display','block');
		$("#quote_company4").css('display','block');
	});
	$("#no_received_quote").bind("click", function(e){
		$("#quote_company1").css('display','none');
		$("#quote_company2").css('display','none');
		$("#quote_company3").css('display','none');
		$("#quote_company4").css('display','none');
	});
	
	/**
	 * displays selected states in location1's cities in a modal window
	 * @author cn
	 */
	$("#sendform").bind("click", function(e){
		var errorArr = new Array()
		var email=$('#req_email').val();
		
		if($('#req_name').val()==''){
			errorArr.push('Name is blank');
		}
		if($('#phone').val()==''){
			errorArr.push('Phone is blank');
		}
		if(email=='' || email.search('@')==0 || !email.search('.')==0 || email.length<6){
			errorArr.push('Email address is blank or invalid');
		}
		if($('#address').val()==''){
			errorArr.push('Address is blank or invalid');
		}
		if($('#req_city').val()==''){
			errorArr.push('City is blank or invalid');
		}
		if($('#state').val()==''){
			errorArr.push('State is blank');
		}
		if($("#req_zip_number").val()==''){
			errorArr.push('Zip Code is blank');
		}
/*		if($("#call_time").val()==''){
			errorArr.push('Best time to call is blank');
		}*/
		if($("#year").val()==''){
			errorArr.push('Year is blank');
		}
		if($("#make").val()==''){
			errorArr.push('Make is blank');
		}
		if($("#model").val()==''){
			errorArr.push('Model is blank');
		}
		if($('#replaced').is(':checked')==false && $('#repaired').is(':checked')==false){
			errorArr.push('Please check either replace or repair');
		}
		if($("#replace").val()==''){
			errorArr.push('Glass replacement is blank');
		}
		if($('#yes_insurance').is(':checked')==false && $('#no_insurance').is(':checked')==false){
			errorArr.push('Please select whether you plan to use insurance or not');
		}
/*		if($('#no_heated').is(':checked')==false && $('#yes_heated').is(':checked')==false && $('#notsure_heated').is(':checked')==false){
			errorArr.push('Please select whether the window is heated');
		}
		if($('#no_sensor').is(':checked')==false && $('#yes_sensor').is(':checked')==false && $('#notsure_sensor').is(':checked')==false){
			errorArr.push('Please select whether the window has a heat sensor');
		}
		if($('#yes_tint').is(':checked')==false && $('#no_tint').is(':checked')==false){
			errorArr.push('Please select whether the window is tinted or not');
		}*/
		
		var dropdownIndex = document.getElementById('make').selectedIndex;
		var make = document.getElementById('make')[dropdownIndex].innerHTML;
		
		$("#frmmake").val(make);
		
		var dropdownIndex = document.getElementById('model').selectedIndex;
		var model = document.getElementById('model')[dropdownIndex].innerHTML;
		
		$("#frmmodel").val(model);
		
		var dropdownIndex = document.getElementById('req_replace').selectedIndex;
		var replace = document.getElementById('req_replace')[dropdownIndex].innerHTML;
		
		$("#frmreplace").val(replace);
		
		var length=errorArr.length;
		
		if(length>0){
			var string = "More information is required... \n \n";
			for(var i=0;i<length;i++){
				string+=errorArr[i]+"\n";
			}
			alert(string);
		}
		else{
			document.homeform.submit();
		}
	});
	
	//$("#req_city").autocomplete("/includes/quoteformiframe.php?func=autofillcity");
});
/*
function returnModel(){
	$.post('/includes/quoteformiframe.php', { func: "returnModel", make_id: $("#make").val()}, function(data){
		$("#modelDiv").html(data);
	});
}*/
