﻿//This file will hold all the js events concerning web forms sending mail
//Jared Bacik - 10/13/2009

var formTables;
var showSuccessAfterEl;
var defaultErrMsg = 'Your message was not able to be sent please make sure all the information given was correct and try to request the information again.';

//Add form validation to the form with the given ID
function InitFormCheck(frmID)
{
    myCheck = new FormCheck(frmID, {
        trimValue : true,
        display : {
            errorsLocation : 0,
            indicateErrors : 2,
            showErrors : 1,
            addClassErrorToField : 1
        }
    });
}

function InitFormTables(tblEls, AfterEl)
{
    formTables = tblEls;
    if ($chk(AfterEl)) {
        showSuccessAfterEl = AfterEl;
    }
}

//Prep the request with an ajax loader and disable the send button for double clicks
function showRequest(){
	//Relies on btnSubmit as the id for the submiting button
	$$('.buttons a','.buttons input').each(function(el) {
	    el.setStyle('display','none');
	});
	//$('btnSubmit').setStyle('display','none');
	var imgLoad = new Element('img', {
	    id: 'imgLoad',
	    src:'Images/ajaxLoad_RedBar.gif',
	    'class':'AjaxLoad',
	    alt:'sending mail...' });
	var spanLoad = new Element('span', {
        id: 'spanLoad',
        html:'Generating and Sending Request...',
        'class':'AjaxLoad'});

    imgLoad.inject($('btnSubmit').getParent());
    spanLoad.inject($('btnSubmit').getParent());
}

//Show Response on send request
function showResponse(reqText){
	//Check the request text for the status
	var rSplit = reqText.split('|');
	var bStatus = rSplit[0];
	var reqMsg = rSplit[1];
	
	var divMsg = new Element('div');
	
	//Remove any success/error divs already there and any pre-loaders
	$$('.form-success', '.form-error', '.AjaxLoad').each(function(el) {
	    el.dispose();
	});
	
    if (bStatus.toUpperCase() == "SUCCESS")
	{
	    //Show the success div and hide the form
	    divMsg.addClass('form-success');
	    $each(formTables, function(tbl, index){
	        tbl.dispose();
	    });
	}else{
	    //Show the error div and increment the sent count
	    //if sent count > 2 then just tell the user to try another day/time 
	    divMsg.addClass('form-error');
	    $('hdnSendAttempts').value = parseInt($('hdnSendAttempts').value + 1);
	    if ($('hdnSendAttempts').value > 2) {
	        reqMsg = "Oops!!  Looks like we are having some techinical issues.  Please " +
	            "come back soon and try to request the more information again. You can also " +
	            "give our service department a call anytime at, 440-237-6677 or directly email us at " +
	            "Info@abcfireinc.net. ";
	        /*$each(formTables, function(tbl, index){
	            tbl.dispose();
	        });*/
	    }else{	
	        $$('.buttons a','.buttons input').each(function(el) {
	            el.setStyle('display','inline');
	        });
	    }
	    //$('btnSubmit').setStyle('display','inline');			    
	}
	divMsg.set('html',reqMsg);
	
	if ($chk(showSuccessAfterEl)) {
    	divMsg.inject(showSuccessAfterEl,'after');				
    }else{
        if (bStatus.toUpperCase() == 'SUCCESS') {
            divMsg.inject($$('form')[0],'bottom');
        }else{
            divMsg.inject($$('form')[0],'top');
        }
    }
}

function failResponse(xhr) {
    //Should log the errors somehow!
    
   	//Remove any success/error divs already there and any pre-loaders
	$$('.form-success', '.form-error', '.AjaxLoad').each(function(el) {
	    el.dispose();
	});
	
	//Show default errors message
	var divMsg = new Element('div');
	divMsg.addClass('form-error');
	divMsg.set('html',defaultErrMsg);
	
	if ($('hdnSendAttempts').value > 2) {
        var reqMsg = "Oops!!  Looks like we are having some techinical issues.  Please " +
            "come back soon and try to request the more information again. You can also " +
            "give our service department a call anytime at, 440-237-6677";
        $each(formTables, function(tbl, index){
            tbl.dispose();
        });
        divMsg.set('html',reqMsg);
    }
    
    if (showSuccessAfterEl != null) {
    	divMsg.inject(showSuccessAfterEl,'after');				
    }else{
        divMsg.inject($$('form')[0],'bottom');
    }				
    
    $('btnSubmit').setStyle('display','inline');
}
