var Urls = [];
Urls["LEAD"] = "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8";
Urls["CASE"] = "https://www.salesforce.com/servlet/servlet.WebToCase?encoding=UTF-8";

var Emails = [];

Emails["Product/price/services enquiry"] = "LEAD";
Emails["Support/lost password/lost user name"] = "CASE";
Emails["Additional user license/training"] = "LEAD";
Emails["Student account request"] = "LEAD";
Emails["Other"] = "LEAD";

var ids = []

ids["first_name"] = "00N20000002Rqyt";
ids["last_name"] = "00N20000002Rqz3";
ids["company"] = "00N20000002Rqz8";
ids["country"] = "00N20000002Rr0G";
ids["email"] = "00N20000002RqzD";
ids["mobile"] = "00N20000002Rqzh";
ids["product"] = "00N20000002RqwY";
ids["subject"] = "00N20000002Rqwd";
ids["description"] = "description";

var selectedSubject;

function submitForm() {
    var selectedText = selectedSubject;                     //get subject
    if (Emails) {                                                           //check if emails is defined
        var arrayText = Emails[selectedText];               //select email from array
        if (arrayText) {                                                //Check that element exists in array
            var newUrl = Urls[arrayText];                       //get url
            if (newUrl) {                                                   //Check that url exists in array
                var idstemp = $('[id]');                                //get all ids
                var arLen = idstemp.length;                         //get the length
                for (var i = 0, len = arLen; i < len; ++i) {        //loop through the ids
                    var id = $(idstemp[i]).attr("id");              //find the id for each element
                    var idname = ids[id];                               //get the new id from array
                    if (idname) {                                           //check that the element exists in array
                        $(idstemp[i]).attr("id", idname);        //change id                        
                        $(idstemp[i]).attr("name", idname);     //change name
                    }
                }
                $("#questback").attr("action", newUrl);      //change post action on form                
                return true;
            }
        }
    }
}

$(document).ready(function () {
    $("#questback").validate();

    $('#subject').change(function (e) {
        selectedSubject = $("#subject").val();
        //$("#subject option:selected").val();
    });

    // And now fire change event when the DOM is ready
    $('#subject').trigger('change');
});
