var Testing = new Object();
var Testing = {
	_url:'/',
	F:function(){},
	FE:function(){},
	FES:function(){},
	goUrl:function(){},
	changePageLimit:function(){},
	changeFormAction:function(){},
	setCheckBoxValue:function(){},
	resetFormElementValue:function(){},
	resetRadiosValue:function(){},
	fill:function(){},
	checkFormElements:function(){},
	addMsg:function(){},
	removeMsg:function(){},
	addEvent:function(){},
	floatEvent:function(){},
	selectTab:function(){},
	addHoverFormElements:function(){},
	tableColumnHover:function(){},
	fillSelect:function(){},
	addSelectValue:function(){}
}

Testing.F=function(el){
	return document.getElementById(el);
}

Testing.FE=function(el){
	return document.getElementsByName(el); 
}

Testing.FES=function(el){
	return document.getElementsByTagName(el); 
}

/** location goTo a url **/
Testing.goUrl=function(sUrl){
	if ( sUrl == null ) 
		return window.location.href = this._url;
	else return window.location.href = sUrl;
}

Testing.changePageLimit=function(sUrl, obj){
	var value = obj.options[obj.selectedIndex].value;
	if ( sUrl == null ) 
		return window.location.href = this._url;
	else return window.location.href = sUrl+value;
}

/** check checkboxnum and change form action **/
Testing.changeFormAction=function(sAction, formId, checkedNum, msg, seleced) {
	if ( checkedNum != "undefined" && msg != "undefined" ) {
		var where = seleced ? checkedNum != 1 : checkedNum < 1;
		if ( where ) {
			alert(msg);
			return false;
		}
	}
	formId.action = sAction;
	formId.submit();
	return true;
}

Testing.resetFormElementValue=function(oField, oValue) {
	if( oField == null || oValue == null ){
		return false;
 	}
	var oField = this.F(oField);
	if (!oField) return false;
	
	switch ( oField.tagName.toLowerCase() ) {
		case "select":	
			for (var i=0; i< oField.length; i++ ) {
				if ( oField.options[i].value == oValue )
					oField.options[i].selected = true;
			}
			break;
		default:  
			switch ( oField.type ) {
				 case "checkbox":
					if ( oField.value == oValue )
						oField.checked = true;
					
					break;
			}
			break;
	}
}

Testing.resetRadiosValue=function(oField, oValue){
	var oField = this.FE(oField);
	if ( oValue.length == 1 ) {
		for (var i=0; i< oField.length; i++ ) {
		if ( oField[i].value == oValue )
			oField[i].checked = true;
		}
	} else {
		for (var i=0; i< oField.length; i++ ) {
			for (var j=0; j< oValue.length; j++ ) {
				if ( oField[i].value == oValue[j] )
					oField[i].checked = true;
			}
		}
	}
}

