
/* Element */


function Element_GetXY(ObjRefOrId)
{
	var oR = {x:0, y:0};

	var o = typeof(ObjRefOrId) == 'string' ? document.getElementById(ObjRefOrId) : ObjRefOrId;
	if(!o) return oR;


	if(o.getBoundingClientRect)
	{
		var r = o.getBoundingClientRect(), 
			x = document.documentElement.scrollLeft || document.body.scrollLeft, 
			y = document.documentElement.scrollTop || document.body.scrollTop;
		oR = {x: r.left + x - 2, y: r.top + y - 2};
	}
	else
	{
		if(document.getBoxObjectFor)
		{
			var r = document.getBoxObjectFor(o);
			oR = {x: r.x, y: r.y};
			
			var p = o.parentNode;
			
            while (p && p.tagName.toUpperCase() != 'BODY' && p.tagName.toUpperCase() != 'HTML')
            {
              oR.x -= p.scrollLeft;
              oR.y -= p.scrollTop;

               p = p.parentNode;
            }
		}
		else
		{
			var p = o.offsetParent;
			if (p != o)
			{
				while (p)
				{
					oR.x += p.offsetLeft;
					oR.y += p.offsetTop;
					p=p.offsetParent;
				}
			}
		}
	}

	return oR;
}



function Element_GetWidth(ObjRefOrId)
{
	var iW, o = typeof(ObjRefOrId) == 'string' ? document.getElementById(ObjRefOrId) : ObjRefOrId;
	if(!o) return 0;

	if(o.style.display == 'none')
	{
		o.style.display = '';
		iW = o.offsetWidth;
		o.style.display = 'none';
	}
	else
		iW = o.offsetWidth;

	return iW;
}



function Element_GetHeight(ObjRefOrId)
{
	var iH, o = typeof(ObjRefOrId) == 'string' ? document.getElementById(ObjRefOrId) : ObjRefOrId;
	if(!o) return 0;

	if(o.style.display == 'none')
	{
		o.style.display = '';
		iH = o.offsetHeight;
		o.style.display = 'none';
	}
	else
		iH = o.offsetHeight;

	return iH;
}



function Element_SetWidth(ObjRefOrId, Height)
{
	var iW, o = typeof(ObjRefOrId) == 'string' ? document.getElementById(ObjRefOrId) : ObjRefOrId;
	if(!o) return;
	
	o.style.width = parseInt(Height) + 'px';
}



function Element_SetDisplay(ObjRefOrId, Show)
{
	var o = typeof(ObjRefOrId) == 'string' ? document.getElementById(ObjRefOrId) : ObjRefOrId;
	if (o) 
		o.style.display = Show ? '' : 'none';
}





/* Event */


function Event_AddListner(ObjRef, EvtName, FncRef)
{
	if(!ObjRef)return;
	if(window.attachEvent)ObjRef.attachEvent("on"+EvtName.toLowerCase(),FncRef);
	else if(window.addEventListener)ObjRef.addEventListener(EvtName.toLowerCase(),FncRef, false);
}





/* PopUp Window */


function PopUp_Open(URL, WindowName, Width, Height)
{
	var w = screen.availWidth, h = screen.availHeight, l = 0, t = 0;
	
	l = Math.round((w-Width-10)/2);
	t = Math.round((h-Height-30)/2);

	var win = window.open(URL,WindowName, "width="+Width+",height="+Height+",left="+l+",top="+t+",location=0,menubar=0,resizable=1,scrollbars=1,status=1,toolbar=0");
	if (win && window.focus) win.focus();
}





/* Cookies */


function Cookie_Get(sName)
{
	var i, v = document.cookie.split(';'), iB, s;
	
	sName = escape(sName);
	
	for (i = 0; i < v.length; i++)
	{
		s = v[i].replace(/^\s+/, '');
		
		iB = s.indexOf(sName + '=');
		if (iB == 0)
			return unescape(s.substr(iB + sName.length + 1));
	}
	return null;
}

function Cookie_Set(sName, sValue, iSeconds)
{
	var sE = "";

	if (iSeconds)
	{
		var d = new Date();
		d.setTime(d.getTime() + iSeconds * 1000);
		sE = '; expires=' + d.toGMTString();
	} 

	document.cookie = escape(sName) + '=' + escape(sValue) + sE + '; path=/';
}

function Cookie_Delete(sName)
{
	Cookie_Set(sName, '', -1);
}




/* String */

function String_Trim(s)
{
	return typeof(s) == 'undefined' ? '' : s.toString().replace(/^\s+|\s+$/g, '');
}


/* Email */

function CheckEmailAddress(field)
{
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	var goodEmail = filter.test(field);
	return goodEmail
}


/* DateTime */

