YAHOO.util.Event.addListener(window, "load", initPage);
var contactForm;
var otherDiv;
var otherInput;

var alertOk = function() {
	this.hide();
};

//Set browser type
var browser;
if (document.layers) {browser = "nn4"}
if (document.all) {browser = "ie"}
if (window.navigator.userAgent.toLowerCase().match("gecko")) {browser = "gecko"}

/**
 * this function is called when the page loads up
 */
function initPage() {
	contactForm = document.getElementById("contactForm");
	// check if other was selected in dropdown and dynamically add a textfield
	var learnAbout = document.getElementById("learnAbout");
	checkOther(learnAbout);
	
	// check if other was selected in dropdown and dynamically add a textfield
	var browserType = document.getElementById("browserType");
	checkOther(browserType);
	
	// set event listener for personal info panel
	setPanelTabEventHandler(onPersonalInfoClick, "click", 0);
	
	// set event listener for type of request panel
	setPanelTabEventHandler(function(e){ return verifyPersonalInfo(1);}, "click", 1);
	setPanelTabEventHandler(function(e){ return setCurrentPanel(1);}, "mouseover", 1);
	setPanelTabEventHandler(resetCurrentPanel, "mouseout", 1);
	
	// set event listeners for request detail panel
	setPanelTabEventHandler(function(e){ return verifyRequestType(2);}, "click", 2);
	setPanelTabEventHandler(function(e){ return setCurrentPanel(2);}, "mouseover", 2);
	setPanelTabEventHandler(resetCurrentPanel, "mouseout", 2);
	
	// set event listeners for additional info panel
	setPanelTabEventHandler(function(e){ return verifyRequestDetails(3);}, "click", 3);
	setPanelTabEventHandler(function(e){ return setCurrentPanel(3);}, "mouseover", 3);
	setPanelTabEventHandler(resetCurrentPanel, "mouseout", 3);
}

function setPanelTabEventHandler(callFunction, event, panelIndex) {
	var panels = Accordion1.getPanels();
	var panel = panels[panelIndex];
	var tab = Accordion1.getPanelTab(panel);
	Spry.Widget.Accordion.addEventListener(tab, event, callFunction, false);
}

/**
 * Checks to see if 'Other' is selected
 */
function checkOther(thisField) {
   	if(thisField.value == "Other") {
		if ( thisField.id == "learnAbout" ) {   	
			showOther("otherField", "learnOther");
		}
		else if ( thisField.id == "browserType" ) {
			showOther("otherBrowserType", "otherBrowser");
		}
	} else {
		if ( thisField.id == "learnAbout" ) {   	
			hideOther("otherField", "learnOther");
		}
		else if ( thisField.id == "browserType" ) {
			hideOther("otherBrowserType", "otherBrowser");
		}
	}
}

/**
 * Hides the 'Other' layer
 */
function hideOther(otherDivName, otherInputName) {
  	otherDiv=document.getElementById(otherDivName);
  	otherInput=document.getElementById(otherInputName);
  	otherDiv.style.display = "none";
  	otherDiv.value = "";
}

/**
 * Shows the 'Other' layer
 */
function showOther(otherDivName, otherInputName) {
  	otherDiv=document.getElementById(otherDivName);
  	otherInput=document.getElementById(otherInputName);

  	otherDiv.style.display = "block";
  	otherDiv.style.visibility="visible";
  	otherInput.style.display = "block";
  	otherInput.style.visibility="visible";
}

/**
 * sets the current panel associated with the panel index passed
 */
var setCurrentPanel = function(panelIndex) {
	var panels = Accordion1.getPanels();
	Accordion1.currentPanel = panels[panelIndex];
}

function getSelectedValue(requestTypes) {
	var reqVal;
	
	var totalReq = requestTypes.length;	
	for(var i = 0; i < totalReq; i++) {
		if(requestTypes[i].checked) {
			reqVal = requestTypes[i].value;
			continue;
		}
	}
	return reqVal;
}

function onPersonalInfoClick() {
	Accordion1.fixedPanelHeight = 210;
	Accordion1.currentPanel = null;
	Accordion1.openFirstPanel();
}

/**
 * this function validate the general info section
 */
var verifyPersonalInfo = function(panelIndex) {	
	if (validatePersonalInfo()) {
		Accordion1.fixedPanelHeight = 190;
		setCurrentPanel(panelIndex-1);
		Accordion1.openNextPanel();
	}
	else {
		setCurrentPanel(panelIndex);
	}
};

/**
 * this function validates the request type panel when the continue button is clicked
 * or request details panel is clicked
 */
