/* utils.js author:Haily.Z Email:haily27@126.com website:http://www.ftwu.cn $ */

var Browser = new Object();

Browser.isMozilla = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined') && (typeof HTMLDocument != 'undefined');
Browser.isIE = window.ActiveXObject ? true : false;
Browser.isFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox") != - 1);
Browser.isSafari = (navigator.userAgent.toLowerCase().indexOf("safari") != - 1);
Browser.isOpera = (navigator.userAgent.toLowerCase().indexOf("opera") != - 1);



/* *
 * 显示时间函数
 * @param :  elemId-表单元素id
 */
function showSendTime(elemId)
{
	tmp_second++ ;
	document.getElementById(elemId).value='已送出,等待.' + tmp_second + "秒";
	window.setTimeout("showSendTime('" + elemId + "')", 1000);
}
/* *
 * 帮助信息的显隐函数
 */
function showNotice(objId)
{
  var obj = document.getElementById(objId);
  if (obj)
  {
    if (obj.style.display != "block")
    {
      obj.style.display = "block";
    }
    else
    {
      obj.style.display = "none";
    }
  }
}

/* *
 * 上传图片的预览函数
 * @param :  obj-上传对象;preId-显示预览图片标签ID;width-预览宽度;height-预览高度
 */
function imagePreview(obj,preId,width,height)
{
  	var img = obj.value;
	if(img)
	{
		var patn = /\.jpg$|\.jpeg$|\.gif$|\.png$/i;
		if(!patn.test(img))
		{
			alert('产品图片格式无效！');
			return false;
		}
		else
		{
			 document.getElementById(preId).innerHTML = '<img src="'+img+'" border="0" width="'+ width +'" height="' + height + '">';
		}
	}
}

/* *
 * add one option of a select to another select.
 *
 * @author  Chunsheng Wang < wwccss@263.net >
 */
function addItem(src, dst)
{
  for (var x = 0; x < src.length; x ++ )
  {
    var opt = src.options[x];
    if (opt.selected && opt.value != '')
    {
      var newOpt = opt.cloneNode(true);
      newOpt.className = '';
      newOpt.text = newOpt.innerHTML.replace(/^\s+|\s+$|&nbsp;/g, '');
      dst.appendChild(newOpt);
    }
  }

  src.selectedIndex = -1;
}

/* *
 * move one selected option from a select.
 *
 * @author  Chunsheng Wang < wwccss@263.net >
 */
function delItem(ItemList)
{
  for (var x = ItemList.length - 1; x >= 0; x -- )
  {
    var opt = ItemList.options[x];
    if (opt.selected)
    {
      ItemList.options[x] = null;
    }
  }
}

/* *
 * join items of an select with ",".
 *
 * @author  Chunsheng Wang < wwccss@263.net >
 */
function joinItem(ItemList)
{
  var OptionList = new Array();
  for (var i = 0; i < ItemList.length; i ++ )
  {
    OptionList[OptionList.length] = ItemList.options[i].text + "|" + ItemList.options[i].value;
  }
  return OptionList.join(",");
}


function rowindex(tr)
{
  if (Browser.isIE)
  {
    return tr.rowIndex;
  }
  else
  {
    table = tr.parentNode.parentNode;
    for (i = 0; i < table.rows.length; i ++ )
    {
      if (table.rows[i] == tr)
      {
        return i;
      }
    }
  }
}

document.getCookie = function(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0])
      return decodeURIComponent(aCrumb[1]);
  }

  // a cookie with the requested name does not exist
  return null;
}

document.setCookie = function(sName, sValue, sExpires)
{
  var sCookie = sName + "=" + encodeURIComponent(sValue);
  if (sExpires != null)
  {
    sCookie += "; expires=" + sExpires;
  }

  document.cookie = sCookie;
}

document.removeCookie = function(sName,sValue)
{
  document.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}

function getPosition(o)
{
    var t = o.offsetTop;
    var l = o.offsetLeft;
    while(o = o.offsetParent)
    {
        t += o.offsetTop;
        l += o.offsetLeft;
    }
    var pos = {top:t,left:l};
    return pos;
}

function cleanWhitespace(element)
{
  var element = element;
  for (var i = 0; i < element.childNodes.length; i++) {
   var node = element.childNodes[i];
   if (node.nodeType == 3 && !/\S/.test(node.nodeValue))
     element.removeChild(node);
   }
}

pageJump=function(html)
{
	var p = document.getElementById("goPage").value;
	p = parseInt(p);
	if (isNaN(p)){return;}
	var u = location.href;
	if(html){
		if(u.indexOf("-cp-")<0)
		{
			u=u.replace(/.html/ig,"");
			u= u+"-cp-"+p+".html";
		}else{
			u=u.replace(/cp-\d+/ig,"");
			if (p>1){
				u=u.replace(".html","cp-"+p+".html");
			}
		}	
	}else{
		if(u.indexOf("&cp=")<0)
		{
			u= u+"&cp="+p+".html";
		}else{
			u=u.replace(/cp=\d+/ig,"cp=");
			if (p>1){
				u=u.replace(".html",p+".html");
			}
		}
	}
	location=u;
}
