// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
String.prototype.trim = function() {
  return this.replace(/(^\s*)|(\s*$)/g, "");
}	

function bytes(str) {
	if (typeof(str) != 'string') {
		str = str.value;
	}
	var len = 0;
	for (var i = 0; i < str.length; i++) {
		if(str.charCodeAt(i) > 127) {
			len++;
		}
		len++;
	}
	return len;
}

function chkstr(str) {
	if (typeof(str) != 'string') {
		str = str.value;
	}
	for (var i = 0; i < str.length; i++) {
		if (str.charCodeAt(i) < 127 && !str.substr(i,1).match(/^\w+$/ig)) {
			return false;
		}
	}
	return true;
}

function clear_notice() {
	if ($('notitle')) { $('notitle').style.display = "none"; }
	if ($('nocontent')) { $('nocontent').style.display = "none"; }
	if ($('errnotice')) { $('errnotice').style.display = "none"; }
}

function check_topic_form(title, content) {
  clear_notice();
  var result = true;
  var titlelen = bytes(title.value.trim());
  var contentlen = bytes(content.value.trim());  

  if (titlelen == 0) {
    document.getElementById('notitle').style.display = "";
    title.focus();        
    result = false;    
  } else if (contentlen == 0) { 
    document.getElementById('nocontent').style.display = "";
    content.focus();  
    result = false;
  }
  
  return result; 
}

function check_reply_form(content) {
  clear_notice();
  var result = true;
  var contentlen = bytes(content.value.trim());  

  if (contentlen == 0) { 
    document.getElementById('nocontent').style.display = "";
    content.focus();  
    result = false;
  }
  if (contentlen > 5000) {    
    if (err = document.getElementById('errnotice')) {
      err.style.display = "";
      content.focus();  
      result = false;    
    }
  }
  
  return result; 
}

function check_pop_form(title, link, intro) {
  var result = true;
  Element.update('errnotice','');
  if (bytes(title.value.trim()) == 0) {
  	new Insertion.Bottom('errnotice', '<span style="color:red;">忘记填写活动主题了?<br/>');
	result = false;
  }
  if (bytes(link.value.trim()) <= 10) {
  	new Insertion.Bottom('errnotice', '<span style="color:red;">忘记填写活动网址了?<br/>');
	result = false;
  }
  if (bytes(intro.value.trim()) == 0) {
  	new Insertion.Bottom('errnotice', '<span style="color:red;">忘记填写你的推介了?<br/>');
	result = false;
  }
  if (link.value.length > 100) {
  	new Insertion.Bottom('errnotice', '<span style="color:red;">网址超长，请改一下:)<br/>');
	result = false;  	
  }    
  if (title.value.length > 30) {
  	new Insertion.Bottom('errnotice', '<span style="color:red;">主题太长了，请浓缩一下:)<br/>');
	result = false;  	
  }  
  if (intro.value.length > 150) {
  	new Insertion.Bottom('errnotice', '<span style="color:red;">推介太长了，请浓缩一下:)<br/>');
	result = false;  	
  }
  return result;
}

function check_e1_form(name, link) {
  clear_notice();
  var result = true;
  var namelen = bytes(name.value.trim());

  if (namelen == 0) {
    document.getElementById('notitle').style.display = "";
    name.focus();        
    result = false;    
  }
	
  return result; 
}

function img_show(imgid){
  cells = document.getElementsByName(imgid);
  for(j = 0; j < cells.length; j++) cells[j].style.display = 'block';
}
	
function img_hidden(imgid){
  cells = document.getElementsByName(imgid);
	for(j = 0; j < cells.length; j++) cells[j].style.display = 'none';
}

function chkbox(form, grpname) {
  var radio;
  for (var i=0; i < form.elements[grpname].length; i++) {
    radio = form.elements[grpname][i];
    if (radio.checked) {
      return true;
    }
  }
  return false;
}	

function loadImage(pid, url) {
  cells = document.getElementsByName(pid);
  for(j = 0; j < cells.length; j++) {cells[j].style.display = 'block'; cells[j].src = 'http://www.bindou.com' + url;}
}

function hideImage(pid) {
  cells = document.getElementsByName(pid);
  for(j = 0; j < cells.length; j++) cells[j].style.display = 'none';
}

function copyToClipBoard() { 
  var clipBoardContent = '';
  clipBoardContent += document.title; 
  clipBoardContent += '\r\n';
  clipBoardContent += location.href;

  if (window.clipboardData) { 
    window.clipboardData.setData("Text",clipBoardContent);
    alert("成功复制网址，可以粘贴到QQ/MSN啦！");
    return true;
  } else { 
    var flashcopier = 'flashcopier';
    if (!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="/j/_clipboard.swf" FlashVars="clipboard='+myesc(clipBoardContent)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
    alert("成功复制网址，可以粘贴到QQ/MSN啦！");
    return true; 
  } 
  return false; 
} 

function copyStrToClipBoard(str, alert_words) { 
  var clipBoardContent = str;

  if (window.clipboardData) { 
    window.clipboardData.setData("Text",clipBoardContent);
    alert(alert_words);
    return true;
  } else { 
    var flashcopier = 'flashcopier';
    if (!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="/j/_clipboard.swf" FlashVars="clipboard='+myesc(clipBoardContent)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
    alert(alert_words);
    return true; 
  } 
  return false; 
} 

function myesc(text) {
  return (text.replace(/'/g, '%27')).replace(/"/g, '%22');
}
