﻿
$(document).ready(function() {
    $(".fencer-span").click(function() {
        var arrtmp = $(this).attr("id").split("-");
        var tmp = arrtmp[1] + '-' + arrtmp[2]; ; //$(this).attr("id");
        //alert(tmp);
        var myfencer = arrtmp[1];
        var mysex = arrtmp[2];
        
        var date = new Date(); var timestamp = date.getTime();

        var mydata = { fencerid: myfencer, sex: mysex, dowhat: "showCompsForFencer", randomDate: timestamp };

        //alert(tmp);

        if ($("." + tmp).is(":hidden") == true) {
            $("." + tmp).show();
            
            $.ajax({
                type: "GET",
                url: "ajax/ajax.aspx",
                data: mydata,
                error: function(msg) {
                    alert("error: " + msg);
                    IsSuccess = false;
                },
                success: function(response) {
                    if (response != "") {
                        $("." + tmp).attr("innerHTML", response);
                    }
                    else {
                        $("." + tmp).attr("innerHTML", "nope");
                    }
                }

            });
        }
        else {
            $("." + tmp).hide();
        }
    });

    /*   fencer hover to momic s:hover*/
    $(".fencer-span").hover(
      function() {
    $(this).attr("class", "fencer-span-hover");
      },
      function() {
      $(this).attr("class", "fencer-span");
      }
    );
    //with fade class
    $(".fencer-span.fade").hover(function() { $(this).fadeOut(100); $(this).fadeIn(500); });



    /*
    //login button
    $("#loginButton").click(function() {
    location.href = "login.aspx";

        });

        //hide buttons for read only - page one
    if ($("[id$=PageOneReadOnly]").attr("value") == "true") {
    $(".save-control").animate({ height: 'hide', opacity: 'hide' }, 'slow');
    $(".other-eval-edit").animate({ height: 'hide', opacity: 'hide' }, 'slow');
    //$("#go-submit").animate({ height: 'hide', opacity: 'hide' }, 'slow');
    $("[id$=EditButton]").animate({ height: 'hide', opacity: 'hide' }, 'slow');
    }
    //hide buttons for Locked
    if ($("[id$=LockedHiddenField]").attr("value") == "true") {
    $(".go-submit").hide();
    }

        //Create New Project
    $("#newproject").click(function() {

            if ($(".new-project").is(":hidden") == true) {
    $(".new-project").animate({ height: 'show', opacity: 'show' }, 'slow');
    $("#newproject").attr("value", "cancel");
    $("#newproject").attr("class", "admin");
    $("#new-project-title").hide();
    }
    else {
    $(".new-project").animate({ height: 'hide', opacity: 'hide' }, 'slow');
    $("#newproject").attr("value", " new ");
    $("#newproject").attr("class", "admin-util");
    $("#new-project-title").show();
    }
    });

        $("#newProject-save").click(function() {

            if (trim($("#newProject").attr("value")) == "") { Boxy.alert("Please enter a name for this project", null, { title: 'Message' }); return false };
    $find('PleaseWaitPopup').show();
    var planName = $("#newProject").attr("value");
    var date = new Date(); var timestamp = date.getTime();
    var mydata = { params: planName, dowhat: "newProject", randomDate: timestamp };

            $.ajax({
    type: "GET",
    url: "ajax/ajax.aspx",
    data: mydata,
    error: function(msg) {
    alert("error: " + msg);
    IsSuccess = false;
    },
    success: function(response) {
    $find('PleaseWaitPopup').hide();
    if (response != "") {
    if (response.slice(0, response.indexOf("-")) == "OK") {
    var projectNum = response.slice(response.indexOf("-") + 1, response.length);
    //alert(projectNum);
    location.href = "project.aspx?project=" + projectNum;
    }
    else {
    Boxy.alert(response, null, { title: 'Message' });
    //$("#newProject").attr("value", "");
    $("#newProject").focus();

                        }

                    }
    else {
    //$find('PleaseWaitPopup').hide();

                    }
    }

            });
    });

        //list plans on demand
    $("#listPlans").click(function() {
    var date = new Date(); var timestamp = date.getTime();
    var mydata = { dowhat: "listPlans", randomTime: timestamp };
    //alert('jj');
    $.ajax({
    type: "GET",
    url: "ajax/ajax.aspx",
    data: mydata,
    error: function(msg) {
    alert("error: " + msg);
    },
    success: function(response) {
    //alert(response);
    if (response != "") {
    $("#plan-list").attr("innerHTML", response).animate({ height: 'show', opacity: 'show' }, 'slow');
    }
    else {
    $("#plan-list").attr("innerHTML", "<p>no plans - err</p>").animate({ height: 'show', opacity: 'show' }, 'slow');
    }
    }

            });
    });

        //submit project to line manager
    //top menu
    $('.go-submit').click(function() {
    Boxy.confirm("Your Project will be locked until reviewed by your line manager<br /><br />Please confirm:", function() { SubmitToManager(); }, { title: 'Submit Project to Line Manager?' });
    return false;
    });

        /////
    $("#go-exit").click(function() {
    Boxy.confirm("Please confirm:", function() { location.href = "default.aspx"; }, { title: 'Exit this project?' });
    return false;
    });


        //////////////////////////////////////////////////////////
    $(".eval-edit").click(function() {
    if ($("#div-eval-label").is(":hidden") == false) {
    $("#div-eval-label").animate({ height: 'hide', opacity: 'hide' }, 'slow');
    $("#div-eval-text").animate({ height: 'show', opacity: 'show' }, 'slow');
    }
    else {
    $("#div-eval-label").animate({ height: 'show', opacity: 'show' }, 'slow');
    $("#div-eval-text").animate({ height: 'hide', opacity: 'hide' }, 'slow');
    }
    });
    //desc dide
    $(".desc-edit").click(function() {
    if ($("#div-desc-label").is(":hidden") == false) {
    $("#div-desc-label").animate({ height: 'hide', opacity: 'hide' }, 'slow');
    $("#div-desc-text").animate({ height: 'show', opacity: 'show' }, 'slow');
    var myID = $("[id$=planDescription]").attr("id");
               
    new nicEditor({ buttonList: ['bold', 'italic', 'indent', 'outdent', 'ol', 'left', 'center', 'right'] }).panelInstance(myID);
    //nicEditors.allTextAreas();
    }
    else {
    $("#div-desc-label").animate({ height: 'show', opacity: 'show' }, 'slow');
    $("#div-desc-text").animate({ height: 'hide', opacity: 'hide' }, 'slow');
    }
    });


        ////////////////////////////////////////
    //other eval
    $(".other-eval-edit").click(function() {
    if ($("#other-desc-label").is(":hidden") == false) {
    $("#other-desc-label").animate({ height: 'hide', opacity: 'hide' }, 'slow');
    $("#div-other-eval").animate({ height: 'show', opacity: 'show' }, 'slow');
    }
    else {
    $("#other-desc-label").animate({ height: 'show', opacity: 'show' }, 'slow');
    $("#div-other-eval").animate({ height: 'hide', opacity: 'hide' }, 'slow');
    }
    });
    //////////////////////////////////////////////////
    $(".helptitle").click(function() {
    if ($(this).next().is(":hidden") == true) {
    $(this).next().animate({ height: 'show', opacity: 'show' }, 'slow');
    }
    else {
    $(this).next().animate({ height: 'hide', opacity: 'hide' }, 'fast');
    }
    });

        //priority - text
    $("#priority-title").click(function() {
    if ($("#priority-text").is(":hidden") == false) {
    $("#priority-text").animate({ height: 'hide', opacity: 'hide' }, 'fast');
    }
    else {
    $("#priority-text").animate({ height: 'show', opacity: 'show' }, 'slow');
    }
    });

        //eval info
    $("#eval-info-head").click(function() {
    if ($("#eval-info-text").is(":hidden") == false) {
    $("#eval-info-text").animate({ height: 'hide', opacity: 'hide' }, 'fast');
    }
    else {
    $("#eval-info-text").animate({ height: 'show', opacity: 'show' }, 'slow');
    }
    });
    //ajax save stuff
    //first generic
    $(".save-control").click(function() {

            $find('PleaseWaitPopup').show();
    var idtosave = $(this).attr("id").slice(0, $(this).attr("id").lastIndexOf('-'));

            var projID = $("[id$=PlanID]").attr("value");
    //alert(projID);
    var name = GetVal(idtosave, "chkbox");
    //alert(Left(name, 5)); //return false;
    if (Left(name, 5) == 'style' || Left(name, 2) == '""') { name = ''; }
    //alert(name);
    var mydata = { params: name, dowhat: idtosave, projectid: projID };

            if (!DoAjax(mydata)) { alert('error in jquery call to ajax.aspx'); }
    });


        // dates & project type
    $(".dateproject").click(function() {
    $find('PleaseWaitPopup').show();
    var projID = $("[id$=PlanID]").attr("value");
    var date = new Date(); var timestamp = date.getTime();

            var idtosave = "startDate";
    var name = GetVal(idtosave, "datetime");
    var mydata = { params: name, dowhat: idtosave, projectid: projID, randtime: timestamp };
    if (!DoAjax(mydata)) { alert('error in jquery call to ajax.aspx'); }

            var idtosave = "endDate";
    var name = GetVal(idtosave, "datetime");
    var mydata = { params: name, dowhat: idtosave, projectid: projID, randtime: timestamp };
    if (!DoAjax(mydata)) { alert('error in jquery call to ajax.aspx'); }

            var idtosave = "projectType";
    var name = GetVal(idtosave, "datetime");
    var mydata = { params: name, dowhat: idtosave, projectid: projID, randtime: timestamp };
    if (!DoAjax(mydata)) { alert('error in jquery call to ajax.aspx'); }

        });

        // Estimated Attendees & Age Range
    $(".attendeeage").click(function() {
    $find('PleaseWaitPopup').show();
    var projID = $("[id$=PlanID]").attr("value");
    var date = new Date(); var timestamp = date.getTime();

            var idtosave = "estMaleAtt";
    var name = GetVal(idtosave, "datetime");
    var mydata = { params: name, dowhat: idtosave, projectid: projID, randtime: timestamp };
    if (!DoAjax(mydata)) { alert('error in jquery call to ajax.aspx'); }

            var idtosave = "estFemaleAtt";
    var name = GetVal(idtosave, "datetime");
    var mydata = { params: name, dowhat: idtosave, projectid: projID, randtime: timestamp };
    if (!DoAjax(mydata)) { alert('error in jquery call to ajax.aspx'); }

            var idtosave = "ageRangeMale";
    var name = GetVal(idtosave, "chkbox");
    if (Left(name, 5) == 'style' || Left(name, 2) == '""') { name = ''; }
    var mydata = { params: name, dowhat: idtosave, projectid: projID, randtime: timestamp };
    if (!DoAjax(mydata)) { alert('error in jquery call to ajax.aspx'); }

            var idtosave = "ageRangeFemale";
    var name = GetVal(idtosave, "chkbox");
    if (Left(name, 5) == 'style' || Left(name, 2) == '""') { name = ''; }
    var mydata = { params: name, dowhat: idtosave, projectid: projID, randtime: timestamp };
    if (!DoAjax(mydata)) { alert('error in jquery call to ajax.aspx'); }

        });


*/

});

