
// JavaScript Document
// initialize variables
var xmlHttp;
var firstSelect;
var secondSelect;
var theForm;

// create Ajax request variable based on browser type
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}

// set all variables
}
function initVars() {
// *** change the values in quotes to reflect the IDs of your select inputs *** //
firstSelect = document.getElementById("firstSelect");
secondSelect = document.getElementById("secondSelect");
// *** Change the value in quotes to reflect the ID of your form *** //
theForm = document.getElementById("theForm");
}

// callback functions.  

function safStatus() {
	
	if (xmlHttp.readyState == 4) {
	
	
	
		if (xmlHttp.status == 200) {
			
			
			
			var errorDiv = document.getElementById("saf_errors");
			var theForm = document.getElementById("theForm");
			errorDiv.innerHTML = "Your message has been sent successfully!  Thank you for sharing the Brelli!";
			
			theForm.their_email.value = '';
						
			clearError(theForm.your_email);
			clearError(theForm.their_email);
			clearError(theForm.your_name);
			
		} 
	
	}
	
}


function setOptions() {
	if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200 && xmlHttp.getResponseHeader("content-type") == "text/xml") {
				var optionlist;
				optionlist = xmlHttp.responseXML.getElementsByTagName("option");
				addOption(optionlist);
				secondSelect.disabled = false;
			} 
			
		
	}
}

function completeForm () {
	if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200 && xmlHttp.getResponseHeader("content-type") == "text/xml") {
				var theForm = document.getElementById("theForm");
				if (xmlHttp.responseXML.getElementsByTagName("s_name")[0].firstChild)
					theForm.s_name.value = xmlHttp.responseXML.getElementsByTagName("s_first_name")[0].firstChild.data + " " + xmlHttp.responseXML.getElementsByTagName("s_last_name")[0].firstChild.data;
				if (xmlHttp.responseXML.getElementsByTagName("s_address1")[0].firstChild)
					theForm.s_address1.value = xmlHttp.responseXML.getElementsByTagName("s_address1")[0].firstChild.data;
				if (xmlHttp.responseXML.getElementsByTagName("s_address2")[0].firstChild)
					theForm.s_address2.value = xmlHttp.responseXML.getElementsByTagName("s_address2")[0].firstChild.data;
				if (xmlHttp.responseXML.getElementsByTagName("s_city")[0].firstChild)
					theForm.s_city.value = xmlHttp.responseXML.getElementsByTagName("s_city")[0].firstChild.data;
				if (xmlHttp.responseXML.getElementsByTagName("s_zip")[0].firstChild)
					theForm.s_zip.value = xmlHttp.responseXML.getElementsByTagName("s_zip")[0].firstChild.data;
				if (xmlHttp.responseXML.getElementsByTagName("s_phone")[0].firstChild)
					theForm.s_phone.value = xmlHttp.responseXML.getElementsByTagName("s_phone")[0].firstChild.data;
				
				if (xmlHttp.responseXML.getElementsByTagName("b_address1")[0].firstChild)	
					theForm.b_address1.value = xmlHttp.responseXML.getElementsByTagName("b_address1")[0].firstChild.data;
				if (xmlHttp.responseXML.getElementsByTagName("b_address2")[0].firstChild)
					theForm.b_address2.value = xmlHttp.responseXML.getElementsByTagName("b_address2")[0].firstChild.data;
				if (xmlHttp.responseXML.getElementsByTagName("b_city")[0].firstChild)
					theForm.b_city.value = xmlHttp.responseXML.getElementsByTagName("b_city")[0].firstChild.data;
				if (xmlHttp.responseXML.getElementsByTagName("b_zip")[0].firstChild)
					theForm.b_zip.value = xmlHttp.responseXML.getElementsByTagName("b_zip")[0].firstChild.data;
				if (xmlHttp.responseXML.getElementsByTagName("b_phone")[0].firstChild)
					theForm.b_phone.value = xmlHttp.responseXML.getElementsByTagName("b_phone")[0].firstChild.data;
				
				if (xmlHttp.responseXML.getElementsByTagName("s_state")[0].firstChild)
					updateSelect(xmlHttp.responseXML.getElementsByTagName("s_state")[0].firstChild.data, theForm.s_state);
				if (xmlHttp.responseXML.getElementsByTagName("b_state")[0].firstChild)
					updateSelect(xmlHttp.responseXML.getElementsByTagName("b_state")[0].firstChild.data, theForm.b_state);
				
			} 
			
		
	}
}


