var hasFocus = false;

Number.prototype.NaN0=function(){return isNaN(this)?0:this;}

function getNumber(str) {
	var num = str;
    if (num.charAt(0) == '0') num = num.substring(1);
    return parseInt(num);
}

function getTime(scheduleTime) {
	var startHour = getNumber(scheduleTime.substring(0,2));
	var startMinute = getNumber(scheduleTime.substring(3,5));
	var endHour = getNumber(scheduleTime.substring(8,11));
	var endMinute = getNumber(scheduleTime.substring(11,13));
	return {startHour: startHour, startMinute: startMinute, endHour:endHour, endMinute:endMinute};
}

function setSelectedOptions(opt,selectedValue) {
	for (var i=0,len=opt.options.length;i<len;i++) {
		if (opt.options[i].value == selectedValue) {
			opt.selectedIndex = i;
			break;
		}
	}
}

function setSelectedOptionsByText(opt,selectedText) {
	for (var i=0,len=opt.options.length;i<len;i++) {
		if (opt.options[i].text == selectedText) {
			opt.selectedIndex = i;
			break;
		}
	}
}

function checkTextAreaLength(textarea,maxLength) {
    if (!textarea) return true;
    if (!textarea.value.length < maxLength) {
    	textarea.value = textarea.value.substring(0, maxLength);
    	return false;
    } else {
    	return true;
    }
}

function getPosition(e){
    var left = 0;
    var top  = 0;
    while (e.offsetParent){
        left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
        top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
        e = e.offsetParent;
    }
    left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
    top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);

    return {x:left, y:top};
}

function getWindowPosition(elementWidth,elementHeight) {
    var windowWidth = window.innerWidth ||  document.body.offsetWidth;
    var windowHeight = window.outerHeight ||  document.body.offsetHeight;
	var left = ((windowWidth-elementWidth)/2);
	var top = ((windowHeight-elementHeight)/2) - 50;
    return {x:left, y:top};
}

function getWindowCenterPosition(elementWidth,elementHeight) {
    var windowWidth = window.innerWidth ||  document.body.offsetWidth;
    var windowHeight = window.outerHeight ||  document.body.offsetHeight;
	var left = ((windowWidth-elementWidth)/2);
	var top = ((windowHeight-elementHeight)/2);
    return {x:left, y:top};
}

function openWindow(url,name,winWidth,winHeight) {
	var pos = getWindowCenterPosition(winWidth,winHeight);
	var top = pos.y;
	var left = pos.x;
	var openedWin = window.open(url,name,"width="+ winWidth + ",height=" + winHeight +",left=" + left +",top=" + top +",scrollbars=yes,titlebar=no,menubar=no");
	return openedWin;
}

function toggleDiv(divName) {
    var div = document.getElementById(divName);
    if (div.style.display == 'none') Util.scrollOut (div);
    else Util.scrollIn(div);
    return;
}
    
function appInit() {
	setInputHandler("input");
	setInputHandler("textarea");
	setInputHandler("select");
}

function setInputHandler(type) {
	var elements, e;
  	var origOnFocus, origOnBlur, origOnMouseOver, origOnMouseOut;
  	elements = document.getElementsByTagName(type);
  
  	for (var i=0, len=elements.length; i<len; i++) {
    	e = elements[i];    
    	if (!(e.type == "text" || e.type == "password" 
          || e.type == "textarea" || e.type == "select-one"
          || e.type == "button" || e.type == "submit" || e.type == "reset" )) continue;
          
    	if (e.type == "button" || e.type == "submit" || e.type == "reset") {
      		if (e.onmouseover) e.origOnMouseOver = e.onmouseover;
      			e.onmouseover = function() {
        		this.className = "btn bluebtn-hover";
        		if (this.origOnMouseOver) this.origOnMouseOver();
      		}
                
      		if (e.onmouseout) e.origOnMouseOut = e.onmouseout;
      			e.onmouseout = function() {
        		this.className = "bluebtn";
        		if (this.origOnMouseOut) this.origOnMouseOut();
      		}
    	} else {
      		if (e.onfocus) e.origOnFocus = e.onfocus;
      			e.onfocus = function() {
        		this.className = "input_focus";
        		if (this.origOnFocus) this.origOnFocus();
      	}
                
      	if (e.onblur) e.origOnBlur = e.onblur;
      		e.onblur = function() {
        		this.className = "input_blur";
        		if (this.origOnBlur) this.origOnBlur();
      		}
    	}
    	if (!hasFocus && e.type == "text") {
      		e.focus();
      		hasFocus = true;
    	}
  	} 
}

