var myDate=new Date();
myDate.setDate(myDate.getDate()+2);

function ajaxRequest(url, callBackFunction, type)
{
	//var url = '/process/_ajax_rate.jsp?URL='+ featured_item.siteURL +'&CID='+ featured_cid +'&Value='+ value;
	var httpRequest;
	if(window.ActiveXObject)
	{
		httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if(window.XMLHttpRequest)
	{
		httpRequest = new XMLHttpRequest();
	}
	//if(bd.browser != "Explorer")
	{
		url = url.replace("&","?");
		url = "../process/proxy.php?P="+ url;
	}
	if(!type || type == null)
	  type = "GET"
	//alert("httpRequest: "+ type +" - "+ url);
	httpRequest.open(type, url, true);
	httpRequest.onreadystatechange = function() {ajaxResponse(httpRequest, callBackFunction); } ;
	httpRequest.send(null);
}
function ajaxResponse(httpRequest, callBackFunction)
{
	if(httpRequest.readyState == 4)
	{
		if(httpRequest.status == 200)
		{
			if(callBackFunction)
			  callBackFunction(httpRequest.responseText);
		}
		else
		{
			//alert("Error loading page\n"+ httpRequest.status +":"+ httpRequest.statusText);
		}
	}
}
function ajaxResponse_parser(arrayOfParams, s)
{
  for(var x = 0; x < arrayOfParams.length; x++)
	{
		if(arrayOfParams[x] == s)
		  return arrayOfParams[x + 1];
	}
}

function processReqChange(req, handler, targetElement, url)
{
  if(req.readyState == 4 && req.status == 200 && req.responseXML)
  {
    parseFeedList(req.responseXML, handler, targetElement, url);
  }
}

function loadXMLDoc(url, handler, targetElement, type)
{
  var req;
  if(window.XMLHttpRequest)
  {
    try { req = new XMLHttpRequest(); } catch(e) { req = false; }
  }
  else if(window.ActiveXObject)
  {
    try { req = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {
    try { req = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { req = false; } }
  }
  if(req)
  {
		try
  	{
    	req.onreadystatechange = function() { processReqChange(req, handler, targetElement, url); };
			//if(bd.browser != "Explorer")
			var sendURL;
      {
				sendURL = url.replace("&","?");
				sendURL = "../process/proxyXML.php?P="+ sendURL;
			}
      if(!type || type == null)
	      type = "GET"
      //alert("AJAX "+ type +" : "+ url);
			req.open(type, sendURL, true);
    	req.setRequestHeader("Expires", myDate);
    	req.send("");
		}
		catch(e)
  	{
			if(e && e.message)
    	  alert("loadXMLDoc error: "+ e.message);
			else
			  alert("loadXMLDoc error: "+ e);
  	}
  }
}
var failCount = 0;
function parseFeedList(responseXML, handler, targetElement, url)
{
  try
  {
    var node = responseXML;
    var xmlChannel = node.getElementsByTagName('channel').item(0);
    if(xmlChannel == null)
      return handler(false);
  }
  catch(e)
  {
    if(failCount < 10)
    {
      failCount++;  //alert("resend....: "+ url);
      loadXMLDoc(url +"&"+ (Math.random() * 999), handler, targetElement, "POST");
    }
    //alert("parseFeedList error: "+ e);
    return false;
  }
  var rss = new Array();
  var channel =
  {
    title: 					_getElementValue(xmlChannel, 'title'),
    link:						_getElementValue(xmlChannel, 'link'),
    description:		_getElementValue(xmlChannel, 'description'),
    language:				_getElementValue(xmlChannel, 'language'),
    copyright:			_getElementValue(xmlChannel, 'copyright'),
    managingEditor:	_getElementValue(xmlChannel, 'managingEditor'),
    webMaster:			_getElementValue(xmlChannel, 'webMaster'),
    pubDate:				_getElementValue(xmlChannel, 'pubDate'),
    lastBuildDate:	_getElementValue(xmlChannel, 'lastBuildDate')
  };
  rss[0] = channel;

  var items = new Array();
  var xmlItems = xmlChannel.getElementsByTagName('item');
  for(var n=0; n<xmlItems.length; n++)
  {
    var xmlItem = xmlItems[n];
    var item = {
      name:				  _getElementValue(xmlItem, 'name'),
      title:				_getElementValue(xmlItem, 'title'),
      link:					_getElementValue(xmlItem, 'link'),
      desc1:				_getElementValue(xmlItem, 'description'),
      //author:				_getElementValue(xmlItem, 'author'),
      category:			_getElementValue(xmlItem, 'category'),
      comments:			_getElementValue(xmlItem, 'comments'),
      guid:					_getElementValue(xmlItem, 'guid'),
      pubDate:			_getElementValue(xmlItem, 'pubDate'),
      wid:					_getElementValue(xmlItem, 'wid'),
      //siteURL:			_getElementValue(xmlItem, 'siteURL'),
      clicks:				_getElementValue(xmlItem, 'clicks'),
			rateClicks:		_getElementValue(xmlItem, 'rateClicks'),
      rateSum:			_getElementValue(xmlItem, 'rateSum'),
      fullVidLow:		_getElementValue(xmlItem, 'fullVidLow'),
      fullVidHigh:	_getElementValue(xmlItem, 'fullVidHigh'),
      countStills:	_getElementValue(xmlItem, 'countStills'),
      countVStills:	_getElementValue(xmlItem, 'countVStills'),
			desc2:				_getElementValue(xmlItem, 'desc2'),
      niche:				_getElementValue(xmlItem, 'niche'),
      studioName:	  _getElementValue(xmlItem, 'studioName'),
      titleName:	  _getElementValue(xmlItem, 'titleName'),
      sceneName:	  _getElementValue(xmlItem, 'sceneName')
    };
    items.push(item);
  }

  var rss = {channel: channel, items: items};
	if(targetElement)
	{ //alert("ajax: "+ targetElement);
		//targetAreaCK(targetElement);
    handler(rss, targetElement);
	}
	else
	  handler(rss);
}

function targetAreaCK(targetElement)
{
	var element = document.getElementById(targetElement);  //alert(element);
	if(!element)
	{
		if(targetElement == "MAIN_1")
		  element.innerHTML = "";
		element = document.getElementById("MAIN");
		var newElement = document.createElement("div");
		newElement.id = targetElement;
		element.appendChild(newElement);
	}
}

function _getElementValue(node, elementName)
{
  try
  {
    var value = node.getElementsByTagName(elementName).item(0).firstChild.data;
  }
  catch(e){var value = '';}
  return value;
}
