﻿function LoadZip() {
    var zipcode = "";
    var ResultZipCode = "Enter Zip";
    $.ajax({
        type: "POST",
        //url: "http://www.pscars.com/zipcode_ip/ZipCode_IP.asmx/OnlyZipCode_ALL",
        //url: "http://testch.pscars.com/wsZipCode-IP-CH/ZipCode_IP.asmx/OnlyZipCode_ALL",
        //url: "http://localhost/PSCars/wsZipCode-IP-CH/ZipCode_IP.asmx/OnlyZipCode_ALL",
        url: "http://www.pscars.com/wsZipCode-IP-CH/ZipCode_IP.asmx/OnlyZipCode_ALL",
        data: "{'IpAddress':'xxxx'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        //cache: false,
        success: function (data) {
            zipcode = data.d;
            var zipch = zipcode.split(":");

            if (zipch[1] == "") {
                ResultZipCode = "Enter Zip";
            }
            else {
                ResultZipCode = zipch[1];
            }
            return ResultZipCode;
        },
        error: function () { return ResultZipCode; }
    });

	//finalZipCode=ResultZipCode== "Enter Zip"? "90210":ResultZipCode;
	
    return ResultZipCode;
    //return "90210";
}

function ZipCodeIndex() {
    var ZipCode = LoadZip();

    $("#usedCarZip-m").val(ZipCode);
    $("#usedCarZipMid").val(ZipCode);
    $("#usedCarZip").val(ZipCode);
	$("#usedZip").val(ZipCode);

    $('input[name=usedCarZip-m]').focus(function () {
        if (this.value == 'Enter Zip') {
            this.value = '';
        }
    });

    $('input[name=usedCarZipMid]').focus(function () {
        if (this.value == 'Enter Zip') {
            this.value = '';
        }
    });

    $('input[name=usedCarZip]').focus(function () {
        if (this.value == 'Enter Zip') {
            this.value = '';
        }
    });
}

function ZipCode() {
    var ZipCode = LoadZip();

    $("#usedCarZip-m").val(ZipCode);
    $("#usedCarZip").val(ZipCode);
	$("#usedZip").val(ZipCode);

    $('input[name=usedCarZip-m]').focus(function () {
        if (this.value == 'Enter Zip') {
            this.value = '';
        }
    });

    $('input[name=usedCarZip]').focus(function () {
        if (this.value == 'Enter Zip') {
            this.value = '';
        }
    });
}

function ZipCodeNewCarLoad() {
    var ZipCode = LoadZip();
    $("#usedCarZip").val(ZipCode);
	$("#usedZip").val(ZipCode);

    $('#usedCarZip').focus(function () {
        if (this.value == 'Enter Zip') {
            this.value = '';
        }
    });
}

//function ZipCodeNewCar() {
//    var ZipCode = LoadZip();

//    //alert(ZipCode);

//    $("#usedCarZip").val(ZipCode);

//    $('#usedCarZip').focus(function () {
//        if (this.value == 'Enter Zip') {
//            this.value = '';
//        }
//    });

//    strUrl = location.href;

//    if (strUrl.indexOf("?") == -1) //Not found
//    {
//        window.location.href = strUrl + "?zip=" + ZipCode;
//    }
//    else {
//        window.location.href = strUrl + "&zip=" + ZipCode;
//    }
//}

function ZipCodeNewCar() {
    var zipcode = "";
    var ResultZipCode = "Enter Zip";
    $.ajax({
        type: "POST",
        url: "http://www.pscars.com/wsZipCode-IP-CH/ZipCode_IP.asmx/OnlyZipCode_ALL",
        data: "{'IpAddress':'xxxx'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function (data) {
            zipcode = data.d;
            var zipch = zipcode.split(":");

            if (zipch[1] == "") {
                ResultZipCode = "Enter Zip";
            }
            else {
                ResultZipCode = zipch[1];
            }
            //return ResultZipCode;
            $("#usedCarZip").val(ResultZipCode);
			$("#usedZip").val(ResultZipCode);

            $('#usedCarZip').focus(function () {
                if (this.value == 'Enter Zip') {
                    this.value = '';
                }
            });

            strUrl = location.href;

            if (strUrl.indexOf("?") == -1) //Not found
            {
                window.location.href = strUrl + "?zip=" + ResultZipCode;
            }
            else {
                window.location.href = strUrl + "&zip=" + ResultZipCode;
            }
        },
        error: function () { return ResultZipCode; }
    });
}

//Show Error
function AjaxFailed(result) {
    //alert(result.status + ' ' + result.statusText);
}
