﻿/**
 * COMMON DHTML FUNCTIONS
 * These are handy functions I use all the time.
 *
 * By Seth Banks (webmaster at subimage dot com)
 * http://www.subimage.com/
 *
 * Up to date code can be found at http://www.subimage.com/dhtml/
 *
 * This code is free for you to use anywhere, just keep this comment block.
 */

/**
 * X-browser event handler attachment and detachment
 * TH: Switched first true to false per http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
 *
 * @argument obj - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn - function to call
 */
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

/**
 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
 *
 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
 *
 * Gets the full width/height because it's different for most browsers.
 */
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 

	return window.undefined; 
}
function getViewportWidth() {
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
}

/**
 * Gets the real scroll top
 */
function getScrollTop() {
	if (self.pageYOffset) // all except Explorer
	{
		return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollTop;
	}
}
function getScrollLeft() {
	if (self.pageXOffset) // all except Explorer
	{
		return self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollLeft)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollLeft;
	}
}


/**
 * SUBMODAL v1.6
 * Used for displaying DHTML only popups instead of using buggy modal windows.
 *
 * By Subimage LLC
 * http://www.subimage.com
 *
 * Contributions by:
 * 	Eric Angel - tab index code
 * 	Scott - hiding/showing selects for IE users
 *	Todd Huss - inserting modal dynamically and anchor classes
 *
 * Up to date code can be found at http://submodal.googlecode.com
 */

// Popup code
var gPopupUSERMask = null;
var gPopupUSERContainer = null;
var gPopFrameUSER = null;
var gReturnFuncUSER;
var gPopupUSERIsShown = false;
var gDefaultPageUSER = "/loading.html";
var gHideSelectsUSER = false;
var gReturnValUSER = null;

var gTabIndexesUSER = new Array();
// Pre-defined list of tags we want to disable/enable tabbing into
var gTabbableTagsUSER = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");	

// If using Mozilla or Firefox, use Tab-key trap.
if (!document.all) {
	document.onkeypress = keyDownHandlerUSER;
}

/**
 * Initializes popup code on load.	
 */
function initPopUpUSER() {
	// Add the HTML to the body
	theBody = document.getElementsByTagName('BODY')[0];
	popmask = document.createElement('div');
	popmask.id = 'popupUSERMask';
	popcont = document.createElement('div');
	popcont.id = 'popupUSERContainer';
	popcont.innerHTML = '' +
		'<div id="popupUSERInner">' +
			'<div id="popupUSERTitleBar">' +
				'<div id="popupUSERTitle"></div>' +
				'<div id="popupUSERControls">' +
					//'<img src="/Layout/images/Close_Popup.gif" onclick="hidePopWinUSER(false);" id="popCloseBox" />' +
				'</div>' +
			'</div>' +
			
			'<b class="b1 b1-user"></b><b class="b2 b-user"></b><b class="b3 b-user"></b><b class="b4 b-user"></b>' +
			'<div class="contentb b-user" style="padding:1px 5px 1px 5px;">' +
				'<div class="agri-atm-chitiet" style="overflow:hidden;">' +
					'<div class="agri-atm-close fr"><a class="btnCloseUSER" href="javascript:hidePopWinUSER(false)" titlebar="Close" title="Close"></a></div>' +
					'<iframe src="'+ gDefaultPageUSER +'" style="width:100%;height:100%;background-color:transparent;" scrolling="auto" frameborder="0" allowtransparency="true" id="popupUSERFrame" name="popupUSERFrame" width="100%" height="100%"></iframe>' +
					'<div class="cl cao-5"></div>' +
				'</div>' +
			'</div>' +
			'<b class="b4 b-user"></b></b><b class="b3 b-user"></b><b class="b2 b-user"></b><b class="b1 b1-user"></b>' +
			
			
		'</div>';
	theBody.appendChild(popmask);
	theBody.appendChild(popcont);
	
	gPopupUSERMask = document.getElementById("popupUSERMask");
	gPopupUSERContainer = document.getElementById("popupUSERContainer");
	gPopFrameUSER = document.getElementById("popupUSERFrame");	
	
	//$("#chh-dialog-USER").draggable({ containment: '#popupUSERMask' });
	
	// check to see if this is IE version 6 or lower. hide select boxes if so
	// maybe they'll fix this in version 7?
	var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
	if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
		gHideSelectsUSER = true;
	}
	
	// Add onclick handlers to 'a' elements of class submodal or submodal-width-height
	var elms = document.getElementsByTagName('a');
	for (i = 0; i < elms.length; i++) {
		if (elms[i].className.indexOf("submodal") == 0) { 
			// var onclick = 'function (){showPopWinUSER(\''+elms[i].href+'\','+width+', '+height+', null);return false;};';
			// elms[i].onclick = eval(onclick);
			elms[i].onclick = function(){
				// default width and height
				var width = 400;
				var height = 200;
				// Parse out optional width and height from className
				params = this.className.split('-');
				if (params.length == 3) {
					width = parseInt(params[1]);
					height = parseInt(params[2]);
				}
				showPopWinUSER(this.href,width,height,null); return false;
			}
		}
	}
}
addEvent(window, "load", initPopUpUSER);

 /**
	* @argument width - int in pixels
	* @argument height - int in pixels
	* @argument url - url to display
	* @argument returnFunc - function to call when returning true from the window.
	* @argument showCloseBox - show the close box - default true
	*/