function SaveDesc() {
    var value = document
    alert('gg');
}

function GetVal(idtosave, datatype) {
    switch (datatype)
    {
        case "datetime":
            var name = $("[id$=" + idtosave + "]").attr("value");
            break;
        case "chkbox":
            var name = $("[id$=" + idtosave + "]").attr("innerHTML");
            name = name.slice(0, name.lastIndexOf("name"));
            name = name.slice(name.lastIndexOf("value=") + 6, name.length);
            break;
        default:
            var name = "error - nor data type in jquery";
            break; 
    } 
    return name;
}

function DoAjax(mydata) {
    //alert('VV');
    var IsSuccess = true;
    
    $.ajax({
        type: "GET",
        url: "ajax/ajax.aspx",
        data: mydata,
        error: function(msg) {
            alert("error: " + msg);
            IsSuccess = false;
        },
        success: function(response) {
            if (response != "") {
                alert(response);
            }
            else {
                //$find('PleaseWaitPopup').hide();
                
            }
        }

    });
    return IsSuccess;
}
function SubmitToManager() { //update project status
    //alert('OK');
    var projID = $("[id$=PlanID]").attr("value");
    var statID = $("[id$=ProjectStatusHiddenField]").attr("value");
    var date = new Date(); var timestamp = date.getTime();
    var mydata = { dowhat: "submitToManager", projectid: projID, statusid: statID, randTime: timestamp};

    $.ajax({
        type: "GET",
        url: "ajax/ajax.aspx",
        data: mydata,
        error: function(msg) {
            alert("error: " + msg);
        },
        success: function(response) {

            if (response != "") {
                Boxy.alert(response, null, { title: 'Message' });
            }
            else {
                location.href = "default.aspx?msg=plan_submitted";
            }
        }

    });

}
function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}

function ListPlans() {
    var date = new Date(); var timestamp = date.getTime();
    var mydata = { dowhat: "listPlans", randomTime: timestamp };

    $.ajax({
        type: "GET",
        url: "ajax/ajax.aspx",
        data: mydata,
        error: function(msg) {
            alert("error: " + msg);
        },
        success: function(response) {

            if (response != "") {
                if (trim(response) == "X") {
                    location.href = "login.aspx?msg=sessionend";
                }
                else {
                    $("#plan-list").attr("innerHTML", response).show();
                }
            }
            else {
                $("#plan-list").attr("innerHTML", "<p>no plans - err</p>").show();
            }
        }

    });
}
function Left(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else
        return String(str).substring(0, n);
}
