﻿var http = createRequestObject();
var field = '';
           var RATE_OBJECT_IMG = "images/rate/full.png";
           var RATE_OBJECT_IMG_HOVER = "images/rate/full.png";
           var RATE_OBJECT_IMG_HALF = "images/rate/half.png";
           var RATE_OBJECT_IMG_BG = "images/rate/none.png";
function createRequestObject() {
	var xmlhttp;
	try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); }
	catch(e) {
    try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	catch(f) { xmlhttp=null; }
  }
  if(!xmlhttp&&typeof XMLHttpRequest!="undefined") {
	xmlhttp=new XMLHttpRequest();
  }
	return  xmlhttp;
}

function handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			response = http.responseText;
			field.innerHTML = response;
			field.scrollIntoView();
			if(!response) window.location.href = url;
		}
  	}
	catch(e){}
	finally{}
}

function load_pages(type,num,apr,id,page) {
	field = document.getElementById(type);
	field.innerHTML = loadingText;
	http.open('POST',  'index.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
    http.send('load_pages=1&type='+type+'&num='+num+'&apr='+apr+'&id='+id+'&page='+page); 
  return false; 
} 
//#######################################
//# SEARCH
//#######################################
// Replaces all instances of the given substring.
String.prototype.replaceAll = function(
strTarget, // The substring you want to replace
strSubString // The string you want to replace in.
){
var strText = this;
var intIndexOfMatch = strText.indexOf( strTarget );
 
// Keep looping while an instance of the target string
// still exists in the string.
while (intIndexOfMatch != -1){
// Relace out the current instance.
strText = strText.replace( strTarget, strSubString )
 
// Get the index of any next matching substring.
intIndexOfMatch = strText.indexOf( strTarget );
}
 
// Return the updated string with ALL the target strings
// replaced out with the new substring.
return( strText );
}
function do_search() {
	kw = document.getElementById("keyword").value;
	kw = ConvertNonMark(kw);
	if (!kw) alert('Please enter some keyword');
	else {
		//kw = encodeURIComponent(kw);
		kw = kw.replaceAll(" ", "+");
		window.location.href = 'search/'+kw+'.html?cx=018293156229743795005%3Aniunodubql8&cof=FORID%3A10&ie=UTF-8&q='+kw+'&sa=Search';	
		//window.location.href = '?act=/search/'+kw;
	}
  return false;
}
//#######################################
//# ADD EMOTIONS
//#######################################
function addText(elname, wrap1) {
	if (document.selection) { // for IE 
		var str = document.selection.createRange().text;
		document.forms['add'].elements[elname].focus();
		var sel = document.selection.createRange();
		sel.text = wrap1 + str;
		return;
	} 
	else if ((typeof document.forms['add'].elements[elname].selectionStart) != 'undefined') { // for Mozilla
		var txtarea = document.forms['add'].elements[elname];
		var selLength = txtarea.textLength;
		var selStart = txtarea.selectionStart;
		var selEnd = txtarea.selectionEnd;
		var oldScrollTop = txtarea.scrollTop;
		var s1 = (txtarea.value).substring(0,selStart);
		var s2 = (txtarea.value).substring(selStart, selEnd)
		var s3 = (txtarea.value).substring(selEnd, selLength);
		txtarea.value = s1 + wrap1 + s2 + s3;
		txtarea.selectionStart = s1.length;
		txtarea.selectionEnd = s1.length + s2.length + wrap1.length;
		txtarea.scrollTop = oldScrollTop;
		txtarea.focus();
		return;
	} else {
		insertText(elname, wrap1);
	}
}
//#######################################
//# COUNT WORDS
//#######################################
var submitcount=0;
   function checkSubmit() {

      if (submitcount == 0)
      {
      submitcount++;
      document.Surv.submit();
      }
   }


function wordCounter(field, countfield, maxlimit) {
wordcounter=0;
for (x=0;x<field.value.length;x++) {
      if (field.value.charAt(x) == " " && field.value.charAt(x-1) != " ")  {wordcounter++}
      if (wordcounter > 250) {field.value = field.value.substring(0, x);}
      else {countfield.value = maxlimit - wordcounter;}
      }
   }

function textCounter(field, countfield, maxlimit) {
  if (field.value.length > maxlimit)
      {field.value = field.value.substring(0, maxlimit);}
      else
      {countfield.value = maxlimit - field.value.length;}
  }
  //#######################################
//# CONTACT
//#######################################
function contact_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			var response = http.responseText;
			if (response == 'OK') {
				game_id = encodeURIComponent(document.getElementById("game_id").value);
				num = encodeURIComponent(document.getElementById("num").value);
                load_pages('contact',num,1,0,1);
            }
			else document.getElementById("contact_loading").innerHTML = response;

		}
  	}
	catch(e){}
	finally{}
}

