/**
  * Default function for creating a http-request handler
  **/

var xml_datastream = "";

var tmp_container;
var tmp_tag;
var tmp_align;

function isReady()
{
	return true;
}

//<![CDATA[

google.load("search", "1.0", {"language" : "en"});
var searchControl;
var keyword = "";
var random_icon_list;

function OnLoad() {
  // Create a search control
  searchControl = new google.search.SearchControl();

  // create a draw options object so that we
  // can position the search form root
  var options = new google.search.DrawOptions();
  options.setDrawMode(GSearchControl.DRAW_MODE_TABBED);
  options.setSearchFormRoot(document.getElementById("GoogleSearchForm"));

  // Add in a full set of searchers
  //var siteSearch = new google.search.WebSearch();
  //siteSearch.setSiteRestriction("easyiconfinder.com");
  //siteSearch.setUserDefinedLabel("EasyIconFinder");
  //searchControl.addSearcher(siteSearch);

  var g_web = new google.search.WebSearch();
  g_web.setSiteRestriction("014466504654672338377:pqi-5miwdtc");
  g_web.setUserDefinedLabel("Web");
  searchControl.addSearcher(g_web);
  
  var g_img = new google.search.ImageSearch();
  g_img.setSiteRestriction("014466504654672338377:pqi-5miwdtc");
  g_img.setUserDefinedLabel("Images");
  searchControl.addSearcher(g_img);
  
  var g_blob = new google.search.BlogSearch();
  //g_blob.setSiteRestriction("014466504654672338377:pqi-5miwdtc");
  g_blob.setUserDefinedLabel("Blog");
  searchControl.addSearcher(g_blob);

  var g_video = new google.search.VideoSearch();
  g_video.setUserDefinedLabel("Video");
  searchControl.addSearcher(g_video);

  // Tell the searcher to draw itself and tell it where to attach
  searchControl.setSearchCompleteCallback(this, clearIcons);
  
  searchControl.draw(document.getElementById("GoogleSearchResults"), options);

  // Execute an inital search
  if (keyword != "" )
  { 
  	searchControl.execute(keyword);
  }
}

google.setOnLoadCallback(OnLoad);

//]]>

function clearIcons(p_searchcontrol, p_searcher)
{
	if (document.getElementById("random_icons") != null)
       	document.getElementById("random_icons").innerHTML = "";
}

function googleSearch(v_keyword)
{
	keyword = v_keyword;
	document.s_form.s_keyword.value = v_keyword;
	if (searchControl != null)
		if (keyword != "" )
		{
			searchControl.execute(keyword);
		}
}

function cleanKeyword()
{
	document.s_form.s_keyword.value = "" ;
}

function resizeFlashObject(v_name, v_width, v_height)
{
	var v_object = document.getElementById(v_name);
	v_object.visible = false;
	v_object.width = v_width;
	v_object.height = v_height;
}

function get_xmlhttp()
{
	var xmlhttp = false;

    	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)

	// JScript gives us Conditional compilation, we can cope with old IE versions.
	//   and security blocked creation of the objects.

 	try
 	{
  		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 	}
 	catch (eIE_basic)
 	{
  		try
  		{
   			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  		}
  		catch (eIE_basic2)
  		{
   			xmlhttp = false;
  		}
 	}

	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined')
	{
		try
		{
			xmlhttp = new XMLHttpRequest();
		} catch (eDefault)
		{
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest)
	{
		try
		{
			xmlhttp = window.createRequest();
		} catch (eDefault)
		{
			xmlhttp=false;
		}
	}

	return xmlhttp;
}

/** REMARK: we need a component with a specific name enclosed with DIV tags **/
function change_content(v_container, v_info, v_tag, v_align)
{
	<!-- Some more robust code for dynamic content. -->
	var newdiv = document.createElement("div");
	newdiv.setAttribute("id",v_tag);
	newdiv.innerHTML = "<p align='" + v_align + "'>" + v_info + "</p>";

	v_container.replaceChild(newdiv, document.getElementById(v_tag));
}


/** REMARK: we need a component with a specific name enclosed with DIV tags **/
function change_content_flash(v_container_name, v_info, v_tag, v_align)
{
	<!-- Some more robust code for dynamic content. -->
	var newdiv = document.createElement("div");
	newdiv.setAttribute("id",v_tag);
	newdiv.innerHTML = "<p align='" + v_align + "'>" + v_info + "</p>";

	/** find the container **/
	var v_container = document.getElementById(v_container_name);

	v_container.replaceChild(newdiv, document.getElementById(v_tag));
}

