
//Opens the new window and also posts the necessary parameters.
function OpenForm(pageName, category, ptype, dealerId) {
    var id = pageName.substring(pageName.lastIndexOf("/") + 1, pageName.length);

    var url = pageName + ".aspx";

    if (partner != "" && partner != null && partner != 'undefined') {
        url = url + "?partner=" + partner;
    }

    if (category != "" && category != null && category != 'undefined') {
        if (url.indexOf('?') != -1) {
            url = url + "&cat=" + category;
        }
        else {
            url = url + "?cat=" + category;
        }
    }

    if (ptype != "" && ptype != null && ptype != 'undefined') {
        if (url.indexOf('?') != -1) {
            url = url + "&ptype=" + ptype;
        }
        else {
            url = url + "?ptype=" + ptype;
        }
    }

    var handle;

    if (dealerId != "" && dealerId != null && dealerId != 'undefined') {
        handle = handle + dealerId
    }
    else {
        handle = handle + id;
    }

    window.open('', handle, 'width=598,height=420,resizable=no,toolbar=no,status=no,scrollbars=yes,location=no,menubar=no,directories=no,top=20,left=20,');

    $('dealerIdHidden').value = dealerId;

    var postForm = $('postForm');
    postForm.target = handle;
    postForm.action = url;
    postForm.submit();
}


//Opens the new window.
function OpenPrintForm(pageName, category, ptype) {
    var url = pageName + ".aspx";

    if (category != "" && category != null && category != 'undefined') {
        url = url + "?cat=" + category;
    }

    if (ptype != "" && ptype != null && ptype != 'undefined') {
        url = url + "&ptype=" + ptype;
    }

    window.open(url, 'printForm', 'width=600,height=450,resizable=no,toolbar=no,status=no,scrollbars=yes,location=no,menubar=no,directories=no,top=200,left=320,');
}

//Opens the genuine demo
function OpenGenuineDemo(strURL) {
    window.open(strURL, 'GenuineDemo', 'width=768,height=557,resizable=no,toolbar=no,status=no,scrollbars=no,location=no,menubar=no,directories=no,top=20,left=20');
}

//Redirects to the specified page.
function SubmitForm(pageName, category, ptype, oType) {
    var url = pageName + ".aspx";

    if (partner != "" && partner != null && partner != 'undefined') {
        url = url + "?partner=" + partner;
    }

    if (category != "" && category != null && category != 'undefined') {
        if (url.indexOf('?') != -1) {
            url = url + "&cat=" + category;
        }
        else {
            url = url + "?cat=" + category;
        }
    }

    if (ptype != "" && ptype != null && ptype != 'undefined') {
        if (url.indexOf('?') != -1) {
            url = url + "&ptype=" + ptype;
        }
        else {
            url = url + "?ptype=" + ptype;
        }
    }

    location.href = url;
}

function ChangeImage(leftImageId, leftImage, rightImageId, rightImage, middleColumnId, middleColumnClass) {
    // Locate Retailer Button Rollovers
    document.getElementById(leftImageId).src = "../static/images/" + leftImage;
    document.getElementById(rightImageId).src = "../static/images/" + rightImage;
    document.getElementById(middleColumnId).className = middleColumnClass;
}

function Popup(pageName) {
    var handle = 'popUpWindow' + pageName;

    window.open('../static/images/clear.gif', handle, 'width=600,height=400,resizable=no,toolbar=no,status=no,scrollbars=no,location=no,menubar=no,directories=no,top=200,left=320,');

    var form = $('postForm');
    form.action = pageName + ".aspx";
    postForm.target = handle;
    form.submit();
}

// This method takes the elementId and returns the element
function $(elementId) {
    if (document.getElementById(elementId)) {
        return document.getElementById(elementId);
    }
    else if (document.getElementById(otherFormId + elementId)) {
        return document.getElementById(otherFormId + elementId);
    }
}

