/*
 * 函数说明：取出对象，等于document.getElementById()
 * 参数：	 对象名，参数可以多个，用逗号隔开
 * 返回值：	对象
 * 时间：2006-6-27
 */
function $() {
  var elements = new Array();
  
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1) 
      return element;
      
    elements.push(element);
  }
  
  return elements;
}
String.prototype.trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.len = function(){
	return this.replace(/[^\x00-\xff]/g,"aa").length;
}
/**
 * 功能：检验长度是否正确
 * 参数：str 检验值,minlen 最小长度,maxlen 最大长度
 * 返回：TRUE OR FALSE
 */
function checkByteLength(str,minlen,maxlen) {
	if (str == null) return false;									//为空返回false
	var l = str.length;
	var blen = 0;
	for(i=0; i<l; i++) {										//循环取得检验值的长度
		if ((str.charCodeAt(i) & 0xff00) != 0) {
			blen ++;
		}
		blen ++;
	}
	if (blen > maxlen || blen < minlen) {							//判断长度是否合法
		return false;
	}
	return true;
}
/**
 * 功能：检验用户名是否合法
 * 参数：value 检验值
 * 返回：TRUE OR FALSE
 */
function validateUsername(value){
	var patn = /^[a-zA-Z]+[a-zA-Z0-9]+$/; 
	//var patn = /^[^\s]*$/;
	if(!checkByteLength(value,4,16)) return true;					//判断长度是否合法
	if(!patn.test(value)){										//判断格式是否合法
		return true;
	}
	return false; 
}
/**
 * 功能：检验密码是否合法
 * 参数：value 检验值
 * 返回：TRUE OR FALSE
 */
function validatePassword(value){
	if(!checkByteLength(value,6,16)) return false;						//判断长度是否合法
	var patn1 = /^[a-zA-Z0-9_]+$/;
	if(!patn1.test(value) ) return false;								//判断格式是否合法
	return true; 
}
/**
 * 功能：检验Email是否合法
 * 参数：value 检验值
 * 返回：TRUE OR FALSE
 */
function validateEmail(value){
	var patn=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	if(!patn.test(value)) return false;
	return true;
}
/**
 * 功能：检验用户名
 * 参数：value 检验值
 * 返回：TRUE OR FALSE
 */
function checkUserName(value)
{
	if(value == '')											//判断用户名是否为空，返回false
	{
		return false;
	}
	if(!validateUsername(value))								//判断用户名是否合法
	{
		return false;
	}
	return true;
}
/**
 * 功能：检验密码
 * 参数：value 检验值
 * 返回：TRUE OR FALSE
 */
function CheckPassword(value)
{
	if(value == '')											//判断密码是否为空
	{
		return false;
	}
	if(!validatePassword(value))								//判断密码是否合法
	{
		return false;
	}
	return true;
}
/**
 * 功能：检验重复密码
 * 参数：value 检验值
 * 返回：TRUE OR FALSE
 */
function CheckConfirm(value)
{
	if(value != document.register.password.value)					//判断重复密码是否与密码相符
	{
		return false;
	}
	return true;
}
/**
 * 功能：检验昵称
 * 参数：value 检验值
 * 返回：TRUE OR FALSE
 */
function CheckNickName(value)
{
	if(value != '')
	{
		if(!checkByteLength(value,4,20))							//判断昵称长度是否正确
		{
			return false;
		}
	}
	return true;
}
/**
 * 功能：检验Email
 * 参数：value 检验值
 * 返回：TRUE OR FALSE
 */
function CheckEmail(value)
{
	if(value == '')
	{
		return false;
	}
	if(!validateEmail(value))
	{
		return false
	}
	return true;
}
/**
 * 功能：博客名称
 * 参数：value 检验值
 * 返回：TRUE OR FALSE
 */
function CheckBlogName(value)
{
	if(value == '')
	{
		return false;
	}
	
	if(!checkByteLength(value,1,50))
	{
		return false;	
	}
	return true;
}
/**
 * 功能：检验表单的各项是否正确
 * 返回：TRUE OR FALSE
 */