function showPopWinUSER(url, title, width, height, beforePopUpFunc, returnFunc) {
	// show or hide the window close widget
	//var showCloseBox = true
//	if (showCloseBox == null || showCloseBox == true) {
//		document.getElementById("popCloseBox").style.display = "block";
//	} else {
//		document.getElementById("popCloseBox").style.display = "none";
//	}
	//document.getElementById("chh-dialog-title-USER").innerHTML = title;
	gPopupUSERIsShown = true;
	disableTabIndexesUSER();
	gPopupUSERMask.style.display = "block";
	gPopupUSERContainer.style.display = "block";
	// calculate where to place the window on screen
	centerPopWinUSER(width, height);
	
	var titleBarHeight = parseInt(document.getElementById("popupUSERTitleBar").offsetHeight, 10);


	gPopupUSERContainer.style.width = width + "px";
	gPopupUSERContainer.style.height = (height+titleBarHeight) + "px";
	
	setMaskSizeUSER();

	// need to set the width of the iframe to the title bar width because of the dropshadow
	// some oddness was occuring and causing the frame to poke outside the border in IE6
	//gPopFrameUSER.style.width = parseInt(document.getElementById("popupUSERTitleBar").offsetWidth, 10) - 12 + "px";
	gPopFrameUSER.style.height = (height) + "px";
	
	// set the url
	gPopFrameUSER.src = url;
	
	gReturnFuncUSER = returnFunc;
	// for IE
	if (gHideSelectsUSER == true) {
		hideSelectBoxes();
	}
	
	window.setTimeout("setPopTitleUSER();", 600);
}

//
var gi = 0;
function centerPopWinUSER(width, height) {
	if (gPopupUSERIsShown == true) {
		if (width == null || isNaN(width)) {
			width = gPopupUSERContainer.offsetWidth;
		}
		if (height == null) {
			height = gPopupUSERContainer.offsetHeight;
		}
		
		//var theBody = document.documentElement;
		var theBody = document.getElementsByTagName("BODY")[0];
		//theBody.style.overflow = "hidden";
		var scTop = parseInt(getScrollTop(),10);
		var scLeft = parseInt(theBody.scrollLeft,10);
	
		setMaskSizeUSER();
		
		//window.status = gPopupUSERMask.style.top + " " + gPopupUSERMask.style.left + " " + gi++;
		
		var titleBarHeight = parseInt(document.getElementById("popupUSERTitleBar").offsetHeight, 10);
		
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();
		
		gPopupUSERContainer.style.top = (scTop + ((fullHeight - (height+titleBarHeight)) / 2)) + "px";
		gPopupUSERContainer.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";
		//alert(fullWidth + " " + width + " " + gPopupUSERContainer.style.left);
	}
}
//addEvent(window, "resize", centerPopWinUSER);
//addEvent(window, "scroll", centerPopWinUSER);
//window.onscroll = centerPopWinUSER;


