jQuery(document).ready(function(){	

	jQuery(".form_block ul li a").click(function(){
		// Close'em up
		if(jQuery(this).hasClass('active')){
			jQuery('.form_block ul li a.active').removeClass('active');
			jQuery("li.sublist").hide();
			return false;
		}
		jQuery('.form_block ul li a.active').removeClass('active');
		jQuery("li.sublist").hide();
		
		//Make Active
		jQuery(this).addClass("active");
		jQuery(this).parent("li").next("li.sublist").show().addClass('active');
		
		return false; 
	});
	
    
	if (jQuery('.bounceArrow')) {
		bounceArrow();
	}
	
	jQuery('#aicons').bind({
		mouseenter: function() {
			jQuery('#rolloverbox').stop(true, true).show();
		},
		mouseleave: function() {
			jQuery('#rolloverbox').stop(true, true).hide();
		},
		"firstload": function() {
			jQuery('#rolloverbox').hide();
		}
	});
	
	jQuery('#aicons').trigger("firstload");
	
	jQuery.validator.addMethod( "isZipCode", function(value, element){
	    return value.match(new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/));
	    }, "Please specify a valid zip code"
	);
	
	jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
	    phone_number = phone_number.replace(/\s+/g, ""); 
		return this.optional(element) || phone_number.length > 9 &&
			phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
	}, "Please specify a valid phone number");
	
	jQuery('.form_block').validate({
		errorClass: "invalid",
		focusCleanup: true,
		submitHandler: function(form) {
			if (jQuery('#state').val()!="" && jQuery('#zipcode').val()!="Your ZIP Code..." ) {
   				PostForm();
   			}
 		}
	});
	
	jQuery('a.thankyouredirect').parent().hide();
	
	jQuery('a.thankyouredirect').bind('click', function() {
		var loc = $(this).attr("href");
	  	window.location = loc;
	});
	
	jQuery('.stepbox.two').hide();
	jQuery('.stepbox.three').hide();
	jQuery('.stepbox.four').hide();
	
	if (jQuery('.themeTester').length) {
		switch(jQuery('.themeTester').css('height'))
		{
		case '1px':
			/*if(jQuery('[name="locationCode"]').length == 0){
				jQuery('#liveballform').append('<input id="locCode" name="locationCode" type="hidden" value="AIO">');
			} else {
				jQuery('#locCode').val('AIO');
			}*/
			break;
		case '2px':
			jQuery('#programOfInterest').html('<option value="-1">Select a Program...</option><option value="BSBA">BS in Business</option><option value="CJBS">BS in Criminal Justice</option><option value="BALA">BA in Liberal Arts</option><option value="BAPSY">BA in Psychology</option><option value="MAEAET">MAEd in Adult Education and Training</option><option value="MAEDHPE">MAEd in Higher and Postsecondary Education</option><option value="MAETAL">MAEd in Teaching and Learning</option><option value="MAFPSY">MA in Forensic Psychology</option><option value="MAIOP">MA in Ind/Org Psychology</option><option value="MASEP">MA in Sport-Exercise Psychology</option><option value="MSHSM">MS in Health Services</option><option value="MSM">MS in Management</option><option value="MPAD">MA in Public Administration</option><option value="MBA">MA in Business Administration</option><option value="DBA">EdD in Business Administration</option><option value="EDDCCEL">EdD in Community College Exec. Leadership</option><option value="EDDCP">EdD in Counseling Psychology</option><option value="EDDEL">EdD in Educational Leadership</option><option value="EDDOL">EdD in Organizational Leadership</option><option value="EDDPCC">EdD in Pastoral Community Counseling</option><option value="EDDTAL">EdD in Teaching and Learning</option>');
			if(jQuery('#locCode').length == 0){
				jQuery('#liveballform').append('<input id="locCode" name="locationCode" type="hidden" value="AUO">');
			} else {
				jQuery('#locCode').val('AUO');
			}
			break;
		case '3px':
			if(jQuery('#locCode').length == 0){
				jQuery('#liveballform').append('<input id="locCode" name="locationCode" type="hidden" value="SUO">');
			} else {
				jQuery('#locCode').val('SUO');
			}
			break;
	
		}
	}

