//variables for googlemap
var gMapLoaded = false;

function limitText(txtArea, maxChars, infoID, text) {
    var obj = document.getElementById(infoID);
    if (txtArea.value.length > maxChars) {
        txtArea.value = txtArea.value.substring(0, maxChars);
    }
    obj.innerHTML = maxChars - txtArea.value.length + " " + text;
}


function checkMinChars(id) {
    obj = document.getElementById(id);
    if (obj.value.length < 3) {
        alert("Minimum search input is 3 charaters");
        return false;
    }
    return true;
}

function checkall(obj, opt) {
    var id = "";
    switch (opt) {
        case 1:
            id = "DNNInfo_MessagePanel";
            break;
        case 2:
            id = "DNNInfo_CountryPanel";
            break;
        case 3:
            id = "DNNInfo_StatePanel";
            break;
        case 4:
            id = "DNNInfo_CityPanel";
            break;
        case 5:
            id = "DNNInfo_CategoryPanel";
            break;
        case 6:
            id = "DNNInfo_ClassifiedPanel";
            break;
        case 7:
            id = "DNNInfo_ClassificationPanel";
            break;
    }

    var divObj = document.getElementById(id);
    if (divObj) {
        if (obj.checked) {
            for (i = 0; i < divObj.getElementsByTagName("input").length; i++) {
                if (divObj.getElementsByTagName("input")[i].type == "checkbox") {
                    if (divObj.getElementsByTagName("input")[i].id != "checker") {
                        divObj.getElementsByTagName("input")[i].checked = true;
                    }
                }
            }
        } else {
            for (i = 0; i < divObj.getElementsByTagName("input").length; i++) {
                if (divObj.getElementsByTagName("input")[i].type == "checkbox") {
                    if (divObj.getElementsByTagName("input")[i].id != "checker") {
                        divObj.getElementsByTagName("input")[i].checked = false;
                    }
                }
            }
        }
    }
}

function BuildRatings(divid, p, id) {
    $("#" + divid).html("<img src='" + spath + "/DesktopModules/DNNInfo_Classifieds/images/loading.gif'/>");
    $.ajax({
        cache: false,
        url: spath + "/DesktopModules/DNNInfo_Classifieds/RatingHandler.ashx?divid=" + divid + "&p=" + p + "&id=" + id,
        data: "{}",
        contentType: "text/html; charset=utf-8",
        dataType: "html",
        error: function(xhr, status, error) {
            //alert the error if needed
            //alert(xhr.responseText);
        },
        success: function(msg) {
            $("#" + divid).html(msg);
        }
    });
}


//ctype 0=generic, 1=cars, 2=properties
//type 1= classifiedlist, 2=customclassifiedlist
//divid = div client id for showing youtube
//ycid = youtube classifiedid
//yvp = youtube paging
//mid = module id
function BuildYoutubeVideos(ctype, type, divid, yvp, ycid, mid) {
    $("#" + divid).html("<img src='" + spath + "/DesktopModules/DNNInfo_Classifieds/images/loading.gif'/>");
    $.ajax({
        cache: false,
        url: spath + "/DesktopModules/DNNInfo_Classifieds/YouTubeHandler.ashx?ctype=" + ctype + "&type=" + type + "&divid=" + divid + "&ycid=" + ycid + "&yvp=" + yvp + "&mid=" + mid,
        data: "{}",
        contentType: "text/html; charset=utf-8",
        dataType: "html",
        error: function(xhr, status, error) {
            //alert the error if needed
            //alert(xhr.responseText);
        },
        success: function(msg) {
            $("#" + divid).html(msg);
        }
    });
}

//type 1= classifiedlist, 2=customclassifiedlist
//divid = div client id for showing youtube
//yvcode = youtube code
//yvid = youtube video id
//mid = module id
function LoadYouTubeVideo(type, divid, yvcode, yvid, mid) {
    $(document).ready(function() {
        $("#" + divid).html("<img src='" + spath + "/DesktopModules/DNNInfo_Classifieds/images/loading.gif' />");
        $.ajax({
            cache: false,
            url: spath + "/DesktopModules/DNNInfo_Classifieds/YouTubeVideoHandler.ashx?type=" + type + "&divid=" + divid + "&yvcode=" + yvcode + "&yvid=" + yvid + "&mid=" + mid,
            data: "{}",
            contentType: "text/html; charset=utf-8",
            dataType: "html",
            error: function(xhr, status, error) {
                //alert the error if needed
                //alert(xhr.responseText);
            },
            success: function(msg) {
                $("#" + divid).html(msg);
            }
        });
    });
}

