var root = "http://localhost/gs2int.com/";
var loading_image_small = "<img src=\""+ root +"images/loading_small.gif\" align=\"absmiddle\" \/>";
var popup_background_image = root +"images/bg_popup.png";

// ajax support
function getBrowserVer(){
	var cb;if(/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){var ffversion=new Number(RegExp.$1);if(ffversion>=3) cb='F3';else if(ffversion>=2) cb='F2';else if(ffversion>=1) cb='F1';}else if(/MSIE (\d+\.\d+);/.test(navigator.userAgent)){var ieversion=new Number(RegExp.$1);if(ieversion>=8) cb='IE8';else if(ieversion>=7) cb='IE7';else if(ieversion>=6) cb='IE6';else if(ieversion>=5) cb='IE5';}else if(/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)){var oprversion=new Number(RegExp.$1);if(oprversion>=10) cb='o10';else if(oprversion>=9) cb='o9';else if(oprversion>=8) cb='o8';else if(oprversion>=7) cb='o7';else cb='ot';}else{cb='ot';}return cb;}
	
function getXmlHttp(){
	var xmlhttp=false;try{xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(E){xmlhttp=false;}}if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){xmlhttp=new XMLHttpRequest();}return xmlhttp;}

// popup
function blockScreen(block) {
	if(block !== false) { 
		var block_screen = document.createElement("div");
			block_screen.setAttribute("id","block_screen");
			block_screen.style.position = "fixed";
			block_screen.style.top  = "0px";
			block_screen.style.left = "0px";
			block_screen.style.width  = "100%";
			block_screen.style.height = "100%";
			block_screen.style.backgroundColor = "#FFF";
			block_screen.style.zIndex = "100";
		
		var browser = getBrowserVer();
			if(browser.indexOf("IE") != -1) block_screen.style.filter = "alpha(opacity='10')";
			else block_screen.style.opacity = 0.1;
		
		document.body.appendChild(block_screen);
	} else { 
		if(document.getElementById("block_screen")) 
			document.body.removeChild(document.getElementById("block_screen"));
	}
}
function createCenterPosition() {
	var center_pos = document.createElement("div");
		center_pos.setAttribute("id","center_pos");
		center_pos.style.position = (getBrowserVer().indexOf('IE6') != -1) ? "absolute" : "fixed"; 
		center_pos.style.top  = "50%";
		center_pos.style.left = "50%";
		center_pos.style.width  = "0px";
		center_pos.style.height = "0px";
		center_pos.style.backgroundColor = "#F00";
		center_pos.style.zIndex = "150";
		
	document.body.appendChild(center_pos);
}
function createPopupWindows(header, w, h, cancel_btn) {
	var posY = (h != 0) ? parseInt(h/2) : 0;
	var posX = parseInt(w/2) + 14;
	
	blockScreen(true);
	createCenterPosition();
	
	if(document.getElementById("center_pos")) { 
		var pointer   = document.getElementById("center_pos");
		var container = document.createElement("div");
			container.style.position = "relative";
			container.style.top  = "0px";
			container.style.left = "0px";
			
		var message_box = document.createElement("div");
			message_box.setAttribute("id","message_box");
			message_box.style.position	= "absolute";
			message_box.style.top		= (h!=0) ? -posY +"px" : -100 +"px";
			message_box.style.left 		= -posX +"px";
			message_box.style.height 	= (h!=0) ? h +"px" : "auto";
			message_box.style.width 	= w +"px";
			message_box.style.padding 	= "7px";
			message_box.style.background = "transparent url("+ popup_background_image +") 0 0 repeat";
			
			var msgbox_header = document.createElement("div");
				msgbox_header.style.backgroundColor = "#526EA6";
				msgbox_header.style.color = "#FFF";
				msgbox_header.style.padding = "3px 10px 3px 10px";
				msgbox_header.style.border  = "1px solid #999";
				msgbox_header.style.font = "bold 16px Arial";
				msgbox_header.innerHTML  = header;
				message_box.appendChild(msgbox_header);
			
			var msgbox_content = document.createElement("div");
				msgbox_content.setAttribute("id","msgbox");
				msgbox_content.style.backgroundColor = "#FFF";
				msgbox_content.style.padding = "5px 10px 5px 10px";
				msgbox_content.style.border  = "solid #999";
				msgbox_content.style.borderWidth  = "0 1px 0 1px";
				
				var cancel_button = document.createElement("input");
					cancel_button.setAttribute("type","button");
					cancel_button.setAttribute("name","cancel_button");
					cancel_button.setAttribute("id","cancel_button");
					cancel_button.setAttribute("value","Cancel");
					cancel_button.style.backgroundColor = "#526EA6";
					cancel_button.style.color  = "#FFF";
					cancel_button.style.font  = "11px Verdana";
					cancel_button.style.padding = "3px 5px";
					cancel_button.style.border = "1px outset #526EA6";
					cancel_button.style.cursor = "pointer";	
					cancel_button.onclick = function(){removePointer();};
					
					if(cancel_btn) msgbox_content.appendChild(cancel_button);
					
				message_box.appendChild(msgbox_content);
				
			container.appendChild(message_box);
			pointer.appendChild(container);
	}
}
function dotpoint() { 
	var position_type = (getBrowserVer().indexOf('IE') != -1) ? "absolute" : "fixed"; 
	
	// create div element and give an ID attribute with 'dotpoint' value
	var dotpoint = document.createElement("div");
		dotpoint.setAttribute("id", "dotpoint");
		dotpoint.style.position = position_type;
		dotpoint.style.top 		= "50%";
		dotpoint.style.left 	= "50%";
		dotpoint.style.height 	= "0px";
		dotpoint.style.width 	= "0px";
		dotpoint.style.backgroundColor = "#F00";
		
		// create dotpoint_container
		// dotpoint_container is div element, set position with relative
		// content of it consist of main display of a page.
		var dotpoint_container = document.createElement("div");
			dotpoint_container.setAttribute("id", "dotpoint_container");
			dotpoint_container.style.position 	= "relative";
			dotpoint_container.style.height 	= "1px";
			dotpoint_container.style.width 		= "1px";
			
			// apply dotpoint_container into dotpoint element
			dotpoint.appendChild(dotpoint_container);
	
	// and pply dotpoint into document body
	document.body.appendChild(dotpoint);
}
function removePointer() { 
	(document.getElementById("center_pos")) ? document.body.removeChild(document.getElementById("center_pos")) : void(0);
	blockScreen(false);
}
function usePrompt(title, message, w, h) { 
	var width  = (typeof w == "undefined") ? 350 : w;
	var height = (typeof h == "undefined") ? 0 : w;
	
	createPopupWindows(title, width, height, false);
	if(document.getElementById("msgbox")) {
		var msgbox = document.getElementById("msgbox");
		msgbox.innerHTML = message;
		
		var textfield = document.createElement("input");
			textfield.setAttribute("type","text");
			textfield.setAttribute("name","numitem");
			textfield.setAttribute("id","numitem");
			textfield.setAttribute("size","50");
			msgbox.innerHTML += "<br \/>\n";
			msgbox.appendChild(textfield);

		var p = document.createElement("p");
			p.className = "btncontainer";
		
		var span = document.createElement('span');
			span.setAttribute("id","promptstat");
			span.style.marginRight = "5px";
			span.style.color = "#900";
			p.appendChild(span);
			
		var btn_ok = document.createElement("input");
			btn_ok.setAttribute("type","button");
			btn_ok.setAttribute("name","btn_ok");
			btn_ok.setAttribute("id","save_button");
			btn_ok.setAttribute("value","OK");
			p.appendChild(btn_ok);
			
		var btn_ccl = document.createElement("input");
			btn_ccl.setAttribute("type","button");
			btn_ccl.setAttribute("name","btn_ccl");
			btn_ccl.setAttribute("id","cancel_button");
			btn_ccl.setAttribute("value","Cancel");
			btn_ccl.onclick = function(){removePointer();}
			p.appendChild(btn_ccl);
			
		msgbox.appendChild(p);
		textfield.focus();
	}
}