function CheckForm()
{
	if(!checkUserName(document.register.username.value))			//判断用户名是否正确
	{
		alert('用户名称格式不正确');
		document.register.UserID.focus();
		return false;
	}
	
	if(!CheckPassword(document.register.password.value))			//判断密码是否正确
	{
		alert('密码格式不正确');
		document.register.password.focus();
		return false;
	}
	
	if(!CheckConfirm(document.register.Confirm.value))				//判断确认密码是否正确
	{
		alert('确认密码与密码不一致');
		document.register.Confirm.focus();		
		return false;
	}
	
	if(!CheckNickName(document.register.NickName.value))			//判断昵称是否正确
	{
		alert('昵称不正确');
		document.register.NickName.focus();
		return false;
	}
	if(!CheckEmail(document.register.Email.value))					//判断Email是否正确
	{
		alert('Email格式不正确');
		document.register.Email.focus();
		return false;
	}
	if(!CheckBlogName(document.register.Blog.value))				//判断博客名称是否正确
	{
		alert('博客名称不正确');
		document.register.Blog.focus();
		return false;		
	}
	return true;
}

function resizePic(pic,rwidth) {
	pwidth = pic.width;
	if (pwidth > rwidth) {
		pheight = pic.height;
		rheight =  Math.ceil(pheight * (rwidth/pwidth));
		pic.width = rwidth;
		pic.height = rheight;
	}	
}

function checkAll(checked, theSelect){                                                                     
	if (theSelect.length > 0) {                                                                             
		for(i = 0; i < theSelect.length; i++) {                                           
			theSelect[i].checked = checked;                                               
		}       
	} else {   
	theSelect.checked = checked;                                                                       
	}            
}
	 
function getSelectId(theSelect) {
	var returnSelectIds = '';
	if (typeof(theSelect) != 'undefined') {                                                       
		if (theSelect.length > 0) {                                                                
			for(i = 0; i < theSelect.length; i++) {                              
				if(theSelect[i].checked) returnSelectIds += ',' + theSelect[i].value;
			}
			return returnSelectIds.substring(1);                                    
		} else {
			returnSelectIds = theSelect.value;                                       
		}       
	}                  
	return returnSelectIds; 
}

