$.fn.setOmnitureValues = function() {	
	s.pageName=""
	 s.server="";
	 s.domain="";
	 s.channel=""
	 s.ch=""
	 s.prop24=""
	 s.eVar24=""
	 s.eVar19=""
	 s.nameplate=""
	 s.modelyear="";
	 s.extcolor=""
	 s.intcolor=""
	 s.vehicletrim=""
	 s.engine=""
	 s.language=""
	 s.searchtype=""
	 s.searchzip=""
	 s.searchstate=""
	 s.searchdealer=""
	 s.searchterm=""
	 s.leadtype=""
	 s.microsite=""
	 s.testdrive=""
	 s.campaign=""
	 s.events=""
	 s.products=""
};

var isSubmitting = false;

$(document).ready(function(e){
	
	
	$(".disccheckbox").toggle(function () {
		$(this).removeClass("unchecked");
		$(this).addClass("checked");
	}, function () {
		$(this).removeClass("checked");
		$(this).addClass("unchecked");
	});
	$("#firstname").focus(function () {
		var val = $(this).val();
		if(val.toLowerCase() == "first name *") {
			$(this).val("");
		}
	});
	$("#firstname").blur(function () {
		var val = $(this).val();
		if(val.toLowerCase() == "") {
	    	$(this).val("First Name *");
		}
	});
	$("#lastname").focus(function () {
		var val = $(this).val();
		if(val.toLowerCase() == "last name *") {
		    $(this).val("");
		}
	});
	$("#lastname").blur(function () {
		var val = $(this).val();
		if(val.toLowerCase() == "") {
		    $(this).val("Last Name *");
		}
	});
	$("#email").focus(function () {
		var val = $(this).val();
		if(val.toLowerCase() == "email *") {
		    $(this).val("");
		}
	});
	$("#email").blur(function () {
		var val = $(this).val();
		if(val.toLowerCase() == "") {
		    $(this).val("Email *");
		}
	});
	$("#mobilenumber").focus(function () {
		var val = $(this).val();
		if(val.toLowerCase() == "mobile number") {
		    $(this).val("");
		}
	});
	$("#mobilenumber").blur(function () {
		var val = $(this).val();
		if(val.toLowerCase() == "") {
		    $(this).val("Mobile Number");
		}
	});
	$("#zipcode").focus(function () {
		var val = $(this).val();
		if(val.toLowerCase() == "zip code *") {
		    $(this).val("");
		}
	});
	$("#zipcode").blur(function () {
		var val = $(this).val();
		if(val.toLowerCase() == "") {
		    $(this).val("Zip Code *");
		}
	});

	$('#showtrules').click(function(e){
		e.preventDefault();
		trackTournamentRules();
	});

	$('a.registernow').click(function(e){
		trackRegisterNow();
	});
	$('a.directions').click(function(e){
		trackGetDirections()
	});


	$('.handraiser .side-tout-anchor').hover(function(e){
		$(this).find('img').css({'top':"-152px"});
	},function(e){
		$(this).find('img').css({'top':0});
	});
	
	
	$('.emaillink').click(function(e){
	    e.preventDefault();
	    trackEmailClick();
	    return false;
	});
    
    $('#submit_button').click(function(e){
        e.preventDefault();
    });

	$(".submit").click(function (e) {
		e.preventDefault();
		
		if(isSubmitting){
			return false;
		}
		
		
    	var isErrors = false;
    	var firstname = $("#firstname");
    	var lastname = $("#lastname");
    	var email = $("#email");
    	var mobile = $("#mobilenumber");
    	var zip = $("#zipcode");
		
		var firstnamespan = $("#firstnamespan");
		var lastnamespan = $("#lastnamespan");
    	var emailspan = $("#emailspan");
    	var mobilespan = $("#mobilenumberspan");
    	var zipspan = $("#zipcodespan");
        
        	//Validate the first name box.
		var nameRegEx =/[a-zA-Z]/;
		var val = firstname.val();
    	if(jQuery.trim(val.toLowerCase()) == "" || jQuery.trim(val.toLowerCase()) == "first name *" || !nameRegEx.test(firstname.val())) {
    		isErrors = true;
    		firstname.addClass("inputerror");
		    firstnamespan.addClass("error");
    	} else {
		    firstname.removeClass("inputerror");
		    firstnamespan.removeClass("error");
		}
		
		//Validate the last name box.
		var lnameRegEx =/[a-zA-Z]/;
		var val = lastname.val();
		if(jQuery.trim(val.toLowerCase()) == "" || jQuery.trim(val.toLowerCase()) == "last name *" || !lnameRegEx.test(lastname.val())) {
			isErrors = true;
			lastname.addClass("inputerror");
			lastnamespan.addClass("error");
		} else {
			lastname.removeClass("inputerror");
			lastnamespan.removeClass("error");
		}
		//validate phone number
		//We don't need this validation considering the phone number is the one optional field.
		/*if(jQuery.trim(mobile.val()) == "" || jQuery.trim(mobile.val()) == "MOBILE") {
			isErrors = true;
			mobilespan.addClass("error");
		} else {
			mobilespan.removeClass("error");
		}
		*/
		
		var val = mobile.val();
		if(val.toLowerCase() != "mobile number" && val.toLowerCase() != '') {
			if (!validatePhoneNumber(jQuery.trim(mobile.val()))){
			    isErrors = true;
				mobilespan.addClass("error");
				mobile.addClass('inputerror');
			} else {
				mobilespan.removeClass("error");
				mobile.removeClass('inputerror');
			}
		}else{
		    mobilespan.removeClass("error");
			mobile.removeClass('inputerror');
		}
		
		
		//Validate the email box.
		var emailRegEx =/^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
		var val = email.val();
		if(jQuery.trim(val.toLowerCase()) == "" || jQuery.trim(val.toLowerCase()) == "email *" || !emailRegEx.test(val)) {
			isErrors = true;
			email.addClass("inputerror");
			emailspan.addClass("error");
		} else {
			email.removeClass("inputerror");
			emailspan.removeClass("error");
		}
		
		//Validate the zip code box.
		var zipCodeRegEx = /^\d{5}([\-]\d{4})?$/;
		var val = zip.val();
		if(jQuery.trim(val.toLowerCase()) == "" || jQuery.trim(val.toLowerCase()) == "zip code *" || !zipCodeRegEx.test(val)) {
			isErrors = true;
			zip.addClass("inputerror");
			zipspan.addClass("error");
		} else {
			zip.removeClass("inputerror");
			zipspan.removeClass("error");
		}
		
		if((!$(".disccheckbox").hasClass("checked")) && (isErrors==true)) {
			$('.legaldisclaimer_uncheckalert').css("visibility", "hidden");
			$('.requiredmark').css("visibility", "hidden");
			$('.fixfields').css('visibility','visible');
		}else if(!$(".disccheckbox").hasClass("checked")) {
			$('.fixfields').css('visibility','hidden');
			$('.requiredmark').css("visibility", "hidden");
			$(".legaldisclaimer_uncheckalert").css("visibility", "visible");
		}else{  
			$('.legaldisclaimer_uncheckalert').css("visibility", "hidden");
        	if(isErrors) {
        		$(".fixfields").css("visibility", "visible");
        		$('.requiredmark').css("visibility", "hidden");
			} else {
				isSubmitting = true;
				$(".fixfields").css("visibility", "hidden");
				$('.handraiser').find('.error').removeClass('error');
				
				var domain = (document.domain) ? document.domain : 'www.vw.com';
				var ajaxGetURL = 'http://' + domain + '/handraiser/ProcessHandraiser.do';
				var xml = '<?xml version="1.0" encoding="UTF-8"?>';
				xml += '<handraiser><campaignID>USVW-CE-JRMASTER-RSP</campaignID><contact><firstName>' + firstname.val() + '</firstName><lastName>' + lastname.val() + '</lastName><email>' + email.val() + '</email>';
				if (mobile.val() != '' && mobile.val() != 'Mobile Number') xml += '<mobile preferred="false">' + stripAlphaChars(jQuery.trim(mobile.val())) + '</mobile>';
				xml += '<address><zip>' + stripAlphaChars(jQuery.trim(zip.val())) + '</zip></address></contact><desiredCar><model>VW0000</model></desiredCar></handraiser>';
				
				$.ajax({
					type: 'GET',
					data: {xml: xml},
					url: ajaxGetURL,
					success: function(xml) {
						var handraiserID = $(xml).text();
						if (handraiserID > 0)
						{
							$('.fixfields').css("visibility", "hidden");
							$('.requiredmark').html('Thanks! We\'ll send updates.').css({visibility:"visible", color:"#FFF"});
						}
						
						$.fn.setOmnitureValues();
						s.pageName="vwhype:soccer:junior:handraiser:submit";
						s.server="www.vw.com";
						s.domain="www.vw.com";
						s.channel="vw hype";
						s.prop2="www.vw.com";
						s.prop16="www.vw.com";
						s.prop24="soccer";
						s.prop49=window.location.href;
						s.eVar5="www.vw.com";
						s.eVar19="vwhype:soccer:junior:handraiser:submit";
						s.eVar21="www.vw.com";
						s.ch="vw hype";
						var s_code=s.t();
						if(s_code)document.write(s_code);
						$.fn.setOmnitureValues();
						trackDoubleClick("type=vwjmt;cat=emailtyp");
						
						//Clear out the handraiser form.
						$("#firstname").val("First Name *");
						$("#lastname").val("Last Name *");
						$("#email").val("Email *");
						$("#mobilenumber").val("Mobile Number");
						$("#zipcode").val("Zip Code *");
						isSubmitting = false;
					},
					error: function(e) {
						isSubmitting = false;
						$('.fixfields').css("visibility", "hidden");
						$('.requiredmark').html('Oops. Please try again later.').css({visibility:"visible", color:"#F00"});
					}
				});
			}
		}
	});
});

