﻿$(document).ready(function () {
    $.blockUI({
        overlayCSS: { backgroundColor: 'transparent' },
        message: null
    });

    $(".collapseBoxClose").click(function () {
        var isClosed = $(this).attr("isClosed");
        var closeBox = $(this).attr("closebox");
        if (isClosed == "0") {
            $("#" + closeBox + " .collapseBoxBody").hide();
            $("#" + closeBox + " .collapseBoxFooter").hide();
            $(this).attr("isClosed", "1");
            $(this).text("[+]");
        }
        else {
            $("#" + closeBox + " .collapseBoxBody").show();
            $("#" + closeBox + " .collapseBoxFooter").show();
            $(this).attr("isClosed", "0");
            $(this).text("[-]");
        }

    });
    $("#dialog").dialog({ autoOpen: false, width: 820, height: 500 });
    $("#dialog2").dialog({ autoOpen: false, width: 800, height: 480 });
    $("#fileUpload").dialog({ autoOpen: false, width: 820, height: 500 });
    $(".datepicker").datepicker({ dateFormat: 'dd-mm-yy' });

    $(".confirm").click(function () {
        var confirmQuestion = "Are you sure you want to do this?";

        if ($(this).attr("confirm") != "") {
            confirmQuestion = $(this).attr("confirm");
        }

        return confirm(confirmQuestion);
    });

    $(".linkstats").click(function () {
        var lsforeignkeyid = $(this).attr("lsforeignkeyid");
        var lslinkstattypeid = $(this).attr("lslinkstattypeid");
        var hlink = $(this).attr("href");

        $.post("/_ajax/linkStats/record.ashx", { foreignkeyid: lsforeignkeyid, link: hlink, linkstattypeid: lslinkstattypeid },
                       function (data) {
                           //alert("Data Loaded: " + data);
                       });

    });


    $(".popupWindow").each(function () {
        $(this).attr("link", $(this).attr("href"));
        $(this).attr("href", "#");
        $(this).click(function () {
            if ($(this).attr("pupwidth") != "") {
                popUp($(this).attr("title"), $(this).attr("link"), $(this).attr("pupwidth"), $(this).attr("pupheight"));
            }
            else {
                popUpMini($(this).attr("title"), $(this).attr("link"));
            }

            return false;

        });
    });

    pop2LinkLoad();

    $(".numeric").keydown(function (event) {
        return isNumberKeyOrDot(event);

    });









    $(".fileUpload").each(function () {

        var noImage = '/images/Template/Default/noImageClickToUpload.png';
        var hasImage = '/images/Template/Default/clickToChangeImage.png';
        var id = $(this).attr("id");
        var imageID = id + "image";
        var previewImage = $(this).attr("previewImage");
        $(this).hide();

        if ($(this).val() == "") {
            $(this).before("<img class='fileUploadImage' src='" + noImage + "' id='" + imageID + "' />");
        }
        else {
            $(this).before("<img class='fileUploadImage' src='" + hasImage + "' id='" + imageID + "' />");
        }

        $("#" + imageID).click(function () {

            var url = '/_pop/upload/Default.aspx?statusid=' + imageID + "&element=" + id + "&previewImage=" + previewImage;

            fileUploadDialog(url, 600, 95);
        });

    });

    $(".cmdAddJobCategory").click(function () {

        var selector = "." + $(this).attr("ddl");

        addJobCategory($(selector).val());

        return false;
    });





});

function populateDirectoryGroupsAvailable(dropDownSelector) {
    var url = "/_ajax/directorysearch/listing.ashx";
    $.post(url, function (data) {
        $(dropDownSelector).html(data);
    });
}

function displayDirectorySearchOptions(groupToSearchSelector, SearchOptionHTML, directorySearchResults) {
    var url = "/_ajax/directorysearch/searchOptions.aspx";
    $.post(url, {group:$(groupToSearchSelector).val()}, function (data) {
        $(SearchOptionHTML).html(data);
        $(directorySearchResults).html("");
    });
}
function displaySearchResults(groupToSearchSelector, QuerySelector, JobCategorySelector,relationshipSelector,steerinGroupSelector,directorySearchResultsSelector) {
    var url = "/_ajax/directorysearch/results.aspx";
    $.post(url,{group:$(groupToSearchSelector).val(),query:$(QuerySelector).val(),jobCategoryID:$(JobCategorySelector).val(),userCompanyRelationShipListID:$(relationshipSelector).val(),steeringGroupID:$(steerinGroupSelector).val()},function(data)
    {
          $(directorySearchResultsSelector).html(data);
    });

}
function recordEmailClick(recipientUserNetworkMembershipIDv, recipientCompanyIDv, recipientEmailv) {
    $.post("/_ajax/emailClick/record.ashx", { recipientUserNetworkMembershipID: recipientUserNetworkMembershipIDv, recipientCompanyID: recipientCompanyIDv, recipientEmail: recipientEmailv },
   function (data) {
       //alert("Data Loaded: " + data);
   });
}

