UTILITIES.js 4.13 KB
Newer Older
Thitichaipun Wutthisak committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

var DEFAULT_WIN_WIDTH = 600;
var DEFAULT_WIN_HEIGHT = 400;

function resizeImg(obj, max_width, max_height) {
	var x, y;

	var width = obj.naturalWidth;
	var height = obj.naturalHeight;

	if(!width && !height) {
		var img = new Image();
		img.src = obj.src;
		width = img.width;
		height = img.height;
	}

	if( !max_width && !max_height) {
		max_width = 300;
		max_height = 300;
	}

	if((width/height)< (max_width/max_height)) {
		// old image ratio < new image ratio
		x = "auto";
		y = max_height  +"px";
	} else  {
		// old image ratio >= new image ratio
		y = "auto";
		x = max_width +"px";
	}
	obj.style.width = x;
	obj.style.height = y;
	obj.style.visibility = "visible";
}

function setPageTitle( title ) {
	document.title = title;
}

function openWindow( url, winName, features) { 
	var OpenWindow;
	OpenWindow = window.open( url, winName, features);
	OpenWindow.focus();
	return OpenWindow;
}

// set properties for the editace window
function PopupCenter( url, winName, w, h) {
	var width = w || DEFAULT_WIN_WIDTH;
	var height = h || DEFAULT_WIN_HEIGHT;
	var left = ( screen.width ) ? ( screen.width-width )/2 : 0;
	var top = ( screen.height ) ? ( screen.height-height )/2 : 0;
	var OpenWindow;
	features = 'height=' + height + ',width=' + width + ',top=' + top + ',left=' + left + ', resizable=yes, toolbar=no , scrollbars=yes , status=yes';
	OpenWindow = window.open( url , winName , features);
	OpenWindow.focus();
	return OpenWindow;
}

function PopupCenterTop( url, winName, w, h) {
	var width = w || DEFAULT_WIN_WIDTH;
	var height = h || DEFAULT_WIN_HEIGHT;
	var left = ( screen.width ) ? ( screen.width-width )/2 : 0;
	var OpenWindow;
	features = 'height=' + height + ',width=' + width + ',top=30,left=' + left + ', resizable=yes, toolbar=no , scrollbars=yes , status=yes';
	OpenWindow = window.open( url , winName , features);
	OpenWindow.focus();
	return OpenWindow;
}
/*
function Tabs() { }
Tabs.init = function( tabListId ) {
  Tabs.tabLinks = document.getElementById(tabListId).getElementsByTagName("LI");

  var tabLink, tabId, tab;
  for (var i = 0; i < Tabs.tabLinks.length; i++) {
    tabLink = Tabs.tabLinks[i];
    tabId = tabLink.getAttribute("tabId");
    if (!tabId) {
		// tabId ไม่มี 
		alert("Expand link does not have a tabId element: " + tabLink.innerHTML);
	}
    tab = document.getElementById(tabId);
    if (!tab) {
		// ไม่มี tabContent
		alert("tabId does not exist: " + tabId);
	}

    if (i == 0) {
      tab.style.display = "block";  // for tab content
      tabLink.className = "current" ; // for tab bar
	  
	}else {
      tab.style.display = "none"; // for tab content
      tabLink.className = "" ; // for tab bar
    }

    tabLink.onclick = function() {
     var tabId = this.getAttribute("tabId");
     for (var i = 0; i < Tabs.tabLinks.length; i++) {
     	var tabLink = Tabs.tabLinks[i];
	    var loopId = tabLink.getAttribute("tabId");
	    if (loopId == tabId) {
	    	document.getElementById(loopId).style.display = "block";
	       tabLink.className = "current" ; 
		}else {
	    	document.getElementById(loopId).style.display = "none";
	        tabLink.className = "" ;
	    }
	}
	  if (this.blur) {
	  	this.blur();
	  }
	  return false;
	}

  }
}*/

// Example:
// value1 = 3; value2 = 4;
// format("text message %s and %s", value1, value2);
// this function will return the text "text message 3 and 4"
function format() {
	var msg = "";
	var argNum = 0;
	var startPos;
	var args = arguments;
	var numArgs = args.length;
	if(numArgs) {
		theStr = args[argNum++];
		startPos = 0;  
		endPos = theStr.indexOf("%s", startPos);
		if(endPos == -1) {
			endPos = theStr.length;
		}
		
		while(startPos < theStr.length) {
			msg += theStr.substring(startPos, endPos);
			if (argNum < numArgs) {
				msg += args[argNum++];
			}
			startPos = endPos+2;  
			endPos = theStr.indexOf("%s", startPos);
			if (endPos == -1) {
				endPos = theStr.length;
			}
		}
		if (!msg) {
			msg = args[0];
		}
	}
	return msg;
}

function preloadImages() {
	var allImgs = [];
	var imgs = document.getElementsByTagName("IMG");
	if(imgs) {
		for(k=0; k<imgs.length; k++) {
			var url = imgs[k];

			allImgs[k] = new Image();
			allImgs[k].src = url.src;
		}
	}
	return allImgs;
}