Testing.checkFormElements=function(oForm) {
  	var errCount = 0;
    for ( var i=0 ; i < oForm.elements.length; i++) {
     	var oField = oForm.elements[i];
     	var oErrMsg = oField.getAttribute("msg");
	 	var oValidate  = oField.getAttribute("validate");
      	if ( ( oErrMsg != null ) && ( oValidate != "false" ) ) {
	      switch (oField.type) {  	      
	        //checkboxes/radio buttons - only return the value if the control is checked.
	        case "checkbox":
	        case "radio":
		        if (!oField.checked) {
		            errCount += 1;
		        	this.addMsg(oField, oErrMsg);
					this.addEvent(oField, "keyup", function(evt){ Testing.removeMsg(evt) }, false);
					this.addEvent(oField, "blur", function(evt){ Testing.removeMsg(evt) }, false);
		         } //End: if
		        break;  

	        //text/hidden/password all return the value
	        case "text":
	        case "hidden":
	        case "password":
		        if ( oField.value == '' ) {
					errCount += 1;
		        	this.addMsg(oField, oErrMsg);
					this.addEvent(oField, "keyup", function(evt){ Testing.removeMsg(evt) }, false);
					this.addEvent(oField, "blur", function(evt){ Testing.removeMsg(evt) }, false);
		        }
	        break;
	        
	        //file return the value and check the upload file type;
	        case "file":
		    	if ( oField.value == '' ) {
		    		errCount += 1;
		    		this.addMsg(oField, oErrMsg);
					this.addEvent(oField, "focus", function(evt){ Testing.removeMsg(evt) }, false);
					this.addEvent(oField, "blur", function(evt){ Testing.removeMsg(evt) }, false);
		    	} else {
		    		var oFileType = oField.getAttribute("filetype");
		    		if ( oFileType != null ) {
		    			var oFileName = oField.value.substring(oField.value.length - 3, oField.value.length).toLowerCase();
		    			if ( oFileType.indexOf(oFileName) == -1 ) {
		    				errCount += 1;
		    				this.addMsg(oField, oErrMsg);
							this.addEvent(oField, "focus", function(evt){ Testing.removeMsg(evt) }, false);
							this.addEvent(oField, "blur", function(evt){ Testing.removeMsg(evt) }, false);
		    			}
		    		}
		    	}
	    	break;
			
	        //everything else
	        default:      
		        switch(oField.tagName.toLowerCase()) {
		           case "select":
					   if ( oField.selectedIndex <= 0 ) {
							errCount += 1;
							this.addMsg(oField, oErrMsg);
							this.addEvent(oField, "change", function(evt){ Testing.removeMsg(evt) }, false);
							this.addEvent(oField, "blur", function(evt){ Testing.removeMsg(evt) }, false);
					   }
			       break;
				   case "textarea":
					   if ( oField.innerHTML == '' ) {
							errCount += 1;
							this.addMsg(oField, oErrMsg);
							this.addEvent(oField, "keyup", function(evt){ Testing.removeMsg(evt) }, false);
							this.addEvent(oField, "blur", function(evt){ Testing.removeMsg(evt) }, false);
					   }
			       break;
		        }
		     break;
	      }
	   }				        
   	}
   	
	//debugger;
	if ( errCount > 0 ) {
		return false;
	} else {
		return true;
	}
}  

Testing.addMsg=function(oField, oErrMsg) {
	this.F(oField.name+"_msgBox") ? this.F(oField.name+"_msgBox").parentNode.removeChild(this.F(oField.name+"_msgBox")): function(){};
   	var oDiv = document.createElement("span");
	oDiv.style.display = "none";
   	oDiv.className = "errorMsg";
   	oDiv.id = oField.name +"_msgBox";
   	oDiv.innerHTML = oErrMsg;
   	oField.parentNode.appendChild(oDiv);
	$('#'+oDiv.id).fadeIn('normal');
   	oField.className += " onerror";
}

Testing.removeMsg=function(evt) {
	var obj = Testing.floatEvent(evt);
	switch (obj.type) { 
		case "checkbox":
	 	case "radio":
		 	if (!obj.checked) {
		 		this.F(obj.name+"_msgBox").style.display ="";
		 		obj.className += " onerror";
		 		return false;
		 	}
	 		break;
	 	case "text":
	    case "hidden":
	    case "password":
	    	if ( obj.value == '' ) {	    		
	    		this.F(obj.name+"_msgBox").style.display ="";
	    		obj.className += " onerror";
	    		return false;
	    	}
	    	break;
	    case "file":
	    	if ( obj.value == '' ){
	    		this.F(obj.name+"_msgBox").style.display = "";
	    		obj.className += " onerror";
	    		return false;
	    	} else {
	    		var oFileType = obj.getAttribute("filetype");
				if ( oFileType != '' ) {
				   var oFileName = obj.value.substring(obj.value.length - 3, obj.value.length).toLowerCase();
				   if ( oFileType.indexOf(oFileName) == -1 ) {
				    	this.F(obj.name+"_msgBox").style.display ="";
				    	obj.className += " onerror";
				    	return false;
				   }
				}
	    	}
	   		break;
	   	 default:
		 	switch(obj.tagName.toLowerCase()) {
				case "select":
					if ( obj.selectedIndex <= 0 ) {
						this.F(obj.name+"_msgBox").style.display ="";
						obj.className += " onerror";
						return false;
					} 
				break;
				case "textarea":
					if ( obj.innerHTML == '' ) {
						this.F(obj.name+"_msgBox").style.display ="";
						obj.className += " onerror";
						return false;
					}
				break;
			}
	}
   	//this.F(obj.name+"_msgBox").style.display = "none";
	$('#'+obj.name+"_msgBox").fadeOut('normal');
   	if ( this.F(obj.name+"_msgBox").style.display == "none" ) {
   		obj.className = obj.className.replace(/onerror/g, "");
   	}
}

