﻿// JavaScript Document
var defaultEmail = "your email";
var defaultName = "first name";
var defaultLname = "last name";
var regEmail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var currentStep = 1;
var testing = false;
var sendTracking = true;

$(document).ready(function() {
	// Initial profile	
    $("#bikerProfileImage").fadeIn();
	$("#bikerProfileIcon").addClass("active");

	// Initial INPUT values
	$("#email").val(defaultEmail);
	$("#name").val(defaultName);
	$("#lname").val(defaultLname);
	trackStep("email");
	
	// Profile selection
	$("#bikerProfileIcon").click(function() {
		$(".profileImage").fadeOut();
		$("#bikerProfileImage").fadeIn();
		$(".profileIcon").removeClass("active");
		$("#bikerProfileIcon").addClass("active");
	});
	$("#marketProfileIcon").click(function() {
		$(".profileImage").fadeOut();
		$("#marketProfileImage").fadeIn();
		$(".profileIcon").removeClass("active");
		$("#marketProfileIcon").addClass("active");
	});
	$("#bartProfileIcon").click(function() {
		$(".profileImage").fadeOut();
		$("#bartProfileImage").fadeIn();
		$(".profileIcon").removeClass("active");
		$("#bartProfileIcon").addClass("active");
	});
	$("#beachProfileIcon").click(function() {
		$(".profileImage").fadeOut();
		$("#beachProfileImage").fadeIn();
		$(".profileIcon").removeClass("active");
		$("#beachProfileIcon").addClass("active");
	});
	$("#cafeProfileIcon").click(function() {
		$(".profileImage").fadeOut();
		$("#cafeProfileImage").fadeIn();
		$(".profileIcon").removeClass("active");
		$("#cafeProfileIcon").addClass("active");
	});

	$(".cbDiv").click(function() {
		$(this).siblings(".cbDiv").removeClass("selected");
		$(this).addClass("selected");
	});
	
	$(".dataStepButton").click(function() {
		gotoStep($(this).attr("rel"));
	});
	
	$(".dataStepButton").hover(
	  function() {
		  if($(this).attr("rel") < currentStep)
		    $(this).addClass("hover");
	  },
	  function() {
		  $(this).removeClass("hover");
	  });
	
	// Next buttons and validation
	// Hover state
	$(".nextButton").hover(
	  function() {
		  $(this).parent().css("background-image","url(../images/nextOverlay.png)");
	  },
	  function() {
		  $(this).parent().css("background-image","none");
	  });
	$(".submitButton").hover(
	  function() {
		  $(this).parent().css("background-image","url(../images/submitOverlay.png)");
	  },
	  function() {
		  $(this).parent().css("background-image","none");
	  });
	hoverImage( "#socialFBimage",       "images/socialFB.png",  "images/socialFB_over.png" );
	hoverImage( "#socialTWimage",       "images/socialTW.png",  "images/socialTW_over.png" );
	hoverImage( "#socialYTimage",       "images/socialYT.png",  "images/socialYT_over.png" );
	hoverImage( "#whyGGUlinkImage",     "images/whyGGUlink.jpg",     "images/whyGGUlink_over.jpg" );
	hoverImage( "#programGGUlinkImage", "images/programGGUlink.jpg", "images/programGGUlink_over.jpg" );
	hoverImage( "#classesGGUlinkImage", "images/classesGGUlink.jpg", "images/classesGGUlink_over.jpg" );
	  
	  
	$("#nextButton1").click(function() {
		if(regEmail.test($('#email').val())){
			$("#emailField").text($('#email').val());
			trackStep("name");
			currentStep = 2;
			$(".formError").hide();
		    $("#dataStep1").fadeOut();
		    $("#dataStep2").fadeIn();			
		} else {
			$(".formError").show();
		}
	});
	$("#email").focus(function() {
		if($(this).val() == defaultEmail)
		  $(this).val("");
	});

	$("#nextButton2").click(function() {
		if( $("#name").val() != defaultName && $.trim($("#name").val()) != "" &&  $("#lname").val() != defaultLname && $.trim($("#lname").val()) != "" ){
			$("#nameField").text($('#name').val());
			$("#lnameField").text($('#lname').val());
			trackStep("interest");
			currentStep = 3;
			$(".formError").hide();
		    $("#dataStep2").fadeOut();
		    $("#dataStep3").fadeIn();			
		} else {
			$(".formError").show();
		}
	});
	$("#name").focus(function() {
		if($(this).val() == defaultName)
		  $(this).val("");
	});

	$("#lname").focus(function() {
		if($(this).val() == defaultLname)
		  $(this).val("");
	});

	$("#nextButton3").click(function() {
		iVal = $("#dataStep3 .cbDiv.selected").attr("id");
		//iVal = $('input[name=cbInterest]:checked').val();
		if( iVal == "Doctorate" || iVal == "Masters" || iVal == "Bachelors" ) {
			$("#interestField").text(iVal);
			trackStep("program");
			currentStep = 4;
			$(".formError").hide();
			$("#dataStep3").fadeOut();
			$("#dataStep4").fadeIn();
		} else {
			$(".formError").show();
		}
	});

	$("#nextButton4").click(function() {
		$(".formError").show();
		//$("#dataStep4").fadeOut();
		//$("#dataStep5").fadeIn();
	});
	$(".popupItem").click(function() {
		//alert($(this).text());
		$("#programField").text($(this).text());
		trackStep("visa");
		currentStep = 5;
		$(this).parent().hide();
		$(".formError").hide();		
		$("#dataStep4").fadeOut();
		$("#dataStep5").fadeIn();
	});
	
	$("#programButton").click(function() {
		if(iVal == "Masters") {
		  $("#gradPopup").slideToggle("slow");
		}
		if(iVal == "Doctorate") {
		  $("#docPopup").slideToggle("slow");
		}
		if(iVal == "Bachelors") {
		  $("#ugPopup").slideToggle("slow");
		}
	});

	$("#nextButton5").click(function() {
		vVal = $("#dataStep5 .cbDiv.selected").attr("id");
		//vVal = $('input[name=cbVisa]:checked').val();
		if( vVal == "Yes" || vVal == "No" ) {
			$("#visaField").text(vVal);
			trackStep("review");
			currentStep = 6;
			$(".formError").hide();
			$("#dataStep5").fadeOut();
			$("#dataStep6").fadeIn();
		} else {
			$(".formError").show();
		}
	});

	$("#nextButton6").click(function() {
		postInquiry();
		trackStep("thanks");
		currentStep = 7;
		$(".formError").hide();
		$("#dataStep6").fadeOut();
		$("#dataStep7").fadeIn();
	});



  $(window).resize(function() {
    centerSite();
  });
  centerSite();

});

