petovera(document).ready(function() {
    challenge_replaceForm();

	csInit({
		domain_key:"A5HA7FC6JM5KVUQPFTKP"
		,afterSubmitContacts:function(contacts) {
   			petovera.each(contacts,function (idx,contact){
   				challenge_addEmail(contact.selectedEmail(),contact.fullName());
   			});
		}
	});
	petovera('#challenge-addblank').click(function(event) {
		event.preventDefault();
		challenge_addBlank();
	});
	petovera('#challenge-submit').click(function(event) {
		event.preventDefault();
		if (!petovera('#senderemail').val().match(/^\w+(\.*\w*)*@[a-zA-Z_]+?\.[a-zA-Z]{2,6}$/i)	|| petovera('#senderemail').val() == 'Email'){
			alert('Please ensure that the email is in the correct format, e.g. john@doe.com');
			return false;
		}
		if (petovera('#sendername').val() == ""	|| petovera('#sendername').val() == "Name"){
			alert('Please enter your name to continue.');
			return false;
		}
		petovera.ajax({
			 type : 'POST'
			,url : '/challenge/send.php'
			,dataType : 'json'
			,data: petovera('#challenge-form').serializeArray()
			,async:false
			,success : function(data){
				if (data.success != 1 && data.success != true) {
					alert(data.msg);
				}else {
					if (typeof pageTracker != undefined) {
						pageTracker._trackPageview('/challengeapp'+window.location.pathname);
						pageTracker._trackEvent('ChallengeApp','ChallengeSent',window.location.pathname);
						
					}
					petovera('#challenge-app').html(data.msg);
				}

			}
		});

	});
});

function challenge_addEmail(email,name){
		petovera("#challenge-list > ul:first").clone(true).insertAfter("#challenge-list > ul:last");
		petovera("#challenge-list > ul:last > li > input").each(function() { petovera(this).val(""); });
		petovera("#challenge-list > ul:last > li > input[name='name[]']").val(name).removeClass('challenge-shadow');
		petovera("#challenge-list > ul:last > li > input[name='email[]']").val(email).removeClass('challenge-shadow');
		challenge_afterAdd()
}
function challenge_addBlank(){
		petovera("#challenge-list > ul:first").clone(true).insertAfter("#challenge-list > ul:last");
		petovera("#challenge-list > ul:last > li > input").each(function() { petovera(this).val(""); });
		petovera("#challenge-list > ul:last > li > input[name='name[]']").val('Name').addClass('challenge-shadow');
		petovera("#challenge-list > ul:last > li > input[name='email[]']").val('Email').addClass('challenge-shadow');
		challenge_afterAdd();
}
function challenge_afterAdd(){
	petovera(".challenge-input").unbind('focus').unbind('blur')
	.focus(function() {
		if (petovera(this).val().match(/^(Email|Name|State)$/i)) petovera(this).val('').removeClass('challenge-shadow');

          })

	.blur(function() {
		if ((petovera(this).attr("name") == "email[]" || petovera(this).attr("name") == "senderemail") && petovera(this).val() == "")	petovera(this).val("Email").addClass('challenge-shadow');
		else if ( (petovera(this).attr("name") == "email[]" || petovera(this).attr("name") == "senderemail") && petovera(this).val() != "") {
		if (!petovera(this).val().match(/^\w+(\.*\w*)*@[a-zA-Z_]+?\.[a-zA-Z]{2,6}$/i))
			alert("Please ensure that the email is in the correct format, e.g. john@doe.com");
		}
		if ((petovera(this).attr("name") == "name[]" || petovera(this).attr("name") == "sendername") && petovera(this).val() == "") petovera(this).val("Name").addClass('challenge-shadow');
	});
}
function challenge_replaceForm(){
	var strOut = '';
	strOut += '<form method="post" action="http://www1.spartanrace.com/challenge/send-sugar.php" id="challenge-form">';
	strOut += '<input type="hidden" name="challenge_subjectBlurb" value="'+challenge_subjectBlurb+'">';
	strOut += '<input type="hidden" name="challenge_bodyBlurb" value="'+challenge_bodyBlurb+'">';
	strOut += '<p style="line-height:14px;">Spread the word about Spartan Race and save 10%! Enter Your Name and Email to get started!</p>';
	strOut += '<ul class="challenge-email" style="margin-bottom: 20px;">';
	strOut += '<li><input class="challenge-input challenge-shadow" type="text" value="Name" id="sendername" name="sendername" size="18" maxlength="30" /></li>';
	strOut += '<li><input class="challenge-input challenge-shadow" type="text" value="Email" id="senderemail" name="senderemail" size="18" maxlength="30" /></li>';
	strOut += '<li><input class="challenge-input challenge-shadow" type="text" value="State" id="senderstate" name="senderstate" size="18" maxlength="30" /></li>';
	strOut += '</ul>';
	strOut += '<a class="cs_import"><img src="/challenge/images/step1.png" alt="Add from Address Book" /></a>';
	strOut += '<p>Or add friends yourself:</p>';
	strOut += '<div class="input-customer">';
	strOut += '<span id="challenge-list">';
	strOut += '<ul class="challenge-email">';
	strOut += '<li><input class="challenge-input challenge-shadow" type="text" value="Name" name="name[]" size="18" maxlength="30" /></li>';
	strOut += '<li><input class="challenge-input challenge-shadow" type="text" value="Email" name="email[]" size="18" maxlength="30" /></li>';
	strOut += '</ul>';
	strOut += '<p style="margin-bottom: 15px; font-size:12px"><span id="challenge-addblank"><u>Click here to add more friends!</u></span></p>';
    strOut += '<p id="challenge-submit"><input type="image" src="/challenge/images/step2.png"></p>';
	strOut += '</span>';
	strOut += '</div>';
	petovera('#challenge-app').html(strOut);
	challenge_afterAdd();
}