var oDateTimeConfig =
{
	en_US:
	{
		MonthsLongNames: ['January','February','March','April','May','June','July','August','September','October','November','December']
	},
	ro_RO:
	{
		MonthsLongNames: ['Ianuarie','Februarie','Martie','Aprilie','Mai','Iunie','Iulie','August','Septembrie','Octombrie','Noiembrie','Decembrie']
	},
	cs_CZ:
	{
		MonthsLongNames: ['Leden','Únor','Březen','Duben','Květen','Červen','Červenec','Srpen','Září','Říjen','Listopad','Prosinec']
	}
};


function DateTime_ToXmlString(d)
{
	var s = 
		d.getFullYear() + "-" + 
		(d.getMonth() < 9 ? "0" + (d.getMonth() + 1) : (d.getMonth() + 1)) + "-" + 
		(d.getDate() < 10 ? "0" + d.getDate() : d.getDate()) + "T" + 
		(d.getHours() < 10 ? "0" + d.getHours() : d.getHours()) + ":" + 
		(d.getMinutes() < 10 ? "0" + d.getMinutes() : d.getMinutes()) + ":" + 
		(d.getSeconds() < 10 ? "0" + d.getSeconds() : d.getSeconds());
	
	return s;
}

function DateTime_ToDateString(d, sLangId)
{
	sLangId = String_Trim(sLangId);
	var oMonths = oDateTimeConfig[sLangId] ? oDateTimeConfig[sLangId].MonthsLongNames : oDateTimeConfig.en_US.MonthsLongNames;
	var s = "";

	switch (sLangId)
	{
		case 'ro_RO':
		case 'cs_CZ':
			s = d.getDate() + " " + oMonths[d.getMonth()] + " " + d.getFullYear();
			break;

		case 'en_US':
		default:
			s = oMonths[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear();
			break;
	}

	return s;
}



/* Loader */

function Loader_Show(sElementId)
{
	var o = document.getElementById('LoaderMaskDiv');
	
	if (!o)
	{
		o = document.createElement("DIV");
		
		if(o && document.body.firstChild)
		{
			o = document.body.insertBefore(o, document.body.firstChild);
			if (o)
			{
				var sThemeURL = typeof(PageConfig) != 'undefined' && PageConfig.ThemeURL ? PageConfig.ThemeURL : '../App_Themes/1';
				
				o.id = 'LoaderMaskDiv';
				o.style.position = 'absolute';
				o.style.top = '-1000px';
				o.style.left = '-1000px';
				o.style.zIndex = 255;
				o.style.width = '400px';
				o.style.height = '400px';
				o.style.display = 'none';
				o.innerHTML = '<div style="width: 100%; height: 100%; background-color: #ebebeb; filter:alpha(opacity=50); -moz-opacity:0.5; opacity: 0.5;" onclick="return false;"><img id="LoaderMaskImg" src="' + sThemeURL + '/i/loading.gif" width="40" height="40" alt="" vspace="0" /></div>';
			}
		}
	}
	
	if (!o) return;
	
	var oLCD = document.getElementById(sElementId);
	if (oLCD)
	{
		var oXY = Element_GetXY(oLCD), iW = Element_GetWidth(oLCD), iH = Element_GetHeight(oLCD);

		var oImg = document.getElementById('LoaderMaskImg');
		if (oImg)
		{
			oImg.style.marginLeft = (parseInt(iW / 2) - 40) + 'px';
			oImg.style.marginTop = (parseInt(iH / 2) - 40) + 'px';
		}
		
		o.style.width = iW + 'px';
		o.style.height = (iH + (document.all ? 0 : 20)) + 'px';
		o.style.left = oXY.x + 'px';
		o.style.top = oXY.y + 'px';
		o.style.display = '';
	}
}

function Loader_Hide()
{
	var o = document.getElementById('LoaderMaskDiv');
	
	if (o)
	{
		o.style.left = '-10000px';
		o.style.top = '-10000px';
		o.style.display = 'none';
	}
}



/* Title */

function Title_Show(obj, message, width, dx, dy)
{
	var oD = document.getElementById('TitleDiv');
	
	if (!oD)
	{
		oD = document.createElement("DIV");
		if (oD)
		{
			oD = document.body.insertBefore(oD, document.body.firstChild);
			
			if (oD)
			{
				oD.id = 'TitleDiv';
				oD.style.display = '';
				oD.style.overflow = 'visible';
				oD.style.zIndex = 255;
				oD.style.position = 'absolute';
				oD.style.left = '-10000px';
				oD.style.top = '-10000px';
				oD.className = 'html_element_title_div';
				oD.style.width = width + 'px';
			}
		}
	}

	if (oD)
	{
		oD.innerHTML = message;
		oD.style.width = width + 'px';
		
		var oXY = Element_GetXY(obj);
		if (oXY)
		{
			oD.style.left = oXY.x + dx + 'px';
			oD.style.top = oXY.y + dy + 'px';
		}
	}
}

function Title_Hide()
{
	var oD = document.getElementById('TitleDiv');
	if (oD)
	{
		oD.style.left = '-10000px';
		oD.style.top = '-10000px';
	}
}
