//------------------------------------------------------[  ]
// Public: JS
//------------------------------------------------------[  ]
// Classes ---------------------------------------------[MN]
var JC_Form = function( Config ) {
	var This = function() {
		// Public Vars -----------------------------------------[MN]
		This._Config = {
			FileContainer	: $( '.FRM_FileContainer' ),
			InputLabelHints	: {},
			ErrorField		: {}
		};
		//------------------------------------------------------[MN]

		// Constructor -----------------------------------------[MN]
		(function() {
			This._Config = $.extend(This._Config, Config);
		})();
		//------------------------------------------------------[MN]
		
		// Public Functions ------------------------------------[MN]
		This.AddFileInput = function() {
			if( This._Config.FileContainer.find( '.FRM_FileInput[value=""]' ).length == 0 )
				This._Config.FileContainer.append( '<div class="FRM_File"><input class="FRM_FileInput" type="file" name="Files[]" /><a href="#" class="FRM_FileDelete">X</a></div>' );
		}

		This.DeleteFileInput = function( File ) {
			File.remove();
			This.AddFileInput();
		}

		This.ShowFieldHints = function() {
			$('.FRM_FormTable input:text').each( function() {
				if ( jQuery.trim( $(this).val() ) == '' || jQuery.trim( $(this).val() ) == 0 || ( This._Config.InputLabelHints[$(this).attr('name')] != null && This._Config.InputLabelHints[$(this).attr('name')] == $(this).val() ) )
					if ( This._Config.InputLabelHints[$(this).attr('name')] != null )
						$(this).val( This._Config.InputLabelHints[$(this).attr('name')] ).addClass('FRM_InputEmpty');
			})

			$('.FRM_FormTable input:text').focus( function() {
				$(this).removeClass('FRM_InputEmpty').val( ( This._Config.InputLabelHints[$(this).attr('name')] != null && ( This._Config.InputLabelHints[$(this).attr('name')] == $(this).val() || jQuery.trim( $(this).val() ) == '' ) ) ? '' : $(this).val()	);
			});

			$('.FRM_FormTable input:text').blur( function() {
				if ( jQuery.trim( $(this).val() ) == '' )
					$(this).addClass('FRM_InputEmpty').val( ( This._Config.InputLabelHints[$(this).attr('name')] != null ) ? This._Config.InputLabelHints[$(this).attr('name')] : '' );
			});

			$('.FRM_FormTable .FRM_TextArea').each( function() {
				if ( jQuery.trim( $(this).val() ) == '' || jQuery.trim( $(this).val() ) == 0 || ( This._Config.InputLabelHints[$(this).attr('name')] != null && This._Config.InputLabelHints[$(this).attr('name')] == $(this).val() ) )
					if ( This._Config.InputLabelHints[$(this).attr('name')] != null )
						$(this).val( This._Config.InputLabelHints[$(this).attr('name')] ).addClass('FRM_InputEmpty');
			})

			$('.FRM_FormTable .FRM_TextArea').focus( function() {
				$(this).removeClass('FRM_InputEmpty').val( ( This._Config.InputLabelHints[$(this).attr('name')] != null && ( This._Config.InputLabelHints[$(this).attr('name')] == $(this).val() || jQuery.trim( $(this).val() ) == '' ) ) ? '' : $(this).val()	);
			});

			$('.FRM_FormTable .FRM_TextArea').blur( function() {
				if ( jQuery.trim( $(this).val() ) == '' )
					$(this).addClass('FRM_InputEmpty').val( ( This._Config.InputLabelHints[$(this).attr('name')] != null ) ? This._Config.InputLabelHints[$(this).attr('name')] : '' );
			});

			$('.FRM_FormTable .FRM_FieldSelect').focus( function() {
				$(this).removeClass('FRM_InputEmpty');
			});

			$('.FRM_FormTable .FRM_FieldSelect').change( function() {
				if ( $(this).val() == 0 )
					$(this).addClass('FRM_InputEmpty');
				else
					$(this).removeClass('FRM_InputEmpty');
			});

			$('.FRM_FormTable .FRM_FieldSelect').blur( function() {
				if ( $(this).val() == 0 )
					$(this).addClass('FRM_InputEmpty');
				else
					$(this).removeClass('FRM_InputEmpty');
			});
		}

		This.SwitchToHidden = function ( MyID ) {
			$('#' +MyID+'Hidden').hide();
			$('#' +MyID+'Shown').show().focus();
		}

		This.SwitchToShown = function( MyID ) {
			alert(2);
			var HiddenInput = $('#' +MyID+'Hidden');
			if( !HiddenInput .val() ){
				$('#' +MyID+'Shown').show();
				HiddenInput.hide()
			}
		}
	
		This.ShowInputErrors = function() {
			// Error Highlighting ----------------------------------[TN]
			for( i=0; i<This._Config.ErrorField.length; i++ ) {
				$(':input[name=' + This._Config.ErrorField[i] + ']').addClass('FRM_InputError');
				$(':radio[name=' + This._Config.ErrorField[i] + ']').parent().addClass('FRM_InputError');
				$(':checkbox[name=' + This._Config.ErrorField[i] + ']').parent().addClass('FRM_InputError');
			}

			$('.FRM_FileInput').each( function(){
				if ( $(this).hasClass('FRM_InputError') )
					$(this).parent().addClass('FRM_InputError');
			});
			//------------------------------------------------------[TN]
		}
		//------------------------------------------------------[MN]

		// Send Back Reference ---------------------------------[MN]
		return This;
		//------------------------------------------------------[MN]
	};
	return This();
};
//------------------------------------------------------[MN]
//------------------------------------------------------[  ]

var SwitchToHidden = function ( MyID ) {

	$('#' +MyID+'Hidden').focus().show();
	$('#' +MyID+'Shown').hide();
}

var SwitchToShown = function( MyID ) {
	var HiddenInput = $('#' +MyID+'Hidden');
	if( HiddenInput .val() ==  ''){
		$('#' +MyID+'Shown').show();
		HiddenInput.hide();
	}
}

// Tracking Object -------------------------------------[JG]
var Tracking = function( Config ) {
	var This = function() {

		// Public Vars -----------------------------------------[MN]
		This._Config = {
				URL:'http://view.atdmt.com/action/',
				LinkTo:false
		};
		//------------------------------------------------------[MN]

		// Constructor -----------------------------------------[MN]
		(function() {
			This._Config = $.extend(This._Config, Config);
		
			// Add Callback to this Function
			//$('#TrackIMG').ready(function(){This.LoadCallBack()});
		})();

		This.Track = function(TrackIndex,LinkTo){
			//alert('Track: '+ TrackIndex+' - '+LinkTo);
			var axel = Math.random() + "";
			var ord = axel * 1000000000000000000;

			This._Config.LinkTo = LinkTo;
			$('#TrackIMG').attr('src',This._Config.URL + TrackIndex + '?rand=' +ord);
			return false;
		};
		This.LoadCallBack = function(){
			if (This._Config.LinkTo){
				location.href=This._Config.LinkTo;
				This._Config.LinkTo = false;
			}
		};
		return This;

	};
	return This();

};


var TRK = new Tracking();
