//xmlhttp.js

var xmlhttp
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
  try {
  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
 } catch (e) {
  try {
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  } catch (E) {
   xmlhttp=false
  }
 }
@else
 xmlhttp=false
@end @*/

if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
{
  try {
	xmlhttp = new XMLHttpRequest ();
  }
  catch (e) {
  xmlhttp = false}
}

function myXMLHttpRequest ()
{
  var xmlhttplocal;
  try {
  	xmlhttplocal = new ActiveXObject ("Msxml2.XMLHTTP")}
  catch (e) {
	try {
	xmlhttplocal = new ActiveXObject ("Microsoft.XMLHTTP")}
	catch (E) {
	  xmlhttplocal = false;
	}
  }

  if (!xmlhttplocal && typeof XMLHttpRequest != 'undefined') {
	try {
	  var xmlhttplocal = new XMLHttpRequest ();
	}
	catch (e) {
	  var xmlhttplocal = false;
	  alert ('XMLHTTPオブジェクト生成時にエラーが発生しました。');
	}
  }
  return (xmlhttplocal);
}

var newzxmlhttp = Array ();
var newzString = Array ();
var responsestring = Array ();
var myxmlhttp = Array ();
var responseString = new String;


function votenewzit (user, id, htmlid, md5)
{
  	if (xmlhttp) {
		url = "/votenewzit.php";
		content = "id=" + id + "&user=" + user + "&md5=" + md5;
		newzxmlhttp[htmlid] = new myXMLHttpRequest ();
		if (newzxmlhttp) {
			newzxmlhttp[htmlid].open ("POST", url, true);
			newzxmlhttp[htmlid].setRequestHeader ('Content-Type',
					   'application/x-www-form-urlencoded');
			newzxmlhttp[htmlid].send (content);
			errormatch = new RegExp ("^ERROR:");
			target1 = document.getElementById ('newz-' + htmlid);
			newzxmlhttp[htmlid].onreadystatechange = function () {
				if (newzxmlhttp[htmlid].readyState == 4) {
					newzString[htmlid] = newzxmlhttp[htmlid].responseText;
					if (newzString[htmlid].match (errormatch)) {
						newzString[htmlid] = newzString[htmlid].substring (6, newzString[htmlid].length);
						alert (newzString[htmlid]);
						changenewzvalues (htmlid, true);
					} else {
						changenewzvalues (htmlid, false);
					}
				}
			}
		}
	}
}

function changenewzvalues (id, error)
{
	split = new RegExp ("~--~");
	b = newzString[id].split (split);
	target1 = document.getElementById ('newz-' + id);
	target2 = document.getElementById ('newzlink-' + id);
	new Effect.Highlight( 'newz-no-' + id );
	if (error) {
		target2.innerHTML = "<span>ERROR</span>";
		return false;
	}
	if (b.length <= 3) {
		target1.innerHTML = b[0];
		target2.innerHTML = "<span>NEWZED</span>";
	}
	return false;
}


function enablebutton (button, button2, target)
{
	var string = target.value;
	button2.disabled = false;
	if (string.length > 0) {
		button.disabled = false;
	} else {
		button.disabled = true;
	}
}

function checkfield (type, form, field)
{
	url = 'checkfield.php?type='+type+'&name=' + field.value;
	checkitxmlhttp = new myXMLHttpRequest ();
	checkitxmlhttp.open ("GET", url, true);
	checkitxmlhttp.onreadystatechange = function () {
		if (checkitxmlhttp.readyState == 4) {
		responsestring = checkitxmlhttp.responseText;
			if (responsestring == 'OK') {
				document.getElementById (type+'checkitvalue').innerHTML = '<span style="color:#333">"' + field.value + 
						'": ' + responsestring + '</span>';
				form.submit.disabled = '';
			} else {
				document.getElementById (type+'checkitvalue').innerHTML = '<span style="color:#F00">"' + field.value + '": ' +
				responsestring + '</span>';
				form.submit.disabled = 'disabled';
			}
		}
	}
  //  xmlhttp.setRequestHeader('Accept','message/x-formresult');
  checkitxmlhttp.send (null);
  return false;
}

function report_problem(frm, user, id, md5 /*id, code*/) {
	if (frm.ratings.value == 0)
		return;
	if (! confirm("本当に通報してよろしいですか？") ) {
		frm.ratings.selectedIndex=0;
		return false;
	}
	content = "id=" + id + "&user=" + user + "&md5=" + md5 + '&value=' +frm.ratings.value;
	url="/problem.php?" + content;
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			frm.ratings.disabled=true;
		}
  	}
	xmlhttp.send(null);
	return false;
}

