﻿var ddlWeightId = "ctl00_ContentPlaceHolder1_MovingFormControl1_ddlWeight";
var movingOriginId = "ctl00_ContentPlaceHolder1_MovingFormControl1_txtMovingOrigin";
var movingDestinationId = "ctl00_ContentPlaceHolder1_MovingFormControl1_txtMovingDestination";
var ddlMoveMonthId = "ctl00_ContentPlaceHolder1_MovingFormControl1_ddlMoveMonth";
var ddlMoveDayId = "ctl00_ContentPlaceHolder1_MovingFormControl1_ddlMoveDay";
var ddlVehicleTypeId = "ctl00_ContentPlaceHolder1_ddlVehicleType";
var ddlVehicleTypeId2 = "ctl00_ContentPlaceHolder1_ddlVehicleType";
var ddlVehicleConditionId = "ctl00_ContentPlaceHolder1_ddlVehicleCondition";
var ddlVehicleConditionId2 = "ctl00_ContentPlaceHolder1_ddlVehicleCondition2";
var txtVehicleMakeId = "ctl00_ContentPlaceHolder1_txtVehicleMake";
var txtVehicleModelId = "ctl00_ContentPlaceHolder1_txtVehicleModel";
var ddlVehicleYearId = "ctl00_ContentPlaceHolder1_ddlVehicleYear";
var txtVehicleMakeId2 = "ctl00_ContentPlaceHolder1_txtVehicleMake2";
var txtVehicleModelId2 = "ctl00_ContentPlaceHolder1_txtVehicleModel2";
var ddlVehicleYearId2 = "ctl00_ContentPlaceHolder1_ddlVehicleYear2";
var chkOriginStorageId = "ctl00_ContentPlaceHolder1_chkOriginStorage";
var chkDestStorageId = "ctl00_ContentPlaceHolder1_chkDestStorage";
var txtPhoneConfirmId = "ctl00_ContentPlaceHolder1_txtPhoneConfirm";
var txtVehicleYearId = "ctl00_ContentPlaceHolder1_ddlVehicleYear";

function ValidateCarMovingForm(webSiteId, pageName)
{
    //For server validation debugging
    // return true;

    //   if the user doesn't have cookies enabled - Postbacking to the same page to show the user the cookie message again
    showPopUp = false;
    debugger;
    if (!CreateCookie()) {
        __doPostBack('', '');
        return;
    }

    var formValid = true;

    if ($("#" + txtVehicleMakeId).val() == "")
    {
        ShowErrorMsg("Please specify the maker of the car that you want to move(such as Honda, Chevrolet,...)", -110, 140, "lblVehicleMake", txtVehicleMakeId);
        formValid = false;
    }

    if ($("#" + txtVehicleModelId).val() == "")
    {
        if (formValid)
        {
            ShowErrorMsg("Please specify the model of the car that you want to move(such as Accord, Malibu,...)", -75, 140, "lblVehicleModel", txtVehicleModelId);
            formValid = false;
        }
    }

    if ($("#" + ddlVehicleYearId).val() == "0")
    {
        if (formValid)
        {
            ShowErrorMsg("Please specify the year of the vehicle you want to move", -20, 140, "lblVehicleYear", txtVehicleYearId);
            formValid = false;
        }
    }

    if (!formValid)
    {
        var auditTrailData = "Vehicle Type:" + document.getElementById(ddlVehicleTypeId).options[document.getElementById(ddlVehicleTypeId).selectedIndex].innerHTML + ";";
        auditTrailData += "Vehicle Make:" + $("#" + txtVehicleMakeId).val() + ";";
        auditTrailData += "Vehicle Model:" + $("#" + txtVehicleModelId).val() + ";";
        auditTrailData += "Vehicle Year:" + document.getElementById(ddlVehicleYearId).options[document.getElementById(ddlVehicleYearId).selectedIndex].value + ";";
        auditTrailData += "Vehicle Condition:" + document.getElementById(ddlVehicleConditionId).options[document.getElementById(ddlVehicleConditionId).selectedIndex].innerHTML + ";";
        if (document.getElementById("move_Car2").style.display == "block")
        {
            auditTrailData += "Vehicle Type2:" + document.getElementById(ddlVehicleTypeId2).options[document.getElementById(ddlVehicleTypeId2).selectedIndex].innerHTML + ";";
            auditTrailData += "Vehicle Make2:" + $("#" + txtVehicleMakeId2).val() + ";";
            auditTrailData += "Vehicle Model2:" + $("#" + txtVehicleModelId2).val() + ";";
            auditTrailData += "Vehicle Year2:" + document.getElementById(ddlVehicleYearId2).options[document.getElementById(ddlVehicleYearId2).selectedIndex].value + ";";
            auditTrailData += "Vehicle Condition2:" + document.getElementById(ddlVehicleConditionId2).options[document.getElementById(ddlVehicleConditionId2).selectedIndex].innerHTML + ";";
        }
        AjaxFunctions.AddFormUnsuccessAuditTrail(auditTrailData, pageName, webSiteId);
        return false;
    } else
    {
        return true;
    }

    return true;
}

