var monthNames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var dayNames = new Array('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
var monthdays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var mouseX, mouseY, startX, startY;
var dlgDragX, dlgDragY, dlgX, dlgY, dlgHeight, dlgWidth;
var ActivePopup, BlockingFrame, BlockingFrame1, BlockingFrame2, BlockingFrame3, BlockingFrame4, isMoving = false;
var topLeftX, topLeftY, bottomRightX, bottomRightY;

var IE = document.all ? true : false;

function ShowPopup(Title, IFrameURL, Width, Height, CanMove) {
    var html = "";
    var ImageFolder = "/Images/Nivalti";
    ImageFolder = window.location.protocol + "//" + window.location.host + ImageFolder;
    dlgHeight = Height;
    dlgWidth = Width;

    html += "<table border=0 cellspacing=0 cellpadding=0 width=" + Width + ">";
    html += "<tr height=5 valign=bottom>";
    html += "<td width=5><img src=" + ImageFolder + "/lefttop.gif></td>";
    html += "<td background=" + ImageFolder + "/top.gif colspan=2></td>";
    html += "<td width=5><img src=" + ImageFolder + "/righttop.gif></td>";
    html += "</tr>";
    html += "<tr height=21>";
    html += "<td width=5 background=" + ImageFolder + "/left.gif align=right></td>";
    html += "<td width=" + (Width - 31) + " background=" + ImageFolder + "/titlebg.gif";
    if (CanMove) {
        html += " onmousedown=DialogPopup_MouseDown()";
        document.onmousemove = MouseMove;
        document.onmouseup = DialogPopup_MouseUp;
    }
    html += "><font color=white face=Verdana size=2 style=\"cursor: default\"><b>" + Title + "</b></font></td>";
    html += "<td background=" + ImageFolder + "/titlebg.gif style=\"border-bottom: solid 1px blue\" align=right valign=top>";
    html +=     "<img src=" + ImageFolder + "/close.gif";
    html +=         " onmouseover=\"this.src='" + ImageFolder + "/close_over.gif'\" ";
    html +=         " onmousedown=\"this.src='" + ImageFolder + "/close_down.gif'\" ";
    html +=         " onmouseout=\"this.src='" + ImageFolder + "/close.gif'\" ";
    html +=         " onmouseup=\"this.src='" + ImageFolder + "/close_over.gif'\" ";
    html +=         " onclick=\"HidePopup();\"></td>";
    html += "<td width=5 background=" + ImageFolder + "/right.gif align=left></td>";
    html += "</tr>";
    html += "<tr height=" + (Height-29) + ">";
    html += "<td width=5 background=" + ImageFolder + "/left.gif align=right></td>";
    html += "<td bgcolor=Gainsboro width=100% colspan=2><iframe frameborder=0 width=100% height=100% src='" + IFrameURL + "'></iframe></td>";
    html += "<td width=5 background=" + ImageFolder + "/right.gif align=left></td>";
    html += "</tr>";
    html += "<tr height=3 valign=top>";
    html += "<td width=5><img src=" + ImageFolder + "/leftbottom.gif></td>";
    html += "<td background=" + ImageFolder + "/bottom.gif colspan=2></td>";
    html += "<td width=5><img src=" + ImageFolder + "/rightbottom.gif></td>";
    html += "</tr>";
    html += "</table>";
    
    var divPopup = document.createElement("div");
    divPopup.style.position = "absolute";
    divPopup.style.left = ((getBrowserWidth() - Width) / 2) + "px";
    divPopup.style.top = ((getBrowserHeight() - Height) / 2) + "px";
    divPopup.style.zIndex = 99999;
   	divPopup.innerHTML = html;
   	document.body.appendChild(divPopup);
   	
    ActivePopup = divPopup;
    
    var divFrame = document.createElement("div");
    document.body.appendChild(divFrame);
    
    if (IE) {
        var iFrame = document.createElement("iframe");
        iFrame.style.position = "absolute";
        iFrame.style.left = "0px";
        iFrame.style.top = "0px";
        iFrame.style.width = "100%";
        iFrame.style.height = "100%";
        iFrame.style.zIndex = 0;
        iFrame.frameBorder = "0";
        iFrame.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
        document.body.appendChild(iFrame);

        BlockingFrame = iFrame;
    } else {
        // -- 4 frames for Firefox (problem with text input cursor over IFrame)
        
        topLeftX = (getBrowserWidth() - Width) / 2 - 1;
        topLeftY = (getBrowserHeight() - Height) / 2 - 1;
        bottomRightX = topLeftX + Width;
        bottomRightY = topLeftY + Height;
        
        // -- Top frame //
        
        var iFrame1 = document.createElement("iframe");
        iFrame1.style.position = "absolute";
        iFrame1.style.left = "0px";
        iFrame1.style.top = "0px";
        iFrame1.style.width = "99%";
        iFrame1.style.height = topLeftY;
        iFrame1.style.zIndex = 1;
        iFrame1.frameBorder = "0";
        iFrame1.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
        document.body.appendChild(iFrame1);

        BlockingFrame1 = iFrame1;

        // -- Bottom frame //

        var iFrame2 = document.createElement("iframe");
        iFrame2.style.position = "absolute";
        iFrame2.style.left = "0px";
        iFrame2.style.top = bottomRightY + 1;
        iFrame2.style.width = "99%";
        iFrame2.style.height = getBrowserHeight() - bottomRightY - 1;
        iFrame2.style.zIndex = 2;
        iFrame2.frameBorder = "0";
        iFrame2.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
        document.body.appendChild(iFrame2);

        BlockingFrame2 = iFrame2;

        // -- Left frame //

        var iFrame3 = document.createElement("iframe");
        iFrame3.style.position = "absolute";
        iFrame3.style.left = "0px";
        iFrame3.style.top = "0px";
        iFrame3.style.width = ((getBrowserWidth() - Width) / 2);
        iFrame3.style.height = "99%";
        iFrame3.style.zIndex = 3;
        iFrame3.frameBorder = "0";
        iFrame3.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
        document.body.appendChild(iFrame3);

        BlockingFrame3 = iFrame3;
        
        // -- Right frame //
        
        var iFrame4 = document.createElement("iframe");
        iFrame4.style.position = "absolute";
        iFrame4.style.left = (getBrowserWidth() - Width) / 2 + Width;
        iFrame4.style.top = "0px";
        iFrame4.style.width = getBrowserWidth() - ((getBrowserWidth() - Width) / 2 + Width) - 10;
        iFrame4.style.height = "99%";
        iFrame4.style.zIndex = 4;
        iFrame4.frameBorder = "0";
        iFrame4.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
        document.body.appendChild(iFrame4);

        BlockingFrame4 = iFrame4;        
    }
    
}

function HidePopup() {
    if (isMoving) return;
    
    if (IE) {
        document.body.removeChild(BlockingFrame);
        BlockingFrame = null;
    } else {
        document.body.removeChild(BlockingFrame1);
        document.body.removeChild(BlockingFrame2);
        document.body.removeChild(BlockingFrame3);
        document.body.removeChild(BlockingFrame4);

        BlockingFrame1 = null;
        BlockingFrame2 = null;
        BlockingFrame3 = null;
        BlockingFrame4 = null;
    }
    document.body.removeChild(ActivePopup);
    ActivePopup = null;
    
    document.onmousemove = "";
    document.onmouseup = "";
}

function PostBackPopup(postBackFrom) {
    if (postBackFrom != null) {
        var postBackFromField = window.parent.document.getElementById("postBackFrom");
        if (postBackFromField != null)
            postBackFromField.value = postBackFrom;
    }
    
    window.parent.document.forms['Form1'].submit();    
}

function DialogPopup_MouseDown() {
    if (!isMoving) {
        dlgDragX = mouseX;
        dlgDragY = mouseY;
        dlgX = parseInt(ActivePopup.style.left);
        dlgY = parseInt(ActivePopup.style.top);
        isMoving = true;
    }
}

function DialogPopup_MouseUp() {
    isMoving = false;
}

function MoveDialog() {
    topLeftX = dlgX + mouseX - dlgDragX;
    topLeftY = dlgY + mouseY - dlgDragY;
    bottomRightX = topLeftX + dlgWidth;
    bottomRightY = topLeftY + dlgHeight;    
    
    ActivePopup.style.left = topLeftX;
    ActivePopup.style.top = topLeftY;
    
    if (!IE) {
        BlockingFrame1.style.height = topLeftY - 1;
        BlockingFrame2.style.top = bottomRightY + 1;
        BlockingFrame2.style.height = getBrowserHeight() - bottomRightY;
        BlockingFrame3.style.width = topLeftX - 1;
        BlockingFrame4.style.left = bottomRightX;
        BlockingFrame4.style.width = getBrowserWidth() - bottomRightX;
    }
}

function MouseMove(e) {
    if (!IE)
	{
		mouseX = e.pageX;
		mouseY = e.pageY;
	}

    if (document.all) {
        mouseX = window.event.clientX;
        mouseY = window.event.clientY;
    }
    if (isMoving) { MoveDialog(); }
}

function HideCalendar(e) {
	var cal = DatePicker_GetCaledarControl(); //document.getElementById("CalendarPopup");
	cal.style.position = 'absolute';
	cal.style.visible = "hidden";
	cal.style.width = "0px";
	cal.style.height = "0px";
	cal.innerHTML = "";
	
	document.onclick = "";
}

function ShowCalendar(e, idDatePicker, offsetX, offsetY, displayDirection) {

	var stringDate, day, month, year, forDate;
	day = DatePicker_GetMagicAjax_ID(idDatePicker + '_day').value;
	month = DatePicker_GetMagicAjax_ID(idDatePicker + '_month').value;
	year = DatePicker_GetMagicAjax_ID(idDatePicker + '_year').value;
	
	stringDate = month + ' ' + day + ',' + year;
	forDate = new Date(stringDate);
	
	startX = mouseX;
	startY = mouseY;
	
	var cal;
	cal = DatePicker_GetCaledarControl();
	cal.style.zIndex = 99999;
	
	if (cal.style.visible != "visible") {
		InitCalendar(forDate, idDatePicker, offsetX, offsetY, displayDirection);
		document.onclick = HideCalendar;
	} else {
		HideCalendar();
	}
	
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();		
	
}


function InitCalendar(forDate, idDatePicker, offsetX, offsetY, displayDirection) {
	var stringDate, day, month, year, selDate;
	day = DatePicker_GetMagicAjax_ID(idDatePicker + '_day').value;
	month = DatePicker_GetMagicAjax_ID(idDatePicker + '_month').value;
	year = DatePicker_GetMagicAjax_ID(idDatePicker + '_year').value;
	
	stringDate = month + ' ' + day + ',' + year;
	selDate = new Date(stringDate);
	if (isNaN(forDate)) {
	    forDate = new Date();
	}
	
	var cal, picker;
	cal = DatePicker_GetCaledarControl();
	picker = DatePicker_GetMagicAjax_ID(idDatePicker);
	
	cal.style.position = 'absolute';
	cal.style.visible = "visible";
	cal.style.width = "219px";
	cal.style.height = "130px";
	if (!IE) {
	    cal.style.width = "214px";
	    cal.style.height = "126px";
	}
	cal.style.left = offsetX == 0 || !IE ? DatePicker_GetX(idDatePicker) : offsetX;
	if (parseInt(startX) + parseInt(cal.style.width) - 90 > getBrowserWidth() == 'Right') {
	    cal.style.left = DatePicker_GetX(idDatePicker) - parseInt(cal.style.width) + 100;
	}
	cal.style.top = offsetY == 0 || !IE ? DatePicker_GetY(idDatePicker) + picker.offsetHeight + 6 : offsetY;
	if (parseInt(startY) + parseInt(cal.style.height) > getBrowserHeight() || displayDirection == 'Above') {
	    cal.style.top = DatePicker_GetY(idDatePicker) - parseInt(cal.style.height) - 4;
	}
		
	iframe = document.createElement("iframe");
	iframe.className = "CalendarPopup_Background";
	iframe.style.width = "100%";
	iframe.style.height = "100%";
	iframe.style.position = "absolute";
	iframe.frameBorder = "0px";
	iframe.style.left = "0px";
	iframe.style.top = "0px";
	cal.appendChild(iframe);	
	
	var divCal = document.createElement("div");
	cal.appendChild(divCal);
	divCal.style.width = "100%";
	divCal.style.height = "100%";
	divCal.className = "CalendarPopup_Calendar";
	divCal.style.position = "absolute";
	divCal.style.left = "0px";
	divCal.style.top = "0px";
	divCal.style.border = "2px solid #46397E";

	var calScript, monthName, yearName, forMonth, forDay;
	forMonth = forDate.getMonth();
	forDay = forDate.getDate();
	yearName = forDate.getFullYear();
	monthName = monthNames[forMonth];

	calScript = "<table cellpadding=0 cellspacing=0 border=0>";

	// Header with Month & Year
	calScript += "<tr><td colspan=7><table cellpadding=0 cellspacing=0 border=0 width=100%>";

	calScript += "<tr><td class=CalendarPopup_ChangeMonth onmouseover=CalendarPopup_DayOver(this) onmouseout=CalendarPopup_DayOut(this) onclick=CalendarPopup_ChangeMonth(event,-1," + yearName + "," + forMonth + "," + forDay + ",'" + idDatePicker + "'," + offsetX + "," + offsetY + ",'" + displayDirection + "')><<</td>";
	calScript += "<td class=CalendarPopup_Month width=100>" + monthName + "</td>";
	calScript += "<td class=CalendarPopup_ChangeMonth onmouseover=CalendarPopup_DayOver(this) onmouseout=CalendarPopup_DayOut(this) onclick=CalendarPopup_ChangeMonth(event,1," + yearName + "," + forMonth + "," + forDay + ",'" + idDatePicker + "'," + offsetX + "," + offsetY + ",'" + displayDirection + "')>>></td>";
	
	calScript += "<td class=CalendarPopup_ChangeMonth onmouseover=CalendarPopup_DayOver(this) onmouseout=CalendarPopup_DayOut(this) onclick=CalendarPopup_ChangeMonth(event,-12," + yearName + "," + forMonth + "," + forDay + ",'" + idDatePicker + "'," + offsetX + "," + offsetY + ",'" + displayDirection + "')><<</td>";
	calScript += "<td class=CalendarPopup_Month>" + yearName + "</td>";
	calScript += "<td class=CalendarPopup_ChangeMonth onmouseover=CalendarPopup_DayOver(this) onmouseout=CalendarPopup_DayOut(this) onclick=CalendarPopup_ChangeMonth(event,12," + yearName + "," + forMonth + "," + forDay + ",'" + idDatePicker + "'," + offsetX + "," + offsetY + ",'" + displayDirection + "')>>></td>";

	calScript += "</tr></table><td></tr>";
	
	// Header with Day of Week
	calScript += "<tr>";
	for (var ind=0; ind<7; ind++) {
		calScript += "<td class=CalendarPopup_WeekDay>" + dayNames[ind] + "</td>";
	}
	calScript += "</tr>";

	// Other days
	var startDate = new Date(yearName, forMonth, forDay);
	startDate.setDate(startDate.getDate() - startDate.getDay() + 1);
	var stopDate = new Date(yearName, forMonth, 1);
	while (startDate >= stopDate) {
		startDate.setDate(startDate.getDate() - 7);
	}

	var subScript = "", selDateText = "", dayText;
	var today = new Date();
	var todayText = "" + today.getFullYear() + "," + today.getMonth() + "," + today.getDate();
	if (!isNaN(selDate)) {
	    selDateText = "" + selDate.getFullYear() + "," + selDate.getMonth() + "," + selDate.getDate();
	}

	for (var indWeek=0; indWeek<6; indWeek++) {
		calScript += "<tr>";
		for (var indDay=0; indDay<7; indDay++) {
		    day = startDate.getDate();
		    month = startDate.getMonth();
		    dayText = "" + startDate.getFullYear() + "," + month + "," + day;
		    if (dayText == todayText) {
		        calScript += "<td class=CalendarPopup_Today";
		    } else {
			    if (month == forMonth) {
				    if (dayText == selDateText) {
					    calScript += "<td class=CalendarPopup_SelectedDay";
				    } else {
					    if (indDay==5 || indDay==6) {
						    calScript += "<td class=CalendarPopup_HolliDay";
					    } else {
						    calScript += "<td class=CalendarPopup_WorkDay";
					    }
				    }
			    } else {
				    calScript += "<td class=CalendarPopup_OtherDay";
			    }
			}
			subScript = " onclick=CalendarPopup_Click(event," + dayText + ",'" + idDatePicker + "')"
			if (dayText != selDateText) {
				subScript += " onmouseover=CalendarPopup_DayOver(this) onmouseout=CalendarPopup_DayOut(this)"
			} 
			subScript += ">" + day;
			startDate.setDate(day + 1);
			subScript += "</td>";
			calScript += subScript;
		}
		calScript += "</tr>";
	}
	
	calScript += "</tr></table>";

	divCal.innerHTML = calScript;
}

function CalendarPopup_DayOver(cell) {
	cell.style.backgroundColor = "#B2CDF8";
}

function CalendarPopup_DayOut(cell) {
	cell.style.backgroundColor = "#F3F5FC"; //"Gainsboro";
}

function CalendarPopup_Click(e, year, month, day, idDatePicker) {
	DatePicker_GetMagicAjax_ID(idDatePicker + '_day').value = day<10?'0' + day:day;
	DatePicker_GetMagicAjax_ID(idDatePicker + '_month').value = monthNames[month].substring(0,3);
	DatePicker_GetMagicAjax_ID(idDatePicker + '_year').value = year;
	
	HideCalendar();
}

function CalendarPopup_ChangeMonth(e, next, year, month, day, idDatePicker, offsetX, offsetY, displayDirection) {
	var nextDate = new Date(year, month+next, day);
	InitCalendar(nextDate, idDatePicker, offsetX, offsetY, displayDirection);
	
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();	
}

function DatePicker_GetX(idDatePicker)
{
	var obj = DatePicker_GetMagicAjax_ID(idDatePicker);
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) {
		curleft += obj.x;
	}
	return parseInt(curleft);// + parseInt(document.body.leftMargin);
}