function ValidateAreaSelection(source, arguments) {
    var locateContentId = "ctl00_MainContentPlaceholder_LocatePageContent_";
    var areaList = document.getElementById(locateContentId + "areaDropDownList");
    if (null != areaList) {
        var iValue = new Number(areaList.selectedIndex);
        if (iValue == 0) {
            document.getElementById(locateContentId + "divResults").className = 'hide';
            document.getElementById(locateContentId + "divResultsThankYou").className = 'hide';
        }
        arguments.IsValid = (iValue > 0);
    }
    else {

        arguments.IsValid = false;
    }
}

function expandProdInfo(image, divId) {
    if (image.src.indexOf("expand") > -1) {
        image.src = "../images/arrow_collapse.gif";
        document.getElementById(divId).style.display = "";
    }
    else {
        image.src = "../images/arrow_expand.gif";
        document.getElementById(divId).style.display = 'none';
    }

}
function invertOnMouseOver(button) {
    var changeThisButton = document.getElementById("togglebutton" + button);

    if (changeThisButton.title == "Expand") {
        changeThisButton.src = "../Static/Images/arrow_expand.gif";
    }
    else {
        changeThisButton.src = "../Static/Images/arrow_collapse.gif";
    }
}

// On mouse out, returns the expand or collapse button to the regular image.
// Requires the Deal ID represented by the row.
function revertOnMouseOut(button) {
    var changeThisButton = document.getElementById("togglebutton" + button);

    if (changeThisButton.title == "Expand") {
        changeThisButton.src = "../Static/Images/arrow_expand.gif";
    }
    else {
        changeThisButton.src = "../Static/Images/arrow_collapse.gif";
    }
}

// On mouse over on the paging buttons, replaces with the inverted image
function invert(image, func) {
    if (image.alt != "Disabled") {
        if (func == "first") {
            image.src = "../Static/Images/bt_begin_over.gif";
        }
        else if (func == "prev") {
            image.src = "../Static/Images/bt_prev_over.gif";
        }
        else if (func == "next") {
            image.src = "../Static/Images/bt_next_over.gif";
        }
        else if (func == "last") {
            image.src = "../Static/Images/bt_end_over.gif";
        }
    }
}


// Toggles whether or not a particular dealer is expanded.
// Requires the Dealer ID represented by the row.
function toggle(row) {
    var toggleThisRow = document.getElementById("Expansion" + row);

    if (toggleThisRow.style.display == 'none') {
        toggleThisRow.style.display = "";
    }
    else {
        toggleThisRow.style.display = 'none';
    }
}

// Changes the row expanding button to a row collapsing button or vice versa.
// Requires the Dealer ID represented by the row.
function changeImageSet(image) {
    if (null != image) {
        if (image.src.indexOf("expand") > -1) {
            image.src = "../Static/Images/arrow_collapse.gif";
            if (image.id == "imgExpandAll") {
                image.title = "Collapse All";
            }
            else {
                image.title = "Collapse";
            }
        }
        else {
            image.src = "../Static/Images/arrow_expand.gif";
            if (image.id == "imgExpandAll") {
                image.title = "Expand All";
            }
            else {
                image.title = "Expand";
            }
        }
    }

    if (null != document.getElementById("divMessage")) {
        document.getElementById("divMessage").style.display = 'none'
    }
}

function DefaultButton_ClientClick() {
    document.getElementById("divMessage").style.display = 'none';
}

// On mouse out of the paging buttons, reverts to the original image
function revert(image, func) {
    if (image.alt != "Disabled") {
        if (func == "first") {
            image.src = "../Static/Images/bt_begin.gif";
        }
        else if (func == "prev") {
            image.src = "../Static/Images/bt_prev.gif";
        }
        else if (func == "next") {
            image.src = "../Static/Images/bt_next.gif";
        }
        else if (func == "last") {
            image.src = "../Static/Images/bt_end.gif";
        }
    }
}

