// Similar function is defined in makemodelscripts.jsp
// This function should be renamed and all calls to it should
// be updated
// -Ismail Elshareef | April 02, 08
// -last editted mkomine | March 30, 2009
//
if (typeof mm_changeModel != 'function') {
    mm_changeModel = function(formName, makeSelectName, modelSelectName, selectModelMessage){
      alert('OK, this function mm_changeModel() needs to be renamed and all references to it updated. Peace out!');
      var selectObject = eval("document." + formName + "." + makeSelectName);

      var selectObject = eval("document." + formName + "." + makeSelectName);

      if (selectObject.options[selectObject.selectedIndex].value == "")
      {
        mm_resetBrowseMakeModel(formName,makeSelectName,modelSelectName, selectModelMessage);
        return;
      }

      mm_fillModel(formName,modelSelectName,selectObject.selectedIndex, selectModelMessage);
    };
}

function setCookieVal(name, value, expires, path, domain) {

    var secure = false;
    document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");

}


function at_submitMakeZip(formName, makeSelectName, zipSelectName, linkid, rootUrl, model, year, urlExtra){
    var selectObject = eval("document." + formName + "." + zipSelectName);

    if(rootUrl == null) {
        rootUrl = "";
    }
    if(model == null) {
        model = "";
    }
    if(year == null) {
        year = "";
    }

    if (selectObject)
    {
        if (selectObject.value)
        {
            if (selectObject.value.length != 5)
            {
                alert("Your zip code is not valid, it needs at least 5 numbers");
                return false;
            }
            else
            {
                if (isNaN(selectObject.value))
                {
                    alert("The zip code must be a valid number entry");
                    return false;
                }
                var expireDate = new Date();
                expireDate.setTime(expireDate.getTime()+3600*1000*24*360);
		        
                if (isNaN(linkid)) {
                    if (linkid == 'mainindex') {
                        linkid = '2';
                    } else {
                        linkid = '0';
                    }
                }

                var makeSelectObject = eval("document." + formName + "." + makeSelectName);

                if (makeSelectObject.options[makeSelectObject.selectedIndex].value == "")
                {
                    alert("You must select a make");
                    return false;
                }
                var fullmake = makeSelectObject.options[makeSelectObject.selectedIndex].text;
                //alert(fullmake + ' escaped = ' + escape(fullmake));
                if (urlExtra == undefined)
                    {
                    urlExtra = "";
                    var tid;
                    tid = selectObject.id;
                    s_objectID=tid;
                    setTID(tid);
                }
                else
                {
                if (urlExtra)
                    {
                        var tidIndex = urlExtra.indexOf("tid");
                        if (tidIndex > -1 && urlExtra.indexOf("mktid") < 0)
                        {
                            var starIndex = urlExtra.lastIndexOf("*");
                            var tidStr = urlExtra.substring(tidIndex,starIndex);
                            var dotIndex = tidStr.lastIndexOf(".");
                            //get make name
                            tidStr = tidStr.substring(0,dotIndex + 1) + escape(fullmake) + "*";
                            //take out the old tid and put in the new one
                            urlExtra = urlExtra.substring(0,tidIndex) + tidStr + urlExtra.substring(starIndex + 1);
                        }
                    }
                }
                var gotoUrl = rootUrl + "/apps/redirector/RedirectController?rd_contractid=124&rd_app=uvl&rd_link=" + linkid +"&year="+year+"&make="+escape(fullmake)+"&rd_zip="+selectObject.value+"&"+urlExtra;
                if (!rootUrl){ gotoUrl = 'http://www.edmunds.com' + gotoUrl;}
                //document.location.href = gotoUrl;
                //alert("***"+gotoUrl);
                var atWindow = window.open(gotoUrl,"autotrader","width=800,height=480,scrollbars=yes,menubar=yes,toolbar=no,resizable=yes");
                                                                    atWindow.focus();
                // Added this logic from autotraderscript.js to pre-populate the zipcode field on
                // Used VDP. We're not removing the setCookieVal() per Monica's request. -Debby
                if (window.YAHOO && YAHOO.Edmunds && YAHOO.Edmunds.UserTrack) {
                    YAHOO.Edmunds.UserTrack.setZipCookie(selectObject.value);
                }
                //alert("setting at_submitMakeZip zip cookie to "+selectObject.value);
				return false;
            }
        }
    }
    alert("You need to enter a zipcode.");
    return false;
 }


