// JavaScript Document
$(document).ready(function(){
	$('#home1').autotab({target: 'home2', form: 'numeric'});
	$('#home2').autotab({target: 'home3', form: 'numeric'});
	$('#biz1').autotab({target: 'biz2', form: 'numeric'});
	$('#biz2').autotab({target: 'biz3', form: 'numeric'});
	$("input.data").focus(function(){
		$(this).css({'background-color' : '#eee', 'font-weight' : 'bold'});
	});
	$("input.data").blur(function(){
		$(this).css({'background-color' : '#fff', 'font-weight' : 'normal'});
	});
});

/* Check email */
function checkEmail(email) {
	var re = new RegExp('^\\S+@\\S+\\.[a-zA-Z_0-9]+$');
	return email.match(re);
}

function checkNum(feed,type){
	var lng = feed.value.length;
	var codascii = feed.value.charCodeAt(lng -1);
	if ( codascii==46 || ( codascii>=48 && codascii<=57)){
		return true;
	}
	else{
		document.getElementById(type).value= feed.value.substr(0,lng -1);
	}
}
function checkMonth(feed){
	document.signup.birthDate_month.value = feed;
}
function checkDay(feed){
	document.signup.birthDate_day.value = feed;
}
function checkYear(feed){
	document.signup.birthDate_year.value = feed;
}

function checkState(feed){
	document.signup.current_state.value = feed;
}

function checkH1(i){
	document.signup.h1.value = i;
}
function checkH2(i){
	document.signup.h2.value = i;
}
function checkH3(i){
	document.signup.phoneNumber.value = document.signup.h1.value+document.signup.h2.value+i;
}
function checkB1(i){
	document.signup.b1.value = i;
}
function checkB2(i){
	document.signup.b2.value = i;
}
function checkB3(i){
	document.signup.phoneNumber2.value = document.signup.b1.value+document.signup.b2.value+i;
}

function checkBox(check){
	if (check){
		document.signup.consent1.value = "yes";
	}
	else{
		document.signup.consent1.value = "no";
	}
}