function DatePicker_GetY(idDatePicker)
{
	var obj = DatePicker_GetMagicAjax_ID(idDatePicker);
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return parseInt(curtop);// + parseInt(document.body.topMargin);
}

function DatePicker_GetMagicAjax_ID(idDatePickerChild) {
	var arr = document.getElementsByTagName("input");
	for (var i=0; i<arr.length; i++) {
		if (arr[i].id.indexOf(idDatePickerChild) >= 0) {
			return(arr[i]);
		}
	}	
}

function DatePicker_GetCaledarControl() {
	var arr = document.getElementsByTagName("div");
	for (var i=0; i<arr.length; i++) {
		if (arr[i].id.indexOf("CalendarPopup") >= 0) {
			return(arr[i]);
		}
	}	
}

function getBrowserType() {
    if (typeof(window.innerWidth) == 'number')
    {
        return "Non-IE";
    } else
    {
        return "IE";
    }
}

function getBrowserWidth() {
	var myWidth = 0;
	if (typeof(window.innerWidth) == 'number') {
		//Non-IE
		myWidth = window.innerWidth;
	} else if (document.documentElement && document.documentElement.clientWidth) {
        //IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
	} else if (document.body && document.body.clientWidth) {
        //IE 4 compatible
		myWidth = document.body.clientWidth;
	}
	return myWidth;
}

function getBrowserHeight() {
	var myHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		//Non-IE
		myHeight = window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
        //IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
	} else if (document.body && document.body.clientHeight) {
        //IE 4 compatible
		myHeight = document.body.clientHeight;
	}
	return myHeight;
}

function closeIFrame(postBackFrom) {
    if (postBackFrom != null) {
        var postBackFromField = window.parent.document.getElementById("postBackFrom");
        if (postBackFromField != null)
            postBackFromField.value = postBackFrom;
    }
    
    window.parent.document.forms['Form1'].submit();    
}

function openWindow(page, width, height) {
	x = (640 - width)/2, y = (480 - height)/2;

	if (screen) {
		y = (screen.availHeight - height)/2;
		x = (screen.availWidth - width)/2;
	}

	window.open(page,'','width='+width+',height='+height+',screenX='+x+',screenY='+y+',top='+y+',left='+x+', toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no');
}

