﻿// Allows posting of form fields to external pages (bypasses the ASP.NET 
// postback system). To use, assign to the 'onclick' event of a button or
// other element. 
 
//   targetUrl: external URL to post to
//   formMethod: 'GET' or 'POST'
//   validateFunction: a function to handle input validation. Should return 
//     true if the form is valid, false otherwise - if false, then post is 
//     cancelled.

function PostForm( targetUrl, formMethod, validateFunction ) {
    
    // Run the validation function if it exists
    var isValid = true
    var functionObject = eval( validateFunction );
    if ( validateFunction && ( typeof( functionObject ) == 'function' ) ) {
        isValid = functionObject();
    }
    
    // Exit if validation failed
    if ( !isValid ) {
        return;
    }
    
    // Cache the old form attributes
    var pageForm = document.getElementById( 'aspnetForm' );
    var oldAction = pageForm.action;
    var oldMethod = pageForm.method;
    
    // Submit using the new ones
    pageForm.action = targetUrl;
    pageForm.method = formMethod;
    pageForm.submit();
    
    // Restore the old ones, for Back button support
    pageForm.action = oldAction;
    pageForm.method = oldMethod;
}