function trackTournamentRules(){
	$.fn.setOmnitureValues();
	s.pageName="vwhype:soccer:junior:usfinals:viewrules";
	s.server="www.vw.com";
	s.domain="www.vw.com";
	s.channel="vw hype";
	s.prop2="www.vw.com";
	s.prop16="www.vw.com";
	s.prop24="soccer";
	s.prop49=window.location.href;
	s.eVar5="www.vw.com";
	s.eVar19="vwhype:soccer:junior:usfinals:viewrule";
	s.eVar21="www.vw.com";
	s.ch="vw hype";
	var s_code=s.t();
	if(s_code)document.write(s_code);
	$.fn.setOmnitureValues();
	
	trackDoubleClick("type=vwjmt;cat=jmrules");
	
}
function trackDownloadPDF(){
	$.fn.setOmnitureValues();
	s.pageName="vwhype:soccer:junior:iusfinals:downloadrules";
	s.server="www.vw.com";
	s.domain="www.vw.com";
	s.channel="vw hype";
	s.prop2="www.vw.com";
	s.prop16="www.vw.com";
	s.prop24="soccer";
	s.prop49=window.location.href;
	s.eVar5="www.vw.com";
	s.eVar19="vwhype:soccer:junior:usfinals:downloadrules";
	s.eVar21="www.vw.com";
	s.ch="vw hype";
	var s_code=s.t();
	if(s_code)document.write(s_code);
	$.fn.setOmnitureValues();
}

