﻿<!-- javascript trim function -->
<!-- trim() is a new method for String object -->
String.prototype.trim = function () {
	//return replaceAll(this.replace(/^\s+|\s+$/g, ""), "　", "");
	return this.replace(/^\s+|\s+$/g, "").replace(/^　+|　+$/g, "");
}

window.IE = function(){
	if(navigator.userAgent.indexOf("MSIE") > 0)
		return true;
	else return false;
}
window.Mozilla = function() {
	if(navigator.userAgent.indexOf("Gecko") > 0)
		return true;
	else return false;
}
window.Netscape = function() {
	if(navigator.userAgent.indexOf("Gecko") > 0 && navigator.userAgent.indexOf("Netscape") > 0)
		return true;
	else return false;
}
window.Firefox = function() {
	if(navigator.userAgent.indexOf("Gecko") > 0 && navigator.userAgent.indexOf("Firefox") > 0)
		return true;
	else return false;
}
window.Opera = function() {
	if(navigator.userAgent.indexOf("Opera") > 0)
		return true;
	else return false;
}

/**
	disable right button
*/
if(window.IE()) {
	document.oncontextmenu = nocontextmenu; // for IE5+
	document.onmouseup = mouseup;
}

function nocontextmenu() {
	event.cancelBubble = true;
	event.returnValue = false;
	return false;
}
function mouseup() {
	mouseover = false;
}

/**
	in post, url may be -1
  */
function setDefaultCursor(obj, url) {
	if(url == '#' || url == 'javascript://' || url == '' || url == -1) {
		obj.style.cursor = 'default';
		return;
	}
}

function IsLeapYear(yr) {
	if      (yr % 4 != 0)   return false;
	else if (yr % 400 == 0) return true;
	else if (yr % 100 == 0) return false;
	else                    return true;
}

function MaxDayOfMonth(mn, yr) {
	var mDay;
	if((mn == 4) || (mn == 6) || (mn == 9) || (mn == 11)) {
		mDay = 30;
	}
	else if(mn == 2) {
		mDay = IsLeapYear(yr) ? 29 : 28;
	}
	else {
		mDay = 31;
	}
	return mDay;
}

function TextCounter(objField, maxLength) {
	var maxlimit = maxLength;
	if (objField.value.length > maxlimit) // if too long...trim it!
		objField.value = objField.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
}

function ReplaceSpecialChar(DesiredStr) {
	//hardcoded check ampersand and number sign
	var FinalStr = replaceAll(DesiredStr, '&', '%26');
	FinalStr = replaceAll(FinalStr, '#', '%23');
	return FinalStr;
}

function doPostBack(objFrm, eventTarget, eventArgument) {
	objFrm.__EVENTTARGET.value = eventTarget;
	objFrm.__EVENTARGUMENT.value = eventArgument;
	objFrm.submit();
}

function CreateHtmlForm(id, name, method, action, encodetype) {
	var objfrm = document.createElement("form");
	objfrm.id = id;
	objfrm.name = name;
	objfrm.method = method;
	objfrm.action = action;
	if(encodetype == '')
		objfrm.enctype = 'application/x-www-form-urlencoded';
	else objfrm.enctype = encodetype;
	return objfrm;
}

function CreateHtmlInput(id, name, value, input_type) {
	var dynamic_obj = document.createElement("input");
	dynamic_obj.id = id;
	dynamic_obj.name = name;
	dynamic_obj.type = input_type;
	dynamic_obj.value = value;
	return dynamic_obj;
}

function replaceAll(oldStr, findStr, repStr) {
  var srchNdx = 0;  // srchNdx will keep track of where in the whole line
                    // of oldStr are we searching.
  var newStr = "";  // newStr will hold the altered version of oldStr.
  while (oldStr.indexOf(findStr,srchNdx) != -1)  
                    // As long as there are strings to replace, this loop
                    // will run. 
  {
    newStr += oldStr.substring(srchNdx,oldStr.indexOf(findStr,srchNdx));
                    // Put it all the unaltered text from one findStr to
                    // the next findStr into newStr.
    newStr += repStr;
                    // Instead of putting the old string, put in the
                    // new string instead. 
    srchNdx = (oldStr.indexOf(findStr,srchNdx) + findStr.length);
                    // Now jump to the next chunk of text till the next findStr.
  }
  newStr += oldStr.substring(srchNdx,oldStr.length);
                    // Put whatever's left into newStr.
  return newStr;
}
