$(document).ready(function() {
	$("#phFirst").mask("999-999-9999");
	$("#phFinal").mask("999-999-9999");
	
	swapCity();
});

function swapCity(newcity) {
	$('.bigCity').fadeOut('fast', function () {
		$('.bigCity').attr('innerHTML', newcity);
		$('.bigCity').fadeIn('fast');
	});
	
	$('.cityHdr').attr('innerHTML', newcity);
}


function valPin() {
	var pin = $('#pin').val();

	if ((isWhole_re(pin)) && (pin.length == 4) && (pin != "5555")) {
		return true;
	} else {
		alert ("Pin invalid!");
		return false;
	}
}

function valPhone(theid, terms) {
	var fullnum = theid.value;
	

	if ((fullnum.length == 12) && (fullnum != "555-555-5555")) {
		if (terms == 1) {
			if($('#termsCheck').is(":checked") == true) {
				return true;
			} else {
				alert("You must agree to the terms and conditions!");
				return false;
			}
		} else {
			return true;
		}

	} else {
			alert ('Phone number invalid!');
			return false;

	}
}

var isWhole_re = /^\s*\d+\s*$/;
function isWhole (s) {
   return String(s).search (isWhole_re) != -1
}


function subPin(pin) {

	$.getJSON('lib/control.php?action=pin&pin=' + pin, function(data) {
		// do it to it lars
	});

}


function subPhone(phone) {
	var tosub = phone.replace(/[A-Za-z$-]/g, ""); // remove dashes from jq mask
	
	
	$.getJSON('lib/control.php?action=phone&ph=' + tosub, function(data) {
		// do it to it lars
	});
	

}



function nextStep(which) {
	var step = parseFloat(which);
	
	if (which == 2) {
		$('#step1').animate({marginLeft: '-50px', opacity: 0}, 200, function() {
			$("#step1").hide(function () {		
				$('#step2').animate({ marginRight: "50px", opacity: 1}, 200);
			});
		});	
	} else if (which == 3) {
		if (valPhone(document.getElementById("phFirst"), 0)) {
			$('#step2').animate({ marginLeft: '-50px', opacity:0}, 200, function() {
				$("#step2").hide(function() {
					$('#step3').animate({ marginRight: "50px", opacity: 1}, 200);
				});
				$('#phFinal').val($('#phFirst').val());
			});	
		} else {
			return false;
		}
	} else if (which == 4) {
			$('#step3').animate({ marginLeft: "-50px", opacity:0}, 200, function() {
				$("#step3").hide(function() {
					$('#bodyTerms').fadeIn('fast');
					$('#phFinal').focus();
					$('#step4a').animate({ marginRight: "50px", opacity: 1}, 200);
				});
			});

	} else if (which == 5) { // 'no thanks' option
		$('#step3').animate({ marginLeft: '-50px', opacity: 0}, 200, function() {
			$('#step3').hide(function () {
				$('#step4a').hide();
				$('#step4b').hide();
				$('#bodyTerms').fadeOut();
				$('#step5').animate({ marginRight: "50px", opacity: 1}, 200);
			});
		});		
	} else if (which == 6) {
		if(valPhone(document.getElementById("phFinal"), 1)) {
			$('#step4a').animate({ marginLeft: "-50px", opacity: 0}, 200, function() {
				subPhone(document.getElementById("phFinal").value);
				$("#step4a").hide(function() {
					$('#pin').focus();
					$('#step4b').animate({ marginRight: "50px", opacity: 1}, 200);
				});
			});
		} else {
			return false;
		}
	} else if (which == 7) {
		if(valPin()) {
			$('#step4b').animate({ marginLeft: "-50px", opacity: 0}, 200, function() {
				subPin(document.getElementById("pin").value);
				$("#step4b").hide(function() {
					$("#step3").hide();
					$('#bodyTerms').fadeOut();
					
					$('#step5').animate({ marginRight: "50px", opacity: 1}, 200);
				});
			});
		} else {
			return false;
		}
	}
}

