var otherField = document.getElementById("otherField");

var learnOther = document.getElementById("learnOther");

var alertOk = function() {

	this.hide();

};



/**

 * resets the current panel to the previous current panel 

 */

function resetCurrentPanel() {

	var panels = Accordion1.getPanels();

	var panel = panels[Accordion1.getCurrentPanelIndex()-1];

	Accordion1.currentPanel = panel;

}



function setButtonListener(buttonName, buttonId, callFunction) {

	var button = new YAHOO.widget.Button(buttonName, { id:buttonId });

	button.addListener("click", callFunction);

}



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);

}



function displayAlert(alertMsg) {

	var alertDialog = new YAHOO.widget.SimpleDialog("alertDialog", { width: "300px", fixedcenter: true, visible: false, draggable: false, close: true, text: alertMsg, icon: YAHOO.widget.SimpleDialog.ICON_WARN, constraintoviewport: true, buttons: [{text:"Ok", handler:alertOk, isDefault:true }]});

	alertDialog.setHeader("Form Not Complete");

	alertDialog.render(document.body);

	YAHOO.util.Event.addListener("show", "click", alertDialog.show, alertDialog, true);

	YAHOO.util.Event.addListener("hide", "click", alertDialog.hide, alertDialog, true);

	

	alertDialog.show();

}



/**

 * Checks to see if 'Other' is selected

 */

function checkOther(thisField) {

   if(thisField.value == "Other") {

		showOther();

	} else {

		hideOther();

	}

}



/**

 * Hides the 'Other' layer

 */

function hideOther() {

  if(otherField == undefined)

	  otherField=document.getElementById("otherField");

  if(learnOther == undefined)

	  learnOther=document.getElementById("learnOther");

  otherField.style.display = "none";

  otherField.value = "";

}



/** 

 * Shows the 'Other' layer

 */

function showOther() {

  	if(otherField == undefined)

	  	otherField=document.getElementById("otherField");

  	if(learnOther == undefined)

	  	learnOther=document.getElementById("learnOther");



  	otherField.style.display = "block";

  		otherField.style.visibility="visible";

  	learnOther.style.display = "block";

  	learnOther.style.visibility="visible";

}



function validateEmail(fieldName,alertTxt) {

	with (fieldName) {

		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+\s*$/.test(value)){

			return true;

		} else {

			fieldName.focus;

			displayAlert(alertTxt);

			return false;

		}

	}

}



function validateOther(fieldName,alertTxt) {

	otherInput=document.getElementById(fieldName);

	if(otherInput.value == ""){

        otherInput.focus;

        displayAlert(alertTxt);

		return false

	}

	return true;

}



function validateLearn(fieldName,alertTxt) {

  with (fieldName) {

	if (value==null||value=="") {

	  fieldName.focus;

	  displayAlert(alertTxt);

	  return false;

	}

	else if(value == "Other"){

		return validateOther("learnOther", "'Identify source' is required.");

	} 

	else {

	  return true;

    }

  }

}



function validateField(fieldName,alertTxt) {

	with (fieldName) {

		if (value==null||value=="") {

	        fieldName.focus;

			displayAlert(alertTxt);

			return false;

		} else {

			return true;

		}

	}

}



/**

 * Validate passwords

 */

function validatePass(passField,confirmField,alertTxt) {

	with (passField) {

		p1 = value;

	}

	with (confirmField) {

		p2 = value;

	}

	

	if(p1.length < 4 || p1.length > 15 || /\s+/.test(p1) || p1 != p2) {

	    displayAlert(alertTxt);

		return false;

	} else {

		return true;

	}

}



function validateRadioButton(fieldName, alertTxt) {

	var myOption = -1;

	for (i=fieldName.length-1; i > -1; i--) {

		if (fieldName[i].checked) {

			myOption = i; i = -1;

		}

	}

	

	if (myOption == -1) {

		fieldName.focus;

		displayAlert(alertTxt);

		return false;

	}



	return true;

}
