/*
This file contains all commonly used functions by Solid Cactus
version: 1.0
last modified: Sunil Lukose
modified date: 08/31/06
*/

/*
1. SC.init() - call all functions that needs to be initialized in this function
Notes: This function should be called just before the end </body> tag

2. SC.getTagArray - Returns array of elements
Syntax - SC.getTagArray(sTagName,oDomElement)
e.g. - SC.getTagArray("a",oObj);
Notes: if domEement is not supplied it defaults to document

3. SC.setAttributes - sets attributes on a dom Element
Syntax - SC.setAttributes(oDomElement,sAttrList)
e.g. - SC.getTagArray(oObj,"width","500px","top","0");
NOTES: attributes should be in name value pair you can pass one or more sets of attributes

4. SC.getObj(sObjId) - Returns a object for the passed ID
e.g. - SC.getObj("contetns");
*/

function SC() {};

var SCClass=SC.prototype;
var scClass = new SC();

SC.setCookie = function(data, path, expires){
	document.cookie = data + "; path=" + path + "; " + expires;
}

SC.getCookie = function(cookieName){
	if (document.cookie.length > 0){
		cookieBegin = document.cookie.indexOf(cookieName + "=");
		if (cookieBegin != -1){ 
			cookieBegin = cookieBegin + cookieName.length + 1; 
			cookieEnd = document.cookie.indexOf(";",cookieBegin);
			if (cookieEnd == -1) cookieEnd = document.cookie.length;
			return unescape(document.cookie.substring(cookieBegin, cookieEnd));
		} 
	}
	return "";
}

SC.returnExpires = function(addTime){
	var curDate = new Date;
	var newDate = new Date( curDate.getTime() + addTime );
	return "expires=" + newDate.toGMTString() + "; "
}

SC.getTagArray = function(sTagName,oObj){
	aTagArray = null;
	oObj = (oObj) ? oObj : document;
	if(oObj.getElementsByTagName)
		aTagArray = oObj.getElementsByTagName(sTagName);
	return aTagArray;
}

SC.setAttributes = function(oObj){
	if(arguments.length > 1){
		for(var i=1; i<arguments.length; i=i+2){
			oObj[arguments[i]] = arguments[i+1];
		}
	}
}

SC.getObj = function(sObjId){ 
	var oObj = null;
  if (document.getElementById) 
      oObj = document.getElementById(sObjId);
  else if (document.all) 
      oObj = document.all[sObjId];
  else if (document.layers) 
      oObj = document.layers[sObjId];
	return oObj;
}

SC.createElement = function(sTagName){
	var oObj = document.createElement(sTagName);
	if(arguments.length > 1){
		for(var i=1; i<arguments.length; i=i+2){
			if(arguments[i] == "txt"){
				var oText = document.createTextNode(arguments[i+1]);
				oObj.appendChild(oText);		
			}else
				oObj[arguments[i]] = arguments[i+1];
		}
	}
	return oObj;
}

SC.appendElement = function(oAppendTo){
	var oAppendTo = (oAppendTo) ? oAppendTo : document.body;
	if(arguments.length > 1){
		for(var i=1; i<arguments.length; i++)
			oAppendTo.appendChild(arguments[i]);
	}
}

SC.createAppend = function(sTagName,oObj){
	var oObj = (oObj)? oObj : document.body;
	var oTagObj = document.createElement(sTagName);
	if(arguments.length > 2){
		for(var i=2; i<arguments.length; i=i+2){
			oTagObj[arguments[i]] = arguments[i+1];
		}
	}	
	oObj.appendChild(oTagObj);
}

SC.showHide = function(oObj,sDisplay){
	oObj.style.display = sDisplay;
}

SC.addListener = function() {
    if ( window.addEventListener ) {
        return function(el, type, fn) {
            el.addEventListener(type, fn, false);
        };
    } else if ( window.attachEvent ) {
        return function(el, type, fn) {
            var f = function() {
                fn.call(el, window.event);
            };
            el.attachEvent('on'+type, f);
        };
    } else {
        return function(el, type, fn) {
            el['on'+type] = fn;
        }
    }
}();

SC.removeListener = function() {
    if ( window.addEventListener ) {
        return function(el, type, fn) {
            el.removeEventListener(type, fn, false);
        };
    } else if ( window.attachEvent ) {
        return function(el, type, fn) {
            var f = function() {
                fn.call(el, window.event);
            };
            el.detachEvent('on'+type, f);
        };
    } else {
        return function(el, type, fn) {
            el['on'+type] = null;
        }
    }
}();

String.prototype.trim=function() {
	if (this==null) return null;
	return this.trimEnd(this.trimStart());
}

String.prototype.trimEnd=function() {
	if (this==null) return null;
	var re = /((\s*\S+)*)\s*/;
	return this.replace(re, "$1");	
}

String.prototype.trimStart=function() {
	if (this==null) return null;
	var re = /\s*((\S+\s*)*)/;
	return this.replace(re, "$1");
}