function CleanUp(obj, mid, iframeurl, filename) {
    obj.disabled = true;
    $("#divAjaxLoading_" + mid).removeClass("DNNInfo_Hidden");
    $("#divProgressBox_" + mid).removeClass("DNNInfo_Hidden");
    $("#spanAjaxLoading_" + mid).html("<img src='" + spath + "/DesktopModules/DNNInfo_Classifieds/images/ajax.gif'/>");
    objiframe = document.getElementById("iframeProgress_" + mid);
    objiframe.src = iframeurl;
    setTimeout("GetFileInfo(" + mid + ",'" + spath + "','" + filename + "')", 1000);
}


function GetFileInfo(mid, path, filename) {
    $.ajax({
        cache: false,
        url: path + "/DesktopModules/DNNInfo_Classifieds/FileHandler.ashx?f=" + filename,
        data: "{}",
        contentType: "text/html; charset=utf-8",
        dataType: "html",
        error: function(xhr, status, error) {
            //alert the error if needed
            //alert(xhr.responseText);
        },
        success: function(msg) {
            if (msg == "1") {
                $("#divAjaxLoading_" + mid).addClass("DNNInfo_Hidden");
                $("#btnCleanUp_" + mid).addClass("DNNInfo_Hidden");
                $("#divAjaxComplete_" + mid).removeClass("DNNInfo_Hidden");
                $("#divAjaxComplete_" + mid).addClass("DNNInfo_Block");
            } else {
                setTimeout("GetFileInfo(" + mid + ",'" + path + "','" + filename + "')", 1000);
            }

        }
    });
}

function SyncClients(obj, mid, iframeurl) {
    obj.disabled = true;
    objiframe = document.getElementById("iframeProgress_" + mid);
    objiframe.src = iframeurl;
}

function resetDD(id, txtID) {
    $("#" + id).val('');
    $("#" + txtID).val('');
}

function LoadLocation(typeID, portalID, ddTopParent, ddParent, ddChild, hiddenStateID, title) {
    var ddTopID = 0;
    var ddID = 0;
    var querystring = "";
    if (ddTopParent != "") {
        objDDTopParent = document.getElementById(ddTopParent);
        if (objDDTopParent) { ddTopID = objDDTopParent.options[objDDTopParent.selectedIndex].value; }
    }
    objDDParent = document.getElementById(ddParent);
    if (objDDParent) { ddID = objDDParent.options[objDDParent.selectedIndex].value; }
    objDDChild = document.getElementById(ddChild);
    objHiddenState = document.getElementById(hiddenStateID);
    if (objDDChild) {
        if (objDDChild) {
            for (var i = objDDChild.options.length; i >= 0; i--) {
                objDDChild.remove(i);
            }
        }
        $("#" + ddChild).append("<option value=''>" + title + "</option>");
        if (ddID > 0) {
            switch (typeID) {
                case 1:
                    querystring = "?portalID=" + portalID + "&ctid=" + ddID;
                    break;
                case 2:
                    querystring = "?portalID=" + portalID + "&ctid=" + ddTopID + "&sid=" + ddID;
                    break;
            }

            $.ajax({
                cache: false,
                url: spath + "/DesktopModules/DNNInfo_Classifieds/LocationHandler.ashx" + querystring,
                data: "{}",
                contentType: "text/html; charset=utf-8",
                dataType: "html",
                error: function(xhr, status, error) {
                    //alert the error if needed
                    //alert(xhr.responseText);
                },
                success: function(msg) {
                    $("#" + ddChild).append(msg);
                }
            });
        }
    }
}


function LoadHiddenLocation(typeID, portalID, countryID, ddParent, ddChild, hiddenStateID, title) {
    var ddTopID = countryID;
    var ddID = 0;
    var querystring = "";

    objDDParent = document.getElementById(ddParent);
    if (objDDParent) { ddID = objDDParent.options[objDDParent.selectedIndex].value; }
    objDDChild = document.getElementById(ddChild);
    objHiddenState = document.getElementById(hiddenStateID);

    if (objDDChild) {
        if (objDDChild) {
            for (var i = objDDChild.options.length; i >= 0; i--) {
                objDDChild.remove(i);
            }
        }
        $("#" + ddChild).append("<option value=''>" + title + "</option>");
        if (ddID > 0) {
            switch (typeID) {
                case 1:
                    querystring = "?portalID=" + portalID + "&ctid=" + ddID;
                    break;
                case 2:
                    querystring = "?portalID=" + portalID + "&ctid=" + ddTopID + "&sid=" + ddID;
                    break;
            }

            $.ajax({
                cache: false,
                url: spath + "/DesktopModules/DNNInfo_Classifieds/LocationHandler.ashx" + querystring,
                data: "{}",
                contentType: "text/html; charset=utf-8",
                dataType: "html",
                error: function(xhr, status, error) {
                    //alert the error if needed
                    //alert(xhr.responseText);
                },
                success: function(msg) {
                    $("#" + ddChild).append(msg);
                }
            });
        }
    }
}

