// colors
var defaultColor='white';
var alertColor='#E7EFFF';
// date and time
dateObj=new Date();
var today=dateObj.getDate()+'. '+(dateObj.getMonth()+1)+'. '+dateObj.getYear();
var prefixZeros=true;
var separator='';

// ****************************************
// window manipulation
// ****************************************

// dreamweaver section
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

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_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

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_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_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 swapAutoColor(folder,image) {
	MM_swapImage('auto','',folder+'color'+image+'.jpg',1);
}
// end dreamweaver section

// opens new window w/o any extra features
function openWin(src,name,xsize,ysize,scrolling) {
	var resize, scroll;
	switch (scrolling) {
		case 1:	// auto
			scroll = 'auto';
			resize = 'yes';
			break;
		case 2:	// yes
			scroll = 'yes';
			resize = 'yes';
			break;
		default:	// no
			scroll = 'no';
			resize = 'no';
	}
	var hscmsWin = window.open(src,name,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars='+scroll+',resizable='+resize+',width='+xsize+',height='+ysize);
}

// opens new image window w/o any extra features
function openImage(file,folder,width,height) {
  	var imageWin = window.open('/common/image_view.php?file='+file+'&folder='+folder+'&width='+width+'&height='+height,'hscms_popup','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width='+width+',height='+height);
}

// resizes window to fit its contents (may be buggy)
function makeMeFit(el,el2){
//	move=false; addY=29; addX=10;
	move=false; addY=0; addX=0;
	if (!el) e1=document.getElementById('ds1836');
	if (!eval(el)) return false;
	newY=getRealTop(el);
	if (newY>screen.availHeight){newY=screen.availHeight; addX=12; move=true;};
	if (!el2) el2=el;
	newX=getRealLeft(el2);
	if (newX>screen.availWidth){newX=screen.availWidth; addY=12; move=true};
	// check for specific window and always move to 0,0
	if (document.location.href.indexOf('nextstep.php')!=-1) {move=true;};
	if (move) self.moveTo(0,0);
	self.resizeTo(newX+addX,newY+addY);
}

// gets realY for any element
function getRealTop(el) {
    yPos = el.offsetTop;
    tempEl = el.offsetParent;
    while (tempEl != null) {
        yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
    }
    return yPos;
}

// gets realX for any element
function getRealLeft(el) {
    xPos = el.offsetLeft;
    tempEl = el.offsetParent;
    while (tempEl != null) {
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    }
    return xPos;
}

// resize window to given size, for image viewer
function resizeWindow(x,offx,y,offy) {
	window.resizeTo(x+offx,y+offy);
}

// smart resize
function smartResize(width, height, position, elementId) {
	// set all required parameters
	if (document.all) {		// IE
		availWidth = window.screen.availWidth;
		availHeight = window.screen.availHeight;
		actualX = window.screenLeft-4;		// konstanta lebo IE je blby
		actualY = window.screenTop-24;		// konstanta lebo IE je blby
	} else {				// Netscape and others
		availWidth = window.screen.availWidth;
		availHeight = window.screen.availHeight;
		actualX = window.screenX;
		actualY = window.screenY;
	}
	// smart window size, ak je dany element pre jeho zistenie a element existuje
	if (elementId) {
		if (document.all) el = document.all[elementId+'_ie'];
		else el = document.getElementById(elementId+'_moz');
		if (el) {
			width = 0;
			height = 0;
			// traverzuj az na root
			while (el.offsetParent) {
				width += el.offsetLeft;
				height += el.offsetTop;
				el = el.offsetParent;
			}
			// uprav na velkost outer width
			if (document.all) {
				// asi by to chcelo nejake jc funkcie
				width += 12;
				height += 52;
			} else {
				width += window.outerWidth - window.innerWidth;
				height += window.outerHeight - window.innerHeight;
			}
		}
	}

	switch(position) {
		case 'center_if_large':
			// width and height musia sa zmestit do okna, inak to prepadne na center
			if ((actualX+width <= availWidth) && (actualY+height <= availHeight)) {
				newX = actualX; newWidth = width;
				newY = actualY; newHeight = height;
				break;
			}
		case 'center':		// okno bude vzdy vycentrovane
			// width and X
			if (width <= availWidth) {
				newX = Math.floor((availWidth - width)/2); newWidth = width;
			} else {
				newX = 0; newWidth = availWidth;
			}
			// height and Y
			if (height <= availHeight) {
				newY = Math.floor((availHeight - height)/2); newHeight = height;
			} else {
				newY = 0; newHeight = availHeight;
			}
			break;
		case 'sticky':		// okno sa posuva tak, aby prvok ktory sa nezmesti sa dotykal okraja
			// width and X
			if (actualX+width > availWidth) {
				if (width <= availWidth) {
					newX = availWidth - width; newWidth = width;
				} else {
					newX = 0; newWidth = availWidth;
				}
			} else {
				newX = actualX; newWidth = width;
			}
			// height and Y
			if (actualY+height > availHeight) {
				if (height <= availHeight) {
					newY = availHeight - height; newHeight = height;
				} else {
					newY = 0; newHeight = availHeight;
				}
			} else {
				newY = actualY; newHeight = height;
			}
			break;
	}

	window.resizeTo(newWidth, newHeight);
	window.moveTo((newX<0?0:newX), (newY<0?0:newY));

}

// ****************************************
// dynamic style changes
// ****************************************

function applyStyle(el,className){
	el.className=className;
}

// ****************************************
// forms and form content checking
// ****************************************

// masks for from checking
inputMasks=new Array;
inputMasks['date']=/^\d{1,2}\s*[.,;:\/]{1}\s*\d{1,2}\s*[.,;:\/]{1}\s*\d{2,4}$/;
inputMasks['time']=/^\d{1,2}\s*[.,;:\/]{1}\s*\d{2}\s*(am|pm)?$/i;
inputMasks['ico']=/^(\d{8}|\d{10})$/;
inputMasks['icorc']=/^((\d{8}|\d{10})|(\d{10}))$/;
inputMasks['dic']=/^(\d{8}|\d{10})[\\\/]\d{3}$/;
inputMasks['email']=/^\w+@\w+.\w{2,5}$/;
inputMasks['phone']=/^[\d\-\/ +]{6,}$/;
inputMasks['number']=/^[\d ]+(.[\d ]+|,[\d ]+)$/;
inputMasks['string']=/[\w]+/;
inputMasks['wagonnumber']=/^(\d{12})$/;
//inputMasks['link']=/^(http|ftp):\/\/\w+\.\w+\.\w{2,5}([\w\/\.?&%_+ =]*)$/;
inputMasks['link']=/^(http|ftp):\/\/(\w+)?\.\w+\.\w{2,5}([\w\/\.?&%_+ =-]*)$/;
inputMasks['int']=/^(\d+)$/;
autoExpand=new Array;
autoExpand['dnes']=autoExpand['d']=autoExpand['today']=addDay(today,0);
autoExpand['zajtra']=autoExpand['z']=autoExpand['y']=autoExpand['tomorrow']=addDay(today,1);
autoExpand['pozajtra']=autoExpand['pz']=autoExpand['py']=autoExpand['aftertomorrow']=addDay(today,2);
autoExpand['vcera']=autoExpand['v']=autoExpand['yesterday']=addDay(today,-1);
autoExpand['predvcerom']=autoExpand['pv']=autoExpand['beforeyesterday']=addDay(today,-2);

// checks if field data matches defined format
// field can be string or regular expression
function inputMask(field) {
	var regMask=null;
	var val=field.value;
	if (!field || !field.value || !field.mask) {
		field.style.background=defaultColor;
		return;
	}

	// test for autoreplace
	if (autoExpand[val]) field.value=val=autoExpand[val];

	// test for specific stuff
	switch (field.mask) {
		case 'ico':	// strips spaces
		case 'icorc':
		case 'dic':
			reg2=new RegExp(/ /g);
			val=val.replace(reg2,'');
			break;
	}

	regMask=inputMasks[field.mask];
	if (!regMask) regMask=field.mask;
	reg=new RegExp(regMask);
	if (reg.test(val)) {
		field.style.background=defaultColor;
		return true;
	} else {
		field.focus();
		field.style.background=alertColor;
		return false;
	}
}

function blurCk() {
	if (document.getElementById('contactForm')) {
		el = document.getElementById('contactForm');
		el.innerHTML += '<input type="hidden" name="emChckHdVl" value="1" />';
	}
}

// ****************************************
// date and time manipulation
// ****************************************

// adds/subtracts date
function addDay(pa_date, pa_s) {
	var den, mes, rok, pole, vyscas, cas, cas2;
	if (!pa_date) return false;
	if (!pa_s) pa_s=0;
	datum = pa_date;

	pole = datum.split(".");
	pole[1] = pole[1] - 1;
	var cas2 = new Date(Date.UTC(pole[2], pole[1], pole[0])+(1000*60*60*24*pa_s));
	var cas = new Date(cas2.toUTCString());

	den = cas.getDate();
	mes = (cas.getMonth())+1;
	rok = cas.getYear();

	if (prefixZeros) {
		vyscas=((den<10) ? "0" : "")+den;
		vyscas+="."+separator+ ((mes<10) ? "0" : "")+mes;
		vyscas+="."+separator+ rok;
	} else vyscas=den+"."+separator+mes+"."+separator+rok;

	return vyscas;
}

// ****************************************
// misc
// ****************************************

function keyPaging(key) {
	switch(key){
		case 39:	// right
			e=document.getElementById('id_linkNext');
		break;
		case 37:	// left
			e=document.getElementById('id_linkPrevious');
		break;
		case 38:	// up
			e=document.getElementById('id_linkFirst');
		break;
		case 40:	// down
			e=document.getElementById('id_linkLast');
		break;
		case 88:	// x
			e=document.getElementById('id_linkClose');
		break;
		default:
			e=undefined;
	}
	if(e) location.href = e;
}

// ****************************************
// query manipulation
// ****************************************
function getQueryVariable(variable, myLocation) {
	myLocation = myLocation ? myLocation : window.location;
	var query = myLocation.substring(myLocation.indexOf('?')+1);
	if (!query.length) return false;

	var vars = query.split("&");
	for (var i=0; i<vars.length; i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
	return false;
}

