String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g,'') }
String.prototype.unescHtml = function(){
	var i,e={'&lt;':'<','&gt;':'>','&amp;':'&','&quot;':'"'},t=this;
	for(i in e)
		t=t.replace(new RegExp(i,'g'),e[i]);
	return t
}

var lastMouseX;
var lastMouseY;
var curPopupWindow = null;
var helpWindow = null;

function setLastMousePosition(e) {
	if (navigator.appName.indexOf("Microsoft") != -1) e = window.event;
	lastMouseX = e.screenX;
	lastMouseY = e.screenY;
}

function openClickout(url) {
		// Taken out due to inconsistent escape results between java and javascript
        //window.open('/sales/linkout.jsp?url='+escape(url), "_blank", 'width=640,height=480,dependent=no,resizable=yes,toolbar=yes,status=yes,directories=yes,menubar=yes,scrollbars=1', false);
	window.open(url, "_blank", 'width=640,height=480,dependent=no,resizable=yes,toolbar=yes,status=yes,directories=yes,menubar=yes,scrollbars=1', false);
}

function openPopup(url, name, pWidth, pHeight, features, snapToLastMousePosition) {
   closePopup();
	if (snapToLastMousePosition) {
		if (lastMouseX - pWidth < 0) {
			lastMouseX = pWidth;
		}
		if (lastMouseY + pHeight > screen.height) {
			lastMouseY -= (lastMouseY + pHeight + 50) - screen.height;
		}
                lastMouseX -= pWidth;
                lastMouseY += 10;
		features +=	"screenX=" + lastMouseX + ",left=" + lastMouseX + "screenY=" + lastMouseY + ",top=" + lastMouseY;
	}
	curPopupWindow = window.open(url, name, features, true);

}

function closePopup() {
	if (curPopupWindow != null) {

		if (!curPopupWindow.closed) {
			curPopupWindow.close();
		}
		curPopupWindow = null;
	}
}


function openLookup(baseURL,modified,searchParam) {
	if (modified == '1') baseURL = baseURL + searchParam;
	openPopup(baseURL, "lookup", 350, 300, "width=430,height=300,toolbar=no,status=no,directories=no,menubar=no,resizable=yes,scrollable=no", true);
}


function lookupPick(formName, parentIdElementName, parentEditElementName, relatedFieldName, id, display, relatedFieldValue) {

	var parentIdElement = "document." + formName + "[\"" + parentIdElementName + "\"]";
	var parentEditElement = "document." + formName + "[\"" + parentEditElementName + "\"]";
	//var parentEditOldValueElement = "document." + formName + "[\"" + parentEditElementName + "_lkold" + "\"]";
	var relatedFieldElement = "document." + formName + "[\"" + relatedFieldName + "\"]";

	if (eval(parentIdElement + ".type") == "select-one") {
		var found = false;
		for (i = 0; i < eval(parentIdElement + ".options.length"); i++) {
			if (eval(parentIdElement + ".options[" + i + "].value") == id ) {
				eval(parentIdElement + ".selectedIndex=" + i);
				found = true;
				break;
			}
		}
		if (!found) {
			eval(parentIdElement + ".options[" + parentIdElement + ".options.length] = new Option('" + display + "','" + id + "')");
			eval(parentIdElement + ".selectedIndex=" + parentIdElement + ".options.length - 1");
		}
		eval(parentEditElement + ".value = " + "\"" + display +"\"");
		//eval(parentEditOldValueElement + ".value = " + "\"" + display +"\"");
	} else {
		eval(parentIdElement + ".value = " + "\"" + id +"\"");
		eval(parentEditElement + ".value = " + "\"" + display +"\"");
		//eval(parentEditOldValueElement + ".value = " + "\"" + display +"\"");
	}

	if (relatedFieldName.length > 0) {
		eval(relatedFieldElement + ".value = " + "\"" + relatedFieldValue +"\"");
	}

	/*var parentSubmitParam = parentEditElementName+"_lspf";
    var doPost = eval("document."+formName+"[\""+ parentSubmitParam + "\"].value");
   	if (doPost == '1') {
   		eval("document."+formName+".submit()");
	}*/
	closePopup();

	return false;
}

