/**************************************************************************************************
* Common JavaScript functions (client side execution)
*
* @author Krzysztof Chmielewski <chris@artcom-kolo.com.pl>
* @version 2.0 (2004-01-02)
***************************************************************************************************/

// Set of definitions, mirrored from /Includes/Globals/classes/Defs.class -----

var UNDEF = 'undefined';
var OK = 'ok';
var ERROR = 'error';

var reNULL = '';
var reNO = '^nie($| )'; var regexIsNo = /\^nie\(\$\| \)/;
var reANY = '.';
//var reEMAIL = '.*[^.]+@.*[^.]+\.[a-z]+[a-z]+$';
var reEMAIL = '^.+@.+[.][a-z]{2,4}$';
var reDATE = '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]';
var reINT = '\d|[ ]';
var reDOUBLE = '\d|[., ]';


var acceptEMAIL = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890-@_."';
var acceptURL = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890-=!@#$%^&*()_+[]{}~,./<>?:;"';
var acceptEN = ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890-=!@#$%^&*()_+[]{}~,./<>?:;"';
var acceptPL = ' aąbcćdeęfghijklłmnńoópqrsśtuvwxyzżźAĄBCĆDEĘFGHIJKLŁMNŃOÓPQRSŚTUVWXYZŻŹ01234567890-=!@#$%^&*()_+[]{}~,./<>?:;"';

var illegalCHARS ='\\';

// Firebug compelmentary object -----------------------------------------------------------
if(!console) {
	//alert('no console')
	var console = 
	{
		log: function () {},
		debug: function () {},	
		info: function () {},	
		warn: function () {},	
		error: function () {},	
		assert: function () {},	
		count: function () {},	
		time: function () {},	
		timeEnd: function () {},	
		profile: function () {},
		profileEnd: function () {},
		trace: function () {},
		group: function () {},
		groupEnd: function () {},
		dir: function () {},
		dirxml: function () {}
	};
}	


function formatCurr(val, lang) 
{
	if(!lang) lang = 'pl';
	
	val = String(Math.round(val*100));
	
	switch(val.length)
	{
		case 1: val = '00' + val; break;
		case 2: val = '0' + val; break;
	}
	
	r = val.substring(0,val.length-2) +','+ val.substr(val.length-2);
	if(r.length > 6) {r = r.substring(0,val.length-5) +'.'+ r.substr(val.length-5);}
//	else 
//		if(r.lenght > 7) {r = r.substring(0,val.length-9) +'.'+ r.substr(val.length-9);}
//		else {r = r.substring(0,val.length-13) +'.'+ r.substr(val.length-13)}
	
	return r;
}


// Set of functions -----------------------------------------------------------

function syncCaption(title) {

	if(document.location.hostname == 'localhost') title = '(l) ' + title;
	if(String(document.location.hostname).match(/^amica$|artcom-kolo/)) title = '(a) ' + title;
  if(top) {
    top.document.title = title;
  }
  document.title = title;
}

function powrot(win, refresh) {
	if(window == top) { 
		if(opener && refresh) win.opener.location.reload();
		win.opener.focus(); 
		win.close(); 
	}
	else 
		history.back();
}

function go2anchor(name) {
	this.location = '#'+ name;
}


function openPopUp(x, y, w, h, name, page) {

	winPopUp = window.open(page,name, 'location=no,menubar=no,scrollbars=yes,status=yes,resizable=yes,dependent=yes,width='+ w +',height='+ h +',screenX='+ x+',screenY='+ y +',left='+ x+',top='+ y +'');
	if(!winPopUp) 
		alert('Okienko pomocnicze nie zostało otwarte z powodu blokady pop-up.')
	else 
		winPopUp.focus();
	return winPopUp;
}

function openPopUpFullLocation(x, y, w, h, name, page) {

	winPopUp = window.open(page,name, 'toolbar=yes,location=yes,menubar=no,scrollbars=yes,status=yes,resizable=yes,dependent=yes,width='+ w +',height='+ h +',screenX='+ x+',screenY='+ y +',left='+ x+',top='+ y +'');
	if(!winPopUp) 
		alert('Okienko pomocnicze nie zostało otwarte z powodu blokady pop-up.')
	else 
		winPopUp.focus();
	return winPopUp;
}