/*	!Dynamic Form Addition	*/

	if (jQuery('#liveballform').length) {
		
		jQuery('.attnarrow').show();
		
		jQuery('#liveballform input[id*="email"]').addClass('email');
		jQuery('#liveballform input[id*="addresszip"]').addClass('isZipCode');
		jQuery('#liveballform input[id*="homephone"]').addClass('phoneUS');
		jQuery('#liveballform input[id*="highschoolgradyear"]').addClass('isGradYear');
		
		jQuery('#liveballform input, #liveballform select').addClass('required');
		
		jQuery('#liveballform input[name=namefirst]').attr('name','nameFirst');
		jQuery('#liveballform input[name=namelast]').attr('name','nameLast');
		jQuery('#liveballform select[name=programofinterest]').attr('name','programOfInterest');
		jQuery('#liveballform input[name=homephone]').attr('name','homePhone');
		jQuery('#liveballform select[name=addressstate]').attr('name','addressState');
		jQuery('#liveballform input[name=addresszip]').attr('name','addressZip');
		jQuery('#liveballform input[name=addressline1]').attr('name','addressLine1');
		jQuery('#liveballform [name=addresscountrycode]').attr('name','addressCountryCode');
		jQuery('#liveballform [name=highschoolgradyear]').attr('name','highSchoolGradYear');
		jQuery('#liveballform [name=locationcode]').attr('name','locationCode');
		jQuery('#liveballform [name=testcase]').attr('name','testCase');
		jQuery('#liveballform select[name=militarystatus]').attr('name','militaryStatus');
	

		if(jQuery('#vendCode').length == 0){
			jQuery('#liveballform').append('<input id="vendCode" name="vendorCode" type="hidden" value="DISPLAY">');
		}

		if(jQuery('#srcCode').length == 0){
			jQuery('#liveballform').append('<input id="srcCode" name="sourceCode" type="hidden" value="ADVER">');
		}

		if(jQuery('[name="testCase"]').length == 0){
			jQuery('#liveballform').append('<input name="testCase" type="hidden" value="N">');
		} 
		
		if(document.referrer){
			if(jQuery('#referringUrl').length == 0){
				jQuery('#liveballform').append('<input name="referringUrl" id="referringUrl" type="hidden">');
			}
			jQuery('#referringUrl').val(document.referrer);
		}

		if(document.location){
			if(jQuery('#locationUrl').length == 0){ 
				jQuery('#liveballform').append('<input name="locationUrl" id="locationUrl" type="hidden">');
			}
			jQuery('#locationUrl').val(document.location);
		}
		
		jQuery('#liveballform .pf_text').each(function(){
			var grab = jQuery(this).next('.pf_hint_text').text();
			jQuery(this).val(grab);
			jQuery(this).next('.pf_hint_text').remove();
			
			jQuery(this).bind('focus', function(){
				textClear( this, grab );
			});
			
			jQuery(this).bind('blur', function(){
				textRefillDyn( this, grab );
			});
		})
		
		jQuery('#liveballform').validate({
			errorClass: "invalid",
			focusCleanup: true,
			submitHandler: function(form) {
				jQuery('#__VIEWSTATE').attr('name','VIEWSTATE');
	   			PostFormDyn(form);
	   			jQuery('.attnarrow').hide();
				jQuery('#liveballform').hide();
	   			jQuery('#formcontainer').append('<h2 style="padding:50px 0; text-align:center;">Please Wait...</h2>');
	 		}
		});
		
		jQuery('#liveballform hr.pf_hr').each(function(index){
			jQuery(this).parent().parent().addClass('dynstep divider');
		});
		stepnum = 0;
		jQuery('#liveballform tr').each(function(){
			if (jQuery(this).hasClass('divider')){
				stepnum++;
				jQuery(this).remove();
			}
			jQuery(this).addClass('dynstep stepbox-'+stepnum);
			if (stepnum != 0) {
				jQuery(this).hide();
			}
		});
		/*
		jQuery('select[name="programofinterest"]').bind('change', function(){
			for(e=0;e<=stepnum;e++){
				if(jQuery(this).parent().parent().hasClass('stepbox-'+e)){
					jQuery('.stepbox-'+e).fadeOut(1000);
					jQuery('.stepbox-'+(e+1)).delay(1000).fadeIn();
				}
			}
		});
		*/
	} else {
		jQuery('.form_block').validate({
			errorClass: "invalid",
			focusCleanup: true,
			submitHandler: function(form) {
				if (jQuery('#state').val()!="" && jQuery('#zipcode').val()!="Your ZIP Code..." ) {
	   				PostForm();
	   			}
	 		}
		});
	}
	
	if(getUrlVars()["locationCode"]){jQuery('#locCode').val(getUrlVars()["locationCode"])}
	if(getUrlVars()["ven"]){jQuery('#vendCode').val(getUrlVars()["ven"])}
	if(getUrlVars()["src"]){jQuery('#srcCode').val(getUrlVars()["src"])}
	
});

