/*
* GRCounseling.js
*
* Common javascript for folio 011
*
* Copyright 2010 FreshFolios.com
*/
var grc = window.grc || {}; 

grc.common = (function($){
  
  //some vars
  var $body,$jump,$subnav,host;
  
  //Init
  function init(){
    initVars();
	initPage();
  }
  
  //Init Vars
  function initVars(){
  	host = "http://www.grcounseling.com/";
  	$body = $("body");
	$jump = $("#jml");
	$subnav = $("#subNav");
  }
  
  //Init Page
  function initPage(){
  	var cat = $body.attr('class');
	
	if ($jump.length > 0) {
		$jump.bind("change", function(e){
			var val = $(this).val();
			if (val != "") {
				location.href = host+val+'/';
			}
		});
	}
	
	if($subnav.length>0){
		$("dt."+cat).addClass('sel');
		
		$("dd").each(function(){
			if(!$(this).prev().is("."+cat)){
				$(this).hide();
			}
		});
		
		$("dt.options a").click(function(e){
			e.preventDefault();
			
			if($(this).parent().is('.sel')){
				$(this).parent().removeClass('sel');
				$("dd:visible").slideUp("slow");
			}else{
				$("dd:visible").slideUp("slow");
				$(this).parent().addClass('sel');
				$(this).parent().next().slideDown("slow");
			}
			
		});
	}
	
	if($body.is('.referral')){
		var currentTime = new Date()
		var month = currentTime.getMonth() + 1;
		var day = currentTime.getDate();
		var year = currentTime.getFullYear();
		var curr = null;
		var $submit = $("input.submit");
		var validate = new Validation;
		
		$(".datepicker").val("M" + "/" + "D" + "/" + "Y")
			.each(function(){
				$me = $(this);
				
				$me.DatePicker({
					format:'m/d/Y',
					date: $("#"+$(this).attr("id")).val(),
					current: $("#"+$(this).attr("id")).val(),
					starts: 1,
					position: 'r',
					onBeforeShow: function(){
						curr = $(this).attr("id");
						$("#"+$(this).attr("id")).DatePickerSetDate(month + "/" + day + "/" + year, true);
					},
					onChange: function(formated, dates){
						$("#"+curr).val(formated);
						if ($('#closeOnSelect input').attr('checked')) {
							$("#"+curr).DatePickerHide();
						}
					}
				});
				
			});
			
		//bind submit
		$submit.bind("click",function(){
				
				validate._run("#form");
				if(validate.errors<1){
					var perms = $("#form").find("input:not(.submit), textarea").serialize();
	        //alert(perms);
	        
					$.ajax({
					  url: 'http://www.grcounseling.com/process_referral/',
					  data:perms,
					  type: 'post',
					  success: function(data) {
					  // validate._reset("#form");
		
					   $('span.thanks').show();
					   setTimeout(function(){
					   		window.print();
					   },2000);
					   setTimeout(function(){
					   	 	$('span.thanks').hide();
					   },5000)
					  }
					});
				
				}
		});
	}
	
	if($body.is('.register')){
		var currentTime = new Date()
		var month = currentTime.getMonth() + 1;
		var day = currentTime.getDate();
		var year = currentTime.getFullYear();
		var curr = null;
		var $submit = $("input.submit");
		var validate = new Validation;
		
		$(".datepicker").val("M" + "/" + "D" + "/" + "Y")
			.each(function(){
				$me = $(this);
				
				$me.DatePicker({
					format:'m/d/Y',
					date: $("#"+$(this).attr("id")).val(),
					current: $("#"+$(this).attr("id")).val(),
					starts: 1,
					position: 'r',
					onBeforeShow: function(){
						curr = $(this).attr("id");
						$("#"+$(this).attr("id")).DatePickerSetDate(month + "/" + day + "/" + year, true);
					},
					onChange: function(formated, dates){
						$("#"+curr).val(formated);
						if ($('#closeOnSelect input').attr('checked')) {
							$("#"+curr).DatePickerHide();
						}
					}
				});
				
			});
			
		//bind submit
		$submit.bind("click",function(){
				
				validate._run("#form");
				if(validate.errors<1){
					var perms = $("#form").find("input:not(.submit), textarea").serialize();
	        //alert(perms);
	        
					$.ajax({
					  url: 'http://www.grcounseling.com/process_registration/',
					  data:perms,
					  type: 'post',
					  success: function(data) {
					  // validate._reset("#form");
		
					   $('span.thanks').show();
					   setTimeout(function(){
					   		window.print();
					   },2000);
					   setTimeout(function(){
					   	 	$('span.thanks').hide();
					   },5000)
					  }
					});
				
				}
		});
	}
	
	
  }
 
  
  return {
  	init:init
	};
   
})(jQuery.noConflict());
jQuery(grc.common.init); 

var Validation;
/*
 * Validation 
 * 	| form container needs: class=form
 * 	| submit button needs: class=submit
 *  | fields that are required need: class=req
 *  | fields need name attribute = field prefill value
 */
(function($) {
	Validation = function() {
   		;
	};
	Validation.prototype = {
		self : null,
		errors : null,
		
		_run : function(form){
			var $fields = $(form).find("input:not(.submit), textarea");
			var errors = 0;
			
			$fields.each(function(){
				if($(this).is('.req')){
					if($(this).val() == "" || $(this).val() == $(this).attr('name')){
						var $me = $(this);
						$me.addClass('red').focus().val('Required');
						setTimeout(function(){
							$me.removeClass('red').val("");
						},3000);
						
						errors++;
					}
					
				}
				
			});
			
			if(!$("input[name=services[]]:checked").val()){
					$(".services legend span").show();
					$("input[name=services[]]").get(0).focus();
					setTimeout(function(){
							$(".services legend span").hide();
						},4000);
					errors++;
			}
			
			this.errors = errors;
		},
		
		_reset : function(form){
			var $fields = $(form).find("input:not(.submit), textarea");
			
			$fields.each(function(){
				$(this).val( $(this).attr('name'));
			});
		}
		

	};
	
})(jQuery);


