function isAlien(a) {
   return isObject(a) && typeof a.constructor != 'function';
}

function isArray(a) {
    return isObject(a) && a.constructor == Array;
}

function isBoolean(a) {
    return typeof a == 'boolean';
}

function isEmpty(o) {
    var i, v;
    if (isObject(o)) {
        for (i in o) {
            v = o[i];
            if (isUndefined(v) && isFunction(v)) {
                return false;
            }
        }
    }
    return true;
}

function openWin(url, id, width, height, scrollbar) {
	if (height > screen.height-80) {
		ts = 0;
		height = screen.height - 80;
	} else {
		ts = Math.round((screen.height-height)/2-50);
	}
	ls = Math.round((screen.width-width)/2);
	params = "width="+width+",height="+height+",top="+ts+",left="+ls+",status=no,toolbar=no,menubar=no,resizable=yes,scrollbars="+scrollbar;
	var my_win = window.open(url, id, params);
	if (my_win.opener == null) my_win.opener = self;
	my_win.focus();
}

function isFunction(a) {
    return typeof a == 'function';
}

function isNull(a) {
    return typeof a == 'object' && !a;
}

function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}

function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}

function isString(a) {
    return typeof a == 'string';
}

function isUndefined(a) {
    return typeof a == 'undefined';
}

function clone(obj) {

	if(!isObject(obj) || obj == null) {
		return obj
	}

	var ret = new Object
	
	for(var i in obj) {
		ret[i] = clone(obj[i])
	}

	return ret
}

if(typeof String.prototype.strReplace == 'undefined') {
	String.prototype.strReplace = function(s, r) {

		var i = this.indexOf(s);
		if(i < 0) {
			return this;
		}

		var ret = '';
		
		ret += this.substr(0, i) + r;

		var tailStart = i + s.length
		var tailLength = this.length - tailStart + 1

		if(tailLength > 0) {
			ret += this.substr(tailStart, tailLength).strReplace(s, r)
		}

		return ret
	}
}

if(typeof String.prototype.ltrim == 'undefined') {
  String.prototype.ltrim = function() {
    return this.replace(/^\s+/, '');
  }
}

if(typeof String.prototype.rtrim == 'undefined') {
  String.prototype.rtrim = function() {
    return this.replace(/\s+$/, '');
  }
}

if(typeof String.prototype.trim == 'undefined') {
  String.prototype.trim = function() {
    return this.replace(/^\s+/, '').replace(/\s+$/, '');
  }
}

if(typeof String.prototype.urlEncode == 'undefined') {
	String.prototype.urlEncode = function() {
		var trans = [];

		for(var i = 0x410; i <= 0x44F; i++) {
			trans[i] = i - 0x350;
		}

		trans[0x401] = 0xA8;
		trans[0x451] = 0xB8;

		var ret = [];

		for(var i = 0; i < this.length; i++) {

			var n = this.charCodeAt(i);

			if(typeof trans[n] != 'undefined') {
				n = trans[n];
			}

			if(n <= 0xFF) {
				ret.push(n);
			}

		}
		return window.escape(String.fromCharCode.apply(null, ret));
	}
}

if(typeof String.prototype.urlDecode == 'undefined') {
	String.prototype.urlDecode = function() {
		var trans = [];

		for(var i = 0xC0; i <= 0xFF; i++) {
			trans[i] = i + 0x350;
		}

		trans[0xA8] = 0x401;
		trans[0xB8] = 0x451;

		var str = window.unescape(this)

		var ret = []

		for(var i = 0; i < str.length; i++) {

			var n = str.charCodeAt(i);

			if(typeof trans[n] != 'undefined') {
				n = trans[n];
			}

			ret.push(n)
		}

		return String.fromCharCode.apply(null, ret);
	}
}

if(typeof Array.prototype.search == 'undefined') {
	Array.prototype.search = function(value) {
		for(var i in this) {
			if(this[i] == value) {
				return i
			}
		}

		return -1
	}
}

function getSelectDefaultIndex(selectElem) {
	for(var i = 0; i < selectElem.options.length; i++) {
		if(typeof selectElem.options[i].defaultSelected != 'undefined') {
			if(selectElem.options[i].defaultSelected) {
				return i
			}
		}
	}

	return 0
}

function setSelectSelectedIndexByValue(selectElem, value) {
	for(var i = 0; i < selectElem.options.length; i++) {
		if(selectElem.options[i].value == value) {
			selectElem.selectedIndex = i
			return i
		}
	}

	return -1
}

function getSelectIndexByValue(selectElem, value) {
	for(var i = 0; i < selectElem.options.length; i++) {
		if(selectElem.options[i].value == value) {
			return i
		}
	}

	return -1
}