function request_server_info_flash(v_url, v_container_name, v_tag, v_align)
{
	var xmlhttp 	  = get_xmlhttp();
	xmlhttp.open("GET", v_url ,true);

	tmp_container = document.getElementById(v_container_name);
	tmp_tag       = v_tag;
	tmp_align     = v_align;

	xmlhttp.onreadystatechange=function()
	{
  		if (xmlhttp.readyState==4)
  		{
 			change_content(tmp_container, xmlhttp.responseText, tmp_tag, tmp_align)
  		}
 	}
 	xmlhttp.send(null);
}

function request_server_info(v_url, v_container, v_tag, v_align)
{
	var xmlhttp 	  = get_xmlhttp();
	xmlhttp.open("GET", v_url ,true);

	tmp_container = v_container;
	tmp_tag       = v_tag;
	tmp_align     = v_align;

	xmlhttp.onreadystatechange=function()
	{
  		if (xmlhttp.readyState==4)
  		{
 			change_content(tmp_container, xmlhttp.responseText, tmp_tag, tmp_align)
  		}
 	}
 	xmlhttp.send(null);
}

function request_server_info_noret(v_url)
{
	var xmlhttp 	  = get_xmlhttp();
	xmlhttp.open("GET", v_url ,true);

	xmlhttp.onreadystatechange=function()
	{
  		if (xmlhttp.readyState==4)
  		{
 			//change_content(tmp_container, xmlhttp.responseText, tmp_tag, tmp_align)
  		}
 	}
 	xmlhttp.send(null);
}

function mutate_editor_content(v_content)
{
	var v_current_content = tinyMCE.activeEditor.getContent();
	tinyMCE.activeEditor.setContent(v_content + v_current_content);
}

function request_server_info_editor(v_url)
{
	var xmlhttp 	  = get_xmlhttp();
	xmlhttp.open("GET", v_url ,true);

	xmlhttp.onreadystatechange=function()
	{
  		if (xmlhttp.readyState==4)
  		{
  			tinyMCE.activeEditor.setContent(xmlhttp.responseText);
  		}
 	}
 	xmlhttp.send(null);
}

function request_server_info_chpost(v_url, v_forms, v_container, v_tag, v_align)
{
      var xmlhttp 	  = get_xmlhttp();
      var xml_start    = 1;
      var xml_datastream  = "";

      tmp_container = v_container;
      tmp_tag       = v_tag;
      tmp_align     = v_align;

      for(i=0; i<v_forms.elements.length; i++)
      {
      		if(v_forms.elements[i].type!="button" && v_forms.elements[i].type!="submit" && v_forms.elements[i].name != "result")
        		xml_datastream = xml_datastream + (xml_start == 1 ? "" : "&") + v_forms.elements[i].name + "=" + encodeURIComponent ( v_forms.elements[i].value );

        	xml_start = 0;
      }

      xmlhttp.open('POST', v_url, true);
      xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlhttp.setRequestHeader("Content-length", xml_datastream.length);
      xmlhttp.setRequestHeader("Connection", "close");

      xmlhttp.onreadystatechange=function()
      {
  		if (xmlhttp.readyState==4)
  		{
  			if (xmlhttp.status==200)
  			{
				change_content(tmp_container, xmlhttp.responseText, tmp_tag, tmp_align);
  			}
  		}
      }
      xmlhttp.send(xml_datastream);
}

function request_server_info_post(v_url, v_forms)
{
      document.getElementById("result").value = 'UPDATING...';

      var xmlhttp 	  = get_xmlhttp();
      var xml_start    = 1;
      var xml_datastream  = "";

      for(i=0; i<v_forms.elements.length; i++)
      {
      		if(v_forms.elements[i].type!="button" && v_forms.elements[i].type!="submit" && v_forms.elements[i].name != "result")
        		xml_datastream = xml_datastream + (xml_start == 1 ? "" : "&") + v_forms.elements[i].name + "=" + encodeURIComponent ( v_forms.elements[i].value );

        	xml_start = 0;
      }

      xmlhttp.open('POST', v_url, true);
      xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlhttp.setRequestHeader("Content-length", xml_datastream.length);
      xmlhttp.setRequestHeader("Connection", "close");

      xmlhttp.onreadystatechange=function()
      {
  		if (xmlhttp.readyState==4)
  		{
  			if (xmlhttp.status==200)
  			{
  				document.getElementById("result").value = xmlhttp.responseText;
  			}
  			else
  				document.getElementById("result").value = 'ERROR';
  		}
      }
      xmlhttp.send(xml_datastream);
}

function change_keyword(v_id)
{
	var v_window = window.open(v_id, "_blank","status=1, height=300,width=500,resizable=0");
	v_window.focus();

}

function change_keyword_vw(v_id, v_width, v_height)
{
	var v_window = window.open(v_id, "_blank","status=1, height=" + v_height + ",width=" + v_width + ",resizable=1");
	v_window.focus();

}

function add_more(v_tag)
{
  var txt = "<br><input type=\"file\" name=\"item_file[]\">";
  document.getElementById(v_tag).innerHTML += txt;
}
