// create object
var quoteRequest = 
{
	// class vars
	overheadDoorTotal: 0,
	doorTotal: 0,
	windowTotal: 0,
	lightTotal: 0,

	// class methods
	populateDesc: function(jqObject)
	{
		var thisId = $(jqObject).attr('id');
		var value = ($(jqObject).attr('id') == 'building_slope') ? $('#building_shape').val()+' '+$(jqObject).val() : $(jqObject).val(); 
		
		if($(jqObject).is('select'))
		{			
			// if no value, set to null and do not do get request
			if($(jqObject).val() == '' || $(jqObject).val() == 0)
			{
				$('#'+thisId+'_title').html('');
				$('#'+thisId+'_body').html('');
				$('#'+thisId+'_thumb').attr('src','descImages/selectOne.jpg');
			}
			else
			{			
				// get description of item selected
				$.ajax(
				{
					type: 'get',
					url: 'ajaxQuoteRequest.php',
					data: 'action=getDescription&field='+thisId+'&value='+encodeURIComponent(value),
					dataType: 'xml',
					success: function(xmlDoc)
					{				
						// show image/title/desc
						$('#'+thisId+'_title').html($('response > title:first', xmlDoc).text());
						$('#'+thisId+'_body').html($('response > desc:first', xmlDoc).text());
						$('#'+thisId+'_thumb').attr('src',$('response > src:first', xmlDoc).text());
					}			
				});
			}
		}	
	},
	
	saveBuildingToDb: function(jqObject)
	{		
		$.ajax(
		{
			type: 'get',
			url: 'ajaxQuoteRequest.php',
			data: 'action='+$('#page').val()+'&field='+$(jqObject).attr('id')+'&value='+encodeURIComponent($(jqObject).val()),
			dataType: 'xml',
			success: function(xmlDoc)
			{
				if($('response:first', xmlDoc).text() == 'success')
				{
					// request description
					quoteRequest.populateDesc(jqObject);					
				}
			}			
		});
		
		// show conditional on building info page
		if($(jqObject).attr('id') == 'building_shape' && $(jqObject).val() == 'Offset Peak')
		{
			var newRow = 
				'<li class="formRowImage" id="conditionalRow">'+
		      		'<div class="formColumnLeft">'+
						'<label class="formElementLabel" for="building_peak_offset">Peak Offset</label>'+
		      			'<input type="text" class="formElementInput" id="building_peak_offset" name="building_peak_offset" value="" />'+
						'<h2 class="descTitle">Peak Offset</h2>'+
						'<p class="descBody">indicate the distance from building center to the offset peak (in feet)</p>'+
					'</div>'+
					'<div class="formColumnRight"><img src="descImages/peakOffset.jpg" alt="thumbnail" height="150" width="150" /></div>'+
				'</li>';
		
			// add row to list and bind ajax save event
			$('.formList').append(newRow);
			$('#building_peak_offset').bind('blur', function() { quoteRequest.saveBuildingToDb($(this)); });
		}
		else if($(jqObject).attr('id') == 'building_shape' && $(jqObject).val() != 'Offset Peak')
		{
			// remove element from building info page
			$('#conditionalRow').remove();
			
			// clear answer
			$.ajax(
			{
				type: 'get',
				url: 'ajaxQuoteRequest.php',
				data: 'action='+$('#page').val()+'&field=building_peak_offset&value=',
				dataType: 'xml',
				success: function(xmlDoc)
				{
					if($('response:first', xmlDoc).text() == 'success')
					{
						// do something if successful					
					}
				}			
			});
		}
		// end show conditional on building info page
		
		// slope / eave image change
		if($(jqObject).attr('id') == 'building_shape' && $(jqObject).val() != '')
		{
			$.ajax(
			{
				type: 'get',
				url: 'ajaxQuoteRequest.php',
				data: 'action=getEaveImage&shape='+$(jqObject).val(),
				dataType: 'xml',
				success: function(xmlDoc)
				{
					var leftEaveImg = $('response:first > left', xmlDoc).text();
					var rightEaveImg = $('response:first > right', xmlDoc).text();
					
					$('#left_eave_thumb').attr('src',leftEaveImg);
					$('#right_eave_thumb').attr('src',rightEaveImg);					
				}			
			});
			
			// also update slope image if it exists
			if(document.getElementById('building_slope'))
			{
				quoteRequest.populateDesc($('#building_slope'));
			}
		}
		
		// show/hide slope conditional (based on shape)			
		if($(jqObject).attr('id') == 'building_shape' && $(jqObject).val() != '' && !document.getElementById('building_slope'))
		{			
			// also show slope field (requires answer to shape)
			var newRow = 
				'<li class="formRowImage" id="conditionalRow2">'+
		      		'<div class="formColumnLeft">'+
						'<label class="formElementLabel" for="building_slope">Slope</label>'+
						'<br />'+
						'<select class="formElementSelect" id="building_slope" name="building_slope">'+
							'<option value="">select one</option>'+
							'<option value="1/2:12">1/2:12</option>'+
							'<option value="1:12">1:12</option>'+
							'<option value="2:12">2:12</option>'+
							'<option value="3:12">3:12</option>'+
						'</select>'+													
						'<h2 class="descTitle" id="building_slope_title"></h2>'+
						'<p class="descBody" id="building_slope_body"></p>'+
					'</div>'+
					'<div class="formColumnRight"><img id="building_slope_thumb" src="descImages/selectOne.jpg" alt="thumbnail" height="150" width="150" /></div>'+
		    	'</li>';
		
			// add row to list and bind ajax save event
			$('.formList').append(newRow);
			$('#building_slope').bind('change', function() { quoteRequest.saveBuildingToDb($(this)); });			
		}
		else if($(jqObject).attr('id') == 'building_shape' && $(jqObject).val() == '')
		{
			// remove element from building info page
			$('#conditionalRow2').remove();
			
			// clear answer
			$.ajax(
			{
				type: 'get',
				url: 'ajaxQuoteRequest.php',
				data: 'action='+$('#page').val()+'&field=building_slope&value=',
				dataType: 'xml',
				success: function(xmlDoc)
				{
					if($('response:first', xmlDoc).text() == 'success')
					{
						// do something if successful					
					}
				}			
			});			
		}
		// end slope / eave image change		
	},
	
	// over head door section
	saveOverheadDoorToDb: function(jqObject)
	{
		// init add/edit overhead door call
		if($(jqObject).attr('id') == 'overheadDoorTotal')
		{
			$.ajax(
			{
				type: 'get',
				url: 'ajaxQuoteRequest.php',
				data: 'action='+$(jqObject).attr('id')+'&value='+encodeURIComponent($(jqObject).val()),
				dataType: 'html',
				success: function(htmlDoc)
				{					
					// remove any previous dynamic and add new list
					$('#overheadDoorList > li').remove('.dynamic');						
					$('#overheadDoorList').append(htmlDoc);
						
					// add events to all selects that includes class dynamic
					$('select.dynamic', $('#overheadDoorList')).bind('change', function() { quoteRequest.saveOverheadDoorToDb($(this)); });
					$('input:text', $('#overheadDoorList')).bind('blur', function() { quoteRequest.saveOverheadDoorToDb($(this)); });										
				}			
			});				
		}
		else
		{
			// get door id
			var doorId = $(jqObject).attr('id').replace('height','').replace('width','').replace('location','').replace('placement','');
			
			// normal ajax save
			$.ajax(
			{
				type: 'get',
				url: 'ajaxQuoteRequest.php',
				data: 'action='+$('#page').val()+'&door_id='+doorId+'&field='+$(jqObject).attr('name')+'&value='+encodeURIComponent($(jqObject).val()),
				dataType: 'xml',
				success: function(xmlDoc)
				{
					// if successful
					if($('response:first', xmlDoc).text() == 'success')
					{
						// success stub					
					}					
				}			
			});			
		}
	},
	
	// fixture section	
	addCustomFields: function(value, type)
	{
		if(value.match(/Custom/))
		{
			// create height/width fields
			var html = 
			'<li class="formRow" id="'+type+'HeightRow">'+
				'<div class="formColumnLeft"><label class="formElementLabel" for="'+type+'Height">Height (in feet)</label></div>'+
				'<div class="formColumnRight">'+
					'<input type="text" class="formElementInput" id="'+type+'Height" name="'+type+'Height" />'+
				'</div>'+						
		   	'</li>'+
			'<li class="formRow" id="'+type+'WidthRow">'+
				'<div class="formColumnLeft"><label class="formElementLabel" for="'+type+'Width">Width (in feet)</label></div>'+
				'<div class="formColumnRight">'+
					'<input type="text" class="formElementInput" id="'+type+'Width" name="'+type+'Width" />'+
				'</div>'+						
		   	'</li>';
		
			// insert before add button
			$('#'+type+'ButtonRow').before(html);
		}
		else
		{
			// remove any height/width fields if not custom
			$('#'+type+'HeightRow').remove();
			$('#'+type+'WidthRow').remove();
		}
	},
	
	saveFixtureToDb: function(type)
	{
		model = $('#'+type+'Model').val();
		quantity = $('#'+type+'Quantity').val();
		height = (document.getElementById(type+'Height')) ? $('#'+type+'Height').val() : '';
		width = (document.getElementById(type+'Width')) ? $('#'+type+'Width').val() : '';
		
		if(model != '')
		{
			// ajax save to db
			$.ajax(
			{
				type: 'get',
				url: 'ajaxQuoteRequest.php',
				data: 'action=addFixture&type='+encodeURIComponent(type)+'&model='+encodeURIComponent(model)+
						'&quantity='+encodeURIComponent(quantity)+'&height='+encodeURIComponent(height)+
						'&width='+encodeURIComponent(width),
				dataType: 'html',
				success: function(htmlDoc)
				{
					// append new entry to list
					$('#'+type+'List').append(htmlDoc);
					
					// add event to delete (remove bound events, and rebind to account for new one)
					$('span.inlineLink').unbind('click');
					$('span.inlineLink').bind('click', function() { quoteRequest.deleteFixture($(this)); });					
				}			
			});			
		}
		else
		{
			// return error message
			alert('Please select a '+type+' model.');
			return false;
		}		
	},
	
	deleteFixture: function(jqObject)
	{		
		if(confirm('Are you sure you want to delete this fixture?'))
		{
			// get fixture id
			var fixtureId = $(jqObject).attr('id').replace('delete','');
			
			// ajax delete from db
			$.ajax(
			{
				type: 'get',
				url: 'ajaxQuoteRequest.php',
				data: 'action=deleteFixture&fixture_id='+encodeURIComponent(fixtureId),
				dataType: 'xml',
				success: function(xmlDoc)
				{
					if($('response:first', xmlDoc).text() == 'success')
					{
						// remove list item from list
						$('#fixtureRow'+fixtureId).remove();					
					}			
				}			
			});
		}
		else
		{
			return false;
		}
	},
	
	// option section
	saveOptionToDb: function(jqObject)
	{
		// determine value (y/n)
		var thisValue = ($(jqObject).attr('checked') == true) ? 'Y' : 'N';
		
		$.ajax(
		{
			type: 'get',
			url: 'ajaxQuoteRequest.php',
			data: 'action=quote_building&field='+$(jqObject).attr('id')+'&value='+encodeURIComponent(thisValue),
			dataType: 'xml',
			success: function(xmlDoc)
			{
				if($('response:first', xmlDoc).text() == 'success')
				{
					// success functionality goes here					
				}
			}			
		});
	},
	
	// update validation
	updateValidation: function(value)
	{
		if(value == 'Phone')
		{
			// force js validation
			$('#phoneLabel').attr('for','phone_required').html('Phone');
			$('#phone').attr('id','phone_required');
			
			// make others not required
			$('#phone2Label').attr('for','phone2').html('Fax <em>optional</em>');
			$('#phone2_required').attr('id','phone2');
			
			$('#emailLabel').attr('for','email').html('Email <em>optional</em>');
			$('#email_required').attr('id','email');
		}
		
		if(value == 'Email')
		{
			// force js validation
			$('#emailLabel').attr('for','email_required').html('Email');
			$('#email').attr('id','email_required');
			
			// make others not required
			$('#phone2Label').attr('for','phone2').html('Fax <em>optional</em>');
			$('#phone2_required').attr('id','phone2');
			
			$('#phoneLabel').attr('for','phone').html('Phone <em>optional</em>');
			$('#phone_required').attr('id','phone');
		}
		
		if(value == 'Fax')
		{
			// force js validation
			$('#phone2Label').attr('for','phone2_required').html('Fax');
			$('#phone2').attr('id','phone2_required');
			
			// make others not required
			$('#phoneLabel').attr('for','phone').html('Phone <em>optional</em>');
			$('#phone_required').attr('id','phone');
			
			$('#emailLabel').attr('for','email').html('Email <em>optional</em>');
			$('#email_required').attr('id','email');
		}		
	},

	// initial method
	init: function()
	{
		// add events to building info view
		if($('#page').val() == 'quote_building')
		{			
			$('input:text').bind('blur', function() { quoteRequest.saveBuildingToDb($(this)); });
			$('textarea').bind('blur', function() { quoteRequest.saveBuildingToDb($(this)); });
			$('select').bind('change', function() { quoteRequest.saveBuildingToDb($(this)); });
		}
		
		// add events to overhead door info view
		if($('#page').val() == 'quote_overhead_door')
		{			
			$('input:text').bind('blur', function() { quoteRequest.saveOverheadDoorToDb($(this)); });
			$('textarea').bind('blur', function() { quoteRequest.saveOverheadDoorToDb($(this)); });
			$('select').bind('change', function() { quoteRequest.saveOverheadDoorToDb($(this)); });
			
			// save inital totals
			quoteRequest.overheadDoorTotal = $('#overheadDoorTotal').val();
		}
		
		// add events to door/window/skylight info view
		if($('#page').val() == 'quote_fixture')
		{
			// add events to combobox (for custom height/width)
			$('#doorModel').bind('change', function() { quoteRequest.addCustomFields($(this).val(), 'door'); });
			$('#windowModel').bind('change', function() { quoteRequest.addCustomFields($(this).val(), 'window'); });
			
			// add events to buttons
			$('#doorButton').bind('click', function() { quoteRequest.saveFixtureToDb('door'); });
			$('#windowButton').bind('click', function() { quoteRequest.saveFixtureToDb('window'); });
			$('#skylightButton').bind('click', function() { quoteRequest.saveFixtureToDb('skylight'); });
			
			// add events to remove fixture
			$('span.inlineLink').bind('click', function() { quoteRequest.deleteFixture($(this)); });
		}
		
		// add events to option info view
		if($('#page').val() == 'quote_building_options')
		{			
			$('input:checkbox').bind('click', function() { quoteRequest.saveOptionToDb($(this)); });
		}
		
		// add event to contact view
		$('#method_required').bind('change', function() { quoteRequest.updateValidation($(this).val()); });
	}	
}

// add to document after dom is loaded
$(document).ready(function() { quoteRequest.init(); });