function addEventHandler(targetElement, type, listener, capture) {	

	if(capture = null) {
		capure = false
	}

	var ret = true

	if(targetElement.addEventListener) {
		targetElement.addEventListener(type, listener, capture)
	}
	else if(targetElement.attachEvent) {
		targetElement.attachEvent('on'+type, listener)
	}
	else {
		ret = false
	}

	return ret
}

function foo(px,py,pw,ph,baseElement,fid)
{
		var win = document.getElementById(this.fid);
}


function dropdown_menu_hack(el)
{
	if(el.runtimeStyle.behavior.toLowerCase()=="none"){return;}
	el.runtimeStyle.behavior="none";

	var ie5 = (document.namespaces==null);
	el.ondblclick = function(e)
	{
		window.event.returnValue=false;
		return false;
	}
	
	if(window.createPopup==null)
	{
		
		var fid = "dropdown_menu_hack_" + Date.parse(new Date());
	
		window.createPopup = function()
		{
			if(window.createPopup.frameWindow==null)
			{
					el.insertAdjacentHTML("AfterEnd","<iframe   id='"+fid+"' name='"+fid+"' src='about:blank'  frameborder='1' scrolling='no'></></iframe>");
					var f = document.frames[fid];
					f.document.open();
					f.document.write("<html><body></body></html>");
					f.document.close();
					f.fid = fid; 
					

					var fwin = document.getElementById(fid);
					fwin.style.cssText="position:absolute;top:0;left:0;display:none;z-index:99999;";
				
					
					f.show = function(px,py,pw,ph,baseElement)
					{							
						py = py + baseElement.getBoundingClientRect().top + Math.max( document.body.scrollTop, document.documentElement.scrollTop) ;
						px = px + baseElement.getBoundingClientRect().left + Math.max( document.body.scrollLeft, document.documentElement.scrollLeft) ;
						fwin.style.width = pw + "px";
						fwin.style.height = ph + "px";						
						fwin.style.posLeft =px ;
						fwin.style.posTop = py ;		
						fwin.style.display="block";						
					}

					
					f_hide = function(e)
					{ 
						if(window.event && window.event.srcElement	&& window.event.srcElement.tagName && window.event.srcElement.tagName.toLowerCase()=="select"){return true;}
						fwin.style.display="none";
					} 
					f.hide = f_hide;
					document.attachEvent("onclick",f_hide);		
					document.attachEvent("onkeydown",f_hide);		
					
			}
			return f;
		}
	}

	function showMenu()
	{
		
		function selectMenu(obj)
			{				
				var o = document.createElement("option");
				o.value = obj.value;
				o.innerHTML = obj.innerHTML;			
				while(el.options.length>0){el.options[0].removeNode(true);}
				el.appendChild(o);
				el.title =  o.innerHTML; 
				el.contentIndex = obj.selectedIndex  ;
				el.menu.hide(); 				
			}		
		
		
		el.menu.show(0 , el.offsetHeight , 10,  10, el); 
		var mb = el.menu.document.body;
		
		mb.style.cssText ="border:solid 1px black;margin:0;padding:0;overflow-y:auto;overflow-x:auto;background:white;text-aligbn:center;font-family:Verdana;font-size:12px;";
		var t = el.contentHTML;
		t = t.replace(/<select/gi,'<ul');
		t = t.replace(/<option/gi,'<li');
		t = t.replace(/<\/option/gi,'</li');
		t = t.replace(/<\/select/gi,'</ul');
		mb.innerHTML = t;	
	
		
		el.select = mb.all.tags("ul")[0];
		el.select.style.cssText="list-style:none;margin:0;padding:0;";
		mb.options = el.select.getElementsByTagName("li");
		
		for(var i=0;i<mb.options.length;i++)
		{
			mb.options[i].selectedIndex = i;
			mb.options[i].style.cssText = "list-style:none;margin:0;padding:1px 2px;width/**/:100%;cursor:hand;cursor:pointer;white-space:nowrap;"
			mb.options[i].title =mb.options[i].innerHTML;
			mb.options[i].innerHTML ="<nobr>" + mb.options[i].innerHTML + "</nobr>";
			mb.options[i].onmouseover = function()
				{
					if( mb.options.selected ){mb.options.selected.style.background="white";mb.options.selected.style.color="black";}
					mb.options.selected = this;
					this.style.background="#333366";this.style.color="white";
				}
			
			mb.options[i].onmouseout = function(){this.style.background="white";this.style.color="black";}
			mb.options[i].onmousedown = function(){selectMenu(this);	}
			mb.options[i].onkeydown = function(){selectMenu(this);	}
				

			if(i == el.contentIndex)
			{
				mb.options[i].style.background="#333366";
				mb.options[i].style.color="white";	
				mb.options.selected = mb.options[i];
			}
		}
	
		
		var mw = Math.max(   ( el.select.offsetWidth + 22 ), el.offsetWidth + 22  );
			 mw = Math.max(  mw, ( mb.scrollWidth+22) );
		var mh =  mb.options.length * 15  + 8 ; 
			 
		var mx = (ie5)?-3:0;
		var my = el.offsetHeight -2;
		var docH =   document.documentElement.offsetHeight ;
		var bottomH = docH  - el.getBoundingClientRect().bottom ; 

		mh = Math.min(mh, Math.max(( docH - el.getBoundingClientRect().top - 50),100)		);
		
		if(( bottomH < mh) )
		{
			
			mh = Math.max( (bottomH - 12),10);
			if( mh <100 ) 
			{
				my = -100 ;

			}
			mh = Math.max(mh,100);			
		}

		
		self.focus(); 
		
		el.menu.show( mx , my ,  mw, mh , el); 
		sync=null;
		if(mb.options.selected)
		{
			mb.scrollTop = mb.options.selected.offsetTop;
		}
	

		
		
		window.onresize = function(){el.menu.hide()};		
	}

	function switchMenu()
	{
		if(event.keyCode)
		{
			if(event.keyCode==40){ el.contentIndex++ ;}
			else if(event.keyCode==38){ el.contentIndex--; }
		}
		else if(event.wheelDelta )
		{
			if (event.wheelDelta >= 120)
			el.contentIndex++ ;
			else if (event.wheelDelta <= -120)
			el.contentIndex-- ;
		}else{return true;}




		if( el.contentIndex > (el.contentOptions.length-1) ){ el.contentIndex =0;}
		else if (el.contentIndex<0){el.contentIndex = el.contentOptions.length-1 ;}

		var o = document.createElement("option");
			 o.value = el.contentOptions[el.contentIndex].value;
			 o.innerHTML = el.contentOptions[el.contentIndex].text;
			 while(el.options.length>0){el.options[0].removeNode(true);}
			 el.appendChild(o);
			 el.title =  o.innerHTML; 
	}
	
	if(dropdown_menu_hack.menu ==null)
	{
		dropdown_menu_hack.menu =  window.createPopup();
		document.attachEvent("onkeydown",dropdown_menu_hack.menu.hide);
	}
	el.menu = dropdown_menu_hack.menu ;
	el.contentOptions = new Array();
	el.contentIndex = el.selectedIndex;
	el.contentHTML = el.outerHTML;

	for(var i=0;i<el.options.length;i++)
	{	
		el.contentOptions [el.contentOptions.length] = 
		{
			"value": el.options[i].value,
			"text": el.options[i].innerHTML
		}

		if(!el.options[i].selected){el.options[i].removeNode(true);i--;};
	}

	
	el.onkeydown = switchMenu;
	el.onclick = showMenu;
	el.onmousewheel= switchMenu;

}

