function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v5.0 // ybh modify
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { 
	  // nm=val.name; 
	  nm=args[i+1];  // tips
	  if ((val=val.value)!="") { // 值不为空
		  //alert(test);
		  if (test.indexOf('isEmail')!=-1) { 
		    // p=val.indexOf('@');
			//if (p<1 || p==(val.length-1)) errors+='- '+nm+' 必须填写正确的E-Mail.\n';
			if (!chkemail(val)){
				errors+='- '+nm+' 必须填写正确的E-Mail.\n';
				break;
			}
		  } else if (test.indexOf('isDate')!=-1) {
			 if (!checkIsValidDate(val)){ // ybh
				 errors+='- '+nm+' 必须正确的日期.\n';
				 break;
			 }
		  } else if (test.indexOf('isLength')!=-1) {
			  len = val.length ;
			  p=test.indexOf(':');
			  min=test.substring(9,p);
			  max=test.substring(p+1);
			  if (len<min || max<len){
				  errors+='- '+nm+' 中输入的字符请在 '+min+' ～ '+max+' 个字符之内.\n';
				  break;
			  }
		  } else if (test!='R') { // 数字
		    num = parseFloat(val);
			if (isNaN(val)) errors+='- '+nm+' 必须填写数字.\n';
			if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
			  min=test.substring(8,p); max=test.substring(p+1);
			  if (num<min || max<num){
				  errors+='- '+nm+' 填写的数字必须在范围 '+min+' ～ '+max+' 之内.\n';
				  break;
			  }
			} 
		  }
	  } else{ // 值为空
		  if (test.charAt(0) == 'R'){
			  errors += '- '+nm+' 必须填写.\n'; 
			  break;
		  }
	  }
	}
  } 
  //
  if (errors) alert('错误提示：\n你提交的信息的有误，请校对:\n'+errors);
  document.MM_returnValue = (errors == '');
}
function checkIsValidDate(str)
{
    //如果为空，则通过校验
    if(str == "")
        return true;
    var pattern = /^((\d{4})|(\d{2}))-(\d{1,2})-(\d{1,2})$/g;
    if(!pattern.test(str))
        return false;
    var arrDate = str.split("-");
    if(parseInt(arrDate[0],10) < 100)
        arrDate[0] = 2000 + parseInt(arrDate[0],10) + "";
    var date =  new Date(arrDate[0],(parseInt(arrDate[1],10) -1)+"",arrDate[2]);
    if(date.getYear() == arrDate[0]
       && date.getMonth() == (parseInt(arrDate[1],10) -1)+""
       && date.getDate() == arrDate[2])
        return true;
    else
        return false;
}
function checknull(val){
	if (val == null || val == "") {
	   return true ;
	}
	return false;
}
//函数名：chkemail
//功能介绍：检查是否为Email Address
//参数说明：要检查的字符串
//返回值：0：不是  1：是 

function chkemail(a)
{ var i=a.length;
 var temp = a.indexOf('@');
 var tempd = a.indexOf('.');
 if (temp > 1) {
  if ((i-temp) > 3){
    if ((i-tempd)>0){
     return true;
    }
  }
 }
 return false;
}
//
function chkDateTime(str)
{
	 var r = str.match(/^(\d{4})-(\d{2})-(\d{2})$/);  
	 if(r==null)return false; 
	 var d= new Date(r[1], r[2]-1, r[3]); 
	 //r[2] = r[2].replace(/^0/,"");
	 return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[2]&&d.getDate()==r[3]);
}
function chkint(str)
{
	var r= str.match(/^\s*[-\+]?\d+\s*$/) ;
	if(r==null){
		return false;
	}else{
		return true;
	}
}
function chkfloat(str)
{
	var r= str.match(/^\s*[-\+]?(\d+)?(.)?(\d+)?\s*$/) ;
	if(r==null){
		return false;
	}else{
		return true;
	}
}

function Err(msg)
{
	alert(msg);
}
function SelectChk(Tval) //這個Function是給asp.net的checkBox用的   
{   
	 var icount = 0 ;
	 var ctrl = document.getElementById(Tval);   
	 var col = ctrl.all   
	 for (i = 0; i < col.length; i++)   
	 {   
		 if (col[i].tagName.toUpperCase() == 'INPUT')   
		 {   
			 if (col[i].checked){
				icount++ ;
			 }
		 }   
	 }  
	 return icount;   
}   
////////////////////////////////////////
var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
//extra height in px to add to iframe in FireFox 1.0+ browsers
var FFextraHeight=getFFVersion>=0.1? 16 : 0 

