/*
this file contains the functions necessary for the garage email notifications - KJP

customers can send a link of a particular vehicle to someone else who can click on a link in the email body
which will bring them directly to the garage page with the specific vehicle showing

Created October 15, 2009
*/


// GLOBAL variables
var xmlHttpGarageEmail;
var responseGarageEmailSet;
var sURLGarageEmail = "http://www.thejunctioninc.com/servlet/ajaxGarageEmail";
var sendToEmail = "";
var fromName = "";
var fromWhoMessage = "";
var emailAddressError = "n";
var emailLinkError = "n";

// FUNCTIONS

/*
this function pops up a box when someone wants to send the link in an email 
*/
function showSendTo() {
	
	//make sure box isn't already showing
	
	if (document.getElementById('garageEmailPopup')==null){

		var outertable = document.createElement('table');
		outertable.setAttribute("id","garageEmailPopup");
		outertable.setAttribute("width","350");
		outertable.border = "0";
		outertable.cellSpacing="5";
		outertable.cellPadding="0";
		outertable.setAttribute("cellspacing","5");
		outertable.setAttribute("cellpadding","0");
		var outertablebody = buildSendTo();
			
		outertable.appendChild(outertablebody);
	
		document.body.appendChild(outertable);
		document.getElementById('emailAddress').focus();
	} else {
		//see if already gone to 'PROCESSING' stage
		if (document.getElementById('emailStatus')==null){
			document.getElementById('emailAddress').focus();
		} else {
		
		}
	}
}

function buildSendTo(){
		var outertablebody = document.createElement('tbody');
		outertablebody.setAttribute("id","garageEmailPopupTbody");

		//create the header row
		var titleLine = document.createElement('tr');
		var titleCell = document.createElement('td');
		titleCell.setAttribute("colspan","2");
		titleCell.colSpan = "2";
		titleCell.setAttribute("align","center");
		titleLine.appendChild(titleCell);
		
		var titleString = "Send This "+selectedYear+" "+selectedMake+" "+selectedModel+" To :";
		var titleCellText = document.createTextNode(titleString);
		titleCell.appendChild(titleCellText);
		outertablebody.appendChild(titleLine);
	
		//create the email label and email text box
		var emailLine = document.createElement('tr');
		var labelCell = document.createElement('td');
		labelCell.setAttribute("width","50%");
		//create and attach the text
		emailText = document.createTextNode('Email');
		labelCell.appendChild(emailText);
		
		var emailBoxCell = document.createElement('td');
		emailBoxCell.setAttribute("width","50%");
		emailBoxCell.setAttribute("align","right");
		//create and attach the textbox
		var inputBox = document.createElement("INPUT");
		inputBox.setAttribute("type","text");
		inputBox.setAttribute("id","emailAddress");
		inputBox.setAttribute("name","emailAddress");
		inputBox.name = "emailAddress";
		inputBox.setAttribute("maxlength","50");
		inputBox.setAttribute("size","25");
		emailBoxCell.appendChild(inputBox);	
		
		emailLine.appendChild(labelCell);
		emailLine.appendChild(emailBoxCell);
		outertablebody.appendChild(emailLine);

		//if there is an error to report
		if (emailAddressError=="y"){
			//insert the next line to display the error
			
			var errorLine = document.createElement('tr');
			var errorCell = document.createElement('td');
			errorCell.setAttribute("colspan","2");
			errorCell.colSpan = "2";
			errorCell.setAttribute("align","center");
			errorLine.appendChild(errorCell);
			
			var errorDiv = document.createElement('div');
			errorDiv.setAttribute("id","emailStatus");
			var errorString = "Invalid email address. Please re-enter.";
			var errorCellText = document.createTextNode(errorString);
			errorDiv.appendChild(errorCellText);
			errorCell.appendChild(errorDiv);
			outertablebody.appendChild(errorLine);
		
		}
	
		//create the fromWho label and fromWho text box
		var fromWhoLine = document.createElement('tr');
		var labelCell = document.createElement('td');
		//create and attach the fromWho text
		fromWhoText = document.createTextNode('Your Name');
		labelCell.appendChild(fromWhoText);
			
		var fromWhoBoxCell = document.createElement('td');
		fromWhoBoxCell.setAttribute("align","right");
		//create and attach the fromWho text box
		var inputBox = document.createElement("INPUT");
		inputBox.setAttribute("type","text");
		inputBox.setAttribute("id","fromWho");
		inputBox.setAttribute("name","fromWho");
		inputBox.name = "fromWho";
		inputBox.setAttribute("maxlength","50");
		inputBox.setAttribute("size","25");
		inputBox.value = fromName;
		fromWhoBoxCell.appendChild(inputBox);	
			
		fromWhoLine.appendChild(labelCell);
		fromWhoLine.appendChild(fromWhoBoxCell);
		outertablebody.appendChild(fromWhoLine);
	
		//create the message label
		var textareaLine = document.createElement('tr');
		var labelCell = document.createElement('td');
		messageText = document.createTextNode('Message (100)');
		labelCell.setAttribute("valign","top");
		labelCell.vAlign = "top";
		labelCell.appendChild(messageText);

		//create the message box
		var textareaCell = document.createElement('td');
		textareaLine.appendChild(textareaCell);
		var inputBox = document.createElement("textarea");
		inputBox.setAttribute("id","emailMessage");
		inputBox.setAttribute("name","emailMessage");
		inputBox.name = "emailMessage";
		inputBox.setAttribute("cols","18");
		inputBox.setAttribute("rows","3");
		inputBox.setAttribute("maxlength","100");
		inputBox.value = fromWhoMessage;
		textareaCell.appendChild(inputBox);

		textareaLine.appendChild(labelCell);
		textareaLine.appendChild(textareaCell);
		outertablebody.appendChild(textareaLine);
	
	
		//create the submit / cancel line
		var submitLine = document.createElement('tr');
		var submitCell = document.createElement('td');
		submitCell.setAttribute("align","center");
		var cancelCell = document.createElement('td');
		cancelCell.setAttribute("align","center");
		
		submitLine.appendChild(submitCell);
		submitLine.appendChild(cancelCell);
	
		submitText = document.createTextNode("Send");
		aElement = document.createElement("a");
		aElement.setAttribute("href","javascript:submitSendTo();");
		aElement.appendChild(submitText);
		submitCell.appendChild(aElement);
	
		cancelText = document.createTextNode("Cancel");
		aElement = document.createElement("a");
		aElement.setAttribute("href","javascript:cancelSendTo();");
		aElement.appendChild(cancelText);
		cancelCell.appendChild(aElement);
		outertablebody.appendChild(submitLine);
		
		return outertablebody;
}