/* Signup */
function checkSignup(){
	var today = new Date();

	var email = document.signup.email.value;
	document.signup.username.value = email.toLowerCase();

	var alias = document.signup.alias.value;
	var firstname = document.signup.firstname.value;
	var lastname = document.signup.lastname.value;

	var pwd=null;
	if (document.signup.password &&  document.signup.password.value != "" ) {
		pwd=document.signup.password.value;
	}
	
	
	var pwd2=null;
	if (document.signup.password2 &&  document.signup.password2.value != "" ) {
		pwd2=document.signup.password2.value;
	}
	
	var state = document.signup.state.value;

	var year = document.signup.birthDate_year.value;
	var month = document.signup.birthDate_month.value;
	var day = document.signup.birthDate_day.value;
	
	var y = year.length;
	var m = month.length;
	var d = day.length;

	var b1 = document.signup.biz1.value;
	var b2 = document.signup.biz2.value;
	var b3 = document.signup.biz3.value;
	
	var h1 = document.signup.home1.value;
	var h2 = document.signup.home2.value;
	var h3 = document.signup.home3.value;

	var business = b1+b2+b3;
	var home = h1+h2+h3;
	
	document.signup.phoneNumber.value = home;
	document.signup.phoneNumber2.value = business;

	var birthDate = new Date(year, month, day);
	var years = Math.floor((today.getTime() - birthDate.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
	
	if (years <= 13){
		alert("There has been an error with your submission.  We are sorry that we cannot accommodate your request at this time.");

		today.setTime(today.getTime()+(1*24*60*60*1000));
		var expires = "; expires="+today.toGMTString();
		document.cookie = "nypostCookie=nosignup; path=/Signup";
		document.location = "/";
		return false;
	}
	else{
		if (y == 0 || m == 0 || d == 0){
			document.signup.month.focus();
			document.signup.month.style.backgroundColor= "#FFFF91";
			alert("1 Please provide your full birth date.");
			return false;
		}
		else if (email == null || email == ""){
			document.signup.email.focus();
			document.signup.email.style.backgroundColor= "#FFFF91";
			alert("Please provide an Email address.");
			return false;
		}
		else if (checkEmail(email) == null){
			document.signup.email.focus();
			document.signup.email.style.backgroundColor= "#FFFF91";
			alert("Please provide a valid login (e-mail address).");
			return false;
		}
		else if (alias == null || alias == ""){
			document.signup.alias.focus();
			document.signup.alias.style.backgroundColor= "#FFFF91";
			alert("Please provide a Display Name.");
			return false;
		}
		else if (firstname == null || firstname == ""){
			document.signup.firstname.focus();
			document.signup.firstname.style.backgroundColor= "#FFFF91";
			alert("Please provide your First Name");
			return false;
		}
		else if (lastname == null || lastname == ""){
			document.signup.lastname.focus();
			document.signup.lastname.style.backgroundColor= "#FFFF91";
			alert("Please provide your Last Name");
			return false;
		}
		else if (pwd != null && pwd == ""){
			document.signup.password.focus();
			document.signup.password.style.backgroundColor= "#FFFF91";
			alert("Please provide a Password. The Password needs to be between 6-16 characters.");
			return false;
		}
		else if (pwd2 != null && pwd2 == ""){
			document.signup.password2.focus();
			document.signup.password2.style.backgroundColor= "#FFFF91";
			alert("Please confirm your Password.");
			return false;
		}
		else if (pwd != pwd2){
			document.signup.password.focus();
			document.signup.password.style.backgroundColor= "#FFFF91";
			alert("Your Password and Password Confirmation do not match.");
			return false;
		}
		else if (pwd != null && pwd.length < 6){
			document.signup.password.focus();
			document.signup.password.style.backgroundColor= "#FFFF91";
			alert("Your Password is too short. It should be at LEAST 6 characters.");
			return false;
		}
		else if (pwd2 != null && pwd2.length < 6){
			document.signup.password2.focus();
			document.signup.password2.style.backgroundColor= "#FFFF91";
			alert("Your Password is too short. It should be at LEAST 6 characters.");
			return false;
		}
		return true;
	}
}

/* Send Letter Editor */
function checkSendEditor(mod){
	var firstname = mod.firstname.value;
	var lastname = mod.lastname.value;
	var email = mod.email.value;
	var sub = mod.subject.value;
	var msg = mod.message.value;
	
	if (firstname == null || firstname == ""){
		mod.firstname.focus();
		mod.firstname.style.backgroundColor= "#FFFF91";
		alert("Please provide your First Name");
		return false;
	}
	else if (lastname == null || lastname == ""){
		mod.lastname.focus();
		mod.lastname.style.backgroundColor= "#FFFF91";
		alert("Please provide your Last Name");
		return false;
	}
	else if (checkEmail(email) == null){
		mod.email.focus();
		mod.email.style.backgroundColor= "#FFFF91";
		alert("Please provide a valid login (e-mail address)");
		return false;
	}
	else if (sub == null || sub == ""){
		mod.subject.focus();
		mod.subject.style.backgroundColor= "#FFFF91";
		alert("Please provide the subject of your letter");
		return false;
	}
	else if (msg == null || msg == ""){
		mod.message.focus();
		mod.message.style.backgroundColor= "#FFFF91";
		alert("Please insert your message.");
		return false;
	}
	else{
		return true;
	}
}

function TrackCount(fieldObj,countFieldName,maxChars) {
	var countField = eval("fieldObj.form."+countFieldName);
	var diff = maxChars - fieldObj.value.length;
		
	// Need to check & enforce limit here also in case user pastes data
	if (diff < 0) {
		fieldObj.value = fieldObj.value.substring(0,maxChars);
		diff = maxChars - fieldObj.value.length;
	}
}
		
function LimitText(fieldObj,maxChars) {
	var result = true;
	if (fieldObj.value.length >= maxChars) {
		result = false;
	}
	if (window.event) {
		window.event.returnValue = result;
	}
	return result;
}