//images
function popImage(imagesrc) {
	window.open(imagesrc + ',image.html', 'Obrazek powiększony', 'toolbar=no,status=yes,width=100,height=100,scrollbars=no');
}

function scaleImage(image) {
	msie = (document.all) ? true : false;	
	element = (msie ? document.all[image] : document.getElementById(image));
	noheight = false;
	if (element.width>screen.width-30) {
		ratio = element.width/element.height;
		element.width = screen.width-30;
		element.height = element.width/ratio;
		noheight = true;
		if (element.height>screen.height-90) {
			ratio = element.height/element.width;
			element.height = screen.height-90;
			element.width = element.height/ratio;
		}
	}
	if (element.height>screen.height-90 && noheight==false) {
		ratio = element.height/element.width;
		element.height = screen.height-90;
		element.width = element.height/ratio;
		if (element.width>screen.width-30) {
			ratio = element.width/element.height;
			element.width = screen.width-30;
			element.height = element.width/ratio;
		}
	}
	window.resizeTo(element.width+10, element.height+55);
}

//open urls with rev="external" in new window
function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rev") == "external") anchor.target = "_blank";
 	}
}
window.onload = externalLinks;

function swapImg(name, image) {
	tmp = eval('document.' + name);
	tmp.src = image;
}

function notepad(ido) {
	var img, i, value, not, tmp;
	
	i = eval('document.notepad_' + ido);
	tmp = i.src.match('/webapp/img/notepad_empty.gif');
	if (tmp == '/webapp/img/notepad_empty.gif') {
		img = '/webapp/img/notepad_checked.gif';
		value = 't';
	} else {
		img = '/webapp/img/notepad_empty.gif';
		value = 'f';
	};
		
	swapImg('notepad_' + ido, img);

	isnotepad = document.cookie.indexOf("js_notepad");
	if (isnotepad == -1 ) { 
		not='';
	} else {
		str_begin = (document.cookie.indexOf("=", isnotepad) +1 );
		str_end=document.cookie.length;
		not = document.cookie.substring(str_begin, str_end);
	}
	
	not = not + '_' + ido + '-' + value;
	Set_Cookie( 'js_notepad', not, 10, '/', '', '' );
	return false;
}

function Set_Cookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );

	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) + ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + ( ( path ) ? ";path=" + path : "" ) + ( ( domain ) ? ";domain=" + domain : "" ) + ( ( secure ) ? ";secure" : "" );
}

function layerSwitch(object) {
	msie = (document.all) ? true : false;	
	element = (msie ? document.all[object] : document.getElementById(object));
	if (element.style.display=='block' || element.style.display=='') {
		element.style.display='none';
	} else {
		element.style.display='block';
	}
}

function validateContactForm(object) {
	var errormsg='';
	if (object.mail_name.value=='') { errormsg = errormsg + "Musisz podać swoje imię / You must enter your name\n"};
	if (object.mail_telefon.value=='' && object.mail_email.value=='') { errormsg = errormsg + "Musisz podać telefon lub e-mail kontaktowy / You must enter either phone number or your e-mail\n"};
	if (!object.mail_email.value.match('@')) {
		errormsg = errormsg + "Adres e-mail jest błędny / E-mail address is invalid\n";
	}
	if (errormsg) {
		alert(errormsg);
		return false;
	} else {
		return true;
	}
}