function EditProductClick(txtBoxClientId, linkMessage1, linkMessage2, linkMessage3, linkMessage4, btnMessage1, btnMessage2, btnMessage3, btnMessage4,  btnEditClientId, productId) {
    if (null != document.getElementById("divMessage")) {
        document.getElementById("divMessage").style.display = 'none'
    }

    document.getElementById(txtBoxClientId).disabled = false;

    document.getElementById(btnMessage1).disabled = false;
    document.getElementById(btnMessage2).disabled = false;
    document.getElementById(btnMessage3).disabled = false;
    document.getElementById(btnMessage4).disabled = false;

    document.getElementById(linkMessage1).href = "javascript:void(0);"
    document.getElementById(linkMessage2).href = "javascript:void(0);"
    document.getElementById(linkMessage3).href = "javascript:void(0);"
    document.getElementById(linkMessage4).href = "javascript:void(0);"

    document.getElementById(linkMessage1).onclick =
        function() {
            ShowMessage(document.getElementById(btnMessage1).getAttribute('message'));
        }
    document.getElementById(linkMessage2).onclick =
        function() {
            ShowMessage(document.getElementById(btnMessage2).getAttribute('message'));
        }
    document.getElementById(linkMessage3).onclick =
        function() {
            ShowMessage( document.getElementById(btnMessage3).getAttribute('message'));
        }
    document.getElementById(linkMessage4).onclick =
        function() {
            ShowMessage(document.getElementById(btnMessage4).getAttribute('message'));
        }

    document.getElementById(btnEditClientId).src = "../Static/Images/btn_save.gif";
    document.getElementById(btnEditClientId).title = "Save";

    document.getElementById(btnEditClientId).onmouseover =
        function() {
            document.getElementById(btnEditClientId).src = "../Static/Images/btn_save_over.gif";
        }

    document.getElementById(btnEditClientId).onmouseout =
        function() {
            document.getElementById(btnEditClientId).src = "../Static/Images/btn_save.gif";
        }

    document.getElementById(btnEditClientId).onclick =
        function() {
            var controlIds = new Array(txtBoxClientId, linkMessage1, linkMessage2, linkMessage3, linkMessage4, btnMessage1, btnMessage2, btnMessage3, btnMessage4,  btnEditClientId, productId);
            PageMethods.SaveButtonClick(productId, document.getElementById(txtBoxClientId).value,  document.getElementById(btnMessage1).checked, document.getElementById(btnMessage2).checked, document.getElementById(btnMessage3).checked, document.getElementById(btnMessage4).checked, EditSuccess, EditFailed, controlIds);
        }
}

function EditSuccess(results, controlIds) {
    if (null != document.getElementById("divMessage")) {
        document.getElementById("divMessage").style.display = "";
        document.getElementById("divMessage").innerHTML = "Product has been updated successfully.";
    }

    var btnAction = document.getElementById(controlIds[9]);

    btnAction.src = "../Static/Images/btn_edit_gray.gif";
    btnAction.title = "Edit";

    document.getElementById(controlIds[0]).disabled = true;

    document.getElementById(controlIds[5]).disabled = true;
    document.getElementById(controlIds[6]).disabled = true;
    document.getElementById(controlIds[7]).disabled = true;
    document.getElementById(controlIds[8]).disabled = true;

    document.getElementById(controlIds[1]).removeAttribute("href");
    document.getElementById(controlIds[2]).removeAttribute("href");
    document.getElementById(controlIds[3]).removeAttribute("href");
    document.getElementById(controlIds[4]).removeAttribute("href");

    document.getElementById(controlIds[1]).onclick =
        function() {
            void (0);
        }
    document.getElementById(controlIds[2]).onclick =
        function() {
            void (0);
        }
    document.getElementById(controlIds[3]).onclick =
        function() {
            void (0);
        }
    document.getElementById(controlIds[4]).onclick =
        function() {
            void (0);
        }

    btnAction.onmouseover =
        function() {
            btnAction.src = "../Static/Images/btn_edit_gray_over.gif";
        }

    btnAction.onmouseout =
        function() {
            btnAction.src = "../Static/Images/btn_edit_gray.gif";
        }

    btnAction.onclick = function() { EditProductClick(controlIds[0], controlIds[1], controlIds[2], controlIds[3], controlIds[4], controlIds[5], controlIds[6], controlIds[7], controlIds[8],  controlIds[9], controlIds[10]); }
}

function EditFailed(results, controlIds) {
    location.href = '../Error/error.aspx';
}

function ShowMessage( MessageId) {
    var url = "message.aspx?messageId=" + MessageId;
    var handle = 'window' + MessageId;

    window.open(url, handle, 'width=425,height=180,resizable=no,toolbar=no,status=no,scrollbars=no,location=no,menubar=no,directories=no,top=300,left=420,');
}