function pick(form,field,val) {
	eval("document."+form+"[\""+field+"\"].value=" + "\""+val+"\"");
   closePopup();
	return false;
}

function openCalendar(url) {
	openPopup(url, "Calendar", 189, 193, "width=189,height=193,dependent=no,resizable=no,toolbar=no,status=no,directories=no,menubar=no", true);
}

function openComboBox(url) {
	openPopup(replaceChar(url, ' ', '%'), "Select", 220, 270, "width=270,height=200,dependent=yes,resizable=yes,toolbar=no,status=no,directories=no,menubar=no,scrollbars=1", true);
}

ie = document.all?1:0
ns4 = document.layers?1:0

function CA(frmName){
	var frm = eval(frmName);
	for (var i=0;i<frm.elements.length;i++)
		{
		var e = frm.elements[i];
		if ((e.name != 'allbox') && (e.type=='checkbox'))
		{
			e.checked = frm.allbox.checked;
			if (frm.allbox.checked)
			hL(e);
			else
			dL(e);
		}
	}
}

function CCA(frmName,CB){
	var frm = eval(frmName);
	if (CB.checked)
		hL(CB);
	else
		dL(CB);
		var TB=TO=0;
		for (var i=0;i<frm.elements.length;i++)
		{
			var e = frm.elements[i];
			if ((e.name != 'allbox') && (e.type=='checkbox'))
			{
			TB++;
			if (e.checked)
			TO++;
		}
	}
	if (TO==TB)
	frm.allbox.checked=true;
	else
	frm.allbox.checked=false;
}

function hL(E){
	if (ie)
	{
		while (E.tagName!="TR")
		{E=E.parentElement;}
	}
	else
	{
		while (E.tagName!="TR")
		{E=E.parentNode;}
	}
	E.className = "H";
}

function dL(E){
	if (ie)
	{
		while (E.tagName!="TR")
		{E=E.parentElement;}
	}
	else
	{
		while (E.tagName!="TR")
		{E=E.parentNode;}
	}
	E.className = "";
}

function buttonOut(param)
	{
	param.borderColor="#C5C8D1";
	}

function buttonOver(param)
	{
	with (param)
		{
		borderLeftColor="#CCCCCC";
		borderTopColor="#CCCCCC";
		borderRightColor="#000000";
		borderBottomColor="#000000";
		}
	}

function buttonDown(param)
	{
	with (param)
		{
		borderLeftColor="#000000";
		borderTopColor="#000000";
		borderRightColor="#CCCCCC";
		borderBottomColor="#CCCCCC";
		}
	}

function updateFreq(val,ob){
	//val is freqid
	//ob is deliver time control
	/*
	id	label
	10	每日1次		10:00
	20	每日2次		10:00,12:00
	30	每日3次		10:00,12:00,14:00
	40	每日4次		10:00,12:00,14:00,16:00
	50	每两日1次		10:00
	60	每三日1次		10:00
	70	每周1次		10:00
	80	每两周1次		10:00
	90	每月1次		10:00
	//*/
	var timetable = [
					 {id:10,time:"10:00"}
					 ,{id:20,time:"10:00,12:00"}
					 ,{id:30,time:"10:00,12:00,14:00"}
					 ,{id:40,time:"10:00,12:00,14:00,16:00"}
					 ,{id:50,time:"10:00"}
					 ,{id:60,time:"10:00"}
					 ,{id:70,time:"10:00"}
					 ,{id:80,time:"10:00"}
					 ,{id:90,time:"10:00"}
					 ];
	//alert (val+ob);
	if (val == null || ob == null) return;

	var time = "";
	var l = timetable.length;
	for(var i=0;i<l;i++){
		var tt = timetable[i];
		if (tt.id == val){
			time = tt.time;
			break;
		}
	}

	ob.value = time;
}