/**
 * Sets the size of the popup mask.
 *
 */
function setMaskSizeUSER() {
	var theBody = document.getElementsByTagName("BODY")[0];
			
	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();
	
	// Determine what's bigger, scrollHeight or fullHeight / width
	if (fullHeight > theBody.scrollHeight) {
		popHeight = fullHeight;
	} else {
		popHeight = theBody.scrollHeight;
	}
	
	if (fullWidth > theBody.scrollWidth) {
		popWidth = fullWidth;
	} else {
		popWidth = theBody.scrollWidth;
	}
	
	gPopupUSERMask.style.height = popHeight + "px";
	gPopupUSERMask.style.width = popWidth + "px";
}

/**
 * @argument callReturnFunc - bool - determines if we call the return function specified
 * @argument returnVal - anything - return value 
 */
function hidePopWinUSER(callReturnFunc) {
	gPopupUSERIsShown = false;
	var theBody = document.getElementsByTagName("BODY")[0];
	theBody.style.overflow = "";
	restoreTabIndexesUSER();
	if (gPopupUSERMask == null) {
		return;
	}
	gPopupUSERMask.style.display = "none";
	gPopupUSERContainer.style.display = "none";
	if (callReturnFunc == true && gReturnFuncUSER != null) {
		// Set the return code to run in a timeout.
		// Was having issues using with an Ajax.Request();
		gReturnValUSER = window.frames["popupUSERFrame"].returnVal;
		window.setTimeout('gReturnFuncUSER(gReturnValUSER);', 1);
	}
	gPopFrameUSER.src = gDefaultPageUSER;
	// display all select boxes
	if (gHideSelectsUSER == true) {
		displaySelectBoxes();
	}
}

/**
 * Sets the popup title based on the title of the html document it contains.
 * Uses a timeout to keep checking until the title is valid.
 */
function setPopTitleUSER() {
	return;
	if (window.frames["popupUSERFrame"].document.title == null) {
		window.setTimeout("setPopTitleUSER();", 10);
	} else {
		document.getElementById("popupUSERTitle").innerHTML = window.frames["popupUSERFrame"].document.title;
	}
}

// Tab key trap. iff popup is shown and key was [TAB], suppress it.
// @argument e - event - keyboard event that caused this function to be called.
function keyDownHandlerUSER(e) {
    if (gPopupUSERIsShown && e.keyCode == 9)  return false;
}

// For IE.  Go through predefined tags and disable tabbing into them.
function disableTabIndexesUSER() {
	if (document.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTagsUSER.length; j++) {
			var tagElements = document.getElementsByTagName(gTabbableTagsUSER[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				gTabIndexesUSER[i] = tagElements[k].tabIndex;
				tagElements[k].tabIndex="-1";
				i++;
			}
		}
	}
}

// For IE. Restore tab-indexes.
function restoreTabIndexesUSER() {
	if (document.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTagsUSER.length; j++) {
			var tagElements = document.getElementsByTagName(gTabbableTagsUSER[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				tagElements[k].tabIndex = gTabIndexesUSER[i];
				tagElements[k].tabEnabled = true;
				i++;
			}
		}
	}
}


/**
 * Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
 * IE has a problem with wanted select form tags to always be the topmost z-index or layer
 *
 * Thanks for the code Scott!
 */
function hideSelectBoxes() {
  var x = document.getElementsByTagName("SELECT");

  for (i=0;x && i < x.length; i++) {
    x[i].style.visibility = "hidden";
  }
}

/**
 * Makes all drop down form select boxes on the screen visible so they do not 
 * reappear after the dialog is closed.
 * 
 * IE has a problem with wanting select form tags to always be the 
 * topmost z-index or layer.
 */
function displaySelectBoxes() {
  var x = document.getElementsByTagName("SELECT");

  for (i=0;x && i < x.length; i++){
    x[i].style.visibility = "visible";
  }
}
/*Hàm refresh lại trang khi close modalwindow*/
function onClose_Refresh(returnVal){
	//alert("close");
    location.href = location.href;
}
