/* Javascript file for Google Contact Form plugin to allow AJAX and validation */

// options
gcfContainerId = "googleFormContainer";
gcfFormId = "googleForm";
// set response URL if not set by PHP in the page source
if(typeof (gcfResponseUrl) == "undefined") { gcfResponseUrl = "/" ;}
// set error message if not set by PHP in the page source
if(typeof (gcfErrorMsg) == "undefined") { gcfErrorMsg = "Error posting form" ;}

jQuery(document).ready(function() {
	
	/*
	 * setup stock Google form for use with jQuery Validate plugin
	 */
	
	
	// add id to Google Form
	jQuery("#" + gcfContainerId + " form").attr("id",gcfFormId);
	
	// add required class
	jQuery("#" + gcfContainerId + " form .ss-item-required input").each(function(){
		jQuery(this).addClass("required");
	});
	
	// add title messages
	jQuery("#" + gcfContainerId + " form .ss-item").each(function(){
		var msg = jQuery(this).find(".ss-q-help").text();
		jQuery(this).find("input").attr("title", msg);
	});
	
	// remove <br/>
	jQuery("#" + gcfContainerId + " br").remove();
	
	// move AJAX messages to after submit button
	jQuery("#ajaxLoading, #ajaxError").appendTo("#" + gcfContainerId + " .ss-navigate .ss-form-entry");
	// hide AJAX messages
	jQuery("#ajaxLoading, #ajaxError").hide();
		
	/*
	 * AJAX form post using jQuery Form plugin
	 * see http://malsup.com/jquery/form/ 
	 */ 
	var targetUrl = jQuery('#' + gcfFormId).attr("action");
	targetUrl = encodeURI(targetUrl);
    var options = { 
        //target:        "#ajaxOutputContainer",   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse,  // post-submit callback
		url: "/wp-content/plugins/google-form/google-form-proxy.php?proxy_url=" + targetUrl,
		clearForm: false,
		resetForm: false,
		type: 'POST',
		timeout: 120000
    }; 
    jQuery('#' + gcfFormId).ajaxForm(options); 
}); 
 
// pre-submit callback for AJAX form post 
function showRequest(formData, jqForm, options) {
	/* run jQuery Validate plugin on form
	 * see http://docs.jquery.com/Plugins/Validation/Validator
	 */
	if (jQuery("#" + gcfFormId).validate().form()) {
		// validated
		// hide error
		jQuery("#ajaxError").hide();
		// show loading 
		jQuery("#ajaxLoading").show();
		// disable submit button
		jQuery("#" + gcfFormId + " .ss-navigate input").attr("disabled","true");
		// continue to post form
		return true;
	} else {
		// didn't validate
		return false;
	} 
} 
 
// post-submit callback for AJAX form post
function showResponse(responseText, statusText)  {
	// check if success
	var successCode = "thanks";
	if(responseText.toLowerCase().indexOf(successCode) != -1){
		// close popup window if loaded (popup-page plugin)
		if(typeof popupPageHidePopup == 'function') {
			popupPageHidePopup();
		} 
		// goto response url if provided
		if(gcfResponseUrl != ""){
			window.location = gcfResponseUrl;		
		}
	} else {
		// hide loading 
	    jQuery("#ajaxLoading").hide();
		// enable submit button
		jQuery("#" + gcfFormId + " .ss-navigate input").removeAttr("disabled");
		// show error
		jQuery("#ajaxError").text(gcfErrorMsg).show();
	}
} 