var verifyRequestType = function(panelIndex) {
	if ( validatePersonalInfo() && 
		 validateRequestType() ) {
		var requestTypes = document.contactForm.requestType;
		var reqVal = getSelectedValue(requestTypes);
		if ( reqVal == 1 ) {
			Accordion1.fixedPanelHeight = 300;
			contactForm.emailTo.value = "tradefutures@lind-waldock.com";
		}
		else if ( reqVal == 2 ) {
			Accordion1.fixedPanelHeight = 450;
			contactForm.emailTo.value = "tradefutures@lind-waldock.com";
		}
		else if ( reqVal == 3 ) {
			Accordion1.fixedPanelHeight = 280;
			contactForm.emailTo.value = "support@lind-waldock.com";
		}
		setCurrentPanel(panelIndex-1);	
		Accordion1.openNextPanel();
	}
	else {
		setCurrentPanel(panelIndex);
	}
};

/**
 * this function validates the request details panel when the continue button is clicked
 * or additional info panel is clicked. 
 */
var verifyRequestDetails = function(panelIndex) {
	if ( validatePersonalInfo() && 
		 validateRequestType() &&
		 validateRequestDetails() ) { 
		Accordion1.fixedPanelHeight = 240;
		setCurrentPanel(panelIndex-1);	
		Accordion1.openNextPanel();
	}
	else {
		setCurrentPanel(panelIndex);
	}
};

/**
 * this function will redirect the user once the submit button has been pressed
 */
function sendContactRequest() {
	if ( validatePersonalInfo() && 
		 validateRequestType() &&
		 validateRequestDetails() &&
		 validateAdditionalInfo() ) {
			contactForm.submit();
	}
}

function validatePersonalInfo() {
	with (contactForm) {
		if (!validateField(firstname,"'First Name' is required.")) {
			return false;
		}
		if (!validateField(lastname,"'Last Name' is required.")) {
			return false;
		}
		if (!validateField(address1,"'Address' is required.")) {
			return false;
		}
		if (!validateField(city,"'City' is required.")) {
			return false;
		}
		if (!validateField(state,"'State/Providence' is required.")) {
			return false;
		}
		if (!validateField(zip, "'Zip/Postal Code' is required.")) {
			return false;
		}
		if (!validateField(daytime, "'Daytime Phone' is required.")) {
			return false;
		}	
		if (!validateField(email, "'E-Mail address' is required.")) {
			return false;
		}
		if (!validateEmail(email, "'E-Mail address' is invalid.")) {
			return false;
		}
	}
	return true;
}

/**
 * this function checks to see if a request type has been selected
 * and returns a boolean value, it also sets the correct request detail
 * info page dependent on which request type was selected.
 */
function validateRequestType() {
	var requestTypes = document.contactForm.requestType;
	if (!validateRadioButton(requestTypes, "'Type of Request' is required.")) {
		setCurrentPanel();
		return false;
	}
	var reqVal = getSelectedValue(requestTypes);

	if ( reqVal == 1 ) {
		document.getElementById("generalInfo").style.display = "block";
		document.getElementById("technicalInfo").style.display = "none";
		document.getElementById("moreInfo").style.display = "none";
	}
	else if ( reqVal == 2 ) {
		document.getElementById("moreInfo").style.display = "block";
		document.getElementById("technicalInfo").style.display = "none";
		document.getElementById("generalInfo").style.display = "none";
	}
	else if ( reqVal == 3 ) {
		document.getElementById("technicalInfo").style.display = "block";
		document.getElementById("generalInfo").style.display = "none";
		document.getElementById("moreInfo").style.display = "none";
	}
	
	var panels = Accordion1.getPanels();
	var panel = panels[2];
	if ( Accordion1.currentPanel == panel ) {
		Accordion1.currentPanel = panel[1];
	}
	
	return true;
}

function validateRequestDetails() {
	with(contactForm) {
		if(generalInfoType.checked) {
			if(!daytimeCB.checked && !emailCB.checked) {
				generalInfoType.focus;
				displayAlert("'How would you like us to contact you?' is required.");
				return false;
			}
			else if(!validateField(generalQuestionComment, "'Please enter your question or comment' is required.")) {
				return false;
			}
		}
		else if(moreInfoType.checked) {
		
		}
		else if(technicalInfoType.checked) {
			if (browserType.value.length == 0) {
				browserType.focus;
				displayAlert("'What type of browser are you using?' is required in Request Details Panel.")
				return false;
			}
			else if(browserType.value == "Other"){
				if (!validateOther("otherBrowser", "'Identify source' is required in Request Details Panel.")) {
					otherBrowser.focus;
					return false;
				}
				else if ( !validateField(problemDescription, "'Please describe any problem you are encountering' is required in Request Details Panel.")) {
					problemDescription.focus;
					return false
				}
			}
		}
	}
	return true;
}

function validateAdditionalInfo() {
	with (contactForm) {	
		if (!validateRadioButton(yesNo, "'Have you ever traded futures or options?' is required.")) {
			return false;
		}
		if (!validateLearn(learnAbout, "'Where did you hear about Lind-Waldock' is required.")) {
			return false;
		}
	}
	
	return true;
}
