var evtid=-1;

$().ready(function() {
	$("label").inFieldLabels();
	
	$("#marcherForm").validate({
		rules: {
			mrFirstName: {
				required: true,
				minlength: 2
			},
			mrLastName: {
				required: true,
				minlength: 2
			},
			mrCity: {
				required: true,
				minlength: 3
			},
			mrState: {
				required: true
			},
			mrEmail: {
				required: true,
				email: true
			},
			mrZipcode: {
				required: true,
				digits: true,
				minlength: 5,
				maxlength: 5
			}
		},
		submitHandler: function() {
			var fname = $("#mrFirstName").val();
			var lname = $("#mrLastName").val();
			var email = $("#mrEmail").val();
			var zip = $("#mrZipcode").val();
			var city = $("#mrCity").val();
			var state = $("#mrState").val();
            
			$.ajax({
			url:"http://www.letthepeoplevote.com/rsvp.php?evtid=VirtualMarcher&first=" + fname + "&last=" + lname + "&email=" + email + "&adr1=&adr2=&city=" + city + "&state=" + state + "&zipcode=" + zip,
			dateType:"text",
			success:function() {
				$(".mr-signup").replaceWith("<span id='submitThanksButton' class='cl_subtitle'>Thanks!</span>");	
			}
		});
		}
	});	
	
	
	$("#rsvp").validate({
		rules: {
			rsvpFirstName: {
				required: true,
				minlength: 2
			},
			rsvpLastName: {
				required: true,
				minlength: 2
			},
			rsvpEmail: {
				required: true,
				email: true
			},
			rsvpAdress: {
				required: true,
				minlength: 5
			},
			rsvpAddress2: {
				required: false,
				minlength: 5
			},
			rsvpCity: {
				required: true,
				minlength: 3
			},
			rsvpState: {
				required: true
			},
			rsvpZipcode: {
				required: true,
				digits: true,
				minlength: 5,
				maxlength: 5
			}
		},
		submitHandler: function() {
			var fname = $("#rsvpFirstName").val();
			var lname = $("#rsvpLastName").val();
			var email = $("#rsvpEmail").val();
			var zip = $("#rsvpZipcode").val();
			var adr1 = $("#rsvpAdress").val();
			var adr2 = $("#rsvpAddress2").val();
			var city = $("#rsvpCity").val();
			var state = $("#rsvpState").val();
            
			$.ajax({
			url:"http://www.letthepeoplevote.com/rsvp.php?evtid=" + evtid + "&first=" + fname + "&last=" + lname + "&email=" + email + "&adr1=" + adr1 + "&adr2=" + adr2 + "&city=" + city + "&state=" + state + "&zipcode=" + zip,
			dateType:"text",
			success:function() {
				$(".popupredBtn").replaceWith("<span id='submitThanksButton' class='cl_subtitle'>Thanks!</span>");	
				setTimeout(function() {
					$('#closersvp').click();
				},3000);
			}
		});
		}		
	});
	
	$("#smallForm").validate({
		rules: {
			smFirstName: {
				required: true,
				minlength: 2
			},
			smLastName: {
				required: true,
				minlength: 2
			},			
			smEmail: {
				required: true,
				email: true
			},
			smZipcode: {
				required: true,
				digits: true,
				minlength: 5,
				maxlength: 5
			}
		},
		messages: {
			smZipcode: {
				required: "Zip missing.",
				minlength: "Invalid Zip"
			}
		},
		submitHandler: function() {
			var fname = $("#smFirstName").val();
			var lname = $("#smLastName").val();
			var email = $("#smEmail").val();
			var zip = $("#smZipcode").val();
            
			$.ajax({
			url:"http://www.letthepeoplevote.com/subscribe.php?email=" + email + "&fname=" + fname + "&lname=" + lname + "&zip=" + zip,
			dateType:"text",
			success:function() {
				$(".sm-signup").replaceWith("<span id='submitThanksButton' class='cl_subtitle'>Thanks!</span>");
				$("#smFirstName").val("");
				$("#smLastName").val("");
				$("#smEmail").val("");
				$("#smZipcode").val("");	
				setTimeout(function() {
					$("#submitThanksButton").replaceWith("<input class='sm-signup' type='submit' value='' />");
				},3000);
			}
		});
		}		
	});	
	
	$("#subscribe").validate({
		rules: {
			email: {
				required: true,
				email: true
			},
			zipcode: {
				required: true,
				digits: true,
				minlength: 5,
				maxlength: 5
			}
		},
		messages: {
			zipcode: {
				required: "Zip missing.",
				minlength: "Too short."
			}
		},
		errorPlacement:null,
		success:null,
		submitHandler: function() {
			var email = $("#email").val();
			var zip = $("#zipcode").val();
            
			$.ajax({
			url:"http://www.letthepeoplevote.com/subscribe.php?email=" + email + "&fname=&lname=&zip=" + zip,
			dateType:"text",
			success:function() {
				$(".input-sendme").replaceWith("<span id='submitThanksButton' class='cl_subtitle' style='color:#FFFFFF;'>Thanks!</span>");
				$("#email").val("");
				$("#zipcode").val("");	
				setTimeout(function() {
					$("#submitThanksButton").replaceWith("<input class='input-sendme' type='submit' value='' />");
				},3000);
			}
		});
		}			
	});
	
	$("#donateForm").validate({
		rules: {
			donateFirstName: {
				required: true,
				minlength: 2
			},
			donateLastName: {
				required: true,
				minlength: 2
			},
			donateAddress: {
				required: true,
				minlength: 5
			},
			donateAddress2: {
				required: false,
				minlength: 5
			},
			donateZipcode: {
				required: true,
				digits: true,
				minlength: 5,
				maxlength: 5
			},
			state: {
				required: true
			},
			donateCity: {
				required: true,
				minlength: 3
			},
			donateEmail: {
				required: true,
				email: true
			},
			donateEmailConfirm: {
				required: true, 
				email: true,
				equalTo:"#donateEmail"
			},
			donatePhoneNumber: {
				required: false,
				digits: true
			},
			creditCardNumber: {
				required: true, 
				creditcard: true
			},
			donateExpirationMonth: {
				required: true, 
				digits: true
			},
			donateExpirationYear: {
				required: true, 
				digits: true
			},
			statement: {
				required:false
			},
			donationAmmountOther: {
				required:false
			},
			Amount1: {}
			},
			errorPlacement: function(error, element) {
				if(element.attr('name') != 'donationAmmountOther') {
					error.appendTo( element.parent() );
				}
			},
			success: function(label) {
				if(label.attr('for') != 'donationAmmountOther') {
					label.html("").addClass("valid");
				}
			}
	});
});
function GetCreditCardTypeByNumber(ccnumber) {
	var cc = (ccnumber + '').replace(/\s/g, ''); //remove space
	
	if ((/^(34|37)/).test(cc) && cc.length == 15) {
		return 'amex'; //AMEX begins with 34 or 37, and length is 15.
	} else if ((/^(51|52|53|54|55)/).test(cc) && cc.length == 16) {
		return 'mastercard'; //MasterCard beigins with 51-55, and length is 16.
	} else if ((/^(4)/).test(cc) && (cc.length == 13 || cc.length == 16)) {
		return 'visa'; //VISA begins with 4, and length is 13 or 16.
	} else if ((/^(300|301|302|303|304|305|36|38)/).test(cc) && cc.length == 14) {
		return 'DinersClub'; //Diners Club begins with 300-305 or 36 or 38, and length is 14.
	} else if ((/^(2014|2149)/).test(cc) && cc.length == 15) {
		return 'enRoute'; //enRoute begins with 2014 or 2149, and length is 15.
	} else if ((/^(6011)/).test(cc) && cc.length == 16) {
		return 'discover'; //Discover begins with 6011, and length is 16.
	} else if ((/^(3)/).test(cc) && cc.length == 16) {
		return 'JCB';  //JCB begins with 3, and length is 16.
	} else if ((/^(2131|1800)/).test(cc) && cc.length == 15) {
		return 'JCB';  //JCB begins with 2131 or 1800, and length is 15.
	}
	return ''; //unknow type
}