function Util() {}

Util.isString = function(val) {
    if (val == null) return false;
    return ((typeof(val) == "string") || (val instanceof String));
}

Util.findObjHeight = function(obj) {
    var height = 0;
    if (obj.offsetHeight) {
        height = obj.offsetHeight;
    }
    else if (obj.document && obj.document.height) {
        height = obj.document.height;
    }
    
    return height;
}

Util.scrollInID = new Object();
Util.scrollInCB = new Object();  // callback method
Util.scrollIn = function(obj, cb) {
    if (Util.isString(obj)) obj = document.getElementById(obj);  // id
    if (!obj) return;
    var id = obj.id;
    
    // disable effect in IE
    if (Util.isIE) {
        obj.style.height = "0px";
        obj.style.display = "none";
        obj.style.visibility = "hidden";
        if (cb) cb();
        return;
    }
    
    if (Util.scrollInID[id]) Util.clearScrollIn(id);
    
    // calculate scroll step
    var objHeight = Util.findObjHeight(obj);
    var step = parseInt(objHeight / 4);
    obj.style.overflow = "hidden";
    obj.style.display = "block";
    obj.style.visibility = "visible";
    
    if (cb) Util.scrollInCB[id] = cb;
    Util.scrollInEffect(id, objHeight, step);
}

Util.scrollInEffect = function(id, currHeight, step) {
    var div = document.getElementById(id);
    if (!div) return;
    
    currHeight = parseInt(currHeight) - step;
    if (currHeight > 0) {
        div.style.height = currHeight + "px";
        Util.scrollInID[id] = window.setTimeout("Util.scrollInEffect(\"" + id + "\", " + currHeight + ", " + step + ")", 40);
    }
    else {
        Util.clearScrollIn(id);
    }
}

Util.clearScrollIn = function(id) {
    var obj = document.getElementById(id);
    if (obj) {
        obj.style.height = "0px";
        obj.style.display = "none";
        obj.style.visibility = "hidden";
    }
    
    if (Util.scrollInID[id]) {
        window.clearTimeout(Util.scrollInID[id]);
        delete Util.scrollInID[id];
    }
    
    if (Util.scrollInCB[id]) {
        Util.scrollInCB[id]();
        delete Util.scrollInCB[id];
    }
}

Util.scrollOutID = new Object();
Util.scrollOutCB = new Object();  // callback method
Util.scrollOut = function(obj, cb) {
    if (Util.isString(obj)) obj = document.getElementById(obj);  // id
    if (!obj) return;
    var id = obj.id;
    
    // disable effect in IE
    if (Util.isIE) {
        obj.style.height = null;
        obj.style.overflow = "visible";
        obj.style.display = "block";
        obj.style.visibility = "visible";
        if (cb) cb();
        return;
    }
    
    if (Util.scrollOutID[id]) Util.clearScrollOut(id);
    
    // calculate the object height
    var clone = obj.cloneNode(true);
    clone.style.position = "absolute";
    clone.style.visibility = "hidden";
    clone.style.display = "block";
    clone.style.height = null;
    clone.style.overflow = "visible";
    obj.parentNode.insertBefore(clone, obj);
    var objHeight = Util.findObjHeight(clone);
    obj.parentNode.removeChild(clone);
    
    // calculate scroll step
    var step = parseInt(objHeight / 4);
    
    obj.style.height = "0px";
    obj.style.visibility = "visible";
    obj.style.display = "block";
    obj.style.overflow = "hidden";
    
    if (cb) Util.scrollOutCB[id] = cb;
    Util.scrollOutEffect(id, 0, objHeight, step);
}

