var rightResizables = new Array('contentRight0', 'contentRight1', 'footerRight');
var emailRegExp = /^[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*@[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*(\.[_a-zA-Z0-9\-]{2,4})$/;

var lang = new Array(
	'Neveljaven email naslov!',
	'Napaka na strežniku!',
	'Prijava je bila uspešna.'
);

String.prototype.trim = function() {
  return( this.replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1') ); 
}

function openPicture(id) {
	window.open('picture/?id=' + id, 'open_large', 'toolbar=no,directiories=no,location=no,status=no,menubar=no,resizable=yes,scrollbars=no,width=50,height=50');
}

function popup(id, mode) {
	window.open('popup/?id=' + id + ',' + mode, 'popup', 'toolbar=no,directiories=no,location=no,status=no,menubar=no,resizable=yes,scrollbars=yes,width=520,height=600');
}

function resizeRights() {
	var t, w, n;
	if (typeof(document.body.clientWidth) == 'undefined') return;
	w = Math.max(0, document.body.clientWidth - 601);
	for (n = 0; n < rightResizables.length; n++) {
		if (t = document.getElementById(rightResizables[n])) t.style['width'] = w + 'px';
	}
}

function getHTTPRequest() {
	var xmlHttp = null;
	
	try {
	  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	  try {
	    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (e2) {
	    xmlHttp = null;
	  }
	}
	if (!xmlHttp && typeof(XMLHttpRequest) != 'undefined') xmlHttp = new XMLHttpRequest();
	
	return xmlHttp;
}

function sendEmail(f) {
	var t = document.getElementById('emailBox');
	if (t == null) return;
	setEmailError();
	var email = t.value.trim();
	if (email.length == 0) {
		t.focus();
		return;
	}
	if (!emailRegExp.test(email)) {
		alert(lang[0]);
		t.focus();
		return;
	}
	var http;
	if ((http = getHTTPRequest()) == null) return;
	http.open("GET", 'scripts/newsletter.php?email=' + email, true);
	http.onreadystatechange = function() { handleSendEmailResponse(http, t); };
   	http.send(null);
}

function handleSendEmailResponse(http, emailField) {
	if (http.readyState == 4) {
    	if (http.status != 200) {
    		setEmailError(lang[1]);
    		return;
    	}
    	var response = http.responseXML;
    	if (response == null) {
    		setEmailError(lang[1]);
    		return;
    	}
		var error = response.getElementsByTagName('error');
		if ((error == null) || (error[0] == null) || (error[0].firstChild == null)) {
			emailField.value = '';
			setEmailError(lang[2]);
		}
		else setEmailError(lang[1]);
	}
}

function setEmailError(error) {
	var fld = document.getElementById('emailError');
	if (fld == null) return;
	if (typeof error == 'undefined' || error == null) fld.style['display'] = 'none';
	else {
		clearField(fld);
		addText(fld, error);
		fld.style['display'] = 'block';
		window.setTimeout('setEmailError();', 2000);
	}
}

function addText(field, text) {
	var fld, t = typeof field;
	
	if (t == 'string') fld = document.getElementById(field);
	else if (t == 'object') fld = field;

	if (typeof fld != 'object' || fld == null) return;
	fld.appendChild(document.createTextNode(text));
}

function clearField(field) {
	if (typeof(field) == 'undefined' || field == null || typeof (field.childNodes) == 'undefined' || field.childNodes == null) return;
	var l = field.childNodes.length;
	for (var n = l - 1; n >= 0; n--) field.removeChild(field.childNodes[n]);
}

function validateSearchFields(f) {
	if (f.elements['search'].value.trim().length == 0) {
		f.elements['search'].focus();
		return false;
	}
	return true;
}

function resizeCenter() {
	var divCenter = document.getElementById('contentCenter');
	var divRight = document.getElementById('contentRight');
	
	if (divCenter == null || divRight == null) return;
	if (divRight.clientHeight > divCenter.clientHeight) divCenter.style['height'] = divRight.clientHeight + 'px';
}