
/** 
 * Array 
 * Additional prototype methods for Array object.
 * Some of them are unnecessary since Array object
 * already supports them.
 * It will be removed in further versions of the API.
 */
_this=Array.prototype;
if(!_this.push){
	_this.push=function(){
		for(var i=0;i<arguments.length;i++)
			this[this.length]=arguments[i];
		return this.length;
	};
}
if(!_this.pop){
	_this.pop=function(){
		var lastElement=this[this.length-1];
		this.length=Math.max(this.length-1,0);
		return lastElement;
	};
}
if(!_this.copy){
	_this.copy=function(){
		var arcopy=new Array();
		for (var i=0;i<this.length;i++)
			arcopy[i]=this[i];
		return arcopy;
	};
}
if(!_this.indexOf){
	_this.indexOf=function(elm){
		for(var i=0;i<this.length;i++)
			if(elm==this[i])
				return i;
		return -1;
	};
}
if(!_this.contains){
	_this.contains=function(elm){
		return this.indexOf(elm)!=-1;
	};
}

/** 
 * Validator 
 * Basically used for html form validations.
 */
_this=Validator.prototype;
function Validator(){}
_this.isDefined=function(x){
	return typeof(x)!="undefined";
};	
_this.isEmpty=function(x){
	if(!this.isDefined(x))
		return false;
	if(this.isNumeric(x))
		return false;
	if(x=="")
		return true;
	return false;
};
_this.isEmail=function(x){
	if(!this.isDefined(x))
		return false;
	return /^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/ig.test(x);
};
_this.isWhiteSpace=function(x){
	if(!this.isDefined(x)) 
		return false;
	return /^\s*$/.test(x);
};
_this.isInteger=function(x){
	if(!this.isDefined(x))
		return false;
	x=(""+x).replace(/\.0*$/g,"");
	var intNumber=parseInt(x);
	if(!isNaN(intNumber)){
		x=x.remove(/^0*/);
		if(x=="")x=0;
	}
	intNumber=parseInt(x);
	if((!isNaN(intNumber))&&((""+intNumber)==x))
		return true;
	return false;
};
_this.isFloat=function(x){
	if(!this.isDefined(x))
		return false;
	x=(""+x).replace(/,/g,".");
	return x!=""&&!isNaN(x);
};
_this.isNumeric=function(x){
	return this.isFloat(x);
};
_this.isString=function(x){
	if(!this.isDefined(x))
		return false;
	return typeof(x)=="string";
};
_this.isDate=function(intYear,intMonth,intDay){
	if(!this.isDefined(intYear)||!this.isDefined(intMonth)||!this.isDefined(intDay)||
			this.isEmpty(intYear)||this.isEmpty(intMonth)||this.isEmpty(intDay))
		return false;
	var arMonth=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if((parseInt(intYear)%4==0&&parseInt(intYear)%100!=0)||parseInt(intYear%400)==0)
		arMonth[1]=29;
	else 
		arMonth[1]=28;
	var intMaxDay=arMonth[parseInt(intMonth)-1];
	if(parseInt(intDay)>intMaxDay)
		return false;return true;
};
_this.isPositive=function(x){
	if(!this.isNumeric(x))
		return false;
	return parseFloat(x)>=0;
};
_this.isPositiveStrict=function(x){
	if(!this.isNumeric(x))
		return false;
	return parseFloat(x)>0;
};
_this.isNegative=function(x){
	if(!this.isNumeric(x))
		return false;
	return parseFloat(x)<=0;
};
_this.isNegativeStrict=function(x){
	if(!this.isNumeric(x))
		return false;
	return parseFloat(x)<0;
};


/** 
 * String 
 * Additional String prototype methods.
 */
_this=String.prototype;
_this.trim=function(blnIgnoreCarriage,blnIgnoreInnerWhiteSpace){
	var temp=this.replace(/^\s*/,"");
	temp=temp.replace(/\s*$/,"");
	blnIgnoreCarriage=blnIgnoreCarriage?true:false;
	blnIgnoreInnerWhiteSpace=blnIgnoreInnerWhiteSpace?true:false;
	if(blnIgnoreCarriage&&blnIgnoreInnerWhiteSpace)
		;
	else if(blnIgnoreCarriage&&!blnIgnoreInnerWhiteSpace){
		temp=temp.replace(/\t+/g," ");
		temp=temp.replace(/ +/g," ");
	}
	else if(!blnIgnoreCarriage&&blnIgnoreInnerWhiteSpace)
		temp=temp.replace(/(\r\n|\n\r|\n|\r)+/g,"");
	else if(!blnIgnoreCarriage&&!blnIgnoreInnerWhiteSpace)
		temp=temp.replace(/\s+/g," ");
	if(temp==" ")temp="";
	return temp;
};
_this.remove=function(varRegExp,strOption){
	var regEx;
	if(new Validator().isString(varRegExp))
		regEx=new RegExp(varRegExp,strOption?strOption:"g");
	else regEx=varRegExp;	
	return this.replace(regEx,"");
};
_this.removeTags=function(){
	return this.replace(/<[\/]?([a-zA-Z0-9]+)[^>^<]*>/ig,"");
};

/** 
 * CBObject 
 * Wraps document.getElementById and
 * provides an additional object check.
 * It accepts the object itself or its DHTML index
 * as per the paremeter to the constructor.
 */
