// AJAX : to retrieve form data

function getXMLHttp() {
	var xmlHttp
	try {
		//Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch(e) {
		//Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e) {
				alert("Your browser does not support AJAX!")
				return false;
			}
		}
	}
	return xmlHttp;
}

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars() { 
	var map = {}; 
	var parts = window.location.search.replace(/[?&]+([^=&]+)(=[^&]*)?/gi, 
					function(m,key,value) { map[key] = (value === undefined) ? true : value.substring(1); 
					}); 
	return map; 
}

// ************************  RETREIVE CALENDAR ENTRIES ***********************************
// ***************************************************************************************
function RetrieveCalendarReq(xDateSelected, xSponsorType, byCategory, xBrowser)
{
  document.getElementById('calendarDiv').innerHTML = "<img src='images/ajax-loader.gif' class='imgLoader' />";

  var xmlHttp = getXMLHttp();
  
  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4) {
      DisplayCalendarEntries(xmlHttp.responseText);
    }
  }
  
  /* user selected list all categories */
  if(byCategory == "S0") { xDateSelected = "ALL"; }
	  
  xmlHttp.open("GET", "sql/retrieve-cal-entries-sql.php?DateSelected=" + xDateSelected + "&Sponsor=" + xSponsorType + "&Category=" + byCategory, true); 
  xmlHttp.send(null);
}

// *************************  CALENDAR RESPONSE HANDLER **********************************
// ***************************************************************************************
function DisplayCalendarEntries(xHTMLreturned) {
  document.getElementById('calendarDiv').innerHTML = xHTMLreturned;
}

// ***************************  REGISTER ONLINE FORM *************************************
// ***************************************************************************************
function RegisterOnline(classSelected, classTitle, classDate, firstName, lastName, zipCode, emailAdd, errorMsg)
{
  document.getElementById('calendarDiv').innerHTML = "<img src='images/ajax-loader.gif' class='imgLoader' />";
  var xmlHttp = getXMLHttp();
  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      LoadRegistrationForm(xmlHttp.responseText, firstName, lastName, zipCode, emailAdd);
    }
  }
  xmlHttp.open("GET", "sql/class-register-sql.php?ClassID=" + classSelected + "&ClassTitle=" + classTitle + "&ClassDate=" + classDate + "&errorMsg=" + errorMsg, true); 
  xmlHttp.send(null);
}

// ****************************  AUTO REGISTER PROCESS ***********************************
// ***************************************************************************************
function AutoRegister(contactID, classSelected)
{
  document.getElementById('calendarDiv').innerHTML = "<img src='images/ajax-loader.gif' class='imgLoader' />";
  var xmlHttp = getXMLHttp();
  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      HandleResponse(xmlHttp.responseText);
    }
  }
  xmlHttp.open("GET", "sql/register-contact-sql.php?ClassID=" + classSelected + "&ContactID=" + contactID, true); 
  xmlHttp.send(null);
}

// *************************  GENERIC RESPONSE HANDLER ***********************************
// ***************************************************************************************
function HandleResponse(xResponse) {
  document.getElementById('calendarDiv').innerHTML = xResponse;
}

// **************************  LOAD REGISTRATION FORM ************************************
// ***************************************************************************************
function LoadRegistrationForm(response, firstName, lastName, zipCode, emailAdd) {
	
 	document.getElementById('calendarDiv').innerHTML = response;

	$('#BusinessTypeID').jselect({
		replaceAll: true,   
		loadType: "GET",
		loadDataType: "html",
		loadOnError: function() { alert("Business Types did not load correctly"); },
		addOption: false,
		loadUrl: "sql/load-business-types-html.php"
	});

	$('#MarketCategoryID').jselect({
		replaceAll: true,   
		loadType: "GET",
		loadDataType: "html",
		loadOnError: function() { alert("Market Categories did not load correctly"); },
		addOption: false,
		loadUrl: "sql/load-mkt-catgs-html.php"
	});

	$('#BusinessStage').jselect({
		replaceAll: true,   
		loadUrl: "xml/business-stages.xml", 
		loadType: "GET",
		addOption: false
	});

// preset the following fields
	$("input[name='FirstName']").val(firstName);
	$("input[name='LastName']").val(lastName);
	$("input[name='MailZipCode']").val(zipCode);
	$("input[name='EmailAddress']").val(emailAdd);
}

// ********************  LOAD SUB-MARKET CATEGORY : DROPDOWN *****************************
// ***************************************************************************************
function LoadSubMarkets(Upline) {

	$('#SubMarketCategoryID').jselect({
		replaceAll: true,   
		loadType: "GET",
		loadDataType: "html",
		loadOnError: function() { alert("Market Categories did not load correctly"); },
		addOption: false,
		loadUrl: "sql/load-mkt-sub-catgs-ajax.php?UplineCategory=" + Upline
	});

}