/*
this clears the entire pop up box contents
*/
function clearSendTo() {
	//this is where we take out the email box and the sender name box and the SEND option
	
	//replace title
	
	//add status line
	
	//change the CANCEL to CLOSE
}

/*
this includes the directives to process the info
*/
function submitSendTo() {
	//get values from text boxes
	var sendTo = document.getElementById('emailAddress').value;
	var from = document.getElementById('fromWho').value; 
	var emailMessage = document.getElementById('emailMessage').value;

	sendToEmail = sendTo;
	fromName = from;
	fromWhoMessage = emailMessage;
		 
	//just make sure email box isn't empty
	if (sendTo != ""){
		var sVarsGarageEmail = "action=garageLinkEmail&user="+sess+"&category="+categoryName+"&make="+categoryMake+"&model="+categoryModel+"&year="+categoryYear+"&sendTo="+sendTo+"&fromWho="+from+"&message="+emailMessage;

		if (window.ActiveXObject){
			xmlHttpGarageEmail = new ActiveXObject("Microsoft.XMLHTTP");
		} else if (window.XMLHttpRequest){
			xmlHttpGarageEmail = new XMLHttpRequest();
		} else {
			window.alert("Your browser does not support the network connection necessary to complete this function.");	
		}

		//modify the contents of the popup box to display status
		showStatusSendTo();
		
		//send request to server		
		count = 0;
		if (!xmlHttpGarageEmail) {
		 	return null;
		} else {
			try {
				xmlHttpGarageEmail.open("POST", sURLGarageEmail, true);
				xmlHttpGarageEmail.setRequestHeader("Method", "POST "+sURLGarageEmail+" HTTP/1.1");
				xmlHttpGarageEmail.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				xmlHttpGarageEmail.onreadystatechange = function handleStateChange()	{
					if (xmlHttpGarageEmail.readyState == 4) {
						if (xmlHttpGarageEmail.status == 200) {
			
							var errors = xmlHttpGarageEmail.responseXML.getElementsByTagName("numErrors")[0].firstChild.data;
							
							if (errors == "address"){

								//do something
								emailAddressError = "y";

								document.getElementById("garageEmailPopup").removeChild(document.getElementById("garageEmailPopupTbody"));
								var outertable = document.getElementById("garageEmailPopup");

								var outertablebody = buildSendTo();
								outertable.appendChild(outertablebody);
							
								document.body.appendChild(outertable);
								document.getElementById('emailAddress').focus();
								
							} else if (errors == "link") {
								//update the status box and show CLOSE button

							} else {
								//update the status box and show CLOSE button

								showStatusClose();
							}
		
						}
					}
				};
				xmlHttpGarageEmail.send(sVarsGarageEmail);
			} catch(z) {return false;}
		}


	} else {
		alert("An email address is required");
		document.getElementById('emailAddress').focus();
	}
}