function setCType(n) {
	var type = GetCreditCardTypeByNumber(n);
	document.getElementById('creditCardType').value = type;
	if(type != "") {
		document.getElementById('creditCardNumber').className = "ci-input " + type;
	}
	else
		document.getElementById('creditCardNumber').className = "ci-input other";
}

function isNumberKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
	return false;
	return true;
}
	  
function otherAmount() {
	$("#Amount9").attr('checked','checked');
}

function changeAmount(sender) {
	$("#Amount").val(sender.value);
	return true;
}

function contribute() {
	//$("#donationSingleForm").submit();
	$("#donateForm").validate();
	if($("#donateForm").valid()) {
		var fname = $("#donateFirstName").val();
		var lname = $("#donateLastName").val();
		var refer = 399;
		var email = $("#donateEmail").val();
		var zip = $("#donateZipcode").val();
		var ip='0.0.0.0';
		
		$.ajax( {
			url:"subscribe.php?from=" + document.location.href + "&email=" + email + "&fname=" + fname + "&lname=" + lname + "&zip=" + zip,
			dateType:"text",
			complete:function() {
				$("#donateForm").submit();
			}
		});
	}
}

$().ready(function() {
	$('.rsvpTrigger').click(function() {
		evtid = $(this).attr('alt');
		$('.bluePopupContainer').css("top",($(this).offset().top-24) + "px").css("left",($(this).offset().left-210) + "px").fadeIn("slow");
	});
	$('#closersvp').click(function () {
		$('.bluePopupContainer').fadeOut("slow");
	});
});
