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

var illegalCHARS ='\\';


var artcom = a = {

	getInt: function(strVal) {
		strVal = strVal? strVal:'0';
		strVal = strVal.replace(/ /, '');
		strVal = strVal.replace(/,/, '.');
		return Math.round(parseFloat(strVal));
	},

	getFloat: function(strVal) {
		strVal = strVal? strVal:'0.00';
		strVal = strVal.replace(/ /, '');
		strVal = strVal.replace(/,/, '.');

		return Math.round(parseFloat(strVal*100))/100;
	},

	formatInt: function(val) {
		if(isNaN(val)) val = 0;
		return Math.round(val? val:0);
	},

	formatFloat: function(val) {

		if(isNaN(val)) val = 0.00;
		var numVal = Math.round((val? val:0) * 100);
		var strVal = '';

		if(numVal >= 100) strVal = String(Math.round(val * 100));
		else if(numVal >= 10) strVal = '0'+ String(Math.round(val * 100));
		else strVal = '00'+ String(Math.round(val * 100));

		var lastVal = strVal.substr(strVal.length-2, 2);
		var baseVal = strVal.substr(0, strVal.length-2);

		baseArray = [];
		while(baseVal.length > 3) {
 			baseArray[baseArray.length] = baseVal.substring(baseVal.length-3);
 			baseVal = baseVal.substring(0, baseVal.length-3);
		}

		var response = '';
		response += (!baseVal && !baseArrray? '0': baseVal);
		for(i=0; i<baseArray.length; i++) {
			response += (' '+ baseArray[i]);
		}
		response += (','+ lastVal);

		return response;

	}	// formatFloat


}	// artcom, a







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

function syncCaption(title) {

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

function powrot(win, refresh) {
	if(opener) {
		if(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 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 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 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 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);
  sendLinkWindow = window.open(link,'sendWin', 'location=no,menubar=no,status=no,resizable=no,dependent=yes,width=310,height=400,screenX=50,screenY=150,left=50,top=150')
	if(!sendLinkWindow)
		alert('Okienko pomocnicze nie zostało otwarte z powodu blokady pop-up.')
	else
		sendLinkWindow.focus();
}


function openSendQuery(uprawnienia) {	// nowe okno z zapytaniem

  var query = "/Commons/Files/mailings/query.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);
  sendQueryWindow = window.open(query,'queryWin', 'location=no,menubar=no,status=no,resizable=no,dependent=yes,width=310,height=400,screenX=50,screenY=150,left=50,top=150')
	if(!sendQueryWindow)
		alert('Okienko pomocnicze nie zostało otwarte z powodu blokady pop-up.')
	else
		sendQueryWindow.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 += 7;
	height += 60;

	winShowImage = openPopUpWithStatus(50, 50, width, height, 'showImageWindow', sSrc);
	winShowImage.resizeTo(width, height);
}




