var idObj,nameObj;

function setValue(id,code,name){
	idObj.value=id;
	nameObj.value=name;
	//alert(id+name);
}

function selectValue(_idObj,_nameObj,url){
	idObj=_idObj;
	nameObj=_nameObj;
	var returnValue;
	//var argumentsArr=new Array(2);
	//showx = event.screenX - event.offsetX - 4 - 10 ; // + deltaX;
	//showy = event.screenY - event.offsetY -16; // + deltaY;
	//window.open(url,'windowName', 'height=460, width=360, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
	returnValue = window.showModalDialog(url,"", "dialogWidth:320px; dialogHeight:460px;center:1; status:0; help:0")
	
	if( returnValue != null ){
		idObj.value = returnValue[0];
		nameObj.value = returnValue[2];
	}
}

function selectCodeNameValue(_codeObj,_nameObj,url){
	idObj=_codeObj;
	nameObj=_nameObj;
	var returnValue;
	//var argumentsArr=new Array(2);
	//showx = event.screenX - event.offsetX - 4 - 10 ; // + deltaX;
	//showy = event.screenY - event.offsetY -16; // + deltaY;
	//window.open(url,'windowName', 'height=460, width=360, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
	returnValue = window.showModalDialog(url,"", "dialogWidth:320px; dialogHeight:460px;center:1; status:0; help:0")
	
	if( returnValue != null ){
		idObj.value = returnValue[1];
		nameObj.value = returnValue[2];
	}
}

function compareDate(beginObj,endObj,caption){
	if(beginObj.value == "" || endObj.value==""){
		return true;	
	}else{
		if(beginObj.value>endObj.value){
			alert(caption);
			endObj.focus();
			return false;
		}
	}
	return true;
}

function isYear(field, caption){
	if(field.value == "")
		return true;
	var pattern=/^\d{4}$/;
	if(!pattern.test(field.value)){
		alert(caption);
		field.focus();
		return false;
	}
	return true; 
}

function submitForm(form, action) {
	form.action = action;
	form.submit();
}

function goURL(url) {
	window.location = url;
}

function highlightTableRows(tableId) {
    var previousClass = null;
    var table = document.getElementById(tableId); 
    var tbody = table.getElementsByTagName("tbody")[0];
    var rows;
    if (tbody == null) {
        rows = table.getElementsByTagName("tr");
    } else {
        rows = tbody.getElementsByTagName("tr");
    }
    // add event handlers so rows light up and are clickable
    for (i=0; i < rows.length; i++) {
    	//clickClass =  rows[i].className+=' click';
        rows[i].onmouseover = function() { previousClass=this.className;this.className+=' over';};
        rows[i].onmouseout = function() { this.className=previousClass;};
        //rows[i].onclick = function() { this.className+=' click';};
        //rows[i].onclick = function() {
            //var cell = this.getElementsByTagName("td")[0];
            //var link = cell.getElementsByTagName("a")[0];
            //location.href = link.getAttribute("href");
            //this.style.cursor="wait";
        //}
    }
}

function highlightFormElements() {
    // add input box highlighting
    addFocusHandlers(document.getElementsByTagName("input"));
    addFocusHandlers(document.getElementsByTagName("textarea"));
}

function addFocusHandlers(elements) {
    for (i=0; i < elements.length; i++) {
        if (elements[i].type != "button" && elements[i].type != "submit" &&
            elements[i].type != "reset" && elements[i].type != "checkbox" && elements[i].type != "radio") {
            if (!elements[i].getAttribute('readonly') && !elements[i].getAttribute('disabled')) {
                elements[i].onfocus=function() {this.style.backgroundColor='#ffd';this.select()};
                elements[i].onmouseover=function() {this.style.backgroundColor='#ffd';};
                elements[i].onblur=function() {this.style.backgroundColor='';}
                elements[i].onmouseout=function() {this.style.backgroundColor='';}
            }
        }
    }
}

// listen form elesment's value is changed or not
function listenFormElements() {
    addValueChangedHandler(document.getElementsByTagName("input"));
    addValueChangedHandler(document.getElementsByTagName("textarea"));
    addValueChangedHandler(document.getElementsByTagName("select"));
}

var changed = false;
function addValueChangedHandler(elements) {
    for (i=0; i < elements.length; i++) {
		elements[i].onchange=function() {changed = true;if(this.onchange) this.onchange=this.onchange}
    }
}

window.onload = function() {
	listenFormElements();
	highlightFormElements();
    var table = document.getElementById("model");
    if(table) 
    	highlightTableRows("model");
}

// Show the document's title on the status bar
window.defaultStatus=document.title;
