var errorcolor="#9daeb9"; //this is the error hightlight color of fields
var normalcolor="#ffffff";  // this is the normal background of fields

$(document).ready(function(){								   			   
	$("#button").hover(function(){
  		var src = $(this).attr('src'); 
		if (src.lastIndexOf("out") != -1) {
		$(this).attr('src', src.replace('out', 'over')); 
		} else {
		$(this).attr('src', src.replace('over', 'out')); 	
		}
  	});					   
					   
	$("#alert").validate({

		//debug: true,
		//turns off giving focus to last touched item (default is true)
		focusInvalid: false,
		//highlights invalid fields
		highlight: function(element, errorClass) {
			if ($(element).is('input:checkbox')) {
					$(element).siblings(".checkbox_copy").css('color', '#F02813');
		  } else {
					$(element).css("background-color",errorcolor); 
		  }

		},
		unhighlight: function(element, errorClass) {		
		  if ($(element).is('input:checkbox')) {                                         
						 $(element).siblings(".checkbox_copy").css('color', '#4D4C4C');
	
		  } else {                                 
				$(element).css("background-color",normalcolor); 
	
		  }                                            
				
			},
		errorPlacement: function(error, element) { 
			error.css('display', 'none');  
		},		
		submitHandler: function(form) {
			// do coppa here
			if (IsUnderage()) 
			{
				$("#alert").hide();
				$("#alert_sorry").css('display','block');
		
			} else 
			{
				var commandForm = jQuery("#alert");
				var urlStr = commandForm.attr("action"); 
				var dataStr = commandForm.serialize();
				jQuery.ajax({
				   type: "GET",
				   url: urlStr,
				   data: dataStr,
				   success: function(msg){	  	
						if (msg != null  && msg.substring(0,4).toLowerCase() == "succ")  {	
							$("#alert").hide();
							$("#alert_thankyou").css('display','block');
							//pixels
							//$("#sweeps_pixel").attr("src","");
							//$("#video_pixel").attr("src","");
							//$("#thankyou_pixel").attr("src","$thankyou_pixel");
						} else {
							$("#alert").hide();
							$("#alert_sorry").css('display','block');
						}
					}
				});	

		   }
		   
		return false;
	}
	});

 	$('#alert .field input').focus(function() {
		$(this).css('background-color',normalcolor);
	});
 	$('#alert .field select').focus(function() {
		$(this).css('background-color',normalcolor);
	});
	function determineDate(){
		function thisYear(){
			var d = new Date();
			d = d.getFullYear();
			return d;
		};
		function minusSeventyFive(){
			var first = thisYear();
			var sec = first - 75;
			val = sec  + ':' + first;
		};
		minusSeventyFive();
	};
	determineDate();
	$('#dateofbirth').datepicker({ showButtonPanel: false,changeYear: true, yearRange: val, changeMonth: true, onSelect: parseDate, onChangeMonthYear: updateInput }); 
	$('#dateofbirth').change(function() {
		parseDate($('#dateofbirth').val());
	});
	//set error messages to "", highlighting will be enough
	jQuery.validator.messages.required = "";
	jQuery.validator.messages.phone = "";
	jQuery.validator.messages.date = "";
	jQuery.validator.messages.zip = "";
	jQuery.validator.messages.email = ""; 
	jQuery.validator.addMethod("notName", function(value, element) {
		return this.optional(element) || (value != "Name"); 
	}, "");



//changes value of input field when month or year is changed in datepicker
function updateInput(year,month,inst) {
               if ($('#dateofbirth').val()) {
                              var inputText = $('#dateofbirth').val();
                              day = inputText.substring(3,5);
                              if (month < 10) {
                                             formattedMonth = '0' + month;
                              } else {
                                             formattedMonth = month;
                              }              
                              var newDate = formattedMonth + '\/' + day + '\/' + year
                              $('#dateofbirth').val(newDate);
                              parseDate(newDate);
               }              
}
	
//takes date from date picker and places month, day of month and year in hidden fields
function parseDate(dateStr) {
	
       var date = new Date(dateStr);
       var month = date.getMonth()+1;
       $('#birthMonth').val(month);
	   var day = date.getDate();
       $('#birthDay').val(day);
	   var year = date.getFullYear();
       $('#birthYear').val(year);
	   $('#alert').validate().element('#dateofbirth');
}	
	
});	




// COPPA STUFF
// Global Variables
var underage = false;
var expdate = new Date();
expdate.setTime(expdate.getTime() +  (24 * 60 * 60 * 1000 * 3)); 

function  IsUnderage() {
  date(); 
  var birth_year=$("#birthYear").val();
  var birth_month=$("#birthMonth").val();
  var birth_day=$("#birthDay").val();
  var isAlert = ""
	// Checks Year for less than 13
	if(((thisyear - 13) < birth_year) && birth_year != "0"){
	  //alert ("under 13");
	  underage = true;
	  SetCookie("AgeVerification",underage,expdate);
	  return true;
	} else if(eval(birth_year) == eval(thisyear - 13)){
	// Checks for the month on the 13the year 
			if((birth_month != "0") && (eval(thismonth) < eval(birth_month))) {
				underage = true;	
				SetCookie("AgeVerification",underage,expdate);
				//alert ("a few months short of 13");
				 return true;
			// checks for the day on the month of the 13th year    
			} else if (eval(birth_month) == eval(thismonth)) {
				if((birth_day != "0") && (eval(thisday) < eval(birth_day))) {       
					underage = true;
					SetCookie("AgeVerification",underage,expdate);
					//alert ("a few days short of 13");
					 return true;
				}
			//if date checks out check for cookie
			} else  {
				if(checkForCookie()) {
					return true;
				} 
				else {
					return false;
				}  
			}  			
	} else {

				if(checkForCookie()) {
					return true;
				} 
				else {
					return false;
				} 	
	}				
}


// If the data is fine - this checks the cookie to make sure that the person hasnt already been in coppa violation
function checkForCookie(){
	if (GetCookie("AgeVerification")== null ){
	//alert ("no cookie");
		return false;
		
	} else {
		if (GetCookie("AgeVerification")!=false) {
			//alert ("cookie set to underage");
			return true;
		} 
	}
}		
		
		
// Functions for Cookies
function date()
{
	var browser = navigator.appName;
	temp_date = new Date();
	thisday = temp_date.getDate();
	thismonth = (temp_date.getMonth()+1);
	thisyear = temp_date.getYear();

	if(browser == "Microsoft Internet Explorer")
	{
		thisyear = thisyear;
	}
	else if(browser == "Netscape")
	{
		thisyear = thisyear + 1900;
	}
}

function SetCookie(name,value,expires) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "");
}
function GetCookie(name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

// An adaptation of Dorcht's function for deleting a cookie.
function delCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
    ((path == null) ? "" : "; path=" + path) +
    ((domain == null) ? "" : "; domain=" + domain) +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