Testing.addEvent=function(el, eventName, callBack, useCapture) {
	if( document.addEventListener)
	{
		el.addEventListener(eventName, callBack, useCapture);
	} else if ( document.attachEvent ) {
		el.attachEvent('on'+ eventName, callBack);
	}
}

Testing.floatEvent=function(Event){
	return 	Event.srcElement ? Event.srcElement : Event.target;
}

Testing.selectTab=function(obj, name) {
	var els = obj.parentNode.getElementsByTagName("li");
	for(i=0;i<els.length;i++)
	{
		if( obj==els[i] )
		{
			els[i].className = "current";
			this.F(name+i).style.display = "block";
		} else {
			els[i].className = "";
			this.F(name+i).style.display = "none";
		}
	}
}

//hack input:hover 
Testing.addHoverFormElements=function(el) {
	var oEls = new Array('input', 'textarea');
	for ( var j=0; j<oEls.length; j++) {
		var sfEls = document.getElementsByTagName(oEls[j]);
		for (var i=0; i<sfEls.length; i++) {
			Testing.addEvent(sfEls[i], 'focus', function (evt) {
				var obj = Testing.floatEvent(evt);
				obj.className += " onfocus";
			}, false);
			Testing.addEvent(sfEls[i], 'blur', function (evt) {
				var obj = Testing.floatEvent(evt);
				obj.className = obj.className.replace(/onfocus/g, "");
			}, false);
		 }
	}
}


Testing.tableColumnHover=function() {
	var tbody = Testing.FES('tbody');
	for ( j=0; j<tbody.length; j++ ){
		if ( tbody[j].className == "listViewTd" ) {
			var tr = tbody[j].getElementsByTagName('tr');
			for ( i=0; i<tr.length; i++ ) {
				Testing.addEvent(tr[i], 'mouseover', function (evt) {
					var obj = Testing.floatEvent(evt);
					obj.parentNode.className += " hover";
				}, false);
				Testing.addEvent(tr[i], 'mouseout', function (evt) {
					var obj = Testing.floatEvent(evt);
					obj.parentNode.className = obj.parentNode.className.replace(new RegExp("( ?|^)hover\\b"), "");
				}, false);
			}
		}
	}
}

Testing.addSelectValue=function(oData, oField, oLength, oRestValue){
	var set = false;
	var obj = oField;
	var list = oData.split("|");
	obj.options.length = oLength ? 1 : 0;
	for ( var i=0; i<list.length; i++) {
		list[i] = list[i].split(",");
		value = list[i][0];
		name = list[i][1];
		if ( value != '' && name != '' ) {
			if ( oRestValue != undefined ) {
				set = oRestValue && value == oRestValue ? true : false;
			}
			obj.options[obj.options.length]= new Option(name,value, false, set);
		}
	}
}

//** select date **//
function setStartDate(oEndObj){
	var oDate = $dp.$(oEndObj);
	WdatePicker({
		onpicked:function(){
			oDate.focus();
		},
		maxDate:'#F{$dp.$D(\''+oEndObj+'\')}'
	});
}

function setEndDate(oStartObj){
	WdatePicker({minDate:'#F{$dp.$D(\''+oStartObj+'\')}'});
}

function setExamDate(){
	WdatePicker({minDate:'%y-%M-%d',maxDate:'#{%y+1}-%M-%d'});
}

function checkAllSelect(obj){
	obj = Testing.F(obj);
	if ( obj.length == 0 ) return false;
	for(var i=0;i<obj.length;i++){ 
		obj.options[i].selected=true;
	}
}

function openWindow(oUrl, oWidth, oHeight){
	window.open(oUrl, '', 'scrollbars=yes, resizable=yes, width='+oWidth+',height='+oHeight);
}

function showDialogWindow(oTitle, oMessage, oWidth, oHeight, oType, oAjaxMessage, callBack) {
	
	if ( oType == 'inner' ) {
		oContent = $('#'+oMessage).html();
		return showModelWindow(oTitle, oContent, oWidth, oHeight, 'dialog', callBack);
	} else if ( oType == 'iframe' ) {
		showModelWindow(oTitle, oAjaxMessage, oWidth, oHeight, 'dialog', callBack);
		oContent= '<iframe name="DialogIFrame"';
		oContent+= 'src="'+oMessage+'" style="width:100%;height:100%;" frameborder="0" marginheight="0" marginwidth="0" >';
		oContent+= '</iframe>';
		return $('#DialogContent').html(oContent).css('padding', '0px');
	} else if ( oType == 'ajax' ) {
		showModelWindow(oTitle, oAjaxMessage, oWidth, oHeight, 'alert', callBack);
		$.ajax({ 
			type: "GET",
            url: oMessage, 
            cache: false,
            success: function(data){
               // unblock when remote call returns
               $('#DialogContent').html(data);
            }
        });
        return;
	}
}


