var CommonFunc = new Object();

CommonFunc.NORMAIL = top.window.location.href;
CommonFunc.BasePath = CommonFunc.NORMAIL.substring(0, CommonFunc.NORMAIL.lastIndexOf('/')+1);

function clearErrors() {
	var spans = document.body.all.tags("SPAN");
	for(i = 0;i< spans.length;i++){
		if(spans[i].id.indexOf("Error",0)>0){
			spans[i].innerText = "";
		}
	}
}

function trimString(str){
    str = this!=window?this:str;
    return str.replace(/^\s+/g,'').replace(/\s+$/g,'');
}

String.prototype.trim = trimString;

 // return 1000500 to 1,000,500
function NumberToMoneyStr(aNumber,aFixLen,aDigital) {
	var lblankStr = "                                               ";
	var lstr = "" + aNumber;
	var lFixLen = aFixLen == null ? 1 : ( aFixLen > 20 ? 20 : aFixLen)
	var ldigital = aDigital == null ? 3 : aDigital	

	var lbase = lstr.length % ldigital;
	var ldstr = (lbase >0) ? lstr.substr( 0, lbase) : "";	
	var lneed = true;
	if(lbase == 0 || (lbase ==1 && ldstr == "-")) {
		lneed = false;
	}
	
	for(var i = lbase; i < lstr.length; i +=ldigital) {
		ldstr += (lneed ? "," : "") + lstr.substr(i,ldigital);
		lneed = true;
	}
	
	if (ldstr.length < lFixLen) {
		ldstr = lblankStr.substr(0,lFixLen - ldstr.length) + ldstr;
	}
	return ldstr;	 
}

 // 1,000,500 to 1000500
function MoneyStrToNumber(aStr,aDigital) {
	if(aStr == null || aStr == "") return 0;
	var lstr = aStr.trim()	
	if(lstr == "" ) return 0;
	lstr = aStr.replace(/,/g,'')
	
	var lint = parseInt(lstr);
	if(isNaN(lint)) lint = -1;
	return lint;
}

 // return aStr like 2001-03-05
function DateToString(aDate) {
	if(aDate == null || aDate.constructor != Date) return "";
//	aDate instanceof Date
	try{
		var ys = aDate.getFullYear();
		var ms = aDate.getMonth() + 1;
		var ds = aDate.getDate();
		
		var s = "" + ys + "-";
		
		if(ms<10) s += "0";
		s += ms +"-"
		
		if(ds<10) s +="0";
		s +=ds
		
		return s
	
	}catch(e){
		return "";
	}
}

 // aStr like 2001-03-05, return a DateObj
function StringToDate(aStr) {
	var y = aStr.indexOf("-")
	if( y <=0 ) return null;
	var ys = parseInt(aStr.substr(0,y), 10);
	
	var m =  aStr.indexOf("-", y + 1);
	if( m <= y + 1 ) return null;
	var ms = parseInt(aStr.substr(y+1, m - y -1), 10);
	
	var ds = parseInt(aStr.substr(m + 1) , 10);

	if( isNaN(ys) || isNaN(ms) || isNaN(ds) ) 	return null;

	if( ys < 1970 || ys > 9999 ||
	    ms < 1    || ms > 12   ||
	    ds < 1    || ds > 31      ) return null;
	    
	if( ds > 28 ){
		if(ds == 31 ) {
			if( ms == 2 || ms == 4 || ms ==6 || ms == 9 || ms == 11 ) return null;
		} else if( ds == 30 ){
			if( ms == 2 ) return null;
		} else {
			if( ms == 2 ){
				if( ys % 4 == 0 ) {
					if( ys % 100 == 0 && ys % 400 != 0){
						return null;
					}
					// yes...
				}else{
					return null;
				}		
			}
		}	
	}
	return new Date(ys, ms - 1, ds);
}

 // return "" or like 2001-03-05
