window.addEvent('domready', function()
	{
		ExpertsQuestion.init($('ExpertsQuestionForm'));
		Utility.turnOff("ExpertsQuestionloadingIcon");
	}
);

var ExpertsQuestion =
{
    myVal:null,
    curform:null,
    init:function(curform)
    {
    	this.curform = curform;
    },
	step1:function()
    {
        this.myVal = new Validate("ExpertsQuestionValidationSummary");
        this.myVal.isEmpty("ExpertsQuestionName","ExpertsQuestionNameError","Enter your name");
		this.myVal.isEmpty("ExpertsQuestionEmail","ExpertsQuestionEmailError","Enter your email");
        this.myVal.isEmpty("ExpertsQuestionCity","ExpertsQuestionCityError","Enter the city you live in");
        this.myVal.isEmpty("ExpertsQuestionState","ExpertsQuestionStateError","Enter your State/Province");
        //this.myVal.isEmpty("ExpertsQuestionCountry","ExpertsQuestionCountryError","Enter your Country");
        this.myVal.isEmpty("ExpertsQuestionCountry","ExpertsQuestionCountryError","Enter your Country");
 		this.myVal.isEmpty("ExpertsQuestionMessage","ExpertsQuestionMessageError","Enter your Question");
        //track('cmCreateConversionEventTag("Ask Expert a Question","1","Site Events","0")');

		// FINAL STEP
		if(this.myVal.isValid())
        {
            //this.curform.submit();
            Utility.turnOnInline("ExpertsQuestionloadingIcon");
			$("ExpertsQuestionMessage").value = encodeURIComponent($("ExpertsQuestionMessage").value);
            var serializer = new Serializer();
            serializer.init(["ExpertsQuestionName", "ExpertsQuestionEmail", "ExpertsQuestionCity", "ExpertsQuestionState", "ExpertsQuestionCountry", "ExpertsQuestionMessage"]);
            var URL = "/callback/ExpertsQuestion/?i=i"+ serializer.yield();
			//$("responseMessage").innerHTML = URL;

			var myAjax = new Ajax(URL,
			{
    			method: 'get',
    			onSuccess: function(responseText)
    			{
                    if(responseText == "sent")
			    	{
                        //$("ExpertsQuestionresponseMessage").innerHTML = "Thank you for your submission. It will be reviewed by an Administrator.";
                        ExpertsQuestion.myVal.generalError(null,["Thank you for your submission.<br />It will be reviewed by an Administrator."]);
			    	    ExpertsQuestion.myVal.peetsYieldSuccess("ExpertsQuestions");
                        Utility.turnOff("ExpertsQuestionloadingIcon");
                        $("ExpertsQuestionName").value = "";
                        $("ExpertsQuestionEmail").value = "";
                        $("ExpertsQuestionCity").value = "";
                        $("ExpertsQuestionState").value = "";
                        $("ExpertsQuestionCountry").value = "";
                        $("ExpertsQuestionMessage").value = "";
                        track('cmCreateConversionEventTag("Ask Expert a Question","2","Site Events","0")');
			    	}
			    	else
			    	{
                        //$("ExpertsQuestionresponseMessage").innerHTML = "1: An error has occured.";
                        ExpertsQuestion.myVal.generalError(null,["An error has occured."]);
			    	    ExpertsQuestion.myVal.peetsYield(true,true);
                        Utility.turnOff("ExpertsQuestionloadingIcon");
			    	}
			    	Utility.turnOn("ExpertsQuestionresponseMessage");
    			},
    			onFailure: function(sender,responseText)
    			{
    				//alert('failed request');
    				//$("ExpertsQuestionresponseMessage").innerHTML = "2: An error has occured.";
    				ExpertsQuestion.myVal.generalError(null,["An error has occured."]);
		    	    ExpertsQuestion.myVal.peetsYield(true);
            		Utility.turnOff("ExpertsQuestionloadingIcon");
    			}
			}).request();

        }
        else
        {
            this.myVal.peetsYield(true);
            return false;
        }
    }
}