function check_date(field) {
	var checkstr = "0123456789";
	var DateField = field;
	var DateValue = "";
	var DateTemp = "";
	var seperator = "/";
	var day = 0;
	var month = 0;
	var year = 0;
	var leap = 0;
	var err = 0;
	var i;
		err = 0;
		DateValue = DateField.value;
   		for (i = 0; i < DateValue.length; i++) {
		  	if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
	     		DateTemp = DateTemp + DateValue.substr(i,1);
	  		}
   		}
   		DateValue = DateTemp;
		if (DateValue.length == 8) {
      		//DateValue = DateValue.substr(0,4) + '19' + DateValue.substr(4,2);
      	}
		else {
			alert("Date is incorrect!");
      		DateField.select();
	  		DateField.focus();
			return false;
		}
   		if (DateValue.length != 8) {
      		err = 19;}
  		year = DateValue.substr(4,4);
	   	if (year == 0) {err = 20;}
   		month = DateValue.substr(0,2);
	   	if ((month < 1) || (month > 12)) {err = 21;	}
   		day = DateValue.substr(2,2);
	   	if (day < 1) {err = 22;	}
	   	if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {	leap = 1; }
	   	if ((month == 2) && (leap == 1) && (day > 29)) {err = 23;	}
	   	if ((month == 2) && (leap != 1) && (day > 28)) {	err = 24;  }
	   	if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      		err = 25;
   		}

	   	if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
    	  	err = 26;
	   	}

   		if ((day == 0) && (month == 0) && (year == 00)) {
      		err = 0; day = ""; month = ""; year = ""; seperator = "";
	   	}

	   	if (err == 0) {
    	  	DateField.value = month + seperator + day + seperator + year;
			return true;
   		}
   		else {
      		alert("Date is incorrect!");
      		DateField.select();
	  		DateField.focus();
			return false;
   		}
}