function openPopUpFull(x, y, w, h, name, page) {

	winPopUp = window.open(page,name, 'toolbar=yes,location=no,menubar=no,scrollbars=yes,status=yes,resizable=yes,dependent=yes,width='+ w +',height='+ h +',screenX='+ x+',screenY='+ y +',left='+ x+',top='+ y +'');
	if(!winPopUp) 
		alert('Okienko pomocnicze nie zostało otwarte z powodu blokady pop-up.')
	else 
		winPopUp.focus();
	return winPopUp;
}

function openPopUpNoStatus(x, y, w, h, name, page) {

	winPopUp = window.open(page,name, 'location=no,menubar=no,scrollbars=yes,status=no,resizable=yes,dependent=yes,width='+ w +',height='+ h +',screenX='+ x+',screenY='+ y +',left='+ x+',top='+ y +'');
	if(!winPopUp) 
		alert('Okienko pomocnicze nie zostało otwarte z powodu blokady pop-up.')
	else 
		winPopUp.focus();
	return winPopUp;
}

function openPopUpWithStatus(x, y, w, h, name, page) {

	winPopUp = window.open(page,name, 'location=no,menubar=no,scrollbars=no,status=yes,resizable=no,dependent=yes,width='+ w +',height='+ h +',screenX='+ x+',screenY='+ y +',left='+ x+',top='+ y +'');
	if(!winPopUp) 
		alert('Okienko pomocnicze nie zostało otwarte z powodu blokady pop-up.')
	else 
		winPopUp.focus();
	return winPopUp;
}



function openPopUpNoBars(x, y, w, h, name, page) {

	winPopUp = window.open(page,name, 'location=no,menubar=no,scrollbars=no,status=no,resizable=no,dependent=yes,width='+ w +',height='+ h +',screenX='+ x+',screenY='+ y +',left='+ x+',top='+ y +'');
	if(!winPopUp) 
		alert('Okienko pomocnicze nie zostało otwarte z powodu blokady pop-up.')
	else 
		winPopUp.focus();
	return winPopUp;
}


function openImage(x, y, w, h, name, url) {		// nowe okno z obrazkiem

  var WinName = name;
  winImage = openPopUpNoStatus(x, y, w+40, h+40, name, url);
	if(!winImage) 
		alert('Okienko pomocnicze nie zostało otwarte z powodu blokady pop-up.')
	else 
		winImage.focus();
}


function openPdf(url) {		// nowe okno z pdf-em

  var WinName = "pdfWin" + String(Math.random()).substring(2);
  winPdf = window.open(url,WinName, "location=no,menubar=yes,status=yes,resizable=yes,dependent=yes");
	if(!winPdf) 
		alert('Okienko pomocnicze nie zostało otwarte z powodu blokady pop-up.')
	else 
		winPdf.focus();
}

function openMovie(url, width, height) {
	window_height = navigator.userAgent.match(/Gecko/)? (height+46):(height+65);
	win_Movie = openPopUpNoBars(100, 60, width, window_height, 'Movie', '/Commons/Files/load/movieload.php?w='+ width +'&h='+ height +'&u='+ url);
}


function openSendLink(uprawnienia) {	// nowe okno z linkiem do wysłania znajomemu

  var link = "/Commons/Files/mailings/link.send.prep.php?tytul=" + (document.title) + "&link=" + escape(location.protocol + '//' + location.hostname + location.port + top.location.pathname + top.location.search + top.location.hash) + "&upr=" + escape(uprawnienia);
	//alert(link);
  sendWindow = window.open(link,'send', 'location=no,menubar=no,status=no,resizable=no,dependent=yes,width=320,height=460,screenX=50,screenY=150,left=50,top=150')
	if(!sendWindow) 
		alert('Okienko pomocnicze nie zostało otwarte z powodu blokady pop-up.')
	else 
		sendWindow.focus();
}


