function openWindow(mypage, myname, w, h, scroll) 
{
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	gmtWindow = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { gmtWindow.window.focus(); }
}

function find_all_checkbox_values(what, checkbox_name) {
	parameters = '';
	what = document.getElementById(what);
	for (var i=0, j=what.elements.length; i<j; i++) {
		if(what.elements[i].name == checkbox_name && what.elements[i].checked == true) {
			parameters += "&" + what.elements[i].name + "=" + encodeURI(what.elements[i].value);
		}
	}
	return parameters;
}

function find_all_form_elements(form_name) {
	parameters = '';
	what = document.getElementById(form_name);
	for (var i=0, j=what.elements.length; i<j; i++) {
		if( (what.elements[i].type == 'radio' || what.elements[i].type == 'checkbox') && what.elements[i].checked != true) {
		} else {
			parameters += "&" + what.elements[i].name + "=" + encodeURI(what.elements[i].value);
		}
	}
	return parameters;
}

function vote_s(item, table, vote) {
	if(table == 'articles') { 
		makePOSTRequest_rate('articles_rate.php?', 'article_id=' + item + '&vote=' + encodeURI(vote) + '', 'rate' + item);
	} else if(table == 'video') { 
		makePOSTRequest_rate('video_rate.php?', 'video_id=' + item + '&vote=' + encodeURI(vote) + '', 'rate' + item);
	} else if(table == 'audio') { 
		makePOSTRequest_rate('audio_rate.php?', 'audio_id=' + item + '&vote=' + encodeURI(vote) + '', 'rate' + item);
	} else if(table == 'photo_topics') { 
		makePOSTRequest_rate('photo_topic_rate.php?', 'photo_topic_id=' + item + '&vote=' + encodeURI(vote) + '', 'rate' + item);
	} else if(table == 'people') { 
		makePOSTRequest_rate('people_rate.php?', 'people_id=' + item + '&vote=' + encodeURI(vote) + '', 'rate' + item);
	} else if(table == 'places') { 
		makePOSTRequest_rate('places_rate.php?', 'places_id=' + item + '&vote=' + encodeURI(vote) + '', 'rate' + item);
	} else if(table == 'events') { 
		makePOSTRequest_rate('events_rate.php?', 'events_id=' + item + '&vote=' + encodeURI(vote) + '', 'rate' + item);
	} else if(table == 'albums') { 
		makePOSTRequest_rate('albums_rate.php?', 'albums_id=' + item + '&vote=' + encodeURI(vote) + '', 'rate' + item);
	}
	
}

function show_msg(div, msg) {
	document.getElementById(div).innerHTML = '' + msg + '';
}
function show_err(div, msg) {
	document.getElementById(div).innerHTML = '' + msg + '';
}

   function makePOSTRequest_rate(url, parameters, myelement) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            http_request.overrideMimeType('text/xml');
            //http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = function() { getXMLContents_rate(http_request, myelement, url); };
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

	
   function getXMLContents_rate(http_request, myelement) {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
				//alert(http_request.responseText + myelement);
				
				var gaga = false;
				
				var xmlDoc=http_request.responseXML.documentElement;
		        if (xmlDoc != null) { // Mozilla, Safari, ...
					gagaz = xmlDoc.getElementsByTagName(myelement + '_err')
					if(gagaz[0] != null) {
						gaga = gagaz[0].childNodes[0].nodeValue;
					}
		        } else { // IE
					gagaz = http_request.responseText;
					re = new RegExp("\(\<" + myelement + "_err\\>)(.*)(\\<\\/" + myelement + "_err\\>)");
					tmpres = re.exec(gagaz);
					gaga = tmpres[2];
				}
				if(gaga == false) {
			        if (xmlDoc != null) { // Mozilla, Safari, ...
						gagaz = xmlDoc.getElementsByTagName(myelement + '_ok')
						if(gagaz[0] != null) {
							gaga = gagaz[0].childNodes[0].nodeValue;
						}
			        } else { // IE
						gagaz = http_request.responseText;
						re = new RegExp("\(\<" + myelement + "_ok\\>)(.*)(\\<\\/" + myelement + "_ok\\>)");
						tmpres = re.exec(gagaz);
						gaga = tmpres[2];
					}
					alert(gaga);
					
			        if (xmlDoc != null) { // Mozilla, Safari, ...
						gagaz = xmlDoc.getElementsByTagName(myelement + '_votes')
						if(gagaz[0] != null) {
							gaga = gagaz[0].childNodes[0].nodeValue;
						}
			        } else { // IE
						gagaz = http_request.responseText;
						re = new RegExp("\(\<" + myelement + "_votes\\>)(.*)(\\<\\/" + myelement + "_votes\\>)");
						tmpres = re.exec(gagaz);
						//alert(tmpres);
						gaga = tmpres[2];
					}

					show_msg(myelement, gaga);
				} else {
					alert(gaga);
					//show_err(myelement, gaga);
				}
				//tmp = document.getElementById(myelement);
				//tmp.innerHTML = gaga;
				//tmp.style.display = "block";
            } else {
                // alert('There was a problem with the request.');
            }
        }

    }

