/* 
	======================================================================
   	
	File        : fortis.core.js
	Description : FortisTCI Core JavaScript
	Version     : 1.0
	Created     : 2011/07/10
	
	Copyright (c) 2011 | Design Squared | All Rights Reserved.
	
	======================================================================
*/

$(document).ready(function() {
	
	/*==================================================
	  _blank XHTML Fix
	==================================================*/
	
	$('a[rel=external]').attr('target', '_blank');
	$('area[class=forceExternal]').attr('target', '_blank');
	
	
	/*==================================================
	  Quick Search Form
	==================================================*/
	
	$('.search-box').blur(function(){
		if ($(this).val() == ''){
			$(this).val('Search...');
		}
	});
	
	$('.search-box').focus(function(){
		if ($(this).val() == 'Search...'){
			$(this).val('');
		}
	});
	
	/*==================================================
	  Activate jQuery Tabs
	==================================================*/
	
	if ($('#jquery-tabs').length){
		$('#jquery-tabs').tabs({ fxSlide: false, fxFade: true, fxSpeed: 'normal' });
	}
	
	/*==================================================
	  Back to Top Function
	==================================================*/
	
	$('#link-to-top').click(function(){
		$(this).blur();
		$('html,body').animate({ scrollTop: 0 }, 'normal');
		return false;
	});
	
	/*==================================================
	  Handle Bill Estimation Request
	==================================================*/
	
	if ($('.bill-form').length)
	{
		//----- Residential - Single Phase -----//
		
		function initResidential(){
			$('#bill-residential').validate({
				rules: {
					residential_usage: {
						required: true,
						number: true
					}
				},
				messages: {
					residential_usage: 'Please enter a valid number.'
				},
				submitHandler: function(form) {
					$.ajax({
						url: 'data/data.bill.php',
						data: $(form).serialize() + '&request=process&type=residential',
						type: 'post',
						cache: false,
						dataType: 'html',
						success: function (data)
						{
							$('#single-residential').html(data);
							
						},
						error: function () {
							alert('Error Connecting with Database; Please Reload the Page and Try Again.');
						}
					});
				},
				onkeyup: false	
			});
		}
		initResidential();
		
		//----- Commercial - Single Phase -----//
		
		function initCommercial(){
			$('#bill-commercial').validate({
				rules: {
					commercial_usage: {
						required: true,
						number: true
					}
				},
				messages: {
					commercial_usage: 'Please enter a valid number.'
				},
				submitHandler: function(form) {
					$.ajax({
						url: 'data/data.bill.php',
						data: $(form).serialize() + '&request=process&type=commercial',
						type: 'post',
						cache: false,
						dataType: 'html',
						success: function (data)
						{
							$('#single-commercial').html(data);
							
						},
						error: function () {
							alert('Error Connecting with Database; Please Reload the Page and Try Again.');
						}
					});
				},
				onkeyup: false	
			});
		}
		initCommercial();
		
		//----- Reset Estimation Request -----//
		
		$('.bill-reset').live('click',function(){
			var resetSection = $(this).attr('rel');
			$.ajax({
				url: 'data/data.bill.php',
				data: 'request=reset&type=' + resetSection,
				type: 'post',
				cache: false,
				dataType: 'html',
				success: function (data)
				{
					if (resetSection == 'commercial'){
						$('#single-commercial').html(data);
						initCommercial();
					} else {
						$('#single-residential').html(data);
						initResidential();
					}					
				},
				error: function () {
					alert('Error Connecting with Database; Please Reload the Page and Try Again.');
				}
			});
			return false;
		});
	}
});
