// Retrieve the value of the cookie with the specified name.
function GetCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");

  for (var i=0; i < aCookie.length; i++)
  {
    // a name alue pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");

    if (sName == aCrumb[0])
    {
      return aCrumb[1];
    }
  }
  return false;

  // a cookie with the requested name does not exist
  return null;
}

function decode64(input,is_debug) {
   var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

	input = unescape( input );
   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace("/[^A-Za-z0-9\+\/\=]/g", "");
   flag = 0; // flag of Bytes follows.
   ucs2_code = 0;

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;
		
	  if ( chr1 <= 0x80) {
		  // 单字节ASCII，直接输出
		  ucs2_code = chr1;
	  } else if (chr1 <= 0xC0)
	  {
		  // 非独立字节
		  --flag;
		  if (flag ==1)
		  {
			  ucs2_code += ( chr1-0x80 ) << 6;
		  }else {
		      ucs2_code += chr1-0x80;
		  }
	  } else if (chr1 <= 0xE0)
	  {
		  // 双字节utf-8编码
		  flag = 1;
		  ucs2_code = ( chr1-0xC0 ) << 6;
	  } else if (chr1 <=0xF0)
	  {
		  // 三字节utf-8编码
		  flag = 2;
		  ucs2_code = ( chr1-0xE0 ) << 12;
	  }
	  if (flag == 0)
	  {
	      output = output + String.fromCharCode(ucs2_code);
	  }

      if (enc3 != 64) {
          if ( chr2 <= 0x80) {
			  ucs2_code = chr2;
		  } else if (chr2 <= 0xC0)
		  {
		      --flag;
			  if (flag == 1)
			  {
				  ucs2_code += ( chr2-0x80 ) << 6;
			  }else {
		          ucs2_code += chr2-0x80;
			  }
	      } else if (chr2 <= 0xE0)
	      {
		      flag = 1;
		      ucs2_code = ( chr2-0xC0 ) << 6;
	      } else if (chr2 <=0xF0)
	      {
		      flag = 2;
		      ucs2_code = ( chr2-0xE0 ) << 12;
	      }
		  if (flag == 0)
		  {
			   output = output + String.fromCharCode(ucs2_code);
		  }
      }

      if (enc4 != 64) {
          if ( chr3 <= 0x80) {
			  ucs2_code = chr3;
		  } else if (chr3 <= 0xC0)
		  {
		      --flag;
			  if (flag==1)
			  {
				  ucs2_code += ( chr3-0x80 ) << 6;
			  }else {
		          ucs2_code += chr3-0x80;
			  }
	      } else if (chr3 <= 0xE0)
	      {
		      flag = 1;
		      ucs2_code = ( chr3-0xC0 ) << 6;
	      } else if (chr3 <=0xF0)
	      {
		      flag = 2;
		      ucs2_code = ( chr3-0xE0 ) << 12;
	      }
		  if (flag == 0)
		  {
			   output = output + String.fromCharCode(ucs2_code);
		  }
      }
   } while (i < input.length);

   return output;
}
var userinfo;
if ( false != GetCookie("tuniuuser") && null != document.getElementById('comment-author'))
{
	userinfo = decode64(GetCookie("tuniuuser")).split(',');

	document.getElementById('comment-author').value = userinfo[2];
	document.getElementById('comment-author').disabled = "disabled";
	document.getElementById('comment-url').value = "http://blog.tuniu.com/"+userinfo[1];
	document.getElementById('comment-url').disabled = "disabled";
}

function check_chinese(str){
        var re = new RegExp("[\u4E00-\u9FA5]+");       //汉字的范围
        if (str == ''){ return false;}
        else if (!re.test(str)){ return false;}
		else { return true; }
}

function validate()
{
	objForm = document.forms[0];
	if(objForm == undefined){
		return ;
	}
	if ( document.forms[0].onsubmit )
	{
		document.forms[0].onsubmit=validate_comment;
	}

	if(document.addEventListener)
		objForm.addEventListener("submit",validate_comment,false);
	else if(document.attachEvent)
	    objForm.attachEvent("onsubmit",validate_comment);  
}

function validate_comment(evt)
{
	str = document.forms[0]['content'].value;
	flag = check_chinese(str);
	if (!flag) {
		alert("回复中必须含有中文");
		if ( evt && evt.preventDefault )
		{
			evt.preventDefault();
		}
		return false;
	}
	else 
	{
		return true;
	}
}

window.onload=validate;