var tmpimgover1 = '';
var tmpimgover2 = '';
var tmpimgover3 = '';
var tmpimgover4 = '';
var tmpimgover5 = '';
	
function highlight_star(itemid, starno) {
	for (i = 1; i <= 5; i++) {
		tmp = document.getElementById('imgstar' + itemid + '_' + i) ;
		window['tmpimgover' + i] = tmp.src;
		if(i <= starno) {
			tmp.src = 'images/star1.gif';
		} else {
			tmp.src = 'images/star0.gif';
		}
	}
}

function unhighlight_star(itemid, starno) {
	for (i = 1; i <= 5; i++) {
		tmp = document.getElementById('imgstar' + itemid + '_' + i) ;
		tmp.src = window['tmpimgover' + i];
	}
}

function highlight_star_vote(itemid, starno) {
	for (i = 1; i <= 5; i++) {
		tmp = document.getElementById('imgstar' + itemid + '_' + i) ;
		window['tmpimgover' + i] = tmp.src;
		if(i <= starno) {
			tmp.src = 'images/star1.gif';
		} else {
			tmp.src = 'images/star0.gif';
		}
	}
}

function unhighlight_star_vote(itemid, starno) {
	for (i = 1; i <= 5; i++) {
		tmp = document.getElementById('imgstar' + itemid + '_' + i) ;
		tmp.src = window['tmpimgover' + i];
	}
}

/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Steve Chipman | http://slayeroffice.com/ */

// constants to define the title of the alert and button text.
var ALERT_TITLE = "Резултат";
var ALERT_BUTTON_TEXT = "Затвори";

// over-ride the alert method only if this a newer browser.
// Older browser will see standard alerts
if(document.getElementById) {
  window.alert = function(txt) {
    createCustomAlert(txt);
  }
}

function createCustomAlert(txt) {
  // shortcut reference to the document object
  d = document;

  // if the modalContainer object already exists in the DOM, bail out.
  if(d.getElementById("modalContainer")) return;

  // create the modalContainer div as a child of the BODY element
  mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
  mObj.id = "modalContainer";
   // make sure its as tall as it needs to be to overlay all the content on the page
  mObj.style.height = document.documentElement.scrollHeight + "px";

  // create the DIV that will be the alert 
  alertObj = mObj.appendChild(d.createElement("div"));
  alertObj.id = "alertBox";
  // MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
  if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";
  // center the alert box
  alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";

  // create an H1 element as the title bar
  h1 = alertObj.appendChild(d.createElement("h1"));
  h1.appendChild(d.createTextNode(ALERT_TITLE));

  // create a paragraph element to contain the txt argument
  msg = alertObj.appendChild(d.createElement("p"));
  msg.innerHTML = txt;
  
  // create an anchor element to use as the confirmation button.
  btn = alertObj.appendChild(d.createElement("a"));
  btn.id = "closeBtn";
  btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
  btn.href = "#";
  // set up the onclick event to remove the alert when the anchor is clicked
  btn.onclick = function() { removeCustomAlert();return false; }
}

// removes the custom alert from the DOM
function removeCustomAlert() {
  document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
}


function prepareHints() {
	var els = document.getElementsByName("cal_entries");
	for (var i=0; i<els.length; i++){
		if ( els[i].title == '' ) {
			continue;
		}
		eval('els[i].onmouseover = function (event) {show_hint(event, \'' + els[i].title  + '\');	}');
		els[i].onmouseout = function () {
			hide_hint();
		}
		els[i].onmousemove = function (event) {
			move_hint(event);
		}
		els[i].setAttribute("title", "");
	}
}

function show_hint(e, msg) {
    var posx = 0;
    var posy = 0;
    if (!e) var e = window.event;
    if (e.pageX || e.pageY)
    {
        posx = e.pageX;
        posy = e.pageY;
    }
    else if (e.clientX || e.clientY)
    {
        posx = e.clientX + document.documentElement.scrollLeft;
        posy = e.clientY + document.documentElement.scrollTop;;
    }
	
	posy += 20;

	if ( document.getElementById('hintDiv' ) ) {
		document.getElementById('hintDiv').innerHTML = msg;
		document.getElementById('hintDiv').style.left = posx + "px";
		document.getElementById('hintDiv').style.top = posy + "px";
		document.getElementById('hintDiv').style.display = 'block';
	}
}

function move_hint(e) {
    var posx = 0;
    var posy = 0;
    if (!e) var e = window.event;
    if (e.pageX || e.pageY)
    {
        posx = e.pageX;
        posy = e.pageY;
    }
    else if (e.clientX || e.clientY)
    {
        posx = e.clientX + document.documentElement.scrollLeft;
        posy = e.clientY + document.documentElement.scrollTop;;
    }
	
	posy += 20;

	if ( document.getElementById('hintDiv' ) ) {
		document.getElementById('hintDiv').style.left = posx + "px";
		document.getElementById('hintDiv').style.top = posy + "px";
	}
}

function hide_hint() {
	if ( document.getElementById('hintDiv' ) ) {
		document.getElementById('hintDiv' ).style.display = 'none';
	}
}