/****************************************************/
/* Autor: Tomasz Ulanowski <tomasz@ulanowski.info>  */
/****************************************************/

// Funkcja weryfikujaca poprawnosc danych w formularzu na podstawie atrybutów: 			
// - required - okresla czy dane pole jest wymagane										
// - vFormat - okresla rodzaj wartosci jaki powinien wprowadzic uzytkownik w przypadku 	 
// pól tekstowych. Akceptowane wartosci to: email, integer, numeric, date, datetime,	
// zip, phone, nip, url																		
// - maxLength - w przypadku elementów TEXTAREA (max długosc pola) i SELECT	(max ilosc
// wybranych elementow)



function verifyForm(form) {
	var cnt = form.length;
	for (var i=0; i<cnt; i++) {
		var f = form[i];
		required = f.attributes.getNamedItem('required') ? f.attributes.getNamedItem('required').value : false;
		vFormat = f.attributes.getNamedItem('vFormat') ? f.attributes.getNamedItem('vFormat').value : false;
		minLength = f.attributes.getNamedItem('minLength') ? f.attributes.getNamedItem('minLength').value : false;
		maxLength = f.attributes.getNamedItem('maxLength') ? f.attributes.getNamedItem('maxLength').value : false;
		if ( (required || vFormat || minLength) && !f.disabled ) {
			var sValidationMsg = '';
			var valid = true;
			//alert(f.tagName);
			if ( f.value && minLength && f.value.length<minLength ) {
				sValidationMsg = 'Podaj poprawne informacje.';
				valid = false;
			}
			if (f.tagName=='TEXTAREA' && maxLength && f.value.length>maxLength) {
				sValidationMsg = 'Entry too long';
				valid = false;
			}
			else if (f.tagName=='SELECT' && maxLength && countSelected(f)>maxLength) {
				sValidationMsg = 'Select '+maxLength+' at most.';
				valid = false;
			}
			else if (required && (f.type!='checkbox' && !trim(f.value) || f.type=='checkbox' && !f.checked ) )
				valid = false;
			else if (f.value && vFormat) switch (vFormat) {
				case 'email':
					valid = verifyEmail(f.value);
					sValidationMsg = 'Correct form: user@domain.country';
					break;
				case 'url':
					valid = isURL(f.value);
					sValidationMsg = 'Wrong page address. Correct form: http://www.site.tld/';
					break;
				case 'integer':
				case 'int':
					valid = isInteger(f.value);
					sValidationMsg = 'It have to be integer.';
					break;
				case 'numeric':
					valid = isNumeric(f.value);
					sValidationMsg = 'It have to be number.';
					break;
				case 'date':
					valid = isDate(f.value);
					sValidationMsg = 'Correct form: YYYY-mm-dd.';
					break;
				case 'datetime':
					valid = isDatetime(f.value) || isDate(f.value);
					sValidationMsg = 'Correct form: YYYY-mm-dd HH:ii lub YYYY-mm-dd.';
					break;
				case 'zip':
					valid = isZip(f.value);
					sValidationMsg = 'Correct form: 00-000.';
					break;
				case 'phone':
					valid = isPhone(f.value);
					sValidationMsg = 'Correct forms (examples): \n22 437 97 00\n(22) 437 97 01\n22 437 97 02 w. 201\n607 990 400';
					break;
				case 'nip':
					valid = isNIP(f.value);
					sValidationMsg = 'Correct form is: 111-11-11-111 lub 111-111-11-11';
					break;
				default:
					break;
			}
			if (!valid) {
				if (f.type!='hidden') f.focus();
				if ( form[i].title && sValidationMsg )
					alert(form[i].title+' '+sValidationMsg);
				else if ( form[i].title || sValidationMsg )
					alert(form[i].title ? form[i].title : sValidationMsg);
				else
					alert('Fill required fields.');
				return false;
			}
		}
	}
	return true;
}