function dyniframesize(iframename,h) {
  var pTar = null;
  if (h==null)
  {
	  h = 0;
  }
  if (document.getElementById){
    pTar = document.getElementById(iframename);
  }
  else{
    eval('pTar = ' + iframename + ';');
  }
  
  if (pTar && !window.opera){ // parent
    //begin resizing iframe
    //pTar.style.display="block"
    if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight){
      //ns6 syntax
      pTar.height = pTar.contentDocument.body.offsetHeight+FFextraHeight; 
    }else if (pTar.document && pTar.document.body.scrollHeight ){ //  
      //ie5+ syntax
      pTar.height = pTar.document.body.scrollHeight-h;
    }
  }
}
function setStyleDefault(tipid)
{
	//Err("setStyleDefault ok")
	try {
		document.getElementById(tipid).className = "css_tr_gray1";
	} catch(e) {}
}

function setStyleFocus(tipid)
{
	//Err("setStyleFocus ok")
	try {
		document.getElementById(tipid).className = "focus";
	} catch(e) {}
}
function setTdblur(tipid)
{
	try {
		var obj =_(tipid) ;
		var thisTd = obj.parentNode;
		thisTd.className = "rtTip";
	} catch(e) {}
}

function setTdFocus(tipid)
{
	try {
		var obj =_(tipid) ;
		var thisTd = obj.parentNode;
		thisTd.className = "tiptd";
	} catch(e) {}
}

function setval(obj,iMsg) {
	if (typeof obj == "string") {
		obj = _(obj);
	}
	obj.innerHTML = iMsg
}
function _(id) {
	return document.getElementById(id);
}
String.prototype.trim = function(){
	return this.replace(/(^[\s]*)|([\s]*$)/g, "");
}
function btcontroll(flag) {
	_("btsubmit").disabled = flag;
	_("btreset").disabled = flag;
}

function btndisplay(btnname,flag) {
	_(btnname).disabled = flag;
}
//显示提示
function showTip(obj,isShow){
	var thisTr = obj.parentNode.parentNode
	//Err(thisTr.tagName);
	var thisTd = obj.parentNode.parentNode.getElementsByTagName("td")[2]
	//Err(thisTd.tagName);
	var thisTip = thisTd.getElementsByTagName("div")[0]
	//Err(thisTip.tagName);
	if(isShow==1){
		thisTd.className="tiptd"
		thisTip.style.display="block"
	}else{
		//if(thisTr.className!="atn"){thisTip.style.display="none"}
	}
}
//显示错误
function ShowAtn(obj,iMsg,status){
	if (typeof obj == "string") {
		obj = _(obj);
	}
	var thisTd = obj.parentNode
	var thisTr = obj.parentNode.parentNode
	
	if(status==1){
		thisTd.className="tiptdc"
		obj.style.display="block"
		obj.innerHTML = iMsg
		thisTr.className="atn"
	}else{
		thisTd.className="rtTip"
		thisTr.className=" "
		obj.innerHTML = iMsg
		//obj.style.display="none"
	}
}
function createXmlHttp() 
{
	var xmlhttp = null;
	// branch for native XMLHttpRequest object
    if(window.XMLHttpRequest)
	{
    	try
		{
			xmlhttp = new XMLHttpRequest();
	    }
		catch(e)
		{
			xmlhttp = null;
	    }
	}
	// branch for IE/Windows ActiveX version
	else if(window.ActiveXObject)
	{
       	try
		{
        		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	     }
		catch(e)
		{
        	try
			{
          			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	       	}
			catch(e)
			{
          			xmlhttp = null;
	       	}
		}
    }
	return xmlhttp;
}

//
function GetCookie(n){
    var nameEQ = n + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function SetCookie(n,v,d){
    var e = "";
    if (d) {
        var date = new Date();
        date.setTime(date.getTime()+(d*24*60*60*1000));
        e = "; expires="+date.toGMTString();
    }
    document.cookie = n+"="+v+e+"; path=/";
}