function contact_check_values() {
	game_id = encodeURIComponent(document.getElementById("game_id").value);
	num = encodeURIComponent(document.getElementById("num").value);
	comment_poster = encodeURIComponent(document.getElementById("contact_poster").value);
	comment_email = encodeURIComponent(document.getElementById("contact_email").value);
	comment_content = encodeURIComponent(document.getElementById("contact_content").value);
	comment_sec = encodeURIComponent(document.getElementById("contact_sec").value);
	try {
	    document.getElementById("contact_loading").innerHTML = loadingText;
		document.getElementById("contact_loading").style.display = "block";
		http.open('POST',  'index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
		http.onreadystatechange = contact_handleResponse;
		http.send('&contact_poster='+contact_poster+'&contact_email='+contact_email+'&contact_content='+contact_content+'&contact_sec='+contact_sec);
		document.getElementById("submit").disabled=true;document.getElementById("submit").value="Wait...";
	}
	catch(e){}
	finally{}
  return false;
}

//#######################################
//# COMMENT
//#######################################
function comment_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			var response = http.responseText;
			if (response == 'OK') {
				game_id = encodeURIComponent(document.getElementById("game_id").value);
				num = encodeURIComponent(document.getElementById("num").value);
                load_pages('comment',num,game_id,0,1);
            }
			else document.getElementById("comment_loading").innerHTML = response;

		}
  	}
	catch(e){}
	finally{}
}

function comment_check_values() {
	game_id = encodeURIComponent(document.getElementById("game_id").value);
	num = encodeURIComponent(document.getElementById("num").value);
	comment_poster = encodeURIComponent(document.getElementById("comment_poster").value);
	comment_email = encodeURIComponent(document.getElementById("comment_email").value);
	comment_content = encodeURIComponent(document.getElementById("comment_content").value);
	setCaptcha = encodeURIComponent(document.getElementById("setCaptcha").value);
	try {
	    document.getElementById("comment_loading").innerHTML = loadingText;
		document.getElementById("comment_loading").style.display = "block";
		http.open('POST',  'index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
		http.onreadystatechange = comment_handleResponse;
		http.send('comment=1&game_id='+game_id+'&num='+num+'&comment_poster='+comment_poster+'&comment_email='+comment_email+'&comment_content='+comment_content+'&setCaptcha='+setCaptcha);
		document.getElementById("submit").disabled=true;document.getElementById("submit").value="Wait...";
	}
	catch(e){}
	finally{}
  return false;
}
//#######################################
//# RATING
//#######################################
function rating(game_id,star) {
   		field = document.getElementById("rating_field");
		field.innerHTML = loadingText;
   		http.open('POST',  'index.php');
   		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   		http.onreadystatechange = handleResponse;
		http.send('rating=1&game_id='+game_id+'&star='+star);
 	return false;
}
	// pre-fetch image
	(new Image()).src = RATE_OBJECT_IMG;
	(new Image()).src = RATE_OBJECT_IMG_HALF;
	(new Image()).src = RATE_OBJECT_IMG_BG;

	function show_star(starNum,rate_text) {
		remove_star();
		document.getElementById("rate_text_d").innerHTML = rate_text;
		full_star(starNum);
	}
	
	function full_star(starNum) {
		for (var i=0; i < starNum; i++)
			document.getElementById('star'+ (i+1)).src = RATE_OBJECT_IMG;
	}
	function remove_star() {
		for (var i=0; i < 5; i++)
			document.getElementById('star' + (i+1)).src = RATE_OBJECT_IMG_BG; // RATE_OBJECT_IMG_REMOVED;
	}
	function remove_all_star() {
		for (var i=0; i < 5; i++) document.getElementById('star' + (i+1)).src = RATE_OBJECT_IMG_BG; // RATE_OBJECT_IMG_REMOVED;
		document.getElementById("rate_text_d").innerHTML = 'Rate';
	}
	function show_rating_process() {
		if(document.getElementById("rating_process")) document.getElementById("rating_process").style.display = "block";
		if(document.getElementById("rate_s")) document.getElementById("rate_s").style.display = "none";
	}
	function hide_rating_process() {
		document.getElementById("rating_process").style.display = "none";
		if(document.getElementById("rate_s")) document.getElementById("rate_s").style.display = "block";
	}
//#######################################
//# REQUEST
//#######################################
function request_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			var response = http.responseText;
			if (response == 'OK') {
				num = encodeURIComponent(document.getElementById("num").value);
                load_pages('request',num,0,0,1);
			}
			else document.getElementById("request_loading").innerHTML = response;
			
		}
  	}
	catch(e){}
	finally{}
}

function request_check_values() {
	num = encodeURIComponent(document.getElementById("num").value);
	request_name = encodeURIComponent(document.getElementById("request_name").value);
	request_email = encodeURIComponent(document.getElementById("request_email").value);
	request_content = encodeURIComponent(document.getElementById("request_content").value);
	request_sec = encodeURIComponent(document.getElementById("request_sec").value);
	try{
			document.getElementById("request_loading").innerHTML = loadingText;
			document.getElementById("request_loading").style.display = "block";
			http.open('POST',  'index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
			http.onreadystatechange = request_handleResponse;
			http.send('request=1&num='+num+'&request_name='+request_name+'&request_email='+request_email+'&request_sec='+request_sec+'&request_content='+request_content);
			//document.getElementById("send").disabled=true;document.getElementById("send").value="Wait...";
		}
	  catch(e){}
	  finally{}
	return false;
}
//#######################################
//# BROKEN
//#######################################
function showBroken(film_id) {
   		field = document.getElementById("broken_field");
		field.innerHTML = loadingText;
   		http.open('POST',  'index.php');
   		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   		http.onreadystatechange = handleResponse;
		http.send('broken=1&film_id='+film_id);
 	return false;
}