function showHideJobCategorySelect() {
    //alert('lets hide!');
    var filterHasOptions = false;
   // alert($(".compJobCategoryIDFilter option").length);
    if ($(".compJobCategoryIDFilter option").length != 0) {
        filterHasOptions = true;
    }
   


    if (filterHasOptions) {
        $(".compJobCategoryIDFilter").show();
        $(".compJobCategoryID").show();
        $(".cmdAddJobCategory").show();
    }
    else {
        $(".compJobCategoryIDFilter").hide();
        $(".compJobCategoryID").hide();
        $(".cmdAddJobCategory").hide();
    }


}

function addJobCategory(jobCategoryID) {
    //alert('add job Category' + jobCategoryID);
    var url = "/_ajax/jobCategories/addToList.ashx?jobCategoryID=" + jobCategoryID;

    $.get(url, function (data) {
       // $("#debug").html($("#debug").html() + "<br/>Add Job Category");
        $("#jobCategoryList tbody").append(data);
    });
}


function setJobCategoryLevel2CustomCallBack(level1Element, level2Element, currentJobCategoryID) {



    var url = "/_ajax/jobCategories/listWithParent.ashx?parentID=" + $(level1Element).val() + "&jobCategoryID=" + currentJobCategoryID + "&currentList=" + listCurrentSelectedJobCategories();

    $.get(url, function (data) {
        $(level2Element).html(data);
        showHideJobCategorySelect();
        preformSearch();
    });
}

function pop2LinkLoad() {
    $(".popupWindow2").each(function () {
        $(this).attr("link", $(this).attr("href"));
        $(this).attr("href", "#");
        $(this).click(function () {
            if ($(this).attr("pupwidth") != "") {
                popUp($(this).attr("title"), $(this).attr("link"), $(this).attr("pupwidth"), $(this).attr("pupheight"));
            }
            else {
                popUpMini($(this).attr("title"), $(this).attr("link"));
            }

            return false;

        });
    });
}

function findTabPosition(selector, title) {

    var returnvalue = -1;
    var position = 0;
    $(selector + " ul li a").each(function () {
        if (title.toLowerCase() == $(this).text().toLowerCase()) {
            returnvalue = position;
        }
        position++;
    });
 
    return returnvalue;


}

function disableButton(objectClass) {
    
    var image = "<img src='/images/template/default/icons/loding.gif' alt='Loading' style='margin-left:5px;' />";
    
    $(objectClass).attr("disabled", true);
    $(objectClass).after(image);
}

function viewCompanyDetails(companyID, addressID,distance) {

    var title = 'View Company';
    var url = '/_pop/companies/view.aspx?companyId=' + companyID + '&addressID=' + addressID + '&distance=' + distance;


    popUp(title, url, 820, 500);
}



function isNumberKeyOrDot(e)//this function is use only for Number
{
    var k;
    document.all ? k = e.keyCode : k = e.which;

    return ((k > 47 && k < 58) || k == 8 || k == 46 || k == 109 || k == 190);
}

function refresh() {
    window.location.reload();
}

function popUpMini(title, url) {
    popUp(title, url, 820, 500);
}
function destorypopup() {
    $("#dialog").dialog("destroy");
}
function popUp(title, url, w, h) {
    //alert(w);
    //alert(h);
    if (w == undefined) {
        w = 820;
    }
    if (h == undefined) {
        h = 500;
    }
    setDialogBlank();
    setDialogLoading();
    $("#dialog").dialog("destroy")
    $("#dialog").dialog({ autoOpen: false, width: w + 'px', height: h, resizable: false });
    $("#dialog").dialog("option", "title", title);
    $("#dialog").dialog("open");


    $.get(url, function (data) {
        $("#dialogHTML").html(data);
    });
}


function popUpMini2(title, url) {
    popUp2(title, url, 820, 500);
}

function popUp2(title, url, w, h) {
    //alert(w);
    //alert(h);
    if (w == undefined) {
        w = 820;
    }
    if (h == undefined) {
        h = 500;
    }
    setDialogBlank2();
    setDialogLoading2();
    $("#dialog2").dialog("destroy")
    $("#dialog2").dialog({ autoOpen: false, width: w + 'px', height: h,resizable:false });
    $("#dialog2").dialog("option", "title", title);
    $("#dialog2").dialog("open");


    $.get(url, function (data) {
        $("#dialog2HTML").html(data);
    });
}

function fileUploadDialog(url, w, h) {

    if (w == undefined) {
        w = 820;
    }
    if (h == undefined) {
        h = 500;
    }
   
    setFileUploadBlank();
    setFileUploadLoading();
    $("#fileUpload").dialog("destroy")
    $("#fileUpload").dialog({ autoOpen: false, width: w + 'px', height: h });
    $("#fileUpload").dialog("open");


    $.get(url, function (data) {
        $("#fileUploadHTML").html(data);
    });

}

