var imgTimerID;
var checkCount;
var loadPic;

//----------------------------------------------------------------
//
// 
//----------------------------------------------------------------
function div_onoff(name){
  var obj = getObject(name);
  if(obj.style.display == "none") obj.style.display = "block";
  else                            obj.style.display = "none";
}


//----------------------------------------------------------------
//
// 
//----------------------------------------------------------------
function isImage(url){
	// delete after ";" from url
	var end = url.lastIndexOf("?");
	if(end > 0){
		url = url.substring(0, end);
	}
	end = url.lastIndexOf(";");
	if(end > 0){
		url = url.substring(0, end);
	}
	

	var n;
	n=url.lastIndexOf(".");

   if(n!=-1){
	var ext = url.substring(n);

	

	ext = ext.toLowerCase();

	if(ext==".bmp")       return(true);
	else if(ext==".cgm")  return(true);
	else if(ext==".gif")  return(true);
	else if(ext==".ief")  return(true);
	else if(ext==".jpeg") return(true);
	else if(ext==".jpg")  return(true);
	else if(ext==".jpe")  return(true);
	else if(ext==".png")  return(true);
	else if(ext==".svg")  return(true);
	else if(ext==".tiff") return(true);
	else if(ext==".tif")  return(true);
	else if(ext==".djvu") return(true);
	else if(ext==".djv")  return(true);
	else if(ext==".wbmp") return(true);
	else if(ext==".ras")  return(true);
	else if(ext==".ico")  return(true);
	else if(ext==".pnm")  return(true);
	else if(ext==".pbm")  return(true);
	else if(ext==".pgm")  return(true);
	else if(ext==".ppm")  return(true);
	else if(ext==".rgb")  return(true);
	else if(ext==".xbm")  return(true);
	else if(ext==".xpm")  return(true);
	else if(ext==".xwd")  return(true);
	else return(false);
   }
   else {
       return false;
   }
}


