/*************************************************************
    website:    www.subaru.com
    type:       Helpers for all Forms
    desc:       javascript helper funcitons and objects for
                all forms
    version:    1.1.0
*************************************************************/
// Start: errorMessage extension
jQuery.fn.errorMessage = function(options)
{
    var inputId = $(this).attr("id");
    var defaults = {
        containerId: inputId,
        messageTop: "This field cannot be left blank",
        messageMain: "Please provide a value and resubmit the form",
        formElement: "true",
        specialCase: "false"

    };
    var options = jQuery.extend(defaults, options);



    if (options.formElement == "true") {
        // add focus to the form element
        if (options.specialCase == "false") {
            var output = "<div id='error_"+options.containerId+"' class='errorCont'>";
            output += "<span class='top'></span>";
            output += "<p class='bold'>"+options.messageTop+"</p>";
            output += "<p>"+options.messageMain+"</p>";
            output += "</div>";
            output += "<div class='errorClear'></div>";

        // apend the output AFTER the form element
        $("#"+options.containerId).after(output);
            $("#" + options.containerId).addClass("errorFocus");
        }
        //Special Case for Request a Brochure, Schedule a Test Drive, Request a Quote
        else {
            var inputId2 = $(this).attr("class");
            var classes = inputId2.split(" ");
            if(classes.length==1){
                var output = "<div id='error_"+options.containerId+"' class='errorCont'>";
                output += "<span class='top'></span>";
                output += "<p class='bold'>"+options.messageTop+"</p>";
                output += "<p>"+options.messageMain+"</p>";
                output += "</div>";
                output += "<div class='errorClear'></div>";

                // apend the output AFTER the form element
                $("#"+options.containerId).after(output);
                $("#" + inputId2).addClass("errorFocus");
            }else{
                $("#" + classes[0]).addClass("errorFocus");
                $("#" + classes[1]).addClass("errorFocus");
                var output = "<div id='error_"+options.containerId+"' class='errorCont'>";
                output += "<span class='top'></span>";
                output += "<p class='bold'>This fields cannot be left blank</p>";
                output += "<p>Please enter a state and a zip code</p>";
                output += "</div>";
                output += "<div class='errorClear'></div>";

                // apend the output AFTER the form element
                $("#"+options.containerId).after(output);


            }

        }
    }

};
// end: errorMessage extension

//create the model year
var modelYear = '2009';
var imagePrefix = pathPrefix+"images/";

// distance dropdown
var distanceFrom = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100];

// create months object
var months = [
	{monthNumber: "01", monthName: "January", monthAbbrev: "Jan"},
	{monthNumber: "02", monthName: "February", monthAbbrev: "Feb"},
	{monthNumber: "03", monthName: "March", monthAbbrev: "Mar"},
	{monthNumber: "04", monthName: "April", monthAbbrev: "Apr"},
	{monthNumber: "05", monthName: "May", monthAbbrev: "May"},
	{monthNumber: "06", monthName: "June", monthAbbrev: "Jun"},
	{monthNumber: "07", monthName: "July", monthAbbrev: "Jul"},
	{monthNumber: "08", monthName: "August", monthAbbrev: "Aug"},
	{monthNumber: "09", monthName: "September", monthAbbrev: "Sept"},
	{monthNumber: "10", monthName: "October", monthAbbrev: "Oct"},
	{monthNumber: "11", monthName: "November", monthAbbrev: "Nov"},
	{monthNumber: "12", monthName: "December", monthAbbrev: "Dec"},
];

// years - used for Brochures
var years = [2010,2009,2008,2007,2006,2005];

/************************************************
                HELPER FUNCTIONS

    alphabetical by function name
*************************************************/

function textCounter(field, maxlimit) {
    if (field.value.length > maxlimit)  {// if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
    }
}

function openBrochureLink()
{
    if ($("#download_brochure").hasClass("disabledButton") == false){
		var year = $("#downloadYear").val();
    	var model = $("#downloadModel").val();
    	window.open(getBrochureLink(year,model));
	}else{
		return false;
	}
     
}