function openSendOpinion(uprawnienia) {	// nowe okno z opinią

  var link = "/Commons/Files/mailings/opinion.send.prep.php?tytul=" + (document.title) + "&link=" + escape(location.protocol + '//' + location.hostname + location.port + top.location.pathname + top.location.search + top.location.hash) + "&upr=" + escape(uprawnienia);
	//alert(link);
  opinionWindow = window.open(link,'opinion', 'location=no,menubar=no,status=no,resizable=no,dependent=yes,width=330,height=460,screenX=50,screenY=150,left=50,top=150')
	if(!opinionWindow) 
		alert('Okienko pomocnicze nie zostało otwarte z powodu blokady pop-up.')
	else 
		opinionWindow.focus();
}



function openSendComment(uprawnienia) {	// nowe okno z koentarzem

  var link = "/Commons/Files/mailings/comment.send.prep.php?tytul=" + (document.title) + "&link=" +  top.location.pathname  + "&upr=" + escape(uprawnienia);
	//alert(link);
  commentWindow = window.open(link,'sendComment', 'location=no,menubar=no,status=no,resizable=no,dependent=yes,width=320,height=460,screenX=50,screenY=150,left=50,top=150')
	if(!commentWindow) 
		alert('Okienko pomocnicze nie zostało otwarte z powodu blokady pop-up.')
	else 
		commentWindow.focus();
}


function openSendFile(script, anchor, uprawnienia) {	// nowe okno do wysyłania plików

  var link = "/Commons/Files/mailings/file.send.prep.php?tytul=" + (document.title) + "&link=" +  top.location.pathname  + "&script=" + escape(script)+ "&anchor=" + escape(anchor) + "&upr=" + escape(uprawnienia);
	//alert(link);
  fileWindow = window.open(link,'sendFile', 'location=no,menubar=no,status=no,resizable=no,dependent=yes,width=335,height=460,screenX=50,screenY=150,left=50,top=150')
	if(!fileWindow) 
		alert('Okienko pomocnicze nie zostało otwarte z powodu blokady pop-up.')
	else 
		fileWindow.focus();
}


function add2Favorite() {	// dodaj do ulubionych - wstęp w .js (dalej funkcja z common.vbs)
var re = /MSIE 4|MSIE 5|MSIE 6/;

	if(!re.test(navigator.userAgent)) {
		alert("Ta funkcja działa tylko w przeglądarce Microsoft Internet Explorer 4 lub wyższej  ");
	} else {
		vb_add2Favorite();
	}
}

function getFullServer(strURL) {	// docelowo niepotrzebne
  var re = /[^:\/]\//
  var results = re.exec(strURL)
  return results.input.substring(0,results.index+1)
}

function dropServer(strURL) {		// docelowo niepotrzebne
  var re = /[^:\/]\//
  var results = re.exec(strURL)
  return results.input.substr(results.index+1)
}


	function showImage(width, height, url, time) {
		
		var nMaxHeight, nMaxWidth, nHFactor, nWFactor, nFactor, sSrc;
		
		nMaxHeight = (screen.availHeight? screen.availHeight:700) - 120;
		nMaxWidth = (screen.availWidtht? screen.availWidth:1000) - 67;
		
		if(height > nMaxHeight) nHFactor = nMaxHeight / height; else nHFactor = 1;
		if(width > nMaxWidth) nWFactor = nMaxWidth / width; else nWFactor = 1;
		nFactor = nHFactor <= nWFactor? nHFactor : nWFactor;

		if(nFactor < 1) {
			height = Math.floor(height * nFactor);
			width = Math.floor(width * nFactor);
			sSrc = '/Commons/Files/view/showImage.php?time='+ time +'&h=' + height  +'&w=' + width +'&link=' + url;
		} else {
			sSrc = '/Commons/Files/view/showImage.php?time='+ time +'&link=' + url;
		}
		
		//alert('h: '+ nHFactor +', w: '+ nWFactor +', = '+ nFactor);
		
		width += 15;
		height += 100;
		
		winShowImage = openPopUpWithStatus(50, 50, width, height, 'showImageWindow', sSrc);
		winShowImage.resizeTo(width, height);
	}


	function showBDGImage(dtID) {
			win_Image = openPopUp(50,100,620,520,'Image','/graf/adm/dane/image_pd.php?dtID='+dtID);
	}
	

