﻿$DL(function(){
	ContactUsManager.Init();
	
	//alert (ContactUsManager.Countries.join("|"));
});

var ContactUsManager = {
    Countries: [],
    Validator: null,
    Init: function() {

        $("ContactUsForm").addEvent("submit", function() {
            //ContactUsManager.Send();
        });

        ContactUsManager.SetCountriesList();

        ContactUsManager.Validator = new FormCheck('ContactUsForm',
				{
				    error_class: 'errorNote',
				    onAfterValid: ContactUsManager.Send,

				    display: {
				        scrollToFirst: false,
				        errorsLocation: 2
				    },
				    alerts: {
				        required: '*'
				    }
				})

        //Request a Demo ---
        var option = Site.QS("option");
        if (option) {
            $("ddlSubject").set("value", unescape(option));
        }
        ///
    },
    SetCountriesList: function() {
        var ddl = $("ddlCountry_form");
        if (!ddl) return;

        ContactUsManager.Countries.each(function(str) {
            if (document.all) {
                ddl.add(new Option(str, str));
            } else {
                ddl.add(new Option(str, str), null);
            }
        });
    },
    Send: function(e) {
        // this is called from the validator so we dont need to check anything
        e = new Event(e).stop();

        $("btSubmit").value = "Sending";
        $("btSubmit").addEvent("click", function(e) { e = new Event(e).stop() });

        Mantis.Optier.Services.PublicService.SendContact(
			$("txtFullName").value,
			$("txtEmail").value,
			$("txtPhone").value,
			$("ddlCountry_form").value,
			$("ddlSubject").value,
			$("txtComments").value,
			$("chkWantInfo").checked,
			ContactUsManager.ShowResponse,
			$ajaxFailure
		);
    },
    ShowResponse: function(serverResponse) {
        /*
        $("ContactUsFormResponse").innerHTML = serverResponse;
        $("ContactUsFormResponse").show();
        $("ContactUsForm").hide();
        */
        mktoMunchkinFunction('associateLead',
                {
                FirstName:$("txtFullName").value,
                LastName:$("txtFullName").value,
			    Email:$("txtEmail").value,
			    Phone:$("txtPhone").value,
			    Country:$("ddlCountry_form").value,
			    PersonalNotes:$("ddlSubject").value+" - "+$("txtComments").value,
			    Customer_Description_Info__c:$("ddlSubject").value+" - "+$("txtComments").value,
			    Lead_Source_Name_Text__c: 'Web contact form',
			    Lead_source_name__c: 'Web contact form',
			    Unsubscribed:false
            }, serverResponse);
        self.location='contact_us_sent.aspx';
    }
};
