$(document).ready(function() {
	// Init accordian
	// jQuery('#list1b').accordion({ 
		// autoheight: false 
	// });
	
	$('#hpToggle').click(function (e) {
		$('#roofingProducts').toggle('slow');
	});	
	
	$('#vpToggle').click(function (e) {
		$('#verticalProducts').toggle('slow');
	});	
	$('#verticalProducts').toggle();
	
	// Quantity validation
	$("tr.product-qty input").keypress(function (e) {
		if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
			alert('Use only numbers in quanity fields!');
			return false;
		}
	});
	
	// Hide error messages
	$('.errorlist').toggle();
	
	// State dropdown drives shipping types
	$('#id_state').change(function (e) {
		var val, country = $('option:selected', this).attr('class');
		val = $('#id_delivery_type').val();
		$('#id_delivery_type').empty();
		$.each($('#id_delivery_type_hidden option.' + country + ''), function(val, option) {
			$('#id_delivery_type').append($(option).clone());
		});
		if(val){
			$('#id_delivery_type').val(val);
		}
	});
	$('#id_state').trigger('change');
	
	// Occupation 'other'
	$('#id_occupation').change(function (e) {
		if ($('option:selected', this).val() === 'Other')
		{
			$('#other').show();
			return;
		}
		$('#other').hide();
		return;
	});
	$('#id_occupation').trigger('change');
	
	$('#Delete').click(function (e) {
		var rid = $('#id').val();
		e.preventDefault();
		confirm("Are you sure you want to delete this request from the system?", function () {
			$.get("requestDelete.php", { id: rid},
			function(data){
				window.location.href='requestList.php';}
			);
		});		
	});
	
	// Validate form
	$('#Submit').click(function (e) {
		$('.errorlist').hide();
		var isValid = true;
		var productValues = [];
		var pText = $('.productText');
		pText.each(function() {
			if($(this).val().length > 0) {
				productValues.push($(this).val());
			}
		});
		if(productValues.length == 0){
			if($.trim($('#id_comments').val()).length == 0 ) {
				isValid = false;
				$('#request-basic').modal({overlayClose:true});
			};
		}
		if($.trim($('#id_first_name').val()).length == 0 ) {
			isValid = false;
			$('#err_first_name').show();
		};
		if($.trim($('#id_last_name').val()).length == 0 ) {
			isValid = false;
			$('#err_last_name').show();
		};
		if($.trim($('#id_company').val()).length == 0 ) {
			isValid = false;
			$('#err_company').show();
		};
		if($.trim($('#id_address').val()).length == 0 ) {
			isValid = false;
			$('#err_address').show();
		};
		if($.trim($('#id_city').val()).length == 0 ) {
			isValid = false;
			$('#err_city').show();
		};
		if($.trim($('#id_zip_code').val()).length == 0 ) {
			isValid = false;
			$('#err_zip_code').show();
		};
		if($('#id_email').val().length == 0 || !$('#id_email').val().match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/gi)) {
			isValid = false;
			$('#err_email').show();
		};
		if($.trim($('#id_phone').val()).length == 0 ) {
			isValid = false;
			$('#err_phone').show();
		};
		if($('#id_delivery_type').val() == 0 ) {
			isValid = false;
			$('#err_delivery_type').show();
		};
        if($('#id_occupation').val() == 0 ) {
			isValid = false;
			$('#err_occupation').show();
		};
		
		if(isValid == true){
      		  $('#Submit').hide();
      		  $('#Loading').show(); 
 		 }
		
		
		return isValid;
		
	});

	// Confirm
	
});

function confirm(message, callback) {
	$('#confirm').modal({
		closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
		position: ["20%",],
		overlayId: 'confirm-overlay',
		containerId: 'confirm-container', 
		onShow: function (dialog) {
			var modal = this;

			$('.message', dialog.data[0]).append(message);

			// if the user clicks "yes"
			$('.yes', dialog.data[0]).click(function () {
				// call the callback
				if ($.isFunction(callback)) {
					callback.apply();
				}
				// close the dialog
				modal.close(); // or $.modal.close();
			});
		}
	});
}