function showModelWindow(oTitle, oMessage, oWidth, oHeight, oType, callBack) {
		
	$.blockUI.defaults.overlayCSS.backgroundColor="#999";
	$.blockUI.defaults.pageMessageCSS.border="none";
	//$.blockUI.defaults.pageMessageCSS.margin="-"+oHeight/2+"px 0 0 -"+oWidth/2+"px";
	$.blockUI.defaults.overlayCSS.cursor='normal';
	
	var dhtml="";
	var cHeight=0;
    dhtml+="<div class='DialogContainer' id='dialogBox'>";
    dhtml+="    <div class='DialogTitle'><span class='DialogCloese' id='btn_close'><a href='javascript:void(0)'>×</a>";
    dhtml+="    </span>"+oTitle+"</div>";
    dhtml+="    <div class='DialogContent' id='DialogContent' style='height:"+oHeight+"px'>"+oMessage+"</div>";
	if ( oType == 'confirm' || oType == 'alert' ) {
		dhtml+="	<div class='DialogButtons'>";
		dhtml+="   		<input type='button' class='button' value='确 定' id='btn_ok'/>";
		if ( oType == 'confirm' ){
			dhtml+="   		<input type='button' class='button' value='取 消' id='btn_cancel'/>";
		}
		dhtml+="	</div>";
	} 
	dhtml+="</div>";
	
	cHeight = ( oType == 'confirm' || oType == 'alert' ) ? 105 : 35;
	
	$.blockUI(dhtml, { 
			top: ''+($(window).height() - oHeight - cHeight ) /2 + 'px',
			left: ''+($(window).width() - oWidth) /2 + 'px',
			width: ''+oWidth+'px',
			margin: '0px'
		}
	);
	
	if ( oType == 'confirm' || oType == 'alert' ) { 
		$("#btn_ok").click(function(){
			$.unblockUI();
	       	if ( $.isFunction(callBack) ) {
	       		setTimeout(function(){
	       			$(callBack);
	       		}, 500);
	    	}
	    });
	    if ( oType == 'confirm' ){
		    $("#btn_cancel").click(function(){
		        $.unblockUI();
			    if ( $.isFunction(callBack) ) {
		       		setTimeout(function(){
		       		$(callBack);
		       		}, 500);
		    	}
		    });
		}
	}
	
    $("#btn_close").click(function(){
        $.unblockUI();
        if ( $.isFunction(callBack) ) {
	       	setTimeout(function(){
	       		$(callBack);
	       	}, 500);
	    }
    });
}

/** js visldate **/
function isEmail(strEmail) {
	if ( strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1 )
		return true;
	else
		return false;
}

function isIdCard(strIdCard) {
	if ( strIdCard.search(/^[1-9]([0-9]{14}|[0-9]{17})$/) != -1 ) 
		return true;
	else
		return false;
}

function isTrim(str) {
	var m = str.match(/^\s*(\S+(\s+\S+)*)\s*$/);
	return (m == null) ? "" : m[1];
} 

function splitArray(array){
	for ( var i=0; i<array.length; i++ ) {
		var arr = array[i];
		array[i] = null;
		var str = array.join('|');
		if ( str.indexOf(arr) > -1 ) {
			return false
		}
	}
	return true;
}
function showHelpDiv(oTitle, oHtml, oWidth, oHeight, OType) {
	if ( OType ) {
		html = oHtml;
	} else {
		html = $('#'+oHtml).html();
	}
	showModelWindow(oTitle, html, oWidth, oHeight, 'alert', '');
	$("#btn_ok").val('关闭');
}

function showTipMsg(obj){
	$('.inputmsg').fadeOut('normal');
	$('#'+obj.name+'Msg').fadeIn('normal');
}
function hideTipMsg(obj){
	setTimeout(function(){$('#'+obj.name+'Msg').fadeOut('normal');}, 500);
}