function form_setup() {
	// Set up Really Simple Validation (RSV) for drop-down click-to-call form
	$("#dropdowncallback_form").RSV({
		onCompleteHandler: function(){

			$('#dropdowncallback_form').ajaxSubmit({
				target: '#dropdowncallback'
			}); 

			$("#dropdowncallback_inner").html('<p style="text-align:center;width:auto;">Thank you for contacting iTechware!<br />We\'re calling you now...</p>');

			return false;
		},
		displayType: 'alert-one',
		errorFieldClass: "errorField",
		rules: [
			"length>=3,phone1,Please enter your full 10-digit US or Canada phone number with area code",
			"digits_only,phone1,Your phone number must contain only digits",
			"length>=3,phone2,Please enter your full 10-digit US or Canada phone number with area code",
			"digits_only,phone2,Your phone number must contain only digits",
			"length>=4,phone3,Please enter your full 10-digit US or Canada phone number with area code",
			"digits_only,phone3,Your phone number must contain only digits"
		]
	});

	// Set up contact form fields to show default fields (focus)
	$('.input_text').focus(function(){
		var e = $(this);
		if( e.hasClass('input_text_default') )
		{
			e.removeClass('input_text_default');
			e.val('');
		}
	});
	
	// Set up contact form fields to show default fields (blur)
	$('.input_text').blur(function(){
		var e = $(this);
		if( e.val() == '' )
		{
			e.val( this.defaultValue );
			e.addClass('input_text_default');
		}
		else if( this.id == 'contact_phone' )
		{
			this.value = this.value.replace(/[^0-9]/g,'');
			if( this.value == '' )
			{
				e.val( this.defaultValue );
				e.addClass('input_text_default');
			}
			else
			{
				this.value = this.value.replace(/(.{3,3})(.{3,3})(.*)/,'$1-$2-$3');
			}
		}
	});
}

$(function(){
	// Click-to-call dropdown functionality upon clicking number/button in header
	$('#tpl_header_contact_number,#tpl_header_contact_call,#sitemap_contact_call').click(function(){
		$(this).blur();
		if( $('#dropdowncallback').css('display') == 'none' ) {
			$(window).scrollTop( 0 );
			$('#dropdowncallback').show().css('top',-100).animate({'top':0},'fast');
			$('#dropdowncallback_phone1').focus();
		} else {
			$('#dropdowncallback').fadeOut();
		}
		return false;
	});
	
	// Smooth transition to new page upon clicking sidebar button, if JavaScript is enabled
	/*
	$('#tpl_content_sidenavi a').click(function(){
		// Determine current page and subpage
		var sub_ele = $('#tpl_content_sidenavi a.active');
		var sub_uri = sub_ele.attr('rel');
		var sub_idx = sub_ele.index();
		
		// Determine new subpage
		var new_ele = $(this);
		var new_uri = new_ele.attr('rel');
		var new_idx = new_ele.index();
		
		// Set anchor to this section, and onhashchange will load it
		//alert( 'new_uri '+new_uri+' sub_uri '+sub_uri+' base_uri '+main_uri );
		//if( new_uri == 'what' )
		//	window.location = main_uri;
		//else
			window.location = '#' + new_uri;
		
		return false;
	});
	
	// Automatically select subpage in hash
	$(window).hashchange(function(){
		var new_uri = location.hash.replace(/[^a-z0-9]/g,'');
		if( !new_uri )
			new_uri = 'what';
		
		// Load new page content
		$.get( main_uri + '-' + new_uri + '?json=1', function(data) {
			// Show new page content
			$(document).attr( 'title', data.document_title );
			$('#tpl_content_header h1').html( data.page_title );
			$('#tpl_content_header h2').html( data.page_subtitle );
			$('#tpl_content_body').html( data.page_body );
			
			// Set correct body class
			$('body').attr( 'class', '' );
			$('body').addClass( 'body_' + main_uri.replace(/[^a-z0-9]/g,'') + new_uri.replace(/[^a-z0-9]/g,'') );
			$('body').addClass( 'body_' + main_uri.replace(/[^a-z0-9]/g,'') );
			$('body').addClass( 'body_' + new_uri.replace(/[^a-z0-9]/g,'') );
			
			// Set anchor to this section and highlight correct sidenavi button
			$('#tpl_content_sidenavi a').removeClass('active');
			$('#tpl_content_sidenavi a.tpl_content_sidenavi_'+new_uri).addClass('active').blur();
			
			// Set up page scripting, if necessary
			form_setup();
		});
	});
	
	// Started with a hash? Select that page
	if( window.location.hash )
		$(window).hashchange();
	//*/
	
	// Set up forms on initial page
	form_setup();
});