function StrToDateStr(aStr) {
	if(aStr == null || aStr == "") return "";
	
	var y = aStr.indexOf("-");
	if( y <=0 ) return "";
	var ys = parseInt(aStr.substr(0,y), 10);
	
	var m =  aStr.indexOf("-", y + 1);
	if( m <= y + 1 ) return "";
	var ms = parseInt(aStr.substr( y + 1, m - y - 1), 10);
	
	var ds = parseInt(aStr.substr(m + 1), 10);

	if( isNaN(ys) || isNaN(ms) || isNaN(ds) ) 	return "";

	if( ys < 1970 || ys > 9999 ||
	    ms < 1    || ms > 12   ||
	    ds < 1    || ds > 31      ) return "";
	    
	if( ds > 28 ){
		if(ds == 31 ) {
			if( ms == 2 || ms == 4 || ms ==6 || ms == 9 || ms == 11 ) return "";
		} else if ( ds == 30 ){
			if( ms == 2 ) return "";
		} else {
			if( ms == 2 ){
				if( ys % 4 == 0 ) {
					if( ys % 100 == 0 && ys % 400 != 0){
						return "";
					}
					// yes...
				}else{
					return "";
				}
			}
		}
	}
	
	var s = "" + ys + "-";
	if(ms<10) s += "0";
	s +=ms +"-";
	if(ds<10) s +="0";
	s +=ds;
	return s;
}

 // is aStr like 2001-03-05, return true of false;
function StringIsDate(aStr) {
	if(aStr == null || aStr == "") return false;
	
	var y = aStr.indexOf("-")
	if( y <=0 ) return false;
	var ys = parseInt(aStr.substr(0,y), 10);
	
	var m =  aStr.indexOf("-", y + 1)
	if( m <= y + 1 ) return false;
	var ms = parseInt(aStr.substr(y+1, m - y - 1), 10);
	
	var ds = parseInt(aStr.substr(m + 1), 10);

	if( isNaN(ys) || isNaN(ms) || isNaN(ds) ) 	return false;

	if( ys < 1970 || ys > 9999 ||
	    ms < 1    || ms > 12   ||
	    ds < 1    || ds > 31   ) 
	    return false;
	    
	if( ds > 28 ){
		if(ds == 31 ) {
			if( ms == 2 || ms == 4 || ms ==6 || ms == 9 || ms == 11 ) return false;
		} else if( ds == 30 ) {
			if( ms == 2 ) return false;
		} else {
			if( ms == 2 ){
				if( ys % 4 == 0 ) {
					if( ys % 100 == 0 && ys % 400 != 0){
						return false;
					}
					// yes...
				}else{
					return false;
				}
			}
		}
	}
	return true;
}
/**
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/
function setMansuoCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

/**
 * name - name of the desired cookie
 * return string containing value of specified cookie or null
 * if cookie does not exist
*/
function getMansuoCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

var _Win_Open_Options = "toolbar=no,scrollbars=0,status=no,resizable=0,dependent=0" ;
function link4(url,w,h) {	
	var mw = w?w:500;
	var mh = h?h:300;
	window.open(url, '', _Win_Open_Options+', width='+mw+', height='+mh+', top='+(screen.availHeight/2-mh/2)+', left='+(screen.availWidth/2-mw/2)+' ');
}

function link3(url,w,h) {
	window.open(url, '', 'toolbar=0, scrollbars=0, resizable=1, width='+w+', height='+h+', top='+(screen.availHeight/2-h/2)+', left='+(screen.availWidth/2-w/2)+' ');
}

function link2(url,w,h) {
	window.open(url, '', 'toolbar=0, scrollbars=1, resizable=1, width='+w+', height='+h+', top='+(screen.availHeight/2-h/2)+', left='+(screen.availWidth/2-w/2)+' ');
}

function link(url,w,h) {
	window.open(url, '', 'toolbar=0, scrollbars=0, resizable=0, width='+w+', height='+h+', top='+(screen.availHeight/2-h/2)+', left='+(screen.availWidth/2-w/2)+' ');
}

function linkMem(url,mem,w,h) {
	var win = window.open(url, 'mem', 'toolbar=0, scrollbars=0, resizable=1, width='+w+', height='+h+', top='+(screen.availHeight/2-h/2)+', left='+(screen.availWidth/2-w/2)+' ');
	win.focus();
}

function checkAll(form, itemName, checked) {
    for (var i=0; i<form.elements.length; i++) {
        var e = form.elements[i];
        if (e.name == itemName) {
            if (e.type == "select-multiple") {
                for(var j=0; j<e.length; j++) {
                    var e1 = e.options[j];
                    e1.selected = checked;
                }
            } else if (e.type == "checkbox") {
                e.checked = checked;
            }
        }
    }
}