function at_submitZip(formName,zipSelectName, linkid, rootUrl, urlExtra){


    if(rootUrl == null) {
        rootUrl = "";
    }

    var selectObject = eval("document." + formName + "." + zipSelectName);
    if (selectObject)
    {
        if (selectObject.value)
        {
            if (selectObject.value.length != 5)
            {
                alert("Your zip code is not valid, it needs at least 5 numbers");
                return false;
            }
            else
            {
                if (isNaN(selectObject.value))
                {
                    alert("The zip code must be a valid number entry");
                    return false;
                }
                var expireDate = new Date();
                expireDate.setTime(expireDate.getTime()+3600*1000*24*360);
                //alert("at_submitZip setting zip cookie to "+selectObject.value);
                if (isNaN(linkid)) {
                    if (linkid == 'mainindex') {
                        linkid = '1';
                    } else {
                        linkid = '0';
                    }
                }
                 if (urlExtra == undefined)
                    {
                    urlExtra = "";
                    var tid;
                    tid = selectObject.id;
                    s_objectID=tid;
                    setTID(tid);
                }
                var gotoUrl = rootUrl + "/apps/redirector/RedirectController?rd_contractid=125&rd_link=" + linkid + "&rd_app=uvl&rd_zip="+selectObject.value+"&"+urlExtra;
                if (!rootUrl){ gotoUrl = 'http://www.edmunds.com' + gotoUrl;}
                var atWindow = window.open(gotoUrl,"autotrader");
                atWindow.focus();
                return false;
            }
        }
    }
    alert("You need to enter a zipcode.");
    return false;
}

/**
 * Submit style and zipcode to Auto Trader
 *
 * @param formName
 * @param zipSelectName
 * @param urlExtra
 * @param linkid
 * @param styleID
 * @param year
 * @param rootUrl
 * @return  bool
 */
function at_submitStyleZip(formName, zipSelectName, urlExtra, linkid, styleID, year, rootUrl) {
    if(!rootUrl) {
        rootUrl = "";
    }
    var selectObject = document[formName][zipSelectName];
    if (selectObject && selectObject.value) {
        if (selectObject.value.length != 5) {
            alert("Your zip code is not valid, it needs at least 5 numbers");
            return false;
        } else {
            if (isNaN(selectObject.value)) {
                alert("The zip code must be a valid number entry");
                return false;
            }
            var expireDate = new Date();
            expireDate.setTime(expireDate.getTime()+3600*1000*24*360);
            if (YAHOO.Edmunds.UserTrack) {
               YAHOO.Edmunds.UserTrack.setZipCookie(selectObject.value);
            }

            //alert("at_submitZip setting zip cookie to "+selectObject.value);
            if (isNaN(linkid)) {
                if (linkid == 'mainindex') {
                    linkid = '1';
                } else {
                    linkid = '0';
                }
            }
            var gotoUrl = rootUrl + "/apps/redirector/RedirectController?rd_contractid=124&rd_link="+ linkid +"&rd_app=uvl&rd_zip="+selectObject.value+"&styleid="+styleID+"&rd_paramlist=search_type,start_year,end_year&search_type=used&start_year="+year+"&end_year="+year+"&"+urlExtra;
            if (!rootUrl){ gotoUrl = 'http://www.edmunds.com' + gotoUrl;}
            var atWindow = window.open(gotoUrl,"autotrader");
            atWindow.focus();
            return false;
        }
    }
    alert("You need to enter a zipcode.");
    return false;
}

function at_submitBuy(formName,zipSelectName,make, urlExtra, linkid, rootUrl, popup){

    if(rootUrl == null) {
        rootUrl = "";
    }

    var selectObject = eval("document." + formName + "." + zipSelectName);
    if (selectObject)
    {
        if (selectObject.value)
        {
            if (selectObject.value.length != 5)
            {
                alert("Your zip code is not valid, it needs at least 5 numbers");
                return false;
            }
            else
            {
                if (isNaN(selectObject.value))
                {
                    alert("The zip code must be a valid number entry");
                    return false;
                }
                var expireDate = new Date();
                expireDate.setTime(expireDate.getTime()+3600*1000*24*360);
                //alert("at_submitBuy setting zip cookie to "+selectObject.value);
                if (isNaN(linkid)) {
                    if (linkid == 'mainindex') {
                        linkid = '1';
                    } else {
                        linkid = '0';
                    }
                }
                var fullmake=make;
                if (urlExtra)
                {
                    var tidIndex = urlExtra.indexOf("tid");
                    if (tidIndex > -1 && urlExtra.indexOf("mktid") < 0)
                    {
                        var starIndex = urlExtra.lastIndexOf("*");
                        var tidStr = urlExtra.substring(tidIndex,starIndex);
                        var dotIndex = tidStr.lastIndexOf(".");
                        //get make name
                        tidStr = tidStr.substring(0,dotIndex + 1) + escape(fullmake) + "*";
                        //take out the old tid and put in the new one
                        urlExtra = urlExtra.substring(0,tidIndex) + tidStr + urlExtra.substring(starIndex + 1);
                    }
                }
                var gotoUrl = rootUrl + "/apps/redirector/RedirectController?rd_contractid=124&rd_app=uvl&rd_link=" + linkid + "&make="+make+ "&rd_zip="+selectObject.value+"&"+urlExtra;
                if (!rootUrl){ gotoUrl = 'http://www.edmunds.com' + gotoUrl;}
                var atWindow = window.open(gotoUrl,"autotrader","width=800,height=480,scrollbars=yes,menubar=yes,toolbar=no,resizable=yes");
                                                                    atWindow.focus();


                return false;

            }
        }
    }
    alert("You need to enter a zipcode.");
    return false;
}
