﻿var filedid = "";
doDrag=false;
rsdoDrag=true;

posLeft=0;
posTop=0;

popupName = 'keypopup';
layername = 'layerunique';

function initKeyPad(popupid,layerid) 
{ 
    writeCSS(); // to write KeyPad

    popupName = popupid;
    layername = layerid;
    writePopUp(); // to generate KeyPad

    assignKeyPress();
}; 

function writeCSS()
{
document.write('<style>'); 
document.writeln('.ptkbtn_close {border-right: #6eaeff 1px solid; border-top: #6eaeff 1px solid; font-weight: bold; border-left: #6eaeff 1px solid; width: 16pt; cursor: pointer; border-bottom: #6eaeff 1px solid; height: 14pt; background-color: #669eff; color: #ffffff;text-align:center;}');
document.write('</style>');
};

function writePopUp()
{
document.write('<div id="'+ popupName +'"');
document.write(' style="z-index:1;position:absolute;');
document.write('padding:2px;background-color:#669eff;cursor:default;display:none;"');
document.write(' onMouseDown="if(rsdoDrag){sldMouseDown(event)}" onMouseUp="sldMouseUp(event)">');
document.write('<div style="color:#FFFFFF; cursor:move;" >');
document.write('<table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td align="left"><div style="padding-left:5px;font-size:15pt;font-weight:bold;" id="'+popupName+'_head"></div></td><td align="right"><input type="button" class="ptkbtn_close" value="X"  name="butt"  onclick="hide()"></td></tr></table>');
document.write('</div>');
document.write('<div id="'+popupName+'_content" style="background-color:#FFFFFF;text-align:center;" onMouseDown="rsdoDrag=false;" onMouseUp="rsdoDrag=true;sldMouseUp(event);">');
document.write('<iframe id="'+popupName+'_iframe" frameborder="0" allowtransparency="true" scrolling="auto"></iframe>');
document.write('</div>');
document.write('</div>');
};

function hide()
{
    $("#"+popupName).hide();
    $("#"+layername).hide();
    if(layername == "WelcomePopUp")
    {
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+1);
        document.cookie = 'AllPackers_Com_WelcomePage=1;expires=' + exdate.toGMTString() + ';';
    }
};

function showPopUp(popuptop, popupleft, wndWidth, wndHeight, pagesource, popuptitle, inframe) {
    $("#" + popupName).css({ left: popupleft + "px", top: popuptop + "px", width: wndWidth + "px", height: wndHeight + "px" });
    $("#" + popupName + "_head").html(popuptitle);
    $("#" + popupName + "_content").css({ width: wndWidth + "px", height: parseInt(wndHeight - 25) + "px" });
    
    if (inframe == true)
        $("#" + popupName + "_iframe").attr({ width: wndWidth, height: parseInt(wndHeight - 22), src: pagesource });
    else {
        $("#" + popupName + "_iframe").hide();
        $("#" + popupName + "_content").html(pagesource);
        $("#" + popupName + "_content").css({ overflow: "auto" });
    }
    $("#" + popupName).show();
    $("#" + layername).css({ display: "block", left: "0px", top: "0px", width: $(document).width(), height: $(document).height() }).fadeTo("slow", 0.33);
};

function setLeft(o,oLeft){o.style.left = oLeft + "px"}

function setTop(o,oTop){o.style.top = oTop + "px"}

function setPosition(o,oLeft,oTop) {setLeft(o,oLeft);setTop(o,oTop)}

function sldMouseDown(e)
{
	if (!e) {e = window.event}
	doDrag=true
	o=document.getElementById(popupName)
	sldLeft=getAbsLeft(o)
	sldTop=getAbsTop(o)
	sldMouseLeft=e.clientX-sldLeft
	sldMouseTop=e.clientY-sldTop

};

function sldMouseUp(e)
{
	rsdoDrag=true;
	doDrag=false;
};

function sldMouseMove(e)
{
	if (!e) {e = window.event}
	if (rsdoDrag && doDrag)
	{
		o=document.getElementById(popupName)
		setPosition(o,e.clientX-sldMouseLeft,e.clientY-sldMouseTop)
		return false
	}	
};

function assignKeyPress(){document.onmousemove = sldMouseMove;};

function getAbsLeft(o) {
	oLeft = o.offsetLeft
	while(o.offsetParent!=null) {
		oParent = o.offsetParent
		oLeft += oParent.offsetLeft
		o = oParent
	}
	return oLeft
};

function getAbsTop(o) {
	oTop = o.offsetTop
	while(o.offsetParent!=null) {
		oParent = o.offsetParent
		oTop += oParent.offsetTop
		o = oParent
	}
	return oTop
};

function openUserList(wt) {

    var offset = $("#" + filedid).offset();
    showPopUp(offset.top, offset.left, 150, 100, wt, 'User List', false);
    var user = "";
    var noofuser = $("#txt_noofuser").val();
    var divuser = "";
    if (filedid == "a_Userto") {
        user = $("#ctl00_main_txt_AssignTo").val();
        if (user != "") {
            for (var i = 0; i < noofuser; i++) {
                divuser = $("#div_" + i).text();
                if (user == divuser)
                    document.getElementById("Checkbox_" + i).checked = true;
            }
        }
    }
    else if (filedid == "a_Usercc") {
        user = $("#ctl00_main_txt_CC").val();
        user = user.split(',');
        for (var i = 0; i < noofuser; i++) {
            divuser = $("#div_" + i).text();
            if ((user[0] == divuser) || (user[1] == divuser)) {
                document.getElementById("Checkbox_" + i).checked = true;
            }
        }
    }
};

function onUserSelect(obj) {
    
    var fullid = obj.id.split('_');
    var divid = "div_" + fullid[1];
    var email = $("#txtEmail_" + fullid[1]).val();
    var n = $("input:checked").length;


    if (filedid == "a_Userto") {
        if (n > 1) {
            obj.checked = false;
            return false;
        }
        if (obj.checked == true) {
            hide();
            if ($("#ctl00_main_txt_AssignTo").val() == "") {
                $("#ctl00_main_txt_AssignTo").val($("#" + divid).text());
                $("#ctl00_main_txt_EmailTo").val(email);
            }
        }
        else {
            $("#ctl00_main_txt_AssignTo").val("");
            $("#ctl00_main_txt_EmailTo").val("");
        }
    }
    else if (filedid == "a_Usercc") {
        if (n > 2) {
            obj.checked = false;
            return false;
        }
        if (obj.checked == true) {
            $("#ctl00_main_txt_CC").val($("#ctl00_main_txt_CC").val() + "," + $("#" + divid).text());
            $("#ctl00_main_txt_EmailCC").val($("#ctl00_main_txt_EmailCC").val() + ";" + email);
            var txt_cc = $("#ctl00_main_txt_CC").val();
            var txt_emailcc = $("#ctl00_main_txt_EmailCC").val();
            if (txt_cc != "") {
                var cc = txt_cc.substr(0, 1);
                var emailcc = txt_emailcc.substr(0, 1);
                if (cc == ",") {
                    txt_cc = txt_cc.substr(1, txt_cc.length);
                    txt_emailcc = txt_emailcc.substr(1, txt_emailcc.length);
                    $("#ctl00_main_txt_CC").val(txt_cc);
                    $("#ctl00_main_txt_EmailCC").val(txt_emailcc);
                }
            }
        }
        else
            $("#ctl00_main_txt_CC").val("")
        if (n == 2)
            hide();
    }
    
}