/**
 * Jerry Tang 2005年5月25日
 * 判断传入的数据是否为数字, 并且小数点为特定位数,
 * 比如:checkNumberDigit("10.33",1);就是判断10.33是不是数字, 而且是不是精确到小数点后1位
*/
function checkNumberDigit(num,digit) {
	if(isNaN(num)){
		return false;
	}
	if(num.indexOf(".")>-1 && (num.length-num.indexOf(".")-1)>digit){
		return false;
	}
	return true;
}

/**
 * Test time style:  hh:mm
*/
function checkHourTime(time) {
	if(time==null || typeof(time)==undefined){
		return false;
	}
	var ddTime = time;
	var tm = ddTime.split(":");
	if(tm.length!=2){
		return false;
	}
	if(isNaN(tm[0]) ||tm[0].indexOf(".")>-1 || parseInt(tm[0])<0 || parseInt(tm[0])>24){
		return false;
	}
	
	if(isNaN(tm[1]) ||tm[1].indexOf(".")>-1 || parseInt(tm[1])<0 || parseInt(tm[1])>60){
		return false;
	}
	
	return true;
}

function openWindow(url, width, height) {
    window.open(url, "", "toolbar=0,scrollbars=1,status=0,resizable=1,width="+width+",height="+height+"");
}

/**
 * 打开用户信息窗口
*/
function openUserWindow(url, w, h) {
	var mw = w?w:500;
	var mh = h?h:300;
	link4(url, mw, mh);
}

function openUserProfileWindow(userId) {
	var url = CommonFunc.BasePath + "/base/actions/profile.do?method=showUser&id=" + userId;
	link4(url);
}

/**
 * 打开项目信息窗口
*/
function openProjectWindow(projectId) {
	var url = CommonFunc.BasePath + "/project/actions/projectDTL.do?method=showDTL&popup=true&id="+projectId;
	link4(url, 800, 600);
}

/**
 * 打开客户信息窗口
*/
function openCustomerWindow(customerId) {
	var url = CommonFunc.BasePath + "/satisfy/actions/loadCustomer.action?popup=true&customerId="+customerId;
	link4(url, 800, 600);
}

/**
 * 打开对话框窗口
*/
function openDialogWindow(url,w,h) {
	var mw = w?w:500;
	var mh = h?h:300;
	var sFeatures = "help:no;dialogWidth:"+mw+"px;dialogHeight:"+mh+"px;";
    var mxh1 = new Array();
    mxh1["url"] = url;
	var dialogUrl = CommonFunc.BasePath + "/include/dialog-iframe.jsp";
    var dialog = window.showModalDialog(dialogUrl, mxh1, sFeatures);
    return dialog;
}

/**
 * 打开打印对话框
 * @param url
 * @param w
 * @param h
 * @return
 */
function openPrintWindow(url, w, h) {
	var mw = w?w:800;
	var mh = h?h:600;
	var style = 'toolbar=0,scrollbars=1,resizable=1,width='+mw+',height='+mh+',top='+(screen.availHeight/2-mh/2)+',left='+(screen.availWidth/2-mw/2);
	window.open(url, '', style);
}

/**
 * 打开弹出对话框
 * @param url
 * @param w
 * @param h
 * @return
 */
function openPopupWindow(url, w, h) {
	var mw = w?w:800;
	var mh = h?h:600;
	var style = 'toolbar=0,scrollbars=0,resizable=1,width='+mw+',height='+mh+',top='+(screen.availHeight/2-mh/2)+',left='+(screen.availWidth/2-mw/2);
    window.open(url, "", style);
}

/**
 * 选中所有的checkbox
 * @param form
 * @param itemName
 * @param checked
 * @return
 */
function checkAll(form, itemName, checked) {
    for (var i=0; i<form.elements.length; i++) {
        var e = form.elements[i];
        if (e.name == itemName) {
            if (e.type == "select-multiple") {
                for(var j=0; j<e.length; j++) {
                    var e1 = e.options[j];
                    e1.selected = checked;
                }
            } else if (e.type == "checkbox") {
                e.checked = checked;
            }
        }
    }
}

/**
  StringBuffer 类的创建
  在IE6，IE7下测试使用StringBuffer比使用加号确实可以节省50%~66%的时间，在IE8下几乎没什么影响(测试结果不是很稳定)，
  但在火狐FF3.5上,Opera浏览器上，chrome浏览器上结果却刚好相反，“+”运算明显要高于我们写的StringBuffer方法，而且也是50%的差距
  所以，当我们处理javascript字符串的连接优化的时候，一定要区分浏览器。
*/
function StringBuffer () {
  this._strings_ = new Array();
}