// initial JS functions, called by form data

function updateSizePulldown() {
			initVars();
			var id, selectedIndex;
			id = firstSelect.options[firstSelect.selectedIndex].value;
			createXMLHttpRequest();
			
			if (id != "") {
				var url = "/admin/includes/get_ajax_data.php?id=" +  id + "&mode=sizes";
				xmlHttp.open("GET", url, true);
				xmlHttp.onreadystatechange = setOptions;
				xmlHttp.send(null);
			}
			else {
				clearOption();
				
			}
}

function completeCustomerForm() {
	
	var id, selectedIndex;
	firstSelect = document.getElementById("firstSelect");
	id = firstSelect.options[firstSelect.selectedIndex].value;
	createXMLHttpRequest();
	if (id != "") {
		var url = "/admin/includes/get_ajax_data.php?id=" +  id + "&mode=customer";
		xmlHttp.open("GET", url, true);
		xmlHttp.onreadystatechange = completeForm;
		xmlHttp.send(null);
	}
	else {
		alert("Please select a customer.");
		
	}
		
}

function sendAFriend() {
	
	var errors = '';
	
	var errorDiv = document.getElementById('saf_errors');
	var theForm = document.getElementById('theForm');
	errorDiv.innerHTML = '';
		
	// validate form
	
	if (theForm.your_email.value == '') {
		errors = 'You must enter your email address.<br />';
		setError(theForm.your_email); 
	}
	else {
		clearError(theForm.your_email);
	}
	
	if (theForm.their_email.value == '') {
		errors = errors + 'You must enter your friend\'s email address.<br />';
		setError(theForm.their_email); 
	}
	else {
		clearError(theForm.their_email);
	}
	
	if (errors != '')	
		errorDiv.innerHTML = errors;
	else {
		createXMLHttpRequest();
		 var poststr = "name=" + encodeURI( theForm.your_name.value ) +
                    "&your_email=" + encodeURI( theForm.your_email.value ) +
                    "&their_email=" + encodeURI( theForm.their_email.value ) +
                    "&message=" + encodeURI( theForm.message.value ) +
                    "&mode=saf" ;
         var url = "/admin/includes/get_ajax_data.php";
         xmlHttp.onreadystatechange = safStatus;
         xmlHttp.open("POST", url, true);
         xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
     	 xmlHttp.setRequestHeader("Content-length", poststr.length);
      	 xmlHttp.setRequestHeader("Connection", "close");
		 xmlHttp.send(poststr);
	}
		
		
	return false;
}


// form-altering functions

function clearOption() {
	secondSelect.options.length = 0;
	secondSelect.options[0] = new Option("Please Choose:", " ", true, true);
	secondSelect.disabled = true;
	
}

function addOption(optionlist) {
	var selectToAdd, value, text;
	clearOption();
	
	
	for (var i=0;i<optionlist.length;i++) {
		value = optionlist[i].getElementsByTagName("value")[0].firstChild.data;
		text = optionlist[i].getElementsByTagName("text")[0].firstChild.data;
		secondSelect.options[i+1] = new Option(text, value, false, false);
	}
}

function updateSelect(new_value, pulldown) {
	
	for (var i=0;i<pulldown.options.length;i++) {
		if(pulldown.options[i].value == new_value)
			pulldown.options[i].selected = true;
	}
	
}

function setError(obj) {
	
	obj.style.backgroundColor = 'yellow';
	
}

function clearError(obj) {
	
	obj.style.backgroundColor = 'white';
	
}