function verifyEmail(email) {
	var emailchecker = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i;
	return emailchecker.test(email);
}
function isURL(url) {
 	var urlPattern = /^(?:(?:ftp|https?):\/\/)?(?:[a-z0-9](?:[-a-z0-9_]*[a-z0-9])?\.)+(?:com|edu|biz|org|gov|int|info|mil|net|name|museum|coop|aero|[a-z][a-z])\b(?:\d+)?(?:\/[^;"'<>()\[\]{}\s\x7f-\xff]*(?:[.,?]+[^;"'<>()\[\]{}\s\x7f-\xff]+)*)?/i;
	return urlPattern.test(url);
}
function isInteger(str) {
	intCheck = /^\-?[0-9]+$/;
	return intCheck.test(str);
}
function isNumeric(str) {
	numCheck = /^[0-9]+(\.[0-9]+)?$/;
	return numCheck.test(str);
}
function isDate(str) {
	dateCheck = /^[12][0-9]{3}-[01][0-9]-[0123][0-9]$/;
	return dateCheck.test(str);
}
function isDatetime(str) {
	datetimeCheck = /^[12][0-9]{3}-[01][0-9]-[0123][0-9] [012][0-9]:[0-5][0-9](:[0-5][0-9])?$/;
	return datetimeCheck.test(str);
}
function isZip(str) {
	zipCheck = /^[0-9]{2}-[0-9]{3}$/;
	return zipCheck.test(str);
}
function isPhone(str) {
	mobileCheck = /^[ ]*[0]?[ \-]?[0-9][ \-]?[0-9][ \-]?[0-9][ \-]?[0-9][ \-]?[0-9][ \-]?[0-9][ \-]?[0-9][ \-]?[0-9][ \-]?[0-9][ ]*$/;
	phoneCheck = /^[ ]*\(?[0-9]{2}\)?[ ]?[0-9][ \-]?[0-9][ \-]?[0-9][ \-]?[0-9][ \-]?[0-9][ \-]?[0-9][ \-]?[0-9]( w\.[ ]?[0-9]{2,4})?[ ]*$/;
	return phoneCheck.test(str) || mobileCheck.test(str);
}
function isNIP(str) {
	nipCheck1 = /^[0-9]{3}-[0-9]{2}-[0-9]{2}-[0-9]{3}$/;
	nipCheck2 = /^[0-9]{3}-[0-9]{3}-[0-9]{2}-[0-9]{2}$/;
	return nipCheck1.test(str) || nipCheck2.test(str);
}

function countSelected( selectList ) {
	cnt =  selectList.options.length;
	sel = 0;
	for(i=0; i<cnt; i++ ) {
		if ( selectList.options(i).selected ) sel++;
	}
	return sel;
}
function openInEditor(input) {
	sEditedText = input.value;
	var re;
	if ( (re = showModalDialog('/htmledit/', window.self, 'scroll:no;dialogWidth:1000px;dialogHeight:650px;resizable:yes;')) )
		input.value = stripHostName( re );
}

function openInEditor2(input) {
	var ver = new String(navigator.appVersion);
	if( !(ver.indexOf("MSIE 6.0") != -1 || ver.indexOf("MSIE 5.5") != -1) ){
		alert("Edytor dostępny w przeglądarce Microsoft Internet Explorer 5.5 oraz wyższych"); 
	}else{
		sEditedText = input.value;
		var re;
		if ( (re = showModalDialog('/htmledit2/', window.self, 'scroll:no;dialogWidth:1000px;dialogHeight:650px;resizable:yes;')) )
			input.value = stripHostName( re );
	}
}

function stripHostName( text ) {
	var re = location.protocol+'//'+location.hostname+'/';
	while ( text.match( re ) )
		text = text.replace(re, '/');
	return text;
}

function radioIsChecked(name) {
	nodelist = document.getElementsByTagName('input');
	checked = false;
	for( i=0; i<nodelist.length; i++ ) {
		it = nodelist.item(i) ;
		if ( it.type=='radio' && it.name==name && it.checked ) {
			checked = true;
			break;
		}	
	}
	return checked;
}

function trim(s) {
	var c;
	while ((c = s.charCodeAt(0)) == 32 || c==9 || c==10 || c==13 ) {
		s = s.substring(1,s.length);
	}
	while ((c = s.charCodeAt(s.length-1)) == 32 || c==9 || c==10 || c==13 ) {
		s = s.substring(0,s.length-1);
	}
	return s;
}

function addOnClickEvent(evt)
{
	if(evt)
		el = evt.target;
	else
		el = event.srcElement	

	if ( el.tagName=='IMG' && el.longDesc ){
		window.open(
			'/imagePopup.php?src=' + el.longDesc,
			"ImagePopup",
			"width=" + 400 + ",height=" + (Math.round(600*el.height/el.width))
		);
	}
}

document.onclick = addOnClickEvent;


function rollover(obj,cNormal,cOver,cActive){
	if(obj.parentNode){
		if(obj.parentNode.className != cActive){		
			obj.parentNode.className = (obj.parentNode.className == cNormal) ? cOver : cNormal;
		}
	}
}
