//去除前后空格Trim
function Trim(inputStr)
{
  return inputStr.replace(/(^\s*)|(\s*$)/g,"");
}

//字段不能为空
function checkEmpty(fieldName)
{
  if(Trim(fieldName.value).length==0)
  {
    fieldName.focus();
    return false;
  }
  return true;
}

//只能是数字0-9
function checkDigital(fieldName)
{
  if(!(/^\d+$/.test(Trim(fieldName.value))))
  {
    fieldName.focus();
    return false;
  }
  return true;
}

function checkNoSymbol(fieldName)
{
  if(!(/^[0-9a-zA-Z\u4E00-\u9FA5]+$/  .test(Trim(fieldName.value))))
  {
    alert("The message you input can only be letters、figures or Chinese words.It cannot contain other ");
    fieldName.focus();
    fieldName.select();
    return false;
  }
  return true;
}

//email地址
function checkEmail(fieldName)
{
  if(!(/\w+@\w+\.\w{1,3}/.test(Trim(fieldName.value))))
  {
    alert("Please input correct Email address!");

    fieldName.focus();
    return false;
  }
  return true;
}

function checkChEnLength(fieldName,length)
{
    var txt=Trim(fieldName.value);
    var reallength = 0;
	for(var i=0;i<txt.length;i++){
		if(/([\u0391-\uFFE5])/.test(txt.charAt(i))){
			reallength += 3;//中文算3个字符
		}else if(/[ -}]/.test(txt.charAt(i))){
			reallength += 1;//单字节算1个字符
		}else{
			reallength += 2;//其它算2个字符
		}
	}
    if(reallength > length)
    { 
       fieldName.focus();
       alert("The length of input is too long.Please input again.");
       return false;
    }
    return true;
}

//短信内容等使用的, 中文算2个
function checkChEnLength_rule(fieldName,maxNum)
{
  var item=fieldName.value;
  var reallength = item.length;
  var maxlength = maxNum;
  var counter=0;//记录字符串中中文字符或3个字节的个数
  var counter2=0;//记录字符串中2个字节的个数
  for (i=0; i<item.length; i++)
  {
    var Pattern = /[ -}]/;

    if(!(Pattern.test(item.charAt(i))))
    {
	  counter++;
    }
	else if (item.charCodeAt(i)>255)
    {
      counter2++;
    }
  }
   if(counter>0)
	   maxlength = Math.ceil(maxNum/2);
	else if(counter2>0)
	   maxlength = Math.ceil(maxNum/2);
  if(reallength>maxlength)
  {
    alert("The length of input is too long.Please input again.");
    fieldName.focus();
    return false;
  }
  return true;
}


//给定的字段的日期格式是yyyy-mm-dd
function compareDate(fieldName1,fieldName2)
{
  str1=Trim(fieldName1.value);
  str2=Trim(fieldName2.value);
  str11=new Date(str1.replace("-",",")).getTime();
  str22=new Date(str2.replace("-",",")).getTime();
  if(str11>str22)
  {
    alert(" Start time shouldn\'t greater than end time.");
    fieldName1.focus();
    return false;
  }
  return true;
}

function checkClick(object,fieldName)
{
	if(object.checked)
	{
		selectAll(fieldName)
	}
	else
	{
		cleanAll(fieldName)
	}
}

//选中所有记录
function selectAll(object)
{
	if(object==null)
	{
		return;
	}
	if(object.value!=null)
	{
		object.checked=true;
	}
	else
	{
		for(var i=0;i<object.length;i++)
		object[i].checked=true;
	}
}

//清除所有选中记录
function cleanAll(object)
{
	if(object==null)
	{
		return;
	}
	if(object.value!=null)
	{
		object.checked=false;
	}
	else
	{
		for(var i=0;i<object.length;i++)
		object[i].checked=false;
	}
}

function findpwd(prefix)
{
     var pre = prefix;
     var url =  pre + '/controller?action=enterfindPassword.do';
     window.document.location.href=url;
}

function logout(prefix)
{
	if (confirm("Are you sure to exit the system?"))
    {
		var pre = prefix;
		var url =  pre + '/controller?action=spLogout.do';
		window.document.location.href=url;
    }
}

function entersearch(prefix)
{
	var pre = prefix;
    var url =  pre + '/controller?action=entersearchmsg.do';
    window.document.location.href=url;
}
//用于多笔删除前判断
function multicheck(fieldName)
{
	if (fieldName==null)
	{
		alert('There are no record to be selected.');
		return false;
	}
	if (fieldName.value!=null&&fieldName.checked==false)
	{
		alert('Please select record');
		return false;
	}
	var i = 0;
	if (fieldName.value==null)
	for (var j=0;j<fieldName.length;j++)
	{
		if (fieldName[j].checked)
		i++;
	}
	if (fieldName.value==null&&i==0)
	{
		alert('Please select one record at least.');
		return false;
	}
	return true;
}
//多笔删除
function del(fieldName,formName,actionFieldValue)
{
	if (!multicheck(fieldName))
	{
		return;
	}
	//删除用户域定义消息
	formName.action.value=actionFieldValue;
	if(!confirm('Are you sure to delete it?'))
	{
		return;
	}
	formName.submit();
}

function iframeAutoFit()
{
	try
    {
		if(window!=parent)
        {
			var a = parent.document.getElementsByTagName("IFRAME");
            for(var i=0; i<a.length; i++) //author:meizz
			{
				if(a[i].contentWindow==window)
                {
					var h = document.body.scrollHeight;
                    if(document.all) {h += 4;}
                    if(window.opera) {h += 1;}
                    a[i].style.height = h;
				}
			}
		}
	}
	catch (ex)
    {
		alert("Script cannot operate domain ");
    }
}

//判断手机号码的输入
function checkMDN(fieldName,max)
{
  if(!checkDigital(fieldName))
  {
	  return false;
  }
  if(Trim(fieldName.value).length-max.value>0)
  {
    alert("The max length should be "+max.value+", please input correct phone number!");
    fieldName.focus();
    fieldName.select();
    return false;
  }
  return true;
}

function checkMsgLengthAdd(fieldName,length)
{
  var item=fieldName.value;
  var reallength = item.length;
  var maxlength = length;
  var counter=0;//记录字符串中中文字符的个数

  for (i=0; i<item.length; i++)
  {
    var Pattern = /[ -}]/;

    if(!(Pattern.test(item.charAt(i))) || (item.charCodeAt(i)>255))
    {
      maxlength = maxlength/2;
      break;
    }
  }
  if(reallength>maxlength)
  {
    alert("The length of input is too long.Please input again.");
    fieldName.focus();
    return false;
  }
  return true;
}

// 根据输入的号码MDN，和国家码countrycode判断MDN是否前面已经包含国家码
function checkMdnWithCtryCode(obj , countrycode)
{
	var mdn = obj.value;
	var len = countrycode.length;
	if(mdn.substring(0,len) == countrycode)
	{
		return false;
	}
	return true;
}

/*
 *功能：判断是否输入参数对象是否存在
 *参数：对象本身，比如document.all.inputName
 */
function IsJsObject(obj)
{
	try
	{
		if(obj == '[object]')
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	catch(e)
	{
		//alert(e.message);
		return false;
	}
}