function decodeHtml(str){
	str = str.replace(/##1/g,"'"  );
	str = str.replace(/##2/g,"\"" );
	str = str.replace(/##3/g,";"  );	 
  return str;
}

function formReset(obj)
{
	obj.reset();
}

function winOpen(url,target,height,width)
{
	window.open (url, target, 'height='+height+', width='+width+',toolbar=no, menubar=no, scrollbars=auto, resizable=no,location=no,status=no');
}
/***********Offer和Prodcut的Subject格式化：介词为小写，其他单词首字母大写;去掉重复的空格*****/
function formatSubject( obj ){
	if(!obj)
		return;
	var oldStr = obj.value;
	if( (!obj.value) || (oldStr.length<=0) ){
		return;
	}
	var newStr = oldStr.toLowerCase();
	//delRepeatBackSpace
	newStr = delRepeatBackSpace(newStr);
	
	var startInd = 0;
	while( startInd < newStr.length ){
		var ind = newStr.indexOf(" ", startInd);
		if(ind<0){
			break;
		}
		//handle the middle words
		newStr = upperHeadStringChar( newStr, startInd, ind );
		startInd = ind+1;
	}
	//handle the last word
	newStr = upperHeadStringChar( newStr, newStr.lastIndexOf(" ")+1, newStr.length );
		
	obj.value = newStr;
}

var prepositions = new Array(
	"the",
	"that",
	"this",
	"these",
	"those",
	"at",
	"in",
	"on",
	"over",
	"above",
	"under",
	"below",
	"between",
	"among",
	"after",
	"behind",
	"across",
	"through",
	"past",
	"for",
	"since",
	"during",
	"thoughout",
	"as",
	"by",
	"to",
	"from",
	"about",
	"with",
	"without",
	"upon",
	"into",
	"of",
	"off",
	"around",
	"onto",
	"against",
	"up",
	"so",
	"or",
	"unless",
	"but",
	"while",
	"amongst",
	"away",
	"beside",
	"both",
	"how",
	"however",
	"like",
	"most",
	"near",
	"next",
	"whether",
	"utmost",
	"upto");

function upperHeadStringChar( newStr, startInd, ind ){
	if( startInd<ind ){
		var tmpStr = newStr.substring(startInd, ind);
		var upChar = "";
		var isPrep = "false";
		if(startInd>0){//第一个单词都要大写
			for( i=0;i<prepositions.length;i++ ){
				if( tmpStr == prepositions[i]  ){
					isPrep = "true";
					return newStr;	
				}	
			}
		}
		if( isPrep == "false" ){
			upChar = tmpStr.substring(0, 1);
			upChar = upChar.toUpperCase();
		}
		var rtnStr = "";
		if(startInd > 1){
			rtnStr = newStr.substring(0, startInd);
		}
		rtnStr = rtnStr + upChar;
		if( startInd+1 < newStr.length ){
			rtnStr = rtnStr + newStr.substring(startInd+1, newStr.length);
		}
		newStr = rtnStr;
	}
	
	return newStr;
} 

function delRepeatBackSpace( subject ){
	var sArray = subject.split(" ");
	var rtnStr = "";
	for(i=0;i<sArray.length;i++){
		if( (rtnStr != "") && ( sArray[i] != "" ) ){
			rtnStr = rtnStr + " ";
		}
		rtnStr = rtnStr + sArray[i];
	}
	return rtnStr;
}

function isContainHtmlLabel( inValue ){
	if( !inValue )	
		return false;
	inValue = inValue.toLowerCase( );
	if( (inValue.indexOf( "<table" ) >-1 ) || ( inValue.indexOf("<img") >-1 ) || ( inValue.indexOf("<a") >-1 ) || ( inValue.indexOf("<tr") >-1 ) || ( inValue.indexOf("<td") >-1 )){
		return true;
	}
	return false;
}
/*
 * 函数说明：判断搜索内容是否为空
 * 参数：	字符串
 * 返回值：	是/否
 * 时间：2006-5-12
 */
function checkform(str){
	searchFormObj = document.getElementById(str);
	var v = trim(searchFormObj.keywords.value);
	if(v.length > 100){
		alert("您输入的关键字过长！");
		return false;
	}
	if(v == ""  || v.substring(0,3) =="请输入") {
		alert("请输入关键字！");
		return false;
	}
}
/*
 * 函数说明：去除头尾空格
 * 参数：	字符串
 * 返回值：	无
 * 时间：2006-5-12
 */
function trim(inputString) {
	return inputString.replace(/^ +/,"").replace(/ +$/,"");
}
/*
 * 函数说明：取cookie值
 * 参数：	cookie字段名
 * 返回值：	cookie值
 * 时间：2006-5-12
 */
function getCookie(sName) {
	  var aCookie = document.cookie.split("; ");
	  for (var i=0; i < aCookie.length; i++)
	  {
	    var aCrumb = aCookie[i].split("=");
	    if (sName == aCrumb[0]) 
	      return unescape(aCrumb[1]);
	  }
	  return null;		
}
String.prototype._indexOf = String.prototype.indexOf;
String.prototype.indexOf = function()
{
 if(typeof(arguments[arguments.length - 1]) != 'boolean'){
  return this._indexOf.apply(this.toLowerCase(),arguments);
 }
 else
 {
  var bi = arguments[arguments.length - 1];
  var thisObj = this;
  var idx = 0;
  if(typeof(arguments[arguments.length - 2]) == 'number')
  {
   idx = arguments[arguments.length - 2];
   thisObj = this.substr(idx);
  }
  
  var re = new RegExp(arguments[0],bi?'i':'');
  var r = thisObj.match(re);
  return r==null?-1:r.index + idx;
 }
}