//
// Default Javascript Functions
//
// Digitalsesame Inc.
// Author: Toad Xu <toad@toad.com.tw>
//

// default

//window.defaultStatus = "芖布沮ユ传┮ - 布獺琩高╰参";

// Cross-Browser

var browser = (((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) >= 3 )) || ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4 )));
var ie4 = ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4 ));
var ns4 = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) >= 4 ));
var mac = (navigator.appVersion.indexOf("Macintosh") != -1);

var rightMargin;

if (ie4){
    layerRef = "document.all";
    styleRef = ".style";
    // imageRef="document.images";
    // rightMargin = document.body.offsetWidth; // only work for scripts in HTML source
} else {
    layerRef = "document.layers";
    styleRef = "";
    // imageRef="document.layers[\"menu\"].document.images";
    // rightMargin = innerWidth;
}

// Windows

function popwin(popFile,popTarget,popPara){
    popTarget = (popTarget) ? popTarget : "new";
    popPara = "status=no,toolbar=no,menubar=no,resizable=yes,width=640,height=400,scrollbars=yes,"+popPara ;
	remote = window.open(popFile,popTarget,popPara);
	// remote.location.href = popFile;
	if (remote.opener == null) remote.opener = window;
}

// Menus

// Date & Time

function lastModified(){ // Returns date this page was last modified.
   var modiDate=new Date(document.lastModified);
   var showAs = modiDate.getYear()+"/"+modiDate.getMonth()+1+"/"+modiDate.getDate();
   return showAs;
}

function shownowdate(){
    theDate = new Date();
    theYear = (ns4) ? (theDate.getYear()-11) : (theDate.getYear()-1911);
    document.write("チ瓣"+theYear+""+(theDate.getMonth()+1)+"る"+theDate.getDate()+"ら");
}

// text & message

function timeMsg() { // short-time message
  var msgText = (arguments[0]) ? arguments[0] : " ";
  var msgTime = (arguments[1]) ? parseInt(arguments[1]) : 3000;
  errorMsg.innerHTML = msgText + "&nbsp;" ;
  window.status = msgText;
  setTimeout("timeMsg()",msgTime,"");
}

// Balloon Help

statusBalloon = 0;

function helpBalloon(){
    if (ie4){
        document.all.helpballoon.style.visibility = statusBalloon ? "hidden" : "visible";
    }else{
        document.layers.helpballoon.visibility = statusBalloon ? "hide" : "show";
    }
    statusBalloon = Math.abs(statusBalloon-1);
}

// specialized functions

pointer1 = new Image();
pointer1.src = "html/images/pointer1.gif";
pointer2 = new Image();
pointer2.src = "html/images/pointer2.gif";

function movePt(x,y,v){
    document.all.pointer1.style.left = x;
    document.all.pointer1.style.top = y;
    document.all.pointer2.style.left = x + 148;
    document.all.pointer2.style.top = y;
    document.all.pointer1.style.visibility = v ? "visible" : "hidden";
    document.all.pointer2.style.visibility = v ? "visible" : "hidden";
}

// Cookies

function setCookie(cookieName, cookieValue, expiry){
    var expDate = new Date();
    if(expiry){
        expDate.setTime (expDate.getTime() + expiry);
        document.cookie = cookieName + "=" + escape (cookieValue) + "; expires=" + expDate.toGMTString();
    }else{
        document.cookie = cookieName + "=" + escape (cookieValue);
	}
}

function getCookie(name) {
    var dc = document.cookie;
    var cname = name + "=";
    var clen = dc.length;
    var cbegin = 0;
    while (cbegin < clen) { 
        var vbegin = cbegin + cname.length;
        if (dc.substring(cbegin, vbegin) == cname) { 
            var vend = dc.indexOf (";", vbegin);
            if (vend == -1) vend = clen;
            return unescape(dc.substring(vbegin, vend));
        }
        cbegin = dc.indexOf(" ", cbegin) + 1;
        if (cbegin== 0) break;
    }
    return null;
}
//start
addEvent(window, "load", makeTreesC);

function makeTreesC() {

    if (!document.createElement) return;
    
    uls = document.getElementsByTagName("ul");
    for (uli=0;uli<uls.length;uli++) {
        ul = uls[uli];
        if (ul.nodeName == "UL" && ul.className == "newTree") {
            processULELC(ul);
        }
    }
}

function processULELC(ul) {
    if (!ul.childNodes || ul.childNodes.length == 0) return;
    for (var itemi=0;itemi<ul.childNodes.length;itemi++) {
        var item = ul.childNodes[itemi];
        if (item.nodeName == "LI") {
            var a;
            var subul;
	    subul = "";
            for (var sitemi=0;sitemi<item.childNodes.length;sitemi++) {
                var sitem = item.childNodes[sitemi];
                switch (sitem.nodeName) {
                    case "A": a = sitem; break;
                    case "UL": subul = sitem; 
                               processULELC(subul);
                               break;
                }
            }
            if (subul) {
                associateELC(a,subul);
            } else {
                a.parentNode.className = "Treeb";
            }
        }
    }
}

function associateELC(a,ul) {
    if (a.parentNode.className.indexOf('TreeO') == -1)
      a.parentNode.className = 'TreeC';
    a.onclick = function () {
        this.parentNode.className = (this.parentNode.className=='TreeO') ? "TreeC" : "TreeO";
        return false;
    }
}



function addEvent(obj, evType, fn){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
  } else if (obj.attachEvent){
	var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
	return false;
  }
}
// END
// field validate

function stopNum0(obj) {

    var preobj = obj.value;
    var lastcode = preobj.charCodeAt(preobj.length-1);

    while ((lastcode<48) || (lastcode>57)) {
        preobj = preobj.substr(0,preobj.length-1);
        lastcode = preobj.charCodeAt(preobj.length-1);
    }
    newobj = "";
    for (i=0;i<preobj.length;i++){
        j = preobj.charCodeAt(i);
        if ((j>47)&&(j<58)) newobj += preobj.charAt(i);
    }
    obj.value = newobj;

}

function checkletter(obj){
    if (obj==""){
        return true;
    }else{
        code0 = obj.charCodeAt(0);
        if ((code0<65)||((code0>90)&&(code0<97))||(code0>122)){
            return false;
        }else{
            return true;
        }
    }
}

var preObj = "";

function stopNum(obj){
    code0 = obj.value.charCodeAt(0);

    checkNum = (oForm.oType[0].checked) ? obj.value.substr(1) : obj.value ;
    numObj = "";
    for (i=0;i<checkNum.length;i++){
        j = checkNum.charCodeAt(i);
        if ((j>47)&&(j<58)) numObj += checkNum.charAt(i);
    }

    if ( oForm.oType[0].checked ){
        if ( !checkletter(obj.value) ){
            if ((obj.value.length>1)&&(preObj.length>0)&&(checkletter(preObj))){ 
                window.status = "replaced";
                obj.value = preObj;
            }else{
                window.status = "cleared";
                obj.value = "";
            }
        }else{
            obj.value = String.fromCharCode(code0)+numObj;
        }
    }else{
        obj.value = numObj;
    }
    
    // check length
    if (oForm.oType[0].checked){
        obj.value = obj.value.substr(0,10);
    }else{
        obj.value = obj.value.substr(0,8);
    }

    preObj = obj.value;  // save previous correct value
    return true;
}
