window.addEvent('domready', function()
	{
		LoveOfPeets.init($('LoveOfPeetsForm'));
		Utility.turnOff("ShareYourStoryloadingIcon");
	}
);

var LoveOfPeets =
{
    myVal:null,
    curform:null,
    init:function(curform)
    {
    	this.curform = curform;
    },
	step1:function()
    {
        this.myVal = new Validate("ShareYourStoryValidationSummary");
        this.myVal.isEmpty("ShareYourStoryname","ShareYourStoryNameError","Enter your name");
        this.myVal.isEmpty("ShareYourStorycity","ShareYourStoryCityError","Enter your city");
        this.myVal.isEmpty("ShareYourStorystate","ShareYourStoryStateError","Enter your state/province");
 		this.myVal.isEmpty("ShareYourStorycountry","ShareYourStoryCountryError","Enter your country");
 		this.myVal.isEmail("ShareYourStoryemail","ShareYourStoryEmailError","Enter a valid email address");
 		this.myVal.isEmpty("ShareYourStorystory","ShareYourStoryStoryError","Enter your Peet&#8217;s story");
        //track('cmCreateConversionEventTag("Share Your Story",1)');
		// FINAL STEP
		if(this.myVal.isValid())
        {
            //this.curform.submit();
            Utility.turnOnInline("ShareYourStoryloadingIcon");
            $("ShareYourStorystory").value = encodeURIComponent($("ShareYourStorystory").value);
            //$("ShareYourStorystory").value = encodeURIcomponent($("ShareYourStorystory").value);
            var serializer = new Serializer();
            serializer.init(["ShareYourStoryname", 
            				"ShareYourStorycity", 
            				"ShareYourStorystate", 
            				"ShareYourStorycountry", 
            				"ShareYourStoryemail", 
            				"ShareYourStorydrinking", 
            				"ShareYourStorystory",
            				"ShareYourStoryZip"]);
            var signupFlag = ($("shareStoryOptin").checked) ? "&signup=1" : "";
            var URL = "/callback/LoveOfPeets/?i=i"+ serializer.yield()+signupFlag;


			var myAjax = new Ajax(URL,
			{
    			method: 'get',
    			onSuccess: function(responseText)
    			{
                    if(responseText == "sent")
			    	{
                        //$("ShareYourStoryresponseMessage").innerHTML = "Your submission will be reviewed by an Administrator.";
                        LoveOfPeets.myVal.generalError(null,["Thank you for your submission.<br />It will be reviewed by an Administrator."]);
			    	    LoveOfPeets.myVal.peetsYieldSuccess("LoveOfPeets");
                        Utility.turnOff("ShareYourStoryloadingIcon");
                        $("ShareYourStoryname").value = "";
                        $("ShareYourStorycity").value = "";
                        $("ShareYourStorystate").value = "";
                        $("ShareYourStoryZip").value = "";
                        $("ShareYourStorycountry").value = "";
                        $("ShareYourStoryemail").value = "";
                        $("ShareYourStorystory").value = "";
                        $("ShareYourStorydrinking").value = "";
                        track('cmCreateConversionEventTag("Share Your Story","2","Site Events","0")');
			    	}
			    	else
			    	{
                        //$("ShareYourStoryresponseMessage").innerHTML = "An error has occured.";
                        LoveOfPeets.myVal.generalError(null,["An error has occured."]);
			    	    LoveOfPeets.myVal.peetsYield(true);
                        Utility.turnOff("ShareYourStoryloadingIcon");
			    	}
			    	Utility.turnOn("ShareYourStoryresponseMessage");
    			},
    			onFailure: function(sender,responseText)
    			{
    				//alert('failed request');
    				//$("ShareYourStoryresponseMessage").innerHTML = "An error has occured.";
            		LoveOfPeets.myVal.generalError(null,["An error has occured."]);
		    	    LoveOfPeets.myVal.peetsYield(true);
    				Utility.turnOff("ShareYourStoryloadingIcon");
    			}
			}).request();
        }
        else
        {
            this.myVal.peetsYield(true);
            return false;
        }
    }
}