function CheckEmail(checkEmail) {
    var tdEmail = document.getElementById("tdEmail");

    if (checkEmail.checked) {
        tdEmail.className = "label";
    }
    else {
        var txtBoxEmail = document.getElementById("textBoxEmail");
        if (null != txtBoxEmail) {
            txtBoxEmail.value = "";
        }
        tdEmail.className = "noLabel";
    }

}

function CheckSMS(checkSMS) {
    var SMSTableRow = document.getElementById("SMSTableRow");

    if (checkSMS.checked) {
        SMSTableRow.style.display = "";
    }
    else {
        var txtBoxSMS = document.getElementById("textBoxSMS");
        if (null != txtBoxSMS) {
            txtBoxSMS.value = "";
        }
        SMSTableRow.style.display = "none";
    }
}

function ChooseOneCheckBox(src, args) {
    debugger;
    args.IsValid = true;
    var CheckEmail = document.getElementById("checkEmail").checked;
    var CheckSMS = document.getElementById("checkSMS").checked;
    if (!CheckEmail && !CheckSMS) {
        args.IsValid = false;
    }
}

function ShowDealerLogo(dealerId) {
    window.open("dealerlogo.aspx?dealerId=" + dealerId, '', 'width=300,height=200,resizable=no,toolbar=no,status=no,scrollbars=yes,location=no,menubar=no,directories=no,top=200,left=320,');
}

function ToggleAll() {
    var imgExpandAll = document.getElementById("imgExpandAll");

    if (null != imgExpandAll) {
        if (imgExpandAll.src.indexOf("expand") > -1) {
            for (dealer in dealerId) {
                var toggleThisRow = document.getElementById("Expansion" + dealerId[dealer]);
                var toggleButton = document.getElementById("togglebutton" + dealerId[dealer]);
                if (null != toggleThisRow && null != toggleButton) {
                    toggleThisRow.style.display = "none";
                    toggleButton.src = "../Static/Images/arrow_expand.gif";
                    toggleButton.title = "Expand";
                }
            }
        }
        else {
            for (dealer in dealerId) {
                var toggleThisRow = document.getElementById("Expansion" + dealerId[dealer]);
                var toggleButton = document.getElementById("togglebutton" + dealerId[dealer]);
                if (null != toggleThisRow && null != toggleButton) {
                    toggleThisRow.style.display = "";
                    toggleButton.src = "../Static/Images/arrow_collapse.gif";
                    toggleButton.title = "Collapse";
                }
            }
        }
    }
}

function DisableDropDown(checkBox, dropdown1, dropdown2, dropdown3, dropdown4) {
    var check = checkBox.checked;
    if(!check)
    {
        document.getElementById(dropdown1).disabled = "disabled";
        document.getElementById(dropdown2).disabled = "disabled";
        document.getElementById(dropdown3).disabled = "disabled";
        document.getElementById(dropdown4).disabled = "disabled";
    }
    else{
        document.getElementById(dropdown1).disabled = false;
        document.getElementById(dropdown2).disabled = false;
        document.getElementById(dropdown3).disabled = false;
        document.getElementById(dropdown4).disabled = false;
    }
}

function ValidateScenarios(checkbox1, checkbox2, checkbox3, checkbox4, checkbox5) {
    var count = 0;
    if (document.getElementById(checkbox1).checked) {
        count++;
    }
    if (document.getElementById(checkbox2).checked) {
        count++;
    }
    if (document.getElementById(checkbox3).checked) {
        count++;
    }
    if (document.getElementById(checkbox4).checked) {
        count++;
    }
    if (document.getElementById(checkbox5).checked) {
        count++;
    }

    if (count >= 3) {
        return true;
    }
    else {
        alert('Atleast 3 scenarios should be selected');
        return false;
    }
}

//function clickButton(e, buttonid) {
//    var evt = e ? e : window.event;
//    var bt = document.getElementById(buttonid);
//    if (bt) {
//        if (evt.keyCode == 13) {
//            bt.click();
//            return false;
//        }
//    }
//}