// ***************************  REGISTER PROMPT FORM *************************************
// ***************************************************************************************
function RegisterPrompt(classSelected, classTitle, classDate) {

  var xmlHttp = getXMLHttp();
  
  xmlHttp.onreadystatechange = function() 
  {
    if(xmlHttp.readyState == 4) {
      HandleResponse(xmlHttp.responseText);
    }
  }
  xmlHttp.open("GET", "php/register-prompt.php?ClassID=" + classSelected + "&ClassTitle=" + classTitle + "&ClassDate=" + classDate, true); 
  xmlHttp.send(null);
}

// ***********************  OFFSITE REGISTRATION (CVENT) *********************************
// ***************************************************************************************
function OffSiteRegistration(urlAddress) {
	window.location = urlAddress;
}

// ***********************  REGISTRANT VERIFICATION FORM *********************************
// ***************************************************************************************
function VerifyContact(classSelected, classTitle, classDate) {

	var fName = $("#FirstName").val();
	var lName = $("#LastName").val();
	var zipCode = $("#ZipCode").val();
	var emailAdd = $("#EmailAddress").val();

    document.getElementById('calendarDiv').innerHTML = "<img src='images/ajax-loader.gif' class='imgLoader' />";

	$.ajax({
			type: "GET",
			url:  "xml/select-contact-xml.php?Fname=" + fName + "&Lname=" + lName + "&ZipCode="+ zipCode + "&Email=" + emailAdd,
			dataType: "xml",
			success: function(xml) {
				$(xml).find('FoundContact').each(function() {
					var errorMsg = $(this).find('ErrorMessage').text();

					if(errorMsg != "NotFound") {
						var contactID = $(this).find('ContactID').text();
						var firstName = $(this).find('FirstName').text();
						var lastName = $(this).find('LastName').text();
						var mainPhone = $(this).find('MainPhone').text();
						var emailAddress = $(this).find('EmailAddress').text();
						var mailAddress1 = $(this).find('MailAddress1').text();
						var mailAddress2 = $(this).find('MailAddress2').text();
						var mailCity = $(this).find('MailCity').text();
						var mailState = $(this).find('MailState').text();
						var mailZipCode = $(this).find('MailZipCode').text();

						$("input[name='ContactID']").val(contactID);
						$("input[name='MainPhone']").val(mainPhone);
						$("input[name='MailAddress1']").val(mailAddress1);
						$("input[name='MailAddress2']").val(mailAddress2);
						$("input[name='MailCity']").val(mailCity);
						$("input[name='MailState']").val(mailState);

						var newHTML = "<p>The following information was found based on the information you entered. If this is not you then click the 'This Is Not Me' button to complete a new registration. If the information is yours but it is no longer current then call the Small Business Development Program at 512.974.7800 to have it updated. You may still continue with this registration.</p> \n";
						newHTML = newHTML + "<table class='table1'>";
						newHTML = newHTML + "<tr><td>Name:</td><td>" + firstName + " " + lastName + "</td><tr> \n";
						newHTML = newHTML + "<tr><td>Address:</td><td>" + mailAddress1 + " " + mailAddress2 + "</td><tr> \n";
						newHTML = newHTML + "<tr><td>&nbsp;</td><td>" + mailCity + ", " + mailState + "&nbsp;&nbsp;" + mailZipCode + "</td><tr> \n";
						newHTML = newHTML + "<tr><td>Phone:</td><td>" + mainPhone + "</td><tr> \n";
						newHTML = newHTML + "<tr><td>Email:</td><td>" + emailAddress + "</td><tr> \n";
						newHTML = newHTML + "<tr><td>&nbsp;</td><tr> \n";
						newHTML = newHTML + "</table> \n";
						newHTML = newHTML + "<p><input type='button' name='submit' id='submit' value='This Is Not Me' onclick='RegisterOnline(" + classSelected + ",\"" + classTitle + "\",\"" + classDate + "\",\"" + firstName + "\",\"" + lastName + "\",\"" + zipCode + "\",\"" + emailAdd + "\");' class='buttonsLong'>&nbsp;<input type='button' name='submit' id='submit' value='Register Me' onclick='AutoRegister(" + contactID + "," + classSelected + ")' class='buttonsLong'></p> \n";

						HandleResponse(newHTML);
					} else {
						RegisterOnline(classSelected, classTitle, classDate, firstName, lastName, zipCode, emailAdd, "NotFound");
					}					
				});
			}
	});
}

