// -----------------------------
// Form Submission routine
// Author: Steven McKain
// Email:  mckains@cadvision.com
// Date:   September 24, 1999
// History:	00/02/20 Jeff Allan: Added local validation for required fields.
// 		01/11/08 Jeff Allan: Changed from formmail.cgi to mailform.cgi
// -----------------------------
// What it does: 
//
//    This routine is designed to be used with 
//    Cadvision's formmail cgi program.  This
//    routine allows one to have multiple buttons
//    to submit information from one form, but
//    redirect the web-client to different pages
//    or files.
//
// Note: 
//
//    More information regarding Cadvision's formmail can
//    be found at: http://www.getdomain.com/webhelp/formmail.html
//
// Instructions: 
// 
//    You should include these minimum tags in your form.
//
//    =========================================================
//    Example:
//    =========================================================
//	<SCRIPT language="JavaScript" src="form_submit.js"></SCRIPT>
//
//	<FORM action="http://www.cadvision.com/cgi-bin/homepage/form_handler" method="post" name="download_form">
//
// 		<INPUT type="hidden" name="redirect" value="Default">
//		<INPUT type="hidden" name="sent_to" value="Default Page">
//		<INPUT type="hidden" name="recipient" value="mckains@cadvision.com">
//		<INPUT type="hidden" name="subject" value="Download Form">
//		<INPUT type="hidden" name="required" value="realname,Company,email">
//
//		<INPUT type="button" value="microsoft stuff" onClick="form_submit(download_form, 'http://www.microsoft.com/', 'Sent web-client to microsoft', true)">
//		<INPUT type="button" value="ibm stuff" onClick="form_submit(download_form, 'http://www.ibm.com/', 'Reasearching info at IBM', false)">
//
//	</FORM>
//    =========================================================
//
//    The example above includes two buttons, one for Microsoft and one for IBM.
//    Both buttons call the form_submit routine, but there is a slight difference between the two.
//
//    Button 1:
//      - web-client will be forwarded to: "http://www.microsoft.com/"
//      - message will be sent via email to "mckains@cadvision.com" (the recipient specified)
//        Why? the last item in the call to form_submit is true, indicating to send the message
//
//    Button 2:
//      - web-client will be forwarded to: "http://www.ibm.com/"
//      - no message will be sent
//        Why? the last item in the call to form_submit is false, indicating not to send the message
//
//    By changing the specified address to the address of a file, one can send the web-client
//    a file when they select the button.
//
// Note:
//    The <FORM> tag must have a name, and each call to the routine form_submit() must include
//    this name as the first element.
// -----------------------------

function form_submit(theform, item_to_goto, sent_to, send_information) {
  if (send_information){

  	// Validate required fields
	var sRequiredFields = theform.required.value.split(',');
	for (var f = 0; f < sRequiredFields.length; f++)
	{
		var element = theform.elements[sRequiredFields[f]];
		if (element.type == "text" || element.type == "textarea")
		{
			if (element.value == null || element.value == "")
			{
				var sURL = window.location.href.split('#')[0];
				window.location.replace(sURL);
				window.alert("Please enter a value for " + element.name);
				return;
			}
		}
		else if (element.type == "select-one" || element.type == "select-multiple")
		{
//			window.alert(element.selectedIndex);
			if (element.selectedIndex == 0)
			{
				var sURL = window.location.href.split('#')[0];
				window.location.replace(sURL);
				window.alert("Please select a value for " + element.name);
				return;
			}
		}
	}

///	theform.action = "http://www.cadvision.com/cgi-bin/mailform";
///    theform.method = "POST";
    theform.elements["redirect"].value = item_to_goto;
//    theform.elements["sent_to"].value = sent_to;
//			window.alert(sent_to);
    theform.elements["subject"].value = sent_to;
    theform.submit();
  }
  else {
    document.location = item_to_goto;
  }
}

