///////////////////////////////////////////////////////////////////////////////
///  slLibs
///  $Id$
///  Encoding: utf8
///  (c) Michal Kouďa, e4you spol. s r.o. 2002-2006, design@e4you.cz
///////////////////////////////////////////////////////////////////////////////
///  Knihovna běžných funkcí v javascriptu
///////////////////////////////////////////////////////////////////////////////

/// konstanty

PASSWORD_LENGTH = 8		/// požadovaná délka hesla

/// identifikace vlastnosti browseru 
runningInGecko = (navigator.product == 'Gecko')
runningInMSIE = (document.all != null)


/// funkce zjistujici CSS tridu objektu
function getCssClass(obj) {
	return obj.className;
}

/// funkce nastavujici CSS objektu tridu
function setCssClass(obj,cls) {
	obj.className=cls;
	return true;
}

/// funkce zajistujici zpetnou kompatibilitu (lze ji casem odstranit)
function setClass(obj,cls) {
	return setCssClass(obj,cls);
}

/// kontroluje, jestli parametr je ve formatu emailove
/// adresy ('' == správné ~= žádný e-mail)
function validEmail(email) {
	if(email=='') return true;
	reEmail = new RegExp("^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+[.][a-zA-Z]{2,7}$");
	return reEmail.test(email);
}

/// kontroluje, jestli parametr je ve formátu
/// správného uživatleského jména
function validUsername(username) {
	if(username=='') return false;
	reLogin = new RegExp("^[a-zA-Z0-9_-]{4,48}$");
	return reLogin.test(username);
}

/// kontroluje, jestli parametr je ve formátu
/// správného uživatleského hesla
function validPasswd(passwd) {
	if(passwd == '******') return true;
	if(passwd == '') return false;
	if(passwd.length < PASSWORD_LENGTH) return false;
	reAZ = new RegExp('[0-9]+');
	if(!reAZ.test(passwd)) return false;
	re09 = new RegExp('[a-zA-Z]+');
	if(!re09.test(passwd)) return false;
	return true;
}

function validUserPasswdPair(username,passwd) {
	if(!validUsername(username)) return false;
	if(!validPasswd(passwd)) return false;
	if(passwd.toUpperCase()==username.toUpperCase()) return false;
	if(passwd.search(username)!=-1) return false;
	revuname = '';
	for(i=0;i<username.length;i++) {
		revuname = username.charAt(i) + revuname;
	}
	if (revuname.toUpperCase()==passwd.toUpperCase()) return false;
	return true;
}

function validInteger(integer) {
	if(integer=='') return true;
	reInteger = new RegExp("^[+-]?[0-9]+$");
	return reInteger.test(integer);
}

/// pro ceska telefonni cisla by mela byt delka
/// telefonniho cisla alepon 9 cislic
function validPhone(phone) {
	if(phone=='') return true;
	rePhone = new RegExp("^[+]?[0-9 ]{9,}$");
	return rePhone.test(phone);
}

/// pro telefonni cisla ve tvaru, ktery lze pouzit pro zasilani SMS
function validPhoneStrict(phone) {
	rePhoneStrict = new RegExp("^420[0-9]{9}$");
	return rePhoneStrict.test(phone);
}

function validICO(ico) {
	if(ico=='') return true;
	reICO = new RegExp("^[0-9]{8}$");
	return reICO.test(ico);
}

/// returns true if tax_no is valid EU tax number
function valid_tax_no(tax_no) {
	if(tax_no == '') {
		return true;
	}
	re_tax_no = new RegExp("^([a-zA-Z]{2}|[0-9]{3}-)[0-9]{8,10}$");
	return re_tax_no.test(tax_no);
}

/// deprecated - nova verze je: valid_tax_no
function validDIC(dic) {
	return valid_tax_no(dic);
}

function validPSC(psc) {
	if(psc=='') return true;
	rePSC = new RegExp("^[0-9]{5}$");
	return rePSC.test(psc);
}

function validIpList(ip) {
	if(ip=='') return false;
	reIpList = new RegExp("^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(/[0-9]{1,2})?(, )?)+$");
	return reIpList.test(ip);
}

function openLink(url) {
	linkWin=window.open(url,'linx','');
}


PicWinOpened = false;
function previewImg(src,w,h,description) {
	if(PicWinOpened) {
		PicWin.close();
		PicWinOpened = false;
	}
	winw = w + 20;
	winh = h + 30;
	PicWin = window.open('','picturedetail','width='+winw+',height='+winh+',resizable=no,location=no,status=no,scrollbars=no');
	with(PicWin) {
		document.open();
		document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
		document.write('<html xmlns="http://www.w3.org/1999/xhtml">');
		document.write('<head><title>'+description+'</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>');
		document.write('<body style="margin: 10px 0 0 0; padding: 0px; background-color: black; text-align:center; font-family: verdana,sans-serif; font-size: 11px;">');
		document.write('<a href="javascript: window.close();" title="Zavřít okno"><img src="'+src+'" alt="Zavřít okno" width="'+w+'" height="'+h+'" border="0" /></a>');
		document.write('<div style="color: white">'+description+'</div>');
		document.write('</body></html>');
		document.close();
		focus();
	};
	PicWinOpened = true;
	return false;
};

/// vraci objekt podle jeho  id
function getObject(objectName) {
	try {
		if(document.all) {
			obj = eval("document.all['"+objectName+"']");
			return obj;
		} else if(document.getElementById) {
			return document.getElementById(objectName);
		} else {
			return false;
		}
	} catch(error) {
		alert("Object find error: "+error);
		return false;
	}
}

/// vytiskne strukturu objektu do nove otevreneho okna
function debugObject(dbgObject) {
	try {
		var debugLog = "[" + dbgObject.name + "]\n";
	} catch(error) {
		var debugLog = "[undefined]\n";
	}
	for (property in dbgObject) {
		try {
			debugLog += '\t.' + property ;
			debugLog += ' = ' + eval('dbgObject.' + property) + "\n";
		} catch(error) {
			debugLog += '\t.' + property + " = ???\n";
		}
	}
	debugWin = window.open('','debug');
	with(debugWin) {
		document.open();
		document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
		document.write('<html xmlns="http://www.w3.org/1999/xhtml">');
		document.write('<head><title>Výpis objektu</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>');
		document.write('<body>\n');
		document.write('<h1>Výpis objektu</h1>');
		document.write('<pre>' + debugLog + '</pre>');
		document.write('</body>\n</html>\n');
		document.close();
		focus();
	}
	return debugLog;
}

/// vytiskne strukturu objektu podle jmena do nove otevreneho okna
function debugObjectByName(dbgObjectName) {
	dbgObject = getObject(dbgObjectName);
	if(dbgObject) {
		return debugObject(dbgObject);
	} else {
		return false;
	}
}

/// proc se jmenuje ....Ex - zrejme historicky podmineno
/// zjisti jestli po prejmenovan na openWindow nedojde
/// k problemum a pokud ne prejmenovat
function openWindowEx(url,window_name,width,height,left,top) {
	win = open(url,window_name,'left='+left+',top='+top+',width='+width+',height='+height+',menubar=no,status=no,scrollbars=yes');
	win.focus();
	return true;
}