Util.scrollOutEffect = function(id, currHeight, targetHeight, step) {
    var div = document.getElementById(id);
    if (!div) return;
    
    currHeight = parseInt(currHeight) + step;
    if (currHeight < targetHeight) {
        div.style.height = currHeight + "px";
        Util.scrollOutID[id] = window.setTimeout("Util.scrollOutEffect(\"" + id + "\", " + currHeight + ", " + targetHeight + ", " + step + ")", 40);
    }
    else {
        Util.clearScrollOut(id);
    }
}

Util.clearScrollOut = function(id) {
    var obj = document.getElementById(id);
    if (obj) {
        obj.style.height = null;
        obj.style.overflow = "visible";
        obj.style.display = "block";
        obj.style.visibility = "visible";
    }
    
    if (Util.scrollOutID[id]) {
        window.clearTimeout(Util.scrollOutID[id]);
        delete Util.scrollOutID[id];
    }
    
    if (Util.scrollOutCB[id]) {
        Util.scrollOutCB[id]();
        delete Util.scrollOutCB[id];
    }
}

Util.updateInnerHTMLWithScrollEffect = function(obj, innerHTML) {
    if (Util.isString(obj)) obj = document.getElementById(obj);  // id
    if (!obj) return;
    
    if (Util.isIE) {  // disable effect in IE
        obj.innerHTML = "";
        obj.innerHTML = innerHTML;
        return;
    }
    
    var cb = function() {
        obj.innerHTML = "";
        obj.innerHTML = innerHTML;
        window.setTimeout("Util.scrollOut(\"" + obj.id + "\")", 100);
    };
    
    Util.scrollIn(obj, cb);
}

/**
 * This method gets ABSOLUTE centered X position of an element in the current viewed document.
 */
Util.findAbsoluteCenterPosX = function(obj)
{
    if (!obj) return;
    return (Util.findRelativeCenterPosX(obj) + Util.getDocumentScrollLeft());
}

/**
 * This method gets ABSOLUTE centered Y position of an element in the current viewed document.
 */
Util.findAbsoluteCenterPosY = function(obj)
{
    if (!obj) return;
    return (Util.findRelativeCenterPosY(obj) + Util.getDocumentScrollTop());
}

/**
 * This method gets RELATIVE centered X position of an element in the current viewed document.
 */
Util.findRelativeCenterPosX = function(obj)
{
    if (!obj) return;
    var docWidth = Util.getViewportWidth();
    var objWidth = Util.findObjWidth(obj);
    return Math.round((docWidth - objWidth) / 2);
}

/**
 * This method gets RELATIVE centered Y position of an element in the current viewed document.
 */
Util.findRelativeCenterPosY = function(obj)
{
    if (!obj) return;
    var docHeight = Util.getViewportHeight();
    var objHeight = Util.findObjHeight(obj);
    return Math.round((docHeight - objHeight) / 2);
}

Util.getViewportWidth = function()
{
    if (window.innerWidth) {
        var innerWidth = window.innerWidth;
        var clientWidth = document.body.clientWidth;
        return (clientWidth && (clientWidth < innerWidth))? clientWidth : innerWidth;
    }
    
    return document.body.clientWidth;
}

Util.getViewportHeight = function()
{
    if (window.innerHeight) {
        var innerHeight = window.innerHeight;
        var clientHeight = document.body.clientHeight;
        return (clientHeight && (clientHeight < innerHeight))? clientHeight : innerHeight;
    }
    
    return document.body.clientHeight;
}

Util.findObjWidth = function(obj)
{
    var width = 0;
    if (obj.offsetWidth) {
        width = obj.offsetWidth;
    }
    else if (obj.document && obj.document.width) {
        width = obj.document.width;
    }
    
    return width;
}

Util.findObjHeight = function(obj)
{
    var height = 0;
    if (obj.offsetHeight) {
        height = obj.offsetHeight;
    }
    else if (obj.document && obj.document.height) {
        height = obj.document.height;
    }
    
    return height;
}

Util.setPosition = function(obj, x, y)
{
    if (!obj) return;
    obj.style.left = x + "px";
    obj.style.top = y + "px";
}

Util.getEventTarget = function(e)
{
    var target;
    if (!e) var e = window.event;
    if (e.target) target = e.target;
    else if (e.srcElement) target = e.srcElement;
    
    return target;
}
