var TopMenuOffsetX=0;
var TopMenuOffsetY=0;
var LeftMenuOffsetX=0;
var LeftMenuOffsetY=0;
var LanguageMenuOffsetX = 0;
var LanguageMenuOffsetY = 0;

function toDate(objYear,objMonth,objDay,dayNum){
  if (objYear.value=='-1' || objMonth.value=='-1'){
	  objMonth.options[0].selected=true;
    objDay.options[0].selected=true;
	  return;
	}
  //with(document.all){
    vYear=parseInt(objYear.options[objYear.selectedIndex].value)
    vMonth=parseInt(objMonth.options[objMonth.selectedIndex].value)
    objDay.length=1;
    for(i=0;i<(new Date(vYear,vMonth,0)).getDate();i++){
      objDay.options[objDay.length++].value=objDay.length-1;
			objDay.options[objDay.length-1].text=objDay.length-1;
		}
		if (dayNum>0){
		  objDay.options[dayNum].selected=true;
		}
  //}
}

function DIY_Browser() {
 d=document;
 this.agent=navigator.userAgent.toLowerCase();
 this.major = parseInt(navigator.appVersion);
 this.dom=(d.getElementById)?1:0; // true for ie6, ns6
 this.ns=(d.layers);
 this.ns4up=(this.ns && this.major >=4);
 this.ns6=(this.dom&&navigator.appName=="Netscape");
 this.op=(this.agent.indexOf('opera')!=-1&&this.major<7);
 this.opera5=(this.agent.indexOf("Opera 5")>-1);
 this.ie=(d.all);
 this.ie4=(d.all&&!this.dom)?1:0;
 this.ie4up=(this.ie && this.major >= 4);
 this.ie5=(d.all&&this.dom);
 this.ie6=(this.agent.indexOf("msie 6")>-1 && this.dom && !this.opera5)?1:0;
 this.win=((this.agent.indexOf("win")!=-1) || (this.agent.indexOf("16bit")!=-1));
 this.mac=(this.agent.indexOf("mac")!=-1);
}

var oBw = new DIY_Browser();

function getH(o) { var h=0; if (oBw.ns) { h=(o.height)? o.height:o.clip.height; return h; } h=(oBw.op)? o.style.pixelHeight:o.offsetHeight; return h; }
function setH(o,h) { if(oBw.ns) {if(o.clip) o.clip.bottom=h;}else if(oBw.op)o.style.pixelHeight=h;else o.style.height=h; }

function getW(o) { var w=0; if(oBw.ns) { w=(o.width)? o.width:o.clip.width; return w; } w=(oBw.op)? o.style.pixelWidth:o.offsetWidth; return w; }
function setW(o,w) { if(oBw.ns) {if(o.clip) o.clip.right=w;}else if(oBw.op)o.style.pixelWidth=w;else o.style.width=w; }

function getX(o) { var x=(oBw.ns)? o.left:(oBw.op)? o.style.pixelLeft:o.offsetLeft; return x;}
function setX(o,x) { (oBw.ns)? o.left=x:(oBw.op)? o.style.pixelLeft=x:o.style.left=x; }

function getY(o) {  var y=(oBw.ns)? o.top:(oBw.op)? o.style.pixelTop:o.offsetTop; return y;}
function setY(o,y) { (oBw.ie||oBw.dom)? o.style.top=y:(oBw.ns)? o.top=y:o.style.pixelTop=y; }

function getPageX(o) { if(oBw.ns) { var x=(o.pageX)? o.pageX:o.x; return x; } else if (oBw.op) {  var x=0; while(eval(o)) { x+=o.stylo.pixelLeft; e=o.offsetParent; } return x; } else { var x=0; while(eval(o)) { x+=o.offsetLeft; o=o.offsetParent; } return x; } }

function getPageY(o) { if(oBw.ns) { var y=(o.pageY)? o.pageY:o.y; return y; } else if (oBw.op) {  var y=0; while(eval(o)) { y+=o.stylo.pixelTop; o=o.offsetParent; } return y; }  else { var y=0; while(eval(o)) { y+=o.offsetTop; o=o.offsetParent; } return y; } }

