﻿function addHandler(object, eventName, handler) {
  if (typeof object.addEventListener != 'undefined')
    object.addEventListener(eventName, handler, false);
  else if (typeof object.attachEvent != 'undefined')
    object.attachEvent('on' + eventName, handler);
  else
    throw "Incompatible browser";
}

function removeHandler(object, event, handler) {
  if (typeof object.removeEventListener != 'undefined')
    object.removeEventListener(event, handler, false);
  else if (typeof object.detachEvent != 'undefined')
    object.detachEvent('on' + event, handler);
  else
    throw "Incompatible browser";
}

function getElementOffsetX( id ) {
	var x;
	x = id.offsetLeft;
	id = id.parentNode;
	while( id.parentNode ) {
		x += id.offsetLeft
	  id = id.parentNode;
	}
	return x;
}

function getElementOffsetY( id ) {
	var y;
	y = id.offsetTop;
	id = id.parentNode;
	while( id.parentNode ) {
		y += id.offsetTop
	  id = id.parentNode;
	}
	return y;
}


function rightClick( e ) {
	if ( !IE && e.which == 3)
		return true;
	else if ( IE && event.button==2)
		return true;
	else
		return false;
}

var stepZoomHW;

function zoomElement( idName , x, y, height, width, step  ) {
	
	ide = document.getElementById( idName );
	
	offsetX = getElementOffsetX( ide );
	offsetY = getElementOffsetY( ide );
	
	stepZoomHeight = height / step;
	stepZoomWidth = width / step;
	stepZoomX = ( x - offsetX ) / step;
	stepZoomY = ( y - offsetY ) / step;
	
	startZoomElement( idName, stepZoomHeight, stepZoomWidth, stepZoomX, stepZoomY, offsetX, offsetY, 0, 0, width, height, x, y );

	
}

function startZoomElement( idName, stepZoomHeight, stepZoomWidth, stepZoomX, stepZoomY, zoomElementX, zoomElementY, height, width, defWidth, defHeight, defX, defY ) {
	
	ide = document.getElementById( idName );
	
	zoomElementX = zoomElementX + stepZoomX;
	zoomElementY = zoomElementY + stepZoomY;
	
	height = height + stepZoomHeight;
	width = width + stepZoomWidth;
	
	ide.style.width = width + 'px';
	ide.style.height = height + 'px';
	ide.style.left = zoomElementX + 'px';
	ide.style.top = zoomElementY + 'px';
	
	if( ( height < defHeight ) && ( width < defWidth ) ) {
		Timer1 = self.setTimeout("startZoomElement( '"+idName+"', "+stepZoomHeight+", "+stepZoomWidth+", "+stepZoomX+", "+stepZoomY+", "+zoomElementX+", "+zoomElementY+", "+height+", "+width+", "+defWidth+", "+defHeight+", "+defX+", "+defY+" )", 10 );
	}
	else {
		ide.style.width = defWidth + 'px';
		ide.style.height = defHeight + 'px';
		ide.style.left = defX + 'px';
		ide.style.top = defY + 'px';		
	}
	
}

function getCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function removeCookie(name) {
	setCookie(name,"",-1);
}

function setCookie(name,value,hours) {
	if (hours) {
		var date = new Date();
		date.setTime(date.getTime()+(hours*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function getId( str ) {
	return document.getElementById( str );
}

function eventTarget () {
	if( IE ) return this.srcElement;
	else return this.target;
}

function eventTarget (evt) {
	if( IE ) return evt.srcElement;
	else return evt.target;
}

function checkMail( sEmail ) {
	if( sEmail != null ) {
		var reEmail = /^[a-z0-9_.-]+([_\\.-][a-z0-9]+)*@([a-z0-9_\.-]+([\.][a-z]{2,4}))+$/i;
	  var bCheck = true;
		if ( sEmail.search( reEmail ) == -1 )
	    return false;
		else
			return true;
	}
	return false;
	
}
	
function getFileExt( fName ){
	fullName = fName;
	shortName = fullName.match(/[^\/\\]+$/);
	splitName = fullName.split(".");
	if( splitName[splitName.length-1] )
		return splitName[splitName.length-1].toLowerCase();
	else
		return fullName;
}

var WaitToHide = 100;
var TimerToHide;
var IdToHide = '';
var PrevClass = '';
var HideClass = 'hideElement';

function showElement(id, classN ){
  if(TimerToHide) {
		clearTimeout(TimerToHide);
	}
  if( ( IdToHide != '' ) && ( IdToHide != id ) ){
    IdToHide.className = HideClass;
  }
  IdToHide = id;
  id.className = classN;
}

function hideElement(id){
  id.className = HideClass;
}

function doHide(id){
  TimerToHide = self.setTimeout("hideElement(IdToHide)", Wait );
}

function dontHide(){
	if(TimerToHide) {
		clearTimeout(TimerToHide);
	}
}


var debug_time = 0;//for debug

function debug(option) {
  timeNow = new Date();
  
  if( option == 'start' ){
    debug_time = timeNow.getTime();
  }
  else if(option == 'stop'){
    alert( timeNow.getTime() - debug_time );
  }
    
    
}