function trackRegisterNow(){
	var loc = window.location.href;
	var pgs = loc.split("/");
	var pg = pgs[5];
	
	$.fn.setOmnitureValues();
	s.pageName="vwhype:soccer:junior:usqualifiers:"+pg+":register";
	s.server="www.vw.com";
	s.domain="www.vw.com";
	s.channel="vw hype";
	s.prop2="www.vw.com";
	s.prop16="www.vw.com";
	s.prop24="soccer";
	s.prop49=window.location.href;
	s.eVar5="www.vw.com";
	s.eVar19="vwhype:soccer:junior:usqualifiers:"+pg+":register";
	s.eVar21="www.vw.com";
	s.ch="vw hype";
	var s_code=s.t();
	if(s_code)document.write(s_code);
	$.fn.setOmnitureValues();
}

function trackGetDirections(){
	var loc = window.location.href;
	var pgs = loc.split("/");
	var pg = pgs[5];
	
	$.fn.setOmnitureValues();
	s.pageName="vwhype:soccer:junior:usqualifiers:"+pg+":directions";
	s.server="www.vw.com";
	s.domain="www.vw.com";
	s.channel="vw hype";
	s.prop2="www.vw.com";
	s.prop16="www.vw.com";
	s.prop24="soccer";
	s.prop49=window.location.href;
	s.eVar5="www.vw.com";
	s.eVar19="vwhype:soccer:junior:usqualifiers:"+pg+":directions";
	s.eVar21="www.vw.com";
	s.ch="vw hype";
	var s_code=s.t();
	if(s_code)document.write(s_code);
	$.fn.setOmnitureValues();
}