function setFileUploadElement(file, statusid, element, previewImage,imagePreview) {
   // alert('file = ' + file);
    //alert('statusid = ' + statusid);
    //alert('element = ' + element);

    $("#" + element).val(file);
    $("#" + statusid).attr("src", "/images/Template/Default/clickToChangeImage.png");
    $("#" + previewImage).attr("src", imagePreview);
    $("#fileUpload").dialog("destroy");
}

function setFileUploadBlank() {
    $("#fileUploadHTML").html('');
}

function setFileUploadLoading() {
    $("#fileUploadHTML").html('<div style="text-align:center;"><img src="/images/loading.gif" alt="Loading" /></div>');
}

function setDialogBlank() {
    $("#dialogHTML").html('');
}
function setDialogLoading() {
    $("#dialogHTML").html('<div style="text-align:center;"><img src="/images/loading.gif" alt="Loading" /></div>');
}

function setDialogBlank2() {
    $("#dialog2HTML").html('');
}
function setDialogLoading2() {
    $("#dialog2HTML").html('<div style="text-align:center;"><img src="/images/loading.gif" alt="Loading" /></div>');
}

function jqueryGetFormData(formName) {

    $("#" + formName + " :input").each(function () {

        //alert($(this).attr("type"));

        switch ($(this).attr("type")) {

            case "checkbox":

                $("#" + formName).data($(this).attr("name"), $(this).attr("checked"))
                break;
            case "radio":
                //needs to be done.
                break;
            default:
                $("#" + formName).data($(this).attr("name"), $(this).val())
                break;
        }

        //
        //$("#" + formName).data($(this).attr("name"),$(this).val())
    });



    return $("#" + formName).data();
}
function setElement(elementid, elementvalue) {
    $("#" + elementid).val(elementvalue);
}



function setJobCategoryLevel2(level1Element, level2Element, currentJobCategoryID) {
    var url = "/_ajax/jobCategories/listWithParent.ashx?parentID=" + $(level1Element).val() + "&jobCategoryID=" + currentJobCategoryID + "&currentList=" + listCurrentSelectedJobCategories();
    
    $.get(url, function (data) {
        $(level2Element).html(data);
        showHideJobCategorySelect();
    });
}

function setJobCategoryLevel1(parentID, element) {
    //$("#debug").html($("#debug").html() + "<br/>Level1");
    var url = "/_ajax/jobCategories/listroots.ashx?parentID=" + parentID + "&currentList=" + listCurrentSelectedJobCategories();

    $.get(url, function (data) {
        $(element).html(data);
        setJobCategoryLevel2(".compJobCategoryIDFilter", ".compJobCategoryID", 0);
        showHideJobCategorySelect();
    });
}

function listCurrentSelectedJobCategories() {
    var currentJobCategories = "";
    $(".jobCategoryList").each(function () {
        currentJobCategories += "," + $(this).val();
    });
    currentJobCategories += ",";
    return currentJobCategories;
}

function checkBox_All(inputRootClass) {

    $('.' + inputRootClass).attr('checked', true);
    
    return false;
}

function checkBox_None(inputRootClass) {
    $('.' + inputRootClass).attr('checked', false);
    return false;
}

function checkBox_WithClass(inputRootClass, className) {

    $('.' + inputRootClass).attr('checked', false);

    $('.' + inputRootClass + '.' + className).attr('checked', true);

    return false;
}


function recipientHolderSetUp(holderID) {

    $.post("/_ajax/recipientHolder/render.ashx",
        {
            mainID: holderID,
            recipients: $("#" + holderID + "People").val()
        },
   function (data) {

       $("#" + holderID).html(data);
   });
}


function recipientHolderRemove(holderID, recipient) {

    var theRecipients = $("#" + holderID + "People").val();
    theRecipients = theRecipients.toLowerCase().replace(recipient.toLowerCase(), "");
    $("#" + holderID + "People").val(theRecipients);

    recipientHolderSetUp(holderID);
}

function recipientHolderRemoveByArray(holderID, recipient) {

    var theRecipients = $("#" + holderID + "People").val();

    for (var i = 0, len = recipient.length; i < len; ++i) {
        theRecipients = theRecipients.toLowerCase().replace(recipient[i].toLowerCase(), "");
    }

    $("#" + holderID + "People").val(theRecipients);

    recipientHolderSetUp(holderID);
}


function recipientHolderAdd(holderID, recipient) {

    var theRecipients = $("#" + holderID + "People").val();
    theRecipients = theRecipients + ";" + recipient;
    $("#" + holderID + "People").val(theRecipients);

    recipientHolderSetUp(holderID);
}


function recipientHolderAddByArray(holderID, recipient) {


    var theRecipients = $("#" + holderID + "People").val();

    for (var i = 0, len = recipient.length; i < len; ++i) {
        theRecipients = theRecipients + ";" + recipient[i];
    }

    $("#" + holderID + "People").val(theRecipients);

    recipientHolderSetUp(holderID);
}



