// JavaScript Document

var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if(msie>0) iMsieVersion = parseInt (ua.substring (msie+5, ua.indexOf (".", msie )));
else iMsieVersion = 0;
iThumbnails_Delta = 0;

// detect user agent -- BEGIN
var sOcms_BrowserName = "";
var sOcms_BrowserVersionMajor = "";
var sOcms_BrowserVersionMinor = "";
var sUserAgent = window.navigator.userAgent;

if(/Firefox[\/\s](\d+\.\d+)/.test(sUserAgent)) sOcms_BrowserName = "Firefox";
else if(/MSIE[\/\s](\d+\.\d+);/.test(sUserAgent)) sOcms_BrowserName = "MSIE";
else if(/Opera[\/\s](\d+\.\d+)/.test(sUserAgent)) sOcms_BrowserName = "Opera";
else if(/Chrome[\/\s](\d+\.\d+)/.test(sUserAgent)) sOcms_BrowserName = "Chrome";
else if(/Version\/(\d+\.\d+)\s+Safari/.test(sUserAgent)) sOcms_BrowserName = "Safari";
a = RegExp.$1.split(/\./);
if(a && a.length) {
	sOcms_BrowserVersionMajor = a[0];
	sOcms_BrowserVersionMinor = a[1];
}
// detect user agent -- END

