﻿/// <reference path="jquery-1.3.2-vsdoc.js" />
$(document).ready(function() {
    $("#hrefImpersonate").click(function() {
        var d = $("<div />").attr("id", "dvImpersonate").css({ display: "none", overflow: "auto", position: "absolute", right: 0, top: 0, width: 400, height: 200, border: "solid 1px #000", backgroundColor: "#fff", zIndex: 1000, textAlign: "left", paddingLeft: 20 }).html("<div style='text-align:center;'><h2 style='text-decoration:underline;'>Impersonate User</h2></div><b>Please enter search criteria:<br/>(First, Last, User or Company Name, R #)</b><br/><br/>");
        var b = $("<input />").attr("type", "button").val("Close").attr("id", "txtCloseImpersonate").click(function() {
            d.hide("slow", function() {
                d.remove();
            });
        });
        var t = $("<input />").attr("type", "text").attr("id", "txtImpersonate").css({ border: "solid 1px #000", width: 330 }).autocomplete({
            source: function(request, response) {
                $.ajax({
                    url: "webservices/ImpersonateUsers.asmx/SearchUsers",
                    type: "POST", contentType: "application/json; charset=utf-8", dataType: "json",
                    data: JSON.stringify({ searchTxt: request.term }),
                    success: function(data) {

                        response($.map(data.d, function(item) {

                            //html(x.d[i].Name).attr("id", x.d[i].UserName).click(function() {                        
                            return {
                                label: item.Name,
                                value: item.Name,
                                id: item.UserName
                            }
                        }));
                    },
                    error: function(e) { alert(e.responseText); }
                });
            },
            minLength: 3,
            select: function(event, ui) {

                var DataU = JSON.stringify({ userName: ui.item.id });
                $.ajax({
                    async: false,
                    type: "POST",
                    data: DataU,
                    url: "webservices/ImpersonateUsers.asmx/ImpersonateSelectedUser",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(x, s) {
                        if (x.d) {
                            setTimeout(function() {
                                window.location = "default.aspx";
                                return true;
                            }, 1);
                        }
                        else {
                            alert("An error has occured");
                        }
                    },
                    error: function(e) {  alert(e.responseText); }
                });
            },
            open: function(event, ui) { $(this).removeClass("ui-corner-all").addClass("ui-corner-top"); },
            focus: function(event, ui) { event.preventDefault(); },
            close: function(event, ui) { $(this).removeClass("ui-corner-top").addClass("ui-corner-all"); }

        });
        //        
        //        
        //        
        //        
        //        .keyup(function() {
        //            $("#divSearchResult").remove();
        //            var Data = JSON.stringify({ searchTxt: $(this).val() });
        //            $.ajax({
        //                async: false,
        //                type: "POST",
        //                data: Data,
        //                url: "webservices/ImpersonateUsers.asmx/SearchUsers",
        //                contentType: "application/json; charset=utf-8",
        //                dataType: "json",
        //                success: function(x, s) {
        //                    if (x.d != null) {
        //                        var ul = $("<div/>").attr("id", "divSearchResult").css({ width: 370 });
        //                        if (x.d.length > 0) {
        //                            var ind = 0;
        //                            var col;
        //                            for (var i = 0; i < x.d.length; i++) {
        //                                if (i % 2 == 0) {
        //                                    col = "#7EA7D5";
        //                                }
        //                                else {
        //                                    col = "#ffffff";
        //                                }
        //                                ind++;
        //                                ul.append($("<div/>").css({ backgroundColor: col, cursor: "pointer", fontWeight: "bold", verticalAlign: "middle" }).html(x.d[i].Name).attr("id", x.d[i].UserName).click(function() {
        //                                    var DataU = JSON.stringify({ userName: this.id });
        //                                    $.ajax({
        //                                        async: false,
        //                                        type: "POST",
        //                                        data: DataU,
        //                                        url: "webservices/ImpersonateUsers.asmx/ImpersonateSelectedUser",
        //                                        contentType: "application/json; charset=utf-8",
        //                                        dataType: "json",
        //                                        success: function(x, s) {
        //                                            if (x.d) {
        //                                                window.location.href = "default.aspx";
        //                                            }
        //                                            else {
        //                                                alert("An error has occured");
        //                                            }
        //                                        },
        //                                        error: function(e) { alert(e.responseText); }
        //                                    });
        //                                }));
        //                            }
        //                        }
        //                        else {
        //                            ul.append($("<div/>").html("No Results Found").attr("id", "liNoneFound"));
        //                        }
        //                        d.append(ul);
        //                        return false;
        //                    }
        //                },
        //                error: function(e) { alert(e.responseText); }

        //            });
        //        });

        d.append(t);
        d.append(b);
        $("#divEasyEnrolHeader").parent().append(d);
        $("#dvImpersonate").show("slow", function() { t.focus(); });

        return false;

    });
});

