// JavaScript Document
//数据表格事件初始化 Kony_Han@hotmail.com
function initDataTableEven(doc){
	  	var tr_css01 = "DEF_TR";		//基本式样
		var tr_css04 = "DEF_TR_C";        //变化式样
		var isturn=false;
	  	var ARR_OBJ = doc.all.tags("TR");
		for(i=0;i<ARR_OBJ.length;i++){
				var temp_comp = ARR_OBJ[i];
				if(temp_comp.className==tr_css01){
					  if(isturn=!isturn){
							temp_comp.className=tr_css04;
					  }
				}
		}
}
function clang(code){
	var loc = location+"";
	var pub_h = "";
	if(loc.indexOf("?")>-1){
		if(loc.indexOf("lang=")>-1){
			loc=loc.substring(0,loc.indexOf("lang="));
			location.href(loc+"lang="+code);
		}else{
			location.href(loc+"&lang="+code);
		}
	}else{
		location.href(loc+"?lang="+code);
	}
}
//恢复JBO数据
function initDefValue(doc){
	var tabs =doc.all.tags("TABLE");
	for(i=0;i<doc.forms.length;i++) {
		  doc.forms[i].doc=doc;
		  restoryAllDate(doc.forms[i]);
	}
}

function restoryAllDate(tf)
{
	if(tf!=null&&tf.tagName=='FORM')
	{
		  for(var i=0;i<tf.length;i++)
		  {
			  var temp_boolean=true;
			  var temp_comp=tf.elements[i];
			  var perElememt = tf.elements[i-1];
			  var temp_type= temp_comp.type;
			  if(temp_type=='reset'){temp_comp.value="Clear"; }
			  if( temp_comp.defvalue!=null&&temp_comp.defvalue!=''&&temp_comp.defvalue!='null')
			  {
				 if(temp_type=='file' || temp_type=='hidden' || temp_type=='text' ||temp_type=='textarea'|| temp_type=='password')
				 {
					temp_comp.value = temp_comp.defvalue;
				 }
				 if(temp_type=='select-one')
				 {
                   for(var j=0;j<temp_comp.length;j++)
 				     {
   					     if(trim(temp_comp.options[j].value)==temp_comp.defvalue)
                        {
								temp_comp.selectedIndex=j;
								break;
   	 				    }
  				    }
					if(temp_comp.needchange!=null && temp_comp.onchange!=null) temp_comp.onchange();
				 }
				 if(temp_type=='checkbox')
				 {
                   	if(temp_comp.value == temp_comp.defvalue)
                 	{
                       temp_comp.checked=true;
                    }
				 }
                 if(temp_type=='radio')
				 {
                 	if(temp_comp.value == temp_comp.defvalue)
                 	{
                       temp_comp.checked=true;
                    }
				 }
			  }
              if(temp_type=='file' || temp_type=='hidden' || temp_type=='text' ||temp_type=='textarea'|| temp_type=='password'){
				if(temp_comp.isfloat!= null||temp_comp.isFloat!= null){
					temp_comp.onkeypress = function (e){
						onlyDigit();
					};
					temp_comp.onchange = function (e){
						if (!e) e = event;
						var t = e.target ? e.target : e.srcElement;
						t.value=formatNum(t.value,1);
						};
					}
				}
              if(temp_type=='checkbox'){
                if(temp_comp.aid!=null)
                {
                	temp_comp.onclick = function ()
                	{
                      checkallbox(this);
	             	};
                }
                if(temp_comp.sid!=null)
                {
                	temp_comp.onclick = function ()
                	{
                      checkparentbox(this);
	             	};
      		}
      	}
	  }
	  if(tf.onsubmit==null&&(tf.novdata==null)){
			tf.onsubmit=function(){return vdata(this);};
	  }
  }
}
function vdata(tf){
	  return verifyData(tf);
}
function verifyData(tf){
	  if(tf!=null&&tf.tagName=='FORM')
	  {
		 for(var i=0;i<tf.length;i++)
		  {
			  var temp_boolean=true;
			  var temp_comp=tf.elements[i];
			  var temp_type= temp_comp.type;
			  if(temp_comp!=null&&temp_comp.msg!=null)
			  {
                if(temp_type=='textarea'||temp_type=='text' || temp_type=='file'||temp_type=='password')
                 {
                   temp_boolean=temp_boolean && trim(temp_comp.value)!='';
                   //判断是否有isemail属性
                   if(temp_boolean && temp_comp.isemail!=null){
                     		temp_boolean = temp_boolean && ValidateEmail(temp_comp.value);
                   }
				   //判断是否有isdigit属性
                   if(temp_boolean && temp_comp.isdigit!=null){
							temp_boolean = temp_boolean && parseInt(temp_comp.value);
                   }
					//判断是否有isfloat属性
                   if(temp_boolean && temp_comp.isfloat!=null){
							temp_boolean = temp_boolean && (parseFloat(temp_comp.value)>-1);
							if(temp_comp.nozero!=null){
								  temp_boolean = temp_boolean && (parseFloat(temp_comp.value)>0);
						}
                   }
				 }
				 if(temp_type=='select-one')
				 {
					temp_boolean=temp_boolean && temp_comp.value!='0' && temp_comp.value!='';
				 }
                 if(temp_type=='checkbox'){
					temp_boolean=temp_boolean && temp_comp.checked;
                 }
				 if(!temp_boolean)
				 {
					showmsg(temp_comp,tf.doc);
					setErrCssForElement(temp_comp);
					return false;
				 }
			  }
		  }
//		  for(var i=0;i<tf.tags("INPUT").length;i++){
//				var but = tf.tags("INPUT")[i];
//				if(but.type=="submit"){	but.disabled=true;}
//		  }
	}
	return true;
}

function showmsg(temp_comp,doc){
	if(temp_comp.msg==''){
		alert(temp_comp.parentNode.parentNode.firstChild.outerText);
	}else{
		alert(temp_comp.msg);
	}
}
function setErrCssForElement(temp_comp){
    var temp_type = temp_comp.type;
	if(temp_type=='textarea'||temp_type=='text' || temp_type=='file'||temp_type=='password'){
		temp_comp.style.background = "URL(images/act.gif)";
	}
	if(temp_type=='button') temp_comp.className="";
	try {temp_comp.focus();} catch(e){};
}

function trim(s)
{
    while (s.substring(0,1) == ' ') s = s.substring(1);
    while (s.substring(s.length - 1, s.length) == ' ') s = s.substring(0, s.length-1);
    return s;
}
function initAll(){
	initDefValue(this.document);
	initDataTableEven(this.document);
}
function ValidateEmail(str)
{
var ret = false;
	if (typeof(str) != "undefined")
	{
		if (/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/.test(str))	ret = true;
	}
	return ret;
}
window.attachEvent('onload',initAll);