StringBuffer.prototype.append = function(str) {
  this._strings_.push(str);
};

StringBuffer.prototype.toString = function() {
  return this._strings_.join("");
};

/**
 * 日期格式化 Y-m-d 输出 2010-02-03
 */
Date.prototype.format = function(format) {
	var returnStr = '';
	var replace = Date.replaceChars;
	for (var i = 0; i < format.length; i++) {
		var curChar = format.charAt(i);
		if (replace[curChar]) {
			returnStr += replace[curChar].call(this);
		} else {
			returnStr += curChar;
		}
	}
	return returnStr;
};

Date.replaceChars = {
	shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
	longMonths: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
	shortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
	longDays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
	
	// Day
	d: function() { return (this.getDate() < 10 ? '0' : '') + this.getDate(); },
	D: function() { return Date.replaceChars.shortDays[this.getDay()]; },
	j: function() { return this.getDate(); },
	l: function() { return Date.replaceChars.longDays[this.getDay()]; },
	N: function() { return this.getDay() + 1; },
	S: function() { return (this.getDate() % 10 == 1 && this.getDate() != 11 ? 'st' : (this.getDate() % 10 == 2 && this.getDate() != 12 ? 'nd' : (this.getDate() % 10 == 3 && this.getDate() != 13 ? 'rd' : 'th'))); },
	w: function() { return this.getDay(); },
	z: function() { return "Not Yet Supported"; },
	// Week
	W: function() { return "Not Yet Supported"; },
	// Month
	F: function() { return Date.replaceChars.longMonths[this.getMonth()]; },
	m: function() { return (this.getMonth() < 9 ? '0' : '') + (this.getMonth() + 1); },
	M: function() { return Date.replaceChars.shortMonths[this.getMonth()]; },
	n: function() { return this.getMonth() + 1; },
	t: function() { return "Not Yet Supported"; },
	// Year
	L: function() { return (((this.getFullYear()%4==0)&&(this.getFullYear()%100 != 0)) || (this.getFullYear()%400==0)) ? '1' : '0'; },
	o: function() { return "Not Supported"; },
	Y: function() { return this.getFullYear(); },
	y: function() { return ('' + this.getFullYear()).substr(2); },
	// Time
	a: function() { return this.getHours() < 12 ? 'am' : 'pm'; },
	A: function() { return this.getHours() < 12 ? 'AM' : 'PM'; },
	B: function() { return "Not Yet Supported"; },
	g: function() { return this.getHours() % 12 || 12; },
	G: function() { return this.getHours(); },
	h: function() { return ((this.getHours() % 12 || 12) < 10 ? '0' : '') + (this.getHours() % 12 || 12); },
	H: function() { return (this.getHours() < 10 ? '0' : '') + this.getHours(); },
	i: function() { return (this.getMinutes() < 10 ? '0' : '') + this.getMinutes(); },
	s: function() { return (this.getSeconds() < 10 ? '0' : '') + this.getSeconds(); },
	// Timezone
	e: function() { return "Not Yet Supported"; },
	I: function() { return "Not Supported"; },
	O: function() { return (-this.getTimezoneOffset() < 0 ? '-' : '+') + (Math.abs(this.getTimezoneOffset() / 60) < 10 ? '0' : '') + (Math.abs(this.getTimezoneOffset() / 60)) + '00'; },
	P: function() { return (-this.getTimezoneOffset() < 0 ? '-' : '+') + (Math.abs(this.getTimezoneOffset() / 60) < 10 ? '0' : '') + (Math.abs(this.getTimezoneOffset() / 60)) + ':' + (Math.abs(this.getTimezoneOffset() % 60) < 10 ? '0' : '') + (Math.abs(this.getTimezoneOffset() % 60)); },
	T: function() { var m = this.getMonth(); this.setMonth(0); var result = this.toTimeString().replace(/^.+ \(?([^\)]+)\)?$/, '$1'); this.setMonth(m); return result;},
	Z: function() { return -this.getTimezoneOffset() * 60; },
	// Full Date/Time
	c: function() { return this.format("Y-m-d") + "T" + this.format("H:i:sP"); },
	r: function() { return this.toString(); },
	U: function() { return this.getTime() / 1000; }
};