dojo.addOnLoad( function(){resetSelects();resetDepartment();displaySchoolContainer();} );

function loadCustomerServiceDepartments( doReset )
{
	resetSelects();
	var customerTypes 		= dojo.byId("customerTypes");
	var productCategoryId 	= dojo.byId("categories");
	
	if( customerTypes.value == 0 || productCategoryId.value == 0 )
	{
		return;
	}
	dojo.xhrGet({
		url: "index.cfm?event=CustomerService.getCustomerServiceDepartmentsByProductCategoryAndProfileOrganizationType&productCategoryId=" + productCategoryId.value + "&customerTypes=" + customerTypes.value,
		load: function(data)
				{
					
					var customerServiceDepartment 			= dojo.byId("customerServiceDepartmentId");
					var customerServiceDepartmentHidden 	= dojo.byId("customerServiceDepartmentIdHidden");
					var customerServiceDepartments 			= dojo.fromJson(data);
					
					//remove old dropdowns
					if ( customerServiceDepartment.hasChildNodes() )
					{
					    while ( customerServiceDepartment.childNodes.length >= 1 )
					    {
					    	customerServiceDepartment.removeChild( customerServiceDepartment.firstChild );       
					    } 
					}
					
					//populate dropdown
					var option = dojo.doc.createElement("option");
						option.value = "0";
						option.innerHTML = "Choose...";
						customerServiceDepartment.appendChild(option);
					
					dojo.forEach(customerServiceDepartments, function(item){
						var option = dojo.doc.createElement("option");
						option.value = item.customerServiceDepartmentId;
						option.innerHTML = item.customerServiceDepartmentName;
						if(customerServiceDepartmentHidden.value != 0)
						{
							if(customerServiceDepartmentHidden.value == option.value)
							{
								option.selected = true;
							}
						}
						customerServiceDepartment.appendChild(option);
					});
			
				}
	})	

}


function getCitiesByStateCode( stateCode )
{
	dojo.byId("profileCustomerId").options[0].selected = 1;
	dojo.xhrGet({
		url: "index.cfm?event=CustomerService.getCitiesByStateCode&stateCode=" + stateCode,
		load: function( data )
			{
				var cityWithCounties 	= dojo.byId( "school_city" );
				var citiesWithCounties 	= dojo.fromJson( data );
				var school_city 		= dojo.byId("school_city");
				var school_cityHidden 	= dojo.byId("school_cityHidden");
				
				if ( school_city.hasChildNodes() )
				{
				    while ( school_city.childNodes.length >= 1 )
				    {
				    	school_city.removeChild( school_city.firstChild );       
				    } 
				}
				
				//populate dropdown
				
				//populate dropdown
				var option = dojo.doc.createElement("option");
					option.value = "0";
					option.innerHTML = "Choose...";
					school_city.appendChild(option);
					
				dojo.forEach(citiesWithCounties, function(item){
					var option = dojo.doc.createElement("option");
					option.value = item.cityCode;
					option.innerHTML = item.cityName;
					if(school_cityHidden.value != 0)
					{
						if(school_cityHidden.value == option.value)
						{
							option.selected = true;
						}
					}
					school_city.appendChild(option);
				});
				if(school_city.value != 0)
				{
					onChangeSchoolCity();
				}
			}
	});	

}

function onChangeSchoolCity()
{
	var stateCode = dojo.byId( "school_state" )
	var stateCodeValue = stateCode.options[stateCode.selectedIndex].value;
	
	
	var cityCode = dojo.byId( "school_city" );
	var cityCodeValue = cityCode.options[cityCode.selectedIndex].value;
	
	var customerTypes = dojo.byId( "customerTypes" );
	var customerTypesValue = customerTypes.options[customerTypes.selectedIndex].value;
	
	var productTypes = dojo.byId( "categories" );
	var productTypesValue = productTypes.options[productTypes.selectedIndex].value;

	if( productTypesValue == 0 )
	{
		alert("Please select a product");
	}

	dojo.byId("school_cityHidden").value = cityCodeValue;
	getCustomersByProductType( stateCodeValue, cityCodeValue, customerTypesValue, productTypesValue );

}