function LoadStatistic(portalID, mid, typeID, titleType, startDate, endDate, noOfRecords, classifiedType) {
    $("#spanAjaxStatistic_" + mid).html("<img src='" + spath + "/DesktopModules/DNNInfo_Classifieds/images/loading.gif'/>");
    $.ajax({
        cache: false,
        url: spath + "/DesktopModules/DNNInfo_Classifieds/StatisticHandler.ashx?portalID=" + portalID + "&mid=" + mid + "&typeID=" + typeID + "&titleType=" + titleType + "&startDate=" + startDate + "&endDate=" + endDate + "&noOfRecords=" + noOfRecords + "&ctype=" + classifiedType,
        data: "{}",
        contentType: "text/html; charset=utf-8",
        dataType: "html",
        error: function(xhr, status, error) {
            //alert the error if needed
            //alert(xhr.responseText);
        },
        success: function(msg) {
            $("#spanAjaxStatistic_" + mid).addClass("DNNInfo_Hidden");
            $("#divAjaxStatistic_" + mid).html(msg);
        }
    });
}

function GetYoutubeImage(labelid, portalID, youtubeCode, ctype) {
    $("#" + labelid).html("<img src='" + spath + "/DesktopModules/DNNInfo_Classifieds/images/loading.gif'/>");
    $.ajax({
        cache: false,
        url: spath + "/DesktopModules/DNNInfo_Classifieds/YouTubeImageHandler.ashx?portalID=" + portalID + "&youtubecode=" + youtubeCode + "&ctype=" + ctype,
        data: "{}",
        contentType: "text/html; charset=utf-8",
        dataType: "html",
        error: function(xhr, status, error) {
            //alert the error if needed
            //alert(xhr.responseText);
        },
        success: function(msg) {
            $("#" + labelid).html(msg);
            //alert(msg);
        }
    });
}

function setDropDownlist(id, hiddenid) {
    obj = document.getElementById(id);
    hiddenobj = document.getElementById(hiddenid);
    if (obj && hiddenobj) {
        hiddenobj.value = obj.options[obj.selectedIndex].value;
    }
}

function popupControl(path, mode, classifiedID, tabID) {
    window.open(path + '/desktopmodules/dnninfo_classifieds/popupcontrol.aspx?mode=' + mode + "&id=" + classifiedID + "&tid=" + tabID, "newwindow", "height=420,width=520, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, directories=no, status=no");
}

function showPropertyListingType(obj, trPriceID, trRentID, trRentPeriodID) {
    if (obj.options[obj.selectedIndex].value == "1") {
        $("#" + trPriceID).show();
        $("#" + trRentID).hide();
        $("#" + trRentPeriodID).hide();
    } else if (obj.options[obj.selectedIndex].value == "2") {
        $("#" + trPriceID).hide();
        $("#" + trRentID).show();
        $("#" + trRentPeriodID).show();
    } else {
        $("#" + trPriceID).hide();
        $("#" + trRentID).hide();
        $("#" + trRentPeriodID).hide();
    }
}

function LoadClassificationValues(portalID, ctype, cname, parentID, subParentID, title) {
    var subParentValue = "";
    var querystring = "";

    objParentID = document.getElementById(parentID);
    if (objParentID) { subParentValue = objParentID.options[objParentID.selectedIndex].value; }
    objSubParentID = document.getElementById(subParentID);

    if (objSubParentID) {
        for (var i = objSubParentID.options.length; i >= 0; i--) {
            objSubParentID.remove(i);
        }
        $("#" + subParentID).append("<option value=''>" + title + "</option>");

        if (subParentValue.length > 0) {
            querystring = "?portalID=" + portalID + "&ctype=" + ctype + "&cname=" + cname + "&cgroup=" + subParentValue;

            $.ajax({
                cache: false,
                url: spath + "/DesktopModules/DNNInfo_Classifieds/ClassificationHandler.ashx" + querystring,
                data: "{}",
                contentType: "text/html; charset=utf-8",
                dataType: "html",
                error: function(xhr, status, error) {
                    //alert the error if needed
                    //alert(xhr.responseText);
                },
                success: function(msg) {
                    $("#" + subParentID).append(msg);
                }
            });
        }
    }
}

function expandDiv(id, divID) {
    $("#" + id).show();
    $("#" + divID).hide();
}

function hideDiv(id, divID) {
    $("#" + id).hide();
    $("#" + divID).show();
}

function showHideObj(chk, id) {
    if ($('#' + chk).attr('checked') == 'checked') {
        $('#' + id).hide();
    }
    else {
        $('#' + id).show();
    }
}

//extract youtube video
function getYoutubeVideoID(id){
    $('#' + id).val($('#' + id).val());
    if($('#' + id).val().match('http://(www.)?youtube|youtu\.be')){
       $('#' + id).val($('#' + id).val().split(/v\/|v=|youtu\.be\//)[1].split(/[?&]/)[0]);
    }
}