function dynValid(stepindex){

	var test = true;
	jQuery('.stepbox-'+stepindex).find('input').each(function(){
	
		jQuery(this).focus();
		
		if (jQuery('#liveballform').validate().element( jQuery(this) ) && jQuery( this ).val()!="-1" && test){
			test = true;	
		} else {
			test = false;
		}
		
	});
	jQuery('.stepbox-'+stepindex).find('select').each(function(){
	
		if( (window['console'] !== undefined) ){
			console.log( "found selects" );
    	}
	
		if (jQuery('#liveballform').validate().element( jQuery(this) ) && jQuery( this ).val()!="-1" && test){
			test = true;	
		} else {
			test = false;
		}
		
		if( (window['console'] !== undefined) ){
			console.log( test );
    	}
		
	});
	if (test) {
		jQuery('.stepbox-'+stepindex).fadeOut(500);
		jQuery('.stepbox-'+(stepindex+1)).delay(500).fadeIn();
	}
}

function PostForm(){
	googleClick();
	jQuery.ajax({
		type: "GET",
		url: "/Javascript/proxy.aspx",
		data: 'u=http%3A%2F%2Fapps.doublepositive.com%2FSuppliers%2FApps%2FRealTimeLeadsEDMC.aspx%3F' + encodeURIComponent(jQuery('#mainform').serialize()),
		complete: function(msg){
			jQuery('a.thankyouredirect').click();
		}
	});
}

function PostFormDyn(){
	googleClick();
	jQuery.ajax({
		type: "GET",
		url: "/Javascript/proxy.aspx",
		data: 'u=http%3A%2F%2Fapps.doublepositive.com%2FSuppliers%2FApps%2FRealTimeLeadsEDMC.aspx%3F' + encodeURIComponent(jQuery('#liveballform').serialize()),
		complete: function(msg){
			jQuery('a.thankyouredirect').click();
			console.log( 'u=http%3A%2F%2Fapps.doublepositive.com%2FSuppliers%2FApps%2FRealTimeLeadsEDMC.aspx%3F' + encodeURIComponent(jQuery('#liveballform').serialize()));
		}
	});
}

function googleClick() {
  // Get the "click_url" argument.
  var regex = new RegExp("[\\?&]click_url=([^&#]*)");
  var tmpURL = window.location.href;
  var clickURL = regex.exec(tmpURL);
  if (clickURL == null) {
    return false;
  }
  clickURL = unescape(clickURL[1]);
  // Add the cpl_submit label for Ad Click Server.
  if (clickURL.indexOf("&adurl=") == -1) {
    return false;
  }
  clickURL = clickURL.replace(
      "&adurl=", "&label=cpl_submit&adurl=");
  // Get and XMLHttpRequest object based on different browser types.
  var xhr = false;
  if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
  } else if (window.ActiveXObject) jQuery
  if (!xhr) {
    return false;
  }
  // Register the request handler callback.
  xhr.onreadystatechange = function() {
    // Do nothing.
  }
  // Finally send the click to Google.
  xhr.open("GET", clickURL);
  try {
    xhr.send();
  } catch (e) {
    // Should be okay here. We expect nothing to be returned from Google.
  }
  return true;
}