function ValidateStorageMovingForm(webSiteId, pageName)
{
    //For server validation debugging
    //  return true;

    if (!$("#" + chkOriginStorageId).attr("checked") && !$("#" + chkDestStorageId).attr("checked"))
    {
        ShowErrorMsg("Check the place or places where you need storage", -100, 210, "lblStorageOrigin", chkOriginStorageId);
        AjaxFunctions.AddFormUnsuccessAuditTrail("No Data", pageName, webSiteId);
        return false;
    }
    return true;
}

var pName;
var wSiteId;

function ValidatePhoneConfirmation(webSiteId, pageName) {
    pName = pageName;
    wSiteId = webSiteId;
    AjaxFunctions.IsValidPhoneNumber(document.getElementById(txtPhoneConfirmId).value, ValidatePhoneCompleted);
    return false; //false is returned to avoid automatic postback of the page
}

function ValidatePhoneCompleted(result)
{
    if (result) {
        __doPostBack('', 'ConfirmedPhone');
    }
    else {
        if (document.getElementById(txtPhoneConfirmId).value == "")
        {
            ShowErrorMsg("You forgot to enter your phone number", -55, 480, "lblMyPhone", txtPhoneConfirmId);
            debugger;
            AjaxFunctions.AddFormUnsuccessAuditTrail("You forgot to enter your phone number", pName, wSiteId)
        } else {
            ShowErrorMsg("The phone number you entered is not valid", -70, 480, "lblMyPhone", txtPhoneConfirmId);
            AjaxFunctions.AddFormUnsuccessAuditTrail("The phone number you entered is not valid", pName, wSiteId)
        }
    }
}

// displayed the popup winodw not more than twice
function OpenExitPopup(setFocusToControl, experimentId, variantId, pageName, websiteId) {
debugger
    var cookie = $.cookie('exit-popup');
    // If the exit pop up has already poped up then do not show it again
    if (cookie && cookie == "2")
    {
        return;
    }
    else if (cookie && cookie == "1") // Second time popup
    {
        if (experimentId && variantId)
        {
            AjaxFunctions.UpdateVariantsUsage(experimentId, variantId);
        }
        // If the selected variant that was chosen is to display the popup the show it otherwise skip it
        if (exitPopupYesVariantId == variantId)
        {
            if (document.getElementById('message_bg').style.display != 'block')
            {
                $.cookie('exit-popup', '2', { path: '/movers' });
                document.getElementById(setFocusToControl).focus();
                ShowPop('message_bg', pageName, websiteId);
            }
        }
    }
    else    // First time show the popup and update the variant usage.
    {
        if (experimentId && variantId)
        {
            AjaxFunctions.UpdateVariantsUsage(experimentId, variantId);
        }
        if (exitPopupYesVariantId == variantId) {
            $.cookie('exit-popup', '1', { path: '/movers' });
            document.getElementById(setFocusToControl).focus();
            ShowPop('message_bg', pageName, websiteId);
        }
    }
}

// Check if the zipcode changed from the first page and if so take the required actions
function CheckZipCodeChange(control)
{
    if (control.value != '')
    {
        AjaxFunctions.CheckZipCodeChange(control.value);
    }
}
