/**
* Knihovna běžných JavaScript funkcí
*
* @package		sllibs3
* @subpackage	slForm
* @version		$Id: slLib.js 2073 2008-02-06 21:12:53Z michal $
* @encoding		utf-8
* @author		Michal Kouďa <michal.kouda@e4you.cz>
* @copyright	(c) e4you spol. s r.o. 2002-2008, <design@e4you.cz>
*
* Obsah tohoto souboru je majetkem e4you spol. s r.o. Jeho kopírování,
* pozměňování, šíření a jakékoli další využití je možné výhradně
* se souhlasem e4you spol. s r.o.
*
*/

/// 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) {
	try {
		return obj.className;
	} catch(error) {
		return '';
	}
}


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


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


/// zjisti jestli ma dany objekt nastavenu predanou CSS tridu
function hasCssClass(obj, cls) {
	all_classes = ' ' + getCssClass(obj) + ' ';
	return (all_classes.indexOf(' ' + cls + ' ') != -1);
}


/// prida CSS tridu predaneho objektu
function addCssClass(obj, cls) {
	if(!hasCssClass(obj, cls)) {
		new_class = getCssClass(obj) + ' ' + cls;
		setCssClass(obj, new_class);
	}
}


/// 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 doménového jména
function validDomainName(domain_name) {
	if(domain_name=='') return true;
	reDomainName = new RegExp("^[a-zA-Z0-9._-]+[.][a-zA-Z]{2,7}$");
	return reDomainName.test(domain_name);
}

/// 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) {
	psc = psc.replace(/ /gi,'');
	if(psc=='') return true;
	rePSC = new RegExp("^[0-9]{5}$");
	return rePSC.test(psc);
}

function validZip(psc) {
	return validPSC(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);
}

/// @todo upravit, nebo zrušit
function validUrl(url) {
	if(url=='') return true;
	reUrl = new RegExp("^http://.+?");
	return reUrl.test(url);
}

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;
};

/**
 * funkce vraci objekt podle jeho  id
 *
 * POZOR !!
 * v IE nastane chyba, pokud se funkce volá následujícím způsobem:
 * jmeno = getObject('jmeno');
 * tedy pokud je jmeno proměnné stejné jako jméno prvku
 * pravděpodobně to souvisí s funkcí eval
 *
 */
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";
		var objectTitle = "ID: " + dbgObject.id + ", Name: " + dbgObject.name;
	} catch(error) {
		var debugLog = "[undefined]\n";
		var objectTitle = "[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 - '+objectTitle+'</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;
}

/// funkce vraci pozici predaneho objektu na strance
function slGetObjectOffset(object) {
	var result = new Object();
	result.offsetLeft = 0;
	result.offsetTop = 0;

	if (runningInMSIE) {
		result.offsetLeft = object.offsetLeft;
		result.offsetTop = object.offsetTop;
		/// try / catch je asi mozno odstranit
		try {
			parentObject = object.offsetParent;
			while (parentObject) {
				result.offsetLeft += parentObject.offsetLeft;
				result.offsetTop += parentObject.offsetTop;
				parentObject = parentObject.offsetParent;
			}
		} catch(error) {
				alert("Error: "+error);
		}
	} else {
		result.offsetLeft = object.offsetLeft;
		result.offsetTop = object.offsetTop;
	}
	return result;
}

/// vrací true pokud jde o input typu multiple (např. radio)
function slInputIsMultiple(input_object) {
	return input_object.length && input_object.item;
}


/// vrací true pokud je alespoň jeden input typu
/// multiple (napž radio) zvolen
function slMultipleInputIsChecked(input_object) {
	try {
		input_object_length = input_object.length;
		for(item_counter=0; item_counter < input_object_length; item_counter++) {
			if(input_object[item_counter].checked) {
				return true;
			}
		}
	} catch(error) {}
	return false;
}

/// vrací zvolenou hodnotu input typu multiple
/// pokud není nikdo zvolen vrací null
function slMultipleInputGetValue(input_object) {
	try {
		for(item_counter=0; item_counter < input_object.length; item_counter++) {
			if(input_object[item_counter].checked) {
				return input_object[item_counter].value;
			}
		}
	} catch(error) {}
	return null;
}

/// označí předanou hodnotu v inputu typu multiple
/// pokud je tam hodnota přítomna - vrací true pokud
/// je hodnota nalezena a označena jinak vrací false
function slMultipleInputSetValue(input_object, input_value) {
	try {
		for(item_counter=0; item_counter < input_object.length; item_counter++) {
			if(input_object[item_counter].value == input_value) {
				input_object[item_counter].checked = true;
				return true;
			}
		}
	} catch(error) {}
	return false;
}

/// vraci cislo doplnene na 2 mista zleva 0
function addLeadingZero(i) {
	if(i.length == 1) {
		return  '0' + i;
	} else {
		return i;
	}
}