//----------------------------------------------------------------
// 
//----------------------------------------------------------------
function OpenImg(url, width, height){


	if(width==0 && height==0){
		var x=100;
		var y=100;
		width  = screen.width -100-200;
		height = screen.height-100-200;
		var option="menubar=no,scrollbars=yes,resizable=yes,toolbar=no,width="+width+",height="+height+",left="+x+",top="+y;
		window.open(url,'',option);
	  
	}
	else {
		width  = width  + 40;
      	height = height + 40;

      	var x=160;
      	var y=160;
      	if(width + x >= screen.width-100 ){
        	width=screen.width - 40;
        	x = 0;
      	}
      	if(height + y >= screen.height-100){
        	height=screen.height - 200;
        	y = 0;
      	}
      	var option="menubar=no,scrollbars=yes,resizable=yes,toolbar=no,width="+width+",height="+height+",left="+x+",top="+y;
      	window.open(url,'',option);
	}
	return;
	
	/**
    if(isImage(img)==true){
		checkCount=0;
		clearInterval(imgTimerID);
		imgTimerID=setInterval("loadImage(loadPic.src)", 300);
    } else {


		var x=100;
		var y=100;
		width  = screen.width -100-200;
		height = screen.height-100-200;
		var option="menubar=no,scrollbars=yes,resizable=yes,toolbar=no,width="+width+",height="+height+",left="+x+",top="+y;
		window.open(img,'',option);
    }
    **/
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
function loadImage(url){
   checkCount++;	
   var width;
   var height;
   if(loadPic.complete || checkCount > 100){
      if(loadPic.complete){
         width  = loadPic.width;
         height = loadPic.height;
      }
      else {
         width  = 640;
         height = 480;
      }   

      width  = width  + 40;
      height = height + 40;

      var x=160;
      var y=160;
      if(width + x >= screen.width-100 ){
         width=screen.width - 40;
         x = 0;
      }
      if(height + y >= screen.height-100){
         height=screen.height - 200;
         y = 0;
      }
      var option="menubar=no,scrollbars=yes,resizable=yes,toolbar=no,width="+width+",height="+height+",left="+x+",top="+y;
      window.open(url,'',option);
      clearInterval(imgTimerID);
   }
}

function popup_help(item){
  fileWindow = window.open("html/"+item,"",
                           "width=550,height=300,status=0,scrollbars=1,menubar=0,location=0,directions=0,toolbar=0,resizable=1");
}


//------------------------------------------------------
//
//------------------------------------------------------
function calcWidth(srcWidth, srcHeight, maxWidth, maxHeight)
{
	var dstWidth=0;
	var dstHeight=0;

	var maxAspectRatio = maxWidth/maxHeight;

	var aspectRatio = srcWidth/srcHeight;

	if(aspectRatio >= maxAspectRatio){
		var scale = maxWidth/srcWidth;
		dstWidth  = maxWidth;
		dstHeight = srcHeight * scale;
	}
	else {
		var scale = maxHeight/srcHeight;
		dstHeight  = maxHeight;
		dstWidth   = srcWidth * scale;
	}
	return dstWidth;
}

//------------------------------------------------------
//
//------------------------------------------------------
function calcHeight(srcWidth, srcHeight, maxWidth, maxHeight)
{
	var dstWidth=0;
	var dstHeight=0;

	var maxAspectRatio = maxWidth/maxHeight;

	var aspectRatio = srcWidth/srcHeight;

	if(aspectRatio >= maxAspectRatio){
		var scale = maxWidth/srcWidth;
		dstWidth  = maxWidth;
		dstHeight = srcHeight * scale;
	}
	else {
		var scale = maxHeight/srcHeight;
		dstHeight  = maxHeight;
		dstWidth   = srcWidth * scale;
	}
	return dstHeight;
}

//------------------------------------------------------
//
//------------------------------------------------------
function adjust_image_size(src_width, src_height)
{
	var width  = calcWidth(src_width, src_height, 400, 400);
	var height = calcHeight(src_width, src_height, 400, 400);
	
	document.photo_image.width  = width;
	document.photo_image.height = height;
	
	//alert("ADJUST SIZE "+width+","+height);
}

//------------------------------------------------------
//
//------------------------------------------------------
var DisableSubmit = {
   init: function() {
      this.addEvent(window, 'load', this.set());
   },

   set: function() {
      var self = this;
      return function() {
         for (var i = 0; i < document.forms.length; ++i) {
            if(document.forms[i].onsubmit) continue;
            document.forms[i].onsubmit = function() {
               self.setDisable(this.getElementsByTagName('input'));
            };
         }
      }
   },

   setDisable: function(elms) {
      for (var i = 0, elm; elm = elms[i]; i++) {
         if ((elm.type == 'submit' || elm.type == 'image' || elm.type == 'button') && !elm.disabled) {
            Set(elm);
         }
      }

      function Set(button) {
         window.setTimeout(function() { button.disabled = true; }, 1);
      }
      function unSet(button) {
         window.setTimeout(function() { button.disabled = false; }, 2000);
      }
   },

   addEvent: function(elm, type, event) {
      if(elm.addEventListener) {
         elm.addEventListener(type, event, false);
      } else if(elm.attachEvent) {
         elm.attachEvent('on'+type, event);
      } else {
         elm['on'+type] = event;
      }
   }
}

DisableSubmit.init();

//------------------------------------------------------
//
//------------------------------------------------------
function DesableAllButtons(){

	var iptObj = document.getElementsByTagName('input');

	for (i=0; i<iptObj.length; i++) {

		var ipt = iptObj[i];
		if ((ipt.type == 'submit' || ipt.type == 'image' || ipt.type == 'button') && !ipt.disabled) {
			ipt.disabled = true;
		}
	}
}

function showAttributePopup(data_id){
        window.open("attributeView.do?nomenu=true&action_status=search&data_id="+data_id);
}

function windowEvent(){
	if(window.event) return window.event;
	var caller = arguments.callee.caller;
	while(caller){
		var ob = caller.arguments[0];
		if(ob && ob.constructor == MouseEvent) return ob;
		caller = caller.caller;
	}
	return null;
}

//------------------------------------------------------
//
//------------------------------------------------------
function attach_file_delete()
{
	var action_status_obj = getObject("action_status");
	action_status_obj.setAttribute("value", "attach_file_delete");
	document.thisform.submit(); 
}

//------------------------------------------------------
//
//------------------------------------------------------
 function getObject(name){
   var obj = document.getElementById(name);
   if(obj==null){
     obj = document.getElementsByName(name).item(0);
   }
   return obj;
 }