/*
this closes the popup box
*/
function cancelSendTo() {
	try {
		document.body.removeChild(document.getElementById('garageEmailPopup'));
	} catch (e) {}
	fromName = "";
	fromWhoMessage = "";
}

/*
this clears the contents and displays status info
*/
function showStatusSendTo() {

	//clear all box contents
	if (document.getElementById('garageEmailPopup')!=null){
	
		document.getElementById("garageEmailPopup").removeChild(document.getElementById("garageEmailPopupTbody"));

		var outertable = document.getElementById("garageEmailPopup");
	
		//build status layout	
		var outertablebody = buildStatus();	
		
		//attach to existing outer table
		outertable.appendChild(outertablebody);
	
	} else {
		//no box found
	
	
	}

}

function buildStatus() {

		var outertablebody = document.createElement('tbody');
		outertablebody.setAttribute("id","garageEmailPopupTbody");

		//create the header row
		var titleLine = document.createElement('tr');
		var titleCell = document.createElement('td');
		titleCell.setAttribute("colspan","2");
		titleCell.colSpan = "2";
		titleCell.setAttribute("align","center");
		titleLine.appendChild(titleCell);
		
		var titleString = "STATUS :";
		var titleCellText = document.createTextNode(titleString);
		titleCell.appendChild(titleCellText);
		outertablebody.appendChild(titleLine);

		var statusLine = document.createElement('tr');
		var statusCell = document.createElement('td');
		statusCell.setAttribute("colspan","2");
		statusCell.colSpan = "2";
		statusCell.setAttribute("align","center");
		statusLine.appendChild(statusCell);
		
		var statusDiv = document.createElement('div');
		statusDiv.setAttribute("id","emailStatus");
		var statusString = "PROCESSING ...";
		var statusCellText = document.createTextNode(statusString);
		statusDiv.appendChild(statusCellText);
		statusCell.appendChild(statusDiv);
		outertablebody.appendChild(statusLine);

		return outertablebody;
}

function showStatusClose() {
	//update the 'emailStatus' id
	if (emailLinkError == "n"){
		document.getElementById("emailStatus").innerHTML = "SUCCESS!!";
	} else {
		document.getElementById("emailStatus").innerHTML = "FAILURE: Our administator has been notified. Please accept our apologies.";
	}
	
	if (document.getElementById('garageEmailPopup')!=null){
	
		var outertable = document.getElementById("garageEmailPopupTbody");
	
		//build status layout	
		var tablebodyrow = buildStatusClose();	
		
		//attach to existing outer table
		outertable.appendChild(tablebodyrow);
	
	} else {
		//no box found
	
	
	}

}

function buildStatusClose() {

	var closeLine = document.createElement('tr');
	var closeCell = document.createElement('td');
	closeCell.setAttribute("colspan","2");
	closeCell.colSpan = "2";
	closeCell.setAttribute("align","center");
	closeLine.appendChild(closeCell);

	closeText = document.createTextNode("Close");
	aElement = document.createElement("a");
	aElement.setAttribute("href","javascript:cancelSendTo();");
	aElement.appendChild(closeText);
	closeCell.appendChild(aElement);
	closeLine.appendChild(closeCell)
	
	return closeLine;
	
}