function popup(url_address,window_width,window_height,framename,additional) {
  sx = (screen.availWidth - window_width) / 2;
  sy = (screen.availHeight - window_height) / 2;
  params = "width="+window_width+",height="+window_height+",Left="+sx+",Top="+sy+",screenX="+sx+",screenY="+sy;
  if(additional>"") params += "," + additional;
  nw = window.open(url_address, framename, params);
  if(nw) nw.focus();
  return false;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function checkDateMy(frm) {
	var yearmonth = frm.yearmonth;
	var day = frm.day;
	var dyearmonth = frm.dyearmonth;
	var dday = frm.dday;
	var start = yearmonth.value+'-'+day.value;
	var end = dyearmonth.value+'-'+dday.value;
	if(start>=end) {
		var DayIndex = day.selectedIndex;
		var YearIndex = yearmonth.selectedIndex;
		if ((DayIndex+1)>30){
			if (YearIndex==dyearmonth.options.length-1){
				var newDayIndex = DayIndex;
				var newYearIndex = YearIndex;
			}else{
				var newDayIndex = 0;
				var newYearIndex = YearIndex+1;
			}
		}else{
			var newDayIndex = DayIndex+1;
			var newYearIndex = YearIndex;
		}
		dday.selectedIndex = newDayIndex;
		dyearmonth.selectedIndex = newYearIndex;
	}
}

function GoViaPost(sLink, sParams) {
	var oForm = document.createElement("FORM");
	if(oForm) {
		oForm.setAttribute("method", "POST");
		oForm.setAttribute("action", sLink);
		if(sParams) {
			a = sParams.split(/\&/);
			if(a && a.length) {
				for(i=0; i<=a.length-1; i++) {
					b = a[i].split(/\=/);
					oInput = document.createElement("INPUT");
					oInput.setAttribute("type", "HIDDEN");
					oInput.setAttribute("name", b[0]);
					oInput.setAttribute("value", b[1]);
					oForm.appendChild(oInput);
				}
			}
		}
		document.body.appendChild(oForm);
		oForm.submit();
	}
	return false;
}

function command(sText) { return false; }
function go(sText) { return false; }
function openpopup(sText) { return go(sText); }

function ShowInStatusbar(sText) {
	window.status = sText;
}

function ValidateOnlyIntegers(oInput) {
	if(!oInput) return "";
	if(!oInput.value) return "";
	var str = oInput.value;
	var new_str = "";
	for(var i=0; i<=str.length-1; i++) {
		ch = str.substr(i,1);
		if(ch>="0" && ch<="9") new_str += ch;
	}
	return new_str;
}

function PrepareLoadDataPage() {
	var oIframe = document.createElement("IFRAME");
	if(!oIframe) {
		for(var i=1; i<1000; i++) {
			document.body.focus();
			oIframe = document.createElement("IFRAME");
			if(oIframe) break;
		}
	}
	if(oIframe) {
		oIframe.style.width = "0px";
		oIframe.style.height = "0px";
		oIframe.style.display = "none";
		oIframe.style.visibility = "hidden";
		oIframe.style.border = "0px";
		oIframe.style.margin = "0px";
		oIframe = document.body.appendChild(oIframe);
		//oIframe.src = "/inc/prepareloaddatapage.php";
	}
	
	//var oIframe = document.getElementById('prepareloaddatapage');
	if(oIframe) {
		oIframe.setAttribute("src", "/inc/prepareloaddatapage.php");
		for(i=1; i<=100; i++) {
			if(oIframe.contentWindow.document && oIframe.contentWindow.document.body && oIframe.contentWindow.document.body.innerHTML=="LOADED") {
				return true;
			}
			document.body.focus();
		}
	}
	return true;
}

function CloseAnyOcmsInfoPanel() {
	var a = document.getElementsByTagName("DIV");
	if(a && a.length) for(var i=0; i<=a.length-1; i++) {
		if(a[i] && a[i].id && a[i].id.search(/^ocmsinfopanel/)!=-1) {
			a[i].style.display = "none";
		}
	}
	return false;
}

function GegIsoDate(iPlusDays, oDate, sDate) {
	if(!oDate) {
		if(!sDate) oDate = new Date();
		else {
			var a = sDate.split("-");
			if(a && a.length && a.length==3) {
				oDate = new Date(1*a[0], 1*a[1] - 1, 1*a[2]);
			}
		}
	}
	if(iPlusDays) oDate.setTime( oDate.getTime() + 24*60*60*1000 );
	var iDay = 1*(oDate.getDate());
	var sDay = (iDay<10 ? "0" : "") + iDay;
	var iMonth = (1*oDate.getMonth()) + 1;
	var sMonth = (iMonth<10 ? "0" : "") + iMonth;
	return oDate.getFullYear() + "-" + sMonth + "-" + sDay;
}

function GetDhtmlPosition(oNode) {
	var iLeft = 0;
	var iTop = 0;
	var oParentNode = oNode;
	while(oParentNode) {
		iLeft += oParentNode.offsetLeft;
		iTop += oParentNode.offsetTop;
		oParentNode = oParentNode.offsetParent;
	}
	return new Array(iLeft, iTop);
}


function ScrollElementToTheTop(oElement) {
	do {
		self.scrollBy(0,iThumbnails_Delta);
	} while(ScrollElementToTheTop_MustScrollThis(oElement));
}

function ScrollElementToTheTop_MustScrollThis(oElement) {
	var Y = document.body.offsetHeight;
	var iScrollY = 0;
	if(document.all) {
    iScrollY += document.body.scrollTop;
    if(iMsieVersion<=6) {
    	for(var i=0; i<=document.childNodes.length-1; i++) {
    		if(document.childNodes[i].tagName=="HTML") {
    			iScrollY += document.childNodes[i].scrollTop;
    			break;
    		}
    	}
    }
    else {
	    if(document.body.parentNode && typeof document.body.parentNode.scrollTop != "undefined") {
	      iScrollY += document.body.parentNode.scrollTop;
	    }
	  }
	}
	else iScrollY = self.scrollY;
	var iTop = oElement.offsetTop;
	var oParent = oElement.offsetParent;
	while(oParent) {
		iTop += oParent.offsetTop;
		oParent = oParent.offsetParent;
	}
	if(iTop<iScrollY && iScrollY>0) return -1;
	else if(iTop>iScrollY && iTop+oElement.offsetHeight-iScrollY>Y) return 1;
	else return 0;
}

function DoPublicLogin(iFormIndex) {
	var oForm = document.getElementById("ocmspubliclogoutform_" + iFormIndex);
	if(oForm) oForm.submit();
	return false;
}

function GetPositionAndDimmension(oNode) {
	var iLeft = 0;
	var iTop = 0;
	var aParams = new Array();
	if(!oNode) return aParams;
	var oParentNode = oNode;
	while(oParentNode) {
		iLeft += oParentNode.offsetLeft;
		iTop += oParentNode.offsetTop;
		oParentNode = oParentNode.offsetParent;
	};
	aParams["left"] = iLeft;
	aParams["top"] = iTop;
	aParams["width"] = oNode.offsetWidth;
	aParams["height"] = oNode.offsetHeight;
	return aParams;
}

function SetNewTextInCell(oCell, sNewText) {
	if(!oCell) return;
	if(oCell.tagName=="INPUT") {
		oCell.value = sNewText;
		return;
	}
	var oNewTextNode = document.createTextNode(sNewText);
	if(!oNewTextNode) return;
	if(!oCell.childNodes || !oCell.childNodes.length || !oCell.childNodes(0)) {
		oCell.appendChild(oNewTextNode);
	}
	else {
  	var oReplaceNode = oCell.childNodes(0);
		oReplaceNode.replaceNode(oNewTextNode);
	}
}

function FlashWindowHref(url) {
	window.location.href = url;
}

function GetDateObjectFromISODateString(sISODate) {
	if(!sISODate) return null;
	var a = sISODate.split("-");
	var m = 1*a[1] - 1;
	var oDate = new Date();
	oDate.setFullYear(1*a[0]);
	oDate.setMonth(m);
	oDate.setDate(1*a[2]);
	return oDate;
}

function GetNightsBetween2ISODates(sISODateNew, sISODateOld) {
	var oDateNew = GetDateObjectFromISODateString(sISODateNew);
	if(!oDateNew) return -1;
	var oDateOld = GetDateObjectFromISODateString(sISODateOld);
	if(!oDateOld) return -1;
	var iMSecInDay = 1000*60*60*24;
	return Math.floor( (oDateNew.getTime()-oDateOld.getTime()) / iMSecInDay);
}

function SetOpacity(oElement, iOpacity) {
	if(sOcms_BrowserName=="MSIE") {
		if(sOcms_BrowserVersionMajor<8) {
			oElement.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + iOpacity + ")";
		}
		else {
			oElement.style.msFilter = "alpha(opacity=" + iOpacity + ")";
		}
	}
	else {
		iOpacity /= 100;
		oElement.style.opacity = iOpacity;
	}
}

function SetSelectedIndex(oSel, iIndex) {
	if(!oSel) return;
	if(sOcms_BrowserName=="MSIE") {
		if(oSel.options[iIndex]) oSel.options[iIndex].setAttribute('selected',true);
	}
	else {
		oSel.selectedIndex = iIndex;
	}
}

