﻿function elementId(id){return document.getElementById(id);}

var _preImgId = 0;
var _preGifs=new Array();
function goPreload(){
    if(_preImgId == imgArr.length-1) return;
    var g = new Image();
    g.onload=goPreload;
    g.src="/images"+PageContext.imgBase+imgArr[_preImgId++];
    _preGifs[_preGifs.length]=g;
}

function getOffset(elm){
	var o = arguments[1];
	if(!o){
		o = new Object();
		o.x=0;
		o.y=0;
	}
	if(!elm || elm.tagName.toUpperCase()=="HTML") return o;
	o.x += elm.offsetLeft;
	o.y += elm.offsetTop;
	return getOffset(elm.parentNode, o);
}


function setLayout(sttText){
    window.status=sttText;;
    var w = (screen.width - 710)/2;
    var h = (screen.height*0.76 - 454)/2;
    if(elementId("nextPrev")){
        var nextPrev = elementId("nextPrev").style;
        var prc = (navigator.userAgent.indexOf("MSIE") != -1)?0.87:0.87;
        prc = (screen.height > 1000)?0.95:prc;
        var st = document.body.scrollTop;
        var sl = document.body.scrollLeft;
        nextPrev.top = (h + st+ 454*prc)+ "px";
        nextPrev.left = (w+sl+3)+"px";
        var o = getOffset(elementId("fTypes"));
        nextPrev.top = (o.y + 454*0.85)+ "px";
//        nextPrev.left = (o.x+(355-nextPrev.offsetWidth)/2)+"px";        
        nextPrev.left = (o.x)+"px";   
    }

/*
var o = elementId("fTypes");
sabc.xx(dff);
var o = elementId("fTypes");
if(o.y){
var j = parseInt(parseInt(o.y) + (0.9*454));
alert("curr=" + nextPrev.top +"  new=" + j);
nextPrev.top=j+"px";
} else {alert(o.y)}
*/

}

function setOpacity(elm, opacity){
    if (navigator.userAgent.indexOf("MSIE") != -1) {
        var normalized = Math.round(opacity * 100);
        elm.style.filter = "alpha(opacity=" + normalized + ")";
    } else {
        elm.style.opacity = opacity;
    }
}

var ImgPool = {
        opVal:1,
        opStep:-0.1,
        elm:null,
        timeout:null,
        canRun:true,
        inProcess:false,
        Switch:function(isNext, isManual, picNum){
            if(!this.elm)this.elm=elementId("fTypes");
            if(!this.inProcess&&!isManual && !this.canRun){
                setOpacity(this.elm, 1);
                if(this.timeout){
                    clearTimeout(this.timeout);
                    this.timeout=null;
                }
                this.opVal=1;
                return;
            }
            this.opVal += this.opStep;
            this.inProcess=true;
            if(this.opVal > 1) {
                this.opVal = 1-this.opStep;
                this.opStep = this.opStep*-1;
                this.inProcess=false;
                return;
            } else if(this.opVal <= 0) {
                this.opStep = this.opStep*-1;
                this.opVal = this.opStep;
                nextImage(isNext, picNum);
            }
            setOpacity(this.elm, this.opVal);
            if(this.timeout){
                clearTimeout(this.timeout);
                this.timeout=null;
            }
            if(typeof(picNum)=='undefined'){
                this.timeout=setTimeout("ImgPool.Switch("+isNext+","+isManual+")", 100);
            } else {
                this.timeout=setTimeout("ImgPool.Switch("+isNext+","+isManual+","+picNum+")", 100);
            }
        }
};

function nextImage(isNext,picNum){
    var o = elementId("fTypes");
    var url = o.src;
    var currImg = url.substring(url.lastIndexOf(PageContext.imgBase) + PageContext.imgBase.length);
    var n=0;
    for(var i=0; i<imgArr.length; i++){
        if(currImg == imgArr[i]){
            n = i;
            break;
        }
    }
    if(typeof(picNum)!='undefined') n = parseInt(picNum) - 1;
    if(isNext){
      n++;
      n=(n==imgArr.length)?0:n;
    } else {
        n--;
        n=(n<0)?imgArr.length-1:n;
    }
    var nextImg = url.substring(0, url.lastIndexOf(PageContext.imgBase) + PageContext.imgBase.length) + imgArr[n];
    o.src=nextImg;
    if(elementId("a"+n)){
        for(var i=0; i<imgArr.length;i++){
            var oA = elementId("a"+i);
            oA.style.fontWeight = (i==n)?"bold":"normal";
        }
    }
}

function openGallery(){
    var url="./gallery.asp";
    var options = "menubar=no,menubar=0,resizable=yes,top=0,left=0,scrollbars=yes"
    var w = window.open(url, "gallery", options);
    if(w != null) {
        w.name = "gallery";
        var winH = screen.height * 0.9;
        var winW = screen.width * 0.9;
        w.resizeTo(winW, winH);
        var t = (screen.height - winH)/2
        var l = (screen.width - winW)/2
        w.moveTo(l,t);
    }
}

function trim(s){
    return (s==null)?'':s.replace(/^\s+/, '').replace(/\s+$/, '');
}

function isValidEmail(emailStr) {
	emailStr = trim(emailStr);
   	if(emailStr.length == 0) return false;

   	var emailPat=/^(.+)@(.+)$/;
   	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
   	var validChars="\[^\\s" + specialChars + "\]";
   	var quotedUser="(\"[^\"]*\")";
   	var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/;
   	var atom=validChars + '+';
   	var word="(" + atom + "|" + quotedUser + ")";
   	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
   	var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");
   	var matchArray=emailStr.match(emailPat);
   	if(matchArray == null) return false;

   	var user=matchArray[1];
   	var domain=matchArray[2];
   	if(user.match(userPat) == null) return false;

   	var IPArray = domain.match(ipDomainPat);
   	if(IPArray != null){
       for(var i = 1; i <= 4; i++) {
          if(IPArray[i] > 255) return false;
       }
       return true;
   	}

   	var domainArray=domain.match(domainPat);
   	if(domainArray == null) return false;

   	var atomPat=new RegExp(atom,"g");
   	var domArr=domain.match(atomPat);
   	var len=domArr.length;
   	if((domArr[domArr.length-1].length < 2) || (domArr[domArr.length-1].length > 3)) return false;
   	if(len < 2) return false;

   	return true;
}