_this=CBObject.prototype;
function CBObject(elmID){
	this._val=new Validator();
	this._obj=this._getObject(elmID);
}
_this.exists=function(){
	return this.getObject()!=null;
};
_this.getObject=function(){
	return this._obj;
};
_this.getID=function(){
	if(this.exists())
		return this.getObject().id;
	else return null;
};
_this._getObject=function(elmID){
	if(!this._val.isString(elmID))
		return elmID;
	else return document.getElementById(elmID);
};

/** 
 * CoookieManager 
 * Sets/gets cookies.
 */
_this=CookieManager.prototype;
function CookieManager(){}
_this.set=function(strName,strValue,intDays){
	var strExpires="",dtDate=new Date();
	if(!new Validator().isDefined(document.cookie))
		return;
	if(intDays){
		dtDate.setTime(dtDate.getTime()+(intDays*24*60*60*1000));
		strExpires="; expires="+dtDate.toGMTString();
	}
	else 
		strExpires="";
	document.cookie=strName+"="+strValue+strExpires+"; path=/";
};
_this.get=function(strName){
	if(!document.cookie)
		return null;
	var strSeek=strName+"=",strCookie="",aryCookie=document.cookie.split(";");
	for(var i=0;i<aryCookie.length;i++){
		strCookie=aryCookie[i];
		while(strCookie.charAt(0)==" ")
			strCookie=strCookie.substring(1,strCookie.length);
		if(strCookie.indexOf(strSeek)==0)
			return strCookie.substring(strSeek.length, strCookie.length);
	}
	return null;
};

/** 
 * StyleManager 
 * Sets/gets/remembers styles/stylesheets.
 */
_this=StyleManager.prototype;
function StyleManager(){
	this._cm=new CookieManager();
	this._val=new Validator();
}
_this.getStyle=function(elmID,cssProperty,cssPropertyExtended){
	var obj=new CBObject(elmID).getObject();
	if(!this.sanityCheck(obj,cssProperty))
		return null;
	if(obj.currentStyle)
		return obj.currentStyle[cssProperty];
	if(window.getComputedStyle)
		return window.getComputedStyle(obj,"").getPropertyValue(cssPropertyExtended);
	if(this._val.isDefined(obj.style))
		return eval("obj.style."+cssProperty);
};
_this.setStyle=function(elmID,cssProperty,value){
	var obj=new CBObject(elmID).getObject();
	if(!this.sanityCheck(obj,cssProperty))
		return;
	eval("obj.style."+cssProperty+"='"+value+"';");
};
_this.setClass=function(elmID,cssClass){
	new CBObject(elmID).getObject().className=cssClass;
};
_this.getClass=function(elmID){
	return new CBObject(elmID).getObject().className;
};
_this.activateAlternateStyleSheet=function(strTitle){
	var objLink=null;
	for(var i=0;true;i++){
		if(!document.getElementsByTagName("link")[i])
			break;
		objLink=document.getElementsByTagName("link")[i];
		if(objLink.getAttribute("rel").indexOf("style")!=-1&&objLink.getAttribute("title"))
			objLink.disabled=true;
		if(objLink.getAttribute("title")==strTitle)
			objLink.disabled=false;
	}
};
_this.remember=function(strTitle){
	this._cm.set("alternateCSS",strTitle,14);
};
_this.recall=function(){
	var strTitle=this._cm.get("alternateCSS");
	if(strTitle)
		this.activateAlternateStyleSheet(strTitle);
};
_this.sanityCheck=function(obj,cssProperty){
	return new Validator().isDefined(eval("obj.style."+cssProperty));
};




/** 
 * LayerObject
 * Wrapper for a DHTML Layer.
 */
LayerObject.prototype=new CBObject();
_this=LayerObject.prototype;
function LayerObject(elmID){
	this._obj=new CBObject(elmID).getObject();
	this._sm=new StyleManager();
}
_this.setStyle=function(strSelector,strValue){
	var obj=null;
	if(!(obj=this.getObject()))
		return;
	this._sm.setStyle(obj,strSelector,strValue);
};
_this.getStyle=function(strSelector,strSelectorExtended){
	var obj=null;
	if(!(obj=this.getObject()))
		return;
	return this._sm.getStyle(obj,strSelector,strSelectorExtended);
};
_this.isVisible=function(){
	return this.getStyle("visibility","visibility")=="visible"&&this.getStyle("display","display")!="none";
};
_this.show=function(){
	this.setStyle("visibility","visible");
};
_this.hide=function(){
	this.setStyle("visibility","hidden");
};
_this.collapse=function(){
	this.setStyle("display","none");
};
_this.expand=function(){
	this.setStyle("display","block");
};
_this.changeContent=function(strNewHTML){
	var obj=null;
	if(!(obj=this.getObject()))
		return;
	if(obj.innerHTML)
		obj.innerHTML=strNewHTML;
};
_this.addContentBefore=function(strHTML){
	var obj=null;
	if(!(obj=this.getObject()))
		return;
	if(obj.innerHTML)
		this.changeContent(strHTML+obj.innerHTML);
};
_this.addContentAfter=function(strHTML){
	var obj=null;
	if(!(obj=this.getObject()))
		return;
	if(obj.innerHTML)
		this.changeContent(obj.innerHTML+strHTML);
};

