/*
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.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;
}

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");
}

function syncSelected(obj,correspondingobj){
	if(obj.type == "checkbox"){
		if(obj.checked){
			if(!(parseInt(correspondingobj.value) >= 1))
				correspondingobj.value = 1;
		}else
			correspondingobj.value = 0;
	}else{
		if(isNaN(obj.value)){
			obj.value = 1;
			correspondingobj.checked = true;
		}else{
			if(obj.value > 0)
				correspondingobj.checked = true;
			else
				correspondingobj.checked = false;
		}
	}
}