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;
}