var NeedHelp = {
	
    container: null,
    boxContainer: null,
	
	
    init: function(){
        NeedHelp.container = $('div.container');
        NeedHelp.boxContainer = NeedHelp.container.find('div#sideOpenAccount');
        NeedHelp.boxContainer.find('input:text').bind('focus', {}, NeedHelp.onTextFieldFocus);
        NeedHelp.boxContainer.find('input:text').bind('blur', {}, NeedHelp.onTextFieldBlur);
		
        NeedHelp.boxContainer
        .find('form#sideOpenAccountForm')
        .bind('submit', {}, NeedHelp.onSubmit);
				

        //set the country in the contact me section from geoIP
        NeedHelp.container.find('form#sideOpenAccountForm select[name="Country"]').find('option[value="' + AppData.GEOIPCountry + '"]').attr('selected','selected');
        NeedHelp.container.find('select[id="countryNeedHelp"]').selectbox(
        {
            inputClass: 'selectboxBig',
            containerClass: 'selectbox-wrapperBig',
            scrollBar: true,
            chainTitle: true,
            onChange: function( name, value, element ) {
                $.post(
                    'rpcProxy/getCountryPrefix',
                    {
                        countryId: value
                    },
                    function(data){
                        if (data!=null){
                            General.container.find('form#sideOpenAccountForm input[name="Prefix"]').val('+' + data);
                        }
                        else{
                            General.container.find('form#sideOpenAccountForm input[name="Prefix"]').val('');
                        }
                    },
                    'json');
            }
        }
        )

    },


    onTextFieldFocus: function(event){
        var field = $(event.target);
        var value = $.trim(field.val());
        if(value == field.attr('default')) field.val('');
		
    },
	
    onTextFieldBlur: function(event){
        var field = $(event.target);
        var value = $.trim(field.val());
        if(value == '') field.val(field.attr('default'))
    },
	
    onSubmit: function(event){
        //validation
        var isValid = true;
		
        //for special nedd help use regular(not ajax Post)
		
		
        var form = $(event.target);
        var aErrors = [];
        form.find('input:text').each(function(index){
            if($(this).val() == $(this).attr('default')){
                            
                //alert($(this).val() + ' must field');
                aErrors[aErrors.length] = $(this).val() + ' is a must field.';
                isValid = false;
            }
			
        });
        if (! isValid ){
            alert( 'Cannot send the form. Please correct the form errors:\r\n' + aErrors.join('\r\n'));
        }
        var formParams = form.serializeArray();
		
        if(NeedHelp.container.find('div#sideOpenAccount').size()){
            if(isValid)
                return true;
            else
                return false;
        }
		
        if(isValid){
            $.ajax({
                type: "POST",
                url: 'rpcProxy/needHelp',
                data: formParams,
                async: true,
                dataType: 'json',
                error : function() {
                    alert('error');
                },
                success: function(data){
                    if(data.status != true) { //failed
                        for (i in data.errors) {
                            alert(data.errors[i]);
                        }
                    }
                    else{ //success
                        form.remove();
                        NeedHelp.container.find('div#thankYou').removeClass('hidden');

                        //Use an iframe so if the campaign code is JS, it will fire.
                        var campaignCodeFrame = $('<iframe id="campaignFrame" frameborder="0" height="1" width="1" scrolling="no" src="rpcProxy/campaignCodeLoader/' + data.leadId + '"></iframe>');
                        NeedHelp.container.find('div#thankYou').append(campaignCodeFrame);

                        var googleIframe = $('<iframe id="googleFrame" frameborder="0" height="1" width="1" scrolling="no"></iframe>').attr('src',AppData.url + 'GoogleAnalytics');
                        $('div#thankYou').append(googleIframe);
                    }
                }
            });
        }
        return false;
    }
	
	
	
}

$(NeedHelp.init);