// string format
function strReplace(str_search, str_replace, str_ori) { 
	var result = (str_ori) ? str_ori : "";
	var intIndexOfMatch = result.indexOf(str_search);
	while(intIndexOfMatch != -1){ 
		result = result.replace(str_search, str_replace);
		intIndexOfMatch = result.indexOf(str_search);
	}
	
	return result;
}


// Form
function createTextField(name, value, class) { 
	var name  = (name == "") ? "" : name;	
	var value = (value == "") ? "" : value;
	var class = (class == "") ? "general" : class;
	var textfield = document.createElement("input");
		textfield.setAttribute("name", name);
		textfield.setAttribute("type", "text");
		textfield.setAttribute("value", value);
		textfield.className = class;
	return textfield;
}

function resizeTextarea(t, def_line) { 
	var minline = (def_line == "") ? 2 : def_line;
	var strReplaceAll = t.value;
	var intIndexOfMatch = strReplaceAll.indexOf("\n");
	
	var counter = 1;
	while(intIndexOfMatch != -1) { 
		strReplaceAll = strReplaceAll.replace("\n", "[n]" );
		intIndexOfMatch = strReplaceAll.indexOf("\n");
		counter++;
	}
	
	if(counter >= minline) t.rows = counter;
}

function markTextField() {
	var inputs = document.getElementsByTagName("input");
		for(var i=0; i<inputs.length; i++) {
			inputs[i].onfocus = function(e) {
				this.parentNode.style.backgroundColor = "#e8f9a6";
				this.style.fontWeight = "bold";
			}
			inputs[i].onblur = function(e) {
				this.parentNode.style.backgroundColor = "#FFF";
				this.style.fontWeight = "normal";
				if((this.className == "numeric") && (this.value != "")) {
					var regex = new RegExp(/A-Za-z/);
						if(regex.test(this.value))
							this.value = "Ada";
						else this.value = "tdk ada";
				}
			}
		}
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if(obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if(obj.y) {
		curtop += obj.y;
	}
	return curtop;
}