function textClear( elem, deftext ) {
	if ( jQuery(elem).val()==deftext ) {
		jQuery(elem).val('');
	}
}

function textRefill( elem, deftext ) {
	jQuery('#mainform').validate().element( elem );
	if ( jQuery(elem).val()=='' ) {
		jQuery(elem).val(deftext);
	} else {
		jQuery(elem).css('color','black').css('font-style','normal');
	}
}

function textRefillDyn( elem, deftext ) {
	jQuery('#liveballform').validate().element( elem );
	if ( jQuery(elem).val()=='' ) {
		jQuery(elem).val(deftext);
	} else {
		jQuery(elem).css('color','black').css('font-style','normal');
	}
}

function validElemOne( elem ) {
	if (jQuery('#mainform').validate().element( elem ) && jQuery( elem ).val()!="-1" ){
		
		jQuery('.stepbox.one').hide();
		jQuery('.stepbox.two').show();
		
		if (jQuery('.hook').length) {
			jQuery('.hook').animate({
				opacity: 0.0,
				height: '8px'
			}, 1000, function(){
				$(this).css('color','white');
			});
		}
		
		jQuery('#formarea .progress.bar .filler div').animate({
			width: '25%'
		});
		jQuery('#formarea .progress.bar #Progress').text('25% complete');
		jQuery('.attnarrow').show();
	}
}

function validElemTwo(){
	if ( jQuery('#nameFirst').val()!="First Name..." && jQuery('#nameLast').val()!="Last Name..." ) {
		if ( jQuery('#mainform').validate().element( jQuery('#nameFirst') ) && jQuery('#mainform').validate().element( jQuery('#nameLast') ) ) {
		
			jQuery('.stepbox.two').hide();
			jQuery('.stepbox.three').show();
			jQuery('#formarea .progress.bar .filler div').animate({
				width: '50%'
			});
			jQuery('#formarea .progress.bar #Progress').text('50% complete');
		}
	}
}

function validElemThree(){
	if ( jQuery('#emailAddr').val()!="Your Email Address..." && jQuery('#homePhone').val()!="Your Phone Number..." ) {
		if ( jQuery('#mainform').validate().element( jQuery('#emailAddr') ) && jQuery('#mainform').validate().element( jQuery('#homePhone') ) ) {
		
			jQuery('.stepbox.three').hide();
			jQuery('.stepbox.four').show();
			jQuery('#formarea .progress.bar .filler div').animate({
				width: '75%'
			});
			jQuery('#formarea .progress.bar #Progress').text('75% complete');
		}
	}
}

function validElemFour(){
	if ( jQuery('#state').val()!="" && jQuery('#zipcode').val()!="Your ZIP Code..." ) {
		if ( jQuery('#mainform').validate().element( jQuery('#state') ) && jQuery('#mainform').validate().element( jQuery('#zipcode') ) ) {
			
			jQuery('#mainform').submit();
						
			jQuery('#formarea .progress.bar .filler div').animate({
				width: '100%'
			});
			
			jQuery('#formcontainer').html('<h2 style="padding:50px 0; text-align:center;">Please Wait...</h2>');
			
			jQuery('#formarea .progress.bar #Progress').text('100% complete');
			jQuery('.attnarrow').hide();
		}
	}
}

function bounceArrow() {
	jQuery('.attnarrow').animate({
		marginTop: '-10'
	}, 500, 'easeOutCirc', function(){
		jQuery('.attnarrow').animate({
			marginTop: '10'
		}, 500, 'easeInCirc', function(){
			bounceArrow();
		});
	});
}

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
