

//Global variable declaration for Ajax handler for CapellaTracker.aspx call
var xmlHttp; //XML Object that will be used for the AJAX call


/*****************************************************************
   Function to make an AJAX call to the CapellaTracker.aspx.
******************************************************************/
function processCapellaTrackerAjaxCall()
{ 
   xmlHttp = getXmlHttpObject();  
	  
   if(xmlHttp == null) {
      //alert ("Your browser does not support AJAX!");
      return;
   } 
	   
   xmlHttp.open("GET", "/CapellaTracking/CapellaTracking.aspx?fact=REQUEST_COURSE_INFO&factValue=TRUE",true); //open connection for Ajax call
   //xmlHttp.onreadystatechange = stateChanged; //Assign a reference to the function that will be notified of state changes (not necessary in this case because just performing a fact capture.
   xmlHttp.send(null); //send the call
}


/*****************************************************************
   Function to be notified of state changes from the Ajax call.
******************************************************************/
function stateChanged()
{   
   //If process has completed (ie, 4), then update text with returned response
   if(xmlHttp.readyState == 4) { 
      //For this instance we will not be doing anything. Simply wanted to capture the fact and that has been done.
   }
}


/*****************************************************************
   Function to determine browser type and return the appropriate 
   XML HTTP Object type.
******************************************************************/
function getXmlHttpObject()
{
   var xmlHttp = null;
	  
   try {
      // Firefox, Opera 8.0+, Safari
      xmlHttp = new XMLHttpRequest();
   }
   catch(e) {
      // Internet Explorer
      try {
         xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch(e) {
         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
   }
	  
   return xmlHttp;
}















/*****************************************************************
   Existing functions for Requesting Course Info.
******************************************************************/

// to get around postback not working with our CMS setup include these handlers with each control that needs
// to communicate asynchronously.

// The method that is called when the RequestCourseInfo control is in it's 'stage1'
function RequestCourseInfoSubmit_Stage1(){
    if(validateStage1() == true){
    
        var firstName = getRCIFirstNameElement().value;
        var lastName = getRCILastNameElement().value;
        var userEmail = getRCIEmailElement().value;
        
        var currentTime = new Date()
        var month = currentTime.getMonth() + 1
        var day = currentTime.getDate()
        var year = currentTime.getFullYear()
        var signupDate = year + "/" + month + "/" + day;
        
        var source = document.location.pathname;
        var requestNewsLetter = $("chkRequestCourseInfoNewsLetter").checked;

			//WT Tracking
			if (requestNewsLetter){
				dcsMultiTrack('DCS.dcsuri','/newsletter_signup.htm','WT.ti','Newsletter Signup link');//wt
				var s=s_gi(s_account); s.tl(this,'o','VC:Newsletter Signup form submit');//omni
			}
			dcsMultiTrack('DCS.dcsuri','/more_course_info.htm','WT.ti','Request More Course Info link');//wt
			var s=s_gi(s_account); s.tl(this,'o','VC:Get Course Descriptions form submit');//omni
			//Memetrics tracking
			processCapellaTrackerAjaxCall();

        
        // Instantiate the WebRequest object.
        var wRequest =  new Sys.Net.WebRequest();
        // Set the request Url.  
        wRequest.set_url("/Handler/RequestCourseInfoHandler.ashx"); 
         
        // Set the request verb.
        wRequest.set_httpVerb("POST");
        
        var body = ""; 
        body += "RequestCourseInfoStage=1&";
        body += "firstName=" + firstName + "&";
        body += "lastName=" + lastName + "&";
        body += "userEmail=" + userEmail + "&";
        body += "requestNewsLetter=" + requestNewsLetter + "&";
        body += "source=" + source + "&";
        body += "signupDate=" + signupDate + "&";
        body += "path=" + document.location.pathname + "&";
        
        wRequest.set_body(body);
        wRequest.get_headers()["Content-Length"] = body.length;
        
        // Set the web request completed event handler,
        // for processing return data.
        wRequest.add_completed(OnRequestCourseInfoWebRequestCompleted);
           
        // Execute the request.
        wRequest.invoke();
    }
}

// This the handler for the Web request completed event
// that is used to display return data.
function OnRequestCourseInfoWebRequestCompleted(executor, eventArgs) 
{
    if(executor.get_responseAvailable()) 
    {
        GetStage1Div().style.display = "none";
        GetStage2Div().style.display = "block";
    }
    else
    {
        if (executor.get_timedOut())
            alert("Timed Out");
        else
            if (executor.get_aborted())
                alert("Aborted");
    }
}
    
// do client side validations, if passes validation return true, else return false
function validateStage1()
{
	var noErrors = true;
	
	// first name must have value
	if (getRCIFirstNameElement().value == null || getRCIFirstNameElement().value.length == 0) {
		$("txtFirstNameValidatorMsg").style.display = "block";
		noErrors = false;
	} else {
		$("txtFirstNameValidatorMsg").style.display = "none";
	}
	
	// last name must have value
	if (getRCILastNameElement().value == null || getRCILastNameElement().value.length == 0) {
		$("txtLastNameValidatorMsg").style.display = "block";
		noErrors = false;
	} else {
		$("txtLastNameValidatorMsg").style.display = "none";
	}
	
	// email must have value
	if (getRCIEmailElement().value == null || getRCIEmailElement().value.length == 0) {
		$("txtEmailValidatorMsg").style.display = "block";
		$("txtEmailValidatorMsg_InvalidFormat").style.display = "none";
		noErrors = false;
	} else {
		$("txtEmailValidatorMsg").style.display = "none";
		
		//var reg = new RegExp("\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"); // not validating, not sure why
		//var reg = new RegExp("^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$");				// not validating, not sure why
		var reg = new RegExp("^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$");
		
		// email has value, check format
		if(getRCIEmailElement().value.match(reg)) {
			$("txtEmailValidatorMsg_InvalidFormat").style.display = "none";
			
		} else {
			$("txtEmailValidatorMsg_InvalidFormat").style.display = "block";
			noErrors = false;
		}
	}
	return noErrors;
}