// get previous/next non-text node
function previousElement(o) {
	if(o.previousSibling) { while (o.previousSibling.nodeType != 1) o = o.previousSibling; return o.previousSibling }
	else return false
}
function nextElement(o) {
	if(o.nextSibling) { while (o.nextSibling.nodeType != 1) o = o.nextSibling; return o.nextSibling }
	else return false
}
// styling functions
function isA(o,klass){ if(!o.className) return false; return new RegExp('\\b'+klass+'\\b').test(o.className) }
function addClass(o,klass){ if(!isA(o,klass)) o.className += ' ' + klass }
function rmClass(o,klass){ o.className = o.className.replace(new RegExp('\\s*\\b'+klass+'\\b'),'') }
function swapClass(o,klass,klass2){ var swap = isA(o,klass) ? [klass,klass2] : [klass2,klass]; rmClass(o,swap[0]); addClass(o,swap[1]) }
function getStyle(o,s) {
	if (document.defaultView && document.defaultView.getComputedStyle) return document.defaultView.getComputedStyle(o,null).getPropertyValue(s)
	else if (o.currentStyle) { return o.currentStyle[s.replace(/-([^-])/g, function(a,b){return b.toUpperCase()})] }
}
// shorter names for grabbing stuff
function $id(id){ return document.getElementById(id) }
function $tags(t,o){ o=o||document; return o.getElementsByTagName(t) }
function $tag(t,o,i) { o=o||document; return o.getElementsByTagName(t)[i||0] }
// get elements by class name, eg $c('post', document, 'li')
function $c(c,o,t) { o=o||document;
	if (!o.length) o = [o]
	else if(o.length == 1 && !o[0]) o = [o] // opera, you're weird
	var elements = []
	for(var i = 0, e; e = o[i]; i++) {
		if(e.getElementsByTagName) {
			var children = e.getElementsByTagName(t || '*')
			for (var j = 0, child; child = children[j]; j++) if(isA(child,c)) elements.push(child)
	}}
	return elements
}

function extend(dest, src) {
	for (var p in src) dest[p] = src[p]
	return dest
}

// get mouse pointer position
function pointerX(e) { return e.pageX || (e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft)) }
function pointerY(e) { return e.pageY || (e.clientY + (document.documentElement.scrollTop || document.body.scrollTop)) }

// get window size
function windowHeight() { return self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0 }
function windowWidth() { return self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 0 }

// get pixel position of an object
function getY(o){ var y = 0
	if (o.offsetParent) while (o.offsetParent) { y += o.offsetTop; o = o.offsetParent }
	return y
}
function getX(o){ var x = 0
	if (o.offsetParent) while (o.offsetParent) { x += o.offsetLeft; o = o.offsetParent }
	return x
}

// form stuff
function getRadioValue(o) {
	for(var i = 0, r; r = o[i]; i++) if (r.checked && r.value) return r.value
	return false
}

// todo: make less crap
function resizeToText(o, text, margin) {
	margin = margin || 0
	var c = $id(o.id + '-copy')
	if (!c) { makeResizeThing(o); c = $id(o.id + '-copy') }
	var esc = {'<':'[','>':']',' ':'&nbsp;'}
	for(var i in esc) text=text.replace(new RegExp(i,'g'), esc[i])
	c.innerHTML = text
	o.style.width = c.offsetWidth + margin + 'px'
}
function makeResizeThing(src) {
	var o = document.createElement('div')
	o.style.position = 'absolute'; o.style.top = o.style.left = 0
	o.style.visibility = 'hidden'
	o.style.fontSize = getStyle(src, 'font-size')
	o.style.fontFamily = getStyle(src, 'font-family')
	o.id = src.id + '-copy'
	src.parentNode.appendChild(o)
}

// event functions
function falseFunc(){ return false }
function addLoadEvent(f) { var old = window.onload
	if (typeof old != 'function') window.onload = f
	else { window.onload = function() { old(); f() }}
}

function mailer(oName,oDomain) {
 email="mailto:" + oName + "@" + oDomain;
 window.location=email;
}

document.write("<script type=\"text/javascript\" src=\"/scripts/Ajax.js\"></script>");

Cookies = {
	set : function(name,value,days){ var expires = '', days = days||30
		var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); expires = "; expires="+date.toGMTString()
		document.cookie = name+"="+value+expires+"; path=/"
	},
	get : function(name){
		var ca = document.cookie.split(';'),i,c; name += '='
		for(i=0; i < ca.length; i++) {
			c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length)
			if (c.indexOf(name) == 0) return c.substring(name.length,c.length)
		}
		return null
}}