function getAttentionByCustomerServiceDepartment( customerServiceDepartmentId )
{
	
	dojo.byId("customerServiceDepartmentIdHidden").value = customerServiceDepartmentId;
	if(customerServiceDepartmentId != 0)
	{
		dojo.xhrGet({
			url: "index.cfm?event=CustomerService.getAttentionByCustomerServiceDepartment&customerServiceDepartmentId=" + customerServiceDepartmentId,
			load: function( data )
	
			{
					var attentionInput 	= dojo.byId( "attention" );
					var attentionText 	= dojo.fromJson( data );
					
					attentionInput.value = attentionText.attention;
					
					
			}
		});
	}
}



function getCustomersByProductType( stateCode, cityCode, customerTypes, productTypes )
{
	dojo.xhrGet({
		url: "index.cfm?event=CustomerService.getCustomersByProductType&stateCode=" + stateCode + "&cityCode=" + cityCode + "&customerTypes=" + customerTypes + "&productTypes=" + productTypes,
		load: function( data )	
				{
					var profileCustomerId 	= dojo.byId( "profileCustomerId" );
					var profileCustomerIdHidden 	= dojo.byId( "profileCustomerIdHidden" );
					var profileCustomers 	= dojo.fromJson( data );
					
					//remove old dropdowns
					if ( profileCustomerId.hasChildNodes() )
					{
					    while ( profileCustomerId.childNodes.length >= 1 )
					    {
					    	profileCustomerId.removeChild( profileCustomerId.firstChild );       
					    } 
					}
					
					//populate dropdown
					var option = dojo.doc.createElement("option");
					option.value = "0";
					option.innerHTML = "Choose...";
					profileCustomerId.appendChild(option);
					
					//populate dropdown
					dojo.forEach(profileCustomers, function(item){
						var option = dojo.doc.createElement("option");
						option.value = item.arNumbers;
						option.innerHTML = item.name;
						if(profileCustomerIdHidden.value != 0)
						{
							if(profileCustomerIdHidden.value == option.value)
							{
								option.selected = true;
							}
						}
						profileCustomerId.appendChild(option);
					});
					
			}
	})	

}


function getCustomerByARNumber( arNumber )
{
	
	dojo.byId("profileCustomerIdHidden").value = arNumber;
	
	dojo.xhrGet({
		url: "index.cfm?event=CustomerService.getCustomerByARNumber&arNumber=" + arNumber,
		load: function( data )
				{
					var profileCustomer 					= dojo.fromJson( data );
					
					var profileCustomerCNumberHidden		= dojo.byId( "profileCustomerCNumberHidden" );
					var customerServiceRequestSchoolName 	= dojo.byId( "customerServiceRequestSchoolName" );
					var customerServiceRequestAddress 		= dojo.byId( "customerServiceRequestAddress" );
					var customerServiceRequestCity 			= dojo.byId( "customerServiceRequestCity" );
					var customerServiceRequestZip 			= dojo.byId( "customerServiceRequestZip" );
					
					//reset values;
					customerServiceRequestSchoolName.value 	= "";
					customerServiceRequestAddress.value 	= "";
					customerServiceRequestCity.value 		= "";
					customerServiceRequestZip.value 		= "";
					
					customerServiceRequestSchoolName.value 	= profileCustomer.name;
					customerServiceRequestAddress.value 	= profileCustomer.address1;
					customerServiceRequestCity.value 		= profileCustomer.cityName;
					customerServiceRequestZip.value 		= profileCustomer.postalCode;
					
					profileCustomerCNumberHidden.value		= profileCustomer.customerNumber;
				}
	})
}	

function displaySchoolContainer(){
	var inputValue = document.getElementById('customerServiceRequestSchoolName').value;
	var target = document.getElementById('SchoolContainer');
	
	if(inputValue.length > 0 && inputValue != 'Your School Name Here')
	{
		target.style.display = "block";
	}
}

function school_not_listed() 
{
	var input = document.getElementById('customerServiceRequestSchoolName');
	var target = document.getElementById('SchoolContainer');
	
	if(input.value.length == 0)
	{
		input.value = "Your School Name Here";
	}
	
	target.style.display = "block";
}

function resetDepartment()
{
	if(dojo.byId("categories").value != 0)
	{
		loadCustomerServiceDepartments(dojo.byId("categories").value, false);
	}
}

function resetSelects()
{

	if(dojo.byId("school_state").value != 0 && dojo.byId("school_city").value == 0)
	{
		getCitiesByStateCode(dojo.byId("school_state").value);
	}
	
	if(dojo.byId("school_city").value != 0)
	{
		onChangeSchoolCity();
	}
	
	
}