var menu=function(){
	var t=15,z=50,s=6,a;
	function dd(n){this.n=n; this.h=[]; this.c=[]}
	dd.prototype.init=function(p,c){
		a=c; var w=$get(p), s=w.getElementsByTagName('ul'), l=s.length, i=0;
		for(i;i<l;i++){
			var h=s[i].parentNode; this.h[i]=h; this.c[i]=s[i];
			h.onmouseover=new Function(this.n+'.st('+i+',true)');
			h.onmouseout=new Function(this.n+'.st('+i+')');
		}
	}
	dd.prototype.st=function(x,f){ 
		var c=this.c[x], h=this.h[x], p=h.getElementsByTagName('a')[0];
		clearInterval(c.t); c.style.overflow='hidden';
		if(f){
			p.className+=' '+a;
			if(!c.mh){c.style.display='block'; c.style.height=''; c.mh=c.offsetHeight; c.style.height=0}
			if(c.mh==c.offsetHeight){c.style.overflow='visible'}
			else{c.style.zIndex=z; z++; c.t=setInterval(function(){sl(c,1)},t)}
		}else{p.className=p.className.replace(a,''); c.t=setInterval(function(){sl(c,-1)},t)}
	}
	function sl(c,f){
		var h=c.offsetHeight;
		if((h<=0&&f!=1)||(h>=c.mh&&f==1)){
			if(f==1){c.style.filter=''; c.style.opacity=1; c.style.overflow='visible'}
			clearInterval(c.t); return
		}
		var d=(f==1)?Math.ceil((c.mh-h)/s):Math.ceil(h/s), o=h/c.mh;
		c.style.opacity=o; c.style.filter='alpha(opacity='+(o*100)+')';
		c.style.height=h+(d*f)+'px'
	}
	return{dd:dd}
} ();

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/, "");
}

function ApplyDateMask(control, onComplete) {

    $(control).mask("99/aaa/99", { completed: function() {

        if ($(this).val() != "") {
            var datePart = $(this).val().split("/");
            var year = datePart[2] > 75 ? 1900 + datePart[2] : 2000 + datePart[2];
            var month = datePart[1].substring(0, 1).toUpperCase() + datePart[1].substring(1).toLowerCase();
            if (!IsMonthValid(month)) {
                alert("Invalid month, please re-enter in format \"dd/mmm/yy\"");
                $(this).val("");
                return;
            }
            if (datePart[0] > daysInMonth(GetMonthNumber(month), year)) {
                alert("Invalid day, please re-enter in format \"dd/mmm/yy\"");
                $(this).val("");
                return;
            }
            $(this).val(datePart[0] + "/" + month + "/" + datePart[2]);
            if (onComplete) {
                return onComplete();
            }
        }
    }
    });
}

function IsMonthValid(InputMonth) {
    var isValid = false; 
    $(GetMonths()).each(function() {
        if (this == InputMonth) {
            isValid = true;
        }
    });
    return isValid;
}

function GetMonths() {
    return ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];    
    
}

function GetMonthNumber(InputMonth) {
    var monthNumber = -1;
    $(GetMonths()).each(function(i) {
        if (this == InputMonth) {
            monthNumber = i;
        }
    });
    return monthNumber;
}

function daysInMonth(iMonth, iYear)
{
	return 32 - new Date(iYear, iMonth, 32).getDate();
}
