// JavaScript Document

function submitShop(val)
{
	document.getElementById("_shopId").value = val;
	document.forms[0].submit();
}


function populateYears()
{
	$("#year").unbind('change');

	$.ajax({  
		type: "GET",  
		url: "years.php5",  
		data: "",  
		dataType: "text/html",  
		success: function(html){       
			$("#yearsDiv").html(html);     
			wireYear();
		}  
  	}); 
}

function wireYear()
{
	$("#year").change(function () {
		
		resetMake();
		resetModel();
		resetBodystyle();
		resetOpening();
		resetGlassOption();
		
		year = $('#year').val();
		if (year > 0)
		{
			$('#yearDisp').val(year);
			populateMake(year);
			return;
		}

		
	});
}

function resetMake()
{
	$("#make").unbind('change');
	$("#makesDiv").html("<select id='make' name='make' disabled='disabled'><option value='0'>(select make)</select>");
}

function populateMake(year)
{
	$("#make").unbind('change');

	$.ajax({  
		type: "GET",  
		url: "makes.php5",  
		data: "year=" + year,  
		dataType: "text/html",  
		success: function(html){       
			$("#makesDiv").html(html);     
			wireMake();
		}  
  	}); 
}

function wireMake()
{	
	$("#make").change(function () {
	
		resetModel();
		resetBodystyle();
		resetOpening();
		resetGlassOption();
		
		year = $('#year').val();
		make = $('#make').val();
		
		if (make > 0)
		{
			$('#makeVal').val(make);
			$('#makeDisp').val(	$('#make :selected').text()); 
 
			populateModel(year,make);
			return;
		}
				
	});
}

function resetModel()
{
	$("#model").unbind('change');
	$("#modelsDiv").html("<select id='model' name='model' disabled='disabled'><option value='0'>(select model)</select>");
}

function populateModel(year, makeId)
{
	$("#model").unbind('change');

	$.ajax({  
		type: "GET",  
		url: "models.php5",  
		data: "year=" + year + "&makeId=" + makeId,  
		dataType: "text/html",  
		success: function(html){       
			$("#modelsDiv").html(html);
			wireModel();     
		}  
  	});
}
function wireModel()
{
	$("#model").change(function () {
		
		resetBodystyle();
		resetOpening();
		resetGlassOption();
		
		year = $('#year').val();
		make = $('#make').val();	
		model = $('#model').val();
		
		if (model != 0)
		{
			populateBodystyle(year,make,model);
		}
	});
}

function resetBodystyle()
{
	$("#vehId").unbind('change');
	$("#bodystylesDiv").html("<select id='vehId' name='vehId' disabled='disabled'><option value='0'>(select body style)</select>");
}

function populateBodystyle(year, makeId, modelId)
{
	$("#vehId").unbind('change');

	$.ajax({  
		type: "GET",  
		url: "bodyStyles.php5",  
		data: "year=" + year + "&makeId=" + makeId + "&modelId=" + modelId,  
		dataType: "text/html",  
		success: function(html){       
			$("#bodystylesDiv").html(html);
			wireBodystyle();
			if (document.getElementById("vehId").length == 1)
			{
				$("#vehId").trigger('change');
			}
			
		}  
  	});
}

function wireBodystyle()
{

	$("#vehId").change(function () {

		resetOpening();
		resetGlassOption();
		year = $('#year').val();
		make = $('#make').val();	
		model = $('#model').val();
		vehId = $('#vehId').val();
		
		if (vehId != 0)
		{
			populateOpening(year,make,model,vehId);
		}
	});
}

function resetOpening()
{
	$("#openingCode").unbind('change');
	$("#openingsDiv").html("<select id='openingCode' name='openingCode' disabled='disabled'><option value='0'>(select glass type)</select>");
}
function populateOpening(vehId)
{
	$("#openingCode").unbind('change');

	$.ajax({  
		type: "GET",  
		url: "openingTypes.php5",  
		data: "vehId=" + vehId,  
		dataType: "text/html",  
		success: function(html){       
			$("#openingsDiv").html(html);			
			wireOpening();     
		}  
  	});
}

function wireOpening()
{

	$("#openingCode").change(function () {		
		resetGlassOption();
		zipcode = $('#zipcode').val();
		vehId = $('#vehId').val();
		openingCode = $('#openingCode').val();
		
		if (openingCode != 0)
		{
			populateGlassOptions(zipcode,vehId, openingCode);
		}
	});
}

function resetFindShops()
{
	$('#findShops').attr('disabled','disabled');	
}
function resetRequestService()
{
	$('#requestService').attr('disabled','disabled');	
}

function validateZIP(field) 
{
	var valid = "0123456789";

	if (field.length!=5) {
		return false;
	}
	
	for (var i=0; i < field.length; i++) {
		temp = "" + field.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") 
			return false;
	}

	return true;
}

//$(window).load(function() {
//      alert("window load occurred!");
	  //if (document.getElementById("year"))
	  //{
	  //	alert(document.getElementById("year").length);
	  //}
	  //else
	 // {
		//  	alert("nope");
		//  	resetMake();
			//resetModel();
			//resetBodystyle();
			//resetOpening();
			//resetGlassOption();

			//resetFindShops();
		//	populateYears();
	//  }
	  //alert('back');
			
//});

$(document).ready(function() {
	//alert($('#yearDisp').val());
	//if ($('#yearDisp').val() == '')
	//{
			resetMake();
			resetModel();
			resetBodystyle();
			resetOpening();
			resetGlassOption();

			resetFindShops();
			populateYears();
	
	//}
	//else
	//{
	//		alert($('#yearDisp').val());
	//}
});