function mpageX(element){
  var x = 0;
  do{
    if (element.style.position == 'absolute'){
      return x + element.offsetLeft;
    }else{
      x += element.offsetLeft;
      if (element.offsetParent)
        if (element.offsetParent.tagName == 'TABLE')
          if (parseInt(element.offsetParent.border) > 0){
            x += 1;
          }
    }
  }
  while ((element = element.offsetParent));
  return x;
}

// Calculates the absolute page y coordinate of any element
function mpageY(element){
  var y = 0;
  do{
    if (element.style.position == 'absolute'){
      return y + element.offsetTop;
    }else{
      y += element.offsetTop;
      if (element.offsetParent)
        if (element.offsetParent.tagName == 'TABLE')
          if (parseInt(element.offsetParent.border) > 0){
            y += 1;
          }
    }
  }
  while ((element = element.offsetParent));
  return y;
}

function getObject(n, d) { //v4.01
	var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=getObject(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function _MyMenuItemOut(){
	 var args=_MyMenuItemOut.arguments;
   _MyShowHideLayers(args[0],args[1],args[2],args[3],args[4],args[5]);
}
function _MyMenuItemOver(){
   var args=_MyMenuItemOver.arguments;
	 _MyShowHideLayers(args[0],args[1],args[2],args[3],args[4],args[5]);
}

function _MyInitDivPosition(){
  //0--src id,1-div id,2-direct,3-offset
  var args=_MyInitDivPosition.arguments;
  var objSrc = getObject(args[0]);
  var obj = getObject(args[1]);
  obj = obj.style;
  if (args[2]=="MAIN_TOP"){
     obj.top = getPageY(objSrc) + objSrc.offsetHeight + TopMenuOffsetY;
     obj.left = getPageX(objSrc) + TopMenuOffsetX;
  }else if (args[2]=="MAIN_LEFT"){
     obj.top = getPageY(objSrc) + LeftMenuOffsetY;
     obj.left = getPageX(objSrc) + objSrc.offsetWidth + LeftMenuOffsetX;
  }else if (args[2]=="LANGUAGE"){
     obj.top = getPageY(objSrc) + objSrc.offsetHeight + LanguageMenuOffsetY;
     obj.left = getPageX(objSrc) + LanguageMenuOffsetX;
  }
}
function _MyShowHideLayers() { //v3.0
  var i,p,v,vt,obj,args=_MyShowHideLayers.arguments;
  i = 0;
	if ((obj=getObject(args[i]))!=null) {
		 v=args[i+2];
		 if (obj.style) { obj=obj.style; vt=(v=='show')?'visible':(v=='hide')?'hidden':v; }
		 obj.display = "block";
		 obj.visibility=vt;
	}
}

function checkLength(obj,num,op){
	var inputLength=obj.replace(/[^\x00-\xff]/g,'**').length;
	var rv = false;
	if (op==">"){ rv = inputLength>num;}
	if (op=="<"){ rv = inputLength<num;}
	if (op=="="){ rv = inputLength!=num;}
	return rv;
}

function trim(str) {
	regExp1 = /^ */;
	regExp2 = / *$/;
	return str.replace(regExp1,'').replace(regExp2,'');
}

function MyWindowClose(){
    opener = null;
    window.close();
}

function MyGetKeyCode(e){
	var code;
	if (!e) var e = window.event;
	if (e.keyCode){
    code = e.keyCode;
	}else if (e.which){
    code = e.which;
	}
  return code;
}

// onkeypress
function CheckNum(obj)
{
	if ((window.event.keyCode<45 || window.event.keyCode>57 || window.event.keyCode==47) && window.event.keyCode!=45 && window.event.keyCode!=9)  window.event.returnValue=false;
	if( window.event.keyCode==46 && obj.value.indexOf(".")>-1) window.event.returnValue=false;
}

// onkeypress
function CheckInt(obj)
{
	if ((window.event.keyCode<48 || window.event.keyCode>57) && window.event.keyCode!=45 && window.event.keyCode!=9) window.event.returnValue=false;
}

//-2.36onkeypress
function MyCheckNum(obj,e)
{
  var code = MyGetKeyCode(e);
  if ((code<45 || code>57 || code==47) && code!=45 && code!=9 && code!=8) return false;
  if(code==46 && obj.value.indexOf(".")>-1) return false;
  return true;
}

//325onkeypress
function MyCheckInt(obj,e)
{
  var code = MyGetKeyCode(e);
  if ((code<48 || code>57) && code!=45 && code!=9 && code!=8) return false;
  return true;
}

function MyOnfocusSelect(obj){
	if (obj.type=="text"){
		obj.select();
	}
}

function isEmail(mail) { 
  return(new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(mail)); 
} 

function getFullName(path){
  var tmp = path;
	var pos = -1;
	pos = tmp.lastIndexOf("\\");
	if (pos==-1){
	  pos = tmp.lastIndexOf("/");
	}
	if (pos==-1){
	  return path;
	}else{
	  return tmp.substring(pos+1);
	}
}

//return abc
function getName(path){
  var tmp = path;
	var pos = -1;
	var endPos = -1;
	pos = tmp.lastIndexOf("\\");
	if (pos==-1){
	  pos = tmp.lastIndexOf("/");
	}
	endPos = tmp.indexOf(".",pos);
	if (endPos==-1){
	  return tmp.substring(pos+1);
	}else{
	  return tmp.substring(pos+1,endPos);
	}
}


function MyOpenWindowPos(url,name,width,height,options){
	//var appVer = parseInt(navigator.appVersion);
	var top = 0;
	var left = 0;
	var srcWidth=screen.width-10;
	var srcHeight=screen.height - 57;
	var other="";
	if (options==1){//Left Top
		other="top=0, left=0, screenX=0, screenY=0,";//Netscape
	}else if (options==2){//Right Top
		other="top=0, left=" + (srcWidth-width) + ", screenX=" + (srcWidth-width) + ", screenY=0,";//Netscape
	}else if (options==0){//Center
		other="";
	}else if (options==3){//Left Buttom
		other="top=" + (srcHeight-height) + ", left=0, screenX=0, screenY=" + (srcHeight-height) + ",";//Netscape
	}else if (options==5){//Right Buttom
		other="top=" + (srcHeight-height) + ", left=" + (srcWidth-width) + ", screenX=" + (srcWidth-width) + ", screenY=" + (srcHeight-height) + ",";//Netscape
	}
	openwindow(url, name,width ,height,other);
}

/**
 * Open window function
 * @param url -- The open object
 * @param name -- The window name
 * @param width -- The window width
 * @param height -- The window height
 * @param options -- The window options
 */
function MyOpenWindow(url,name,width,height,options){
    openwindow(url, name,width ,height,options);
}

function openwindow( url, winName,width,height,otherproperty)
{
  //width,height
  //otherproperty
  xposition=0; yposition=0;
  if ((parseInt(navigator.appVersion) >= 4 )){
		if (width==1){
			width=screen.width - 10;xposition = 0;
		}else{
			xposition = (screen.width - width) / 2;
		}
		if (height==1){
			height=screen.height - 57;yposition = 0;
		}else{
			yposition = (screen.height - height) / 2 - 15;
		}
		if (yposition < 0 ) yposition = 0;
	}
	theproperty= "width=" + width + ", " + "height=" + height + ", ";
	if (otherproperty.toLowerCase().indexOf("screenx")==-1) theproperty += "screenX=" + xposition + ", "; //Netscape
	if (otherproperty.toLowerCase().indexOf("screenY")==-1) theproperty += "screenY=" + yposition + ", "; //Netscape
	if (otherproperty.toLowerCase().indexOf("left")==-1) theproperty += "left=" + xposition + ", "; //IE
	if (otherproperty.toLowerCase().indexOf("top")==-1) theproperty += "top=" + yposition + ", "; //IE

	if (otherproperty.toLowerCase().indexOf("location")==-1) theproperty += "location=0, ";
	if (otherproperty.toLowerCase().indexOf("menubar")==-1) theproperty += "menubar=0, ";
	if (otherproperty.toLowerCase().indexOf("resizable")==-1) theproperty += "resizable=0, ";
	if (otherproperty.toLowerCase().indexOf("scrollbars")==-1) theproperty += "scrollbars=1, ";
	if (otherproperty.toLowerCase().indexOf("status")==-1) theproperty += "status=0, ";
	if (otherproperty.toLowerCase().indexOf("toolbar")==-1) theproperty += "toolbar=0, ";
	if (otherproperty.toLowerCase().indexOf("hotkeys")==-1) theproperty += "hotkeys=0, ";
	theproperty = theproperty + ', ' + otherproperty;
	return window.open( url,winName,theproperty );
}

/**
 * Clear the object in the form
 * @param obj -- The object in the form
 */
function MyClearObject(obj){
    obj.value = "";
}

/**
 * Get file's extends
 */
function getFileExt(path){
    var tmp = path;
    tmp = tmp.substring(tmp.lastIndexOf(".")+1);
    return tmp.toUpperCase();
}

/**
 * Check file's extends name in extList
 */
function MyCheckExt(path,extList){
    var ext = getFileExt(path);
    extList = extList.toUpperCase();
    var cExt = extList.indexOf("," + ext + ",");
    if (ext == "") return -1;
    if (cExt==-1){
        return 0;
    }else{
        return 1;
    }
}
function AutoResizeImage(maxWidth,maxHeight,objImg){
	var img = new Image();
	img.src = objImg.src;
  var hRatio;
	var wRatio;
	var Ratio = 1;
	var w = img.width;
	var h = img.height;
	wRatio = maxWidth / w;
	hRatio = maxHeight / h;
	if (maxWidth ==0 && maxHeight==0){
		Ratio = 1;
	}else if (maxWidth==0){//
		if (hRatio<1) Ratio = hRatio;
	}else if (maxHeight==0){
		if (wRatio<1) Ratio = wRatio;
	}else if (wRatio<1 || hRatio<1){
		Ratio = (wRatio<=hRatio?wRatio:hRatio);
	}
	if (Ratio<1){
	 w = w * Ratio;
	 h = h * Ratio;
	}
	objImg.height = h;
	objImg.width = w;
}

function AutoResizeImage1(maxWidth,maxHeight,objImgTmp){
	var hRatio;
	var wRatio;
	var Ratio = 1;
	var isKernel = true;
	var objImg = getObject(objImgTmp);
	if (objImg==null) {isKernel = false};
	if (isKernel && objImg.width==0) {isKernel = false};
	var width = 0;
	var height = 0;
	if (isKernel){
		var imgTmp = new Image();
		imgTmp.src = objImg.src;
		width = imgTmp.width;
		height = imgTmp.height;
		if (width==0){
			var timer=window.setTimeout("AutoResizeImage(" + maxWidth + "," + maxHeight + ",'" + objImgTmp + "')",500);
		}else{
			wRatio = maxWidth / width;
			hRatio = maxHeight / height;
			//alert(wRatio + "--" + hRatio);
			if (maxWidth==0){//
					if (hRatio<1) Ratio = hRatio;
			}else if (maxHeight==0){
					if (wRatio<1) Ratio = wRatio;
			}else if (wRatio<1 || hRatio<1){
					Ratio = (wRatio<=hRatio?wRatio:hRatio);
			}
			if (Ratio<1){
			 objImg.width = width * Ratio;
			 objImg.height = height * Ratio;
			}else{
			 objImg.width = width;
			 objImg.height = height;
			}
		}
	}else{
		var timer=window.setTimeout("AutoResizeImage(" + maxWidth + "," + maxHeight + ",'" + objImgTmp + "')",500);
	}
}

function MyStr2Float(as_str,ai_digit,as_type)
{
   var fdb_tmp = 0;
   var fi_digit = 0;
   var fs_digit = "1";
   var fs_str = "" + as_str;
   var fs_tmp1 = "";
   var fs_tmp2 = "";
   var fi_pos = 0;
   var fi_len = 0;
   fdb_tmp = parseFloat(isNaN(parseFloat(fs_str))?0:fs_str);

   switch (true)
   {
      case (ai_digit==null)://
         fdb_tmp = fdb_tmp;
         break;
      case (ai_digit==0)://
         fdb_tmp = Math.round(fdb_tmp);
         break;
      case (ai_digit>0)://
         for (var i=0;i<ai_digit;i++) fs_digit +="0";
         fi_digit = parseInt(fs_digit);
         fdb_tmp = Math.round(fdb_tmp * fi_digit) / fi_digit;
         if (as_type=="str")
         {
            fs_tmp1 = fdb_tmp.toString();
            fs_tmp1 +=((fs_tmp1.indexOf(".")!=-1)?"":".") + fs_digit.substr(1);
            fi_pos = fs_tmp1.indexOf(".") + 1 + ai_digit;
            fdb_tmp = fs_tmp1.substr(0,fi_pos);
         }
         break;
   }
   return fdb_tmp;
}


function ClearSelected(obj,index){
  if (obj != null){
    if(obj.length==null){
		  if (index==-1) obj.checked = false;
    }else{
      for (i = 0 ; i< obj.length ; i++){
			  if (index!=-1 && index==i){
				  obj[i].checked = true;
				}else{
          obj[i].checked = false;
				}
      }
    }
  }
}

function SelectedAll(obj){
  if (obj != null){
    if(obj.length==null){
		  obj.checked = true;
    }else{
      for (i = 0 ; i< obj.length ; i++){
			  obj[i].checked = true;
      }
    }
  }
}
function FocusOptions(obj){
	if (obj != null){
		if(obj.length==null){
			obj.focus();
		}else{
			obj[0].focus();
		}
	}	
}
function CheckIsSelected(obj){
	var ischeck = false;
	if (obj != null){
		if(obj.length==null){
			if (obj.checked == true) ischeck = true;
		}else{
			for (i = 0 ; i< obj.length ; i++){
				if (obj[i].checked == true){ischeck = true;break;}
			}
		}
	}
	return ischeck;
}

function OperateSelect(sel,ddl){
	var selectType;
	var obj=sel;
	try{
		selectType = ddl.value
		ddl.options[0].selected=true;
	}catch(e){
		selectType = ddl
		alert("ss")
	}
	if(obj){
		if(obj.length==null){
			if (!obj.disabled){
				switch (selectType){
					case "all":obj.checked=true;break;
					case "invert":obj.checked=!obj.checked;break;
					case "none":obj.checked=false;break;
				}
			}
		}else{
			for (var i = 0 ; i< obj.length ; i++){
				if (!obj[i].disabled){
					switch (selectType){
						case "all":obj[i].checked=true;break;
						case "invert":obj[i].checked=!obj[i].checked;break;
						case "none":obj[i].checked=false;break;
					}
			  }
			}
		}
	}
}

function OperateSelectCustom(frmName,selPrefix,stuffix,ddl){
	var selectType;
	var obj=stuffix;
	var objSel = null;
	try{
		selectType = ddl.value
		ddl.options[0].selected=true;
	}catch(e){
		selectType = ddl
		alert("ss")
	}
	if(obj){
		if(obj.length==null){
			if (!obj.disabled){
				objSel = eval("document." + frmName + "." + selPrefix + obj.value);
				switch (selectType){
					case "all":objSel.checked=true;break;
					case "invert":objSel.checked=!objSel.checked;break;
					case "none":objSel.checked=false;break;
				}
			}
		}else{
			for (var i = 0 ; i< obj.length ; i++){
				if (!obj[i].disabled){
					objSel = eval("document." + frmName + "." + selPrefix + obj[i].value);

					switch (selectType){
						case "all":objSel.checked=true;break;
						case "invert":objSel.checked=!objSel.checked;break;
						case "none":objSel.checked=false;break;
					}
			  }
			}
		}
	}
}

function SetSelectItem(ddl,v){
	for (var i = 0 ; i<ddl.options.length;i++){
		if (ddl.options[i].value==v){
			ddl.options[i].selected = true;
			break;
		}
	}
}

function PreviewPic(path){
  if (path == ""){
		alert("Please select a pic");
  }else{
		window.open("previewImage.asp?img=" + path,"preImg","");
  }
}

function AddToFavorite(title,url,desc)
{
	if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function'))//Gecko
	{
		window.sidebar.addPanel(title,url,desc);
	}
	else//IE
	{
		window.external.AddFavorite(url,title);
	}
}


function isRegisterUserName(s)
{
	var patrn=/^[a-zA-Z]{1}([a-zA-Z0-9_]){5,19}$/;
	if (!patrn.exec(s)) return false
	return true
}

function high(which2){
  theobject=which2
  highlighting=setInterval("highlightit(theobject)",50)
}
function low(which2){
  clearInterval(highlighting)
  which2.filters.alpha.opacity=50
}



function highlightit(cur2){
  if (cur2.filters.alpha.opacity<100)
    cur2.filters.alpha.opacity+=5
  else if (window.highlighting)
    clearInterval(highlighting)
}

function hidDiv(o){
	o.style.display = "none";	
}

function showDiv(o){
	o.style.display = "";
}
// -----------------------------------------------------------------------------------

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
