// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();
// when set to true, display detailed error messages
var showErrors = true;
// initialize the requests cache 
var cache = new Array();


// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {} // ignore potential error
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

// function that displays an error message
function displayError($message)
{
  // ignore errors if showErrors is false
  if (showErrors)
  {
    // turn error displaying Off
    showErrors = false;
    // display error message
    alert("Error encountered: \n" + $message);
 
  }
}



// xplus_detail.php投票用
function process(magid)
{
  params = "";
  if (xmlHttp)
  {

     if (GetCookie(magid)==null)
	{
		
		var i = magid.indexOf("_");
		var mag_ID = magid.substring( 0,i );
		var classid = magid.substring( i+1 );

		var rightNow = new Date();
		var expdate = new Date();
		expdate.setTime (expdate.getTime() + 1 * (24 * 60 * 60 * 1000)); //+1 day
		SetCookie (magid, rightNow.getTime(), expdate, "/");
		params = "?magid=" + mag_ID + "&classid=" + classid;
		
	}
	else
	{
		alert("您已经推荐过了");	
	}
  	
    // don't add null params to cache            
    if (params) cache.push(params);

    // try to connect to the server
    try
    {
      // only continue if the connection is clear and cache is not empty
      if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
           && cache.length>0)
      {
        // get next set of values from cache
        var cacheEntry = cache.shift();
        // initiate the request
        xmlHttp.open("GET", "../inc/xplus_detail_vote.php" + cacheEntry, true);
        xmlHttp.setRequestHeader("Content-Type", 
                                 "application/x-www-form-urlencoded");
        xmlHttp.onreadystatechange = handleRequestStateChange;
        xmlHttp.send(null);
      }
      else
      {
        //setTimeout("process();", 1000);  
      }
    }
    // display the error in case of failure
    catch (e)
    {
      displayError(e.toString());
    }
  }
}

// function that retrieves the HTTP response
function handleRequestStateChange() 
{
  // when readyState is 4, we also read the server response
  if (xmlHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
      	var response = xmlHttp.responseText;
      	if(response == "1" || response == 1)
      	{
      		
      		alert("推荐成功，感谢您的支持");
      	}
      	else if(response == "2" || response == 2)
      	{
      		
      		alert("");
      	}
      	else if(response == "3" || response == 3)
      	{
      		
      		alert("");
      	}
      	else
      	{
	      //	res_win(classids);
	      //  postUpdateProcess();
      	}
     
      }
      catch(e)
      {
        // display error message
        displayError(e.toString());
      }
    } 
    else 
    {
      displayError(xmlHttp.statusText);
    }
  }
}


function getCookieVal (offset)
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	{
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}


function GetCookie (name)
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	
	while(i < clen)
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		{
			return getCookieVal (j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0)
		{
			break;
		}
	}
	return null;
}



// xplus_detail.php留言板用
function checkInput(form)
{
	form.body.value = form.body.value.Trim();
	form.nickName.value = form.nickName.value.Trim();
	
	if(!checklength(form.body.value,1,200))
	{
		alert("内容输入不能为空并且不能超过200个字符！");
		return false;
	}
    
	if(!document.messageForm.cekNick.checked && document.messageForm.nickName.length> 10)
    {
		 alert("* 昵称不能超过10个字符\n");
    }   
	if(!document.messageForm.cekNick.checked && document.messageForm.nickName.value == "")
    {
		 alert( "* 昵称不能为空, 您可以选择匿名发表\n");
		 return false;
    }

	//return true;
	Request(form);
}

function checklength(str,min,max)
{
	if(String_GetLength(str)>=min&&String_GetLength(str)<=max)
		return true;
	else
		return false;
}


function String_GetLength(str)
{ 
       var i,rt=0; 
       for(i=0;i<str.length;i++) 
       { 
         rt++; 
         if(str.charCodeAt(i)>256) 
         rt++; 
       } 
       return rt; 
}
            

String.prototype.Trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}


  function Request(form)
{
  params = "";
  if (xmlHttp)
  {

        form.body.value = form.body.value.Trim();
		form.magid.value = form.magid.value.Trim();
		form.nickName.value = form.nickName.value.Trim();
		var b  = form.body.value;
		var c  = form.magid.value;
		var p  = form.nickName.value;
		body=b;
	    magid=c;
	    nickName=p;
		
	    body=encodeURIComponent(body);
	    nickName=encodeURIComponent(nickName);
	    
		params = "?body=" + body + "&magid=" + magid + "&nickName=" + nickName;

    // don't add null params to cache            
    if (params) cache.push(params);

    // try to connect to the server
    try
    {
      // only continue if the connection is clear and cache is not empty
      if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
           && cache.length>0)
      {
        // get next set of values from cache
        var cacheEntry = cache.shift();
        // initiate the request
        xmlHttp.open("GET", "../inc/xplus_detail_ajax.php" + cacheEntry, true);
        xmlHttp.setRequestHeader("Content-Type", 
                                 "application/x-www-form-urlencoded");
        xmlHttp.onreadystatechange = handleRequestStateChange1;
        xmlHttp.send(null);
      }
      else
      {
        //setTimeout("process();", 1000);  
      }
    }
    // display the error in case of failure
    catch (e)
    {
      displayError(e.toString());
    }
  }
}
function handleRequestStateChange1() 
{
  // when readyState is 4, we also read the server response
  if (xmlHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
      	var response = xmlHttp.responseText;
      	if(response == "1" || response == 1)
      	{
      		
      		res_win();
      	}
      	else if(response == "2" || response == 2)
      	{
      		
      		alert("");
      	}
      	else if(response == "3" || response == 3)
      	{
      		
      		alert("");
      	}
      	else
      	{
	      //	res_win(classids);
	      //  postUpdateProcess();
      	}
     
      }
      catch(e)
      {
        // display error message
        displayError(e.toString());
      }
    } 
    else 
    {
      displayError(xmlHttp.statusText);
    }
  }
}