centerSite();

function gotoStep(num) {
	if( num < currentStep ) {
      $(".dataStep").fadeOut();
	  $("#dataStep" + num).fadeIn();
	}
	return(false);
}

function hoverImage(id, image, imageOver) {
	$(id).hover(
	  function() {
		  $(this).attr("src", imageOver);
	  },
	  function() {
		  $(this).attr("src", image);
	  });
}

function centerSite() {
		var w = $(window).width();
		var v = Math.min(Math.floor(w - 2500), 0);
		var ml;

		ml = (w < 1100) ? -700 + "px" : (v/2) + "px";
        // console.log(w + " " + v + " " + ml );
		$("body").css("margin-left", ml);
}

function trackStep(step) {
	switch(step) {
		case "email":
		  id = 110932;
		  break;

		case "name":
		  id = 110927;
		  break;

		case "interest":
		  id = 110928;
		  break;

		case "program":
		  id = 110929;
		  break;

		case "visa":
		  id = 110930;
		  break;

		case "review":
		  id = 110931;
		  break;

		case "thanks":
		  id = 110933;
		  break;
	}

	var ebRand = Math.random() + '';
	ebRand = ebRand * 1000000;
	var query = "http://bs.serving-sys.com/BurstingPipe/ActivityServer.bs?cn=as&ActivityID=" + id + "&rnd='" + ebRand + "'";
	sendMessage(query);
}

function postInquiry() {
	var fname = $("#nameField").text();
	var lname = $("#lnameField").text();
	var email = $("#emailField").text();
	var interest = $("#interestField").text();
	var program = $("#programField").text();
	var visa = $("#visaField").text();
	var channel = "C-BANNER";
	var urlVars = getUrlVars();
	var source = urlVars['source'];
	
	var postUrl = "http://www.ggu.edu/forms/ggu_inquiry";
	if(testing) {
		postUrl += "_uat";
	}
	postUrl += "/request_information.do?";
	
	postUrl += "firstName=" + fname;
	postUrl += "&lastName=" + lname;
	postUrl += "&email=" + email;
	postUrl += "&levelOfInterest=" + interest;
	postUrl += "&programOfInterest=" + program;
	postUrl += "&internationalStudent=" + visa;
	postUrl += "&channel=" + channel;
	
	if( source === undefined )
	  source = "SHINE";
	  
	postUrl += "&source=" + source;
	
	// Additional default values
	postUrl += "&internationalEducation=NA&educationLevel=NA&attendTimeframe=NA&gguSite=NA&stage=New&contactType=Regular%20Contact&currentStatus=Not%20Attempted&nextAction=1st%20Call/Email";
	
	trackSuccess();
	
	sendMessage(postUrl);	 
	
	$.get("http://www.bannermountain.com/post.php", {"data": postUrl}, function(data){}, "jsonp");	
}

function trackSuccess()
{
	// Yahoo conversion pixel - Advertiser 'Mortar Agency',  Conversion tracking 'GGU - NEW conversion pixel'
	var conv = "http://ads.bluelithium.com/pixel?id=1395409&t=2";
	sendMessage(conv);
	
	// Fetchback opt-out pixel
	var conv2 = "https://pixel.fetchback.com/serve/fb/pdj?cat=&name=success&sid=788";
	sendMessage(conv2);
	
	// Google code for SHINE Thank you confirmation Conversion Page
	document.getElementById("goog").src = "google.html";
	
}

function sendMessage(str) {
	if(sendTracking == false) {
	    return;
	}
	  
	$.ajax({
    	dataType: "jsonp",
        url: str,
        jsonpCallback: "complete",
        success: complete
    });
	function complete(result) {
	}
}

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;
}