function trackEmailClick(){
	window.location.href="mailto:vw@asgsports.com;"
	$.fn.setOmnitureValues();
	s.pageName="vwhype:soccer:junior:email:"+pageName;
	s.server="www.vw.com";
	s.domain="www.vw.com";
	s.channel="vw hype";
	s.prop2="www.vw.com";
	s.prop16="www.vw.com";
	s.prop24="soccer"; 
	s.prop49=window.location.href;
	s.eVar5="www.vw.com";
	s.eVar19="vwhype:soccer:junior:email:"+pageName;
	s.eVar21="www.vw.com";
	s.ch="vw hype";
	var s_code=s.t();
	if(s_code)document.write(s_code);
	$.fn.setOmnitureValues();
}

/* function to strip non-numeric characters from a string */

function stripAlphaChars(str) {  
	return str.replace(/[^0-9]/g, '');  
}

/* function to test if string is empty */
//function isEmpty(str) {	
//	if(str == null || str == "" || str.length <1)
//		return true;
//	return false;
//};


function validatePhoneNumber(elementValue){
    var phoneNumberPattern = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
    if(phoneNumberPattern.test(elementValue))
        return true;
    else
        return false;
}

/* function to test validity of phone number field */

// function isValidPhone(p_phone){
// 
// 	p_phone = stripAlphaChars(p_phone);
// 
// 	var valid = true;
// 	
// 	// we are allowing the format 12345 or 12345-6789
// 	if ( p_phone == null || p_phone == "" || p_phone.length < 10 || p_phone.length > 10 || isNaN(parseInt(p_phone,10))) {
// 		valid = false;
// 	}
// 	
// 	for ( var i = 0; i < p_phone.length; i++ ){	
// 	
// 		if (isNaN(p_phone.charAt(i)))
// 			return false;
// 		}
// 	
// 	return valid;
// }




function trackDoubleClick(typeCat){
	var baseURL = "http://fls.doubleclick.net/activityi;src=1033942;";
	var axel = Math.random()+"";
	var a = axel * 10000000000000;
	var extra = ";ord=1;num="+ a + '?';
	if(document.getElementById("DCLK_FLDiv")){
		var flDiv=document.getElementById("DCLK_FLDiv");
	}else{
		var flDiv=document.body.appendChild(document.createElement("div"));
		void(flDiv.setAttribute("id","DCLK_FLDiv"));
		void(flDiv.style.position="absolute");
		void(flDiv.style.visibility="hidden");
		void(flDiv.style.top="0px");
		void(flDiv.style.height="1px");
		void(flDiv.style.width="1px");
	}
	void(flDiv.innerHTML='<iframe id="DCLK_FLIframe" src="'+baseURL+typeCat+extra+'"></iframe>');
}
