// JavaScript Document

function initajax() {
  try { // Firefox, Opera 8.0+, Safari
		return new XMLHttpRequest();
	} catch (e) {    // Internet Explorer    
		try {      
			return new ActiveXObject("Msxml2.XMLHTTP");      
		} catch (e) {
			try {        
				return new ActiveXObject("Microsoft.XMLHTTP");        
			} catch (e) {
				alert("Sinu sirvija ei toeta AJAX tehnoloogiat, anna Bix'ile probleemist teada");        
				return null;        
			}      
		}
	}
}

var rate;

function submitrate(artid) {
	rate = document.getElementById("r"+artid).value;
  if (rate < 0) return true;			 
	var sQuery = "http://www.poogen.ee/redirect.php?method=rate&artid=" + artid + "&rate=" + rate;
	var httpRequest = initajax();	
	if (httpRequest !== null) {
		httpRequest.onreadystatechange = function() {getrate(httpRequest);};
		httpRequest.open('GET', sQuery, true);
		httpRequest.send('');
	}
	return true;
}

function getrate(httpRequest) {
	var error = "";			 
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			var xmlDoc=httpRequest.responseXML.documentElement;
			if (xmlDoc.getElementsByTagName("error")[0].hasChildNodes())  {
				error = xmlDoc.getElementsByTagName("error")[0].childNodes[0].nodeValue; 
			}
			artid = xmlDoc.getElementsByTagName("artid")[0].childNodes[0].nodeValue;
			if (error != "") {
				alert(error);
				document.getElementById("r"+artid).selectedIndex = 'Hinda';
			} else {
				error = "hinne: " + xmlDoc.getElementsByTagName("avgrate")[0].childNodes[0].nodeValue;
				error = error + ", sina panid: " + rate;
				error = error + ", hindeid: " + xmlDoc.getElementsByTagName("ratecnt")[0].childNodes[0].nodeValue;
				document.getElementById("s"+artid).innerHTML=error;
			}
    } else {
			alert('Hinne jäi muutmata serveri probleemide tõttu. Proovi veidi aja pärast uuesti.');
		}
	}
}

function ShowCommentField() {
	var commform = document.getElementById("commform");			 
	if (commform===null) {
		return true;
	} else {
		commform.style.display="inline";
		document.getElementById("commfield").focus();
		return false;
	}
}

function testoldpassword() {
	var newpw = document.getElementById("newpw1").value;
	if (newpw.length < 4) {
		alert('Parool peab olema vähemalt 4 sümbolit pikk! Proovi uuesti');
	} else {
		if (newpw != document.getElementById("newpw2").value) {
			alert('Uue parooli sisestused ei klapi omavahel. Proovi uuesti');
		} else {return true}
	}
	document.getElementById("newpw1").value = '';
	document.getElementById("newpw2").value = '';
	return false;
}

function MenuMouseOver(tblcell) {
	tblcell.style.borderColor='red';
	tblcell.style.cursor='pointer';
	return false;
}

function MenuMouseOut(tblcell) {
	tblcell.style.borderColor = tblcell.style.backgroundColor;
	return false;
}

function showform(ID) {
	var Result = true;
	for (var x = 0; x < document.forms.length; x++) {
		var FormTag = document.forms[x];
			if (FormTag.id == ID) {
				Result = false;
				FormTag.style.display="inline";
				for (var i = 0; i < FormTag.length; i++) {
					var FrmElem = FormTag.elements[i]; 	
					if ((FrmElem.type == 'textarea') || (FrmElem.type == "text")) {
						FrmElem.focus();
						break;
			 		}
				}
			} else
				FormTag.style.display="none";
	}
	return Result; 
}

function addartcatclick(params) {
  if (params == 1) {
    document.getElementById('addart').style.display = "inline";
    document.getElementById('addcompl').style.display = "none";
  } else if (params == 2) {
    document.getElementById('addart').style.display = "none";
    document.getElementById('addcompl').style.display = "inline";
  } else {
    document.getElementById('addart').style.display = "inline";
    document.getElementById('addcompl').style.display = "inline";
  }
  return true;
}

function AddArtSubmit(cmpl) {
  document.getElementById('cmpl').value=cmpl;
  return true;
}

function MenuBarClick(menubar) {
	var sQuery = "http://www.poogen.ee/redirect.php?method=menubar&menubar=" + menubar;
	var httpRequest = initajax();	
	if (httpRequest !== null) {
		httpRequest.open('GET', sQuery, true);
		httpRequest.send('');
	}
  if (menubar==1) {
    document.getElementById('rightcolumn').style.display = "inline";
    document.getElementById('knopka').innerHTML='<img src="images/f2.gif" alt="" width="22" height="22">';
  } else {
    document.getElementById('rightcolumn').style.display = "none";
    document.getElementById('knopka').innerHTML='<img src="images/f2_kn.gif" onclick="MenuBarClick(1)" alt="Ava menüü;" width="22" height="22" border="0">';
  }
  return false;
}

function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=660,height=490');");
	return false;
}

function popUp2(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=2,location=0,statusbar=0,menubar=0,resizable=0,width=660,height=490');");
	return false;
}

