/**
*
*	This method is called when the user submits the contact us form
*/

var originalSessionId;

function submitPressed(number)
{

	var name = document.getElementsByName('name')[number].value;

	var visitorEmail = document.getElementsByName('visitorEmail')[number].value;

	var comments = document.getElementsByName('comments')[number].value;
	
	var departmentSelected = null;
	
	if( document.getElementsByName('department')[number] != null)
	{
		var departments = document.getElementsByName('department')[number];
		for(var i=0; i<departments.options.length; i++)
		{
			if(departments.options[i].selected == true)
			{
				departmentSelected = departments.options[i].value;
			}
		}
	}
	
	var pageTitle = null;
	var pageUrl = null;
	
	
	if(document.getElementsByName('pageTitle')[number] != null)
	{
		pageTitle = document.getElementsByName('pageTitle')[number].value;
	}
	
	
	if(document.getElementsByName('pageUrl')[number] != null)
	{
		pageUrl = document.getElementsByName('pageUrl')[number].value;
	}
	
	if(document.getElementsByName('imageText')[number] != null)
	{
		imageText = document.getElementsByName('imageText')[number].value;
	}

	originalSessionId = document.getElementById("sessionId").value;	
	submitForm(name, visitorEmail, comments, departmentSelected, pageTitle, pageUrl, imageText);
}

/**
*
*	This method will construct a url to send a request to
*   retrieve the form submission message.  The method will construct a
*   XMLHttpRequest object to send the request without having
*   to refresh the form (ajax technology).
*   The method associates the function (callbackContactUs) which 
*   will be called when a response is received.
*/
function submitForm(name, visitorEmail, comments, departmentSelected, pageTitle, pageUrl, imageText) {
   //alert("in submitForm");
   
   //construct the url
   var ajaxComponentPath = document.getElementById("ajaxpath").value;
	
   
   var url = "/ajaxHelper?path=" + encodeURIComponent(ajaxComponentPath);
   url = url + "&name=" + encodeURIComponent(name);
   url = url + "&visitorEmail=" + encodeURIComponent(visitorEmail);
   url = url + "&comments=" + encodeURIComponent(comments);
   url = url + "&department=" + encodeURIComponent(departmentSelected);
   url = url + "&pageTitle=" + encodeURIComponent(pageTitle);
   url = url + "&pageUrl=" + encodeURIComponent(pageUrl);
   url = url + "&imageText=" + encodeURIComponent(imageText);
   url = url + "&sessionId=" + encodeURIComponent(originalSessionId);
   url = url + "&formSubmitted=true";
   
   //alert("url is: " + url);
   if (typeof XMLHttpRequest != "undefined") {
       req = new XMLHttpRequest();
   } else if (window.ActiveXObject) {
       req = new ActiveXObject("Microsoft.XMLHTTP");
   }
   req.open("GET", url, true);
   req.onreadystatechange = callbackContactUs;
   req.send(null);
   //alert("finished submitForm");
}

/**
*
*	Call back function to handle the response from the request
*   made by the submitForm function.  This function will replace
*   the existing form html code with the html received.
*   If a status other than 200 eg 404 if the target server cannot
*   be found then the method will call an error handling function.
*/
function callbackContactUs() {
	//alert("in callbackContactUs: readystate:" + req.readyState + ", status:" + req.status);
    if (req.readyState == 4) {
        if (req.status == 200) {
            // update the HTML DOM based on whether or not message is valid
            var message = req.responseText;
           // alert("message is: " + message);
            
            var popup_message = document.getElementById("popup_message");
            popup_message.innerHTML = message;
            var cookieDate = new date();
            cookieDate.setDate(cookieDate.getDate()+1);
           	document.cookie = "JSESSIONID" + "=" + originalSessionId + "; expires=" + cookieDate.getDate() + "/" + cookieDate.getMonth() + "/" + cookieDate.getYear() + "; path=/";
        }
        else
        {
        	ajaxError();
        }
    }
}

/**
*
*	This method is called whenever the ajax functionality fails for
*   some reason.  This method will attempt to update the poll via
*   the tradional (non-ajax) form submit (page refresh) functionality.
*/
function ajaxError()
{
	//alert("A problem occured when attempting to get the next month via ajax");	
	//alert("href: " + document.getElementById(savedDirection).href);
	window.location = "#";
}

/**
*
*	This method limits the number of characters that can be entered intgio the comments textarea field
*/
function limitText(number, limitNum) 
{
	if (document.getElementsByName('comments')[number].value.length > limitNum) 
	{
		document.getElementsByName('comments')[number].value = document.getElementsByName('comments')[number].value.substring(0, limitNum);
	}
}
